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.api.config.MuleProperties;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.transport.http.HttpConnector;
14  import org.mule.transport.http.HttpConstants;
15  import org.mule.transport.servlet.MuleReceiverServlet;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import javax.servlet.ServletContext;
21  
22  import org.mortbay.jetty.Server;
23  import org.mortbay.jetty.servlet.Context;
24  import org.mortbay.jetty.servlet.ServletHolder;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  public abstract class AbstractServletTestCase extends FunctionalTestCase
29  {
30  
31      //TODO(pablo.kraan): replace with a dynamic endpoint
32      public static final int HTTP_PORT = 63088;
33  
34      private Server httpServer;
35      private String context;
36  
37      public AbstractServletTestCase(String context)
38      {
39          super();
40          this.context = context;
41      }
42  
43      @Override
44      protected void doSetUp() throws Exception
45      {
46          super.doSetUp();
47  
48          httpServer = new Server(HTTP_PORT);
49  
50          Context root = new Context(httpServer,"/",Context.SESSIONS);
51          ServletHolder holder = new ServletHolder(MuleReceiverServlet.class);
52          root.addServlet(holder, context);
53  
54          ServletContext context = root.getServletContext();
55          context.setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
56          
57          httpServer.start();
58      }
59  
60      @Override
61      protected void doTearDown() throws Exception
62      {
63          super.doTearDown();
64          if (httpServer != null && httpServer.isStarted())
65          {
66              httpServer.stop();
67          }
68      }
69  
70      public void doTestBasic(String root) throws Exception
71      {
72          MuleClient client = new MuleClient(muleContext);
73  
74          MuleMessage result = client.send(root + "/helloworld", "", null);
75          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
76          assertEquals("Hello World", result.getPayloadAsString());
77  
78          result = client.send(root + "/hello", "", null);
79          assertEquals((Integer)404, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
80  
81          Map<String, String> props = new HashMap<String, String>();
82          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
83          result = client.send(root + "/helloworld", "", props);
84          assertEquals((Integer)405, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
85  
86          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_DELETE);
87          result = client.send(root + "/helloworld", "", props);
88          assertEquals("Hello World Delete", result.getPayloadAsString());
89          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
90      }
91  
92  }