View Javadoc

1   /*
2    * $Id: JsonPathAnnotatedEntryPointResolverTestCase.java 20383 2010-11-29 19:44:13Z dfeist $
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.module.json.config;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.expression.RequiredValueException;
16  import org.mule.api.model.InvocationResult;
17  import org.mule.api.transformer.TransformerException;
18  import org.mule.api.transport.PropertyScope;
19  import org.mule.impl.model.resolvers.AnnotatedEntryPointResolver;
20  import org.mule.tck.AbstractMuleTestCase;
21  
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.codehaus.jackson.JsonNode;
26  
27  public class JsonPathAnnotatedEntryPointResolverTestCase extends AbstractMuleTestCase
28  {
29      public static final String TEST_PAYLOAD = "{\"foo\" : {\"bar\" : [4, 8] }}";
30  
31      @Override
32      protected void doSetUp() throws Exception
33      {
34          muleContext.getRegistry().registerObject("primitives" , new PrimitveTransformers());
35      }
36  
37      public void testAnnotatedMethod() throws Exception
38      {
39          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
40          AnnotatedComponent component = new AnnotatedComponent();
41          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
42          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
43          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff", PropertyScope.INVOCATION);
44          InvocationResult result = resolver.invoke(component, context);
45          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
46          assertTrue(result.getResult() instanceof Map);
47          Map<?, ?> map = (Map<?, ?>)result.getResult();
48          assertEquals(3, map.size());
49          assertTrue(map.get("foo") instanceof JsonNode);
50          assertTrue((Boolean)map.get("isBarValue"));
51          assertEquals("4", map.get("bar"));
52      }
53  
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 JsonNode);
67          assertTrue((Boolean)map.get("isBarValue"));
68          assertEquals(new Double(8), map.get("bar"));
69      }
70  
71      public void testAnnotatedMethod3() throws Exception
72      {
73          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
74          AnnotatedComponent component = new AnnotatedComponent();
75          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
76          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
77          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff3", PropertyScope.INVOCATION);
78          InvocationResult result = resolver.invoke(component, context);
79          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
80          assertTrue(result.getResult() instanceof Map);
81          Map<?, ?> map = (Map<?, ?>)result.getResult();
82          assertEquals(2, map.size());
83          assertTrue(map.get("foo") instanceof JsonNode);
84          assertTrue(map.get("bar") instanceof List);
85          assertEquals(2, ((List)map.get("bar")).size());
86      }
87  
88      public void testAnnotatedMethodRequiredMissing() throws Exception
89      {
90          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
91          AnnotatedComponent component = new AnnotatedComponent();
92          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
93          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
94          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff4", PropertyScope.INVOCATION);
95          try
96          {
97              resolver.invoke(component, context);
98              fail("The xpath expression returned null, nbut a value was required");
99          }
100         catch (RequiredValueException e)
101         {
102             //expected
103         }
104     }
105 
106     public void testAnnotatedMethodMissingNotRequired() throws Exception
107     {
108         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
109         AnnotatedComponent component = new AnnotatedComponent();
110         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
111         //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
112         context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff5", PropertyScope.INVOCATION);
113         InvocationResult result = resolver.invoke(component, context);
114         assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
115         assertTrue(result.getResult() instanceof Map);
116         Map<?, ?> map = (Map<?, ?>)result.getResult();
117         assertEquals(1, map.size());
118         assertNull(map.get("foo"));
119     }
120 
121     public void testIllegalAnnotatedMethod() throws Exception
122     {
123         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
124         IllegalAnnotatedComponent component = new IllegalAnnotatedComponent();
125         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
126         try
127         {
128             resolver.invoke(component, context);
129             fail("Annotated parameter has an illegal return type argument");
130         }
131         catch (TransformerException e)
132         {
133             //expected
134         }
135     }
136 }