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