View Javadoc

1   /*
2    * $Id: JsonPathAnnotatedEntryPointResolverTestCase.java 18562 2010-07-26 00:33:32Z rossmason $
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.json.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.transformer.TransformerException;
17  import org.mule.api.transport.PropertyScope;
18  import org.mule.impl.model.resolvers.AnnotatedEntryPointResolver;
19  import org.mule.tck.AbstractMuleTestCase;
20  
21  import java.util.Map;
22  
23  import org.codehaus.jackson.JsonNode;
24  import org.codehaus.jackson.node.ArrayNode;
25  
26  public class JsonPathAnnotatedEntryPointResolverTestCase extends AbstractMuleTestCase
27  {
28      public static final String TEST_PAYLOAD = "{\"foo\" : {\"bar\" : [4, 8] }}";
29  
30      @Override
31      protected void doSetUp() throws Exception
32      {
33          muleContext.getRegistry().registerObject("primitives" , new PrimitveTransformers());
34      }
35  
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 JsonNode);
49          assertTrue((Boolean)map.get("isBarValue"));
50          assertEquals("4", map.get("bar"));
51      }
52  
53      public void testAnnotatedMethod2() throws Exception
54      {
55          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
56          AnnotatedComponent component = new AnnotatedComponent();
57          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
58          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
59          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff2", PropertyScope.INVOCATION);
60          InvocationResult result = resolver.invoke(component, context);
61          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
62          assertTrue(result.getResult() instanceof Map);
63          Map<?, ?> map = (Map<?, ?>)result.getResult();
64          assertEquals(3, map.size());
65          assertTrue(map.get("foo") instanceof JsonNode);
66          assertTrue((Boolean)map.get("isBarValue"));
67          assertEquals(new Double(8), map.get("bar"));
68      }
69  
70      public void testAnnotatedMethod3() throws Exception
71      {
72          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
73          AnnotatedComponent component = new AnnotatedComponent();
74          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
75          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
76          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff3", PropertyScope.INVOCATION);
77          InvocationResult result = resolver.invoke(component, context);
78          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
79          assertTrue(result.getResult() instanceof Map);
80          Map<?, ?> map = (Map<?, ?>)result.getResult();
81          assertEquals(2, map.size());
82          assertTrue(map.get("foo") instanceof JsonNode);
83          assertTrue(map.get("bar") instanceof ArrayNode);
84          assertEquals(2, ((ArrayNode)map.get("bar")).size());
85      }
86  
87      public void testAnnotatedMethodRequiredMissing() throws Exception
88      {
89          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
90          AnnotatedComponent component = new AnnotatedComponent();
91          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
92          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
93          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff4", PropertyScope.INVOCATION);
94          try
95          {
96              resolver.invoke(component, context);
97              fail("The xpath expression returned null, nbut a value was required");
98          }
99          catch (RequiredValueException e)
100         {
101             //expected
102         }
103     }
104 
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     public void testIllegalAnnotatedMethod() throws Exception
121     {
122         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
123         IllegalAnnotatedComponent component = new IllegalAnnotatedComponent();
124         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
125         try
126         {
127             resolver.invoke(component, context);
128             fail("Annotated parameter has an illegal return type argument");
129         }
130         catch (TransformerException e)
131         {
132             //expected
133         }
134     }
135 }