1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
78
79
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
101 prefixes = merge("<element/>");
102 assertTrue(prefixes.size() == 5);
103
104
105 prefixes = merge("<element>garbage</element>");
106 assertTrue(prefixes.size() == 5);
107
108
109 prefixes = merge("<element xmlns:soap=\"12345\">garbage</element>");
110 assertTrue(prefixes.size() == 5);
111 assertEquals("12345", prefixes.get("soap"));
112
113
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
119
120 prefixes = merge("<element xmlns:internal=\"uri\">garbage</element>");
121 assertTrue(prefixes.size() == 5);
122 assertEquals("uri", prefixes.get("internal"));
123
124
125 assertEquals("body-ns3", prefixes.get("ns3"));
126 }
127 }