use of org.eclipse.hono.client.kafka.HonoTopic 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());
}
use of org.eclipse.hono.client.kafka.HonoTopic 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 org.eclipse.hono.client.kafka.HonoTopic 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 org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedCommandTest method testGetInvalidCommandReason.
/**
* Verifies the return value of getInvalidCommandReason().
*/
@Test
public void testGetInvalidCommandReason() {
final String topic = new HonoTopic(HonoTopic.Type.COMMAND, Constants.DEFAULT_TENANT).toString();
final String deviceId = "4711";
final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, List.of(KafkaRecordHelper.createDeviceIdHeader(deviceId)));
final KafkaBasedCommand command = KafkaBasedCommand.from(commandRecord);
assertFalse(command.isValid());
// verify the returned validation error contains all missing/invalid fields
assertThat(command.getInvalidCommandReason()).contains("subject");
}
use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedCommandTest method testFromRecordSucceedsWithoutResponseRequiredAndCorrelationId.
/**
* Verifies that a command can be created from a valid record that has no <em>response-required</em>
* and no <em>correlation-id</em> header.
* Verifies that the command reports that it is a one-way command.
*/
@Test
public void testFromRecordSucceedsWithoutResponseRequiredAndCorrelationId() {
final String topic = new HonoTopic(HonoTopic.Type.COMMAND, Constants.DEFAULT_TENANT).toString();
final String deviceId = "4711";
final String subject = "doThis";
final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, getHeaders(deviceId, subject));
final KafkaBasedCommand cmd = KafkaBasedCommand.from(commandRecord);
assertTrue(cmd.isValid());
assertThat(cmd.getName()).isEqualTo("doThis");
assertThat(cmd.getDeviceId()).isEqualTo(deviceId);
assertThat(cmd.getGatewayOrDeviceId()).isEqualTo(deviceId);
assertThat(cmd.getGatewayId()).isNull();
assertThat(cmd.getCorrelationId()).isNull();
assertTrue(cmd.isOneWay());
}
Aggregations