2 * Copyright (c) 2014 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.impl.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;
16 import com.google.common.base.Optional;
17 import com.google.common.collect.Maps;
18 import java.io.ByteArrayOutputStream;
21 import java.net.URISyntaxException;
22 import java.util.AbstractMap;
23 import java.util.Arrays;
24 import java.util.Date;
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.LeafrefTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
45 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
46 import org.opendaylight.yangtools.yang.model.util.StringType;
47 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
48 import org.w3c.dom.Document;
50 public class XmlStreamUtilsTest {
52 public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
54 private static SchemaContext schemaContext;
55 private static Module leafRefModule;
58 public static void initialize() throws URISyntaxException {
59 final YangParserImpl yangParser = new YangParserImpl();
60 final File file = new File(XmlStreamUtils.class.getResource("/leafref-test.yang").toURI());
61 schemaContext = yangParser.parseFiles(Arrays.asList(file));
62 assertNotNull(schemaContext);
63 assertEquals(1,schemaContext.getModules().size());
64 leafRefModule = schemaContext.getModules().iterator().next();
65 assertNotNull(leafRefModule);
70 public void testWriteAttribute() throws Exception {
71 final ByteArrayOutputStream out = new ByteArrayOutputStream();
72 final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
73 writer.writeStartElement("element");
75 QName name = getAttrQName("namespace", "2012-12-12", "attr", Optional.of("prefix"));
76 final Map.Entry<QName, String> attributeEntry = new AbstractMap.SimpleEntry<>(name, "value");
78 name = getAttrQName("namespace2", "2012-12-12", "attr", Optional.<String>absent());
79 final Map.Entry<QName, String> attributeEntryNoPrefix = new AbstractMap.SimpleEntry<>(name, "value");
81 final RandomPrefix randomPrefix = new RandomPrefix();
82 XmlStreamUtils.writeAttribute(writer, attributeEntry, randomPrefix);
83 XmlStreamUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
85 writer.writeEndElement();
89 final String xmlAsString = new String(out.toByteArray());
91 final Map<String, String> mappedPrefixes = mapPrefixed(randomPrefix.getPrefixes());
92 assertEquals(2, mappedPrefixes.size());
93 final String randomPrefixValue = mappedPrefixes.get("namespace2");
95 final String expectedXmlAsString = "<element xmlns:a=\"namespace\" a:attr=\"value\" xmlns:" + randomPrefixValue + "=\"namespace2\" " + randomPrefixValue + ":attr=\"value\"></element>";
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);
102 final boolean identical = diff.identical();
103 assertTrue("Xml differs: " + diff.toString(), identical);
107 public void testWriteIdentityRef() throws Exception {
108 final ByteArrayOutputStream out = new ByteArrayOutputStream();
109 final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
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();
116 writer.writeStartElement("elementDifferent");
117 XmlStreamUtils.write(writer, null, QName.create("different:namespace", "identity"), Optional.of(parent));
118 writer.writeEndElement();
123 final String xmlAsString = new String(out.toByteArray()).replaceAll("\\s*", "");
124 assertThat(xmlAsString, containsString("element>identity"));
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());
132 * One leafref reference to other leafref via relative references
135 public void testLeafRefRelativeChaining() {
136 getTargetNodeForLeafRef("leafname3",StringType.class);
140 public void testLeafRefRelative() {
141 getTargetNodeForLeafRef("pointToStringLeaf",StringType.class);
145 public void testLeafRefAbsoluteWithSameTarget() {
146 getTargetNodeForLeafRef("absname",InstanceIdentifierType.class);
150 * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
152 @Ignore //ignored because this isn't implemented
154 public void testLeafRefWithDoublePointInPath() {
155 getTargetNodeForLeafRef("lf-with-double-point-inside",StringType.class);
159 public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
160 final TypeDefinition<?> targetNodeForAbsname = getTargetNodeForLeafRef("absname",InstanceIdentifierType.class);
161 final TypeDefinition<?> targetNodeForRelname = getTargetNodeForLeafRef("relname",InstanceIdentifierType.class);
162 assertEquals(targetNodeForAbsname, targetNodeForRelname);
165 private TypeDefinition<?> getTargetNodeForLeafRef(final String nodeName, final Class<?> clas) {
166 final LeafSchemaNode schemaNode = findSchemaNodeWithLeafrefType(leafRefModule, nodeName);
167 assertNotNull(schemaNode);
168 final LeafrefTypeDefinition leafrefTypedef = findLeafrefType(schemaNode);
169 assertNotNull(leafrefTypedef);
170 final TypeDefinition<?> targetBaseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypedef, schemaContext, schemaNode);
171 assertEquals("Wrong class found.", clas, targetBaseType.getClass());
172 return targetBaseType;
175 private static Map<String, String> mapPrefixed(final Iterable<Map.Entry<URI, String>> prefixes) {
176 final Map<String, String> mappedPrefixes = Maps.newHashMap();
177 for (final Map.Entry<URI, String> prefix : prefixes) {
178 mappedPrefixes.put(prefix.getKey().toString(), prefix.getValue());
180 return mappedPrefixes;
183 private static QName getAttrQName(final String namespace, final String revision, final String localName, final Optional<String> prefix) {
184 if (prefix.isPresent()) {
185 final QName moduleQName = QName.create(namespace, revision, "module");
186 final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision());
187 return QName.create(module, localName);
189 return QName.create(namespace, revision, localName);
193 private LeafSchemaNode findSchemaNodeWithLeafrefType(final DataNodeContainer module, final String nodeName) {
194 for (final DataSchemaNode childNode : module.getChildNodes()) {
195 if (childNode instanceof DataNodeContainer) {
196 LeafSchemaNode leafrefFromRecursion = findSchemaNodeWithLeafrefType((DataNodeContainer)childNode, nodeName);
197 if (leafrefFromRecursion != null) {
198 return leafrefFromRecursion;
200 } else if (childNode.getQName().getLocalName().equals(nodeName) && childNode instanceof LeafSchemaNode) {
201 final TypeDefinition<?> leafSchemaNodeType = ((LeafSchemaNode)childNode).getType();
202 if (leafSchemaNodeType instanceof LeafrefTypeDefinition) {
203 return (LeafSchemaNode)childNode;
210 private static LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) {
211 final TypeDefinition<?> type = schemaNode.getType();
212 if (type instanceof LeafrefTypeDefinition) {
213 return (LeafrefTypeDefinition)type;