Search in sources :

Example 11 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedTelemetrySenderTest method testSendTelemetryCreatesCorrectRecord.

/**
 * Verifies that the Kafka record is created as expected when sending telemetry data.
 *
 * @param qos The quality of service used for sending the message.
 * @param expectedTtl The ttl expected in the message.
 * @param ctx The vert.x test context.
 */
@ParameterizedTest
@CsvSource(value = { "AT_MOST_ONCE,10000", "AT_LEAST_ONCE,60000" })
public void testSendTelemetryCreatesCorrectRecord(final QoS qos, final long expectedTtl, final VertxTestContext ctx) {
    // GIVEN a telemetry sender
    final String payload = "the-payload";
    final String contentType = "text/plain";
    final Map<String, Object> properties = Map.of("foo", "bar");
    final var spanFinished = ctx.checkpoint();
    final var messageHasHeaders = ctx.checkpoint();
    final var span = TracingMockSupport.mockSpan();
    doAnswer(invocation -> {
        spanFinished.flag();
        return null;
    }).when(span).finish();
    final var tracer = TracingMockSupport.mockTracer(span);
    final var mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final var factory = CachingKafkaProducerFactory.testFactory(vertxMock, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
    final var sender = new KafkaBasedTelemetrySender(vertxMock, factory, kafkaProducerConfig, true, tracer);
    tenant.setResourceLimits(new ResourceLimits().setMaxTtlTelemetryQoS0(10L).setMaxTtlTelemetryQoS1(60L));
    // WHEN sending telemetry data
    sender.sendTelemetry(tenant, device, qos, contentType, Buffer.buffer(payload), properties, null).onComplete(ctx.succeeding(t -> {
        ctx.verify(() -> {
            // THEN the producer record is created from the given values...
            final var producerRecord = mockProducer.history().get(0);
            assertThat(producerRecord.key()).isEqualTo(device.getDeviceId());
            assertThat(producerRecord.topic()).isEqualTo(new HonoTopic(HonoTopic.Type.TELEMETRY, tenant.getTenantId()).toString());
            assertThat(producerRecord.value().toString()).isEqualTo(payload);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), "foo", "bar");
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), MessageHelper.SYS_HEADER_PROPERTY_TTL, expectedTtl);
            // ...AND contains the standard headers
            KafkaClientUnitTestHelper.assertStandardHeaders(producerRecord, device.getDeviceId(), contentType, qos.ordinal());
        });
        messageHasHeaders.flag();
    }));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) CsvSource(org.junit.jupiter.params.provider.CsvSource) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) HashMap(java.util.HashMap) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) KafkaClientUnitTestHelper(org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper) ResourceLimits(org.eclipse.hono.util.ResourceLimits) Tracer(io.opentracing.Tracer) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Buffer(io.vertx.core.buffer.Buffer) QoS(org.eclipse.hono.util.QoS) MockProducer(org.apache.kafka.clients.producer.MockProducer) Mockito.mock(org.mockito.Mockito.mock) ResourceLimits(org.eclipse.hono.util.ResourceLimits) TenantObject(org.eclipse.hono.util.TenantObject) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedCommandConsumerFactoryImplIT method getOneWayCommandRecord.

private static KafkaProducerRecord<String, Buffer> getOneWayCommandRecord(final String tenantId, final String deviceId, final String subject) {
    final List<KafkaHeader> headers = List.of(KafkaRecordHelper.createDeviceIdHeader(deviceId), KafkaRecordHelper.createSubjectHeader(subject));
    final String commandTopic = new HonoTopic(HonoTopic.Type.COMMAND, tenantId).toString();
    final KafkaProducerRecord<String, Buffer> record = KafkaProducerRecord.create(commandTopic, deviceId, Buffer.buffer(subject + "_payload"));
    record.addHeaders(headers);
    return record;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader)

Example 13 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedCommandConsumerFactoryImplIT method getInternalCommandConsumer.

private HonoKafkaConsumer getInternalCommandConsumer(final Handler<KafkaConsumerRecord<String, Buffer>> recordHandler) {
    final Map<String, String> consumerConfig = IntegrationTestSupport.getKafkaConsumerConfig().getConsumerConfig("internal_cmd_consumer_test");
    consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, UUID.randomUUID().toString());
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND_INTERNAL, adapterInstanceId).toString();
    final HonoKafkaConsumer honoKafkaConsumer = new HonoKafkaConsumer(vertx, Set.of(topic), recordHandler, consumerConfig);
    componentsToStopAfterTest.add(honoKafkaConsumer);
    topicsToDeleteAfterTest.add(topic);
    return honoKafkaConsumer;
}
Also used : HonoKafkaConsumer(org.eclipse.hono.client.kafka.consumer.HonoKafkaConsumer) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic)

Example 14 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedMappingAndDelegatingCommandHandlerTest method getCommandRecord.

@SuppressWarnings("unchecked")
private KafkaConsumerRecord<String, Buffer> getCommandRecord(final String tenantId, final String deviceId, final String subject, final int partition, final long offset) {
    final List<KafkaHeader> headers = new ArrayList<>();
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND, tenantId).toString();
    final KafkaConsumerRecord<String, Buffer> consumerRecord = mock(KafkaConsumerRecord.class);
    Optional.ofNullable(deviceId).ifPresent(ok -> headers.add(KafkaRecordHelper.createDeviceIdHeader(deviceId)));
    Optional.ofNullable(subject).ifPresent(ok -> headers.add(KafkaRecordHelper.createSubjectHeader(subject)));
    when(consumerRecord.headers()).thenReturn(headers);
    when(consumerRecord.topic()).thenReturn(topic);
    when(consumerRecord.key()).thenReturn(deviceId);
    when(consumerRecord.partition()).thenReturn(partition);
    when(consumerRecord.offset()).thenReturn(offset);
    return consumerRecord;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader)

Example 15 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedCommandResponseSenderTest method testIfValidCommandResponseKafkaRecordIsSent.

@Test
void testIfValidCommandResponseKafkaRecordIsSent(final VertxTestContext ctx, final Vertx vertx) {
    // GIVEN a command response sender
    final String tenantId = "test-tenant";
    final String deviceId = "test-device";
    final String correlationId = UUID.randomUUID().toString();
    final String contentType = "text/plain";
    final String payload = "the-payload";
    final int status = 200;
    final String additionalHeader1Name = "testHeader1";
    final String additionalHeader1Value = "testHeader1Value";
    final String additionalHeader2Name = "testHeader2";
    final String additionalHeader2Value = "testHeader2Value";
    final Map<String, Object> additionalProperties = Map.of(additionalHeader1Name, additionalHeader1Value, additionalHeader2Name, additionalHeader2Value);
    final CommandResponse commandResponse = CommandResponse.fromAddressAndCorrelationId(String.format("%s/%s/%s", CommandConstants.COMMAND_RESPONSE_ENDPOINT, tenantId, Commands.getDeviceFacingReplyToId("", deviceId, MessagingType.kafka)), correlationId, Buffer.buffer(payload), contentType, status);
    commandResponse.setAdditionalProperties(additionalProperties);
    final Span span = TracingMockSupport.mockSpan();
    final Tracer tracer = TracingMockSupport.mockTracer(span);
    final var mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final var factory = CachingKafkaProducerFactory.testFactory(vertx, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
    final var sender = new KafkaBasedCommandResponseSender(vertx, factory, kafkaProducerConfig, tracer);
    final TenantObject tenant = TenantObject.from(tenantId);
    tenant.setResourceLimits(new ResourceLimits().setMaxTtlCommandResponse(10L));
    // WHEN sending a command response
    sender.sendCommandResponse(tenant, new RegistrationAssertion(deviceId), commandResponse, NoopSpan.INSTANCE.context()).onComplete(ctx.succeeding(t -> {
        ctx.verify(() -> {
            // THEN the producer record is created from the given values...
            final ProducerRecord<String, Buffer> record = mockProducer.history().get(0);
            assertThat(record.key()).isEqualTo(deviceId);
            assertThat(record.topic()).isEqualTo(new HonoTopic(HonoTopic.Type.COMMAND_RESPONSE, tenantId).toString());
            assertThat(record.value().toString()).isEqualTo(payload);
            // Verify if the record contains the necessary headers.
            final Headers headers = record.headers();
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, MessageHelper.SYS_PROPERTY_CORRELATION_ID, correlationId);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, MessageHelper.APP_PROPERTY_STATUS, status);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, MessageHelper.SYS_PROPERTY_CONTENT_TYPE, contentType);
            final var creationTimeHeader = headers.headers(MessageHelper.SYS_PROPERTY_CREATION_TIME);
            assertThat(creationTimeHeader).hasSize(1);
            final Long creationTimeMillis = Json.decodeValue(Buffer.buffer(creationTimeHeader.iterator().next().value()), Long.class);
            assertThat(creationTimeMillis).isGreaterThan(0L);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, MessageHelper.SYS_HEADER_PROPERTY_TTL, 10000L);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, additionalHeader1Name, additionalHeader1Value);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(headers, additionalHeader2Name, additionalHeader2Value);
            verify(span).finish();
        });
        ctx.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Json(io.vertx.core.json.Json) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Headers(org.apache.kafka.common.header.Headers) HashMap(java.util.HashMap) Commands(org.eclipse.hono.client.command.Commands) MessagingType(org.eclipse.hono.util.MessagingType) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) CommandConstants(org.eclipse.hono.util.CommandConstants) KafkaClientUnitTestHelper(org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) ResourceLimits(org.eclipse.hono.util.ResourceLimits) Tracer(io.opentracing.Tracer) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Mockito.verify(org.mockito.Mockito.verify) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) NoopSpan(io.opentracing.noop.NoopSpan) Mockito.mock(org.mockito.Mockito.mock) Tracer(io.opentracing.Tracer) Headers(org.apache.kafka.common.header.Headers) CommandResponse(org.eclipse.hono.client.command.CommandResponse) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Span(io.opentracing.Span) NoopSpan(io.opentracing.noop.NoopSpan) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) ResourceLimits(org.eclipse.hono.util.ResourceLimits) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test)

Aggregations

HonoTopic (org.eclipse.hono.client.kafka.HonoTopic)35 Buffer (io.vertx.core.buffer.Buffer)23 Test (org.junit.jupiter.api.Test)17 KafkaHeader (io.vertx.kafka.client.producer.KafkaHeader)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 Span (io.opentracing.Span)8 VertxTestContext (io.vertx.junit5.VertxTestContext)8 Vertx (io.vertx.core.Vertx)7 VertxExtension (io.vertx.junit5.VertxExtension)7 Future (io.vertx.core.Future)6 Promise (io.vertx.core.Promise)6 DownstreamMessage (org.eclipse.hono.application.client.DownstreamMessage)6 CachingKafkaProducerFactory (org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory)6 MessagingKafkaProducerConfigProperties (org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties)6 MessageHelper (org.eclipse.hono.util.MessageHelper)6 Tracer (io.opentracing.Tracer)5 NoopSpan (io.opentracing.noop.NoopSpan)5 HttpURLConnection (java.net.HttpURLConnection)5