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.hamcrest.core.IsNull;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.client.LocalMuleClient;
12  import org.mule.module.client.MuleClient;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  import org.mule.transport.NullPayload;
22  
23  import static org.hamcrest.core.Is.is;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertThat;
27  
28  public class ExceptionBasedRouterTestCase extends FunctionalTestCase
29  {
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "org/mule/test/integration/routing/outbound/exception-based-router.xml";
35      }
36  
37      @Test
38      public void testStaticEndpointsByName() throws Exception
39      {
40          MuleClient client = new MuleClient(muleContext);
41          
42          MuleMessage reply = client.send("vm://in1", "request", null);
43          assertNotNull(reply);
44          assertEquals("success", reply.getPayload());
45      }
46  
47      @Test
48      public void testStaticEndpointsByURI() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          
52          MuleMessage reply = client.send("vm://in2", "request", null);
53          assertNotNull(reply);
54          assertEquals("success", reply.getPayload());
55      }
56  
57      @Test
58      public void testDynamicEndpointsByName() throws Exception
59      {
60          MuleClient client = new MuleClient(muleContext);
61          
62          Map<String, Object> props = new HashMap<String, Object>();
63          props.put("recipients", "service1,service2,service3");
64          MuleMessage reply = client.send("vm://in3", "request", props);
65          assertNotNull(reply);
66          assertEquals("success", reply.getPayload());
67      }
68  
69      @Test
70      public void testDynamicEndpointsByURI() throws Exception
71      {
72          MuleClient client = new MuleClient(muleContext);
73          
74          Map<String, Object> props = new HashMap<String, Object>();
75          List<String> recipients = new ArrayList<String>();
76          recipients.add("vm://service4?responseTransformers=validateResponse&exchangePattern=request-response");
77          recipients.add("vm://service5?responseTransformers=validateResponse&exchangePattern=request-response");
78          recipients.add("vm://service6?responseTransformers=validateResponse&exchangePattern=request-response");
79          props.put("recipients", recipients);
80          MuleMessage reply = client.send("vm://in3", "request", props);
81          assertNotNull(reply);
82          assertEquals("success", reply.getPayload());
83      }
84  
85      /**
86       * Test endpoints which generate a natural exception because they don't even exist.
87       */
88      @Test
89      public void testIllegalEndpoint() throws Exception
90      {
91          MuleClient client = new MuleClient(muleContext);
92          
93          Map<String, Object> props = new HashMap<String, Object>();
94          List<String> recipients = new ArrayList<String>();
95          recipients.add("vm://service998?exchangePattern=request-response");
96          recipients.add("vm://service5?exchangePattern=request-response");
97          recipients.add("vm://service999");
98          props.put("recipients", recipients);
99          MuleMessage reply = client.send("vm://in3", "request", props);
100         assertNotNull(reply);
101         assertEquals("success", reply.getPayload());
102     }
103 
104     /**
105      * Test failing endpoint do not cause transaction rollback
106      */
107     @Test
108     public void testTransactionIsNotRolledBack() throws Exception
109     {
110         LocalMuleClient client = muleContext.getClient();
111         MuleMessage result = client.send("jms://in", "some message", null, RECEIVE_TIMEOUT);
112         assertThat(result, IsNull.<Object>notNullValue());
113         assertThat((NullPayload) result.getPayload(), is(NullPayload.getInstance()));
114         MuleMessage outputMessage = client.request("jms://out",LOCK_TIMEOUT);
115         assertThat(outputMessage, IsNull.<Object>notNullValue());
116         assertThat(outputMessage.getPayloadAsString(), is("some message"));
117     }
118 }
119 
120