use of io.opentracing.Span in project hono by eclipse.
the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesWithSuspectedAdapterInstance.
/**
* Verifies that the <em>getCommandHandlingAdapterInstances</em> operation fails
* if the adapter instance mapping entry is associated with a adapter having the state 'SUSPECTED_DEAD'.
*
* @param ctx The vert.x context.
*/
@Test
public void testGetCommandHandlingAdapterInstancesWithSuspectedAdapterInstance(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.SUSPECTED_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 not been removed
verify(cache, never()).remove(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId), adapterInstance);
});
ctx.completeNow();
}));
}
use of io.opentracing.Span 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();
}));
}
use of io.opentracing.Span in project hono by eclipse.
the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesForDevice.
/**
* Verifies that the <em>getCommandHandlingAdapterInstances</em> operation succeeds if an adapter instance had
* been registered for the given device.
*
* @param ctx The vert.x context.
*/
@Test
public void testGetCommandHandlingAdapterInstancesForDevice(final VertxTestContext ctx) {
final var entry = new JsonObject().put(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID, "testDevice").put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID, "adapterInstance");
final var value = new JsonObject().put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCES, new JsonArray().add(entry));
when(cache.get(anyString())).thenReturn(Future.succeededFuture("adapterInstance"));
info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, "testDevice", Set.of(), span).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
verify(cache).get(eq(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, "testDevice")));
assertThat(result).isEqualTo(value);
});
ctx.completeNow();
}));
}
use of io.opentracing.Span in project hono by eclipse.
the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesWithoutLastKnownGatewayIsGivingDevicePrecedence.
/**
* 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 other gateway given in the viaGateway.
*
* @param extraUnusedViaGateways Test values.
* @param ctx The vert.x context.
*/
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesWithoutLastKnownGatewayIsGivingDevicePrecedence(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 has no last known gateway registered
when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture());
// 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, 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(() -> {
assertNotNull(result);
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 CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesForMultipleSubscribedVias.
/**
* Verifies that the <em>getCommandHandlingAdapterInstances</em> operation succeeds with a result containing
* the mappings of gateways, even though there is no last known gateway set for the device.
*
* @param extraUnusedViaGateways Test values.
* @param ctx The vert.x context.
*/
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesForMultipleSubscribedVias(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 String otherGatewayId = "gw-2";
final Set<String> viaGateways = new HashSet<>(Set.of(gatewayId, otherGatewayId));
viaGateways.addAll(extraUnusedViaGateways);
// GIVEN testDevice has no last known gateway registered
when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture());
// and gw-1's and gw-2's command handling adapter instance are set to
// adapterInstance and otherAdapterInstance respectively
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, viaGateways))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), adapterInstance, CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, otherGatewayId), otherAdapterInstance)));
info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
assertNotNull(result);
assertGetInstancesResultMapping(result, gatewayId, adapterInstance);
assertGetInstancesResultMapping(result, otherGatewayId, otherAdapterInstance);
assertGetInstancesResultSize(result, 2);
});
ctx.completeNow();
}));
}
Aggregations