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;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.transport.PropertyScope;
11  import org.mule.session.DefaultMuleSession;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  import org.mule.tck.testmodels.fruit.Apple;
14  import org.mule.tck.testmodels.fruit.Orange;
15  import org.mule.transformer.types.MimeTypes;
16  import org.mule.transport.NullPayload;
17  import org.mule.util.IOUtils;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.activation.DataHandler;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNull;
28  import static org.junit.Assert.assertTrue;
29  import static org.junit.Assert.fail;
30  
31  public class DefaultMuleMessageTestCase extends AbstractMuleContextTestCase
32  {
33      //
34      // corner cases/errors
35      //
36      @Test
37      public void testConstructorWithNoMuleContext()
38      {
39          try
40          {
41              new DefaultMuleMessage(TEST_MESSAGE, null);
42              fail("DefaultMuleMessage must fail when created with null MuleContext");
43          }
44          catch (IllegalArgumentException iae)
45          {
46              // this one was expected
47          }
48      }
49  
50      @Test
51      public void testConstructorWithNullPayload()
52      {
53          MuleMessage message = new DefaultMuleMessage(null, muleContext);
54          assertEquals(NullPayload.getInstance(), message.getPayload());
55      }
56  
57      //
58      // payload-only ctor tests
59      //
60      @Test
61      public void testOneArgConstructor()
62      {
63          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
64          assertEquals(TEST_MESSAGE, message.getPayload());
65      }
66  
67      @Test
68      public void testOneArgConstructorWithMuleMessageAsPayload()
69      {
70          MuleMessage oldMessage = createMuleMessage();
71  
72          MuleMessage message = new DefaultMuleMessage(oldMessage, muleContext);
73          assertEquals("MULE_MESSAGE", message.getPayload());
74          assertOutboundMessageProperty("MuleMessage", message);
75      }
76  
77      //
78      // ctor with message properties
79      //
80      @Test
81      public void testMessagePropertiesConstructor()
82      {
83          Map<String, Object> properties = createMessageProperties();
84  
85          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, properties, muleContext);
86          assertEquals(TEST_MESSAGE, message.getPayload());
87          assertOutboundMessageProperty("MessageProperties", message);
88      }
89  
90      @Test
91      public void testMessagePropertiesAccessors()
92      {
93          Map<String, Object> properties = createMessageProperties();
94  
95          properties.put("number", "24");
96          properties.put("decimal", "24.3");
97          properties.put("boolean", "true");
98          Apple apple = new Apple(true);
99          properties.put("apple", apple);
100         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, properties, muleContext);
101         assertTrue(message.getOutboundProperty("boolean", false));
102         assertEquals(new Integer(24), message.getOutboundProperty("number", 0));
103         assertEquals(new Byte((byte) 24), message.getOutboundProperty("number", (byte) 0));
104         assertEquals(new Long(24), message.getOutboundProperty("number", 0l));
105         assertEquals(new Float(24.3), message.getOutboundProperty("decimal", 0f));
106         Double d = message.getOutboundProperty("decimal", 0d);
107         assertEquals(new Double(24.3), d);
108 
109         assertEquals("true", message.getOutboundProperty("boolean", ""));
110 
111         assertEquals(apple, message.getOutboundProperty("apple"));
112         try
113         {
114             message.getOutboundProperty("apple", new Orange());
115             fail("Orange is not assignable to Apple");
116         }
117         catch (IllegalArgumentException e)
118         {
119             //expected
120         }
121 
122         //Test null
123         assertNull(message.getOutboundProperty("banana"));
124         assertNull(message.getOutboundProperty("blah"));
125 
126         //Test default value
127         assertEquals(new Float(24.3), message.getOutboundProperty("blah", 24.3f));
128 
129     }
130 
131     @Test
132     public void testMessagePropertiesConstructorWithMuleMessageAsPayload()
133     {
134         Map<String, Object> properties = createMessageProperties();
135         MuleMessage previousMessage = createMuleMessage();
136 
137         MuleMessage message = new DefaultMuleMessage(previousMessage, properties, muleContext);
138         assertEquals("MULE_MESSAGE", message.getPayload());
139         assertOutboundMessageProperty("MessageProperties", message);
140         assertOutboundMessageProperty("MuleMessage", message);
141     }
142 
143     //
144     // ctor with previous message
145     //
146     @Test
147     public void testPreviousMessageConstructorWithRegularPayloadAndMuleMessageAsPrevious()
148     {
149         MuleMessage previous = createMuleMessage();
150 
151         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, previous, muleContext);
152         assertEquals(TEST_MESSAGE, message.getPayload());
153         assertOutboundMessageProperty("MuleMessage", message);
154         assertEquals(previous.getUniqueId(), message.getUniqueId());
155     }
156 
157     @Test
158     public void testPreviousMessageConstructorWithMuleMessageAsPayloadAndMuleMessageAsPrevious()
159     {
160         MuleMessage payload = createMuleMessage();
161         payload.setOutboundProperty("payload", "payload");
162 
163         MuleMessage previous = createMuleMessage();
164         previous.setOutboundProperty("previous", "previous");
165 
166         MuleMessage message = new DefaultMuleMessage(payload, previous, muleContext);
167         assertEquals("MULE_MESSAGE", message.getPayload());
168         assertOutboundMessageProperty("MuleMessage", message);
169         assertOutboundMessageProperty("payload", message);
170         assertEquals(previous.getUniqueId(), message.getUniqueId());
171     }
172 
173     @Test
174     public void testClearProperties()
175     {
176         MuleMessage payload = createMuleMessage();
177         payload.setOutboundProperty("foo", "fooValue");
178         payload.setInvocationProperty("bar", "barValue");
179 
180         assertEquals(1, payload.getInvocationPropertyNames().size());
181         assertEquals(2, payload.getOutboundPropertyNames().size());
182         assertEquals(0, payload.getInboundPropertyNames().size());
183 
184         payload.clearProperties(PropertyScope.INVOCATION);
185         assertEquals(0, payload.getInvocationPropertyNames().size());
186 
187         payload.clearProperties(PropertyScope.OUTBOUND);
188         assertEquals(0, payload.getOutboundPropertyNames().size());
189 
190         //See http://www.mulesoft.org/jira/browse/MULE-4968 for additional test needed here
191     }
192 
193     //
194     // copy ctor
195     //
196     @Test
197     public void testCopyConstructor() throws Exception
198     {
199         DefaultMuleMessage original = (DefaultMuleMessage) createMuleMessage();
200         Map<String, Object> properties = createMessageProperties();
201         original.addInboundProperties(properties);
202         assertInboundAndOutboundMessageProperties(original);
203 
204         MuleMessage copy = new DefaultMuleMessage(original);
205         assertInboundAndOutboundMessageProperties(copy);
206     }
207 
208     private void assertInboundAndOutboundMessageProperties(MuleMessage original)
209     {
210         assertOutboundMessageProperty("MuleMessage", original);
211         assertEquals("MessageProperties", original.getInboundProperty("MessageProperties"));
212     }
213 
214     //
215     // attachments
216     //
217     @Test
218     public void testLegacyAddingAttachment() throws Exception
219     {
220         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
221 
222         DataHandler handler = new DataHandler("this is the attachment", "text/plain");
223         message.addOutboundAttachment("attachment", handler);
224 
225         assertTrue(message.getOutboundAttachmentNames().contains("attachment"));
226         assertEquals(handler, message.getOutboundAttachment("attachment"));
227     }
228 
229     @Test
230     public void testAddingOutboundAttachment() throws Exception
231     {
232         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
233 
234         DataHandler handler = new DataHandler("this is the attachment", "text/plain");
235         message.addOutboundAttachment("attachment", handler);
236 
237         assertTrue(message.getOutboundAttachmentNames().contains("attachment"));
238         assertEquals(handler, message.getOutboundAttachment("attachment"));
239         assertEquals(0, message.getInboundAttachmentNames().size());
240 
241         message.removeOutboundAttachment("attachment");
242         assertEquals(0, message.getOutboundAttachmentNames().size());
243 
244         //Try with content type set
245         message.addOutboundAttachment("spi-props", IOUtils.getResourceAsUrl("test-spi.properties", getClass()), MimeTypes.TEXT);
246 
247         assertTrue(message.getOutboundAttachmentNames().contains("spi-props"));
248         handler = message.getOutboundAttachment("spi-props");
249         assertEquals(MimeTypes.TEXT, handler.getContentType());
250         assertEquals(1, message.getOutboundAttachmentNames().size());
251 
252         //Try without content type set
253         message.addOutboundAttachment("dummy", IOUtils.getResourceAsUrl("dummy.xml", getClass()), null);
254         handler = message.getOutboundAttachment("dummy");
255         assertEquals(MimeTypes.APPLICATION_XML, handler.getContentType());
256         assertEquals(2, message.getOutboundAttachmentNames().size());
257 
258 
259     }
260 
261     @Test
262     public void testAddingInboundAttachment() throws Exception
263     {
264         Map<String, DataHandler> attachments = new HashMap<String, DataHandler>();
265 
266         DataHandler dh = new DataHandler("this is the attachment", "text/plain");
267         attachments.put("attachment", dh);
268         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, null, attachments, muleContext);
269 
270         assertTrue(message.getInboundAttachmentNames().contains("attachment"));
271         assertEquals(dh, message.getInboundAttachment("attachment"));
272 
273         assertEquals(0, message.getOutboundAttachmentNames().size());
274 
275     }
276 
277     @Test
278     public void testNewMuleMessageFromMuleMessageWithAttachment() throws Exception
279     {
280         MuleMessage previous = createMuleMessage();
281         DataHandler handler = new DataHandler("this is the attachment", "text/plain");
282         previous.addOutboundAttachment("attachment", handler);
283 
284         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, previous, muleContext);
285         assertTrue(message.getOutboundAttachmentNames().contains("attachment"));
286         assertEquals(handler, message.getOutboundAttachment("attachment"));
287     }
288 
289     @Test
290     public void testFindPropertiesInAnyScope() throws Exception
291     {
292         MuleMessage message = createMuleMessage();
293         //Not sure why this test adds this property
294         message.removeProperty("MuleMessage", PropertyScope.OUTBOUND);
295 
296         //We need a session and current event for this test
297         RequestContext.setEvent(new DefaultMuleEvent(
298                 message,
299                 getTestInboundEndpoint("foo"),
300                 new DefaultMuleSession(muleContext)));
301 
302         message.setOutboundProperty("foo", "fooOutbound");
303         message.setInvocationProperty("bar", "barInvocation");
304         message.setInvocationProperty("foo", "fooInvocation");
305         message.setProperty("foo", "fooInbound", PropertyScope.INBOUND);
306         message.setSessionProperty("foo", "fooSession");
307 
308 
309         assertEquals(2, message.getInvocationPropertyNames().size());
310         assertEquals(1, message.getOutboundPropertyNames().size());
311         assertEquals(1, message.getInboundPropertyNames().size());
312         assertEquals(1, message.getSessionPropertyNames().size());
313 
314         String value = message.findPropertyInAnyScope("foo", null);
315         assertEquals("fooOutbound", value);
316 
317         message.removeProperty("foo", PropertyScope.OUTBOUND);
318 
319         value = message.findPropertyInAnyScope("foo", null);
320         assertEquals("fooInvocation", value);
321 
322         message.removeProperty("foo", PropertyScope.INVOCATION);
323 
324         value = message.findPropertyInAnyScope("foo", null);
325         assertEquals("fooSession", value);
326         message.removeProperty("foo", PropertyScope.SESSION);
327 
328         value = message.findPropertyInAnyScope("foo", null);
329         assertEquals("fooInbound", value);
330 
331         value = message.findPropertyInAnyScope("bar", null);
332         assertEquals("barInvocation", value);
333 
334     }
335 
336     //
337     // helpers
338     //
339     private Map<String, Object> createMessageProperties()
340     {
341         HashMap<String, Object> map = new HashMap<String, Object>();
342         map.put("MessageProperties", "MessageProperties");
343         return map;
344     }
345 
346     private MuleMessage createMuleMessage()
347     {
348         MuleMessage previousMessage = new DefaultMuleMessage("MULE_MESSAGE", muleContext);
349         previousMessage.setOutboundProperty("MuleMessage", "MuleMessage");
350         return previousMessage;
351     }
352 
353     private void assertOutboundMessageProperty(String key, MuleMessage message)
354     {
355         // taking advantage of the fact here that key and value are the same
356         assertEquals(key, message.getOutboundProperty(key));
357     }
358 }