View Javadoc

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