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.issues;
8   
9   import org.mule.tck.junit4.FunctionalTestCase;
10  import org.mule.tck.junit4.rule.DynamicPort;
11  import org.mule.util.IOUtils;
12  import org.mule.util.SystemUtils;
13  
14  import java.io.IOException;
15  import java.io.StringReader;
16  import java.net.URL;
17  
18  import javax.xml.parsers.DocumentBuilder;
19  import javax.xml.parsers.DocumentBuilderFactory;
20  import javax.xml.parsers.ParserConfigurationException;
21  
22  import org.custommonkey.xmlunit.XMLUnit;
23  import org.junit.Rule;
24  import org.junit.Test;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  import org.xml.sax.InputSource;
28  import org.xml.sax.SAXException;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.fail;
33  
34  public class ProxyServiceServingWsdlMule4092TestCase extends FunctionalTestCase
35  {
36      private String expectedWsdlFileName;
37  
38      @Rule
39      public DynamicPort dynamicPort = new DynamicPort("port1");
40  
41      @Override
42      protected String getConfigResources()
43      {
44          return "issues/proxy-service-serving-wsdl-mule4092.xml";
45      }
46  
47      @Override
48      protected void doSetUp() throws Exception
49      {
50          super.doSetUp();
51          XMLUnit.setIgnoreWhitespace(true);
52          setupExpectedWsdlFileName();
53      }
54  
55      /**
56       * The WSDL generated by CXF is basically the same but slightly differs in whitespace and
57       * element ordering (which does not matter). XMLUnit's javadoc says it can ignore element
58       * ordering but obviously that does not work, hence this hack.
59       */
60      private void setupExpectedWsdlFileName()
61      {
62          if (SystemUtils.isSunJDK() || SystemUtils.isAppleJDK())
63          {
64              expectedWsdlFileName = "test.wsdl";
65          }
66          else if (SystemUtils.isIbmJDK())
67          {
68              if (SystemUtils.isJavaVersionAtLeast(160))
69              {
70                  expectedWsdlFileName = "test.wsdl.ibmjdk-6";
71              }
72              else
73              {
74                  expectedWsdlFileName = "test.wsdl.ibmjdk-5";
75              }
76          }
77          else
78          {
79              fail("Unknown JDK");
80          }
81      }
82  
83      @Test
84      public void testProxyServiceWSDL() throws Exception
85      {
86          String expected = getXML("issues/" + expectedWsdlFileName);
87  
88          URL url = new URL("http://localhost:" + dynamicPort.getNumber() + "/services/onlinestore?wsdl");
89          String wsdlFromService = IOUtils.toString(url.openStream());
90  
91          // The exact string representation may differ, so we'll spot check the WSDL contents
92          //assertTrue(compareResults(expected, wsdlFromService));
93          Document expectedDom = buildDOM(expected);
94          Document actualDom = buildDOM(wsdlFromService);
95  
96          // Check that it's WSDL
97          Element topElement = expectedDom.getDocumentElement();
98          String wsdlNamespace = topElement.getNamespaceURI();
99          assertEquals(wsdlNamespace, actualDom.getDocumentElement().getNamespaceURI());
100         assertEquals(topElement.getLocalName(), actualDom.getDocumentElement().getLocalName());
101 
102         Element expectedService = (Element) expectedDom.getElementsByTagNameNS(wsdlNamespace, "service").item(0);
103         Element actualService = (Element) actualDom.getElementsByTagNameNS(wsdlNamespace, "service").item(0);
104         assertNotNull(actualService);
105         assertEquals(expectedService.getAttribute("name"), actualService.getAttribute("name"));
106 
107         Element expectedPort = (Element) expectedDom.getElementsByTagNameNS(wsdlNamespace, "port").item(0);
108         Element actualPort = (Element) actualDom.getElementsByTagNameNS(wsdlNamespace, "port").item(0);
109         assertNotNull(actualPort);
110         assertEquals(expectedPort.getAttribute("name"), actualPort.getAttribute("name"));
111 
112         int expectedNumberOfMessages =  expectedDom.getElementsByTagNameNS(wsdlNamespace, "message").getLength();
113         int actualNumberOfmMessages =  actualDom.getElementsByTagNameNS(wsdlNamespace, "message").getLength();
114         assertEquals(expectedNumberOfMessages, actualNumberOfmMessages);
115     }
116 
117     protected String getXML(String requestFile) throws Exception
118     {
119         String xml = IOUtils.toString(IOUtils.getResourceAsStream(requestFile, this.getClass()), "UTF-8");
120         if (xml != null)
121         {
122             return xml;
123         }
124         else
125         {
126             fail("Unable to load test request file");
127             return null;
128         }
129     }
130 
131     private Document buildDOM(String xmlString) throws ParserConfigurationException, IOException, SAXException
132     {
133         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
134         factory.setNamespaceAware(true);
135         DocumentBuilder builder = factory.newDocumentBuilder();
136         InputSource source = new InputSource(new StringReader(xmlString));
137         return builder.parse(source);
138     }
139 
140 }