View Javadoc

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