TenantClientImpl extends AbstractRequestResponseClient<TenantResult<TenantObject>> implements TenantClient { @Override public final Future<TenantObject> get(final String tenantId) { return get(tenantId, null); } protected  TenantClientImpl(final HonoConnection connection, final SendMessageSampler sampler); protected  TenantClientImpl(
            final HonoConnection connection,
            final ProtonSender sender,
            final ProtonReceiver receiver,
            final SendMessageSampler sampler); static final String getTargetAddress(); static final Future<TenantClient> create(
            final CacheProvider cacheProvider,
            final HonoConnection connection,
            final SendMessageSampler sampler,
            final Handler<String> senderCloseHook,
            final Handler<String> receiverCloseHook); @Override final Future<TenantObject> get(final String tenantId); @Override final Future<TenantObject> get(final String tenantId, final SpanContext parent); @Override final Future<TenantObject> get(final X500Principal subjectDn); @Override final Future<TenantObject> get(final X500Principal subjectDn, final SpanContext parent);  }
@SuppressWarnings("unchecked") @Test public void testGetTenantInvokesServiceIfNoCacheConfigured(final VertxTestContext ctx) { client.setResponseCache(null); final JsonObject tenantResult = newTenantResult("tenant"); client.get("tenant").onComplete(ctx.succeeding(tenant -> { ctx.verify(() -> { assertThat(tenant).isNotNull(); assertThat(tenant.getTenantId()).isEqualTo("tenant"); verify(cache, never()).put(any(), any(TenantResult.class), any(Duration.class)); verify(span).finish(); }); ctx.completeNow(); })); final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); verify(sender).send(messageCaptor.capture(), any(Handler.class)); 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, tenantResult.toBuffer()); final ProtonDelivery delivery = mock(ProtonDelivery.class); client.handleResponse(delivery, response); }
@SuppressWarnings("unchecked") @Test public void testGetTenantAddsInfoToCacheOnCacheMiss(final VertxTestContext ctx) { client.setResponseCache(cache); final JsonObject tenantResult = newTenantResult("tenant"); client.get("tenant").onComplete(ctx.succeeding(tenant -> { ctx.verify(() -> { assertThat(tenant).isNotNull(); assertThat(tenant.getTenantId()).isEqualTo("tenant"); verify(cache).put(eq(TriTuple.of(TenantAction.get, "tenant", null)), any(TenantResult.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, tenantResult.toBuffer()); final ProtonDelivery delivery = mock(ProtonDelivery.class); client.handleResponse(delivery, response); }
@Test public void testGetTenantFailsWithSendError(final VertxTestContext ctx) { when(sender.sendQueueFull()).thenReturn(true); client.get("tenant").onComplete(ctx.failing(t -> { verify(span).setTag(eq(Tags.ERROR.getKey()), eq(Boolean.TRUE)); verify(span).finish(); ctx.completeNow(); })); }
@Test public void testGetTenantFailsWithRejectedRequest(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("tenant").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(); })); }
@Test public void testGetTenantByCaUsesRFC2253SubjectDn() { final X500Principal dn = new X500Principal("CN=ca, OU=Hono, O=Eclipse"); client.get(dn); final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); verify(sender).send(messageCaptor.capture(), VertxMockSupport.anyHandler()); final Message sentMessage = messageCaptor.getValue(); final JsonObject payload = MessageHelper.getJsonPayload(sentMessage); assertThat(payload.getString(TenantConstants.FIELD_PAYLOAD_SUBJECT_DN)).isEqualTo("CN=ca,OU=Hono,O=Eclipse"); }
@Test public void testGetTenantIncludesRequiredInformationInRequest() { client.get("tenant"); final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); verify(sender).send(messageCaptor.capture(), VertxMockSupport.anyHandler()); final Message sentMessage = messageCaptor.getValue(); assertNull(MessageHelper.getTenantId(sentMessage)); assertThat(sentMessage.getMessageId()).isNotNull(); assertThat(sentMessage.getSubject()).isEqualTo(TenantConstants.TenantAction.get.toString()); assertThat(MessageHelper.getJsonPayload(sentMessage).getString(TenantConstants.FIELD_PAYLOAD_TENANT_ID)).isEqualTo("tenant"); }