def test_should_send_unicode_message(self, kp):
        instance = log_publisher.LogPublisher()
        instance._kafka_publisher = kp

        for um in base.UNICODE_MESSAGES:
            case, msg = um.values()
            try:
                envelope = model.Envelope(log={
                    'message': msg,
                    'application_type': 'test',
                    'dimensions': {
                        'test': 'test_log_publisher',
                        'case': 'test_should_send_unicode_message'
                    }
                },
                                          meta={'tenantId': 1})
                instance.send_message(envelope)

                expected_message = json.dumps(envelope, ensure_ascii=False)

                if six.PY3:
                    expected_message = expected_message.encode('utf-8')

                instance._kafka_publisher.publish.assert_called_with(
                    cfg.CONF.kafka.logs_topics[0], [expected_message])
            except Exception:
                LOG.exception('Failed to evaluate unicode case %s', case)
                raise
    def test_should_send_message(self, kafka_producer):
        instance = log_publisher.LogPublisher()
        instance._kafka_publisher = kafka_producer
        instance.send_message({})

        creation_time = ((datetime.datetime.utcnow() -
                          EPOCH_START).total_seconds())
        application_type = 'monasca-log-api'
        dimension_1_name = 'disk_usage'
        dimension_1_value = '50'
        dimension_2_name = 'cpu_time'
        dimension_2_value = '60'

        msg = model.Envelope(log={
            'message': '1',
            'application_type': application_type,
            'dimensions': {
                dimension_1_name: dimension_1_value,
                dimension_2_name: dimension_2_value
            }
        },
                             meta={'tenantId': '1'})
        msg['creation_time'] = creation_time
        instance.send_message(msg)

        instance._kafka_publisher.publish.assert_called_once_with(
            cfg.CONF.kafka.logs_topics[0],
            [json.dumps(msg, ensure_ascii=False).encode('utf-8')])
    def test_should_send_message_multiple_topics(self, _):
        topics = ['logs_topics', 'analyzer', 'tester']
        self.conf_override(logs_topics=topics, group='kafka')
        self.conf_override(max_message_size=5000, group='log_publisher')

        instance = log_publisher.LogPublisher()
        instance._kafka_publisher = mock.Mock()
        instance.send_message({})

        creation_time = ((datetime.datetime.utcnow() -
                          EPOCH_START).total_seconds())
        dimension_1_name = 'disk_usage'
        dimension_1_value = '50'
        dimension_2_name = 'cpu_time'
        dimension_2_value = '60'
        application_type = 'monasca-log-api'
        msg = model.Envelope(log={
            'message': '1',
            'application_type': application_type,
            'dimensions': {
                dimension_1_name: dimension_1_value,
                dimension_2_name: dimension_2_value
            }
        },
                             meta={'tenantId': '1'})
        msg['creation_time'] = creation_time
        json_msg = json.dumps(msg, ensure_ascii=False)

        instance.send_message(msg)

        self.assertEqual(len(topics),
                         instance._kafka_publisher.publish.call_count)
        for topic in topics:
            instance._kafka_publisher.publish.assert_any_call(
                topic, [json_msg.encode('utf-8')])