1   /*
2    * $Id: WsdlCallTestCase.java 12304 2008-07-11 20:08:47Z dandiep $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transport.cxf;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.FunctionalTestCase;
16  import org.mule.transport.http.HttpConnector;
17  import org.mule.transport.http.HttpConstants;
18  import org.mule.transport.servlet.MuleReceiverServlet;
19  
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.dom4j.Document;
25  import org.dom4j.DocumentHelper;
26  import org.dom4j.Element;
27  import org.mortbay.jetty.Server;
28  import org.mortbay.jetty.nio.SelectChannelConnector;
29  import org.mortbay.jetty.servlet.ServletHandler;
30  
31  public class WsdlCallTestCase extends FunctionalTestCase
32  {
33      public static final int HTTP_PORT = 63088;
34  
35      private Server httpServer;
36  
37      @Override
38      protected void doSetUp() throws Exception
39      {
40          super.doSetUp();
41          httpServer = new Server();
42          SelectChannelConnector conn = new SelectChannelConnector();
43          conn.setPort(HTTP_PORT);
44          httpServer.addConnector(conn);
45  
46          ServletHandler handler = new ServletHandler();
47          handler.addServletWithMapping(MuleReceiverServlet.class, "/services/*");
48          
49          httpServer.addHandler(handler);
50          
51          httpServer.start();
52      }
53  
54      // @Override
55      protected void doTearDown() throws Exception
56      {
57          super.doTearDown();
58          if (httpServer != null && httpServer.isStarted())
59          {
60              httpServer.stop();
61          }
62      }
63  
64      public void xtestRequestWsdlWithServlets() throws Exception
65      {
66          Map<String, String> props = new HashMap<String, String>();
67          props.put(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
68          MuleClient client = new MuleClient();
69          MuleMessage result = client.send("http://localhost:" + HTTP_PORT + "/services/mycomponent?wsdl", null,
70              props);
71  
72          assertNotNull(result);
73          if (logger.isDebugEnabled())
74          {
75              logger.debug(result.getPayloadAsString());
76          }
77  
78          String location = "http://localhost:" + HTTP_PORT + "/services/mycomponent?wsdl";
79          location = location.substring(0, location.length() - 5);
80  
81          assertTrue(result.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, "").startsWith("text/xml"));
82  
83          if (logger.isDebugEnabled())
84          {
85              logger.debug(result.getPayloadAsString());
86          }
87  
88          Document document = DocumentHelper.parseText(result.getPayloadAsString());
89          List<?> nodes = document.selectNodes("//wsdl:definitions/wsdl:service");
90          assertEquals(((Element) nodes.get(0)).attribute("name").getStringValue(), "mycomponent");
91      }
92  
93      public void testRequestWsdlWithHttp() throws Exception
94      {
95          MuleClient client = new MuleClient();
96          Map<String, String> props = new HashMap<String, String>();
97          props.put("http.method", "GET");
98          MuleMessage reply = client.send("http://localhost:63082/cxfService?wsdl", null, props);
99  
100         assertNotNull(reply);
101         assertNotNull(reply.getPayload());
102 
103         Document document = DocumentHelper.parseText(reply.getPayloadAsString());
104         List<?> nodes = document.selectNodes("//wsdl:definitions/wsdl:service");
105         assertEquals(((Element) nodes.get(0)).attribute("name").getStringValue(), "TestServiceComponent");
106     }
107 
108     protected String getConfigResources()
109     {
110         return "wsdl-conf.xml";
111     }
112 }