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.ImmutableList;
17 import com.google.common.collect.ImmutableSet;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import java.net.URISyntaxException;
22 import java.util.HashMap;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.XMLStreamReader;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.yangtools.util.xml.UntrustedXML;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
37 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
45 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
46 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
47 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
48 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
50 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
51 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
52 import org.xml.sax.SAXException;
54 public class XmlToNormalizedNodesTest {
56 private static final QNameModule FOO_MODULE = QNameModule.create(URI.create("foo-namespace"));
57 private static final QName PARENT_CONTAINER = QName.create(FOO_MODULE, "parent-container");
59 private static final QNameModule BAZ_MODULE = QNameModule.create(URI.create("baz-namespace"));
60 private static final QName OUTER_CONTAINER = QName.create(BAZ_MODULE, "outer-container");
62 private static final QName MY_CONTAINER_1 = QName.create(BAZ_MODULE, "my-container-1");
63 private static final QName MY_KEYED_LIST = QName.create(BAZ_MODULE, "my-keyed-list");
64 private static final QName MY_KEY_LEAF = QName.create(BAZ_MODULE, "my-key-leaf");
65 private static final QName MY_LEAF_IN_LIST_1 = QName.create(BAZ_MODULE, "my-leaf-in-list-1");
66 private static final QName MY_LEAF_IN_LIST_2 = QName.create(BAZ_MODULE, "my-leaf-in-list-2");
67 private static final QName MY_LEAF_1 = QName.create(BAZ_MODULE, "my-leaf-1");
68 private static final QName MY_LEAFLIST = QName.create(BAZ_MODULE, "my-leaf-list");
70 private static final QName MY_CONTAINER_2 = QName.create(BAZ_MODULE, "my-container-2");
71 private static final QName INNER_CONTAINER = QName.create(BAZ_MODULE, "inner-container");
72 private static final QName MY_LEAF_2 = QName.create(BAZ_MODULE, "my-leaf-2");
73 private static final QName MY_LEAF_3 = QName.create(BAZ_MODULE, "my-leaf-3");
74 private static final QName MY_CHOICE = QName.create(BAZ_MODULE, "my-choice");
75 private static final QName MY_LEAF_IN_CASE_2 = QName.create(BAZ_MODULE, "my-leaf-in-case-2");
77 private static final QName MY_CONTAINER_3 = QName.create(BAZ_MODULE, "my-container-3");
78 private static final QName MY_DOUBLY_KEYED_LIST = QName.create(BAZ_MODULE, "my-doubly-keyed-list");
79 private static final QName MY_FIRST_KEY_LEAF = QName.create(BAZ_MODULE, "my-first-key-leaf");
80 private static final QName MY_SECOND_KEY_LEAF = QName.create(BAZ_MODULE, "my-second-key-leaf");
81 private static final QName MY_LEAF_IN_LIST_3 = QName.create(BAZ_MODULE, "my-leaf-in-list-3");
83 private static EffectiveModelContext schemaContext;
84 private static ContainerSchemaNode outerContainerSchema;
85 private static ContainerSchemaNode parentContainerSchema;
88 public static void setup() {
89 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/");
90 parentContainerSchema = (ContainerSchemaNode) SchemaContextUtil.findNodeInSchemaContext(schemaContext,
91 ImmutableList.of(PARENT_CONTAINER));
92 outerContainerSchema = (ContainerSchemaNode) SchemaContextUtil.findNodeInSchemaContext(schemaContext,
93 ImmutableList.of(OUTER_CONTAINER));
97 public static void cleanup() {
99 parentContainerSchema = null;
100 outerContainerSchema = null;
104 public void testComplexXmlParsing() throws IOException, SAXException, URISyntaxException, XMLStreamException,
105 ParserConfigurationException {
106 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/baz.xml");
108 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
110 final NormalizedNodeResult result = new NormalizedNodeResult();
111 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
113 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
114 xmlParser.parse(reader);
119 final NormalizedNode<?, ?> transformedInput = result.getResult();
120 assertNotNull(transformedInput);
122 final NormalizedNode<?, ?> expectedNormalizedNode = buildOuterContainerNode();
123 assertNotNull(expectedNormalizedNode);
125 assertEquals(expectedNormalizedNode, transformedInput);
129 public void testSimpleXmlParsing() throws IOException, URISyntaxException, XMLStreamException,
130 ParserConfigurationException, SAXException {
131 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/foo.xml");
133 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
135 final NormalizedNodeResult result = new NormalizedNodeResult();
136 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
138 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
139 xmlParser.parse(reader);
141 final NormalizedNode<?, ?> transformedInput = result.getResult();
142 assertNotNull(transformedInput);
146 public void shouldFailOnDuplicateLeaf() throws XMLStreamException, IOException,
147 ParserConfigurationException, SAXException, URISyntaxException {
148 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo.xml");
150 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
152 final NormalizedNodeResult result = new NormalizedNodeResult();
153 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
155 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
156 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
157 assertThat(ex.getMessage(), containsString("Duplicate element \"decimal64-leaf\" in namespace"
158 + " \"foo-namespace\" with parent \"container leaf-container\" in XML input"));
162 public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
163 ParserConfigurationException, SAXException, URISyntaxException {
164 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
166 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
168 final NormalizedNodeResult result = new NormalizedNodeResult();
169 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
171 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
172 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
173 assertThat(ex.getMessage(), containsString("Duplicate element \"my-anyxml\" in namespace"
174 + " \"foo-namespace\" with parent \"container anyxml-container\" in XML input"));
178 public void shouldFailOnDuplicateContainer() throws XMLStreamException, IOException,
179 ParserConfigurationException, SAXException, URISyntaxException {
180 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml");
182 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
184 final NormalizedNodeResult result = new NormalizedNodeResult();
185 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
187 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
188 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
189 assertThat(ex.getMessage(), containsString("Duplicate element \"leaf-container\" in namespace"
190 + " \"foo-namespace\" with parent \"container parent-container\" in XML input"));
194 public void shouldFailOnUnterminatedLeafElement() throws XMLStreamException, IOException,
195 ParserConfigurationException, SAXException, URISyntaxException {
196 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.xml");
198 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
200 final NormalizedNodeResult result = new NormalizedNodeResult();
201 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
203 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
204 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
205 assertThat(ex.getMessage(), containsString(" START_ELEMENT "));
209 public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
210 ParserConfigurationException, SAXException, URISyntaxException {
211 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml");
213 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
215 final NormalizedNodeResult result = new NormalizedNodeResult();
216 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
218 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
219 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
220 assertThat(ex.getMessage(), containsString("</my-leaf-1>"));
224 public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
225 ParserConfigurationException, SAXException, URISyntaxException {
226 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
228 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
230 final NormalizedNodeResult result = new NormalizedNodeResult();
231 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
233 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
234 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
235 assertThat(ex.getMessage(), containsString("</my-container-1>"));
239 public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
240 ParserConfigurationException, SAXException, URISyntaxException {
241 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
243 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
245 final NormalizedNodeResult result = new NormalizedNodeResult();
246 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
248 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
249 final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
251 assertThat(ex.getMessage(), containsString("Schema for node with name my-container-1 and namespace "
252 + "baz-namespace does not exist at "
253 + "AbsoluteSchemaPath{path=[(baz-namespace)outer-container, (baz-namespace)my-container-1]}"));
256 private static NormalizedNode<?, ?> buildOuterContainerNode() {
258 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST))
259 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
260 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1"))
261 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
262 .withValue("listleafvalue1").build())
263 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
264 .withValue("listleafvalue2").build()).build())
265 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
266 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2"))
267 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
268 .withValue("listleafvalue12").build())
269 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
270 .withValue("listleafvalue22").build()).build()).build();
272 LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
273 .withValue("value1").build();
275 LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().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;