1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.jersey;
12
13 import static org.junit.Assert.assertEquals;
14
15 import org.mule.api.MuleMessage;
16 import org.mule.module.client.MuleClient;
17 import org.mule.tck.AbstractServiceAndFlowTestCase;
18 import org.mule.transport.http.HttpConnector;
19 import org.mule.transport.http.HttpConstants;
20
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.junit.Test;
27 import org.junit.runners.Parameterized.Parameters;
28
29 public class BasicJerseyTestCase extends AbstractServiceAndFlowTestCase
30 {
31 public BasicJerseyTestCase(ConfigVariant variant, String configResources)
32 {
33 super(variant, configResources);
34 }
35
36 @Parameters
37 public static Collection<Object[]> parameters()
38 {
39 return Arrays.asList(new Object[][]{
40 {ConfigVariant.SERVICE, "basic-conf.xml"},
41 });
42 }
43
44 @Test
45 public void testBasic() throws Exception
46 {
47 MuleClient client = new MuleClient(muleContext);
48
49 MuleMessage result = client.send("http://localhost:63081/helloworld", "", null);
50 assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
51 assertEquals("Hello World", result.getPayloadAsString());
52
53
54 result = client.send("http://localhost:63081/hello", "", null);
55 assertEquals((Integer)404, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
56
57 Map<String, String> props = new HashMap<String, String>();
58 props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
59 result = client.send("http://localhost:63081/helloworld", "", props);
60 assertEquals((Integer)405, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
61
62 props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_DELETE);
63 result = client.send("http://localhost:63081/helloworld", "", props);
64 assertEquals("Hello World Delete", result.getPayloadAsString());
65 assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
66 }
67
68 @Test
69 public void testParams() throws Exception
70 {
71 MuleClient client = new MuleClient(muleContext);
72
73 Map<String, String> props = new HashMap<String, String>();
74 props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
75 MuleMessage result = client.send("http://localhost:63081/helloworld/sayHelloWithUri/Dan", "", props);
76 assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
77 assertEquals("Hello Dan", result.getPayloadAsString());
78
79
80 result = client.send("http://localhost:63081/helloworld/sayHelloWithJson/Dan", "", props);
81 assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
82 assertEquals("{\"message\":\"Hello Dan\"}", result.getPayloadAsString());
83
84 result = client.send("http://localhost:63081/helloworld/sayHelloWithQuery?name=Dan", "", props);
85 assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
86 assertEquals("Hello Dan", result.getPayloadAsString());
87
88 props.put("X-Name", "Dan");
89 result = client.send("http://localhost:63081/helloworld/sayHelloWithHeader", "", props);
90 assertEquals((Integer)201, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
91 assertEquals("Hello Dan", result.getPayloadAsString());
92 assertEquals("Dan", result.getInboundProperty("X-ResponseName"));
93 }
94
95 @Test
96 public void testThrowException() throws Exception
97 {
98 callThrowException(500, "Failed to invoke JerseyResourcesComponent{helloWorldResource.commponent}. Component that caused exception is: JerseyResourcesComponent{helloWorldResource.commponent}. Message payload is of type: String");
99 }
100
101 protected void callThrowException(Integer expectedErrorCode, String expectedData) throws Exception
102 {
103 MuleClient client = new MuleClient(muleContext);
104
105 Map<String, String> props = new HashMap<String, String>();
106 props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
107 MuleMessage result = client.send("http://localhost:63081/helloworld/throwException", "", props);
108 assertEquals(expectedErrorCode, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
109 assertEquals(expectedData, result.getPayloadAsString());
110 }
111
112 }