Search in sources :

Example 86 with Span

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

the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesForSingleResultAndLastKnownGatewayNotInVia.

/**
 * Verifies that the <em>getCommandHandlingAdapterInstances</em> operation fails if an adapter instance has
 * been registered for the last known gateway associated with the given device, but that gateway isn't in the
 * given viaGateways set.
 *
 * @param extraUnusedViaGateways Test values.
 * @param ctx The vert.x context.
 */
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesForSingleResultAndLastKnownGatewayNotInVia(final Set<String> extraUnusedViaGateways, final VertxTestContext ctx) {
    final String deviceId = "testDevice";
    final String adapterInstance = "adapterInstance";
    final String gatewayId = "gw-1";
    final Set<String> viaGateways = new HashSet<>(Set.of("otherGatewayId"));
    viaGateways.addAll(extraUnusedViaGateways);
    // GIVEN testDevice's last known gateway is set to gw-1
    when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture(gatewayId));
    // and a command handling adapter instance is set for gw-1 only
    when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, gatewayId))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), adapterInstance)));
    // but not for the gateways in the via list
    when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, viaGateways))).thenReturn(Future.succeededFuture(Map.of()));
    when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, gatewayId))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), adapterInstance)));
    info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span).onComplete(ctx.failing(t -> {
        ctx.verify(() -> {
            assertThat(t).isInstanceOf(ClientErrorException.class);
            assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_NOT_FOUND);
        });
        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) HashSet(java.util.HashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 87 with Span

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

the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesWithLastKnownGatewayIsGivingDevicePrecedence.

/**
 * Verifies that the <em>getCommandHandlingAdapterInstances</em> operation succeeds with a result containing just
 * the mapping of *the given device* to its command handling adapter instance, even though an adapter instance is
 * also registered for the last known gateway associated with the given device.
 *
 * @param extraUnusedViaGateways Test values.
 * @param ctx The vert.x context.
 */
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesWithLastKnownGatewayIsGivingDevicePrecedence(final Set<String> extraUnusedViaGateways, final VertxTestContext ctx) {
    final String deviceId = "testDevice";
    final String adapterInstance = "adapterInstance";
    final String otherAdapterInstance = "otherAdapterInstance";
    final String gatewayId = "gw-1";
    final Set<String> viaGateways = new HashSet<>(Set.of(gatewayId));
    viaGateways.addAll(extraUnusedViaGateways);
    // GIVEN testDevice's last known gateway is set to gw-1
    when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture(gatewayId));
    // and testDevice's and gw-1's command handling adapter instances are set to
    // adapterInstance and otherAdapterInstance respectively
    when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, gatewayId))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId), adapterInstance, CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), otherAdapterInstance)));
    when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, viaGateways))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId), adapterInstance, CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), otherAdapterInstance)));
    info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span).onComplete(ctx.succeeding(result -> {
        ctx.verify(() -> {
            assertThat(result).isNotNull();
            assertGetInstancesResultMapping(result, deviceId, adapterInstance);
            // be sure that only the mapping for the device is returned, not the mappings for the gateway
            assertGetInstancesResultSize(result, 1);
        });
        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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashSet(java.util.HashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 88 with Span

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

the class VertxBasedMqttProtocolAdapterTest method testOnPublishedAuthenticatedMessageFailsForNonMatchingTenant.

/**
 * Verifies that the adapter rejects a message published by a gateway whose tenant
 * does not match the tenant specified in the topic.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testOnPublishedAuthenticatedMessageFailsForNonMatchingTenant(final VertxTestContext ctx) {
    givenAnAdapter();
    // WHEN an authenticated gateway publishes a message to a topic that does not match the gateway's tenant
    final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT + "/other-tenant/4711", span, new Device("my-tenant", "gateway"));
    adapter.onPublishedMessage(context).onComplete(ctx.failing(t -> {
        // THEN the message cannot be published
        assertServiceInvocationException(ctx, t, HttpURLConnection.HTTP_FORBIDDEN);
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) VertxExtension(io.vertx.junit5.VertxExtension) EventConstants(org.eclipse.hono.util.EventConstants) Device(org.eclipse.hono.auth.Device) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Buffer(io.vertx.core.buffer.Buffer) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) Mockito.mock(org.mockito.Mockito.mock) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test)

Example 89 with Span

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

the class VertxBasedMqttProtocolAdapterTest method testMapTopicSupportsShortAndLongTopicNames.

/**
 * Verifies that the adapter supports all required topic names.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testMapTopicSupportsShortAndLongTopicNames(final VertxTestContext ctx) {
    givenAnAdapter();
    MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, EventConstants.EVENT_ENDPOINT);
    MqttContext context = newContext(message, span, null);
    adapter.mapTopic(context).onComplete(ctx.succeeding(address -> {
        ctx.verify(() -> assertThat(MetricsTags.EndpointType.fromString(address.getEndpoint())).isEqualTo(MetricsTags.EndpointType.EVENT));
    }));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, EventConstants.EVENT_ENDPOINT_SHORT);
    context = newContext(message, span, null);
    adapter.mapTopic(context).onComplete(ctx.succeeding(address -> {
        ctx.verify(() -> assertThat(MetricsTags.EndpointType.fromString(address.getEndpoint())).isEqualTo(MetricsTags.EndpointType.EVENT));
    }));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
    context = newContext(message, span, null);
    adapter.mapTopic(context).onComplete(ctx.succeeding(address -> {
        ctx.verify(() -> assertThat(MetricsTags.EndpointType.fromString(address.getEndpoint())).isEqualTo(MetricsTags.EndpointType.TELEMETRY));
    }));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT_SHORT);
    context = newContext(message, span, null);
    adapter.mapTopic(context).onComplete(ctx.succeeding(address -> {
        ctx.verify(() -> assertThat(MetricsTags.EndpointType.fromString(address.getEndpoint())).isEqualTo(MetricsTags.EndpointType.TELEMETRY));
    }));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, "unknown");
    context = newContext(message, span, null);
    adapter.mapTopic(context).onSuccess(v -> ctx.failNow("should not have succeeded mapping topic"));
    ctx.completeNow();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) VertxExtension(io.vertx.junit5.VertxExtension) EventConstants(org.eclipse.hono.util.EventConstants) Device(org.eclipse.hono.auth.Device) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Buffer(io.vertx.core.buffer.Buffer) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) Mockito.mock(org.mockito.Mockito.mock) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Test(org.junit.jupiter.api.Test)

Example 90 with Span

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

the class VertxBasedMqttProtocolAdapterTest method testOnPublishedMessageFailsForMissingDeviceId.

/**
 * Verifies that the adapter fails to map a topic without a device ID received from an anonymous device.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testOnPublishedMessageFailsForMissingDeviceId(final VertxTestContext ctx) {
    givenAnAdapter();
    // WHEN an anonymous device publishes a message to a topic that does not contain a device ID
    final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT + "/my-tenant", span);
    adapter.onPublishedMessage(context).onComplete(ctx.failing(t -> {
        // THEN the message cannot be published
        assertServiceInvocationException(ctx, t, HttpURLConnection.HTTP_BAD_REQUEST);
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) VertxExtension(io.vertx.junit5.VertxExtension) EventConstants(org.eclipse.hono.util.EventConstants) Device(org.eclipse.hono.auth.Device) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Buffer(io.vertx.core.buffer.Buffer) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) Mockito.mock(org.mockito.Mockito.mock) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Test(org.junit.jupiter.api.Test)

Aggregations

Span (io.opentracing.Span)370 Future (io.vertx.core.Future)182 HttpURLConnection (java.net.HttpURLConnection)174 Tracer (io.opentracing.Tracer)123 JsonObject (io.vertx.core.json.JsonObject)119 ClientErrorException (org.eclipse.hono.client.ClientErrorException)117 Map (java.util.Map)116 SpanContext (io.opentracing.SpanContext)115 Objects (java.util.Objects)109 List (java.util.List)104 TracingHelper (org.eclipse.hono.tracing.TracingHelper)104 Optional (java.util.Optional)102 Promise (io.vertx.core.Promise)98 Test (org.junit.jupiter.api.Test)96 MessageHelper (org.eclipse.hono.util.MessageHelper)89 Vertx (io.vertx.core.Vertx)84 Constants (org.eclipse.hono.util.Constants)82 ServerErrorException (org.eclipse.hono.client.ServerErrorException)80 Truth.assertThat (com.google.common.truth.Truth.assertThat)79 Mockito.mock (org.mockito.Mockito.mock)79