@Test public void usesNullPayloadAsNull() throws Exception { Message message = InternalMessage.builder(createMuleMessage()).addOutboundProperty(FOO_PROPERTY, null).build(); assertThat(((InternalMessage) message).getOutboundProperty(FOO_PROPERTY), is(nullValue())); }
protected PrivilegedEvent createEvent(String payload, DataType dataType, Object attributes, DataType attributesDataType) { InternalMessage message = mock(InternalMessage.class); when(message.getPayload()).thenReturn(new TypedValue<>(payload, dataType)); when(message.getAttributes()).thenReturn(new TypedValue<>(attributes, attributesDataType)); try { return this.<PrivilegedEvent.Builder>getEventBuilder().message(message).build(); } catch (MuleException e) { throw new MuleRuntimeException(e); } }
@Test public void addingInboundAttachmentsRespectOrder() { InternalMessage message = InternalMessage.builder() .payload(TypedValue.of("test")) .mediaType(TEXT) .addInboundAttachment("another", new DataHandler(new ByteArrayDataSource("no".getBytes(), TEXT, "another"))) .addInboundAttachment("field1", new DataHandler(new ByteArrayDataSource("yes".getBytes(), TEXT, "field1"))) .exceptionPayload(new DefaultExceptionPayload(new NullPointerException("error"))) .build(); validateAttachments(message.getInboundAttachmentNames()); }
@Test public void inboundPropertyMap() { Map<String, Serializable> inboundProperties = singletonMap(PROPERTY_KEY, PROPERTY_VALUE); InternalMessage message = new DefaultMessageBuilder().value(TEST_PAYLOAD).inboundProperties(inboundProperties).build(); assertThat(message.getInboundProperty(PROPERTY_KEY), equalTo(PROPERTY_VALUE)); assertThat(message.getInboundPropertyDataType(PROPERTY_KEY), equalTo(STRING)); assertThat(message.getInboundPropertyNames(), hasSize(1)); assertThat(message.getInboundPropertyNames(), hasItem(PROPERTY_KEY)); }
@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 testClearProperties() { InternalMessage payload = InternalMessage.builder(createMuleMessage()).addOutboundProperty(FOO_PROPERTY, "fooValue").build(); assertThat(payload.getOutboundPropertyNames(), hasSize(2)); assertThat(payload.getInboundPropertyNames(), empty()); payload = InternalMessage.builder(payload).outboundProperties(emptyMap()).build(); assertThat(payload.getOutboundPropertyNames(), empty()); // See http://www.mulesoft.org/jira/browse/MULE-4968 for additional test needed here }
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 payloadAsType() throws Exception { InternalMessage transformedMessage = mock(InternalMessage.class, RETURNS_DEEP_STUBS); final TypedValue<Object> expectedPayload = new TypedValue<>(new Object(), OBJECT); when(transformedMessage.getPayload()).thenReturn(expectedPayload); TransformationService transformationService = mock(TransformationService.class); muleContext.setTransformationService(transformationService); when(transformationService.transform(any(InternalMessage.class), any(DataType.class))).thenReturn(transformedMessage); assertSame(transformedMessage.getPayload().getValue(), evaluate("message.payloadAs(org.mule.tck.testmodels.fruit.Banana)", event)); }
@Test public void testStreamPayloadSerialization() throws Exception { InputStream stream = new ByteArrayInputStream(TEST_MESSAGE.getBytes()); final Message message = InternalMessage.builder().value(stream).addOutboundProperty("foo", "bar").build(); currentMuleContext.set(muleContext); InternalMessage deserializedMessage = serializationRoundtrip(message); assertEquals(byte[].class, deserializedMessage.getPayload().getDataType().getType()); byte[] payload = (byte[]) deserializedMessage.getPayload().getValue(); assertTrue(Arrays.equals(TEST_MESSAGE.getBytes(), payload)); }
@Test public void parseTemplateFromLocation() throws InitialisationException, IOException { parseTemplateProcessor.setLocation(LOCATION); parseTemplateProcessor.initialise(); when(mockMuleMessage.getInboundProperty("errorMessage")).thenReturn("ERROR!!!"); String expectedExpression = IOUtils.getResourceAsString(LOCATION, this.getClass()); when(mockMuleMessage.getPayload()).thenReturn(of("Parsed")); when(mockMuleMessage.getAttributes()).thenReturn(of(new HashMap<>())); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertNotNull(response); assertEquals("Parsed", response.getMessage().getPayload().getValue()); // Call a second time to make sure the template is stored once the transformer has been initialized response = parseTemplateProcessor.process(event); assertNotNull(response); assertEquals("Parsed", response.getMessage().getPayload().getValue()); }
public static void headersToStringBuilder(InternalMessage m, StringBuilder buf) { buf.append(" Message properties:").append(lineSeparator()); try { if (!m.getInboundPropertyNames().isEmpty()) { Set<String> inboundNames = new TreeSet(m.getInboundPropertyNames()); buf.append(" ").append(INBOUND_NAME).append(" scoped properties:").append(lineSeparator()); appendPropertyValues(m, buf, inboundNames, name -> m.getInboundProperty(name)); } if (!m.getOutboundPropertyNames().isEmpty()) { Set<String> outboundNames = new TreeSet(m.getOutboundPropertyNames()); buf.append(" ").append(OUTBOUND_NAME).append(" scoped properties:").append(lineSeparator()); appendPropertyValues(m, buf, outboundNames, name -> m.getOutboundProperty(name)); } } catch (IllegalArgumentException e) { // ignored } }
@Test public void addingOutboundAttachmentsRespectOrder() { InternalMessage message = InternalMessage.builder() .payload(TypedValue.of("test")) .mediaType(TEXT) .addOutboundAttachment("another", new DataHandler(new ByteArrayDataSource("no".getBytes(), TEXT, "another"))) .addOutboundAttachment("field1", new DataHandler(new ByteArrayDataSource("yes".getBytes(), TEXT, "field1"))) .exceptionPayload(new DefaultExceptionPayload(new NullPointerException("error"))) .build(); validateAttachments(message.getOutboundAttachmentNames()); }
private void assertSplitParts(int count, boolean counted, Map<String, Serializable> inboundProps, Map<String, Serializable> outboundProps, Map<String, Object> invocationProps, List<CoreEvent> splits, Set<Object> actualSequences) { for (CoreEvent event : splits) { Message msg = event.getMessage(); assertTrue(msg.getPayload().getValue() instanceof String); if (counted) { assertThat(event.getGroupCorrelation().get().getGroupSize().getAsInt(), is(count)); } else { assertThat(event.getGroupCorrelation().get().getGroupSize().isPresent(), is(false)); } actualSequences.add(event.getGroupCorrelation().get().getSequence()); String str = (String) msg.getPayload().getValue(); assertTrue(TEST_LIST_MULTIPLE.contains(str)); for (String key : inboundProps.keySet()) { assertEquals(((InternalMessage) msg).getInboundProperty(key), inboundProps.get(key)); } for (String key : outboundProps.keySet()) { assertEquals(((InternalMessage) msg).getOutboundProperty(key), outboundProps.get(key)); } for (String key : invocationProps.keySet()) { assertEquals(event.getVariables().get(key).getValue(), invocationProps.get(key)); } } }
@Override public String toString() { Map<String, Object> map = new HashMap<String, Object>(); for (String key : ((InternalMessage) event.getMessage()).getInboundPropertyNames()) { Object value = ((InternalMessage) event.getMessage()).getInboundProperty(key); map.put(key, value); } return map.toString(); } }
@Override public String toString() { Map<String, Object> map = new HashMap<>(); for (String key : ((InternalMessage) event.getMessage()).getOutboundPropertyNames()) { Object value = ((InternalMessage) event.getMessage()).getOutboundProperty(key); map.put(key, value); } return map.toString(); } }
@Test public void assignValueToNewOutboundProperty() throws Exception { CoreEvent.Builder eventBuilder = CoreEvent.builder(event); evaluate("message.outboundProperties['foo']='bar'", event, eventBuilder); assertEquals("bar", ((InternalMessage) eventBuilder.build().getMessage()).getOutboundProperty("foo")); }
@Override public Serializable getCredentials(CoreEvent event) { return ((InternalMessage) event.getMessage()).getInboundProperty(MULE_USER_PROPERTY); } }
@Test public void attributes() throws Exception { Object attributes = mock(Object.class); when(message.getAttributes()).thenReturn(new TypedValue<>(attributes, DataType.OBJECT)); assertThat(evaluate(ATTRIBUTES, event), sameInstance(attributes)); }
private void assertInboundPropertyDataType(InternalMessage muleMessage, DataType dataType) { DataType actualDataType = muleMessage.getInboundPropertyDataType(TEST_PROPERTY); assertThat(actualDataType, like(dataType)); }