View Javadoc

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