use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedCommandTest method testFromRecordSucceeds.
/**
* Verifies that a command can be created from a valid record.
*/
@Test
public void testFromRecordSucceeds() {
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 KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, deviceId, getHeaders(deviceId, subject, correlationId));
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);
assertTrue(cmd.isOneWay());
}
use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedCommandTest method testFromRoutedCommandRecordSucceeds.
/**
* Verifies that a command can be created from a valid record representing a routed command
* message with a <em>via</em> header containing a gateway identifier.
*/
@Test
public void testFromRoutedCommandRecordSucceeds() {
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));
headers.add(KafkaRecordHelper.createTenantIdHeader(Constants.DEFAULT_TENANT));
final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, targetDeviceId, headers);
final KafkaBasedCommand cmd = KafkaBasedCommand.fromRoutedCommandRecord(commandRecord);
assertTrue(cmd.isValid());
assertThat(cmd.getName()).isEqualTo(subject);
assertThat(cmd.getDeviceId()).isEqualTo(targetDeviceId);
assertThat(cmd.getGatewayOrDeviceId()).isEqualTo(gatewayId);
assertThat(cmd.getGatewayId()).isEqualTo(gatewayId);
assertThat(cmd.getCorrelationId()).isEqualTo(correlationId);
assertTrue(cmd.isOneWay());
}
use of org.eclipse.hono.client.kafka.HonoTopic 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 org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedCommand method from.
/**
* Creates a command from a Kafka consumer record.
* <p>
* The message is required to contain a <em>topic</em> of the format <em>hono.command.${tenant_id}</em>
* and a <em>key</em> with the command target device id, matching the value of the <em>device_id</em> header.
* If that is not the case, an {@link IllegalArgumentException} is thrown.
* <p>
* In addition, the record is expected to contain
* <ul>
* <li>a <em>subject</em> header</li>
* <li>a non-empty <em>correlation-id</em> header if the <em>response-required</em> header is
* set to {@code true}.</li>
* </ul>
* or otherwise the returned command's {@link #isValid()} method will return {@code false}.
*
* @param record The record containing the command.
* @return The command.
* @throws NullPointerException if record is {@code null}.
* @throws IllegalArgumentException if the record's topic is not a proper command topic or if the record's
* headers do not contain a target device identifier matching the record's key.
*/
public static KafkaBasedCommand from(final KafkaConsumerRecord<String, Buffer> record) {
Objects.requireNonNull(record);
if (Strings.isNullOrEmpty(record.topic())) {
throw new IllegalArgumentException("topic is not set");
}
final HonoTopic honoTopic = HonoTopic.fromString(record.topic());
if (honoTopic == null || !honoTopic.getType().equals(HonoTopic.Type.COMMAND)) {
throw new IllegalArgumentException("unsupported topic");
}
final String tenantId = honoTopic.getTenantId();
return from(record, tenantId);
}
use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedCommandResponseSender method removeTenantTopicBasedProducerMetrics.
private void removeTenantTopicBasedProducerMetrics(final KafkaProducer<String, Buffer> producer, final String tenantId) {
final HonoTopic topic = new HonoTopic(HonoTopic.Type.COMMAND_RESPONSE, tenantId);
KafkaProducerHelper.removeTopicMetrics(producer, Stream.of(topic.toString()));
}
Aggregations