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