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