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
9 package org.opendaylight.yangtools.yang.data.codec.xml;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import com.google.common.collect.ImmutableList;
17 import java.io.IOException;
18 import java.io.InputStream;
20 import java.net.URISyntaxException;
21 import java.util.Collections;
22 import java.util.HashMap;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.stream.XMLInputFactory;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.XMLStreamReader;
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
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.SchemaContext;
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 SchemaContext 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 XMLInputFactory factory = XMLInputFactory.newInstance();
109 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
111 final NormalizedNodeResult result = new NormalizedNodeResult();
112 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
114 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
115 xmlParser.parse(reader);
120 final NormalizedNode<?, ?> transformedInput = result.getResult();
121 assertNotNull(transformedInput);
123 final NormalizedNode<?, ?> expectedNormalizedNode = buildOuterContainerNode();
124 assertNotNull(expectedNormalizedNode);
126 assertEquals(expectedNormalizedNode, transformedInput);
130 public void testSimpleXmlParsing() throws IOException, URISyntaxException, XMLStreamException,
131 ParserConfigurationException, SAXException {
132 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/foo.xml");
134 final XMLInputFactory factory = XMLInputFactory.newInstance();
135 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
137 final NormalizedNodeResult result = new NormalizedNodeResult();
138 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
140 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
141 xmlParser.parse(reader);
143 final NormalizedNode<?, ?> transformedInput = result.getResult();
144 assertNotNull(transformedInput);
148 public void shouldFailOnDuplicateLeaf() throws XMLStreamException, IOException,
149 ParserConfigurationException, SAXException, URISyntaxException {
150 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo.xml");
152 final XMLInputFactory factory = XMLInputFactory.newInstance();
153 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
155 final NormalizedNodeResult result = new NormalizedNodeResult();
156 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
158 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
160 xmlParser.parse(reader);
161 fail("IllegalStateException should have been thrown because of duplicate leaf.");
162 } catch (IllegalStateException ex) {
163 assertTrue(ex.getMessage().contains("Duplicate element \"decimal64-leaf\" in XML input"));
169 public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
170 ParserConfigurationException, SAXException, URISyntaxException {
171 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
173 final XMLInputFactory factory = XMLInputFactory.newInstance();
174 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
176 final NormalizedNodeResult result = new NormalizedNodeResult();
177 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
179 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
181 xmlParser.parse(reader);
182 fail("IllegalStateException should have been thrown because of duplicate anyxml");
183 } catch (IllegalStateException ex) {
184 assertTrue(ex.getMessage().contains("Duplicate element \"my-anyxml\" in XML input"));
189 public void shouldFailOnDuplicateContainer() throws XMLStreamException, IOException,
190 ParserConfigurationException, SAXException, URISyntaxException {
191 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml");
193 final XMLInputFactory factory = XMLInputFactory.newInstance();
194 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
196 final NormalizedNodeResult result = new NormalizedNodeResult();
197 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
199 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
201 xmlParser.parse(reader);
202 fail("IllegalStateException should have been thrown because of duplicate container");
203 } catch (IllegalStateException ex) {
204 assertTrue(ex.getMessage().contains("Duplicate element \"leaf-container\" in XML input"));
209 public void shouldFailOnUnterminatedLeafElement() throws XMLStreamException, IOException,
210 ParserConfigurationException, SAXException, URISyntaxException {
211 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.xml");
213 final XMLInputFactory factory = XMLInputFactory.newInstance();
214 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
216 final NormalizedNodeResult result = new NormalizedNodeResult();
217 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
219 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
221 xmlParser.parse(reader);
222 fail("XMLStreamException should have been thrown because of unterminated leaf element.");
223 } catch (XMLStreamException ex) {
224 assertTrue(ex.getMessage().contains("elementGetText() function expects text only elment but "
225 + "START_ELEMENT was encountered."));
230 public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
231 ParserConfigurationException, SAXException, URISyntaxException {
232 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml");
234 final XMLInputFactory factory = XMLInputFactory.newInstance();
235 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
237 final NormalizedNodeResult result = new NormalizedNodeResult();
238 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
240 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
242 xmlParser.parse(reader);
243 fail("XMLStreamException should have been thrown because of unterminated leaf element.");
244 } catch (XMLStreamException ex) {
245 assertTrue(ex.getMessage().contains("The element type \"my-leaf-1\" must be terminated by the matching "
246 + "end-tag \"</my-leaf-1>\"."));
251 public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
252 ParserConfigurationException, SAXException, URISyntaxException {
253 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
255 final XMLInputFactory factory = XMLInputFactory.newInstance();
256 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
258 final NormalizedNodeResult result = new NormalizedNodeResult();
259 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
261 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
263 xmlParser.parse(reader);
264 fail("XMLStreamException should have been thrown because of unterminated container element.");
265 } catch (XMLStreamException ex) {
266 assertTrue(ex.getMessage().contains("The element type \"my-container-1\" must be terminated by the "
267 + "matching end-tag \"</my-container-1>\"."));
272 public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
273 ParserConfigurationException, SAXException, URISyntaxException {
274 final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
276 final XMLInputFactory factory = XMLInputFactory.newInstance();
277 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
279 final NormalizedNodeResult result = new NormalizedNodeResult();
280 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
282 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
284 xmlParser.parse(reader);
285 fail("IllegalStateException should have been thrown because of an unknown child node.");
286 } catch (IllegalStateException ex) {
287 assertEquals("Schema for node with name my-container-1 and namespace baz-namespace does not exist at "
288 + "AbsoluteSchemaPath{path=[(baz-namespace)outer-container, (baz-namespace)my-container-1]}",
293 private static NormalizedNode<?, ?> buildOuterContainerNode() {
295 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST))
296 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
297 new NodeIdentifierWithPredicates(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1"))
298 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
299 .withValue("listleafvalue1").build())
300 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
301 .withValue("listleafvalue2").build()).build())
302 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
303 new NodeIdentifierWithPredicates(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2"))
304 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
305 .withValue("listleafvalue12").build())
306 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
307 .withValue("listleafvalue22").build()).build()).build();
309 LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
310 .withValue("value1").build();
312 LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST))
313 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
314 new NodeWithValue<>(MY_LEAFLIST, "lflvalue1")).withValue("lflvalue1").build())
315 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
316 new NodeWithValue<>(MY_LEAFLIST, "lflvalue2")).withValue("lflvalue2").build()).build();
318 ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
319 new NodeIdentifier(MY_CONTAINER_1))
320 .withChild(myKeyedListNode)
321 .withChild(myLeaf1Node)
322 .withChild(myLeafListNode).build();
325 ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
326 new NodeIdentifier(INNER_CONTAINER))
327 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_2))
328 .withValue("value2").build()).build();
330 LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_3))
331 .withValue("value3").build();
333 ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(MY_CHOICE))
334 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_CASE_2))
335 .withValue("case2value").build()).build();
337 ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
338 new NodeIdentifier(MY_CONTAINER_2))
339 .withChild(innerContainerNode)
340 .withChild(myLeaf3Node)
341 .withChild(myChoiceNode).build();
344 Map<QName, Object> keys = new HashMap<>();
345 keys.put(MY_FIRST_KEY_LEAF, "listkeyvalue1");
346 keys.put(MY_SECOND_KEY_LEAF, "listkeyvalue2");
348 MapNode myDoublyKeyedListNode = Builders.mapBuilder()
349 .withNodeIdentifier(new NodeIdentifier(MY_DOUBLY_KEYED_LIST))
350 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
351 new NodeIdentifierWithPredicates(MY_DOUBLY_KEYED_LIST, keys))
352 .withChild(Builders.leafBuilder().withNodeIdentifier(
353 new NodeIdentifier(MY_LEAF_IN_LIST_3)).withValue("listleafvalue1").build()).build())
356 AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
357 new AugmentationIdentifier(Collections.singleton(MY_DOUBLY_KEYED_LIST)))
358 .withChild(myDoublyKeyedListNode).build();
360 ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
361 new NodeIdentifier(MY_CONTAINER_3))
362 .withChild(myDoublyKeyedListAugNode).build();
364 AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
365 new AugmentationIdentifier(Collections.singleton(MY_CONTAINER_3)))
366 .withChild(myContainer3Node).build();
368 ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
369 new NodeIdentifier(OUTER_CONTAINER))
370 .withChild(myContainer1Node)
371 .withChild(myContainer2Node)
372 .withChild(myContainer3AugNode).build();
374 return outerContainerNode;