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