View Javadoc

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