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.api.endpoint.InboundEndpoint;
11  import org.mule.config.i18n.LocaleMessageHandler;
12  import org.mule.module.client.MuleClient;
13  import org.mule.module.xml.util.XMLUtils;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  import org.mule.tck.junit4.rule.DynamicPort;
16  import org.mule.transport.http.HttpConstants;
17  import org.mule.util.IOUtils;
18  
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  import javax.xml.transform.TransformerFactoryConfigurationError;
25  
26  import org.custommonkey.xmlunit.XMLUnit;
27  import org.junit.Rule;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.assertTrue;
33  
34  public class CxfBasicTestCase extends FunctionalTestCase
35  {
36      private String echoWsdl;
37  
38      @Rule
39      public DynamicPort dynamicPort = new DynamicPort("port1");
40  
41      @Override
42      protected String getConfigResources()
43      {
44          return "basic-conf.xml";
45      }
46  
47      @Override
48      protected void doSetUp() throws Exception
49      {
50          super.doSetUp();
51          echoWsdl = IOUtils.getResourceAsString("cxf-echo-service.wsdl", getClass());
52          XMLUnit.setIgnoreWhitespace(true);
53          try
54          {
55              XMLUnit.getTransformerFactory();
56          }
57          catch (TransformerFactoryConfigurationError e)
58          {
59              XMLUnit.setTransformerFactory(XMLUtils.TRANSFORMER_FACTORY_JDK5);
60          }
61      }
62  
63      @Test
64      public void testEchoService() throws Exception
65      {
66          MuleClient client = new MuleClient(muleContext);
67          Map<String, Object> props = new HashMap<String, Object>();
68          props.put("Content-Type", "application/soap+xml");
69          InputStream xml = getClass().getResourceAsStream("/direct/direct-request.xml");
70          MuleMessage result = client.send(((InboundEndpoint) client.getMuleContext()
71              .getRegistry()
72              .lookupObject("httpInbound")).getAddress(), xml, props);
73          assertTrue(result.getPayloadAsString().contains("Hello!"));
74          String ct = result.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, "");
75          assertEquals("text/xml; charset=UTF-8", ct);
76      }
77  
78      @Test
79      public void testEchoServiceEncoding() throws Exception
80      {
81          MuleClient client = new MuleClient(muleContext);
82          String message = LocaleMessageHandler.getString("test-data",
83              Locale.JAPAN, "CxfBasicTestCase.testEchoServiceEncoding", new Object[]{});
84          MuleMessage result = client.send("cxf:" + ((InboundEndpoint) client.getMuleContext()
85                          .getRegistry()
86                          .lookupObject("httpInbound")).getAddress() + "?method=echo", message, null);
87          String ct = result.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, "");
88  
89          assertEquals(message, result.getPayload());
90          assertEquals("text/xml; charset=UTF-8", ct);
91      }
92  
93      @Test
94      public void testEchoWsdl() throws Exception
95      {
96          MuleClient client = new MuleClient(muleContext);
97          MuleMessage result = client.request(((InboundEndpoint) client.getMuleContext()
98                          .getRegistry()
99                          .lookupObject("httpInbound")).getAddress() + "?wsdl", 5000);
100         assertNotNull(result.getPayload());
101         XMLUnit.compareXML(echoWsdl, result.getPayloadAsString());
102     }
103 
104 }