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