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.jersey;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.transport.http.HttpConnector;
13  import org.mule.transport.http.HttpConstants;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  public class BasicJerseyTestCase extends FunctionalTestCase
23  {
24      
25      @Override
26      protected String getConfigResources()
27      {
28          return "basic-conf.xml";
29      }
30  
31      @Test
32      public void testBasic() throws Exception
33      {
34          MuleClient client = new MuleClient(muleContext);
35  
36          MuleMessage result = client.send("http://localhost:63081/helloworld", "", null);
37          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
38          assertEquals("Hello World", result.getPayloadAsString());
39  
40          // try invalid url
41          result = client.send("http://localhost:63081/hello", "", null);
42          assertEquals((Integer)404, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
43  
44          Map<String, String> props = new HashMap<String, String>();
45          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
46          result = client.send("http://localhost:63081/helloworld", "", props);
47          assertEquals((Integer)405, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
48  
49          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_DELETE);
50          result = client.send("http://localhost:63081/helloworld", "", props);
51          assertEquals("Hello World Delete", result.getPayloadAsString());
52          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
53      }
54  
55      @Test
56      public void testParams() throws Exception
57      {
58          MuleClient client = new MuleClient(muleContext);
59  
60          Map<String, String> props = new HashMap<String, String>();
61          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
62          MuleMessage result = client.send("http://localhost:63081/helloworld/sayHelloWithUri/Dan", "", props);
63          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
64          assertEquals("Hello Dan", result.getPayloadAsString());
65  
66  
67          result = client.send("http://localhost:63081/helloworld/sayHelloWithJson/Dan", "", props);
68          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
69          assertEquals("{\"message\":\"Hello Dan\"}", result.getPayloadAsString());
70  
71          result = client.send("http://localhost:63081/helloworld/sayHelloWithQuery?name=Dan", "", props);
72          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
73          assertEquals("Hello Dan", result.getPayloadAsString());
74  
75          props.put("X-Name", "Dan");
76          result = client.send("http://localhost:63081/helloworld/sayHelloWithHeader", "", props);
77          assertEquals((Integer)201, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
78          assertEquals("Hello Dan", result.getPayloadAsString());
79          assertEquals("Dan", result.getInboundProperty("X-ResponseName"));
80      }
81  
82      @Test
83      public void testThrowException() throws Exception
84      {
85      	callThrowException(500, "");
86      }
87  
88      protected void callThrowException(Integer expectedErrorCode, String expectedData) throws Exception
89      {
90      	MuleClient client = new MuleClient(muleContext);
91  
92      	Map<String, String> props = new HashMap<String, String>();
93          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
94          MuleMessage result = client.send("http://localhost:63081/helloworld/throwException", "", props);
95          assertEquals(expectedErrorCode, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
96          assertEquals(expectedData, result.getPayloadAsString());
97      }
98  
99  }