View Javadoc

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