1   /*
2    * $Id: XFireWsdlCallTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.providers.soap.xfire;
12  
13  import org.mule.extras.client.MuleClient;
14  import org.mule.providers.http.HttpConnector;
15  import org.mule.providers.http.HttpConstants;
16  import org.mule.providers.http.servlet.MuleReceiverServlet;
17  import org.mule.tck.FunctionalTestCase;
18  import org.mule.umo.UMOMessage;
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.http.HttpContext;
28  import org.mortbay.http.SocketListener;
29  import org.mortbay.jetty.Server;
30  import org.mortbay.jetty.servlet.ServletHandler;
31  import org.mortbay.util.InetAddrPort;
32  
33  public class XFireWsdlCallTestCase extends FunctionalTestCase
34  {
35      public static final int HTTP_PORT = 63088;
36  
37      private Server httpServer;
38  
39      // @Override
40      protected void doPreFunctionalSetUp() throws Exception
41      {
42          super.doPreFunctionalSetUp();
43          httpServer = new Server();
44          SocketListener socketListener = new SocketListener(new InetAddrPort(HTTP_PORT));
45          httpServer.addListener(socketListener);
46  
47          HttpContext context = httpServer.getContext("/");
48          context.setRequestLog(null);
49  
50          ServletHandler handler = new ServletHandler();
51          handler.addServlet("MuleReceiverServlet", "/services/*", MuleReceiverServlet.class
52              .getName());
53  
54          context.addHandler(handler);
55          httpServer.start();
56      }
57  
58      // @Override
59      protected void doFunctionalTearDown() throws Exception
60      {
61          super.doFunctionalTearDown();
62          if (httpServer != null && httpServer.isStarted())
63          {
64              httpServer.stop();
65          }
66      }
67  
68      public void testRequestWsdlWithServlets() throws Exception
69      {
70          Map props = new HashMap();
71          props.put(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
72          MuleClient client = new MuleClient();
73          UMOMessage result = client.send("http://localhost:" + HTTP_PORT
74                                          + "/services/mycomponent?wsdl", null, props);
75  
76          assertNotNull(result);
77          if (logger.isDebugEnabled())
78          {
79              logger.debug(result.getPayloadAsString());
80          }
81  
82          String location = "http://localhost:" + HTTP_PORT + "/services/mycomponent?wsdl";
83          location = location.substring(0, location.length() - 5);
84  
85          assertTrue(result.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, "").startsWith(
86              "text/xml"));
87  
88          if (logger.isDebugEnabled())
89          {
90              logger.debug(result.getPayloadAsString());
91          }
92  
93          Document document = DocumentHelper.parseText(result.getPayloadAsString());
94          List nodes;
95  
96          nodes = document.selectNodes("//wsdl:definitions/wsdl:service");
97          assertEquals(((Element)nodes.get(0)).attribute("name").getStringValue(), "mycomponent");
98      }
99  
100     public void testRequestWsdlWithHttp() throws Exception
101     {
102         MuleClient client = new MuleClient();
103         Map props = new HashMap();
104         props.put("http.method", "GET");
105         UMOMessage reply = client.send("http://localhost:63082/xfireService?wsdl", null, props);
106 
107         assertNotNull(reply);
108         assertNotNull(reply.getPayload());
109 
110         Document document = DocumentHelper.parseText(reply.getPayloadAsString());
111         List nodes;
112         nodes = document.selectNodes("//wsdl:definitions/wsdl:service");
113         assertEquals(((Element)nodes.get(0)).attribute("name").getStringValue(), "xfireService");
114     }
115 
116     protected String getConfigResources()
117     {
118         return "xfire-wsdl-conf.xml";
119     }
120 }