View Javadoc

1   /*
2    * $Id: AbstractMessageProcessorTestCase.java 23030 2011-09-26 18:02:33Z mike.schilling $
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.endpoint;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.MessageExchangePattern;
16  import org.mule.api.MuleContext;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleException;
19  import org.mule.api.MuleMessage;
20  import org.mule.api.context.MuleContextAware;
21  import org.mule.api.context.MuleContextBuilder;
22  import org.mule.api.context.notification.EndpointMessageNotificationListener;
23  import org.mule.api.context.notification.SecurityNotificationListener;
24  import org.mule.api.endpoint.EndpointBuilder;
25  import org.mule.api.endpoint.EndpointException;
26  import org.mule.api.endpoint.ImmutableEndpoint;
27  import org.mule.api.endpoint.InboundEndpoint;
28  import org.mule.api.endpoint.OutboundEndpoint;
29  import org.mule.api.exception.MessagingExceptionHandler;
30  import org.mule.api.exception.RollbackSourceCallback;
31  import org.mule.api.lifecycle.InitialisationException;
32  import org.mule.api.processor.MessageProcessor;
33  import org.mule.api.routing.filter.Filter;
34  import org.mule.api.security.EndpointSecurityFilter;
35  import org.mule.api.security.SecurityFilter;
36  import org.mule.api.service.Service;
37  import org.mule.api.transaction.TransactionConfig;
38  import org.mule.api.transformer.Transformer;
39  import org.mule.context.notification.EndpointMessageNotification;
40  import org.mule.context.notification.SecurityNotification;
41  import org.mule.context.notification.ServerNotificationManager;
42  import org.mule.message.DefaultExceptionPayload;
43  import org.mule.processor.SecurityFilterMessageProcessor;
44  import org.mule.routing.MessageFilter;
45  import org.mule.tck.junit4.AbstractMuleContextTestCase;
46  import org.mule.transport.NullPayload;
47  import org.mule.util.concurrent.Latch;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.HashMap;
52  import java.util.List;
53  import java.util.Map;
54  import java.util.concurrent.CountDownLatch;
55  
56  public abstract class AbstractMessageProcessorTestCase extends AbstractMuleContextTestCase
57  {
58      protected static final String TEST_URI = "test://myTestUri";
59      protected static String RESPONSE_MESSAGE = "response-message";
60      protected static MuleMessage responseMessage;
61  
62      @Override
63      protected void doSetUp() throws Exception
64      {
65          super.doSetUp();
66          responseMessage = createTestResponseMuleMessage();
67          muleContext.start();
68      }
69  
70      @Override
71      protected void configureMuleContext(MuleContextBuilder builder)
72      {
73          super.configureMuleContext(builder);
74  
75          // Configure EndpointMessageNotificationListener for notifications test
76          ServerNotificationManager notificationManager = new ServerNotificationManager();
77          notificationManager.addInterfaceToType(EndpointMessageNotificationListener.class,
78              EndpointMessageNotification.class);
79          notificationManager.addInterfaceToType(SecurityNotificationListener.class, SecurityNotification.class);
80  
81          builder.setNotificationManager(notificationManager);
82      }
83  
84      protected InboundEndpoint createTestInboundEndpoint(Transformer transformer,
85                                                          Transformer responseTransformer)
86          throws EndpointException, InitialisationException
87      {
88          return createTestInboundEndpoint(null, null, transformer, responseTransformer, 
89              MessageExchangePattern.REQUEST_RESPONSE, null);
90      }
91  
92      protected InboundEndpoint createTestInboundEndpoint(Filter filter,
93                                                          SecurityFilter securityFilter,
94                                                          MessageExchangePattern exchangePattern,
95                                                          TransactionConfig txConfig)
96          throws InitialisationException, EndpointException
97      {
98          return createTestInboundEndpoint(filter, securityFilter, null, null, exchangePattern, txConfig);
99      }
100 
101     protected InboundEndpoint createTestInboundEndpoint(Filter filter,
102                                                         SecurityFilter securityFilter,
103                                                         Transformer transformer,
104                                                         Transformer responseTransformer,
105                                                         MessageExchangePattern exchangePattern,
106                                                         TransactionConfig txConfig)
107         throws EndpointException, InitialisationException
108     {
109         EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(TEST_URI, muleContext);
110         if (filter != null)
111         {
112             endpointBuilder.addMessageProcessor(new MessageFilter(filter));
113         }
114         if (securityFilter != null)
115         {
116             endpointBuilder.addMessageProcessor(new SecurityFilterMessageProcessor(securityFilter));
117         }
118         if (transformer != null)
119         {
120             endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>singletonList(transformer));
121         }
122         if (responseTransformer != null)
123         {
124             endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>singletonList(responseTransformer));
125         }
126         endpointBuilder.setExchangePattern(exchangePattern);
127         endpointBuilder.setTransactionConfig(txConfig);
128         InboundEndpoint endpoint = endpointBuilder.buildInboundEndpoint();
129         return endpoint;
130     }
131 
132     protected MuleEvent createTestInboundEvent(InboundEndpoint endpoint) throws Exception
133     {
134         Map<String, Object> props = new HashMap<String, Object>();
135         props.put("prop1", "value1");
136         return new DefaultMuleEvent(new DefaultMuleMessage(TEST_MESSAGE, props, muleContext), endpoint,
137             getTestSession(getTestService(), muleContext));
138     }
139 
140     protected OutboundEndpoint createTestOutboundEndpoint(Transformer transformer,
141                                                           Transformer responseTransformer)
142         throws EndpointException, InitialisationException
143     {
144         return createTestOutboundEndpoint(null, null, transformer, responseTransformer, 
145             MessageExchangePattern.REQUEST_RESPONSE, null);
146     }
147 
148     protected OutboundEndpoint createTestOutboundEndpoint(Filter filter,
149                                                           EndpointSecurityFilter securityFilter,
150                                                           MessageExchangePattern exchangePattern,
151                                                           TransactionConfig txConfig)
152         throws InitialisationException, EndpointException
153     {
154         return createTestOutboundEndpoint(filter, securityFilter, null, null, exchangePattern, 
155             txConfig);
156     }
157 
158     protected OutboundEndpoint createTestOutboundEndpoint(Filter filter,
159                                                           EndpointSecurityFilter securityFilter,
160                                                           Transformer transformer,
161                                                           Transformer responseTransformer,
162                                                           MessageExchangePattern exchangePattern,
163                                                           TransactionConfig txConfig)
164         throws EndpointException, InitialisationException
165     {
166         return createTestOutboundEndpoint("test://test", filter, securityFilter, transformer, responseTransformer, exchangePattern, txConfig);
167     }
168     protected OutboundEndpoint createTestOutboundEndpoint(String uri, Filter filter,
169                                                           SecurityFilter securityFilter,
170                                                           Transformer transformer,
171                                                           Transformer responseTransformer,
172                                                           MessageExchangePattern exchangePattern,
173                                                           TransactionConfig txConfig)
174         throws EndpointException, InitialisationException
175     {
176         EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(uri,
177             muleContext);
178         if (filter != null)
179         {
180             endpointBuilder.addMessageProcessor(new MessageFilter(filter));
181         }
182         if (securityFilter != null)
183         {
184             endpointBuilder.addMessageProcessor(new SecurityFilterMessageProcessor(securityFilter));
185         }
186         if (transformer != null)
187         {
188             endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>singletonList(transformer));
189         }
190         if (responseTransformer != null)
191         {
192             endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>singletonList(responseTransformer));
193         }
194         endpointBuilder.setExchangePattern(exchangePattern);
195         endpointBuilder.setTransactionConfig(txConfig);
196         customizeEndpointBuilder(endpointBuilder);
197         return endpointBuilder.buildOutboundEndpoint();
198     }
199 
200     protected void customizeEndpointBuilder(EndpointBuilder endpointBuilder)
201     {
202         // template method
203     }
204 
205     protected MuleEvent createTestOutboundEvent() throws Exception
206     {
207         return createTestOutboundEvent(null);
208     }
209     
210     protected MuleEvent createTestOutboundEvent(MessagingExceptionHandler exceptionListener) throws Exception
211     {
212         Map<String, Object> props = new HashMap<String, Object>();
213         props.put("prop1", "value1");
214         props.put("port", 12345);
215 
216         Service svc = getTestService();
217         if (exceptionListener != null)
218         {
219             svc.setExceptionListener(exceptionListener);
220         }
221         return new DefaultMuleEvent(new DefaultMuleMessage(TEST_MESSAGE, props, muleContext), getTestInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE), getTestSession(svc, muleContext));
222     }
223 
224     protected MuleMessage createTestResponseMuleMessage()
225     {
226         return new DefaultMuleMessage(RESPONSE_MESSAGE, muleContext);
227     }
228 
229     public static class TestFilter implements Filter
230     {
231         public boolean accept;
232 
233         public TestFilter(boolean accept)
234         {
235             this.accept = accept;
236         }
237 
238         public boolean accept(MuleMessage message)
239         {
240             return accept;
241         }
242     }
243 
244     public static class TestSecurityNotificationListener implements SecurityNotificationListener<SecurityNotification>
245     {
246         public SecurityNotification securityNotification;
247         public Latch latch = new Latch();
248 
249         public void onNotification(SecurityNotification notification)
250         {
251             securityNotification = notification;
252             latch.countDown();
253         }
254     }
255 
256     public static class TestListener implements MessageProcessor
257     {
258         public MuleEvent sensedEvent;
259 
260         public MuleEvent process(MuleEvent event) throws MuleException
261         {
262             sensedEvent = event;
263             return event;
264         }
265     }
266 
267     public static class TestEndpointMessageNotificationListener implements EndpointMessageNotificationListener<EndpointMessageNotification>
268     {
269         public TestEndpointMessageNotificationListener()
270         {
271             latch = new CountDownLatch(1);
272         }
273 
274         public TestEndpointMessageNotificationListener(int numExpected)
275         {
276             latch = new CountDownLatch(numExpected);
277         }
278 
279         public EndpointMessageNotification messageNotification;
280         public List<EndpointMessageNotification> messageNotificationList = new ArrayList<EndpointMessageNotification>();
281 
282         public CountDownLatch latch;
283 
284         public void onNotification(EndpointMessageNotification notification)
285         {
286             messageNotification = notification;
287             messageNotificationList.add(notification);
288             latch.countDown();
289         }
290     }
291 
292     public static class ExceptionThrowingMessageProcessr implements MessageProcessor
293     {
294         public MuleEvent process(MuleEvent event) throws MuleException
295         {
296             throw new IllegalStateException();
297         }
298     }
299 
300     public static class TestExceptionListener implements MessagingExceptionHandler
301     {
302         public Exception sensedException;
303 
304         public MuleEvent handleException(Exception exception, MuleEvent event, RollbackSourceCallback rollbackMethod)
305         {
306             sensedException = exception;
307             event.getMessage().setPayload(NullPayload.getInstance());
308             event.getMessage().setExceptionPayload(new DefaultExceptionPayload(exception));
309             return event;
310         }
311 
312         public MuleEvent handleException(Exception exception, MuleEvent event)
313         {
314             return handleException(exception, event, null);
315         }
316     }
317 
318     public class ObjectAwareProcessor implements MessageProcessor, EndpointAware, MuleContextAware
319     {
320 
321         public MuleContext context;
322         public ImmutableEndpoint endpoint;
323 
324         public MuleEvent process(MuleEvent event) throws MuleException
325         {
326             return null;
327         }
328 
329         public void setEndpoint(ImmutableEndpoint endpoint)
330         {
331             this.endpoint = endpoint;
332         }
333 
334         public void setMuleContext(MuleContext context)
335         {
336             this.context = context;
337         }
338     }
339     
340 }