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.module.xml.config;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.api.expression.RequiredValueException;
12  import org.mule.api.model.InvocationResult;
13  import org.mule.api.transport.PropertyScope;
14  import org.mule.impl.model.resolvers.AnnotatedEntryPointResolver;
15  import org.mule.tck.junit4.AbstractMuleContextTestCase;
16  
17  import java.util.Map;
18  
19  import org.junit.Test;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.Node;
23  import org.w3c.dom.NodeList;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNull;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  public class XPathAnnotatedEntryPointResolverTestCase extends AbstractMuleContextTestCase
31  {
32  
33      public static final String TEST_PAYLOAD = "<foo><bar>4</bar><bar>8</bar></foo>";
34  
35      @Test
36      public void testAnnotatedMethod() throws Exception
37      {
38          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
39          AnnotatedComponent component = new AnnotatedComponent();
40          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
41          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
42          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff", PropertyScope.INVOCATION);
43          InvocationResult result = resolver.invoke(component, context);
44          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
45          assertTrue(result.getResult() instanceof Map);
46          Map<?, ?> map = (Map<?, ?>) result.getResult();
47          assertEquals(3, map.size());
48          assertTrue(map.get("foo") instanceof Element);
49          assertTrue((Boolean) map.get("isBarValue"));
50          assertEquals("4", map.get("bar"));
51      }
52  
53      @Test
54      public void testAnnotatedMethod2() throws Exception
55      {
56          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
57          AnnotatedComponent component = new AnnotatedComponent();
58          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
59          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
60          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff2", PropertyScope.INVOCATION);
61          InvocationResult result = resolver.invoke(component, context);
62          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
63          assertTrue(result.getResult() instanceof Map);
64          Map<?, ?> map = (Map<?, ?>) result.getResult();
65          assertEquals(3, map.size());
66          assertTrue(map.get("foo") instanceof Document);
67          assertTrue((Boolean) map.get("isBarValue"));
68          assertEquals(new Double(8), map.get("bar"));
69      }
70  
71      @Test
72      public void testAnnotatedMethod3() throws Exception
73      {
74          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
75          AnnotatedComponent component = new AnnotatedComponent();
76          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
77          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
78          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff3", PropertyScope.INVOCATION);
79          InvocationResult result = resolver.invoke(component, context);
80          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
81          assertTrue(result.getResult() instanceof Map);
82          Map<?, ?> map = (Map<?, ?>) result.getResult();
83          assertEquals(2, map.size());
84          assertTrue(map.get("foo") instanceof Node);
85          assertTrue(map.get("bar") instanceof NodeList);
86          assertEquals(2, ((NodeList) map.get("bar")).getLength());
87      }
88  
89      @Test(expected = RequiredValueException.class)
90      public void testAnnotatedMethodRequiredMissing() throws Exception
91      {
92          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
93          AnnotatedComponent component = new AnnotatedComponent();
94          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
95          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
96          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff4", PropertyScope.INVOCATION);
97  
98          resolver.invoke(component, context);
99      }
100 
101     @Test
102     public void testAnnotatedMethodMissingNotRequired() throws Exception
103     {
104         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
105         AnnotatedComponent component = new AnnotatedComponent();
106         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
107         //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
108         context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff5", PropertyScope.INVOCATION);
109         InvocationResult result = resolver.invoke(component, context);
110         assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
111         assertTrue(result.getResult() instanceof Map);
112         Map<?, ?> map = (Map<?, ?>) result.getResult();
113         assertEquals(1, map.size());
114         assertNull(map.get("foo"));
115     }
116 
117     @Test(expected = IllegalArgumentException.class)
118     public void testIllegalAnnotatedMethod() throws Exception
119     {
120         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
121         IllegalAnnotatedComponent component = new IllegalAnnotatedComponent();
122         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
123 
124         resolver.invoke(component, context);
125     }
126 }