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