View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.ws.cxf.payload;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.InputStream;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.xml.XMLConstants;
27  import javax.xml.parsers.DocumentBuilder;
28  import javax.xml.parsers.DocumentBuilderFactory;
29  
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Element;
34  import org.w3c.dom.NamedNodeMap;
35  import org.w3c.dom.Node;
36  
37  /**
38   * @author Dmytro Rud
39   */
40  public class InNamespaceMergeInterceptorTest {
41  
42      private static DocumentBuilder builder;    
43      
44      private static final String SOAP_STRING =
45          "<soap:Envelope " +
46                  "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" " + 
47                  "xmlns:urn=\"urn:hl7-org:v3\" " +
48                  "myattribute=\"12345\" " +
49                  "xmlns:ns3=\"urn:ihe\" " +
50                  "xmlns:xmlnsqq=\"urn:dummy:xmlnsns\" " +
51                  "xmlnsqq:kpss=\"abcd\"> " +
52              "<soap:Header/> " +
53              "<soap:Body xmlns:ns3=\"body-ns3\" xmlns:internal=\"internal\">garbage</soap:Body> " + 
54          "</soap:Envelope>";
55      
56      private static Document SOURCE;
57  
58      
59      @BeforeClass
60      public static void setUpClass() throws Exception {
61          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62          factory.setValidating(false);
63          factory.setNamespaceAware(true);
64          builder = factory.newDocumentBuilder();
65          SOURCE = parse(SOAP_STRING);
66      }
67      
68      /**
69       * Parses string into DOM document.
70       */
71      private static Document parse(String document) throws Exception {
72          InputStream stream = new ByteArrayInputStream(document.getBytes());
73          return builder.parse(stream);
74      }
75  
76      /**
77       * Merges prefixes of the globally defined source document with the ones
78       * declared in the given string XML payload.  
79       * Returns the prefix-to-uri map. 
80       */
81      private static Map<String, String> merge(String payload) throws Exception {
82          String target = InNamespaceMergeInterceptor.enrichNamespaces(SOURCE, payload);
83          Element element = parse(target).getDocumentElement();
84          Map<String, String> result = new HashMap<>();
85          NamedNodeMap attributes = element.getAttributes();
86          for (int i = 0; i < attributes.getLength(); ++i) {
87              Node attribute = attributes.item(i);
88              if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attribute.getNamespaceURI())) {
89                  result.put(attribute.getLocalName(), attribute.getTextContent());
90              }
91          }
92          return result;
93      }
94      
95      
96      @Test
97      public void testMergeNamespaces() throws Exception {
98          Map<String, String> prefixes;
99          
100         // empty target element, short form  
101         prefixes = merge("<element/>");
102         assertTrue(prefixes.size() == 5);
103 
104         // target element without NS declarations
105         prefixes = merge("<element>garbage</element>");
106         assertTrue(prefixes.size() == 5);
107         
108         // target element with redeclared "soap" prefix
109         prefixes = merge("<element xmlns:soap=\"12345\">garbage</element>");
110         assertTrue(prefixes.size() == 5);
111         assertEquals("12345", prefixes.get("soap"));
112 
113         // target element with declared prefix that begins with "xmlns"
114         prefixes = merge("<element xmlns:xmlns1=\"uri\" xmlns1:soap=\"12345\">garbage</element>");
115         assertTrue(prefixes.size() == 6);
116         assertEquals("http://www.w3.org/2003/05/soap-envelope", prefixes.get("soap"));
117 
118         // target element with redeclared prefix "internal" which has been  
119         // initially declared in the SOAP Body
120         prefixes = merge("<element xmlns:internal=\"uri\">garbage</element>");
121         assertTrue(prefixes.size() == 5);
122         assertEquals("uri", prefixes.get("internal"));
123         
124         // declaration from SOAP Body have higher priority than the ones from SOAP Envelope
125         assertEquals("body-ns3", prefixes.get("ns3"));
126     }
127 }