private Message createTestMessage() { return new DefaultMessageBuilder().value(TEST_PAYLOAD).mediaType(TEXT).build(); }
@Test public void inboundPropertyMapCopy() { Map<String, Serializable> inboundProperties = singletonMap(PROPERTY_KEY, PROPERTY_VALUE); InternalMessage copy = new DefaultMessageBuilder(new DefaultMessageBuilder().value(TEST_PAYLOAD) .inboundProperties(inboundProperties).build()).build(); assertThat(copy.getInboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(copy.getInboundPropertyDataType(PROPERTY_KEY), equalTo(STRING)); assertThat(copy.getInboundPropertyNames(), hasSize(1)); assertThat(copy.getInboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@Test public void copyPreservesDataType() { Apple apple = new Apple(); long appleSize = 111; Message message = new DefaultMessageBuilder().payload(new TypedValue(apple, fromObject(apple), OptionalLong.of(appleSize))).build(); Message copy = new DefaultMessageBuilder(message).build(); assertThat(copy.getPayload(), is(message.getPayload())); assertThat(copy.getAttributes(), is(message.getAttributes())); assertThat(message.getPayload().getByteLength().getAsLong(), is(appleSize)); assertThat(copy.getPayload().getByteLength().getAsLong(), is(appleSize)); }
@Test public void messageAttributesCopy() { assertTestMessage(new DefaultMessageBuilder(createTestMessage()).build()); }
private void copyMessageAttributes(InternalMessage message) { this.exceptionPayload = message.getExceptionPayload(); message.getInboundPropertyNames().forEach(key -> { if (message.getInboundPropertyDataType(key) != null) { addInboundProperty(key, message.getInboundProperty(key), message.getInboundPropertyDataType(key)); } else { addInboundProperty(key, message.getInboundProperty(key)); } }); message.getOutboundPropertyNames().forEach(key -> { if (message.getOutboundPropertyDataType(key) != null) { addOutboundProperty(key, message.getOutboundProperty(key), message.getOutboundPropertyDataType(key)); } else { addOutboundProperty(key, message.getOutboundProperty(key)); } }); message.getInboundAttachmentNames().forEach(name -> addInboundAttachment(name, message.getInboundAttachment(name))); message.getOutboundAttachmentNames().forEach(name -> addOutboundAttachment(name, message.getOutboundAttachment(name))); }
@Test public void mutateEntirePayload() { Message message = createTestMessage(); Message copy = new DefaultMessageBuilder(message).payload(new TypedValue<>(EMPTY_JSON, JSON_STRING)).build(); assertThat(copy.getPayload().getValue(), equalTo(EMPTY_JSON)); assertThat(copy.getPayload().getDataType().getType(), equalTo(String.class)); assertThat(copy.getPayload().getDataType().getMediaType(), is(APPLICATION_JSON)); }
@Override public InternalMessage.Builder create() { return new DefaultMessageBuilder(); }
@Override public InternalMessage.CollectionBuilder nullAttributesValue() { this.attributes = new TypedValue<>(null, resolveAttributesDataType(null)); return this; }
@Override public InternalMessage.CollectionBuilder collectionValue(Object[] payload) { requireNonNull(payload); return collectionValue(asList(payload), payload.getClass().getComponentType()); }
public DefaultMessageBuilder(org.mule.runtime.api.message.Message message) { requireNonNull(message); this.payload = message.getPayload(); this.attributes = message.getAttributes(); if (message instanceof InternalMessage) { copyMessageAttributes((InternalMessage) message); } }
@Test public void mutateEntirePayload() { Message message = createTestMessage(); Message copy = new DefaultMessageBuilder(message).payload(new TypedValue<>(EMPTY_JSON, JSON_STRING)).build(); assertThat(copy.getPayload().getValue(), equalTo(EMPTY_JSON)); assertThat(copy.getPayload().getDataType().getType(), equalTo(String.class)); assertThat(copy.getPayload().getDataType().getMediaType(), is(APPLICATION_JSON)); }
@Test public void messageAttributesCopy() { assertTestMessage(new DefaultMessageBuilder(createTestMessage()).build()); }
@Override public InternalMessage.Builder create(Message message) { return new DefaultMessageBuilder(message); }
@Override public InternalMessage.CollectionBuilder attributesValue(Object value) { this.attributes = new TypedValue<>(value, resolveAttributesDataType(value)); return this; }
@Test public void outboundPropertyMap() { Map<String, Serializable> outboundProperties = singletonMap(PROPERTY_KEY, PROPERTY_VALUE); InternalMessage message = new DefaultMessageBuilder().value(TEST_PAYLOAD).outboundProperties(outboundProperties).build(); assertThat(message.getOutboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(message.getOutboundPropertyDataType(PROPERTY_KEY), equalTo(STRING)); assertThat(message.getOutboundPropertyNames(), hasSize(1)); assertThat(message.getOutboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@Test public void outboundPropertyMapCopy() { Map<String, Serializable> outboundProperties = singletonMap(PROPERTY_KEY, PROPERTY_VALUE); InternalMessage copy = new DefaultMessageBuilder(new DefaultMessageBuilder().value(TEST_PAYLOAD) .outboundProperties(outboundProperties).build()).build(); assertThat(copy.getOutboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(copy.getOutboundPropertyDataType(PROPERTY_KEY), equalTo(STRING)); assertThat(copy.getOutboundPropertyNames(), hasSize(1)); assertThat(copy.getOutboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@Test public void copyPreservesDataType() { Apple apple = new Apple(); long appleSize = 111; Message message = new DefaultMessageBuilder().payload(new TypedValue(apple, fromObject(apple), OptionalLong.of(appleSize))).build(); Message copy = new DefaultMessageBuilder(message).build(); assertThat(copy.getPayload(), is(message.getPayload())); assertThat(copy.getAttributes(), is(message.getAttributes())); assertThat(message.getPayload().getByteLength().getAsLong(), is(appleSize)); assertThat(copy.getPayload().getByteLength().getAsLong(), is(appleSize)); }
@Test public void outboundPropertyDataType() { InternalMessage message = new DefaultMessageBuilder().value(TEST_PAYLOAD) .addOutboundProperty(PROPERTY_KEY, PROPERTY_VALUE, HTML_STRING).build(); assertThat(message.getOutboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(message.getOutboundPropertyDataType(PROPERTY_KEY), equalTo(HTML_STRING)); assertThat(message.getOutboundPropertyNames(), hasSize(1)); assertThat(message.getOutboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@Test public void inboundPropertyMapCopy() { Map<String, Serializable> inboundProperties = singletonMap(PROPERTY_KEY, PROPERTY_VALUE); InternalMessage copy = new DefaultMessageBuilder(new DefaultMessageBuilder().value(TEST_PAYLOAD) .inboundProperties(inboundProperties).build()).build(); assertThat(copy.getInboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(copy.getInboundPropertyDataType(PROPERTY_KEY), equalTo(STRING)); assertThat(copy.getInboundPropertyNames(), hasSize(1)); assertThat(copy.getInboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@Test public void inboundPropertyDataType() { InternalMessage message = new DefaultMessageBuilder().value(TEST_PAYLOAD).addInboundProperty(PROPERTY_KEY, PROPERTY_VALUE, HTML_STRING).build(); assertThat(message.getInboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(message.getInboundPropertyDataType(PROPERTY_KEY), equalTo(HTML_STRING)); assertThat(message.getInboundPropertyNames(), hasSize(1)); assertThat(message.getInboundPropertyNames(), hasItem(PROPERTY_KEY)); }