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.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Maps;
17 import java.io.ByteArrayOutputStream;
20 import java.net.URISyntaxException;
21 import java.util.AbstractMap;
22 import java.util.Arrays;
23 import java.util.Collections;
25 import javax.xml.stream.XMLOutputFactory;
26 import javax.xml.stream.XMLStreamWriter;
27 import org.custommonkey.xmlunit.Diff;
28 import org.custommonkey.xmlunit.XMLUnit;
29 import org.junit.BeforeClass;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.data.api.Node;
35 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
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.LeafrefTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
44 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
45 import org.opendaylight.yangtools.yang.model.util.StringType;
46 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
47 import org.w3c.dom.Document;
49 public class XmlStreamUtilsTest {
51 public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
53 private static SchemaContext schemaContext;
54 private static Module leafRefModule;
57 public static void initialize() throws URISyntaxException {
58 final YangParserImpl yangParser = new YangParserImpl();
59 final File file = new File(XmlStreamUtils.class.getResource("/leafref-test.yang").toURI());
60 schemaContext = yangParser.parseFiles(Arrays.asList(file));
61 assertNotNull(schemaContext);
62 assertEquals(1,schemaContext.getModules().size());
63 leafRefModule = schemaContext.getModules().iterator().next();
64 assertNotNull(leafRefModule);
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");
74 QName name = getAttrQName("namespace", "2012-12-12", "attr", Optional.of("prefix"));
75 final Map.Entry<QName, String> attributeEntry = new AbstractMap.SimpleEntry<>(name, "value");
77 name = getAttrQName("namespace2", "2012-12-12", "attr", Optional.<String>absent());
78 final Map.Entry<QName, String> attributeEntryNoPrefix = new AbstractMap.SimpleEntry<>(name, "value");
80 final RandomPrefix randomPrefix = new RandomPrefix();
81 XmlStreamUtils.writeAttribute(writer, attributeEntry, randomPrefix);
82 XmlStreamUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
84 writer.writeEndElement();
88 final String xmlAsString = new String(out.toByteArray());
90 final Map<String, String> mappedPrefixes = mapPrefixed(randomPrefix.getPrefixes());
91 assertEquals(2, mappedPrefixes.size());
92 final String randomPrefixValue = mappedPrefixes.get("namespace2");
94 final String expectedXmlAsString = "<element xmlns:a=\"namespace\" a:attr=\"value\" xmlns:" + randomPrefixValue + "=\"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 * One leafref reference to other leafref via relative references
109 public void testLeafRefRelativeChaining() {
110 getTargetNodeForLeafRef("leafname3",StringType.class);
114 public void testLeafRefRelative() {
115 getTargetNodeForLeafRef("pointToStringLeaf",StringType.class);
119 public void testLeafRefAbsoluteWithSameTarget() {
120 getTargetNodeForLeafRef("absname",InstanceIdentifierType.class);
124 * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
126 @Ignore //ignored because this isn't implemented
128 public void testLeafRefWithDoublePointInPath() {
129 getTargetNodeForLeafRef("lf-with-double-point-inside",StringType.class);
133 public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
134 final TypeDefinition<?> targetNodeForAbsname = getTargetNodeForLeafRef("absname",InstanceIdentifierType.class);
135 final TypeDefinition<?> targetNodeForRelname = getTargetNodeForLeafRef("relname",InstanceIdentifierType.class);
136 assertEquals(targetNodeForAbsname, targetNodeForRelname);
139 private TypeDefinition<?> getTargetNodeForLeafRef(final String nodeName, final Class<?> clas) {
140 final LeafSchemaNode schemaNode = findSchemaNodeWithLeafrefType(leafRefModule, nodeName);
141 assertNotNull(schemaNode);
142 final LeafrefTypeDefinition leafrefTypedef = findLeafrefType(schemaNode);
143 assertNotNull(leafrefTypedef);
144 final TypeDefinition<?> targetBaseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypedef, schemaContext, schemaNode);
145 assertEquals("Wrong class found.", clas, targetBaseType.getClass());
146 return targetBaseType;
150 public void testEmptyNodeWithAttribute() throws Exception {
151 final ByteArrayOutputStream out = new ByteArrayOutputStream();
152 final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
154 final Map<QName, String> attrs = Maps.newHashMap();
155 attrs.put(QName.create("namespaceAttr", "2012-12-12", "attr1"), "value");
156 final QName qName = QName.create("urn:opendaylight:controller:rpc:test", "2014-07-28", "cont");
157 final ImmutableCompositeNode dataAttributes = ImmutableCompositeNode.create(qName, attrs, Collections.<Node<?>>emptyList());
158 XmlStreamUtils.create(XmlUtils.DEFAULT_XML_CODEC_PROVIDER).writeDocument(writer, dataAttributes);
163 final String xmlAsString = new String(out.toByteArray());
165 // TODO why resulting xml does not have namespace definition ? If sending xml by e.g. netconf the namespace is there but not here in test
166 final String expectedXmlAsString = "<cont xmlns:a=\"namespaceAttr\" a:attr1=\"value\"></cont>";
168 XMLUnit.setIgnoreAttributeOrder(true);
169 final Document control = XMLUnit.buildControlDocument(expectedXmlAsString);
170 final Document test = XMLUnit.buildTestDocument(xmlAsString);
171 final Diff diff = XMLUnit.compareXML(control, test);
173 final boolean identical = diff.identical();
174 assertTrue("Xml differs: " + diff.toString(), identical);
177 private Map<String, String> mapPrefixed(final Iterable<Map.Entry<URI, String>> prefixes) {
178 final Map<String, String> mappedPrefixes = Maps.newHashMap();
179 for (final Map.Entry<URI, String> prefix : prefixes) {
180 mappedPrefixes.put(prefix.getKey().toString(), prefix.getValue());
182 return mappedPrefixes;
185 private QName getAttrQName(final String namespace, final String revision, final String localName, final Optional<String> prefix) {
187 if(prefix.isPresent()) {
188 final QName moduleQName = QName.create(namespace, revision, "module");
189 final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision());
190 return QName.create(module, prefix.get(), localName);
192 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, nodeName);
200 if (leafrefFromRecursion != null) {
201 return leafrefFromRecursion;
203 } else if (childNode.getQName().getLocalName().equals(nodeName) && childNode instanceof LeafSchemaNode) {
204 final TypeDefinition<?> leafSchemaNodeType = ((LeafSchemaNode)childNode).getType();
205 if (leafSchemaNodeType instanceof LeafrefTypeDefinition) {
206 return (LeafSchemaNode)childNode;
213 private LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) {
214 final TypeDefinition<?> type = schemaNode.getType();
215 if (type instanceof LeafrefTypeDefinition) {
216 return (LeafrefTypeDefinition)type;