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.exceptions;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.client.LocalMuleClient;
12  import org.mule.api.processor.MessageProcessor;
13  import org.mule.api.service.Service;
14  import org.mule.exception.DefaultServiceExceptionStrategy;
15  import org.mule.message.ExceptionMessage;
16  import org.mule.module.client.MuleClient;
17  import org.mule.routing.outbound.MulticastingRouter;
18  import org.mule.tck.exceptions.FunctionalTestException;
19  import org.mule.tck.junit4.FunctionalTestCase;
20  import org.mule.tck.probe.PollingProber;
21  import org.mule.tck.probe.Probe;
22  import org.mule.tck.probe.Prober;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertNotSame;
32  import static org.junit.Assert.assertNull;
33  import static org.junit.Assert.assertTrue;
34  
35  public class DefaultServiceExceptionStrategyTestCase extends FunctionalTestCase
36  {
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "org/mule/test/integration/exceptions/default-service-exception-strategy-config.xml";
42      }
43  
44      @Test
45      public void testDefaultExceptionStrategySingleEndpoint() throws MuleException
46      {
47          Service service = muleContext.getRegistry().lookupService("testService1");
48          assertNotNull(service);
49          assertNotNull(service.getExceptionListener());
50          assertTrue(service.getExceptionListener() instanceof DefaultServiceExceptionStrategy);
51          assertEquals(1, ((DefaultServiceExceptionStrategy) service.getExceptionListener()).getMessageProcessors().size());
52  
53          MuleClient mc = new MuleClient(muleContext);
54          mc.dispatch("vm://in1", "test", null);
55          assertExceptionMessage(mc.request("vm://out1", RECEIVE_TIMEOUT));
56          // request one more time to ensure that only one exception message was sent per exception
57          assertNull(mc.request("vm://out1", RECEIVE_TIMEOUT));
58      }
59  
60      @Test
61      public void testDefaultExceptionStrategyMultipleEndpoints() throws MuleException
62      {
63          Service service = muleContext.getRegistry().lookupService("testService2");
64          assertNotNull(service);
65          assertNotNull(service.getExceptionListener());
66          assertTrue(service.getExceptionListener() instanceof DefaultServiceExceptionStrategy);
67          DefaultServiceExceptionStrategy exceptionListener = 
68              (DefaultServiceExceptionStrategy) service.getExceptionListener();
69          MessageProcessor mp = exceptionListener.getMessageProcessors().iterator().next();
70          assertTrue(mp.getClass().getName(), mp instanceof MulticastingRouter);
71          assertEquals(2, ((MulticastingRouter) mp).getRoutes().size());
72  
73          MuleClient mc = new MuleClient(muleContext);
74          mc.dispatch("vm://in2", "test", null);
75          MuleMessage out2 = mc.request("vm://out2", FunctionalTestCase.RECEIVE_TIMEOUT);
76          MuleMessage out3 = mc.request("vm://out3", FunctionalTestCase.RECEIVE_TIMEOUT);
77          assertExceptionMessage(out2);
78          assertExceptionMessage(out3);
79          assertNotSame(out2, out3);
80          assertEquals(out2.getPayload(), out3.getPayload());
81      }
82      
83      @Test
84      public void testDefaultExceptionStrategyNonEndpoint() throws Exception
85      {
86          LocalMuleClient mc = muleContext.getClient();
87  
88          mc.dispatch("vm://in3", "test", null);
89  
90          MuleMessage out4 = mc.request("vm://out4", FunctionalTestCase.RECEIVE_TIMEOUT);
91          assertEquals("ERROR!", out4.getPayloadAsString());
92      }
93  
94      @Test
95      public void testSerializablePayload() throws MuleException
96      {
97          Map<String, String> map = new HashMap<String, String>();
98          map.put("key1", "value1");
99          map.put("key2", "value2");
100 
101         MuleClient mc = new MuleClient(muleContext);
102         mc.dispatch("vm://in1", map, null);
103         MuleMessage message = mc.request("vm://out1", FunctionalTestCase.RECEIVE_TIMEOUT);
104 
105         assertTrue(message.getPayload() instanceof ExceptionMessage);
106         Object payload = ((ExceptionMessage) message.getPayload()).getPayload();
107         assertTrue("payload shoud be a Map, but is " + payload.getClass().getName(), 
108             payload instanceof Map<?, ?>);
109         Map<?, ?> payloadMap = (Map<?, ?>) payload;
110         assertEquals("value1", payloadMap.get("key1"));
111         assertEquals("value2", payloadMap.get("key2"));
112     }
113 
114     @Test
115     public void testStopsServiceOnException() throws MuleException, InterruptedException
116     {
117         final Service service = muleContext.getRegistry().lookupService("testService5");
118 
119         MuleClient mc = new MuleClient(muleContext);
120         mc.dispatch("vm://in5", "test", null);
121 
122         assertExceptionMessage(mc.request("vm://out5", FunctionalTestCase.RECEIVE_TIMEOUT));
123 
124         Prober prober = new PollingProber(5000, 100);
125         prober.check(new Probe()
126         {
127             public boolean isSatisfied()
128             {
129                 return !service.isStarted();
130             }
131 
132             public String describeFailure()
133             {
134                 return "Service was not stopped after processing the exception";
135             }
136         });
137     }
138 
139     private void assertExceptionMessage(MuleMessage out)
140     {
141         assertTrue(out.getPayload() instanceof ExceptionMessage);
142         ExceptionMessage exceptionMessage = (ExceptionMessage) out.getPayload();
143         assertEquals(FunctionalTestException.class, exceptionMessage.getException().getCause().getClass());
144         assertEquals("test", exceptionMessage.getPayload());
145     }
146 }