View Javadoc

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