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 "
155 + "\"EmptyContainerEffectiveStatement{argument=(foo-namespace)leaf-container}\" in XML input"));
159 public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
160 ParserConfigurationException, SAXException, URISyntaxException {
161 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
163 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
165 final NormalizedNodeResult result = new NormalizedNodeResult();
166 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
168 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, parentContainerSchema);
169 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
170 assertThat(ex.getMessage(), containsString("Duplicate element \"my-anyxml\" in namespace \"foo-namespace\" "
171 + "with parent \"EmptyContainerEffectiveStatement{argument=(foo-namespace)anyxml-container}\" in XML "
176 public void shouldFailOnDuplicateContainer() throws XMLStreamException, IOException,
177 ParserConfigurationException, SAXException, URISyntaxException {
178 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml");
180 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
182 final NormalizedNodeResult result = new NormalizedNodeResult();
183 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
185 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, parentContainerSchema);
186 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
187 assertThat(ex.getMessage(), containsString("Duplicate element \"leaf-container\" in namespace"
188 + " \"foo-namespace\" with parent "
189 + "\"EmptyContainerEffectiveStatement{argument=(foo-namespace)parent-container}\" in XML input"));
193 public void shouldFailOnUnterminatedLeafElement() throws XMLStreamException, IOException,
194 ParserConfigurationException, SAXException, URISyntaxException {
195 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.xml");
197 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
199 final NormalizedNodeResult result = new NormalizedNodeResult();
200 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
202 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
203 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
204 assertThat(ex.getMessage(), containsString(" START_ELEMENT "));
208 public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
209 ParserConfigurationException, SAXException, URISyntaxException {
210 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml");
212 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
214 final NormalizedNodeResult result = new NormalizedNodeResult();
215 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
217 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
218 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
219 assertThat(ex.getMessage(), containsString("</my-leaf-1>"));
223 public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
224 ParserConfigurationException, SAXException, URISyntaxException {
225 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
227 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
229 final NormalizedNodeResult result = new NormalizedNodeResult();
230 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
232 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
233 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
234 assertThat(ex.getMessage(), containsString("</my-container-1>"));
238 public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
239 ParserConfigurationException, SAXException, URISyntaxException {
240 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
242 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
244 final NormalizedNodeResult result = new NormalizedNodeResult();
245 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
247 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, outerContainerSchema);
248 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
250 assertThat(ex.getMessage(), containsString("Schema for node with name my-container-1 and namespace "
251 + "baz-namespace does not exist in parent "
252 + "EmptyContainerEffectiveStatement{argument=(baz-namespace)my-container-1}"));
255 private static NormalizedNode buildOuterContainerNode() {
257 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST))
258 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
259 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1"))
260 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
261 .withValue("listleafvalue1").build())
262 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
263 .withValue("listleafvalue2").build()).build())
264 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
265 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2"))
266 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
267 .withValue("listleafvalue12").build())
268 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
269 .withValue("listleafvalue22").build()).build()).build();
271 LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
272 .withValue("value1").build();
274 SystemLeafSetNode<?> myLeafListNode = Builders.leafSetBuilder()
275 .withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST))
276 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
277 new NodeWithValue<>(MY_LEAFLIST, "lflvalue1")).withValue("lflvalue1").build())
278 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
279 new NodeWithValue<>(MY_LEAFLIST, "lflvalue2")).withValue("lflvalue2").build()).build();
281 ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
282 new NodeIdentifier(MY_CONTAINER_1))
283 .withChild(myKeyedListNode)
284 .withChild(myLeaf1Node)
285 .withChild(myLeafListNode).build();
288 ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
289 new NodeIdentifier(INNER_CONTAINER))
290 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_2))
291 .withValue("value2").build()).build();
293 LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_3))
294 .withValue("value3").build();
296 ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(MY_CHOICE))
297 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_CASE_2))
298 .withValue("case2value").build()).build();
300 ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
301 new NodeIdentifier(MY_CONTAINER_2))
302 .withChild(innerContainerNode)
303 .withChild(myLeaf3Node)
304 .withChild(myChoiceNode).build();
307 Map<QName, Object> keys = new HashMap<>();
308 keys.put(MY_FIRST_KEY_LEAF, "listkeyvalue1");
309 keys.put(MY_SECOND_KEY_LEAF, "listkeyvalue2");
311 MapNode myDoublyKeyedListNode = Builders.mapBuilder()
312 .withNodeIdentifier(new NodeIdentifier(MY_DOUBLY_KEYED_LIST))
313 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
314 NodeIdentifierWithPredicates.of(MY_DOUBLY_KEYED_LIST, keys))
315 .withChild(Builders.leafBuilder().withNodeIdentifier(
316 new NodeIdentifier(MY_LEAF_IN_LIST_3)).withValue("listleafvalue1").build()).build())
319 AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
320 new AugmentationIdentifier(ImmutableSet.of(MY_DOUBLY_KEYED_LIST)))
321 .withChild(myDoublyKeyedListNode).build();
323 ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
324 new NodeIdentifier(MY_CONTAINER_3))
325 .withChild(myDoublyKeyedListAugNode).build();
327 AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
328 new AugmentationIdentifier(ImmutableSet.of(MY_CONTAINER_3)))
329 .withChild(myContainer3Node).build();
331 ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
332 new NodeIdentifier(OUTER_CONTAINER))
333 .withChild(myContainer1Node)
334 .withChild(myContainer2Node)
335 .withChild(myContainer3AugNode).build();
337 return outerContainerNode;