CacheBasedDeviceConnectionInfo implements DeviceConnectionInfo, HealthCheckProvider { @Override public Future<Void> setCommandHandlingAdapterInstance(final String tenantId, final String deviceId, final String adapterInstanceId, final Duration lifespan, final Span span) { Objects.requireNonNull(tenantId); Objects.requireNonNull(deviceId); Objects.requireNonNull(adapterInstanceId); Objects.requireNonNull(span); final long lifespanMillis = lifespan == null || lifespan.isNegative() || lifespan.getSeconds() > (Long.MAX_VALUE / 1000L) ? -1 : lifespan.toMillis(); return cache.put(getAdapterInstanceEntryKey(tenantId, deviceId), adapterInstanceId, lifespanMillis, TimeUnit.MILLISECONDS) .map(replacedValue -> { LOG.debug("set command handling adapter instance [tenant: {}, device-id: {}, adapter-instance: {}, lifespan: {}ms]", tenantId, deviceId, adapterInstanceId, lifespanMillis); return (Void) null; }) .recover(t -> { LOG.debug("failed to set command handling adapter instance [tenant: {}, device-id: {}, adapter-instance: {}, lifespan: {}ms]", tenantId, deviceId, adapterInstanceId, lifespanMillis, 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 public void testSetCommandHandlingAdapterInstanceSucceeds(final VertxTestContext ctx) { final String deviceId = "testDevice"; final String adapterInstance = "adapterInstance"; info.setCommandHandlingAdapterInstance(Constants.DEFAULT_TENANT, deviceId, adapterInstance, null, span) .onComplete(ctx.succeeding(result -> ctx.completeNow())); }
@Test public void testSetCommandHandlingAdapterInstanceWithLifespanSucceeds(final VertxTestContext ctx) { final String deviceId = "testDevice"; final String adapterInstance = "adapterInstance"; info.setCommandHandlingAdapterInstance(Constants.DEFAULT_TENANT, deviceId, adapterInstance, Duration.ofSeconds(10), span) .onComplete(ctx.succeeding(result -> ctx.completeNow())); }