InfoProviderFactory { public static ConnectionMonitor.InfoProvider forExternalMessage(final ExternalMessage externalMessage) { final String correlationId = extractCorrelationId(externalMessage.getHeaders()); final Instant timestamp = Instant.now(); final Supplier<String> payloadSupplier = supplyPayloadFromExternalMessage(externalMessage); return new ImmutableInfoProvider(correlationId, timestamp, null, externalMessage.getHeaders(), payloadSupplier, false); } private  InfoProviderFactory(); static ConnectionMonitor.InfoProvider forExternalMessage(final ExternalMessage externalMessage); static ConnectionMonitor.InfoProvider forSignal(final Signal<?> signal); static ConnectionMonitor.InfoProvider forHeaders(final Map<String, String> headers); static ConnectionMonitor.InfoProvider empty();  }
@Test public void forExternalMessage() { final String correlationId = "theCorrelation"; final Map<String, String> headersWithCorrelationId = DittoHeaders.newBuilder().correlationId(correlationId).build(); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headersWithCorrelationId) .build(); final Instant before = Instant.now(); final ConnectionMonitor.InfoProvider info = InfoProviderFactory.forExternalMessage(externalMessage); final Instant after = Instant.now(); assertThat(info) .hasNoThingId() .hasCorrelationId(correlationId) .hasTimestampBetween(before, after); }
@Test public void forExternalMessageWithHeaders() { final Map<String, String> headers = DittoHeaders.newBuilder() .putHeader("foo", "bar") .putHeader("138", "ditto") .build(); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers).build(); final ConnectionMonitor.InfoProvider info = InfoProviderFactory.forExternalMessage(externalMessage); assertThat(info).hasHeaders(headers); }
@Test public void forExternalMessageWithTextPayload() { final String textPayload = "{ \"foo\":\"bar\" }"; final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(new HashMap<>()) .withText(textPayload) .build(); final ConnectionMonitor.InfoProvider info = InfoProviderFactory.forExternalMessage(externalMessage); assertThat(info).hasPayload(textPayload); }
@Test public void forExternalMessageWithBytePayload() { final byte[] bytePayload = "{ \"foo\":\"bar\" }".getBytes(); final String expectedBase64Payload = Base64.getEncoder().encodeToString(bytePayload); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(new HashMap<>()) .withBytes(bytePayload) .build(); final ConnectionMonitor.InfoProvider info = InfoProviderFactory.forExternalMessage(externalMessage); assertThat(info).hasPayload(expectedBase64Payload); }
@Test public void forExternalMessageWithoutTextPayload() { final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(new HashMap<>()) .withText(null) .build(); final ConnectionMonitor.InfoProvider info = InfoProviderFactory.forExternalMessage(externalMessage); assertThat(info).hasEmptyTextPayload(); }
@Test public void forExternalMessageWithoutBytePayload() { final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(new HashMap<>()) .withBytes((byte[]) null) .build(); final ConnectionMonitor.InfoProvider info = InfoProviderFactory.forExternalMessage(externalMessage); assertThat(info).hasEmptyBytePayload(); }