Search in sources :

Example 26 with HonoTopic

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

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

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

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

Example 30 with HonoTopic

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

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