View Javadoc

1   /*
2    * $Id: ServletTestCase.java 22477 2011-07-20 15:27:35Z justin.calleja $
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.cxf;
12  
13  import static org.junit.Assert.assertTrue;
14  
15  import java.util.Arrays;
16  import java.util.Collection;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.junit.Rule;
21  import org.junit.Test;
22  import org.junit.runners.Parameterized.Parameters;
23  import org.mule.api.MuleMessage;
24  import org.mule.module.client.MuleClient;
25  import org.mule.tck.AbstractServiceAndFlowTestCase;
26  import org.mule.tck.junit4.rule.DynamicPort;
27  import org.mule.transport.http.HttpConnector;
28  import org.mule.transport.servlet.MuleReceiverServlet;
29  import org.mule.transport.servlet.jetty.util.EmbeddedJettyServer;
30  
31  public class ServletTestCase extends AbstractServiceAndFlowTestCase
32  {
33  
34      public int HTTP_PORT = -1;
35  
36      private EmbeddedJettyServer httpServer;
37  
38      @Rule
39      public DynamicPort dynamicPort = new DynamicPort("port1");
40  
41      public ServletTestCase(ConfigVariant variant, String configResources)
42      {
43          super(variant, configResources);
44      }
45  
46      @Parameters
47      public static Collection<Object[]> parameters()
48      {
49          return Arrays.asList(new Object[][]{
50              {ConfigVariant.SERVICE, "servlet-conf-service.xml"},
51              {ConfigVariant.FLOW, "servlet-conf-flow.xml"}
52          });
53      }
54  
55      @Override
56      protected void doSetUp() throws Exception
57      {
58          super.doSetUp();
59          HTTP_PORT = dynamicPort.getNumber();
60          httpServer = new EmbeddedJettyServer(HTTP_PORT, getContextPath(), "/services/*",
61              new MuleReceiverServlet(), muleContext);
62          httpServer.start();
63      }
64  
65      protected String getContextPath()
66      {
67          return "";
68      }
69  
70      @Override
71      protected void doTearDown() throws Exception
72      {
73          if (httpServer != null && httpServer.isStarted())
74          {
75              httpServer.stop();
76          }
77  
78          super.doTearDown();
79      }
80  
81      @Test
82      public void testRequestWsdlWithServlets() throws Exception
83      {
84          String request = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
85                           + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
86                           + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soap:Body>"
87                           + "<ns1:echo xmlns:ns1=\"http://testmodels.cxf.module.mule.org/\">"
88                           + "<text>Test String</text>" + "</ns1:echo>" + "</soap:Body>" + "</soap:Envelope>";
89  
90          MuleClient client = new MuleClient(muleContext);
91          MuleMessage result = client.send("http://localhost:" + HTTP_PORT + getContextPath()
92                                           + "/services/mycomponent", request, null);
93          String res = result.getPayloadAsString();
94  
95          assertTrue(res.indexOf("Test String") != -1);
96      }
97  
98      @Test
99      public void testHttpGet() throws Exception
100     {
101         MuleClient client = new MuleClient(muleContext);
102         Map<String, Object> props = new HashMap<String, Object>();
103         props.put(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
104         MuleMessage result = client.send("http://localhost:" + HTTP_PORT + getContextPath()
105                                          + "/services/mycomponent/echo/text/Test String", "", props);
106         String res = result.getPayloadAsString();
107         assertTrue(res.indexOf("Test String") != -1);
108     }
109 
110 }