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