View Javadoc

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