Search in sources :

Example 11 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaBasedCommandTest method testFromRoutedCommandRecordFailsForMissingTenantId.

/**
 * Verifies that a command cannot be created from a record that doesn't contain a <em>tenant_id</em> header
 * if the record is parsed as a command forwarded by the Command Router.
 */
@Test
public void testFromRoutedCommandRecordFailsForMissingTenantId() {
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND_INTERNAL, "myAdapterInstanceId").toString();
    final String correlationId = "the-correlation-id";
    final String gatewayId = "gw-1";
    final String targetDeviceId = "4711";
    final String subject = "doThis";
    final List<KafkaHeader> headers = new ArrayList<>(getHeaders(targetDeviceId, subject, correlationId));
    headers.add(KafkaRecordHelper.createViaHeader(gatewayId));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, targetDeviceId, headers);
    assertThrows(IllegalArgumentException.class, () -> {
        KafkaBasedCommand.fromRoutedCommandRecord(commandRecord);
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test)

Example 12 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaBasedCommandTest method testFromRecordFailsForRecordWithoutSubject.

/**
 * Verifies that a command cannot be created from a record that doesn't contain a <em>subject</em> header.
 */
@Test
public void testFromRecordFailsForRecordWithoutSubject() {
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND, Constants.DEFAULT_TENANT).toString();
    final String deviceId = "4711";
    final List<KafkaHeader> headers = List.of(KafkaRecordHelper.createDeviceIdHeader(deviceId));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, headers);
    final KafkaBasedCommand command = KafkaBasedCommand.from(commandRecord);
    assertFalse(command.isValid());
    assertThat(command.getInvalidCommandReason()).contains("subject");
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test)

Example 13 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaBasedCommandTest method testFromRecordFailsForMissingCorrelationIdWithResponseRequired.

/**
 * Verifies that a valid command cannot be created from a record that has the <em>response-required</em>
 * header set to "true" but has no <em>correlation-id</em> header.
 */
@Test
public void testFromRecordFailsForMissingCorrelationIdWithResponseRequired() {
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND, Constants.DEFAULT_TENANT).toString();
    final String deviceId = "4711";
    final String subject = "doThis";
    final List<KafkaHeader> headers = new ArrayList<>(getHeaders(deviceId, subject));
    headers.add(KafkaRecordHelper.createResponseRequiredHeader(true));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, headers);
    final KafkaBasedCommand cmd = KafkaBasedCommand.from(commandRecord);
    assertFalse(cmd.isValid());
    assertThat(cmd.getInvalidCommandReason()).contains("correlation-id");
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test)

Example 14 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaBasedCommandTest method testGetDeliveryFailureNotificationProperties.

/**
 * Verifies the return value of getDeliveryFailureNotificationProperties().
 */
@Test
public void testGetDeliveryFailureNotificationProperties() {
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND, Constants.DEFAULT_TENANT).toString();
    final String correlationId = "the-correlation-id";
    final String deviceId = "4711";
    final String subject = "doThis";
    final List<KafkaHeader> headers = new ArrayList<>(getHeaders(deviceId, subject, correlationId));
    headers.add(KafkaHeader.header(KafkaBasedCommand.DELIVERY_FAILURE_NOTIFICATION_METADATA_PREFIX, "v1"));
    headers.add(KafkaHeader.header(KafkaBasedCommand.DELIVERY_FAILURE_NOTIFICATION_METADATA_PREFIX, "toBeIgnored"));
    headers.add(KafkaHeader.header(KafkaBasedCommand.DELIVERY_FAILURE_NOTIFICATION_METADATA_PREFIX + "-title", "v2"));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, headers);
    final KafkaBasedCommand cmd = KafkaBasedCommand.from(commandRecord);
    final Map<String, String> deliveryFailureNotificationProperties = cmd.getDeliveryFailureNotificationProperties();
    assertThat(deliveryFailureNotificationProperties.size()).isEqualTo(2);
    assertThat(deliveryFailureNotificationProperties.get(KafkaBasedCommand.DELIVERY_FAILURE_NOTIFICATION_METADATA_PREFIX)).isEqualTo("v1");
    assertThat(deliveryFailureNotificationProperties.get(KafkaBasedCommand.DELIVERY_FAILURE_NOTIFICATION_METADATA_PREFIX + "-title")).isEqualTo("v2");
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test)

Example 15 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaCommandProcessingQueueTest method getTestCommandContext.

@SuppressWarnings("unchecked")
private KafkaBasedCommandContext getTestCommandContext(final int offset) {
    final String deviceId = "deviceId";
    final List<KafkaHeader> headers = new ArrayList<>(List.of(KafkaRecordHelper.createDeviceIdHeader(deviceId), KafkaRecordHelper.createSubjectHeader("subject_" + offset), KafkaRecordHelper.createCorrelationIdHeader("correlationId")));
    final KafkaConsumerRecord<String, Buffer> consumerRecord = mock(KafkaConsumerRecord.class);
    when(consumerRecord.headers()).thenReturn(headers);
    when(consumerRecord.topic()).thenReturn(topic);
    when(consumerRecord.key()).thenReturn(deviceId);
    when(consumerRecord.offset()).thenReturn((long) offset);
    when(consumerRecord.partition()).thenReturn(0);
    final KafkaBasedCommand cmd = KafkaBasedCommand.from(consumerRecord);
    return new KafkaBasedCommandContext(cmd, mock(CommandResponseSender.class), mock(Span.class)) {

        @Override
        public String toString() {
            return "Command " + offset;
        }
    };
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) ArrayList(java.util.ArrayList) KafkaBasedCommand(org.eclipse.hono.client.command.kafka.KafkaBasedCommand) KafkaBasedCommandContext(org.eclipse.hono.client.command.kafka.KafkaBasedCommandContext) Span(io.opentracing.Span) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader)

Aggregations

KafkaHeader (io.vertx.kafka.client.producer.KafkaHeader)15 Buffer (io.vertx.core.buffer.Buffer)14 Test (org.junit.jupiter.api.Test)11 ArrayList (java.util.ArrayList)10 HonoTopic (org.eclipse.hono.client.kafka.HonoTopic)10 Span (io.opentracing.Span)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 SpanContext (io.opentracing.SpanContext)2 Tracer (io.opentracing.Tracer)2 CommandContext (org.eclipse.hono.client.command.CommandContext)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Configuration (io.jaegertracing.Configuration)1 References (io.opentracing.References)1 Tags (io.opentracing.tag.Tags)1 Future (io.vertx.core.Future)1 EncodeException (io.vertx.core.json.EncodeException)1 Json (io.vertx.core.json.Json)1 HealthCheckHandler (io.vertx.ext.healthchecks.HealthCheckHandler)1 Status (io.vertx.ext.healthchecks.Status)1 KafkaProducer (io.vertx.kafka.client.producer.KafkaProducer)1