View Javadoc

1   /*
2    * $Id: PayloadAnnotationTestCase.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.api.transformer.TransformerException;
19  import org.mule.module.client.MuleClient;
20  import org.mule.tck.AbstractServiceAndFlowTestCase;
21  import org.mule.util.ExceptionUtils;
22  import org.mule.util.IOUtils;
23  
24  import java.io.InputStream;
25  import java.util.Arrays;
26  import java.util.Collection;
27  
28  import org.junit.Test;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  public class PayloadAnnotationTestCase 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/payload-annotation-service.xml"},
38              {ConfigVariant.FLOW, "org/mule/test/annotations/payload-annotation-flow.xml"}});
39      }
40  
41      public PayloadAnnotationTestCase(ConfigVariant variant, String configResources)
42      {
43          super(variant, configResources);
44          setDisposeContextPerClass(true);
45      }
46  
47      @Test
48      public void testPayloadNoTransform() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          MuleMessage message = client.send("vm://payload1", "foo", null);
52          assertNotNull("return message from MuleClient.send() should not be null", message);
53          assertTrue("Message payload should be a String", message.getPayload() instanceof String);
54          assertEquals("foo", message.getPayload());
55      }
56  
57      @Test
58      public void testPayloadAutoTransform() throws Exception
59      {
60          MuleClient client = new MuleClient(muleContext);
61          MuleMessage message = client.send("vm://payload2", "foo", null);
62          assertNotNull("return message from MuleClient.send() should not be null", message);
63          assertTrue("Message payload should be a String", message.getPayload() instanceof InputStream);
64          assertEquals("foo", IOUtils.toString((InputStream) message.getPayload()));
65      }
66  
67      @Test
68      public void testPayloadFailedTransform() throws Exception
69      {
70          MuleClient client = new MuleClient(muleContext);
71          MuleMessage message = client.send("vm://payload3", null, null);
72          assertNotNull(message);
73          assertNotNull(message.getExceptionPayload());
74          assertEquals(TransformerException.class,
75              ExceptionUtils.getRootCause(message.getExceptionPayload().getException()).getClass());
76      }
77  }