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;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.transport.PropertyScope;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.test.filters.FilterCounter;
14  
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  
22  /**
23   * Test for MULE-4412 : selective-consumer filter is applied twice. We test that the
24   * filter is only applied once in the positive case, plus make sure it doesn't get
25   * filtered at all when the message does not meet the filter criteria
26   */
27  public class Mule4412TestCase extends FunctionalTestCase
28  {
29      private int RECEIVE_TIMEOUT_MS = 3000;
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "mule-4412.xml";
35      }
36  
37      @Override
38      protected void doSetUp() throws Exception
39      {
40          super.doSetUp();
41          // reset the counter for every test
42          FilterCounter.counter.set(0);
43      }
44  
45      @Override
46      protected void doTearDown() throws Exception
47      {
48          super.doTearDown();
49          // reset the counter for every test
50          FilterCounter.counter.set(0);
51      }
52  
53      /**
54       * Make sure that the message only gets filtered once
55       * 
56       * @throws Exception
57       */
58      @Test
59      public void testFilterOnce() throws Exception
60      {
61          DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
62          msg.setOutboundProperty("pass", "true");
63          MuleClient client = new MuleClient(muleContext);
64          client.send("vm://async", msg);
65          MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
66          int times = FilterCounter.counter.get();
67          assertTrue("did not filter one time as expected, times filtered " + times, times == 1);
68          assertNotNull(reply);
69          assertEquals("wrong message received : " + reply.getPayloadAsString(), TEST_MESSAGE,
70              reply.getPayloadAsString());
71          assertEquals("'pass' property value not correct", "true", reply.getInboundProperty("pass"));
72  
73          // make sure there are no more messages
74          assertNull(client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS));
75      }
76  
77      /**
78       * Make sure the message does not get filtered when the property key is incorrect
79       * 
80       * @throws Exception
81       */
82      @Test
83      public void testWrongPropertyKey() throws Exception
84      {
85          DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
86          msg.setProperty("fail", "true", PropertyScope.INVOCATION);
87          MuleClient client = new MuleClient(muleContext);
88          client.send("vm://async", msg);
89          MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
90          assertNull(reply);
91          assertTrue("should not have filtered", FilterCounter.counter.get() == 0);
92      }
93  
94      /**
95       * Make sure the message does not get filtered when the property value is not as
96       * expected
97       * 
98       * @throws Exception
99       */
100     @Test
101     public void testWrongPropertyValue() throws Exception
102     {
103         DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
104         msg.setInboundProperty("pass", "false");
105         MuleClient client = new MuleClient(muleContext);
106         client.send("vm://async", msg);
107         MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
108         assertNull(reply);
109         assertTrue("should not have filtered", FilterCounter.counter.get() == 0);
110     }
111 
112     /**
113      * Make sure the message does not get filtered at all when the expected property
114      * is not defined
115      * 
116      * @throws Exception
117      */
118     @Test
119     public void testNoProperty() throws Exception
120     {
121         DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
122         MuleClient client = new MuleClient(muleContext);
123         client.send("vm://async", msg);
124         MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
125         assertNull(reply);
126         assertTrue("should not have filtered", FilterCounter.counter.get() == 0);
127     }
128 }