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.fail;
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);
157 xmlParser.parse(reader);
158 fail("XMLStreamException should have been thrown because of duplicate leaf.");
159 } catch (XMLStreamException ex) {
160 assertThat(ex.getMessage(), containsString("Duplicate namespace \"foo-namespace\" element "
161 + "\"decimal64-leaf\" in XML input"));
166 public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
167 ParserConfigurationException, SAXException, URISyntaxException {
168 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
170 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
172 final NormalizedNodeResult result = new NormalizedNodeResult();
173 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
175 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
177 xmlParser.parse(reader);
178 fail("XMLStreamException should have been thrown because of duplicate anyxml");
179 } catch (XMLStreamException ex) {
180 assertThat(ex.getMessage(), containsString("Duplicate namespace \"foo-namespace\" element \"my-anyxml\""));
185 public void shouldFailOnDuplicateContainer() throws XMLStreamException, IOException,
186 ParserConfigurationException, SAXException, URISyntaxException {
187 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml");
189 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
191 final NormalizedNodeResult result = new NormalizedNodeResult();
192 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
194 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
196 xmlParser.parse(reader);
197 fail("XMLStreamException should have been thrown because of duplicate container");
198 } catch (XMLStreamException ex) {
199 assertThat(ex.getMessage(), containsString("Duplicate namespace \"foo-namespace\" element "
200 + "\"leaf-container\" in XML input"));
205 public void shouldFailOnUnterminatedLeafElement() throws XMLStreamException, IOException,
206 ParserConfigurationException, SAXException, URISyntaxException {
207 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.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, schemaContext, outerContainerSchema);
216 xmlParser.parse(reader);
217 fail("XMLStreamException should have been thrown because of unterminated leaf element.");
218 } catch (XMLStreamException ex) {
219 assertThat(ex.getMessage(), containsString(" START_ELEMENT "));
224 public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
225 ParserConfigurationException, SAXException, URISyntaxException {
226 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.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);
235 xmlParser.parse(reader);
236 fail("XMLStreamException should have been thrown because of unterminated leaf element.");
237 } catch (XMLStreamException ex) {
238 assertThat(ex.getMessage(), containsString("</my-leaf-1>"));
243 public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
244 ParserConfigurationException, SAXException, URISyntaxException {
245 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
247 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
249 final NormalizedNodeResult result = new NormalizedNodeResult();
250 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
252 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
254 xmlParser.parse(reader);
255 fail("XMLStreamException should have been thrown because of unterminated container element.");
256 } catch (XMLStreamException ex) {
257 assertThat(ex.getMessage(), containsString("</my-container-1>"));
262 public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
263 ParserConfigurationException, SAXException, URISyntaxException {
264 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
266 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
268 final NormalizedNodeResult result = new NormalizedNodeResult();
269 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
271 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
273 xmlParser.parse(reader);
274 fail("XMLStreamException should have been thrown because of an unknown child node.");
275 } catch (XMLStreamException ex) {
276 assertEquals("Schema for node with name my-container-1 and namespace baz-namespace does not exist at "
277 + "AbsoluteSchemaPath{path=[(baz-namespace)outer-container, (baz-namespace)my-container-1]}",
282 private static NormalizedNode<?, ?> buildOuterContainerNode() {
284 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST))
285 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
286 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1"))
287 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
288 .withValue("listleafvalue1").build())
289 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
290 .withValue("listleafvalue2").build()).build())
291 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
292 NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2"))
293 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
294 .withValue("listleafvalue12").build())
295 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
296 .withValue("listleafvalue22").build()).build()).build();
298 LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
299 .withValue("value1").build();
301 LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST))
302 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
303 new NodeWithValue<>(MY_LEAFLIST, "lflvalue1")).withValue("lflvalue1").build())
304 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
305 new NodeWithValue<>(MY_LEAFLIST, "lflvalue2")).withValue("lflvalue2").build()).build();
307 ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
308 new NodeIdentifier(MY_CONTAINER_1))
309 .withChild(myKeyedListNode)
310 .withChild(myLeaf1Node)
311 .withChild(myLeafListNode).build();
314 ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
315 new NodeIdentifier(INNER_CONTAINER))
316 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_2))
317 .withValue("value2").build()).build();
319 LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_3))
320 .withValue("value3").build();
322 ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(MY_CHOICE))
323 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_CASE_2))
324 .withValue("case2value").build()).build();
326 ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
327 new NodeIdentifier(MY_CONTAINER_2))
328 .withChild(innerContainerNode)
329 .withChild(myLeaf3Node)
330 .withChild(myChoiceNode).build();
333 Map<QName, Object> keys = new HashMap<>();
334 keys.put(MY_FIRST_KEY_LEAF, "listkeyvalue1");
335 keys.put(MY_SECOND_KEY_LEAF, "listkeyvalue2");
337 MapNode myDoublyKeyedListNode = Builders.mapBuilder()
338 .withNodeIdentifier(new NodeIdentifier(MY_DOUBLY_KEYED_LIST))
339 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
340 NodeIdentifierWithPredicates.of(MY_DOUBLY_KEYED_LIST, keys))
341 .withChild(Builders.leafBuilder().withNodeIdentifier(
342 new NodeIdentifier(MY_LEAF_IN_LIST_3)).withValue("listleafvalue1").build()).build())
345 AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
346 new AugmentationIdentifier(ImmutableSet.of(MY_DOUBLY_KEYED_LIST)))
347 .withChild(myDoublyKeyedListNode).build();
349 ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
350 new NodeIdentifier(MY_CONTAINER_3))
351 .withChild(myDoublyKeyedListAugNode).build();
353 AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
354 new AugmentationIdentifier(ImmutableSet.of(MY_CONTAINER_3)))
355 .withChild(myContainer3Node).build();
357 ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
358 new NodeIdentifier(OUTER_CONTAINER))
359 .withChild(myContainer1Node)
360 .withChild(myContainer2Node)
361 .withChild(myContainer3AugNode).build();
363 return outerContainerNode;