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.jaxws;
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.NullPayload;
14  
15  import org.apache.commons.httpclient.HttpClient;
16  import org.apache.commons.httpclient.HttpMethod;
17  import org.apache.commons.httpclient.methods.GetMethod;
18  import org.junit.Rule;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  public class CxfJaxWsTestCase extends FunctionalTestCase
24  {
25  
26      @Rule
27      public DynamicPort dynamicPort = new DynamicPort("port1");
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "jaxws-conf.xml";
33      }
34  
35      @Test
36      public void testEchoService() throws Exception
37      {
38          String url = "cxf:http://localhost:" + dynamicPort.getNumber() + "/services/Echo?method=echo";
39  
40          MuleClient client = new MuleClient(muleContext);
41          MuleMessage result = client.send(url, "Hello!", null);
42          assertEquals("Hello!", result.getPayload());
43      }
44  
45      @Test
46      public void testOneWay() throws Exception
47      {
48          String url = "cxf:http://localhost:" + dynamicPort.getNumber() + "/services/async?method=send";
49  
50          MuleClient client = new MuleClient(muleContext);
51          MuleMessage result = client.send(url, "Hello!", null);
52          assertEquals(NullPayload.getInstance(), result.getPayload());
53      }
54  
55      @Test
56      public void testHttpCall() throws Exception
57      {
58          HttpClient client =  new HttpClient();
59          // The format in which CXF processes the request in Http GET is:
60          // http://host/service/OPERATION/PARAM_NAME/PARAM_VALUE
61          // In this case: http://localhost:63081/Echo/echo/text/hello
62          // (service: Echo corresponds to the name in the mule config file: TC-HTTP-CALL.xml)
63          HttpMethod httpMethod = new GetMethod("http://localhost:" + dynamicPort.getNumber() + "/services/Echo/echo/text/hello");
64          // Http Status Code 200 means OK, the request has succeeded. (500 would indicate an error)
65          assertEquals(200, client.executeMethod(httpMethod));
66          // By default the class package - in its other way round - is used for the namespace:
67          // Here, EchoServiceImpl classpath is: com\mulesoft\test\connectors\module\cxf
68          // Therefore namespace should be: http://cxf.transport.connectors.test.mulesoft.com
69          assertEquals(
70                  "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
71                      "<soap:Body>" +
72                          "<ns2:echoResponse xmlns:ns2=\"http://testmodels.cxf.module.mule.org/\">" +
73                              "<text>hello</text>" +
74                          "</ns2:echoResponse>" +
75                      "</soap:Body>" +
76                  "</soap:Envelope>", httpMethod.getResponseBodyAsString());
77      }
78  
79      @Test
80      public void testWebServiceContext() throws Exception
81      {
82          String url = "cxf:http://localhost:" + dynamicPort.getNumber() + "/services/Echo?method=ensureWebSerivceContextIsSet";
83  
84          MuleClient client = new MuleClient(muleContext);
85          MuleMessage result = client.send(url, TEST_MESSAGE, null);
86          assertEquals(TEST_MESSAGE, result.getPayload());
87      }
88  }