View Javadoc

1   /*
2    * $Id: OGNLFilterTestCase.java 22519 2011-07-22 07:27:13Z justin.calleja $
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.routing.filters;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertNull;
17  import static org.junit.Assert.fail;
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  import org.mule.DefaultMuleMessage;
25  import org.mule.api.MuleMessage;
26  import org.mule.api.config.ConfigurationException;
27  import org.mule.api.routing.OutboundRouterCollection;
28  import org.mule.construct.Flow;
29  import org.mule.module.client.MuleClient;
30  import org.mule.module.ognl.filters.OGNLFilter;
31  import org.mule.routing.MessageFilter;
32  import org.mule.routing.outbound.FilteringOutboundRouter;
33  import org.mule.tck.AbstractServiceAndFlowTestCase;
34  
35  public class OGNLFilterTestCase extends AbstractServiceAndFlowTestCase
36  {
37  
38      public static final String DEFAULT_INPUT_QUEUE = "vm://in";
39      public static final String DEFUALT_OUTPUT_QUEUE = "vm://out";
40      public static final String FIRST_MESSAGE = "foo";
41      public static final String SECOND_MESSAGE = "foobar";
42      public static final String THIRD_MESSAGE = "INPUT MESSAGE";
43      public static final long TIMEOUT = 5000;
44      public static final String OGNL_EXSPRESSION = " equals(\"foo\") || content.endsWith(\"bar\") ";
45      public static final String SERVICE_NAME = "OGNLServiceWrapper1";
46  
47      private OGNLFilter filter;
48  
49      public OGNLFilterTestCase(ConfigVariant variant, String configResources)
50      {
51          super(variant, configResources);
52      }
53  
54      @Parameters
55      public static Collection<Object[]> parameters()
56      {
57          return Arrays.asList(new Object[][]{{ConfigVariant.SERVICE, "ognl-functional-test-service.xml"},
58              {ConfigVariant.FLOW, "ognl-functional-test-flow.xml"}});
59      }
60  
61      @Override
62      protected void doSetUp() throws Exception
63      {
64          filter = new OGNLFilter();
65      }
66  
67      @Override
68      protected void doTearDown() throws Exception
69      {
70          filter = null;
71      }
72  
73      @Test
74      public void testNewFilter()
75      {
76          assertFalse(filter.accept(null));
77      }
78  
79      @Test
80      public void testNoExpressionEmptyMessage()
81      {
82          MuleMessage message = new DefaultMuleMessage(null, muleContext);
83          assertFalse(filter.accept(message));
84      }
85  
86      @Test
87      public void testNoExpressionValidMessage()
88      {
89          MuleMessage message = new DefaultMuleMessage("foo", muleContext);
90          assertFalse(filter.accept(message));
91      }
92  
93      @Test
94      public void testNamespaceHandler()
95      {
96          String expression;
97  
98          if (variant.equals(ConfigVariant.SERVICE))
99          {
100             expression = ((OGNLFilter) ((FilteringOutboundRouter) ((OutboundRouterCollection) muleContext.getRegistry()
101                 .lookupService(SERVICE_NAME)
102                 .getOutboundMessageProcessor()).getRoutes().get(0)).getFilter()).getExpression();
103         }
104         else
105         {
106             expression = ((OGNLFilter) ((MessageFilter) ((Flow) muleContext.getRegistry()
107                 .lookupFlowConstruct(SERVICE_NAME)).getMessageProcessors().get(0)).getFilter()).getExpression();
108         }
109 
110         assertEquals(expression, OGNL_EXSPRESSION);
111     }
112 
113     @Test
114     public void testFunctionalTest() throws Exception
115     {
116         MuleClient client = new MuleClient(muleContext);
117         try
118         {
119             client.dispatch(DEFAULT_INPUT_QUEUE, FIRST_MESSAGE, null);
120             MuleMessage message = client.request(DEFUALT_OUTPUT_QUEUE, TIMEOUT);
121             assertNotNull(message);
122             assertNotNull(message.getPayload());
123             assertNull(message.getExceptionPayload());
124             assertEquals(FIRST_MESSAGE, message.getPayload());
125 
126             Dummy payload = new Dummy();
127             payload.setContent(SECOND_MESSAGE);
128             client.dispatch(DEFAULT_INPUT_QUEUE, new DefaultMuleMessage(payload, muleContext));
129             message = client.request(DEFUALT_OUTPUT_QUEUE, TIMEOUT);
130             assertNotNull(message);
131             assertNotNull(message.getPayload());
132             assertNull(message.getExceptionPayload());
133             assertEquals(SECOND_MESSAGE, ((Dummy) message.getPayload()).getContent());
134 
135             client.dispatch(DEFAULT_INPUT_QUEUE, THIRD_MESSAGE, null);
136             message = client.request(DEFUALT_OUTPUT_QUEUE, TIMEOUT);
137             assertNull(message);
138         }
139         finally
140         {
141             client.dispose();
142         }
143     }
144 
145     @Test
146     public void testFunctionalTestUsingExpressionFilter() throws Exception
147     {
148         MuleClient client = new MuleClient(muleContext);
149         try
150         {
151             client.dispatch("vm://in2", FIRST_MESSAGE, null);
152             MuleMessage message = client.request("vm://out2", TIMEOUT);
153             assertNotNull(message);
154             assertNotNull(message.getPayload());
155             assertNull(message.getExceptionPayload());
156             assertEquals(FIRST_MESSAGE, message.getPayload());
157 
158             Dummy payload = new Dummy();
159             payload.setContent(SECOND_MESSAGE);
160             client.dispatch("vm://in2", new DefaultMuleMessage(payload, muleContext));
161             message = client.request("vm://out2", TIMEOUT);
162             assertNotNull(message);
163             assertNotNull(message.getPayload());
164             assertNull(message.getExceptionPayload());
165             assertEquals(SECOND_MESSAGE, ((Dummy) message.getPayload()).getContent());
166 
167             client.dispatch("vm://in2", THIRD_MESSAGE, null);
168             message = client.request("vm://out2", TIMEOUT);
169             assertNull(message);
170         }
171         finally
172         {
173             client.dispose();
174         }
175     }
176 
177     @Test
178     public void testInvalidObjectExpression()
179     {
180         try
181         {
182             filter.setExpression("foo:bar");
183             fail("should have failed with ConfigurationException");
184         }
185         catch (ConfigurationException configex)
186         {
187             // expected
188         }
189 
190         // make sure the filter is still unconfigured
191         assertNull(filter.getExpression());
192     }
193 
194     // a simple POJO for testing object expressions
195     private static class Dummy
196     {
197         private int id;
198         private String content;
199 
200         public Dummy()
201         {
202             super();
203         }
204 
205         /**
206          * @return Returns the content.
207          */
208         public String getContent()
209         {
210             return content;
211         }
212 
213         /**
214          * @param content The content to set.
215          */
216         public void setContent(String content)
217         {
218             this.content = content;
219         }
220 
221         /**
222          * @return Returns the id.
223          */
224         public int getId()
225         {
226             return id;
227         }
228 
229         /**
230          * @param id The id to set.
231          */
232         public void setId(int id)
233         {
234             this.id = id;
235         }
236     }
237 
238 }