View Javadoc

1   /*
2    * $Id: LegacyEntryPointResolverTestCase.java 22377 2011-07-11 12:41:42Z 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.mule.model;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.model.EntryPointResolverSet;
17  import org.mule.model.resolvers.ArrayEntryPointResolver;
18  import org.mule.model.resolvers.EntryPointNotFoundException;
19  import org.mule.model.resolvers.LegacyEntryPointResolverSet;
20  import org.mule.tck.junit4.AbstractMuleContextTestCase;
21  import org.mule.tck.testmodels.fruit.Apple;
22  import org.mule.tck.testmodels.fruit.Banana;
23  import org.mule.tck.testmodels.fruit.Fruit;
24  import org.mule.tck.testmodels.fruit.FruitBowl;
25  import org.mule.tck.testmodels.fruit.FruitCleaner;
26  import org.mule.tck.testmodels.fruit.FruitLover;
27  import org.mule.tck.testmodels.fruit.Kiwi;
28  import org.mule.tck.testmodels.fruit.Orange;
29  import org.mule.tck.testmodels.fruit.WaterMelon;
30  
31  import java.util.Arrays;
32  
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertFalse;
36  import static org.junit.Assert.assertTrue;
37  import static org.junit.Assert.fail;
38  
39  public class LegacyEntryPointResolverTestCase extends AbstractMuleContextTestCase
40  {
41  
42      /** Name of the method override property on the event. */
43      private static final String METHOD_PROPERTY_NAME = MuleProperties.MULE_METHOD_PROPERTY;
44  
45      /** Name of the non-existent method. */
46      private static final String INVALID_METHOD_NAME = "nosuchmethod";
47  
48      @Test
49      public void testExplicitMethodMatch() throws Exception
50      {
51          try
52          {
53              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
54              resolver.invoke(new WaterMelon(), getTestEventContext("blah"));
55          }
56          catch (MuleException e)
57          {
58              fail("Test should have passed: " + e);
59          }
60      }
61  
62      @Test
63      public void testExplicitMethodMatchComplexObject() throws Exception
64      {
65          try
66          {
67              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
68              resolver.invoke(new FruitBowl(), getTestEventContext(new FruitLover("Mmmm")));
69          }
70          catch (MuleException e)
71          {
72              fail("Test should have passed: " + e);
73          }
74      }
75  
76      @Test
77      public void testExplicitMethodMatchSetArrayFail() throws Exception
78      {
79          try
80          {
81              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
82              resolver.invoke(new FruitBowl(), getTestEventContext(new Fruit[]{new Apple(), new Orange()}));
83              fail("Test should have failed because the arguments were not wrapped properly: ");
84          }
85          catch (MuleException e)
86          {
87              //expected
88          }
89      }
90  
91      @Test
92      public void testExplicitMethodMatchSetArrayPass() throws Exception
93      {
94          try
95          {
96              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
97              resolver.invoke(new FruitBowl(), getTestEventContext(new Object[]{new Fruit[]{new Apple(), new Orange()}}));
98          }
99          catch (MuleException e)
100         {
101             fail("Test should have passed: " + e);
102         }
103     }
104 
105     /* this tests the same as above except it uses the {@link ArrayEntryPointResolver} and does not wrap the args with an array
106      */
107     @Test
108     public void testExplicitMethodMatchSetArrayPassUsingExplicitResolver() throws Exception
109     {
110         try
111         {
112             LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
113             resolver.addEntryPointResolver(new ArrayEntryPointResolver());
114             resolver.invoke(new FruitBowl(), getTestEventContext(new Fruit[]{new Apple(), new Orange()}));
115         }
116         catch (MuleException e)
117         {
118             fail("Test should have passed: " + e);
119         }
120     }
121 
122 //    public ComponentMethodMapping[] getComponentMappings()
123 //    {
124 //        ComponentMethodMapping[] mappings = new ComponentMethodMapping[5];
125 //        mappings[0] = new ComponentMethodMapping(WaterMelon.class, "myEventHandler", MuleEvent.class);
126 //        mappings[1] = new ComponentMethodMapping(FruitBowl.class, "consumeFruit", FruitLover.class);
127 //        // see testArrayArgumentResolution
128 //        mappings[2] = new ComponentMethodMapping(FruitBowl.class, "setFruit", Fruit[].class);
129 //        // see testListArgumentResolution
130 //        mappings[3] = new ComponentMethodMapping(FruitBowl.class, "setFruit", Collection.class);
131 //        mappings[4] = new ComponentMethodMapping(Banana.class, "peelEvent", EventObject.class);
132 //
133 //        // TODO This fails because "implementation" can no longer be a container reference, it must be
134 //        // an actual object and InvocationHandler does not have a default constructor.
135 //
136 //        // test proxy objects
137 //        //mappings[5] = new ComponentMethodMapping(InvocationHandler.class, "invoke", FruitLover.class, true);
138 //
139 //        return mappings;
140 //    }
141 
142 //    @Override
143 //    public UMODescriptor getDescriptorToResolve(String className) throws Exception
144 //    {
145 //        UMODescriptor descriptor = super.getDescriptorToResolve(className);
146 //        descriptor.setInboundRouter(new DefaultInboundRouterCollection());
147 //        Endpoint endpoint = new MuleEndpoint("test://foo", true);
148 //
149 //        if (className.equals(FruitBowl.class.getName()))
150 //        {
151 //            endpoint.setTransformers(CollectionUtils.singletonList(new ObjectToFruitLover()));
152 //            descriptor.getInboundRouter().addEndpoint(endpoint);
153 //        }
154 //        else if (className.equals(InvocationHandler.class.getName()))
155 //        {
156 //            endpoint.setTransformers(CollectionUtils.singletonList(new ObjectToFruitLover()));
157 //            descriptor.getInboundRouter().addEndpoint(endpoint);
158 //        }
159 //        return descriptor;
160 //    }
161 
162     /**
163      * Tests entrypoint discovery when there is more than one discoverable method
164      * with MuleEventContext parameter.
165      */
166     @Test
167     public void testFailEntryPointMultiplePayloadMatches() throws Exception
168     {
169         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
170 
171         try
172         {
173             RequestContext.setEvent(getTestEvent("Hello"));
174             resolverSet.invoke(new MultiplePayloadsTestObject(), RequestContext.getEventContext());
175             fail("Should have failed to find entrypoint.");
176         }
177         catch (EntryPointNotFoundException itex)
178         {
179             // expected
180         }
181     }
182 
183     /**
184      * If there was a method parameter specified to override the discovery mechanism
185      * and no such method exists, an exception should be thrown, and no fallback to
186      * the default discovery should take place.
187      */
188     @Test
189     public void testMethodOverrideDoesNotFallback() throws Exception
190     {
191         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
192         RequestContext.setEvent(getTestEvent(new FruitLover("Yummy!")));
193 
194         // those are usually set on the endpoint and copied over to the message
195         final String methodName = "nosuchmethod";
196         final String propertyName = MuleProperties.MULE_METHOD_PROPERTY;
197         RequestContext.getEventContext().getMessage().setOutboundProperty(propertyName, methodName);
198 
199         resolverSet.invoke(new FruitBowl(), RequestContext.getEventContext());
200         // fail("Should have failed to find an entrypoint.");
201     }
202 
203     /**
204      * If there was a method parameter specified to override the discovery mechanism
205      * and a Callable instance is serving the request, call the Callable, ignore the
206      * method override parameter.
207      */
208     @Test
209     public void testMethodOverrideIgnoredWithCallable() throws Exception
210     {
211         EntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
212 
213         RequestContext.setEvent(getTestEvent(new FruitLover("Yummy!")));
214 
215         // those are usually set on the endpoint and copied over to the message
216         RequestContext.getEventContext().getMessage().setOutboundProperty(METHOD_PROPERTY_NAME,
217                                                                           INVALID_METHOD_NAME);
218 
219         Apple apple = new Apple();
220         apple.setAppleCleaner(new FruitCleaner()
221         {
222             public void wash(Fruit fruit)
223             {
224                 // dummy
225             }
226 
227             public void polish(Fruit fruit)
228             {
229                 // dummy
230             }
231         });
232         resolver.invoke(apple, RequestContext.getEventContext());
233     }
234 
235     /**
236      * If there was a method parameter specified to override the discovery mechanism
237      * and a target instance has a method accepting MuleEventContext, proceed to call
238      * this method, ignore the method override parameter.
239      */
240     @Test
241     public void testMethodOverrideIgnoredWithEventContext() throws Exception
242     {
243         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
244 
245         RequestContext.setEvent(getTestEvent(new FruitLover("Yummy!")));
246 
247         // those are usually set on the endpoint and copied over to the message
248         final String methodName = "nosuchmethod";
249         final String propertyName = MuleProperties.MULE_METHOD_PROPERTY;
250         RequestContext.getEventContext().getMessage().setOutboundProperty(propertyName, methodName);
251 
252         try
253         {
254             resolverSet.invoke(new Kiwi(), RequestContext.getEventContext());
255             fail("no such method on service");
256         }
257         catch (EntryPointNotFoundException e)
258         {
259             // expected
260         }
261     }
262 
263     /** Test for proper resolution of a method that takes an array as argument. */
264     // TODO MULE-1088: currently fails, therefore disabled
265     @Test
266     public void testArrayArgumentResolution() throws Exception
267     {
268         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
269 
270         Object payload = new Object[]{new Fruit[]{new Apple(), new Banana()}};
271         RequestContext.setEvent(getTestEvent(payload));
272 
273         FruitBowl bowl = new FruitBowl();
274         assertFalse(bowl.hasApple());
275         assertFalse(bowl.hasBanana());
276 
277         resolverSet.invoke(bowl, RequestContext.getEventContext());
278 
279         assertTrue(bowl.hasApple());
280         assertTrue(bowl.hasBanana());
281     }
282 
283     /** Test for proper resolution of a method that takes a List as argument. */
284     @Test
285     public void testListArgumentResolution() throws Exception
286     {
287         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
288         Object payload = Arrays.asList(new Fruit[]{new Apple(), new Banana()});
289         RequestContext.setEvent(getTestEvent(payload));
290 
291         FruitBowl bowl = new FruitBowl();
292         assertFalse(bowl.hasApple());
293         assertFalse(bowl.hasBanana());
294 
295         resolverSet.invoke(bowl, RequestContext.getEventContext());
296 
297         assertTrue(bowl.hasApple());
298         assertTrue(bowl.hasBanana());
299     }
300 
301     /** Test for proper resolution of an existing method specified as override */
302     @Test
303     public void testExplicitOverride() throws Exception
304     {
305         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
306 
307         Object payload = Arrays.asList(new Fruit[]{new Apple(), new Banana()});
308         RequestContext.setEvent(getTestEvent(payload));
309 
310         final String methodName = "setFruit";
311         final String propertyName = MuleProperties.MULE_METHOD_PROPERTY;
312         RequestContext.getEventContext().getMessage().setOutboundProperty(propertyName, methodName);
313 
314         FruitBowl bowl = new FruitBowl();
315         assertFalse(bowl.hasApple());
316         assertFalse(bowl.hasBanana());
317 
318         resolverSet.invoke(bowl, RequestContext.getEventContext());
319 
320         assertTrue(bowl.hasApple());
321         assertTrue(bowl.hasBanana());
322     }
323 }