Merge "BUG-2350: do encapsulte null snapshot"
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlStreamUtilsTest.java
1 /*
2  * Copyright (c) 2014 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.impl.codec.xml;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Maps;
17 import java.io.ByteArrayOutputStream;
18 import java.io.File;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.AbstractMap;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.Map;
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;
48
49 public class XmlStreamUtilsTest {
50
51     public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
52
53     private static SchemaContext schemaContext;
54     private static Module leafRefModule;
55
56     @BeforeClass
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);
65     }
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.<String>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 + "=\"namespace2\" " + randomPrefixValue + ":attr=\"value\"></element>";
95
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);
100
101         final boolean identical = diff.identical();
102         assertTrue("Xml differs: " + diff.toString(), identical);
103     }
104
105     /**
106      * One leafref reference to other leafref via relative references
107      */
108     @Test
109     public void testLeafRefRelativeChaining() {
110         getTargetNodeForLeafRef("leafname3",StringType.class);
111     }
112
113     @Test
114     public void testLeafRefRelative() {
115         getTargetNodeForLeafRef("pointToStringLeaf",StringType.class);
116     }
117
118     @Test
119     public void testLeafRefAbsoluteWithSameTarget() {
120         getTargetNodeForLeafRef("absname",InstanceIdentifierType.class);
121     }
122
123     /**
124      * Tests relative path with double point inside path (e. g. "../../lf:interface/../lf:cont2/lf:stringleaf")
125      */
126     @Ignore //ignored because this isn't implemented
127     @Test
128     public void testLeafRefWithDoublePointInPath() {
129         getTargetNodeForLeafRef("lf-with-double-point-inside",StringType.class);
130     }
131
132     @Test
133     public void testLeafRefRelativeAndAbsoluteWithSameTarget() {
134         final TypeDefinition<?> targetNodeForAbsname = getTargetNodeForLeafRef("absname",InstanceIdentifierType.class);
135         final TypeDefinition<?> targetNodeForRelname = getTargetNodeForLeafRef("relname",InstanceIdentifierType.class);
136         assertEquals(targetNodeForAbsname, targetNodeForRelname);
137     }
138
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;
147     }
148
149     @Test
150     public void testEmptyNodeWithAttribute() throws Exception {
151         final ByteArrayOutputStream out = new ByteArrayOutputStream();
152         final XMLStreamWriter writer =  XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
153
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);
159
160         writer.close();
161         out.close();
162
163         final String xmlAsString = new String(out.toByteArray());
164
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>";
167
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);
172
173         final boolean identical = diff.identical();
174         assertTrue("Xml differs: " + diff.toString(), identical);
175     }
176
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());
181         }
182         return mappedPrefixes;
183     }
184
185     private QName getAttrQName(final String namespace, final String revision, final String localName, final Optional<String> prefix) {
186
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, localName);
191         } else {
192             return QName.create(namespace, revision, localName);
193         }
194     }
195
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;
202                 }
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;
207                 }
208             }
209         }
210         return null;
211     }
212
213     private LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) {
214         final TypeDefinition<?> type = schemaNode.getType();
215         if (type instanceof LeafrefTypeDefinition) {
216             return (LeafrefTypeDefinition)type;
217         }
218         return null;
219     }
220 }