View Javadoc

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