1
2
3
4
5
6
7
8
9
10
11 package org.mule.tck.functional;
12
13 import org.mule.RequestContext;
14 import org.mule.api.MuleContext;
15 import org.mule.api.MuleEventContext;
16 import org.mule.api.MuleException;
17 import org.mule.api.MuleMessage;
18 import org.mule.api.context.MuleContextAware;
19 import org.mule.api.lifecycle.Callable;
20 import org.mule.api.lifecycle.Disposable;
21 import org.mule.api.lifecycle.Initialisable;
22 import org.mule.tck.exceptions.FunctionalTestException;
23 import org.mule.util.ClassUtils;
24 import org.mule.util.NumberUtils;
25 import org.mule.util.StringMessageUtils;
26 import org.mule.util.SystemUtils;
27
28 import java.util.List;
29 import java.util.concurrent.CopyOnWriteArrayList;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class FunctionalTestComponent implements Callable, Initialisable, Disposable, MuleContextAware, Receiveable
50 {
51 protected transient Log logger = LogFactory.getLog(getClass());
52
53 public static final int STREAM_SAMPLE_SIZE = 4;
54 public static final int STREAM_BUFFER_SIZE = 4096;
55
56 private EventCallback eventCallback;
57 private Object returnData = null;
58 private boolean throwException = false;
59 private Class<? extends Throwable> exceptionToThrow;
60 private String exceptionText = "";
61 private boolean enableMessageHistory = true;
62 private boolean enableNotifications = true;
63 private boolean doInboundTransform = true;
64 private String appendString;
65 private long waitTime = 0;
66 private boolean logMessageDetails = false;
67 private MuleContext muleContext;
68
69
70
71
72
73
74 private List<Object> messageHistory;
75
76 @Override
77 public void initialise()
78 {
79 if (enableMessageHistory)
80 {
81 messageHistory = new CopyOnWriteArrayList<Object>();
82 }
83 }
84
85 @Override
86 public void setMuleContext(MuleContext context)
87 {
88 this.muleContext = context;
89 }
90
91 @Override
92 public void dispose()
93 {
94
95 }
96
97
98
99
100 @Override
101 public Object onCall(MuleEventContext context) throws Exception
102 {
103
104 if (isThrowException())
105 {
106 throwException();
107 }
108 return process(getMessageFromContext(context), context);
109 }
110
111 private Object getMessageFromContext(MuleEventContext context) throws MuleException
112 {
113 if (isDoInboundTransform())
114 {
115 Object o = context.getMessage().getPayload();
116 if (getAppendString() != null && !(o instanceof String))
117 {
118 o = context.transformMessageToString();
119 }
120 return o;
121 }
122 else if (getAppendString()!=null)
123 {
124 return context.getMessageAsString();
125 }
126 else
127 {
128 return context.getMessage().getPayload();
129 }
130 }
131
132
133
134
135
136
137
138
139
140 @Override
141 public Object onReceive(Object data) throws Exception
142 {
143 MuleEventContext context = RequestContext.getEventContext();
144
145 if (isThrowException())
146 {
147 throwException();
148 }
149 return process(data, context);
150 }
151
152
153
154
155
156
157
158
159 protected void throwException() throws Exception
160 {
161 if (getExceptionToThrow() != null)
162 {
163 if (StringUtils.isNotBlank(exceptionText))
164 {
165 Throwable exception = ClassUtils.instanciateClass(getExceptionToThrow(),
166 new Object[] { exceptionText });
167 throw (Exception) exception;
168 }
169 else
170 {
171 throw (Exception) getExceptionToThrow().newInstance();
172 }
173 }
174 else
175 {
176 if (StringUtils.isNotBlank(exceptionText))
177 {
178 throw new FunctionalTestException(exceptionText);
179 }
180 else
181 {
182 throw new FunctionalTestException();
183 }
184 }
185 }
186
187
188
189
190
191
192
193
194
195
196 protected String append(String contents, MuleMessage message)
197 {
198 return contents + muleContext.getExpressionManager().parse(appendString, message);
199 }
200
201
202
203
204
205
206
207
208
209
210 protected Object process(Object data, MuleEventContext context) throws Exception
211 {
212
213 if (enableMessageHistory)
214 {
215 messageHistory.add(data);
216 }
217
218 if (logger.isInfoEnabled())
219 {
220 String msg = StringMessageUtils.getBoilerPlate("Message Received in service: "
221 + context.getFlowConstruct().getName() + ". Content is: "
222 + StringMessageUtils.truncate(data.toString(), 100, true), '*', 80);
223
224 logger.info(msg);
225 }
226
227 final MuleMessage message = context.getMessage();
228 if (isLogMessageDetails() && logger.isInfoEnabled())
229 {
230 StringBuilder sb = new StringBuilder();
231
232 sb.append("Full Message payload: ").append(SystemUtils.LINE_SEPARATOR);
233 sb.append(message.getPayload()).append(SystemUtils.LINE_SEPARATOR);
234 sb.append(StringMessageUtils.headersToString(message));
235 logger.info(sb.toString());
236 }
237
238 if (eventCallback != null)
239 {
240 eventCallback.eventReceived(context, this);
241 }
242
243 Object replyMessage;
244 if (returnData != null)
245 {
246 if (returnData instanceof String && muleContext.getExpressionManager().isExpression(returnData.toString()))
247 {
248 replyMessage = muleContext.getExpressionManager().parse(returnData.toString(), message);
249 }
250 else
251 {
252 replyMessage = returnData;
253 }
254 }
255 else
256 {
257 if (appendString != null)
258 {
259 replyMessage = append(data.toString(), message);
260 }
261 else
262 {
263 replyMessage = data;
264 }
265 }
266
267 if (isEnableNotifications())
268 {
269 muleContext.fireNotification(
270 new FunctionalTestNotification(context, replyMessage, FunctionalTestNotification.EVENT_RECEIVED));
271 }
272
273
274 if (waitTime > 0)
275 {
276 try
277 {
278 Thread.sleep(waitTime);
279 }
280 catch (InterruptedException e)
281 {
282 logger.info("FunctionalTestComponent waitTime was interrupted");
283 }
284 }
285 return replyMessage;
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 public EventCallback getEventCallback()
303 {
304 return eventCallback;
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 public void setEventCallback(EventCallback eventCallback)
322 {
323 this.eventCallback = eventCallback;
324 }
325
326
327
328
329
330
331
332
333 public Object getReturnData()
334 {
335 return returnData;
336 }
337
338
339
340
341
342
343
344
345 public void setReturnData(Object returnData)
346 {
347 this.returnData = returnData;
348 }
349
350
351
352
353
354
355
356
357
358 public boolean isThrowException()
359 {
360 return throwException;
361 }
362
363
364
365
366
367
368
369
370
371 public void setThrowException(boolean throwException)
372 {
373 this.throwException = throwException;
374 }
375
376 public boolean isEnableMessageHistory()
377 {
378 return enableMessageHistory;
379 }
380
381 public void setEnableMessageHistory(boolean enableMessageHistory)
382 {
383 this.enableMessageHistory = enableMessageHistory;
384 }
385
386
387
388
389
390 public int getReceivedMessagesCount()
391 {
392 if (messageHistory != null)
393 {
394 return messageHistory.size();
395 }
396 else
397 {
398 return NumberUtils.INTEGER_MINUS_ONE.intValue();
399 }
400 }
401
402
403
404
405
406
407 public Object getReceivedMessage(int number)
408 {
409 Object message = null;
410 if (messageHistory != null)
411 {
412 if (number <= messageHistory.size())
413 {
414 message = messageHistory.get(number - 1);
415 }
416 }
417 return message;
418 }
419
420
421
422
423 public Object getLastReceivedMessage()
424 {
425 if (messageHistory != null)
426 {
427 return messageHistory.get(messageHistory.size() - 1);
428 }
429 else
430 {
431 return null;
432 }
433 }
434
435 public String getAppendString()
436 {
437 return appendString;
438 }
439
440 public void setAppendString(String appendString)
441 {
442 this.appendString = appendString;
443 }
444
445 public boolean isEnableNotifications()
446 {
447 return enableNotifications;
448 }
449
450 public void setEnableNotifications(boolean enableNotifications)
451 {
452 this.enableNotifications = enableNotifications;
453 }
454
455 public Class<? extends Throwable> getExceptionToThrow()
456 {
457 return exceptionToThrow;
458 }
459
460 public void setExceptionToThrow(Class<? extends Throwable> exceptionToThrow)
461 {
462 this.exceptionToThrow = exceptionToThrow;
463 }
464
465 public long getWaitTime()
466 {
467 return waitTime;
468 }
469
470 public void setWaitTime(long waitTime)
471 {
472 this.waitTime = waitTime;
473 }
474
475 public boolean isDoInboundTransform()
476 {
477 return doInboundTransform;
478 }
479
480 public void setDoInboundTransform(boolean doInboundTransform)
481 {
482 this.doInboundTransform = doInboundTransform;
483 }
484
485 public boolean isLogMessageDetails()
486 {
487 return logMessageDetails;
488 }
489
490 public void setLogMessageDetails(boolean logMessageDetails)
491 {
492 this.logMessageDetails = logMessageDetails;
493 }
494
495 public String getExceptionText()
496 {
497 return exceptionText;
498 }
499
500 public void setExceptionText(String text)
501 {
502 exceptionText = text;
503 }
504 }