View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformers.xml;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.module.xml.transformer.ObjectToXml;
13  import org.mule.module.xml.transformer.XmlToObject;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  import org.mule.tck.testmodels.fruit.Apple;
16  
17  import java.util.Set;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.assertTrue;
26  
27  public class XmlMuleMessageTransformersTestCase extends AbstractMuleContextTestCase
28  {
29  
30      @Test
31      public void testMessageSerialization() throws Exception
32      {
33          ObjectToXml t1 = createObject(ObjectToXml.class);
34          t1.setAcceptMuleMessage(true);
35  
36          MuleMessage msg = new DefaultMuleMessage("test", muleContext);
37          msg.setEncoding("UTF-8");
38          msg.setCorrelationId("1234");
39          msg.setInvocationProperty("number", 1);
40          msg.setOutboundProperty("object", new Apple());
41          msg.setOutboundProperty("string", "hello");
42  
43          String xml = (String) t1.transform(msg);
44          assertNotNull(xml);
45  
46          XmlToObject t2 = createObject(XmlToObject.class);
47  
48          Object result = t2.transform(xml);
49          assertNotNull(result);
50          assertTrue(result instanceof MuleMessage);
51  
52          msg = (MuleMessage) result;
53  
54          assertEquals("test", msg.getPayloadAsString());
55          assertEquals(new Apple(), msg.getOutboundProperty("object"));
56          //with different case
57          assertEquals(new Apple(), msg.getOutboundProperty("oBjeCt"));
58          //Make sure we don't have the property in a different scope
59          assertNull(msg.getInboundProperty("oBjeCt"));
60          assertNull(msg.getInvocationProperty("oBjeCt"));
61          assertNull(msg.getSessionProperty("oBjeCt"));
62  
63          assertEquals("hello", msg.getOutboundProperty("string"));
64          //with different case
65          assertEquals("hello", msg.getOutboundProperty("String"));
66          //Make sure we don't have the property in a different scope
67          assertNull(msg.getInboundProperty("string"));
68          assertNull(msg.getInvocationProperty("string"));
69          assertNull(msg.getSessionProperty("string"));
70  
71          assertEquals(1, msg.getInvocationProperty("number"));
72          //with different case
73          assertEquals(1, msg.getInvocationProperty("NUMBER"));
74          //Make sure we don't have the property in a different scope
75          assertNull(msg.getInboundProperty("number"));
76          assertNull(msg.getOutboundProperty("number"));
77          assertNull(msg.getSessionProperty("number"));
78  
79          assertEquals("1234", msg.getCorrelationId());
80          assertEquals("UTF-8", msg.getEncoding());
81  
82  
83          assertEquals(1, msg.getInvocationPropertyNames().size());
84          Set<String> outboundProps = msg.getOutboundPropertyNames();
85          assertEquals(4, outboundProps.size());
86  
87          //Remove Mule properties
88          outboundProps.remove(MuleProperties.MULE_CORRELATION_ID_PROPERTY);
89          outboundProps.remove(MuleProperties.MULE_ENCODING_PROPERTY);
90  
91          for (String key : outboundProps)
92          {
93              assertTrue(key.equals("number") || key.equals("string") || key.equals("object"));
94              assertFalse(key.equals("NUMBER") || key.equals("STRING") || key.equals("OBJECT"));
95          }
96      }
97  }