1
2
3
4
5
6
7
8
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
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
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
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
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
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
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
133 }
134 }
135 }