View Javadoc

1   /*
2    * $Id: MixedAnnotationsTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.api.annotations.param;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.util.StringDataSource;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.activation.DataHandler;
23  
24  public class MixedAnnotationsTestCase extends FunctionalTestCase
25  {
26      private MuleMessage muleMessage;
27  
28      public MixedAnnotationsTestCase()
29      {
30          setDisposeManagerPerSuite(true);
31      }
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "org/mule/test/annotations/mixed-annotations.xml";
37      }
38  
39      @Override
40      public void doSetUp() throws Exception
41      {
42          super.doSetUp();
43          
44          Map<String, Object> props = new HashMap<String, Object>(3);
45          props.put("foo", "fooValue");
46          props.put("bar", "barValue");
47          props.put("baz", "bazValue");
48  
49          muleMessage = new DefaultMuleMessage("test", props, muleContext);
50  
51          try
52          {
53              muleMessage.addAttachment("foo", new DataHandler(new StringDataSource("fooValue")));
54              muleMessage.addAttachment("bar", new DataHandler(new StringDataSource("barValue")));
55              muleMessage.addAttachment("baz", new DataHandler(new StringDataSource("bazValue")));
56          }
57          catch (Exception e)
58          {
59              e.printStackTrace();
60              fail(e.getMessage());
61          }
62      }
63  
64      public void testProcessAllAnnotated() throws Exception
65      {
66          MuleClient client = new MuleClient(muleContext);
67          MuleMessage message = client.send("vm://allAnnotated", muleMessage);
68          assertNotNull("return message from MuleClient.send() should not be null", message);
69          assertTrue("Message payload should be a Map", message.getPayload() instanceof Map);
70          Map<?, ?> result = (Map<?, ?>) message.getPayload();
71          assertEquals(3, result.size());
72  
73          //Payload
74          assertEquals("test", result.get("payload"));
75  
76          //Headers
77          assertNotNull(result.get("inboundHeaders"));
78          Map<?, ?> headers = (Map<?, ?>)result.get("inboundHeaders");
79          assertEquals(2, headers.size());
80          assertEquals("fooValue", headers.get("foo"));
81          assertEquals("barValue", headers.get("bar"));
82  
83          //Attachments
84          assertNotNull(result.get("inboundAttachments"));
85          Map<?, ?> attachments = (Map<?, ?>)result.get("inboundAttachments");
86          assertEquals(3, attachments.size());
87          assertNotNull(attachments.get("foo"));
88          assertNotNull(attachments.get("bar"));
89          assertNotNull(attachments.get("baz"));
90      }
91  
92      public void testPayloadNotAnnotated() throws Exception
93      {
94          //When using param annotations every param needs t obe annotated
95          MuleClient client = new MuleClient(muleContext);
96          MuleMessage message = client.send("vm://someAnnotated", muleMessage);
97          assertNotNull("return message from MuleClient.send() should not be null", message);
98          assertNotNull(message.getExceptionPayload());
99          assertTrue(message.getExceptionPayload().getRootException() instanceof IllegalArgumentException);
100         assertEquals("wrong number of arguments", message.getExceptionPayload().getRootException().getMessage());
101     }
102 }