View Javadoc

1   /*
2    * $Id: FlowTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
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.construct;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.processor.ResponseMessageProcessorAdapter;
18  import org.mule.tck.MuleTestUtils;
19  import org.mule.tck.SensingNullMessageProcessor;
20  import org.mule.transformer.simple.StringAppendTransformer;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotSame;
29  import static org.junit.Assert.assertNull;
30  import static org.junit.Assert.fail;
31  
32  public class FlowTestCase extends AbstractFlowConstuctTestCase
33  {
34  
35      private Flow flow;
36      private SensingNullMessageProcessor sensingMessageProcessor;
37  
38      @Override
39      protected void doSetUp() throws Exception
40      {
41          super.doSetUp();
42  
43          sensingMessageProcessor = getSensingNullMessageProcessor();
44  
45          flow = new Flow("test-flow", muleContext);
46          flow.setMessageSource(directInboundMessageSource);
47  
48          List<MessageProcessor> processors = new ArrayList<MessageProcessor>();
49          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("f")));
50          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("e")));
51          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("d")));
52          processors.add(new StringAppendTransformer("a"));
53          processors.add(new StringAppendTransformer("b"));
54          processors.add(new StringAppendTransformer("c"));
55          processors.add(new MessageProcessor()
56          {
57              public MuleEvent process(MuleEvent event) throws MuleException
58              {
59                  event.getMessage().setOutboundProperty("thread", Thread.currentThread());
60                  return event;
61              }
62          });
63          processors.add(sensingMessageProcessor);
64          flow.setMessageProcessors(processors);
65      }
66  
67      @Override
68      protected AbstractFlowConstruct getFlowConstruct() throws Exception
69      {
70          return flow;
71      }
72  
73      @Test
74      public void testProcessOneWayEndpoint() throws Exception
75      {
76          flow.initialise();
77          flow.start();
78          MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestEvent("hello",
79              MessageExchangePattern.ONE_WAY, muleContext));
80          Thread.sleep(50);
81  
82          assertNull(response);
83  
84          assertEquals("helloabc", sensingMessageProcessor.event.getMessageAsString());
85          assertNotSame(Thread.currentThread(), sensingMessageProcessor.event.getMessage().getOutboundProperty(
86              "thread"));
87      }
88      
89      @Test
90      public void testProcessRequestResponseEndpoint() throws Exception
91      {
92          flow.initialise();
93          flow.start();
94          MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestEvent("hello",
95              MessageExchangePattern.REQUEST_RESPONSE, muleContext));
96  
97          assertEquals("helloabcdef", response.getMessageAsString());
98          assertEquals(Thread.currentThread(), response.getMessage().getOutboundProperty("thread"));
99  
100         // Sensed (out) event also is appended with 'def' because it's the same event
101         // instance
102         assertEquals("helloabcdef", sensingMessageProcessor.event.getMessageAsString());
103         assertEquals(Thread.currentThread(), sensingMessageProcessor.event.getMessage().getOutboundProperty(
104             "thread"));
105 
106     }
107 
108     @Test
109     public void testProcessStopped() throws Exception
110     {
111         flow.initialise();
112 
113         try
114         {
115             directInboundMessageSource.process(MuleTestUtils.getTestEvent("hello", muleContext));
116             fail("exception expected");
117         }
118         catch (Exception e)
119         {
120         }
121     }
122 }