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.test.integration.routing.outbound;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.component.Component;
12  import org.mule.component.ComponentException;
13  import org.mule.module.client.MuleClient;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  import org.mule.transport.NullPayload;
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.assertNull;
22  import static org.junit.Assert.assertTrue;
23  
24  public class ChainingRouterNullsHandlingTestCase extends FunctionalTestCase
25  {
26  
27      @Override
28      protected String getConfigResources()
29      {
30          return "org/mule/test/integration/routing/outbound/chaining-router-null-handling.xml";
31      }
32  
33      @Test
34      public void testNoComponentFails() throws Exception
35      {
36          MuleClient muleClient = new MuleClient(muleContext);
37          MuleMessage message = new DefaultMuleMessage("thePayload", muleContext);
38          MuleMessage result = muleClient.send("vm://incomingPass", message);
39          assertNull("Shouldn't have any exceptions", result.getExceptionPayload());
40          assertEquals("thePayload Received component1 Received component2Pass", result.getPayloadAsString());
41      }
42  
43      @Test
44      public void testLastComponentFails() throws Exception
45      {
46          MuleClient muleClient = new MuleClient(muleContext);
47          MuleMessage message = new DefaultMuleMessage("thePayload", muleContext);
48          MuleMessage result = muleClient.send("vm://incomingLastFail", message);
49          assertNotNull("Should be a NullPayload instead.", result);
50          assertEquals("Should be a NullPayload instead.", NullPayload.getInstance(), result.getPayload());
51          assertNotNull("Should've contained an exception payload", result.getExceptionPayload());
52          Throwable exception = result.getExceptionPayload().getException();
53          assertNotNull("Exception required", exception);
54          assertTrue("Wrong exception", exception instanceof ComponentException);
55          Component component = ((ComponentException) exception).getComponent();
56          assertEquals("Exception raised in wrong service", muleContext.getRegistry().lookupService(
57              "component2Fail").getComponent(), component);
58      }
59  
60      @Test
61      public void testFirstComponentFails() throws Exception
62      {
63          MuleClient muleClient = new MuleClient(muleContext);
64          MuleMessage message = new DefaultMuleMessage("thePayload", muleContext);
65          MuleMessage result = muleClient.send("vm://incomingFirstFail", message);
66          assertNotNull("Should be a NullPayload instead.", result);
67          assertEquals("Should be a NullPayload instead.", NullPayload.getInstance(), result.getPayload());
68          assertNotNull("Should've contained an exception payload", result.getExceptionPayload());
69          Throwable exception = result.getExceptionPayload().getException();
70          assertNotNull("Exception required", exception);
71          assertTrue("Wrong exception", exception instanceof ComponentException);
72          Component component = ((ComponentException) exception).getComponent();
73          assertEquals("Exception raised in wrong service", muleContext.getRegistry().lookupService(
74          "component1Fail").getComponent(), component);
75      }
76  
77  }