Search in sources :

Example 16 with HonoTopic

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());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Test(org.junit.jupiter.api.Test)

Example 17 with HonoTopic

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

Example 18 with HonoTopic

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);
    });
}
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 19 with HonoTopic

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);
}
Also used : HonoTopic(org.eclipse.hono.client.kafka.HonoTopic)

Example 20 with HonoTopic

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()));
}
Also used : HonoTopic(org.eclipse.hono.client.kafka.HonoTopic)

Aggregations

HonoTopic (org.eclipse.hono.client.kafka.HonoTopic)35 Buffer (io.vertx.core.buffer.Buffer)23 Test (org.junit.jupiter.api.Test)17 KafkaHeader (io.vertx.kafka.client.producer.KafkaHeader)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 Span (io.opentracing.Span)8 VertxTestContext (io.vertx.junit5.VertxTestContext)8 Vertx (io.vertx.core.Vertx)7 VertxExtension (io.vertx.junit5.VertxExtension)7 Future (io.vertx.core.Future)6 Promise (io.vertx.core.Promise)6 DownstreamMessage (org.eclipse.hono.application.client.DownstreamMessage)6 CachingKafkaProducerFactory (org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory)6 MessagingKafkaProducerConfigProperties (org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties)6 MessageHelper (org.eclipse.hono.util.MessageHelper)6 Tracer (io.opentracing.Tracer)5 NoopSpan (io.opentracing.noop.NoopSpan)5 HttpURLConnection (java.net.HttpURLConnection)5