DeviceConnectionClientImpl extends AbstractRequestResponseClient<DeviceConnectionResult> implements DeviceConnectionClient { @Override public Future<Void> setLastKnownGatewayForDevice(final String deviceId, final String gatewayId, final SpanContext context) { Objects.requireNonNull(deviceId); Objects.requireNonNull(gatewayId); final Map<String, Object> properties = createDeviceIdProperties(deviceId); properties.put(MessageHelper.APP_PROPERTY_GATEWAY_ID, gatewayId); final Span currentSpan = newFollowingSpan(context, "set last known gateway for device"); TracingHelper.setDeviceTags(currentSpan, getTenantId(), deviceId); currentSpan.setTag(MessageHelper.APP_PROPERTY_GATEWAY_ID, gatewayId); final Promise<DeviceConnectionResult> resultTracker = Promise.promise(); createAndSendRequest( DeviceConnectionConstants.DeviceConnectionAction.SET_LAST_GATEWAY.getSubject(), properties, null, null, resultTracker, null, currentSpan); return mapResultAndFinishSpan( resultTracker.future(), result -> { switch (result.getStatus()) { case HttpURLConnection.HTTP_NO_CONTENT: return null; 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 testSetLastKnownGatewayForDeviceSuccess(final VertxTestContext ctx) { client.setLastKnownGatewayForDevice("deviceId", "gatewayId", span.context()) .onComplete(ctx.succeeding(r -> { ctx.verify(() -> { verify(span).finish(); }); ctx.completeNow(); })); final Message sentMessage = verifySenderSend(); final Message response = createNoContentResponseMessage(sentMessage.getMessageId()); client.handleResponse(mock(ProtonDelivery.class), response); }
@Test public void testSetLastKnownGatewayForDeviceFailsWithSendError(final VertxTestContext ctx) { when(sender.sendQueueFull()).thenReturn(true); client.setLastKnownGatewayForDevice("deviceId", "gatewayId", 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 testSetLastKnownGatewayForDeviceFailsWithRejectedRequest(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.setLastKnownGatewayForDevice("deviceId", "gatewayId", span.context()) .onComplete(ctx.failing(t -> { ctx.verify(() -> { assertThat(((ServiceInvocationException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_BAD_REQUEST); verify(span).setTag(eq(Tags.ERROR.getKey()), eq(Boolean.TRUE)); verify(span).finish(); }); ctx.completeNow(); })); }
@Test public void testSetLastKnownGatewayForDeviceIncludesRequiredInformationInRequest() { final String deviceId = "deviceId"; final String gatewayId = "gatewayId"; client.setLastKnownGatewayForDevice(deviceId, gatewayId, span.context()); final Message sentMessage = verifySenderSend(); assertThat(MessageHelper.getDeviceId(sentMessage)).isEqualTo(deviceId); assertThat(MessageHelper.getApplicationProperty(sentMessage.getApplicationProperties(), MessageHelper.APP_PROPERTY_GATEWAY_ID, String.class)) .isEqualTo(gatewayId); assertThat(sentMessage.getMessageId()).isNotNull(); assertThat(sentMessage.getSubject()).isEqualTo(DeviceConnectionConstants.DeviceConnectionAction.SET_LAST_GATEWAY.getSubject()); assertThat(MessageHelper.getJsonPayload(sentMessage)).isNull(); }