View Javadoc

1   /*
2    * $Id: OutboundAttachmentsAnnotationTestCase.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  
17  import org.mule.api.MuleMessage;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.AbstractServiceAndFlowTestCase;
20  import org.mule.util.ExceptionUtils;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Map;
25  
26  import javax.activation.DataHandler;
27  
28  import org.junit.Test;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  public class OutboundAttachmentsAnnotationTestCase extends AbstractServiceAndFlowTestCase
32  {
33      @Parameters
34      public static Collection<Object[]> parameters()
35      {
36          return Arrays.asList(new Object[][]{
37              {ConfigVariant.SERVICE, "org/mule/test/annotations/outbound-attachments-annotation-service.xml"},
38              {ConfigVariant.FLOW, "org/mule/test/annotations/outbound-attachments-annotation-flow.xml"}});
39      }
40  
41      public OutboundAttachmentsAnnotationTestCase(ConfigVariant variant, String configResources)
42      {
43          super(variant, configResources);
44          setDisposeContextPerClass(true);
45      }
46  
47      @Test
48      public void testProcessAttachment() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          MuleMessage message = client.send("vm://attachment", 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      }
57  
58      @Test
59      public void testProcessAttachmentWithExistingOutAttachments() throws Exception
60      {
61          MuleClient client = new MuleClient(muleContext);
62          MuleMessage message = client.send("vm://attachment2", null, null);
63          assertNotNull("return message from MuleClient.send() should not be null", message);
64          assertTrue("Message payload should be a Map", message.getPayload() instanceof Map);
65          Map<String, DataHandler> result = getMapPayload(message);
66          assertEquals("barValue", result.get("bar").getContent());
67          assertEquals("fooValue", result.get("foo").getContent());
68      }
69  
70      @Test
71      public void testInvalidParamType() throws Exception
72      {
73          MuleMessage message = muleContext.getClient().send("vm://invalid", null, null);
74          assertNotNull(message);
75          assertNotNull(message.getExceptionPayload());
76          assertEquals(IllegalArgumentException.class,
77              ExceptionUtils.getRootCause(message.getExceptionPayload().getException()).getClass());
78  
79      }
80  
81      @SuppressWarnings("unchecked")
82      private Map<String, DataHandler> getMapPayload(MuleMessage message)
83      {
84          return (Map<String, DataHandler>) message.getPayload();
85      }
86  }