RawMessageMapper extends AbstractMessageMapper { @Override public List<Adaptable> map(final ExternalMessage externalMessage) { final Optional<MessageHeaders> messageHeadersOptional = evaluateIncomingMessageHeaders(externalMessage, incomingMessageHeaders); if (messageHeadersOptional.isEmpty()) { return dittoMessageMapper.map(externalMessage); } final MessageHeaders messageHeaders = messageHeadersOptional.get(); return List.of(ProtocolFactory.newAdaptableBuilder(toTopicPath(messageHeaders)) .withPayload(toPayload(externalMessage, messageHeaders)) .withHeaders(retainContentTypeOnly(messageHeaders)) .build()); }  @Override List<Adaptable> map(final ExternalMessage externalMessage); @Override List<ExternalMessage> map(final Adaptable adaptable); @Override JsonObject getDefaultOptions(); static final MappingContext CONTEXT; }
@Test public void mapFromMessageWithoutPayloadWithoutContentType() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().randomCorrelationId().build(); final Message<Object> message = messageBuilder(null).build(); final Signal<?> sendThingMessage = SendThingMessage.of(THING_ID, message, dittoHeaders); final List<ExternalMessage> result = underTest.map(ADAPTER.toAdaptable(sendThingMessage)); assertThat(result).hasSize(1); final ExternalMessage externalMessage = result.get(0); assertThat(externalMessage.getBytePayload()).isEmpty(); assertThat(externalMessage.getTextPayload()).isEmpty(); assertThat(externalMessage.getHeaders()).containsAllEntriesOf(message.getHeaders()); }
@Test public void mapFromMessageWithTextPayload() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().randomCorrelationId().build(); final Message<Object> message = messageBuilder("text/plain") .payload("hello world") .build(); final Signal<?> sendThingMessage = SendThingMessage.of(THING_ID, message, dittoHeaders); final List<ExternalMessage> result = underTest.map(ADAPTER.toAdaptable(sendThingMessage)); assertThat(result).hasSize(1); assertThat(result.get(0).getBytePayload()).isEmpty(); assertThat(result.get(0).getTextPayload()).contains("hello world"); assertThat(result.get(0).getHeaders()).containsExactlyEntriesOf(message.getHeaders()); }
@Test public void mapFromMessageWithBinaryPayload() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().randomCorrelationId().build(); final Message<Object> message = messageBuilder("application/whatever") .rawPayload(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6})) .build(); final Signal<?> sendThingMessage = SendThingMessage.of(THING_ID, message, dittoHeaders); final List<ExternalMessage> result = underTest.map(ADAPTER.toAdaptable(sendThingMessage)); assertThat(result).hasSize(1); assertThat(result.get(0).getBytePayload()).satisfies(byteBufferOptional -> { assertThat(byteBufferOptional).isNotEmpty(); assertThat(byteBufferOptional.get()) .isEqualTo(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6})); }); assertThat(result.get(0).getTextPayload()).isEmpty(); assertThat(result.get(0).getHeaders()).containsAllEntriesOf(message.getHeaders()); }
@Test public void mapFromJsonMessageWithBinaryContentType() { final Adaptable adaptable = ProtocolFactory.jsonifiableAdaptableFromJson(JsonObject.of("{\n" + " \"topic\": \"x/1/things/live/messages/subject\",\n" + " \"headers\": {\n" + " \"content-type\": \"application/octet-stream\"\n" + " },\n" + " \"path\": \"/inbox/messages/subject\",\n" + " \"value\": {\n" + " \"jsonKey\": \"jsonValue\"\n" + " }\n" + "}")); assertThatExceptionOfType(MessageFormatInvalidException.class).isThrownBy(() -> underTest.map(adaptable)); }
@Test public void mapFromTextMessageWithBinaryContentType() { final Adaptable adaptable = ProtocolFactory.jsonifiableAdaptableFromJson(JsonObject.of("{\n" + " \"topic\": \"x/1/things/live/messages/subject\",\n" + " \"headers\": {\n" + " \"content-type\": \"application/octet-stream\"\n" + " },\n" + " \"path\": \"/inbox/messages/subject\",\n" + " \"value\": \"This is not a base64-encoded octet stream.\"" + "}")); assertThatExceptionOfType(MessageFormatInvalidException.class).isThrownBy(() -> underTest.map(adaptable)); }
@Test public void mapFromNonMessageCommand() { final Signal<?> signal = ThingDeleted.of(ThingId.of("thing:id"), 25L, DittoHeaders.empty()); final Adaptable adaptable = ADAPTER.toAdaptable(signal); final List<ExternalMessage> actualExternalMessages = underTest.map(adaptable); final List<ExternalMessage> expectedExternalMessages = new DittoMessageMapper().map(adaptable); assertThat(actualExternalMessages).isEqualTo(expectedExternalMessages); }
@Test public void mapFromMessageWithDittoProtocolContentType() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .randomCorrelationId() .contentType("application/vnd.eclipse.ditto+json") .build(); final Message<Object> messageWithoutPayload = messageBuilder(null).payload("hello world").build(); final Signal<?> sendThingMessage = SendThingMessage.of(THING_ID, messageWithoutPayload, dittoHeaders); final Adaptable adaptable = ADAPTER.toAdaptable(sendThingMessage); final List<ExternalMessage> result = underTest.map(adaptable); final List<ExternalMessage> expectedResult = new DittoMessageMapper().map(adaptable); assertThat(result).isEqualTo(expectedResult); }
@Test public void mapToMessageWithoutHeadersOrConfig() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> underTest.map(ExternalMessageFactory.newExternalMessageBuilder(Map.of()).build()) ); }
@Test public void mapToMessageWithoutThingId() { final Map<String, String> headers = Map.of( "ditto-message-subject", "hello/world" ); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers).build()) ); }
@Test public void mapToBinaryMessagePerDefault() { final Map<String, String> headers = Map.of( "ditto-message-subject", "hello/world", "ditto-message-thing-id", "thing:id" ); final ByteBuffer payload = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6}); final List<Adaptable> adaptables = underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers) .withBytes(payload.array()) .build()); assertThat(adaptables).hasSize(1); assertThat(adaptables.get(0).getPayload().getValue()).contains(JsonValue.of("AQIDBAUG")); final Signal<?> signal = ADAPTER.fromAdaptable(adaptables.get(0)); assertThat(signal).isInstanceOf(SendThingMessage.class); final SendThingMessage<?> sendThingMessage = (SendThingMessage<?>) signal; assertThat(sendThingMessage.getEntityId().toString()).isEqualTo("thing:id"); assertThat(sendThingMessage.getMessage().getRawPayload().orElseThrow()).isEqualTo(payload); }
@Test public void mapToTextMessage() { final Map<String, String> headers = Map.of( "content-type", "text/plain", "ditto-message-subject", "hello/world", "ditto-message-thing-id", "thing:id" ); final String payload = "lorem ipsum dolor sit amet"; final List<Adaptable> adaptables = underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(payload) .build()); assertThat(adaptables).hasSize(1); final Signal<?> signal = ADAPTER.fromAdaptable(adaptables.get(0)); assertThat(signal).isInstanceOf(SendThingMessage.class); final SendThingMessage<?> sendThingMessage = (SendThingMessage<?>) signal; assertThat(sendThingMessage.getEntityId().toString()).isEqualTo("thing:id"); assertThat(sendThingMessage.getMessage().getPayload().orElseThrow()).isEqualTo(payload); }
@Test public void mapToTextMessageWithContentTypeOverride() { final Map<String, String> headers = Map.of( "content-type", "application/octet-stream", "ditto-message-subject", "hello/world", "ditto-message-thing-id", "thing:id" ); final String payload = "lorem ipsum dolor sit amet"; underTest.configure(DefaultMappingConfig.of(ConfigFactory.empty()), DefaultMessageMapperConfiguration.of("RawMessage", Map.of("incomingMessageHeaders", JsonObject.newBuilder() .set("content-type", "text/plain") .build() ), Map.of(), Map.of())); final List<Adaptable> adaptables = underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers) .withBytes(payload.getBytes()) .build()); assertThat(adaptables).hasSize(1); final Signal<?> signal = ADAPTER.fromAdaptable(adaptables.get(0)); assertThat(signal).isInstanceOf(SendThingMessage.class); final SendThingMessage<?> sendThingMessage = (SendThingMessage<?>) signal; assertThat(sendThingMessage.getEntityId().toString()).isEqualTo("thing:id"); assertThat(sendThingMessage.getMessage().getPayload().orElseThrow()).isEqualTo(payload); }
@Test public void mapToBinaryMessageWithContentTypeOverride() { final Map<String, String> headers = Map.of( "content-type", "text/plain", "ditto-message-subject", "hello/world", "ditto-message-thing-id", "thing:id" ); final String payload = "lorem ipsum dolor sit amet"; underTest.configure(DefaultMappingConfig.of(ConfigFactory.empty()), DefaultMessageMapperConfiguration.of("RawMessage", Map.of("incomingMessageHeaders", JsonObject.newBuilder() .set("content-type", "application/octet-stream") .build() ), Map.of(), Map.of())); final List<Adaptable> adaptables = underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(payload) .build()); assertThat(adaptables).hasSize(1); final Signal<?> signal = ADAPTER.fromAdaptable(adaptables.get(0)); assertThat(signal).isInstanceOf(SendThingMessage.class); final SendThingMessage<?> sendThingMessage = (SendThingMessage<?>) signal; assertThat(sendThingMessage.getEntityId().toString()).isEqualTo("thing:id"); assertThat(sendThingMessage.getMessage().getPayload().orElseThrow()) .isEqualTo(ByteBuffer.wrap(payload.getBytes())); }
@Test public void mapToJsonMessage() { final Map<String, String> headers = Map.of( "content-type", "application/vnd.hello.world+json", "ditto-message-subject", "hello/world", "ditto-message-thing-id", "thing:id" ); final String payload = "{\"lorem\":\"ipsum\"}"; final List<Adaptable> adaptables = underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(payload) .build()); assertThat(adaptables).hasSize(1); assertThat(adaptables.get(0).getPayload().getValue()).contains(JsonObject.of(payload)); final Signal<?> signal = ADAPTER.fromAdaptable(adaptables.get(0)); assertThat(signal).isInstanceOf(SendThingMessage.class); final SendThingMessage<?> sendThingMessage = (SendThingMessage<?>) signal; assertThat(sendThingMessage.getEntityId().toString()).isEqualTo("thing:id"); assertThat(sendThingMessage.getMessage().getPayload().orElseThrow()).isEqualTo(JsonObject.of(payload)); }
@Test public void mapToDittoProtocolMessage() { final Map<String, String> headers = Map.of( "content-type", "application/vnd.eclipse.ditto+json", "ditto-message-subject", "hello/world", "ditto-message-thing-id", "thing:id" ); final String payload = "{\n" + " \"topic\": \"com.acme/xdk_53/things/twin/commands/delete\",\n" + " \"headers\": {},\n" + " \"path\": \"/\",\n" + " \"status\": 204\n" + "}"; final List<Adaptable> adaptables = underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(payload) .build()); assertThat(adaptables).hasSize(1); final Signal<?> signal = ADAPTER.fromAdaptable(adaptables.get(0)); assertThat(signal).isInstanceOf(DeleteThingResponse.class); }
@Test public void mapToSendFeatureMessageResponse() { final Map<String, String> headers = Map.of( "content-type", "application/json", "status", "418", "ditto-message-subject", "hello/world", "ditto-message-thing-id", "thing:id", "ditto-message-feature-id", "accelerometer" ); final String payload = "{\"lorem\":\"ipsum\"}"; final List<Adaptable> adaptables = underTest.map(ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(payload) .build()); assertThat(adaptables).hasSize(1); final Signal<?> signal = ADAPTER.fromAdaptable(adaptables.get(0)); assertThat(signal).isInstanceOf(SendFeatureMessageResponse.class); final SendFeatureMessageResponse<?> response = (SendFeatureMessageResponse<?>) signal; assertThat(response.getStatusCode()).isEqualTo(HttpStatusCode.IM_A_TEAPOT); assertThat(response.getEntityId().toString()).isEqualTo("thing:id"); assertThat(response.getFeatureId()).isEqualTo("accelerometer"); assertThat(response.getMessage().getPayload().orElseThrow()).isEqualTo(JsonObject.of(payload)); }
@Test public void mapToNonMessageCommandWithDittoProtocolContentType() { final Signal<?> signal = ThingDeleted.of(ThingId.of("thing:id"), 25L, DittoHeaders.empty()); final Adaptable adaptable = ADAPTER.toAdaptable(signal); final ExternalMessage externalMessage = new DittoMessageMapper().map(adaptable).get(0) .withHeader("content-type", "application/vnd.eclipse.ditto+json"); final List<Adaptable> mapped = underTest.map(externalMessage); assertThat(mapped).hasSize(1); assertThat(mapped.get(0)).isEqualTo(adaptable); }