RegistrationClientImpl extends AbstractRequestResponseClient<RegistrationResult> implements RegistrationClient { @Override public final Future<JsonObject> assertRegistration(final String deviceId) { return assertRegistration(deviceId, null); } protected  RegistrationClientImpl(final HonoConnection connection, final String tenantId, final SendMessageSampler sampler); protected  RegistrationClientImpl(
            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<RegistrationClient> create(
            final CacheProvider cacheProvider,
            final HonoConnection con,
            final String tenantId,
            final SendMessageSampler sampler,
            final Handler<String> senderCloseHook,
            final Handler<String> receiverCloseHook); @Override final Future<JsonObject> assertRegistration(final String deviceId); @Override final Future<JsonObject> assertRegistration(final String deviceId, final String gatewayId); @Override final Future<JsonObject> assertRegistration(
            final String deviceId,
            final String gatewayId,
            final SpanContext parent);  }
@Test public void testAssertRegistrationAddsResponseToCacheOnCacheMiss(final VertxTestContext ctx) { final JsonObject registrationAssertion = newRegistrationAssertionResult(); client.setResponseCache(cache); client.assertRegistration("myDevice").onComplete(ctx.succeeding(result -> { ctx.verify(() -> { assertThat(result).isEqualTo(registrationAssertion); verify(cache).put(eq(TriTuple.of("assert", "myDevice", null)), any(RegistrationResult.class), any(Duration.class)); verify(span).finish(); }); ctx.completeNow(); })); final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); verify(sender).send(messageCaptor.capture(), VertxMockSupport.anyHandler()); final Message response = ProtonHelper.message(); MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60)); response.setCorrelationId(messageCaptor.getValue().getMessageId()); MessageHelper.setPayload(response, MessageHelper.CONTENT_TYPE_APPLICATION_JSON, registrationAssertion.toBuffer()); final ProtonDelivery delivery = mock(ProtonDelivery.class); client.handleResponse(delivery, response); }
@Test public void testAssertRegistrationInvokesServiceIfNoCacheConfigured(final VertxTestContext ctx) { final JsonObject registrationAssertion = newRegistrationAssertionResult(); final Message response = ProtonHelper.message(); MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60)); MessageHelper.setPayload(response, MessageHelper.CONTENT_TYPE_APPLICATION_JSON, registrationAssertion.toBuffer()); client.assertRegistration("device").onComplete(ctx.succeeding(result -> { ctx.verify(() -> { assertThat(result).isEqualTo(registrationAssertion); verify(cache, never()).put(any(), any(RegistrationResult.class), any(Duration.class)); verify(span).finish(); }); ctx.completeNow(); })); final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); verify(sender).send(messageCaptor.capture(), VertxMockSupport.anyHandler()); response.setCorrelationId(messageCaptor.getValue().getMessageId()); final ProtonDelivery delivery = mock(ProtonDelivery.class); client.handleResponse(delivery, response); }
@Test public void testGetRegistrationInfoReturnsValueFromCache(final VertxTestContext ctx) { client.setResponseCache(cache); final JsonObject registrationAssertion = newRegistrationAssertionResult(); final RegistrationResult regResult = RegistrationResult.from(HttpURLConnection.HTTP_OK, registrationAssertion); when(cache.get(eq(TriTuple.of("assert", "device", "gateway")))).thenReturn(regResult); client.assertRegistration("device", "gateway").onComplete(ctx.succeeding(result -> { assertEquals(registrationAssertion, result); verify(sender, never()).send(any(Message.class), VertxMockSupport.anyHandler()); verify(span).finish(); ctx.completeNow(); })); }
@Test public void testGetRegistrationInfoIncludesRequiredParamsInRequest() { client.assertRegistration("device", "gateway"); final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); verify(sender).send(messageCaptor.capture(), VertxMockSupport.anyHandler()); final Message sentMessage = messageCaptor.getValue(); assertThat(MessageHelper.getDeviceId(sentMessage)).isEqualTo("device"); assertThat( MessageHelper.getApplicationProperty( sentMessage.getApplicationProperties(), MessageHelper.APP_PROPERTY_GATEWAY_ID, String.class)) .isEqualTo("gateway"); }