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.transport.vm.functional.transactions;
8   
9   import org.mule.api.DefaultMuleException;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.processor.MessageProcessor;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  import org.mule.transport.NullPayload;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  
26  /** Test transaction behavior when "joinExternal" is set to disallow joining external transactions
27   * There is one test per legal transactional behavior (e.g. ALWAYS_BEGIN).
28   */
29  public class MessageFilterTestCase extends FunctionalTestCase
30  {
31  
32      protected static final Log logger = LogFactory.getLog(MessageFilterTestCase.class);
33  
34      private static String rejectMesage;
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "org/mule/test/config/message-filter-config.xml";
40      }
41  
42      /** Check that the configuration specifies considers external transactions */
43      @Test
44      public void testConfiguration() throws Exception
45      {
46          MuleClient client = new MuleClient(muleContext);
47          
48          MuleMessage response = client.send("vm://order.validation", "OK", null);
49          assertTrue(response.getPayload() instanceof NullPayload);
50          assertEquals("OK(rejected!-1)", rejectMesage);
51          
52          response = client.send("vm://order.validation", "OK-ABC", null);
53          assertTrue(response.getPayload() instanceof NullPayload);
54          assertEquals("OK-ABC(rejected!-2)", rejectMesage);
55          
56          response = client.send("vm://order.validation", "OK-DEF", null);
57          assertTrue(response.getPayload() instanceof NullPayload);
58          assertEquals("OK-DEF(rejected!-1)", rejectMesage);
59          rejectMesage = null;
60          
61          response = client.send("vm://order.validation", "OK-ABC-DEF", null);
62          assertEquals("OK-ABC-DEF(success)", response.getPayloadAsString());
63          assertNull(rejectMesage);
64      }
65  
66      public static class Reject1 implements MessageProcessor
67      {
68          public void setName(String name)
69          {
70          }
71  
72          public MuleEvent process(MuleEvent event) throws MuleException
73          {
74              try
75              {
76                  MuleMessage msg = event.getMessage();
77                  String payload = msg.getPayloadAsString();
78                  rejectMesage = payload + "(rejected!-1)";
79                  return null;
80              }
81              catch (Exception e)
82              {
83                  throw new DefaultMuleException(e);
84              }
85          }
86      }
87  
88       public static class Reject2 implements MessageProcessor
89       {
90           public void setName(String name)
91           {
92           }
93  
94          public MuleEvent process(MuleEvent event) throws MuleException
95          {
96              try
97              {
98                  MuleMessage msg = event.getMessage();
99                  String payload = msg.getPayloadAsString();
100                 rejectMesage = payload + "(rejected!-2)";
101                 return null;
102             }
103             catch (Exception e)
104             {
105                 throw new DefaultMuleException(e);
106             }
107         }
108     }
109 }