2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.data.codec.xml;
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;
17 import java.io.ByteArrayOutputStream;
18 import java.io.FileNotFoundException;
20 import java.net.URISyntaxException;
21 import java.util.AbstractMap;
22 import java.util.HashMap;
24 import java.util.Optional;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import javax.xml.stream.XMLOutputFactory;
28 import javax.xml.stream.XMLStreamWriter;
29 import org.custommonkey.xmlunit.Diff;
30 import org.custommonkey.xmlunit.XMLUnit;
31 import org.junit.BeforeClass;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.QNameModule;
36 import org.opendaylight.yangtools.yang.common.Revision;
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.test.util.YangParserTestUtils;
49 import org.w3c.dom.Document;
51 public class XmlStreamUtilsTest {
53 public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
55 private static SchemaContext schemaContext;
56 private static Module leafRefModule;
59 public static void initialize() throws URISyntaxException, FileNotFoundException, ReactorException {
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);
68 public void testWriteAttribute() throws Exception {
69 final ByteArrayOutputStream out = new ByteArrayOutputStream();
70 final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
71 writer.writeStartElement("element");
73 QName name = getAttrQName("namespace", "2012-12-12", "attr", Optional.of("prefix"));
74 final Map.Entry<QName, String> attributeEntry = new AbstractMap.SimpleEntry<>(name, "value");
76 name = getAttrQName("namespace2", "2012-12-12", "attr", Optional.empty());
77 final Map.Entry<QName, String> attributeEntryNoPrefix = new AbstractMap.SimpleEntry<>(name, "value");
79 final RandomPrefix randomPrefix = new RandomPrefix(null);
80 XMLStreamWriterUtils.writeAttribute(writer, attributeEntry, randomPrefix);
81 XMLStreamWriterUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
83 writer.writeEndElement();
87 final String xmlAsString = new String(out.toByteArray());
89 final Map<String, String> mappedPrefixes = mapPrefixed(randomPrefix.getPrefixes());
90 assertEquals(2, mappedPrefixes.size());
91 final String randomPrefixValue = mappedPrefixes.get("namespace2");
93 final String expectedXmlAsString = "<element xmlns:a=\"namespace\" a:attr=\"value\" xmlns:" + randomPrefixValue
94 + "=\"namespace2\" " + randomPrefixValue + ":attr=\"value\"></element>";
96 XMLUnit.setIgnoreAttributeOrder(true);
97 final Document control = XMLUnit.buildControlDocument(expectedXmlAsString);
98 final Document test = XMLUnit.buildTestDocument(xmlAsString);
99 final Diff diff = XMLUnit.compareXML(control, test);
101 final boolean identical = diff.identical();
102 assertTrue("Xml differs: " + diff.toString(), identical);
106 public void testWriteIdentityRef() throws Exception {
107 final ByteArrayOutputStream out = new ByteArrayOutputStream();
108 final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
110 writer.writeStartElement("element");
111 final QNameModule parent = QNameModule.create(URI.create("parent:uri"), Revision.valueOf("2000-01-01"));
112 XMLStreamWriterUtils.write(writer, null, QName.create(parent, "identity"), parent);
113 writer.writeEndElement();
115 writer.writeStartElement("elementDifferent");
116 XMLStreamWriterUtils.write(writer, null, QName.create("different:namespace", "identity"), parent);
117 writer.writeEndElement();
122 final String xmlAsString = new String(out.toByteArray()).replaceAll("\\s*", "");
123 assertThat(xmlAsString, containsString("element>identity"));
125 final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*");
126 final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString);
127 assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches());
131 * One leafref reference to other leafref via relative references.
134 public void testLeafRefRelativeChaining() {
135 getTargetNodeForLeafRef("leafname3", StringTypeDefinition.class);
139 public void testLeafRefRelative() {
140 getTargetNodeForLeafRef("pointToStringLeaf", StringTypeDefinition.class);
144 public void testLeafRefAbsoluteWithSameTarget() {
145 getTargetNodeForLeafRef("absname", InstanceIdentifierTypeDefinition.class);
149 * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
151 // ignored because this isn't implemented
154 public void testLeafRefWithDoublePointInPath() {
155 getTargetNodeForLeafRef("lf-with-double-point-inside", StringTypeDefinition.class);
159 public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
160 final TypeDefinition<?> targetNodeForAbsname = getTargetNodeForLeafRef("absname",
161 InstanceIdentifierTypeDefinition.class);
162 final TypeDefinition<?> targetNodeForRelname = getTargetNodeForLeafRef("relname",
163 InstanceIdentifierTypeDefinition.class);
164 assertEquals(targetNodeForAbsname, targetNodeForRelname);
167 private TypeDefinition<?> getTargetNodeForLeafRef(final String nodeName, final Class<?> clas) {
168 final LeafSchemaNode schemaNode = findSchemaNodeWithLeafrefType(leafRefModule, nodeName);
169 assertNotNull(schemaNode);
170 final LeafrefTypeDefinition leafrefTypedef = findLeafrefType(schemaNode);
171 assertNotNull(leafrefTypedef);
172 final TypeDefinition<?> targetBaseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypedef, schemaContext,
174 assertTrue("Wrong class found.", clas.isInstance(targetBaseType));
175 return targetBaseType;
178 private static Map<String, String> mapPrefixed(final Iterable<Map.Entry<URI, String>> prefixes) {
179 final Map<String, String> mappedPrefixes = new HashMap<>();
180 for (final Map.Entry<URI, String> prefix : prefixes) {
181 mappedPrefixes.put(prefix.getKey().toString(), prefix.getValue());
183 return mappedPrefixes;
186 private static QName getAttrQName(final String namespace, final String revision, final String localName,
187 final Optional<String> prefix) {
188 if (prefix.isPresent()) {
189 final QName moduleQName = QName.create(namespace, revision, "module");
190 final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision());
191 return QName.create(module, localName);
193 return QName.create(namespace, revision, localName);
196 private LeafSchemaNode findSchemaNodeWithLeafrefType(final DataNodeContainer module, final String nodeName) {
197 for (final DataSchemaNode childNode : module.getChildNodes()) {
198 if (childNode instanceof DataNodeContainer) {
199 LeafSchemaNode leafrefFromRecursion = findSchemaNodeWithLeafrefType((DataNodeContainer) childNode,
201 if (leafrefFromRecursion != null) {
202 return leafrefFromRecursion;
204 } else if (childNode.getQName().getLocalName().equals(nodeName) && childNode instanceof LeafSchemaNode) {
205 final TypeDefinition<?> leafSchemaNodeType = ((LeafSchemaNode) childNode).getType();
206 if (leafSchemaNodeType instanceof LeafrefTypeDefinition) {
207 return (LeafSchemaNode) childNode;
214 private static LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) {
215 final TypeDefinition<?> type = schemaNode.getType();
216 if (type instanceof LeafrefTypeDefinition) {
217 return (LeafrefTypeDefinition) type;