Improve JAXP implementation compatibility
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlStreamUtilsTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.data.codec.xml;
10
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.net.URI;
20 import java.util.AbstractMap;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import javax.xml.stream.XMLOutputFactory;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamWriter;
29 import org.custommonkey.xmlunit.Diff;
30 import org.custommonkey.xmlunit.XMLUnit;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.QNameModule;
37 import org.opendaylight.yangtools.yang.common.Revision;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
47 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
48 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
49 import org.w3c.dom.Document;
50
51 public class XmlStreamUtilsTest {
52     @FunctionalInterface
53     interface XMLStreamWriterConsumer {
54         void accept(XMLStreamWriter writer) throws XMLStreamException;
55     }
56
57     public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
58
59     private static SchemaContext schemaContext;
60     private static Module leafRefModule;
61
62     @BeforeClass
63     public static void initialize() {
64         schemaContext = YangParserTestUtils.parseYangResource("/leafref-test.yang");
65         assertNotNull(schemaContext);
66         assertEquals(1, schemaContext.getModules().size());
67         leafRefModule = schemaContext.getModules().iterator().next();
68         assertNotNull(leafRefModule);
69     }
70
71     @AfterClass
72     public static void cleanup() {
73         leafRefModule = null;
74         schemaContext = null;
75     }
76
77     @Test
78     public void testWriteAttribute() throws Exception {
79         final ByteArrayOutputStream out = new ByteArrayOutputStream();
80         final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
81         writer.writeStartElement("element");
82
83         QName name = getAttrQName("namespace", "2012-12-12", "attr", Optional.of("prefix"));
84         final Map.Entry<QName, String> attributeEntry = new AbstractMap.SimpleEntry<>(name, "value");
85
86         name = getAttrQName("namespace2", "2012-12-12", "attr", Optional.empty());
87         final Map.Entry<QName, String> attributeEntryNoPrefix = new AbstractMap.SimpleEntry<>(name, "value");
88
89         final RandomPrefix randomPrefix = new RandomPrefix(null);
90         XMLStreamWriterUtils.writeAttribute(writer, attributeEntry, randomPrefix);
91         XMLStreamWriterUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
92
93         writer.writeEndElement();
94         writer.close();
95         out.close();
96
97         final String xmlAsString = new String(out.toByteArray());
98
99         final Map<String, String> mappedPrefixes = mapPrefixed(randomPrefix.getPrefixes());
100         assertEquals(2, mappedPrefixes.size());
101         final String randomPrefixValue = mappedPrefixes.get("namespace2");
102
103         final String expectedXmlAsString = "<element xmlns:a=\"namespace\" a:attr=\"value\" xmlns:" + randomPrefixValue
104                 + "=\"namespace2\" " + randomPrefixValue + ":attr=\"value\"></element>";
105
106         XMLUnit.setIgnoreAttributeOrder(true);
107         final Document control = XMLUnit.buildControlDocument(expectedXmlAsString);
108         final Document test = XMLUnit.buildTestDocument(xmlAsString);
109         final Diff diff = XMLUnit.compareXML(control, test);
110
111         final boolean identical = diff.identical();
112         assertTrue("Xml differs: " + diff.toString(), identical);
113     }
114
115     @Test
116     public void testWriteIdentityRef() throws Exception {
117         final QNameModule parent = QNameModule.create(URI.create("parent:uri"), Revision.of("2000-01-01"));
118
119         String xmlAsString = createXml(writer -> {
120             writer.writeStartElement("element");
121             XMLStreamWriterUtils.write(writer, null, QName.create(parent, "identity"), parent);
122             writer.writeEndElement();
123         });
124
125         assertThat(xmlAsString, containsString("element>identity"));
126
127         xmlAsString = createXml(writer -> {
128             writer.writeStartElement("elementDifferent");
129             XMLStreamWriterUtils.write(writer, null, QName.create("different:namespace", "identity"), parent);
130             writer.writeEndElement();
131
132         });
133
134         final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*");
135         final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString);
136         assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches());
137     }
138
139     private static String createXml(XMLStreamWriterConsumer consumer) throws XMLStreamException, IOException {
140         final ByteArrayOutputStream out = new ByteArrayOutputStream();
141         final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
142
143         consumer.accept(writer);
144
145         writer.close();
146         out.close();
147
148         return new String(out.toByteArray()).replaceAll("\\s*", "");
149     }
150
151     /**
152      * One leafref reference to other leafref via relative references.
153      */
154     @Test
155     public void testLeafRefRelativeChaining() {
156         getTargetNodeForLeafRef("leafname3", StringTypeDefinition.class);
157     }
158
159     @Test
160     public void testLeafRefRelative() {
161         getTargetNodeForLeafRef("pointToStringLeaf", StringTypeDefinition.class);
162     }
163
164     @Test
165     public void testLeafRefAbsoluteWithSameTarget() {
166         getTargetNodeForLeafRef("absname", InstanceIdentifierTypeDefinition.class);
167     }
168
169     /**
170      * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
171      */
172     // ignored because this isn't implemented
173     @Ignore
174     @Test
175     public void testLeafRefWithDoublePointInPath() {
176         getTargetNodeForLeafRef("lf-with-double-point-inside", StringTypeDefinition.class);
177     }
178
179     @Test
180     public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
181         final TypeDefinition<?> targetNodeForAbsname = getTargetNodeForLeafRef("absname",
182             InstanceIdentifierTypeDefinition.class);
183         final TypeDefinition<?> targetNodeForRelname = getTargetNodeForLeafRef("relname",
184             InstanceIdentifierTypeDefinition.class);
185         assertEquals(targetNodeForAbsname, targetNodeForRelname);
186     }
187
188     private TypeDefinition<?> getTargetNodeForLeafRef(final String nodeName, final Class<?> clas) {
189         final LeafSchemaNode schemaNode = findSchemaNodeWithLeafrefType(leafRefModule, nodeName);
190         assertNotNull(schemaNode);
191         final LeafrefTypeDefinition leafrefTypedef = findLeafrefType(schemaNode);
192         assertNotNull(leafrefTypedef);
193         final TypeDefinition<?> targetBaseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypedef, schemaContext,
194                 schemaNode);
195         assertTrue("Wrong class found.", clas.isInstance(targetBaseType));
196         return targetBaseType;
197     }
198
199     private static Map<String, String> mapPrefixed(final Iterable<Map.Entry<URI, String>> prefixes) {
200         final Map<String, String> mappedPrefixes = new HashMap<>();
201         for (final Map.Entry<URI, String> prefix : prefixes) {
202             mappedPrefixes.put(prefix.getKey().toString(), prefix.getValue());
203         }
204         return mappedPrefixes;
205     }
206
207     private static QName getAttrQName(final String namespace, final String revision, final String localName,
208             final Optional<String> prefix) {
209         if (prefix.isPresent()) {
210             final QName moduleQName = QName.create(namespace, revision, "module");
211             final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision());
212             return QName.create(module, localName);
213         }
214         return QName.create(namespace, revision, localName);
215     }
216
217     private LeafSchemaNode findSchemaNodeWithLeafrefType(final DataNodeContainer module, final String nodeName) {
218         for (final DataSchemaNode childNode : module.getChildNodes()) {
219             if (childNode instanceof DataNodeContainer) {
220                 LeafSchemaNode leafrefFromRecursion = findSchemaNodeWithLeafrefType((DataNodeContainer) childNode,
221                         nodeName);
222                 if (leafrefFromRecursion != null) {
223                     return leafrefFromRecursion;
224                 }
225             } else if (childNode.getQName().getLocalName().equals(nodeName) && childNode instanceof LeafSchemaNode) {
226                 final TypeDefinition<?> leafSchemaNodeType = ((LeafSchemaNode) childNode).getType();
227                 if (leafSchemaNodeType instanceof LeafrefTypeDefinition) {
228                     return (LeafSchemaNode) childNode;
229                 }
230             }
231         }
232         return null;
233     }
234
235     private static LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) {
236         final TypeDefinition<?> type = schemaNode.getType();
237         if (type instanceof LeafrefTypeDefinition) {
238             return (LeafrefTypeDefinition) type;
239         }
240         return null;
241     }
242 }