JavaScriptMessageMapperRhino extends AbstractMessageMapper { @Override public List<Adaptable> map(final ExternalMessage message) { return incomingMapping.apply(message); } JavaScriptMessageMapperRhino(); @Override void doConfigure(final MappingConfig mappingConfig, final MessageMapperConfiguration options); @Override List<Adaptable> map(final ExternalMessage message); @Override List<ExternalMessage> map(final Adaptable adaptable);  }
@Test public void testNoopJavascriptIncomingMapping() { final String correlationId = UUID.randomUUID().toString(); final Map<String, String> headers = new HashMap<>(); headers.put(HEADER_CORRELATION_ID, correlationId); headers.put(ExternalMessage.CONTENT_TYPE_HEADER, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE); final ModifyAttribute modifyAttribute = ModifyAttribute.of(ThingId.of(MAPPING_INCOMING_NAMESPACE, MAPPING_INCOMING_ID), JsonPointer.of("foo"), JsonValue.of(MAPPING_INCOMING_PAYLOAD_STRING), DittoHeaders.newBuilder() .correlationId(correlationId) .schemaVersion(JsonSchemaVersion.V_2) .build()); final Adaptable inputAdaptable = DittoProtocolAdapter.newInstance().toAdaptable(modifyAttribute); final JsonifiableAdaptable jsonifiableInputAdaptable = ProtocolFactory.wrapAsJsonifiableAdaptable(inputAdaptable); final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(jsonifiableInputAdaptable.toJsonString()) .build(); final long startTs = System.nanoTime(); final List<Adaptable> adaptables = javaScriptRhinoMapperNoop.map(message); final Adaptable mappedAdaptable = adaptables.get(0); System.out.println(mappedAdaptable); System.out.println( "testNoopJavascriptIncomingMapping Duration: " + (System.nanoTime() - startTs) / 1000000.0 + "ms"); assertThat(mappedAdaptable).isEqualTo(jsonifiableInputAdaptable); }
@Test public void testNoopJavascriptOutgoingMapping() { final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-plain"); final String correlationId = UUID.randomUUID().toString(); final Thing newThing = Thing.newBuilder() .setId(thingId) .setAttributes(Attributes.newBuilder().set("foo", "bar").build()) .build(); final CreateThing createThing = CreateThing.of(newThing, null, DittoHeaders.newBuilder().correlationId(correlationId).putHeader("subject", "{{topic:action-subject}}").build()); final Adaptable inputAdaptable = DittoProtocolAdapter.newInstance().toAdaptable(createThing); final JsonifiableAdaptable jsonifiableInputAdaptable = ProtocolFactory.wrapAsJsonifiableAdaptable(inputAdaptable); final long startTs = System.nanoTime(); final List<ExternalMessage> rawMessageOpt = javaScriptRhinoMapperNoop.map(inputAdaptable); final ExternalMessage rawMessage = rawMessageOpt.get(0); System.out.println(rawMessage.getHeaders()); System.out.println(rawMessage); System.out.println( "testNoopJavascriptOutgoingMapping Duration: " + (System.nanoTime() - startTs) / 1000000.0 + "ms"); assertThat(rawMessage.findContentType()).contains(DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE); assertThat(rawMessage.findHeader(HEADER_CORRELATION_ID)).contains(correlationId); assertThat(rawMessage.isTextMessage()).isTrue(); final String textPayload = rawMessage.getTextPayload().get(); DittoJsonAssertions.assertThat(JsonFactory.readFrom(textPayload).asObject()) .isEqualToIgnoringFieldDefinitions(jsonifiableInputAdaptable.toJson()); }
@Test public void testDefaultJavascriptOutgoingMapping() { final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-plain"); final String correlationId = UUID.randomUUID().toString(); final Thing newThing = Thing.newBuilder() .setId(thingId) .setAttributes(Attributes.newBuilder().set("foo", "bar").build()) .build(); final CreateThing createThing = CreateThing.of(newThing, null, DittoHeaders.newBuilder().correlationId(correlationId).putHeader("subject", "{{topic:action-subject}}").build()); System.out.println("HEADERS: " + ProtocolFactory.newHeadersWithDittoContentType(createThing.getDittoHeaders())); System.out.println("CREATE THING :" + createThing); final Adaptable inputAdaptable = DittoProtocolAdapter.newInstance().toAdaptable(createThing); final JsonifiableAdaptable jsonifiableInputAdaptable = ProtocolFactory.wrapAsJsonifiableAdaptable(inputAdaptable); final long startTs = System.nanoTime(); System.out.println("ADAPTABLE: " + jsonifiableInputAdaptable); System.out.println("ADAPTABLE TO JSON: " + jsonifiableInputAdaptable.toJsonString()); final List<ExternalMessage> rawMessageOpt = javaScriptRhinoMapperDefault.map(inputAdaptable); final ExternalMessage rawMessage = rawMessageOpt.get(0); System.out.println(rawMessage.getHeaders()); System.out.println(rawMessage); System.out.println( "testNoopJavascriptOutgoingMapping Duration: " + (System.nanoTime() - startTs) / 1000000.0 + "ms"); assertThat(rawMessage.findContentType()).contains(DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE); assertThat(rawMessage.findHeader(HEADER_CORRELATION_ID)).contains(correlationId); assertThat(rawMessage.isTextMessage()).isTrue(); final String textPayload = rawMessage.getTextPayload().get(); DittoJsonAssertions.assertThat(JsonFactory.readFrom(textPayload).asObject()) .isEqualToIgnoringFieldDefinitions(jsonifiableInputAdaptable.toJson()); }
@Test public void testDefaultJavascriptOutgoingMappingWithStatus() { final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-plain"); final String correlationId = UUID.randomUUID().toString(); final Thing newThing = Thing.newBuilder() .setId(thingId) .setAttributes(Attributes.newBuilder().set("foo", "bar").build()) .build(); final CreateThingResponse createThingResponse = CreateThingResponse.of(newThing, DittoHeaders.newBuilder() .correlationId(correlationId) .putHeader("subject", "{{topic:action-subject}}") .build()); System.out.println( "HEADERS: " + ProtocolFactory.newHeadersWithDittoContentType(createThingResponse.getDittoHeaders())); System.out.println("CREATE THING RESPONSE :" + createThingResponse); final Adaptable adaptable = DittoProtocolAdapter.newInstance().toAdaptable(createThingResponse); final JsonifiableAdaptable jsonifiableAdaptable = ProtocolFactory.wrapAsJsonifiableAdaptable(adaptable); final long startTs = System.nanoTime(); System.out.println("ADAPTABLE: " + jsonifiableAdaptable); System.out.println("ADAPTABLE TO JSON: " + jsonifiableAdaptable.toJsonString()); final List<ExternalMessage> rawMessageOpt = javaScriptRhinoMapperDefault.map(adaptable); final ExternalMessage rawMessage = rawMessageOpt.get(0); System.out.println(rawMessage.getHeaders()); System.out.println(rawMessage); System.out.println("testDefaultJavascriptOutgoingMappingWithStatus Duration: " + (System.nanoTime() - startTs) / 1000000.0 + "ms"); assertThat(rawMessage.findContentType()).contains(DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE); assertThat(rawMessage.findHeader(HEADER_CORRELATION_ID)).contains(correlationId); assertThat(rawMessage.isTextMessage()).isTrue(); final String textPayload = rawMessage.getTextPayload().get(); final JsonObject jsonPayload = JsonFactory.readFrom(textPayload).asObject(); DittoJsonAssertions.assertThat(jsonPayload).isEqualToIgnoringFieldDefinitions(jsonifiableAdaptable.toJson()); }
@Test public void testPlainJavascriptIncomingMapping() { final String correlationId = UUID.randomUUID().toString(); final Map<String, String> headers = new HashMap<>(); headers.put(HEADER_CORRELATION_ID, correlationId); headers.put(ExternalMessage.CONTENT_TYPE_HEADER, CONTENT_TYPE_PLAIN); final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(MAPPING_INCOMING_PAYLOAD_STRING) .build(); final long startTs = System.nanoTime(); final List<Adaptable> adaptables = javaScriptRhinoMapperPlain.map(message); final Adaptable adaptable = adaptables.get(0); System.out.println(adaptable); System.out.println( "testPlainJavascriptIncomingMapping Duration: " + (System.nanoTime() - startTs) / 1000000.0 + "ms"); assertThat(adaptable.getTopicPath().getChannel()).isEqualTo(TopicPath.Channel.TWIN); assertThat(adaptable.getTopicPath().getCriterion()).isEqualTo(TopicPath.Criterion.COMMANDS); assertThat(adaptable.getTopicPath().getAction()).contains(TopicPath.Action.MODIFY); assertThat(adaptable.getTopicPath().getNamespace()).isEqualTo(MAPPING_INCOMING_NAMESPACE); assertThat(adaptable.getTopicPath().getId()).isEqualTo(MAPPING_INCOMING_ID); assertThat(adaptable.getPayload().getPath().toString()).isEqualTo(MAPPING_INCOMING_PATH); assertThat(adaptable.getPayload().getValue()).contains(JsonValue.of(MAPPING_INCOMING_PAYLOAD_STRING)); }
@Test public void testPlainJavascriptIncomingMappingWithStatus() { final String correlationId = UUID.randomUUID().toString(); final Map<String, String> headers = new HashMap<>(); headers.put(HEADER_CORRELATION_ID, correlationId); headers.put(ExternalMessage.CONTENT_TYPE_HEADER, CONTENT_TYPE_PLAIN); final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-plain"); final ModifyThingResponse modifyThingResponse = ModifyThingResponse.modified(thingId, DittoHeaders.newBuilder() .correlationId(correlationId) .putHeader("subject", "{{topic:action-subject}}") .build()); final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(modifyThingResponse.toJsonString()) .build(); final long startTs = System.nanoTime(); final List<Adaptable> adaptables = javaScriptRhinoMapperPlainWithStatus.map(message); final Adaptable adaptable = adaptables.get(0); System.out.println(adaptable); System.out.println( "testPlainJavascriptIncomingMapping Duration: " + (System.nanoTime() - startTs) / 1000000.0 + "ms"); assertThat(adaptable.getTopicPath().getChannel()).isEqualTo(TopicPath.Channel.TWIN); assertThat(adaptable.getTopicPath().getCriterion()).isEqualTo(TopicPath.Criterion.COMMANDS); assertThat(adaptable.getTopicPath().getAction()).contains(TopicPath.Action.MODIFY); assertThat(adaptable.getTopicPath().getNamespace()).isEqualTo(MAPPING_INCOMING_NAMESPACE); assertThat(adaptable.getTopicPath().getId()).isEqualTo(MAPPING_INCOMING_ID); assertThat(adaptable.getPayload().getPath().toString()).isEqualTo(MAPPING_INCOMING_PATH); assertThat(adaptable.getPayload().getValue()).map(JsonValue::asString).contains(modifyThingResponse.toJsonString()); assertThat(adaptable.getPayload().getStatus()).contains(HttpStatusCode.NO_CONTENT); }
@Test public void testPlainJavascriptIncomingMappingWithStatusAndExtra() { final String correlationId = UUID.randomUUID().toString(); final Map<String, String> headers = new HashMap<>(); headers.put(HEADER_CORRELATION_ID, correlationId); headers.put(ExternalMessage.CONTENT_TYPE_HEADER, CONTENT_TYPE_PLAIN); final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-plain"); final ModifyThingResponse modifyThingResponse = ModifyThingResponse.modified(thingId, DittoHeaders.newBuilder() .correlationId(correlationId) .putHeader("subject", "{{topic:action-subject}}") .build()); final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers) .withText(modifyThingResponse.toJsonString()) .build(); final long startTs = System.nanoTime(); final List<Adaptable> adaptables = javaScriptRhinoMapperPlainWithStatusAndExtra.map(message); final Adaptable adaptable = adaptables.get(0); System.out.println(adaptable); System.out.println( "testPlainJavascriptIncomingMapping Duration: " + (System.nanoTime() - startTs) / 1000000.0 + "ms"); try (final AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) { softly.assertThat(adaptable.getTopicPath()).satisfies(topicPath -> { softly.assertThat(topicPath.getChannel()).isEqualTo(TopicPath.Channel.TWIN); softly.assertThat(topicPath.getCriterion()).isEqualTo(TopicPath.Criterion.COMMANDS); softly.assertThat(topicPath.getAction()).contains(TopicPath.Action.MODIFY); softly.assertThat(topicPath.getNamespace()).isEqualTo(MAPPING_INCOMING_NAMESPACE); softly.assertThat(topicPath.getId()).isEqualTo(MAPPING_INCOMING_ID); }); softly.assertThat(adaptable.getPayload()).satisfies(payload -> { softly.assertThat(payload.getPath().toString()).isEqualTo(MAPPING_INCOMING_PATH); softly.assertThat(payload.getValue()).map(JsonValue::asString) .contains(modifyThingResponse.toJsonString()); softly.assertThat(payload.getStatus()).contains(HttpStatusCode.NO_CONTENT); softly.assertThat(payload.getExtra()).contains(JsonObject.newBuilder() .set("attributes", JsonObject.newBuilder().set("enriched", "field").build()) .build()); }); } }
@Test public void testPlainJavascriptOutgoingMapping() { final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-plain"); final String correlationId = UUID.randomUUID().toString(); final Thing newThing = Thing.newBuilder() .setId(thingId) .setAttributes(Attributes.newBuilder().set("foo", "bar").build()) .build(); final CreateThing createThing = CreateThing.of(newThing, null, DittoHeaders.newBuilder().correlationId(correlationId).build()); final Adaptable adaptable = DittoProtocolAdapter.newInstance().toAdaptable(createThing); assertThat(javaScriptRhinoMapperPlain.map(adaptable)).allSatisfy(rawMessage -> { System.out.println(rawMessage); final long startTs = System.nanoTime(); System.out.println( "testPlainJavascriptOutgoingMapping Duration: " + (System.nanoTime() - startTs) / 1_000_000.0 + "ms"); assertThat(rawMessage.findContentType()).contains(CONTENT_TYPE_PLAIN); assertThat(rawMessage.findHeader(HEADER_CORRELATION_ID)).contains(correlationId); assertThat(rawMessage.isTextMessage()).isTrue(); assertThat(rawMessage.getTextPayload()).contains("Thing ID was: " + thingId); }); }
@Test public void testEmptyJavascriptIncomingMapping() { final String correlationId = UUID.randomUUID().toString(); final Map<String, String> headers = new HashMap<>(); headers.put(HEADER_CORRELATION_ID, correlationId); headers.put(ExternalMessage.CONTENT_TYPE_HEADER, CONTENT_TYPE_PLAIN); final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers).build(); final long startTs = System.nanoTime(); final List<Adaptable> adaptables = javaScriptRhinoMapperEmpty.map(message); System.out.println( "testEmptyJavascriptIncomingMapping Duration: " + (System.nanoTime() - startTs) / 1_000_000.0 + "ms"); assertThat(adaptables).isEmpty(); }
@Test public void testEmptyJavascriptOutgoingMapping() { final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-empty"); final String correlationId = UUID.randomUUID().toString(); final Thing newThing = Thing.newBuilder() .setId(thingId) .setAttributes(Attributes.newBuilder().set("foo", "bar").build()) .build(); final CreateThing createThing = CreateThing.of(newThing, null, DittoHeaders.newBuilder().correlationId(correlationId).build()); final Adaptable adaptable = DittoProtocolAdapter.newInstance().toAdaptable(createThing); final long startTs = System.nanoTime(); final List<ExternalMessage> rawMessageOpt = javaScriptRhinoMapperEmpty.map(adaptable); System.out.println( "testEmptyJavascriptOutgoingMapping Duration: " + (System.nanoTime() - startTs) / 1_000_000.0 + "ms"); assertThat(rawMessageOpt).isEmpty(); }
@Test public void testBinaryJavascriptIncomingMapping() { final String correlationId = UUID.randomUUID().toString(); final Map<String, String> headers = new HashMap<>(); headers.put(HEADER_CORRELATION_ID, correlationId); headers.put(ExternalMessage.CONTENT_TYPE_HEADER, CONTENT_TYPE_BINARY); final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers) .withBytes(MAPPING_INCOMING_PAYLOAD_BYTES) .build(); assertThat(javaScriptRhinoMapperBinary.map(message)).allSatisfy(adaptable -> { System.out.println(adaptable); final long startTs = System.nanoTime(); System.out.println( "testBinaryJavascriptIncomingMapping Duration: " + (System.nanoTime() - startTs) / 1_000_000.0 + "ms"); assertThat(adaptable.getTopicPath()).satisfies(topicPath -> { assertThat(topicPath.getChannel()).isEqualTo(TopicPath.Channel.TWIN); assertThat(topicPath.getCriterion()).isEqualTo(TopicPath.Criterion.COMMANDS); assertThat(topicPath.getAction()).contains(TopicPath.Action.MODIFY); assertThat(topicPath.getNamespace()).isEqualTo(MAPPING_INCOMING_NAMESPACE); assertThat(topicPath.getId()).isEqualTo(MAPPING_INCOMING_ID); }); assertThat(adaptable.getPayload()).satisfies(payload -> { assertThat(payload.getPath().toString()).isEqualTo(MAPPING_INCOMING_PATH); assertThat(payload.getValue()).contains(JsonValue.of(MAPPING_INCOMING_PAYLOAD_STRING)); }); }); }
@Test public void testBinaryJavascriptOutgoingMapping() { final ThingId thingId = ThingId.of("org.eclipse.ditto:foo-bar-binary"); final String correlationId = UUID.randomUUID().toString(); final Thing newThing = Thing.newBuilder() .setId(thingId) .setAttributes(Attributes.newBuilder().set("foo", "bar").build()) .build(); final CreateThing createThing = CreateThing.of(newThing, null, DittoHeaders.newBuilder().correlationId(correlationId).build()); final Adaptable adaptable = DittoProtocolAdapter.newInstance().toAdaptable(createThing); assertThat(javaScriptRhinoMapperBinary.map(adaptable)).allSatisfy(rawMessage -> { System.out.println(rawMessage); final long startTs = System.nanoTime(); System.out.println( "testBinaryJavascriptOutgoingMapping Duration: " + (System.nanoTime() - startTs) / 1_000_000.0 + "ms"); assertThat(rawMessage.findContentType()).contains(CONTENT_TYPE_BINARY); assertThat(rawMessage.findHeader(HEADER_CORRELATION_ID)).contains(correlationId); assertThat(rawMessage.isTextMessage()).isFalse(); assertThat(rawMessage.isBytesMessage()).isTrue(); assertThat(rawMessage.getTextPayload()).isEmpty(); assertThat(rawMessage.getBytePayload()).map(JavaScriptMessageMapperRhinoTest::byteBuffer2String) .contains(thingId.toString()); }); }