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.outbound;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.MuleSession;
14  import org.mule.api.endpoint.OutboundEndpoint;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.tck.MuleTestUtils;
17  import org.mule.tck.junit4.AbstractMuleContextTestCase;
18  
19  import com.mockobjects.dynamic.Mock;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertTrue;
31  import static org.junit.Assert.fail;
32  
33  public class StaticRecipientListRouterTestCase extends AbstractMuleContextTestCase
34  {
35      public StaticRecipientListRouterTestCase()
36      {
37          setStartContext(true);        
38      }
39  
40      @Test
41      public void testRecipientListRouterAsync() throws Exception
42      {
43          Mock session = MuleTestUtils.getMockSession();
44          session.matchAndReturn("getFlowConstruct", getTestService());
45          
46          OutboundEndpoint endpoint1 = getTestOutboundEndpoint("Test1Provider");
47          assertNotNull(endpoint1);
48          Mock mockendpoint1 = RouterTestUtils.getMockEndpoint(endpoint1);
49  
50          List<String> recipients = new ArrayList<String>();
51          String recipient1 = "test://recipient1";
52          recipients.add(recipient1);
53          String recipient2 = "test://recipient2";
54          recipients.add(recipient2);
55          MockingStaticRecipientList router = createObject(MockingStaticRecipientList.class);
56  
57          router.setRecipients(recipients);
58  
59          List<MessageProcessor> endpoints = new ArrayList<MessageProcessor>();
60          endpoints.add((OutboundEndpoint) mockendpoint1.proxy());
61          router.setRoutes(endpoints);
62          router.setMuleContext(muleContext);
63  
64          assertEquals(2, router.getRecipients().size());
65  
66          MuleMessage message = new DefaultMuleMessage("test event", muleContext);
67          assertTrue(router.isMatch(message));
68  
69          // Set up the mock targets as we discover them
70          final List<Mock> mockEndpoints = new ArrayList<Mock>();
71          router.setMockEndpointListener(new MockEndpointListener()
72          {
73              public void mockEndpointAdded(Mock recipient)
74              {
75                  mockEndpoints.add(recipient);
76                  recipient.expect("process", RouterTestUtils.getArgListCheckerMuleEvent());
77              }
78          });
79  
80          router.route(new OutboundRoutingTestEvent(message, (MuleSession)session.proxy()));
81          for (Mock mockEp : mockEndpoints)
82          {
83              mockEp.verify();
84          }
85      }
86  
87  
88      @Test
89      public void testRecipientListRouterSync() throws Exception
90      {
91          Mock session = MuleTestUtils.getMockSession();
92          session.matchAndReturn("getFlowConstruct", getTestService());
93          session.matchAndReturn("setFlowConstruct", RouterTestUtils.getArgListCheckerFlowConstruct(), null);
94          
95          OutboundEndpoint endpoint1 = getTestOutboundEndpoint("Test1Provider");
96          assertNotNull(endpoint1);
97  
98          List<String> recipients = new ArrayList<String>();
99          recipients.add("test://recipient1?exchangePattern=request-response");
100         recipients.add("test://recipient2?exchangePattern=request-response");
101         MockingStaticRecipientList router = createObject(MockingStaticRecipientList.class);
102 
103         router.setRecipients(recipients);
104 
105         List<MessageProcessor> endpoints = new ArrayList<MessageProcessor>();
106         endpoints.add(endpoint1);
107         router.setRoutes(endpoints);
108         router.setMuleContext(muleContext);
109 
110         assertEquals(2, router.getRecipients().size());
111 
112         MuleMessage message = new DefaultMuleMessage("test event", muleContext);
113         assertTrue(router.isMatch(message));
114         // note this router clones targets so that the endpointUri can be
115         // changed
116 
117         // The static recipient list router duplicates the message for each endpoint
118         // so we can't
119         // check for equality on the arguments passed to the dispatch / send methods
120         message = new DefaultMuleMessage("test event", muleContext);
121         final MuleEvent event = new OutboundRoutingTestEvent(message, null);
122 
123          // Set up the mock targets as we discover them
124          final List<Mock> mockEndpoints = new ArrayList<Mock>();
125          router.setMockEndpointListener(new MockEndpointListener()
126          {
127              public void mockEndpointAdded(Mock recipient)
128              {
129                  mockEndpoints.add(recipient);
130                  recipient.expectAndReturn("process", RouterTestUtils.getArgListCheckerMuleEvent(), event);
131              }
132          });
133 
134         router.getRecipients().add("test://recipient3?exchangePattern=request-response");
135         MuleEvent result = router.route(new OutboundRoutingTestEvent(message, (MuleSession)session.proxy()));
136         assertNotNull(result);
137         MuleMessage resultMessage = result.getMessage();
138         assertNotNull(resultMessage);
139         assertTrue(resultMessage.getPayload() instanceof List);
140         assertEquals(3, ((List)resultMessage.getPayload()).size());
141         session.verify();
142 
143     }
144 
145     @Test
146     public void testBadRecipientListRouter() throws Exception
147     {
148         Mock session = MuleTestUtils.getMockSession();
149 
150         OutboundEndpoint endpoint1 = getTestOutboundEndpoint("Test1Provider");
151         assertNotNull(endpoint1);
152 
153         List<String> recipients = new ArrayList<String>();
154         recipients.add("malformed-endpointUri-recipient1");
155         StaticRecipientList router = createObject(StaticRecipientList.class);
156 
157         router.setRecipients(recipients);
158 
159         List<MessageProcessor> endpoints = new ArrayList<MessageProcessor>();
160         endpoints.add(endpoint1);
161         router.setRoutes(endpoints);
162 
163         assertEquals(1, router.getRecipients().size());
164 
165         MuleMessage message = new DefaultMuleMessage("test event", muleContext);
166         assertTrue(router.isMatch(message));
167         try
168         {
169             router.route(new OutboundRoutingTestEvent(message, (MuleSession)session.proxy()));
170             fail("Should not allow malformed endpointUri");
171         }
172         catch (Exception e)
173         {
174             // ignore
175         }
176         session.verify();
177     }
178 
179     /** subclass the router, so that we can mock the targets it creates dynamically.  */
180     public static class MockingStaticRecipientList extends StaticRecipientList
181     {
182         private Map<String, Mock> recipients = new HashMap<String, Mock>();
183         private MockEndpointListener listener;
184 
185         Mock getRecipient(String name)
186         {
187             return recipients.get(name);
188         }
189 
190         public void setMockEndpointListener(MockEndpointListener listener)
191         {
192             this.listener = listener;
193         }
194 
195         @Override
196         protected OutboundEndpoint getRecipientEndpointFromString(MuleMessage message, String recipient) throws MuleException
197         {
198             OutboundEndpoint endpoint = super.getRecipientEndpointFromString(message, recipient);
199             if (!recipients.containsKey(recipient))
200             {
201                 Mock mock = RouterTestUtils.getMockEndpoint(endpoint);
202                 recipients.put(recipient, mock);
203                 if (listener != null)
204                     listener.mockEndpointAdded(mock);
205             }
206             return (OutboundEndpoint) recipients.get(recipient).proxy();
207         }
208     }
209 
210     /** Callback called when new recipient is added */
211     interface MockEndpointListener
212     {
213         void mockEndpointAdded(Mock recipient);
214     }
215 }