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();
}));
}
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;
}
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;
}
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;
}
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();
}));
}
Aggregations