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