DeviceConnectionClientImpl extends AbstractRequestResponseClient<DeviceConnectionResult> implements DeviceConnectionClient { @Override public Future<JsonObject> getCommandHandlingAdapterInstances(final String deviceId, final List<String> viaGateways, final SpanContext context) { Objects.requireNonNull(deviceId); final Promise<DeviceConnectionResult> resultTracker = Promise.promise(); final Map<String, Object> properties = createDeviceIdProperties(deviceId); final JsonObject payload = new JsonObject(); payload.put(DeviceConnectionConstants.FIELD_GATEWAY_IDS, new JsonArray(viaGateways)); final Span currentSpan = newChildSpan(context, "get command handling adapter instances"); TracingHelper.setDeviceTags(currentSpan, getTenantId(), deviceId); createAndSendRequest( DeviceConnectionConstants.DeviceConnectionAction.GET_CMD_HANDLING_ADAPTER_INSTANCES.getSubject(), properties, payload.toBuffer(), RequestResponseApiConstants.CONTENT_TYPE_APPLICATION_JSON, resultTracker, null, currentSpan); return mapResultAndFinishSpan(resultTracker.future(), result -> { switch (result.getStatus()) { case HttpURLConnection.HTTP_OK: return result.getPayload(); default: throw StatusCodeMapper.from(result); } }, currentSpan); } protected  DeviceConnectionClientImpl(final HonoConnection connection, final String tenantId, final SendMessageSampler sampler); protected  DeviceConnectionClientImpl(
            final HonoConnection connection,
            final String tenantId,
            final ProtonSender sender,
            final ProtonReceiver receiver,
            final SendMessageSampler sampler); static final String getTargetAddress(final String tenantId); static final Future<DeviceConnectionClient> create(
            final HonoConnection con,
            final String tenantId,
            final SendMessageSampler sampler,
            final Handler<String> senderCloseHook,
            final Handler<String> receiverCloseHook); @Override Future<Void> setLastKnownGatewayForDevice(final String deviceId, final String gatewayId, final SpanContext context); @Override Future<JsonObject> getLastKnownGatewayForDevice(final String deviceId, final SpanContext context); @Override Future<Boolean> removeCommandHandlingAdapterInstance(final String deviceId, final String adapterInstanceId, final SpanContext context); @Override Future<Void> setCommandHandlingAdapterInstance(final String deviceId, final String adapterInstanceId,
            final Duration lifespan, final SpanContext context); @Override Future<JsonObject> getCommandHandlingAdapterInstances(final String deviceId, final List<String> viaGateways, final SpanContext context);  }
@Test public void testGetCommandHandlingAdapterInstances(final VertxTestContext ctx) { final String adapterInstanceId = "adapterInstanceId"; final String deviceId = "4711"; final JsonArray adapterInstancesArray = new JsonArray(); adapterInstancesArray .add(new JsonObject().put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID, adapterInstanceId) .put(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId)); final JsonObject adapterInstancesResult = new JsonObject(). put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCES, adapterInstancesArray); client.getCommandHandlingAdapterInstances(deviceId, Collections.emptyList(), span.context()) .onComplete(ctx.succeeding(resultJson -> { ctx.verify(() -> { assertThat(resultJson).isEqualTo(adapterInstancesResult); verify(span).finish(); }); ctx.completeNow(); })); final Message sentMessage = verifySenderSend(); final Message response = ProtonHelper.message(); MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60)); response.setCorrelationId(sentMessage.getMessageId()); MessageHelper.setPayload(response, MessageHelper.CONTENT_TYPE_APPLICATION_JSON, adapterInstancesResult.toBuffer()); client.handleResponse(mock(ProtonDelivery.class), response); }
@Test public void testGetCommandHandlingAdapterInstancesFailsWithSendError(final VertxTestContext ctx) { when(sender.sendQueueFull()).thenReturn(true); client.getCommandHandlingAdapterInstances("deviceId", Collections.emptyList(), span.context()) .onComplete(ctx.failing(t -> { ctx.verify(() -> { verify(span).setTag(eq(Tags.ERROR.getKey()), eq(Boolean.TRUE)); verify(span).finish(); }); ctx.completeNow(); })); }
@Test public void testGetCommandHandlingAdapterInstancesFailsWithRejectedRequest(final VertxTestContext ctx) { final ProtonDelivery update = mock(ProtonDelivery.class); when(update.getRemoteState()).thenReturn(new Rejected()); when(update.remotelySettled()).thenReturn(true); when(sender.send(any(Message.class), VertxMockSupport.anyHandler())).thenAnswer(invocation -> { final Handler<ProtonDelivery> dispositionHandler = invocation.getArgument(1); dispositionHandler.handle(update); return mock(ProtonDelivery.class); }); client.getCommandHandlingAdapterInstances("deviceId", Collections.emptyList(), span.context()) .onComplete(ctx.failing(t -> { assertThat(((ServiceInvocationException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_BAD_REQUEST); ctx.verify(() -> { verify(span).setTag(eq(Tags.ERROR.getKey()), eq(Boolean.TRUE)); verify(span).finish(); }); ctx.completeNow(); })); }
@Test public void testGetCommandHandlingAdapterInstancesIncludesRequiredInformationInRequest() { final String deviceId = "deviceId"; final String gatewayId = "gw-1"; client.getCommandHandlingAdapterInstances(deviceId, Collections.singletonList(gatewayId), span.context()); final Message sentMessage = verifySenderSend(); assertThat(MessageHelper.getDeviceId(sentMessage)).isEqualTo(deviceId); assertThat(sentMessage.getMessageId()).isNotNull(); assertThat(sentMessage.getSubject()).isEqualTo(DeviceConnectionConstants.DeviceConnectionAction.GET_CMD_HANDLING_ADAPTER_INSTANCES.getSubject()); final JsonObject msgJsonPayload = MessageHelper.getJsonPayload(sentMessage); assertThat(msgJsonPayload).isNotNull(); final JsonArray gatewaysJsonArray = msgJsonPayload.getJsonArray(DeviceConnectionConstants.FIELD_GATEWAY_IDS); assertThat(gatewaysJsonArray.getList().iterator().next()).isEqualTo(gatewayId); }