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.api.annotations.param;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.util.StringDataSource;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import javax.activation.DataHandler;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  public class MixedAnnotationsTestCase extends FunctionalTestCase
28  {
29      private MuleMessage muleMessage;
30  
31      public MixedAnnotationsTestCase()
32      {
33          setDisposeContextPerClass(true);
34      }
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "org/mule/test/annotations/mixed-annotations.xml";
40      }
41  
42      @Override
43      public void doSetUp() throws Exception
44      {
45          super.doSetUp();
46  
47          Map<String, Object> props = new HashMap<String, Object>(3);
48          props.put("foo", "fooValue");
49          props.put("bar", "barValue");
50          props.put("baz", "bazValue");
51  
52          muleMessage = new DefaultMuleMessage("test", props, muleContext);
53  
54          try
55          {
56              muleMessage.addOutboundAttachment("foo", new DataHandler(new StringDataSource("fooValue")));
57              muleMessage.addOutboundAttachment("bar", new DataHandler(new StringDataSource("barValue")));
58              muleMessage.addOutboundAttachment("baz", new DataHandler(new StringDataSource("bazValue")));
59          }
60          catch (Exception e)
61          {
62              e.printStackTrace();
63              fail(e.getMessage());
64          }
65      }
66  
67      @Test
68      public void testProcessAllAnnotated() throws Exception
69      {
70          MuleClient client = new MuleClient(muleContext);
71          MuleMessage message = client.send("vm://allAnnotated", muleMessage);
72          assertNotNull("return message from MuleClient.send() should not be null", message);
73          assertTrue("Message payload should be a Map", message.getPayload() instanceof Map);
74          Map<?, ?> result = (Map<?, ?>) message.getPayload();
75          assertEquals(3, result.size());
76  
77          //Payload
78          assertEquals("test", result.get("payload"));
79  
80          //Headers
81          assertNotNull(result.get("inboundHeaders"));
82          Map<?, ?> headers = (Map<?, ?>)result.get("inboundHeaders");
83          assertEquals(2, headers.size());
84          assertEquals("fooValue", headers.get("foo"));
85          assertEquals("barValue", headers.get("bar"));
86  
87          //Attachments
88          assertNotNull(result.get("inboundAttachments"));
89          Map<?, ?> attachments = (Map<?, ?>)result.get("inboundAttachments");
90          assertEquals(3, attachments.size());
91          assertNotNull(attachments.get("foo"));
92          assertNotNull(attachments.get("bar"));
93          assertNotNull(attachments.get("baz"));
94      }
95  
96      @Test
97      public void testPayloadNotAnnotated() throws Exception
98      {
99          //When using param annotations every param needs t obe annotated
100         MuleClient client = new MuleClient(muleContext);
101         MuleMessage message = client.send("vm://someAnnotated", muleMessage);
102         assertNotNull("return message from MuleClient.send() should not be null", message);
103         assertNotNull(message.getExceptionPayload());
104         assertTrue(message.getExceptionPayload().getRootException() instanceof IllegalArgumentException);
105         assertEquals("wrong number of arguments", message.getExceptionPayload().getRootException().getMessage());
106     }
107 }