View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.construct;
8   
9   import org.mule.MessageExchangePattern;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.processor.MessageProcessor;
13  import org.mule.processor.ResponseMessageProcessorAdapter;
14  import org.mule.tck.MuleTestUtils;
15  import org.mule.tck.SensingNullMessageProcessor;
16  import org.mule.transformer.simple.StringAppendTransformer;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotSame;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.fail;
27  
28  public class FlowTestCase extends AbstractFlowConstuctTestCase
29  {
30  
31      private SimpleFlowConstruct flow;
32      private SensingNullMessageProcessor sensingMessageProcessor;
33  
34      @Override
35      protected void doSetUp() throws Exception
36      {
37          super.doSetUp();
38  
39          sensingMessageProcessor = getSensingNullMessageProcessor();
40  
41          flow = new SimpleFlowConstruct("test-flow", muleContext);
42          flow.setMessageSource(directInboundMessageSource);
43  
44          List<MessageProcessor> processors = new ArrayList<MessageProcessor>();
45          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("f")));
46          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("e")));
47          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("d")));
48          processors.add(new StringAppendTransformer("a"));
49          processors.add(new StringAppendTransformer("b"));
50          processors.add(new StringAppendTransformer("c"));
51          processors.add(new MessageProcessor()
52          {
53              public MuleEvent process(MuleEvent event) throws MuleException
54              {
55                  event.getMessage().setOutboundProperty("thread", Thread.currentThread());
56                  return event;
57              }
58          });
59          processors.add(sensingMessageProcessor);
60          flow.setMessageProcessors(processors);
61      }
62  
63      @Override
64      protected AbstractFlowConstruct getFlowConstruct() throws Exception
65      {
66          return flow;
67      }
68  
69      @Test
70      public void testProcessOneWayEndpoint() throws Exception
71      {
72          flow.initialise();
73          flow.start();
74          MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent("hello",
75              MessageExchangePattern.ONE_WAY, muleContext));
76          Thread.sleep(10);
77  
78          assertNull(response);
79  
80          assertEquals("helloabc", sensingMessageProcessor.event.getMessageAsString());
81          assertNotSame(Thread.currentThread(), sensingMessageProcessor.event.getMessage().getOutboundProperty(
82              "thread"));
83      }
84  
85      @Test
86      public void testProcessRequestResponseEndpoint() throws Exception
87      {
88          flow.initialise();
89          flow.start();
90          MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent("hello",
91              MessageExchangePattern.REQUEST_RESPONSE, muleContext));
92  
93          assertEquals("helloabcdef", response.getMessageAsString());
94          assertEquals(Thread.currentThread(), response.getMessage().getOutboundProperty("thread"));
95  
96          // Sensed (out) event also is appended with 'def' because it's the same event
97          // instance
98          assertEquals("helloabcdef", sensingMessageProcessor.event.getMessageAsString());
99          assertEquals(Thread.currentThread(), sensingMessageProcessor.event.getMessage().getOutboundProperty(
100             "thread"));
101 
102     }
103 
104     @Test
105     public void testProcessStopped() throws Exception
106     {
107         flow.initialise();
108 
109         try
110         {
111             directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent("hello", muleContext));
112             fail("exception expected");
113         }
114         catch (Exception e)
115         {
116         }
117     }
118 }