1
2
3
4
5
6
7
8
9
10
11 package org.mule.endpoint.outbound;
12
13 import org.mule.MessageExchangePattern;
14 import org.mule.RequestContext;
15 import org.mule.api.MuleEvent;
16 import org.mule.api.MuleException;
17 import org.mule.api.MuleMessage;
18 import org.mule.api.config.MuleProperties;
19 import org.mule.api.endpoint.OutboundEndpoint;
20 import org.mule.api.lifecycle.LifecycleException;
21 import org.mule.api.routing.filter.Filter;
22 import org.mule.api.security.EndpointSecurityFilter;
23 import org.mule.api.transaction.TransactionConfig;
24 import org.mule.api.transformer.Transformer;
25 import org.mule.api.transport.Connector;
26 import org.mule.api.transport.MessageDispatcher;
27 import org.mule.context.notification.EndpointMessageNotification;
28 import org.mule.context.notification.SecurityNotification;
29 import org.mule.endpoint.AbstractMessageProcessorTestCase;
30 import org.mule.endpoint.EndpointURIEndpointBuilder;
31 import org.mule.tck.security.TestSecurityFilter;
32 import org.mule.tck.testmodels.mule.TestMessageDispatcher;
33 import org.mule.tck.testmodels.mule.TestMessageDispatcherFactory;
34 import org.mule.transformer.simple.OutboundAppendTransformer;
35 import org.mule.transformer.simple.ResponseAppendTransformer;
36 import org.mule.transport.AbstractMessageDispatcher;
37 import org.mule.util.concurrent.Latch;
38
39 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
40
41
42
43
44
45
46 public class OutboundEndpointTestCase extends AbstractMessageProcessorTestCase
47 {
48 protected FakeMessageDispatcher dispacher;
49 protected MuleEvent testOutboundEvent;
50
51 public void testDefaultFlowSync() throws Exception
52 {
53 OutboundEndpoint endpoint = createOutboundEndpoint(null, null, null, null,
54 MessageExchangePattern.REQUEST_RESPONSE, null);
55
56 testOutboundEvent = createTestOutboundEvent(endpoint);
57 MuleEvent result = endpoint.process(testOutboundEvent);
58
59 assertMessageSentSame(true);
60
61
62
63
64 assertNotSame(responseMessage, result.getMessage());
65
66
67
68 assertMessageEqualEncodingPropertyAdded(responseMessage, result.getMessage());
69
70 }
71
72 public void testDefaultFlowAsync() throws Exception
73 {
74 OutboundEndpoint endpoint = createOutboundEndpoint(null, null, null, null,
75 MessageExchangePattern.ONE_WAY, null);
76
77 testOutboundEvent = createTestOutboundEvent(endpoint);
78 MuleEvent result = endpoint.process(testOutboundEvent);
79
80 dispacher.latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS);
81 assertMessageSentSame(false);
82 assertNull(result);
83 }
84
85 public void testSecurityFilterAccept() throws Exception
86 {
87 OutboundEndpoint endpoint = createOutboundEndpoint(null, new TestSecurityFilter(true),
88 null, null, MessageExchangePattern.REQUEST_RESPONSE, null);
89
90 testOutboundEvent = createTestOutboundEvent(endpoint);
91 MuleEvent result = endpoint.process(testOutboundEvent);
92
93 assertMessageSentSame(true);
94
95
96
97
98 assertNotSame(responseMessage, result.getMessage());
99
100
101
102 assertMessageEqualEncodingPropertyAdded(responseMessage, result.getMessage());
103 }
104
105 public void testSecurityFilterNotAccept() throws Exception
106 {
107 TestSecurityNotificationListener securityNotificationListener = new TestSecurityNotificationListener();
108 muleContext.registerListener(securityNotificationListener);
109
110 OutboundEndpoint endpoint = createOutboundEndpoint(null, new TestSecurityFilter(false),
111 null, null, MessageExchangePattern.REQUEST_RESPONSE, null);
112
113 testOutboundEvent = createTestOutboundEvent(endpoint);
114 RequestContext.setEvent(testOutboundEvent);
115 MuleEvent result = endpoint.process(testOutboundEvent);
116
117 assertMessageNotSent();
118 assertNotNull(result);
119 assertEquals(TestSecurityFilter.SECURITY_EXCEPTION_MESSAGE, result.getMessage().getPayloadAsString());
120 assertNotNull(result.getMessage().getExceptionPayload());
121 assertTrue(result.getMessage().getExceptionPayload().getException() instanceof TestSecurityFilter.StaticMessageUnauthorisedException);
122
123 assertTrue(securityNotificationListener.latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
124 assertEquals(SecurityNotification.SECURITY_AUTHENTICATION_FAILED,
125 securityNotificationListener.securityNotification.getAction());
126 assertEquals(securityNotificationListener.securityNotification.getResourceIdentifier(),
127 TestSecurityFilter.StaticMessageUnauthorisedException.class.getName());
128 }
129
130 public void testSendNotfication() throws Exception
131 {
132 TestEndpointMessageNotificationListener listener = new TestEndpointMessageNotificationListener();
133 muleContext.registerListener(listener);
134
135 OutboundEndpoint endpoint = createOutboundEndpoint(null, null, null, null,
136 MessageExchangePattern.REQUEST_RESPONSE, null);
137 MuleEvent outboundEvent = createTestOutboundEvent(endpoint);
138 endpoint.process(outboundEvent);
139
140 assertTrue(listener.latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
141 assertEquals(EndpointMessageNotification.MESSAGE_SENT, listener.messageNotification.getAction());
142 assertEquals(endpoint.getEndpointURI().getUri().toString(),
143 listener.messageNotification.getEndpoint());
144 assertTrue(listener.messageNotification.getSource() instanceof MuleMessage);
145 assertEquals(outboundEvent.getMessage().getPayload(),
146 ((MuleMessage) listener.messageNotification.getSource()).getPayload());
147 }
148
149 public void testDispatchNotfication() throws Exception
150 {
151 TestEndpointMessageNotificationListener listener = new TestEndpointMessageNotificationListener();
152 muleContext.registerListener(listener);
153
154 OutboundEndpoint endpoint = createOutboundEndpoint(null, null, null, null,
155 MessageExchangePattern.ONE_WAY, null);
156 MuleEvent outboundEvent = createTestOutboundEvent(endpoint);
157 endpoint.process(outboundEvent);
158
159 assertTrue(listener.latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
160 assertEquals(EndpointMessageNotification.MESSAGE_DISPATCHED, listener.messageNotification.getAction());
161 assertEquals(endpoint.getEndpointURI().getUri().toString(),
162 listener.messageNotification.getEndpoint());
163 assertTrue(listener.messageNotification.getSource() instanceof MuleMessage);
164 assertEquals(outboundEvent.getMessage().getPayload(),
165 ((MuleMessage) listener.messageNotification.getSource()).getPayload());
166 }
167
168 public void testTransformers() throws Exception
169 {
170 OutboundEndpoint endpoint = createOutboundEndpoint(null, null, new OutboundAppendTransformer(),
171 new ResponseAppendTransformer(), MessageExchangePattern.REQUEST_RESPONSE, null);
172 MuleEvent outboundEvent = createTestOutboundEvent(endpoint);
173 MuleEvent result = endpoint.process(outboundEvent);
174
175 assertMessageSent(true);
176
177 assertEquals(TEST_MESSAGE + OutboundAppendTransformer.APPEND_STRING,
178 dispacher.sensedSendEvent.getMessageAsString());
179
180 assertNotNull(result);
181 assertEquals(RESPONSE_MESSAGE + ResponseAppendTransformer.APPEND_STRING, result.getMessageAsString());
182 }
183
184 public void testConnectorNotStarted() throws Exception
185 {
186 OutboundEndpoint endpoint = createOutboundEndpoint(null, null, null, null,
187 MessageExchangePattern.REQUEST_RESPONSE, null);
188 testOutboundEvent = createTestOutboundEvent(endpoint);
189 endpoint.getConnector().stop();
190
191 try
192 {
193 endpoint.process(testOutboundEvent);
194 fail("Exception expected");
195 }
196 catch (LifecycleException e)
197 {
198
199 }
200 }
201
202 public void testTimeoutSetOnEvent() throws Exception
203 {
204
205 int testTimeout = 999;
206
207 OutboundEndpoint endpoint = createOutboundEndpoint(null, null, null, null,
208 MessageExchangePattern.REQUEST_RESPONSE, null);
209 testOutboundEvent = createTestOutboundEvent(endpoint);
210 testOutboundEvent.getMessage()
211 .setOutboundProperty(MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, testTimeout);
212
213 endpoint.process(testOutboundEvent);
214
215 assertEquals(testTimeout, dispacher.sensedSendEvent.getTimeout());
216 }
217
218 public void testObjectAwareInjection() throws Exception
219 {
220 EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(TEST_URI, muleContext);
221 endpointBuilder.addMessageProcessor(new ObjectAwareProcessor());
222
223 OutboundEndpoint endpoint = endpointBuilder.buildOutboundEndpoint();
224 endpoint.process(createTestOutboundEvent(endpoint));
225
226 ObjectAwareProcessor objectAware = (ObjectAwareProcessor) endpoint.getMessageProcessors().get(0);
227
228 assertEquals(muleContext, objectAware.context);
229 assertEquals(endpoint, objectAware.endpoint);
230 }
231
232 public void testTransaction()
233 {
234
235 }
236
237 public void testExceptionHandling()
238 {
239
240 }
241
242 protected MuleEvent assertMessageSent(boolean sync) throws MuleException
243 {
244 MuleEvent event;
245 if (sync)
246 {
247 assertNull(dispacher.sensedDispatchEvent);
248 assertNotNull(dispacher.sensedSendEvent);
249 event = dispacher.sensedSendEvent;
250 }
251 else
252 {
253 assertNull(dispacher.sensedSendEvent);
254 assertNotNull(dispacher.sensedDispatchEvent);
255 event = dispacher.sensedDispatchEvent;
256 }
257 assertNotNull(event.getMessage());
258 return event;
259 }
260
261 protected MuleEvent assertMessageSentSame(boolean sync) throws MuleException
262 {
263 MuleEvent event = assertMessageSent(sync);
264 if (sync)
265 {
266
267 assertEquals(testOutboundEvent, event);
268 }
269 assertEquals(TEST_MESSAGE, event.getMessageAsString());
270 assertEquals("value1", event.getMessage().getOutboundProperty("prop1"));
271 return event;
272 }
273
274 protected void assertMessageNotSent() throws MuleException
275 {
276 assertNull(dispacher.sensedSendEvent);
277 assertNull(dispacher.sensedDispatchEvent);
278 }
279
280 protected void assertMessageEqualEncodingPropertyAdded(MuleMessage expect, MuleMessage actual)
281 {
282 assertEquals(expect.getPayload(), actual.getPayload());
283 assertEquals(expect.getEncoding(), actual.getEncoding());
284 assertEquals(expect.getUniqueId(), actual.getUniqueId());
285 assertEquals(expect.getExceptionPayload(), actual.getExceptionPayload());
286
287
288 assertEquals(muleContext.getConfiguration().getDefaultEncoding(),
289 actual.getOutboundProperty(MuleProperties.MULE_ENCODING_PROPERTY));
290
291 }
292
293 protected OutboundEndpoint createOutboundEndpoint(String uri, Filter filter,
294 EndpointSecurityFilter securityFilter,
295 Transformer in,
296 Transformer response,
297 MessageExchangePattern exchangePattern,
298 TransactionConfig txConfig) throws Exception
299 {
300
301 OutboundEndpoint endpoint = createTestOutboundEndpoint(uri, filter, securityFilter, in, response,
302 exchangePattern, txConfig);
303 dispacher = new FakeMessageDispatcher(endpoint);
304 Connector connector = endpoint.getConnector();
305 connector.setDispatcherFactory(new TestMessageDispatcherFactory()
306 {
307 @Override
308 public MessageDispatcher create(OutboundEndpoint ep) throws MuleException
309 {
310 return dispacher;
311 }
312
313 });
314 return endpoint;
315 }
316
317 protected OutboundEndpoint createOutboundEndpoint(Filter filter,
318 EndpointSecurityFilter securityFilter,
319 Transformer in,
320 Transformer response,
321 MessageExchangePattern exchangePattern,
322 TransactionConfig txConfig) throws Exception
323 {
324 return createOutboundEndpoint("test://test", filter, securityFilter, in, response, exchangePattern, txConfig);
325
326 }
327
328 static class FakeMessageDispatcher extends TestMessageDispatcher
329 {
330 Latch latch = new Latch();
331 MuleEvent sensedSendEvent;
332 MuleEvent sensedDispatchEvent;
333
334 public FakeMessageDispatcher(OutboundEndpoint endpoint)
335 {
336 super(endpoint);
337 }
338
339 @Override
340 protected MuleMessage doSend(MuleEvent event) throws Exception
341 {
342 sensedSendEvent = event;
343 latch.countDown();
344 return responseMessage;
345 }
346
347 @Override
348 protected void doDispatch(MuleEvent event) throws Exception
349 {
350 sensedDispatchEvent = event;
351 latch.countDown();
352 }
353 }
354
355 }