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.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  
13  import java.util.Map;
14  
15  import javax.activation.DataHandler;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public class OutboundAttachmentsAnnotationTestCase extends FunctionalTestCase
24  {
25      public OutboundAttachmentsAnnotationTestCase()
26      {
27          setDisposeContextPerClass(true);
28      }
29  
30      @Override
31      protected String getConfigResources()
32      {
33          return "org/mule/test/annotations/outbound-attachments-annotation.xml";
34      }
35  
36      @Test
37      public void testProcessAttachment() throws Exception
38      {
39          MuleClient client = new MuleClient(muleContext);
40          MuleMessage message = client.send("vm://attachment", null, null);
41          assertNotNull("return message from MuleClient.send() should not be null", message);
42          assertTrue("Message payload should be a Map", message.getPayload() instanceof Map);
43          Map<String, DataHandler> result = getMapPayload(message);
44          assertEquals("barValue", result.get("bar").getContent());
45      }
46  
47      @Test
48      public void testProcessAttachmentWithExistingOutAttachments() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          MuleMessage message = client.send("vm://attachment2", null, null);
52          assertNotNull("return message from MuleClient.send() should not be null", message);
53          assertTrue("Message payload should be a Map", message.getPayload() instanceof Map);
54          Map<String, DataHandler> result = getMapPayload(message);
55          assertEquals("barValue", result.get("bar").getContent());
56          assertEquals("fooValue", result.get("foo").getContent());
57      }
58  
59      @Test
60      public void testInvalidParamType() throws Exception
61      {
62          MuleClient client = new MuleClient(muleContext);
63          MuleMessage message = client.send("vm://invalid", null, null);
64          assertNotNull("return message from MuleClient.send() should not be null", message);
65          assertNotNull(message.getExceptionPayload());
66          assertTrue(message.getExceptionPayload().getRootException() instanceof IllegalArgumentException);
67      }
68      
69      @SuppressWarnings("unchecked")
70      private Map<String, DataHandler> getMapPayload(MuleMessage message)
71      {
72          return (Map<String, DataHandler>) message.getPayload();
73      }
74  }