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