CommandTargetMapperImpl implements CommandTargetMapper { @Override public final Future<JsonObject> getTargetGatewayAndAdapterInstance(final String tenantId, final String deviceId, final SpanContext context) { if (!initialized.get()) { LOG.error("not initialized"); return Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR)); } final Span span = TracingHelper .buildChildSpan(tracer, context, "get target gateway and adapter instance", CommandTargetMapper.class.getSimpleName()) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CONSUMER) .withTag(TracingHelper.TAG_TENANT_ID, tenantId) .withTag(TracingHelper.TAG_DEVICE_ID, deviceId) .start(); return registrationClientFactory.getOrCreateRegistrationClient(tenantId) .compose(client -> client.assertRegistration(deviceId, null, span.context())) .recover(t -> { LOG.debug("Error getting registration assertion", t); return Future.failedFuture(t); }).compose(registrationAssertionJson -> { final Object viaObject = registrationAssertionJson.getValue(RegistrationConstants.FIELD_VIA); @SuppressWarnings("unchecked") final List<String> viaGateways = viaObject instanceof JsonArray ? new ArrayList<String>(((JsonArray) viaObject).getList()) : Collections.emptyList(); return deviceConnectionClientFactory.getOrCreateDeviceConnectionClient(tenantId) .compose(client -> client.getCommandHandlingAdapterInstances(deviceId, viaGateways, span.context())) .compose(resultJson -> determineTargetInstanceJson(resultJson, deviceId, viaGateways, span)); }).map(result -> { span.finish(); return result; }).recover(t -> { LOG.debug("Error getting target gateway and adapter instance", t); TracingHelper.logError(span, t); Tags.HTTP_STATUS.set(span, ServiceInvocationException.extractStatusCode(t)); span.finish(); return Future.failedFuture(t); }); } CommandTargetMapperImpl(final Tracer tracer); @Override void initialize(final RegistrationClientFactory registrationClientFactory,
            final BasicDeviceConnectionClientFactory deviceConnectionClientFactory); @Override final Future<JsonObject> getTargetGatewayAndAdapterInstance(final String tenantId, final String deviceId, final SpanContext context);  }
@Test public void testGetTargetGatewayAndAdapterInstanceUsingDeviceWithNoVia() { final JsonObject assertRegistrationResult = new JsonObject(); when(regClient.assertRegistration(anyString(), any(), any())).thenReturn(Future.succeededFuture(assertRegistrationResult)); final String adapterInstanceId = "adapter1"; final JsonObject adapterInstancesResult = new JsonObject(); final JsonObject adapterInstanceEntry = new JsonObject(); adapterInstanceEntry.put(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId); adapterInstanceEntry.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID, adapterInstanceId); adapterInstancesResult.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCES, new JsonArray(Collections.singletonList(adapterInstanceEntry))); when(devConClient.getCommandHandlingAdapterInstances(eq(deviceId), any(), any())).thenReturn(Future.succeededFuture(adapterInstancesResult)); final Future<JsonObject> mappedGatewayDeviceFuture = commandTargetMapper.getTargetGatewayAndAdapterInstance(tenantId, deviceId, null); assertThat(mappedGatewayDeviceFuture.isComplete()).isTrue(); assertThat(mappedGatewayDeviceFuture.result()).isEqualTo(adapterInstanceEntry); verify(span).finish(); }
@Test public void testGetTargetGatewayAndAdapterInstanceUsingDeviceWithMappedGateway() { final String gatewayId = "testDeviceVia"; final JsonObject assertRegistrationResult = new JsonObject(); final JsonArray viaArray = new JsonArray(Collections.singletonList(gatewayId)); assertRegistrationResult.put(RegistrationConstants.FIELD_VIA, viaArray); when(regClient.assertRegistration(anyString(), any(), any())).thenReturn(Future.succeededFuture(assertRegistrationResult)); final String adapterInstanceId = "adapter1"; final JsonObject adapterInstancesResult = new JsonObject(); final JsonObject adapterInstanceEntry = new JsonObject(); adapterInstanceEntry.put(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID, gatewayId); adapterInstanceEntry.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID, adapterInstanceId); adapterInstancesResult.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCES, new JsonArray(Collections.singletonList(adapterInstanceEntry))); when(devConClient.getCommandHandlingAdapterInstances(eq(deviceId), any(), any())).thenReturn(Future.succeededFuture(adapterInstancesResult)); final Future<JsonObject> mappedGatewayDeviceFuture = commandTargetMapper.getTargetGatewayAndAdapterInstance(tenantId, deviceId, null); assertThat(mappedGatewayDeviceFuture.isComplete()).isTrue(); assertThat(mappedGatewayDeviceFuture.result()).isEqualTo(adapterInstanceEntry); verify(span).finish(); }
@Test public void tesGetTargetGatewayAndAdapterInstanceUsingDeviceWithMappedGateway() { final String gatewayId = "testDeviceVia"; final String otherGatewayId = "otherGatewayId"; final JsonObject assertRegistrationResult = new JsonObject(); final JsonArray viaArray = new JsonArray(); viaArray.add(gatewayId); viaArray.add(otherGatewayId); assertRegistrationResult.put(RegistrationConstants.FIELD_VIA, viaArray); when(regClient.assertRegistration(anyString(), any(), any())).thenReturn(Future.succeededFuture(assertRegistrationResult)); final String adapterInstanceId = "adapter1"; final JsonObject adapterInstancesResult = new JsonObject(); final JsonObject adapterInstanceEntry = new JsonObject(); adapterInstanceEntry.put(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID, gatewayId); adapterInstanceEntry.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID, adapterInstanceId); final JsonObject adapterInstanceOtherEntry = new JsonObject(); adapterInstanceOtherEntry.put(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID, otherGatewayId); adapterInstanceOtherEntry.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID, adapterInstanceId); final JsonArray adapterInstances = new JsonArray(); adapterInstances.add(adapterInstanceEntry); adapterInstances.add(adapterInstanceOtherEntry); adapterInstancesResult.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCES, adapterInstances); when(devConClient.getCommandHandlingAdapterInstances(eq(deviceId), any(), any())).thenReturn(Future.succeededFuture(adapterInstancesResult)); final Future<JsonObject> mappedGatewayDeviceFuture = commandTargetMapper.getTargetGatewayAndAdapterInstance(tenantId, deviceId, null); assertThat(mappedGatewayDeviceFuture.isComplete()).isTrue(); assertThat(mappedGatewayDeviceFuture.result()).isEqualTo(adapterInstanceEntry); verify(span).finish(); }