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
8 package org.opendaylight.yangtools.yang.data.codec.xml;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
16 import com.google.common.collect.ImmutableSet;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
20 import java.util.HashMap;
22 import javax.xml.parsers.ParserConfigurationException;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamReader;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.util.xml.UntrustedXML;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.common.XMLNamespace;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
36 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
44 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
46 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
47 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
48 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
49 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
50 import org.xml.sax.SAXException;
52 public class XmlToNormalizedNodesTest {
54 private static final QNameModule FOO_MODULE = QNameModule.create(XMLNamespace.of("foo-namespace"));
55 private static final QName PARENT_CONTAINER = QName.create(FOO_MODULE, "parent-container");
57 private static final QNameModule BAZ_MODULE = QNameModule.create(XMLNamespace.of("baz-namespace"));
58 private static final QName OUTER_CONTAINER = QName.create(BAZ_MODULE, "outer-container");
60 private static final QName MY_CONTAINER_1 = QName.create(BAZ_MODULE, "my-container-1");
61 private static final QName MY_KEYED_LIST = QName.create(BAZ_MODULE, "my-keyed-list");
62 private static final QName MY_KEY_LEAF = QName.create(BAZ_MODULE, "my-key-leaf");
63 private static final QName MY_LEAF_IN_LIST_1 = QName.create(BAZ_MODULE, "my-leaf-in-list-1");
64 private static final QName MY_LEAF_IN_LIST_2 = QName.create(BAZ_MODULE, "my-leaf-in-list-2");
65 private static final QName MY_LEAF_1 = QName.create(BAZ_MODULE, "my-leaf-1");
66 private static final QName MY_LEAFLIST = QName.create(BAZ_MODULE, "my-leaf-list");
68 private static final QName MY_CONTAINER_2 = QName.create(BAZ_MODULE, "my-container-2");
69 private static final QName INNER_CONTAINER = QName.create(BAZ_MODULE, "inner-container");
70 private static final QName MY_LEAF_2 = QName.create(BAZ_MODULE, "my-leaf-2");
71 private static final QName MY_LEAF_3 = QName.create(BAZ_MODULE, "my-leaf-3");
72 private static final QName MY_CHOICE = QName.create(BAZ_MODULE, "my-choice");
73 private static final QName MY_LEAF_IN_CASE_2 = QName.create(BAZ_MODULE, "my-leaf-in-case-2");
75 private static final QName MY_CONTAINER_3 = QName.create(BAZ_MODULE, "my-container-3");
76 private static final QName MY_DOUBLY_KEYED_LIST = QName.create(BAZ_MODULE, "my-doubly-keyed-list");
77 private static final QName MY_FIRST_KEY_LEAF = QName.create(BAZ_MODULE, "my-first-key-leaf");
78 private static final QName MY_SECOND_KEY_LEAF = QName.create(BAZ_MODULE, "my-second-key-leaf");
79 private static final QName MY_LEAF_IN_LIST_3 = QName.create(BAZ_MODULE, "my-leaf-in-list-3");
81 private static EffectiveModelContext schemaContext;
82 private static Inference outerContainerSchema;
83 private static Inference parentContainerSchema;
86 public static void setup() {
87 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/");
88 parentContainerSchema = Inference.ofDataTreePath(schemaContext, PARENT_CONTAINER);
89 outerContainerSchema = Inference.ofDataTreePath(schemaContext, OUTER_CONTAINER);
93 public static void cleanup() {
95 parentContainerSchema = null;
96 outerContainerSchema = null;
100 public void testComplexXmlParsing() throws IOException, SAXException, URISyntaxException, XMLStreamException,
101 ParserConfigurationException {
102 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/baz.xml");
104 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
106 final NormalizedNodeResult result = new NormalizedNodeResult();
107 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
109 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
110 xmlParser.parse(reader);
115 final NormalizedNode transformedInput = result.getResult();
116 assertNotNull(transformedInput);
118 final NormalizedNode expectedNormalizedNode = buildOuterContainerNode();
119 assertNotNull(expectedNormalizedNode);
121 assertEquals(expectedNormalizedNode, transformedInput);
125 public void testSimpleXmlParsing() throws IOException, URISyntaxException, XMLStreamException,
126 ParserConfigurationException, SAXException {
127 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/foo.xml");
129 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
131 final NormalizedNodeResult result = new NormalizedNodeResult();
132 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
134 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, parentContainerSchema);
135 xmlParser.parse(reader);
137 final NormalizedNode transformedInput = result.getResult();
138 assertNotNull(transformedInput);
142 public void shouldFailOnDuplicateLeaf() throws XMLStreamException, IOException,
143 ParserConfigurationException, SAXException, URISyntaxException {
144 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo.xml");
146 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
148 final NormalizedNodeResult result = new NormalizedNodeResult();
149 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
151 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, parentContainerSchema);
152 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
153 assertThat(ex.getMessage(), containsString("Duplicate element \"decimal64-leaf\" in namespace"
154 + " \"foo-namespace\" with parent \"container leaf-container\" in XML input"));
158 public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
159 ParserConfigurationException, SAXException, URISyntaxException {
160 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
162 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
164 final NormalizedNodeResult result = new NormalizedNodeResult();
165 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
167 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, parentContainerSchema);
168 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
169 assertThat(ex.getMessage(), containsString("Duplicate element \"my-anyxml\" in namespace"
170 + " \"foo-namespace\" with parent \"container anyxml-container\" in XML input"));
174 public void shouldFailOnDuplicateContainer() throws XMLStreamException, IOException,
175 ParserConfigurationException, SAXException, URISyntaxException {
176 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml");
178 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
180 final NormalizedNodeResult result = new NormalizedNodeResult();
181 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
183 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, parentContainerSchema);
184 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
185 assertThat(ex.getMessage(), containsString("Duplicate element \"leaf-container\" in namespace"
186 + " \"foo-namespace\" with parent \"container parent-container\" in XML input"));
190 public void shouldFailOnUnterminatedLeafElement() throws XMLStreamException, IOException,
191 ParserConfigurationException, SAXException, URISyntaxException {
192 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.xml");
194 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
196 final NormalizedNodeResult result = new NormalizedNodeResult();
197 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
199 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
200 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
201 assertThat(ex.getMessage(), containsString(" START_ELEMENT "));
205 public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
206 ParserConfigurationException, SAXException, URISyntaxException {
207 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml");
209 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
211 final NormalizedNodeResult result = new NormalizedNodeResult();
212 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
214 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
215 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
216 assertThat(ex.getMessage(), containsString("</my-leaf-1>"));
220 public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
221 ParserConfigurationException, SAXException, URISyntaxException {
222 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
224 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
226 final NormalizedNodeResult result = new NormalizedNodeResult();
227 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
229 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
230 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
231 assertThat(ex.getMessage(), containsString("</my-container-1>"));
235 public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
236 ParserConfigurationException, SAXException, URISyntaxException {
237 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
239 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
241 final NormalizedNodeResult result = new NormalizedNodeResult();
242 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
244 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
245 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
247 assertThat(ex.getMessage(), containsString("Schema for node with name my-container-1 and namespace "
248 + "baz-namespace does not exist in parent container my-container-1"));
251 private static NormalizedNode buildOuterContainerNode() {
253 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST))
254 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
255 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1"))
256 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
257 .withValue("listleafvalue1").build())
258 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
259 .withValue("listleafvalue2").build()).build())
260 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
261 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2"))
262 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
263 .withValue("listleafvalue12").build())
264 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
265 .withValue("listleafvalue22").build()).build()).build();
267 LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
268 .withValue("value1").build();
270 SystemLeafSetNode<?> myLeafListNode = Builders.leafSetBuilder()
271 .withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST))
272 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
273 new NodeWithValue<>(MY_LEAFLIST, "lflvalue1")).withValue("lflvalue1").build())
274 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
275 new NodeWithValue<>(MY_LEAFLIST, "lflvalue2")).withValue("lflvalue2").build()).build();
277 ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
278 new NodeIdentifier(MY_CONTAINER_1))
279 .withChild(myKeyedListNode)
280 .withChild(myLeaf1Node)
281 .withChild(myLeafListNode).build();
284 ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
285 new NodeIdentifier(INNER_CONTAINER))
286 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_2))
287 .withValue("value2").build()).build();
289 LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_3))
290 .withValue("value3").build();
292 ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(MY_CHOICE))
293 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_CASE_2))
294 .withValue("case2value").build()).build();
296 ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
297 new NodeIdentifier(MY_CONTAINER_2))
298 .withChild(innerContainerNode)
299 .withChild(myLeaf3Node)
300 .withChild(myChoiceNode).build();
303 Map<QName, Object> keys = new HashMap<>();
304 keys.put(MY_FIRST_KEY_LEAF, "listkeyvalue1");
305 keys.put(MY_SECOND_KEY_LEAF, "listkeyvalue2");
307 MapNode myDoublyKeyedListNode = Builders.mapBuilder()
308 .withNodeIdentifier(new NodeIdentifier(MY_DOUBLY_KEYED_LIST))
309 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
310 NodeIdentifierWithPredicates.of(MY_DOUBLY_KEYED_LIST, keys))
311 .withChild(Builders.leafBuilder().withNodeIdentifier(
312 new NodeIdentifier(MY_LEAF_IN_LIST_3)).withValue("listleafvalue1").build()).build())
315 AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
316 new AugmentationIdentifier(ImmutableSet.of(MY_DOUBLY_KEYED_LIST)))
317 .withChild(myDoublyKeyedListNode).build();
319 ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
320 new NodeIdentifier(MY_CONTAINER_3))
321 .withChild(myDoublyKeyedListAugNode).build();
323 AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
324 new AugmentationIdentifier(ImmutableSet.of(MY_CONTAINER_3)))
325 .withChild(myContainer3Node).build();
327 ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
328 new NodeIdentifier(OUTER_CONTAINER))
329 .withChild(myContainer1Node)
330 .withChild(myContainer2Node)
331 .withChild(myContainer3AugNode).build();
333 return outerContainerNode;