View Javadoc

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