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.mule.model;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.model.InvocationResult;
11  import org.mule.model.resolvers.ExplicitMethodEntryPointResolver;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  import org.mule.tck.testmodels.fruit.Apple;
14  import org.mule.tck.testmodels.fruit.Fruit;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.fail;
20  
21  public class ExplicitMethodEntryPointResolverTestCase extends AbstractMuleContextTestCase
22  {
23      @Test
24      public void testMethodSetPass() throws Exception
25      {
26          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
27          resolver.addMethod("someBusinessMethod");
28          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
29          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
30      }
31  
32      @Test
33      public void testMethodSetMatchFirst() throws Exception
34      {
35          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
36          resolver.addMethod("someBusinessMethod");
37          resolver.addMethod("someSetter");
38          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
39          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
40      }
41  
42      @Test
43      public void testMethodNotFound() throws Exception
44      {
45          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
46          resolver.addMethod("noMethod");
47          resolver.addMethod("noMethod2");
48          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
49          assertEquals(result.getState(), InvocationResult.State.FAILED);
50      }
51  
52      @Test
53      public void testNoMethodSet() throws Exception
54      {
55          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
56          try
57          {
58              resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
59              fail("method property is not set, this should cause an error");
60          }
61          catch (IllegalStateException e)
62          {
63              //Expected
64          }
65      }
66  
67      /**
68       * If a method with correct name is available then it should be used is the
69       * parameter type is assignable from the payload type and not just if there is an
70       * exact match. See MULE-3636.
71       *
72       * @throws Exception
73       */
74      @Test
75      public void testMethodPropertyParameterAssignableFromPayload() throws Exception
76      {
77          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
78          resolver.addMethod("wash");
79          MuleEventContext ctx = getTestEventContext(new Apple());
80          InvocationResult result = resolver.invoke(new TestFruitCleaner(), ctx);
81          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
82      }
83  
84      /**
85       * If a method with correct name is available then it should be used even if one
86       * or more parameter types in the payload are null, as long as the parameter
87       * count matches.
88       *
89       * @throws Exception
90       */
91      @Test
92      public void testMethodPropertyParameterNull() throws Exception
93      {
94          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
95          resolver.addMethod("someOtherBusinessMethod");
96          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(),
97              getTestEventContext(new Object[]{null, "blah"}));
98          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
99      }
100 
101     public static class TestFruitCleaner
102     {
103         public void wash(Fruit fruit)
104         {
105             // dummy
106         }
107 
108         public void polish(Fruit fruit)
109         {
110             // dummy
111         }
112     }
113 }