Search in sources :

Example 6 with KafkaHeader

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

the class KafkaBasedCommandTest method testFromRecordFailsForRecordWithWrongKey.

/**
 * Verifies that a command cannot be created from a record that doesn't have the <em>device_id</em> header
 * value as key.
 */
@Test
public void testFromRecordFailsForRecordWithWrongKey() {
    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, "other_key", headers);
    assertThrows(IllegalArgumentException.class, () -> {
        KafkaBasedCommand.from(commandRecord);
    });
}
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 7 with KafkaHeader

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

the class KafkaBasedInternalCommandConsumerTest method testHandleCommandMessageWithInvalidRecord.

/**
 * Verifies that the consumer handles a command record with missing subject by invoking the matching handler.
 */
@Test
void testHandleCommandMessageWithInvalidRecord() {
    // command record with missing subject header
    final String tenantId = "myTenant";
    final String deviceId = "4711";
    final List<KafkaHeader> headers = List.of(KafkaRecordHelper.createTenantIdHeader(tenantId), KafkaRecordHelper.createDeviceIdHeader(deviceId), KafkaRecordHelper.createOriginalPartitionHeader(0), KafkaRecordHelper.createOriginalOffsetHeader(0L));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(deviceId, headers);
    final Handler<CommandContext> commandHandler = VertxMockSupport.mockHandler();
    commandHandlers.putCommandHandler(tenantId, deviceId, null, commandHandler, context);
    internalCommandConsumer.handleCommandMessage(commandRecord);
    final ArgumentCaptor<CommandContext> commandContextCaptor = ArgumentCaptor.forClass(CommandContext.class);
    verify(commandHandler).handle(commandContextCaptor.capture());
    assertThat(commandContextCaptor.getValue()).isNotNull();
    // assert that command is not valid
    assertThat(commandContextCaptor.getValue().getCommand().isValid()).isFalse();
    assertThat(commandContextCaptor.getValue().getCommand().getInvalidCommandReason()).contains("subject");
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CommandContext(org.eclipse.hono.client.command.CommandContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with KafkaHeader

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

the class KafkaBasedInternalCommandConsumerTest method testHandleCommandMessageWithHandlerForGateway.

/**
 * Verifies that the consumer handles a valid message, targeted at a gateway, by invoking the matching command
 * handler.
 */
@Test
void testHandleCommandMessageWithHandlerForGateway() {
    final String tenantId = "myTenant";
    final String deviceId = "4711";
    final String gatewayId = "gw-1";
    final String subject = "subject";
    final List<KafkaHeader> headers = new ArrayList<>(getHeaders(tenantId, deviceId, subject, 0L));
    headers.add(KafkaRecordHelper.createViaHeader(gatewayId));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(deviceId, headers);
    final Handler<CommandContext> commandHandler = VertxMockSupport.mockHandler();
    commandHandlers.putCommandHandler(tenantId, gatewayId, null, commandHandler, context);
    internalCommandConsumer.handleCommandMessage(commandRecord);
    final ArgumentCaptor<CommandContext> commandContextCaptor = ArgumentCaptor.forClass(CommandContext.class);
    verify(commandHandler).handle(commandContextCaptor.capture());
    assertThat(commandContextCaptor.getValue()).isNotNull();
    assertThat(commandContextCaptor.getValue().getCommand().isValid()).isTrue();
    // assert that command is directed at the gateway
    assertThat(commandContextCaptor.getValue().getCommand().getGatewayId()).isEqualTo(gatewayId);
    assertThat(commandContextCaptor.getValue().getCommand().getDeviceId()).isEqualTo(deviceId);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CommandContext(org.eclipse.hono.client.command.CommandContext) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with KafkaHeader

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

the class KafkaBasedCommandTest method testFromRecordFailsForRecordWithoutDeviceId.

/**
 * Verifies that a command cannot be created from a record that doesn't contain a <em>device_id</em> header.
 */
@Test
public void testFromRecordFailsForRecordWithoutDeviceId() {
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND, Constants.DEFAULT_TENANT).toString();
    final String deviceId = "4711";
    final String subject = "doThis";
    final List<KafkaHeader> headers = List.of(KafkaRecordHelper.createSubjectHeader(subject));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, headers);
    assertThrows(IllegalArgumentException.class, () -> {
        KafkaBasedCommand.from(commandRecord);
    });
}
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 10 with KafkaHeader

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

the class KafkaBasedCommandTest method testFromRecordSucceedsForRequestResponseCommand.

/**
 * Verifies that a command can be created from a valid record that represent a request/response command.
 */
@Test
public void testFromRecordSucceedsForRequestResponseCommand() {
    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(KafkaRecordHelper.createResponseRequiredHeader(true));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, headers);
    final KafkaBasedCommand cmd = KafkaBasedCommand.from(commandRecord);
    assertTrue(cmd.isValid());
    assertThat(cmd.getName()).isEqualTo(subject);
    assertThat(cmd.getDeviceId()).isEqualTo(deviceId);
    assertThat(cmd.getGatewayOrDeviceId()).isEqualTo(deviceId);
    assertThat(cmd.getGatewayId()).isNull();
    assertThat(cmd.getCorrelationId()).isEqualTo(correlationId);
    assertFalse(cmd.isOneWay());
}
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)

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