MapBasedDeviceConnectionService implements DeviceConnectionService { @Override public final Future<DeviceConnectionResult> getCommandHandlingAdapterInstances(final String tenantId, final String deviceId, final List<String> viaGateways, final Span span) { Objects.requireNonNull(tenantId); Objects.requireNonNull(deviceId); final Map<String, ExpiringValue<JsonObject>> commandHandlersForTenantMap = commandHandlingAdapterInstancesMap.get(tenantId); final DeviceConnectionResult result; if (commandHandlersForTenantMap != null) { final Map<String, String> resultMap = new HashMap<>(); final JsonObject deviceAdapterInstanceIdJson = getValue(commandHandlersForTenantMap, deviceId); if (deviceAdapterInstanceIdJson != null) { resultMap.put(deviceId, getAdapterInstanceIdFromJson(deviceAdapterInstanceIdJson)); } else if (!viaGateways.isEmpty()) { final Map<String, JsonObject> lastKnownGatewaysForTenantMap = lastKnownGatewaysMap.get(tenantId); if (lastKnownGatewaysForTenantMap != null) { final JsonObject lastKnownGatewayJson = lastKnownGatewaysForTenantMap.get(deviceId); if (lastKnownGatewayJson != null) { final String gatewayId = getGatewayIdFromLastKnownGatewayJson(lastKnownGatewayJson); if (viaGateways.contains(gatewayId)) { final JsonObject gwAdapterInstanceIdJson = getValue(commandHandlersForTenantMap, gatewayId); if (gwAdapterInstanceIdJson != null) { resultMap.put(gatewayId, getAdapterInstanceIdFromJson(gwAdapterInstanceIdJson)); } } else { log.trace("ignoring found last known gateway [{}]; gateway is not in given via list", gatewayId); } } } } if (resultMap.isEmpty() && !viaGateways.isEmpty()) { log.trace("no command handling adapter instance found for given device or last known gateway; getting instances for all via gateways"); for (final String viaGateway : viaGateways) { final JsonObject gwAdapterInstanceIdJson = getValue(commandHandlersForTenantMap, viaGateway); if (gwAdapterInstanceIdJson != null) { resultMap.put(viaGateway, getAdapterInstanceIdFromJson(gwAdapterInstanceIdJson)); } } } if (!resultMap.isEmpty()) { result = DeviceConnectionResult.from(HttpURLConnection.HTTP_OK, getResultJson(resultMap)); } else { result = DeviceConnectionResult.from(HttpURLConnection.HTTP_NOT_FOUND); } } else { result = DeviceConnectionResult.from(HttpURLConnection.HTTP_NOT_FOUND); } return Future.succeededFuture(result); }  @Autowired void setConfig(final MapBasedDeviceConnectionsConfigProperties configuration); MapBasedDeviceConnectionsConfigProperties getConfig(); @Override final Future<DeviceConnectionResult> setLastKnownGatewayForDevice(final String tenantId, final String deviceId,
            final String gatewayId, final Span span); @Override final Future<DeviceConnectionResult> getLastKnownGatewayForDevice(final String tenantId, final String deviceId,
            final Span span); @Override final Future<DeviceConnectionResult> setCommandHandlingAdapterInstance(final String tenantId,
            final String deviceId, final String protocolAdapterInstanceId, final Duration lifespan, final Span span); @Override final Future<DeviceConnectionResult> removeCommandHandlingAdapterInstance(
            final String tenantId,
            final String deviceId,
            final String adapterInstanceId,
            final Span span); @Override final Future<DeviceConnectionResult> getCommandHandlingAdapterInstances(final String tenantId,
            final String deviceId, final List<String> viaGateways, final Span span);  }
@Test public void testGetCommandHandlingAdapterInstancesFails(final VertxTestContext ctx) { final String deviceId = "testDevice"; final List<String> viaGateways = Collections.emptyList(); svc.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span) .onComplete(ctx.succeeding(result -> ctx.verify(() -> { assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.getStatus()); ctx.completeNow(); }))); }
@Test public void testGetCommandHandlingAdapterInstancesFailsWithGivenGateways(final VertxTestContext ctx) { final String deviceId = "testDevice"; final String gatewayId = "gw-1"; final List<String> viaGateways = List.of(gatewayId); svc.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span) .onComplete(ctx.succeeding(result -> ctx.verify(() -> { assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.getStatus()); ctx.completeNow(); }))); }