Search in sources :

Example 31 with Tracer

use of io.opentracing.Tracer in project hono by eclipse.

the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesWithTerminatedAdapterInstance.

/**
 * Verifies that the <em>getCommandHandlingAdapterInstances</em> operation fails
 * if the adapter instance mapping entry is associated with a terminated adapter.
 *
 * @param ctx The vert.x context.
 */
@Test
public void testGetCommandHandlingAdapterInstancesWithTerminatedAdapterInstance(final VertxTestContext ctx) {
    final AdapterInstanceStatusProvider statusProvider = mock(AdapterInstanceStatusProvider.class);
    info = new CacheBasedDeviceConnectionInfo(cache, tracer, statusProvider);
    final String deviceId = "testDevice";
    final String adapterInstance = "adapterInstance";
    when(cache.get(anyString())).thenReturn(Future.succeededFuture(adapterInstance));
    when(statusProvider.getStatus(adapterInstance)).thenReturn(AdapterInstanceStatus.DEAD);
    when(cache.remove(anyString(), anyString())).thenReturn(Future.succeededFuture(Boolean.TRUE));
    info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, Set.of(), span).onComplete(ctx.failing(t -> {
        ctx.verify(() -> {
            verify(cache).get(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId));
            assertThat(t).isInstanceOf(ClientErrorException.class);
            assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_NOT_FOUND);
            // verify mapping entry for terminated adapter instance has been removed
            verify(cache).remove(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId), adapterInstance);
        });
        ctx.completeNow();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) AdapterInstanceStatus(org.eclipse.hono.util.AdapterInstanceStatus) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) Timeout(io.vertx.junit5.Timeout) HashSet(java.util.HashSet) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) Map(java.util.Map) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MethodSource(org.junit.jupiter.params.provider.MethodSource) Tracer(io.opentracing.Tracer) DeviceConnectionConstants(org.eclipse.hono.util.DeviceConnectionConstants) Set(java.util.Set) ServerErrorException(org.eclipse.hono.client.ServerErrorException) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) Mockito.never(org.mockito.Mockito.never) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Span(io.opentracing.Span) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 32 with Tracer

use of io.opentracing.Tracer in project hono by eclipse.

the class ProtonBasedTenantClientTest method setUp.

/**
 * Sets up the fixture.
 */
@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() {
    span = TracingMockSupport.mockSpan();
    final Tracer tracer = TracingMockSupport.mockTracer(span);
    eventBus = mock(EventBus.class);
    vertx = mock(Vertx.class);
    when(vertx.eventBus()).thenReturn(eventBus);
    VertxMockSupport.executeBlockingCodeImmediately(vertx);
    receiver = AmqpClientUnitTestHelper.mockProtonReceiver();
    sender = AmqpClientUnitTestHelper.mockProtonSender();
    final RequestResponseClientConfigProperties config = new RequestResponseClientConfigProperties();
    connection = AmqpClientUnitTestHelper.mockHonoConnection(vertx, config, tracer);
    when(connection.connect()).thenReturn(Future.succeededFuture());
    when(connection.isConnected(anyLong())).thenReturn(Future.succeededFuture());
    when(connection.createReceiver(anyString(), any(ProtonQoS.class), any(ProtonMessageHandler.class), VertxMockSupport.anyHandler())).thenReturn(Future.succeededFuture(receiver));
    when(connection.createSender(anyString(), any(ProtonQoS.class), VertxMockSupport.anyHandler())).thenReturn(Future.succeededFuture(sender));
    cache = mock(Cache.class);
    when(cache.asMap()).thenReturn(cacheBackingMap);
}
Also used : ProtonQoS(io.vertx.proton.ProtonQoS) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) EventBus(io.vertx.core.eventbus.EventBus) Vertx(io.vertx.core.Vertx) Cache(com.github.benmanes.caffeine.cache.Cache) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 33 with Tracer

use of io.opentracing.Tracer in project hono by eclipse.

the class KafkaBasedEventSenderTest method testThatSendEventThrowsOnMissingMandatoryParameter.

/**
 * Verifies that
 * {@link KafkaBasedEventSender#sendEvent(TenantObject, RegistrationAssertion, String, Buffer, Map, io.opentracing.SpanContext)}
 * throws a nullpointer exception if a mandatory parameter is {@code null}.
 */
@Test
public void testThatSendEventThrowsOnMissingMandatoryParameter() {
    final MockProducer<String, Buffer> mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final CachingKafkaProducerFactory<String, Buffer> factory = CachingKafkaProducerFactory.testFactory(vertxMock, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
    final Tracer tracer = NoopTracerFactory.create();
    final KafkaBasedEventSender sender = new KafkaBasedEventSender(vertxMock, factory, kafkaProducerConfig, true, tracer);
    assertThrows(NullPointerException.class, () -> sender.sendEvent(null, device, "the-content-type", null, null, null));
    assertThrows(NullPointerException.class, () -> sender.sendEvent(tenant, null, "the-content-type", null, null, null));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Tracer(io.opentracing.Tracer) Test(org.junit.jupiter.api.Test)

Example 34 with Tracer

use of io.opentracing.Tracer in project hono by eclipse.

the class KafkaBasedEventSenderTest method testSendEventCreatesCorrectRecord.

/**
 * Verifies that the Kafka record is created as expected.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testSendEventCreatesCorrectRecord(final VertxTestContext ctx) {
    // GIVEN a sender
    final String contentType = "text/plain";
    final String payload = "the-payload";
    final Map<String, Object> properties = Map.of("foo", "bar", MessageHelper.SYS_HEADER_PROPERTY_TTL, 5);
    final var span = TracingMockSupport.mockSpan();
    final var tracer = TracingMockSupport.mockTracer(span);
    final var mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final var factory = newProducerFactory(mockProducer);
    final var sender = new KafkaBasedEventSender(vertxMock, factory, kafkaProducerConfig, true, tracer);
    // WHEN sending a message
    sender.sendEvent(tenant, device, contentType, Buffer.buffer(payload), properties, null).onComplete(ctx.succeeding(t -> {
        ctx.verify(() -> {
            // THEN the producer record is created from the given values...
            final var producerRecord = mockProducer.history().get(0);
            assertThat(producerRecord.key()).isEqualTo(device.getDeviceId());
            assertThat(producerRecord.topic()).isEqualTo(new HonoTopic(HonoTopic.Type.EVENT, tenant.getTenantId()).toString());
            assertThat(producerRecord.value().toString()).isEqualTo(payload);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), "foo", "bar");
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), MessageHelper.SYS_HEADER_PROPERTY_TTL, 5000L);
            // ...AND contains the standard headers
            KafkaClientUnitTestHelper.assertStandardHeaders(producerRecord, device.getDeviceId(), contentType, QoS.AT_LEAST_ONCE.ordinal());
            verify(span).finish();
        });
        ctx.completeNow();
    }));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) KafkaClientUnitTestHelper(org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper) Tracer(io.opentracing.Tracer) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Mockito.verify(org.mockito.Mockito.verify) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Buffer(io.vertx.core.buffer.Buffer) QoS(org.eclipse.hono.util.QoS) MockProducer(org.apache.kafka.clients.producer.MockProducer) Mockito.mock(org.mockito.Mockito.mock) TenantObject(org.eclipse.hono.util.TenantObject) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Test(org.junit.jupiter.api.Test)

Example 35 with Tracer

use of io.opentracing.Tracer in project hono by eclipse.

the class KafkaBasedEventSenderTest method testThatConstructorThrowsOnMissingParameter.

/**
 * Verifies that the constructor throws a nullpointer exception if a parameter is {@code null}.
 */
@Test
public void testThatConstructorThrowsOnMissingParameter() {
    final MockProducer<String, Buffer> mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final CachingKafkaProducerFactory<String, Buffer> factory = CachingKafkaProducerFactory.testFactory(vertxMock, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
    final Tracer tracer = NoopTracerFactory.create();
    assertThrows(NullPointerException.class, () -> new KafkaBasedEventSender(null, factory, kafkaProducerConfig, true, tracer));
    assertThrows(NullPointerException.class, () -> new KafkaBasedEventSender(vertxMock, null, kafkaProducerConfig, true, tracer));
    assertThrows(NullPointerException.class, () -> new KafkaBasedEventSender(vertxMock, factory, null, true, tracer));
    assertThrows(NullPointerException.class, () -> new KafkaBasedEventSender(vertxMock, factory, kafkaProducerConfig, true, null));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Tracer(io.opentracing.Tracer) Test(org.junit.jupiter.api.Test)

Aggregations

Tracer (io.opentracing.Tracer)104 Span (io.opentracing.Span)49 SpanContext (io.opentracing.SpanContext)30 Map (java.util.Map)21 Vertx (io.vertx.core.Vertx)19 HashMap (java.util.HashMap)19 Test (org.junit.Test)19 BeforeEach (org.junit.jupiter.api.BeforeEach)19 Test (org.junit.jupiter.api.Test)19 Future (io.vertx.core.Future)18 Buffer (io.vertx.core.buffer.Buffer)16 HttpURLConnection (java.net.HttpURLConnection)14 EventBus (io.vertx.core.eventbus.EventBus)13 JsonObject (io.vertx.core.json.JsonObject)12 Objects (java.util.Objects)12 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Tags (io.opentracing.tag.Tags)9 Scope (io.opentracing.Scope)8 GlobalTracer (io.opentracing.util.GlobalTracer)8