CacheBasedDeviceConnectionInfo implements DeviceConnectionInfo, HealthCheckProvider { @Override public Future<Void> setLastKnownGatewayForDevice(final String tenantId, final String deviceId, final String gatewayId, final Span span) { Objects.requireNonNull(tenantId); Objects.requireNonNull(deviceId); Objects.requireNonNull(gatewayId); Objects.requireNonNull(span); final long lifespanMillis = LAST_KNOWN_GATEWAY_CACHE_ENTRY_LIFESPAN.toMillis(); return cache.put(getGatewayEntryKey(tenantId, deviceId), gatewayId, lifespanMillis, TimeUnit.MILLISECONDS) .map(replacedValue -> { LOG.debug("set last known gateway [tenant: {}, device-id: {}, gateway: {}]", tenantId, deviceId, gatewayId); return (Void) null; }) .recover(t -> { LOG.debug("failed to set last known gateway [tenant: {}, device-id: {}, gateway: {}]", tenantId, deviceId, gatewayId, t); return Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, t)); }); } CacheBasedDeviceConnectionInfo(final Cache<String, String> cache, final Tracer tracer); @Override Future<Void> setLastKnownGatewayForDevice(final String tenantId, final String deviceId,
            final String gatewayId, final Span span); @Override Future<JsonObject> getLastKnownGatewayForDevice(final String tenantId, final String deviceId, final Span span); @Override Future<Void> setCommandHandlingAdapterInstance(final String tenantId, final String deviceId,
            final String adapterInstanceId, final Duration lifespan, final Span span); @Override Future<Boolean> removeCommandHandlingAdapterInstance(final String tenantId, final String deviceId,
            final String adapterInstanceId, final Span span); @Override Future<JsonObject> getCommandHandlingAdapterInstances(final String tenantId, final String deviceId,
            final Set<String> viaGateways, final Span span); @Override void registerReadinessChecks(final HealthCheckHandler readinessHandler); @Override void registerLivenessChecks(final HealthCheckHandler livenessHandler);  }
@Test void testSetLastKnownGatewaySucceeds(final VertxTestContext ctx) { final Cache<String, String> mockedCache = mockCache(); info = new CacheBasedDeviceConnectionInfo(mockedCache, tracer); when(mockedCache.put(anyString(), anyString(), anyLong(), any())).thenReturn(Future.succeededFuture("oldValue")); info.setLastKnownGatewayForDevice(Constants.DEFAULT_TENANT, "device-id", "gw-id", mock(Span.class)) .onComplete(ctx.completing()); }
@Test void testSetLastKnownGatewayFails(final VertxTestContext ctx) { final Cache<String, String> mockedCache = mockCache(); info = new CacheBasedDeviceConnectionInfo(mockedCache, tracer); when(mockedCache.put(anyString(), anyString(), anyLong(), any())).thenReturn(Future.failedFuture(new IOException("not available"))); info.setLastKnownGatewayForDevice(Constants.DEFAULT_TENANT, "device-id", "gw-id", mock(Span.class)) .onComplete(ctx.failing(t -> { ctx.verify(() -> assertThat(t).isInstanceOf(ServiceInvocationException.class)); ctx.completeNow(); })); }