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.routing;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleSession;
12  import org.mule.api.endpoint.OutboundEndpoint;
13  import org.mule.api.routing.RoutingException;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.api.transport.MessageDispatcher;
16  import org.mule.endpoint.MuleEndpointURI;
17  import org.mule.routing.filters.PayloadTypeFilter;
18  import org.mule.routing.outbound.DefaultOutboundRouterCollection;
19  import org.mule.routing.outbound.FilteringOutboundRouter;
20  import org.mule.tck.MuleTestUtils;
21  import org.mule.tck.junit4.AbstractMuleContextTestCase;
22  import org.mule.transformer.AbstractTransformer;
23  import org.mule.util.CollectionUtils;
24  
25  import com.mockobjects.constraint.Constraint;
26  import com.mockobjects.dynamic.C;
27  import com.mockobjects.dynamic.Mock;
28  
29  import java.util.HashMap;
30  
31  import org.junit.Test;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertNotNull;
35  import static org.junit.Assert.fail;
36  
37  public class CatchAllStrategiesTestCase extends AbstractMuleContextTestCase
38  {
39  
40      public CatchAllStrategiesTestCase()
41      {
42          setStartContext(true);
43      }
44  
45      @Test
46      public void testLoggingOnlyStrategy() throws Exception
47      {
48          // Just test it works without failure
49          MuleEvent event = getTestEvent("UncaughtEvent");
50          LoggingCatchAllStrategy strategy = new LoggingCatchAllStrategy();
51          strategy.process(event);
52      }
53  
54      @Test
55      public void testForwardingStrategy() throws Exception
56      {
57          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
58          Mock endpoint = MuleTestUtils.getMockOutboundEndpoint();
59          Mock dispatcher = new Mock(MessageDispatcher.class);
60          Mock connector = MuleTestUtils.getMockConnector();
61          MuleEvent event = getTestEvent("UncaughtEvent");
62          strategy.setEndpoint((OutboundEndpoint) endpoint.proxy());
63  
64          endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
65          endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
66          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
67          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
68          endpoint.expect("process", C.isA(DefaultMuleEvent.class));
69  
70          strategy.process(event);
71  
72          endpoint.verify();
73          dispatcher.verify();
74          connector.verify();
75  
76          assertNotNull(strategy.getEndpoint());
77      }
78  
79      /**
80       * Test for MULE-3034
81       */
82      @Test
83      public void testForwardingStrategyNullEndpoint() throws Exception
84      {
85          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
86          strategy.setEndpoint(null);
87          MuleEvent event = getTestEvent("UncaughtEvent");
88          MuleSession session = getTestSession(getTestService(), muleContext);
89  
90          try
91          {
92              strategy.process(event);
93              fail();
94          }
95          catch (RoutingException sre)
96          {
97              // we expected this exception
98          }
99      }
100 
101     private static class TestEventTransformer extends AbstractTransformer
102     {
103         public TestEventTransformer()
104         {
105             super();
106         }
107 
108         @Override
109         public Object doTransform(Object src, String encoding) throws TransformerException
110         {
111             return "Transformed Test Data";
112         }
113     }
114 
115     @Test
116     public void testForwardingStrategyWithTransform() throws Exception
117     {
118         ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
119         strategy.setSendTransformed(true);
120         Mock endpoint = MuleTestUtils.getMockOutboundEndpoint();
121         Mock dispatcher = new Mock(MessageDispatcher.class);
122         Mock connector = MuleTestUtils.getMockConnector();
123         MuleEvent event = getTestEvent("UncaughtEvent");
124         strategy.setEndpoint((OutboundEndpoint) endpoint.proxy());
125 
126         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
127         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
128         endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
129         endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
130         endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
131         endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
132         endpoint.expect("process", new Constraint()
133         {
134             public boolean eval(Object object)
135             {
136                 if (object instanceof MuleEvent)
137                 {
138                     return "Transformed Test Data".equals(((MuleEvent) object).getMessage().getPayload());
139                 }
140                 return false;
141             }
142         });
143 
144         strategy.process(event);
145 
146         endpoint.verify();
147         dispatcher.verify();
148         connector.verify();
149 
150         assertNotNull(strategy.getEndpoint());
151     }
152 
153     @Test
154     public void testFullRouter() throws Exception
155     {
156         final int[] count1 = new int[]{0};
157         final int[] count2 = new int[]{0};
158         final int[] catchAllCount = new int[]{0};
159 
160         DefaultOutboundRouterCollection messageRouter = new DefaultOutboundRouterCollection();
161 
162         FilteringOutboundRouter filterRouter1 = new FilteringOutboundRouter()
163         {
164             @Override
165             public MuleEvent route(MuleEvent event)
166             {
167                 count1[0]++;
168                 return event;
169             }
170         };
171 
172         FilteringOutboundRouter filterRouter2 = new FilteringOutboundRouter()
173         {
174             @Override
175             public MuleEvent route(MuleEvent event)
176             {
177                 count2[0]++;
178                 return event;
179             }
180         };
181 
182         filterRouter1.setFilter(new PayloadTypeFilter(Exception.class));
183         filterRouter2.setFilter(new PayloadTypeFilter(StringBuffer.class));
184         messageRouter.addRoute(filterRouter1);
185         messageRouter.addRoute(filterRouter2);
186 
187         AbstractCatchAllStrategy strategy = new AbstractCatchAllStrategy()
188         {
189             @Override
190             public MuleEvent doCatchMessage(MuleEvent event)
191             {
192                 catchAllCount[0]++;
193                 return null;
194             }
195         };
196         messageRouter.setCatchAllStrategy(strategy);
197 
198         MuleSession session = getTestSession(getTestService(), muleContext);
199 
200         messageRouter.process(getTestEvent("hello"));
201         assertEquals(1, catchAllCount[0]);
202         assertEquals(0, count1[0]);
203         assertEquals(0, count2[0]);
204 
205         messageRouter.process(getTestEvent(new StringBuffer()));
206         assertEquals(1, catchAllCount[0]);
207         assertEquals(0, count1[0]);
208         assertEquals(1, count2[0]);
209 
210         messageRouter.process(getTestEvent(new Exception()));
211         assertEquals(1, catchAllCount[0]);
212         assertEquals(1, count1[0]);
213         assertEquals(1, count2[0]);
214     }
215 
216 }