ScriptedIncomingMapping implements MappingFunction<ExternalMessage, List<Adaptable>> { static NativeObject mapExternalMessageToNativeObject(final ExternalMessage message) { final NativeObject headersObj = new NativeObject(); message.getHeaders().forEach((key, value) -> headersObj.put(key, headersObj, value)); final NativeArrayBuffer bytePayload = message.getBytePayload() .map(bb -> { final NativeArrayBuffer nativeArrayBuffer = new NativeArrayBuffer(bb.remaining()); bb.get(nativeArrayBuffer.getBuffer()); return nativeArrayBuffer; }) .orElse(null); final String contentType = message.getHeaders().get(ExternalMessage.CONTENT_TYPE_HEADER); final String textPayload = message.getTextPayload().orElse(null); final NativeObject externalMessage = new NativeObject(); externalMessage.put(EXTERNAL_MESSAGE_HEADERS, externalMessage, headersObj); externalMessage.put(EXTERNAL_MESSAGE_TEXT_PAYLOAD, externalMessage, textPayload); externalMessage.put(EXTERNAL_MESSAGE_BYTE_PAYLOAD, externalMessage, bytePayload); externalMessage.put(EXTERNAL_MESSAGE_CONTENT_TYPE, externalMessage, contentType); return externalMessage; } ScriptedIncomingMapping(@Nullable final ContextFactory contextFactory, @Nullable final Scriptable scope); @Override List<Adaptable> apply(final ExternalMessage message);  }
@Test public void mapExternalMessageTextAndBytes() { final ExternalMessage externalMessage = ExternalMessageFactory .newExternalMessageBuilder(new HashMap<>()) .withTextAndBytes(PAYLOAD, ByteBuffer.wrap(BYTES)) .build(); final NativeObject nativeObject = ScriptedIncomingMapping.mapExternalMessageToNativeObject(externalMessage); final String textPayload = (String) nativeObject.get("textPayload"); final NativeArrayBuffer bytePayload = (NativeArrayBuffer) nativeObject.get("bytePayload"); assertThat(textPayload).isEqualTo(PAYLOAD); assertThat(bytePayload.getBuffer()).isEqualTo(BYTES); }