CredentialsClientImpl extends AbstractRequestResponseClient<CredentialsResult<CredentialsObject>> implements CredentialsClient { @Override public Future<CredentialsObject> get(final String type, final String authId) { return get(type, authId, new JsonObject()); } CredentialsClientImpl(final HonoConnection connection, final String tenantId, final SendMessageSampler sampler);   CredentialsClientImpl(
            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<CredentialsClient> create(
            final CacheProvider cacheProvider,
            final HonoConnection con,
            final String tenantId,
            final SendMessageSampler sampler,
            final Handler<String> senderCloseHook,
            final Handler<String> receiverCloseHook); @Override Future<CredentialsObject> get(final String type, final String authId); @Override Future<CredentialsObject> get(final String type, final String authId, final JsonObject clientContext); @Override final Future<CredentialsObject> get(final String type, final String authId, final JsonObject clientContext,
            final SpanContext spanContext);  }
@SuppressWarnings("unchecked") @Test public void testGetCredentialsInvokesServiceIfNoCacheConfigured(final VertxTestContext ctx) { final String authId = "test-auth"; final String credentialsType = CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD; final JsonObject credentialsObject = newCredentialsResult("device", authId); 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, credentialsObject.toBuffer()); final Future<CredentialsObject> getFuture = client.get(credentialsType, authId); 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); final Message sentMessage = messageCaptor.getValue(); getFuture.onComplete(ctx.succeeding(credentials -> { ctx.verify(() -> { assertNotNull(credentials); assertEquals("device", credentials.getDeviceId()); verify(cache, never()).put(any(), any(CredentialsResult.class), any(Duration.class)); verify(span).finish(); assertEquals(sentMessage.getSubject(), CredentialsConstants.CredentialsAction.get.toString()); assertEquals(MessageHelper.getJsonPayload(sentMessage).getString(CredentialsConstants.FIELD_TYPE), credentialsType); assertEquals(MessageHelper.getJsonPayload(sentMessage).getString(CredentialsConstants.FIELD_AUTH_ID), authId); }); ctx.completeNow(); })); client.handleResponse(delivery, response); }
@SuppressWarnings("unchecked") @Test public void testGetCredentialsAddsResponseToCacheOnCacheMiss(final VertxTestContext ctx) { final String authId = "test-auth"; final String credentialsType = CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD; client.setResponseCache(cache); final JsonObject credentialsObject = newCredentialsResult("device", authId); final JsonObject clientContext = new JsonObject(); client.get(credentialsType, authId, clientContext) .onComplete(ctx.succeeding(credentials -> { ctx.verify(() -> { assertNotNull(credentials); assertEquals("device", credentials.getDeviceId()); verify(cache).put( eq(TriTuple.of(CredentialsConstants.CredentialsAction.get, String.format("%s-%s", credentialsType, authId), clientContext.hashCode())), any(CredentialsResult.class), any(Duration.class)); verify(span).finish(); }); ctx.completeNow(); })); final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); final ProtonDelivery delivery = mock(ProtonDelivery.class); verify(client.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, credentialsObject.toBuffer()); client.handleResponse(delivery, response); }
@Test public void testGetCredentialsFailsWithSendError(final VertxTestContext ctx) { when(sender.sendQueueFull()).thenReturn(true); client.get(CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, "test-auth") .onComplete(ctx.failing(t -> { verify(span).setTag(eq(Tags.ERROR.getKey()), eq(Boolean.TRUE)); verify(span).finish(); ctx.completeNow(); })); }
@Test public void testGetCredentialsFailsWithRejectedRequest(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.get(CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, "test-auth") .onComplete(ctx.failing(t -> { assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode()); verify(span).setTag(eq(Tags.ERROR.getKey()), eq(Boolean.TRUE)); verify(span).finish(); ctx.completeNow(); })); }