View Javadoc

1   /*
2    * $Id: MixedAnnotationsTestCase.java 22735 2011-08-25 16:02:35Z 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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import org.mule.DefaultMuleMessage;
19  import org.mule.api.MuleMessage;
20  import org.mule.module.client.MuleClient;
21  import org.mule.tck.AbstractServiceAndFlowTestCase;
22  import org.mule.util.ExceptionUtils;
23  import org.mule.util.StringDataSource;
24  
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import javax.activation.DataHandler;
31  
32  import org.junit.Test;
33  import org.junit.runners.Parameterized.Parameters;
34  
35  public class MixedAnnotationsTestCase extends AbstractServiceAndFlowTestCase
36  {
37      private MuleMessage muleMessage;
38  
39      @Parameters
40      public static Collection<Object[]> parameters()
41      {
42          return Arrays.asList(new Object[][]{
43              {ConfigVariant.SERVICE, "org/mule/test/annotations/mixed-annotations-service.xml"},
44              {ConfigVariant.FLOW, "org/mule/test/annotations/mixed-annotations-flow.xml"}});
45      }
46  
47      public MixedAnnotationsTestCase(ConfigVariant variant, String configResources)
48      {
49          super(variant, configResources);
50          setDisposeContextPerClass(true);
51      }
52  
53      @Override
54      public void doSetUp() throws Exception
55      {
56          super.doSetUp();
57  
58          Map<String, Object> props = new HashMap<String, Object>(3);
59          props.put("foo", "fooValue");
60          props.put("bar", "barValue");
61          props.put("baz", "bazValue");
62  
63          muleMessage = new DefaultMuleMessage("test", props, muleContext);
64  
65          try
66          {
67              muleMessage.addOutboundAttachment("foo", new DataHandler(new StringDataSource("fooValue")));
68              muleMessage.addOutboundAttachment("bar", new DataHandler(new StringDataSource("barValue")));
69              muleMessage.addOutboundAttachment("baz", new DataHandler(new StringDataSource("bazValue")));
70          }
71          catch (Exception e)
72          {
73              e.printStackTrace();
74              fail(e.getMessage());
75          }
76      }
77  
78      @Test
79      public void testProcessAllAnnotated() throws Exception
80      {
81          MuleClient client = new MuleClient(muleContext);
82          MuleMessage message = client.send("vm://allAnnotated", muleMessage);
83          assertNotNull("return message from MuleClient.send() should not be null", message);
84          assertTrue("Message payload should be a Map", message.getPayload() instanceof Map);
85          Map<?, ?> result = (Map<?, ?>) message.getPayload();
86          assertEquals(3, result.size());
87  
88          // Payload
89          assertEquals("test", result.get("payload"));
90  
91          // Headers
92          assertNotNull(result.get("inboundHeaders"));
93          Map<?, ?> headers = (Map<?, ?>) result.get("inboundHeaders");
94          assertEquals(2, headers.size());
95          assertEquals("fooValue", headers.get("foo"));
96          assertEquals("barValue", headers.get("bar"));
97  
98          // Attachments
99          assertNotNull(result.get("inboundAttachments"));
100         Map<?, ?> attachments = (Map<?, ?>) result.get("inboundAttachments");
101         assertEquals(3, attachments.size());
102         assertNotNull(attachments.get("foo"));
103         assertNotNull(attachments.get("bar"));
104         assertNotNull(attachments.get("baz"));
105     }
106 
107     @Test
108     public void testPayloadNotAnnotated() throws Exception
109     {
110         // When using param annotations every param needs t obe annotated
111         MuleMessage message = muleContext.getClient().send("vm://someAnnotated", muleMessage);
112         assertNotNull(message);
113         assertNotNull(message.getExceptionPayload());
114         assertEquals(IllegalArgumentException.class,
115             ExceptionUtils.getRootCause(message.getExceptionPayload().getException()).getClass());
116     }
117 }