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