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.api.transport.PropertyScope;
12  import org.mule.model.resolvers.MethodHeaderPropertyEntryPointResolver;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.tck.testmodels.fruit.Apple;
15  import org.mule.tck.testmodels.fruit.Fruit;
16  import org.mule.transport.NullPayload;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  public class MethodHeaderEntryPointResolverTestCase extends AbstractMuleContextTestCase
23  {
24  
25      private MethodHeaderPropertyEntryPointResolver resolver;
26  
27      @Override
28      protected void doSetUp() throws Exception
29      {
30          super.doSetUp();
31          resolver = new MethodHeaderPropertyEntryPointResolver();
32      }
33  
34      @Test
35      public void testMethodSetPass() throws Exception
36      {
37          MuleEventContext ctx = getTestEventContext("blah");
38          ctx.getMessage().setProperty("method", "someBusinessMethod", PropertyScope.INBOUND);
39  
40          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), ctx);
41          assertInvocationWasSuccessful(result);
42      }
43  
44      @Test
45      public void testMethodSetWithNoArgsPass() throws Exception
46      {
47          MuleEventContext ctx = getTestEventContext(NullPayload.getInstance());
48          ctx.getMessage().setProperty("method", "wash", PropertyScope.INBOUND);
49  
50          InvocationResult result = resolver.invoke(new Apple(), ctx);
51          assertInvocationWasSuccessful(result);
52          assertEquals("wash", result.getMethodCalled());
53      }
54  
55      @Test
56      public void testCustomMethodProperty() throws Exception
57      {
58          resolver.setMethodProperty("serviceMethod");
59  
60          MuleEventContext ctx = getTestEventContext("blah");
61          ctx.getMessage().setProperty("serviceMethod", "someBusinessMethod", PropertyScope.INBOUND);
62  
63          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), ctx);
64          assertInvocationWasSuccessful(result);
65      }
66  
67      @Test
68      public void testCustomMethodPropertyFail() throws Exception
69      {
70          resolver.setMethodProperty("serviceMethod");
71  
72          MuleEventContext ctx = getTestEventContext("blah");
73          ctx.getMessage().setProperty("serviceMethod", "noMethod", PropertyScope.INBOUND);
74  
75          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), ctx);
76          assertInvocationFailed(result);
77      }
78  
79      @Test
80      public void testMethodPropertyFail() throws Exception
81      {
82          resolver.setMethodProperty("serviceMethod");
83  
84          MuleEventContext ctx = getTestEventContext("blah");
85          ctx.getMessage().setProperty("myMethod", "someBusinessMethod", PropertyScope.INBOUND);
86  
87          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), ctx);
88          assertInvocationFailed(result);
89      }
90  
91      @Test
92      public void testMethodPropertyMismatch() throws Exception
93      {
94          MuleEventContext ctx = getTestEventContext("blah");
95          ctx.getMessage().setProperty("method", "noMethod", PropertyScope.INBOUND);
96  
97          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), ctx);
98          assertInvocationFailed(result);
99      }
100 
101     /**
102      * If a method with correct name is available then it should be used if the
103      * parameter type is assignable from the payload type and not just if there is an
104      * exact match. See MULE-3636.
105      */
106     @Test
107     public void testMethodPropertyParameterAssignableFromPayload() throws Exception
108     {
109         MuleEventContext ctx = getTestEventContext(new Apple());
110         ctx.getMessage().setProperty("method", "wash", PropertyScope.INBOUND);
111 
112         InvocationResult result = resolver.invoke(new TestFruitCleaner(), ctx);
113         assertInvocationWasSuccessful(result);
114     }
115 
116     private void assertInvocationWasSuccessful(InvocationResult result)
117     {
118         assertEquals(InvocationResult.State.SUCCESSFUL, result.getState());
119     }
120 
121     private void assertInvocationFailed(InvocationResult result)
122     {
123         assertEquals(InvocationResult.State.FAILED, result.getState());
124     }
125 
126     public static class TestFruitCleaner
127     {
128         public void wash(Fruit fruit)
129         {
130             // dummy
131         }
132 
133         public void polish(Fruit fruit)
134         {
135             // dummy
136         }
137     }
138 }