Consider namespace in namesakes
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlToNormalizedNodesTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.codec.xml;
10
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;
15
16 import com.google.common.collect.ImmutableList;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
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.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;
53
54 public class XmlToNormalizedNodesTest {
55
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");
58
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");
61
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");
69
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");
76
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");
82
83     private static SchemaContext schemaContext;
84     private static ContainerSchemaNode outerContainerSchema;
85     private static ContainerSchemaNode parentContainerSchema;
86
87     @BeforeClass
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));
94     }
95
96     @AfterClass
97     public static void cleanup() {
98         schemaContext = null;
99         parentContainerSchema = null;
100         outerContainerSchema = null;
101     }
102
103     @Test
104     public void testComplexXmlParsing() throws IOException, SAXException, URISyntaxException, XMLStreamException,
105             ParserConfigurationException {
106         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/baz.xml");
107
108         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
109
110         final NormalizedNodeResult result = new NormalizedNodeResult();
111         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
112
113         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
114         xmlParser.parse(reader);
115
116         xmlParser.flush();
117         xmlParser.close();
118
119         final NormalizedNode<?, ?> transformedInput = result.getResult();
120         assertNotNull(transformedInput);
121
122         final NormalizedNode<?, ?> expectedNormalizedNode = buildOuterContainerNode();
123         assertNotNull(expectedNormalizedNode);
124
125         assertEquals(expectedNormalizedNode, transformedInput);
126     }
127
128     @Test
129     public void testSimpleXmlParsing() throws IOException, URISyntaxException, XMLStreamException,
130             ParserConfigurationException, SAXException {
131         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/foo.xml");
132
133         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
134
135         final NormalizedNodeResult result = new NormalizedNodeResult();
136         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
137
138         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
139         xmlParser.parse(reader);
140
141         final NormalizedNode<?, ?> transformedInput = result.getResult();
142         assertNotNull(transformedInput);
143     }
144
145     @Test
146     public void shouldFailOnDuplicateLeaf() throws XMLStreamException, IOException,
147             ParserConfigurationException, SAXException, URISyntaxException {
148         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo.xml");
149
150         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
151
152         final NormalizedNodeResult result = new NormalizedNodeResult();
153         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
154
155         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
156         try {
157             xmlParser.parse(reader);
158             fail("IllegalStateException should have been thrown because of duplicate leaf.");
159         } catch (IllegalStateException ex) {
160             assertEquals("Duplicate namespace \"foo-namespace\" element \"decimal64-leaf\" in XML input at: line 7 "
161                     + "column 25", ex.getMessage());
162         }
163     }
164
165     @Test
166     public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
167             ParserConfigurationException, SAXException, URISyntaxException {
168         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
169
170         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
171
172         final NormalizedNodeResult result = new NormalizedNodeResult();
173         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
174
175         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
176         try {
177             xmlParser.parse(reader);
178             fail("IllegalStateException should have been thrown because of duplicate anyxml");
179         } catch (IllegalStateException ex) {
180             assertEquals("Duplicate namespace \"foo-namespace\" element \"my-anyxml\" in XML input at: line 19 "
181                     + "column 20", ex.getMessage());
182         }
183     }
184
185     @Test
186     public void shouldFailOnDuplicateContainer() throws XMLStreamException, IOException,
187             ParserConfigurationException, SAXException, URISyntaxException {
188         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml");
189
190         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
191
192         final NormalizedNodeResult result = new NormalizedNodeResult();
193         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
194
195         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
196         try {
197             xmlParser.parse(reader);
198             fail("IllegalStateException should have been thrown because of duplicate container");
199         } catch (IllegalStateException ex) {
200             assertEquals("Duplicate namespace \"foo-namespace\" element \"leaf-container\" in XML input at: line 13 "
201                     + "column 21", ex.getMessage());
202         }
203     }
204
205     @Test
206     public void shouldFailOnUnterminatedLeafElement() throws XMLStreamException, IOException,
207             ParserConfigurationException, SAXException, URISyntaxException {
208         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.xml");
209
210         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
211
212         final NormalizedNodeResult result = new NormalizedNodeResult();
213         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
214
215         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
216         try {
217             xmlParser.parse(reader);
218             fail("XMLStreamException should have been thrown because of unterminated leaf element.");
219         } catch (XMLStreamException ex) {
220             assertTrue(ex.getMessage().contains("elementGetText() function expects text only elment but "
221                         + "START_ELEMENT was encountered."));
222         }
223     }
224
225     @Test
226     public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
227             ParserConfigurationException, SAXException, URISyntaxException {
228         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml");
229
230         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
231
232         final NormalizedNodeResult result = new NormalizedNodeResult();
233         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
234
235         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
236         try {
237             xmlParser.parse(reader);
238             fail("XMLStreamException should have been thrown because of unterminated leaf element.");
239         } catch (XMLStreamException ex) {
240             assertTrue(ex.getMessage().contains("The element type \"my-leaf-1\" must be terminated by the matching "
241                         + "end-tag \"</my-leaf-1>\"."));
242         }
243     }
244
245     @Test
246     public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
247             ParserConfigurationException, SAXException, URISyntaxException {
248         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
249
250         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
251
252         final NormalizedNodeResult result = new NormalizedNodeResult();
253         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
254
255         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
256         try {
257             xmlParser.parse(reader);
258             fail("XMLStreamException should have been thrown because of unterminated container element.");
259         } catch (XMLStreamException ex) {
260             assertTrue(ex.getMessage().contains("The element type \"my-container-1\" must be terminated by the "
261                         + "matching end-tag \"</my-container-1>\"."));
262         }
263     }
264
265     @Test
266     public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
267             ParserConfigurationException, SAXException, URISyntaxException {
268         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
269
270         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
271
272         final NormalizedNodeResult result = new NormalizedNodeResult();
273         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
274
275         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
276         try {
277             xmlParser.parse(reader);
278             fail("IllegalStateException should have been thrown because of an unknown child node.");
279         } catch (IllegalStateException ex) {
280             assertEquals("Schema for node with name my-container-1 and namespace baz-namespace does not exist at "
281                     + "AbsoluteSchemaPath{path=[(baz-namespace)outer-container, (baz-namespace)my-container-1]}",
282                     ex.getMessage());
283         }
284     }
285
286     private static NormalizedNode<?, ?> buildOuterContainerNode() {
287         // my-container-1
288         MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST))
289                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
290                         new NodeIdentifierWithPredicates(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1"))
291                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
292                                 .withValue("listleafvalue1").build())
293                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
294                                 .withValue("listleafvalue2").build()).build())
295                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
296                         new NodeIdentifierWithPredicates(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2"))
297                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
298                                 .withValue("listleafvalue12").build())
299                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
300                                 .withValue("listleafvalue22").build()).build()).build();
301
302         LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
303                 .withValue("value1").build();
304
305         LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST))
306                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
307                         new NodeWithValue<>(MY_LEAFLIST, "lflvalue1")).withValue("lflvalue1").build())
308                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
309                         new NodeWithValue<>(MY_LEAFLIST, "lflvalue2")).withValue("lflvalue2").build()).build();
310
311         ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
312                 new NodeIdentifier(MY_CONTAINER_1))
313                 .withChild(myKeyedListNode)
314                 .withChild(myLeaf1Node)
315                 .withChild(myLeafListNode).build();
316
317         // my-container-2
318         ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
319                 new NodeIdentifier(INNER_CONTAINER))
320                 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_2))
321                         .withValue("value2").build()).build();
322
323         LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_3))
324                 .withValue("value3").build();
325
326         ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(MY_CHOICE))
327                 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_CASE_2))
328                         .withValue("case2value").build()).build();
329
330         ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
331                 new NodeIdentifier(MY_CONTAINER_2))
332                 .withChild(innerContainerNode)
333                 .withChild(myLeaf3Node)
334                 .withChild(myChoiceNode).build();
335
336         // my-container-3
337         Map<QName, Object> keys = new HashMap<>();
338         keys.put(MY_FIRST_KEY_LEAF, "listkeyvalue1");
339         keys.put(MY_SECOND_KEY_LEAF, "listkeyvalue2");
340
341         MapNode myDoublyKeyedListNode = Builders.mapBuilder()
342                 .withNodeIdentifier(new NodeIdentifier(MY_DOUBLY_KEYED_LIST))
343                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
344                         new NodeIdentifierWithPredicates(MY_DOUBLY_KEYED_LIST, keys))
345                         .withChild(Builders.leafBuilder().withNodeIdentifier(
346                                 new NodeIdentifier(MY_LEAF_IN_LIST_3)).withValue("listleafvalue1").build()).build())
347                 .build();
348
349         AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
350                 new AugmentationIdentifier(Collections.singleton(MY_DOUBLY_KEYED_LIST)))
351                 .withChild(myDoublyKeyedListNode).build();
352
353         ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
354                 new NodeIdentifier(MY_CONTAINER_3))
355                 .withChild(myDoublyKeyedListAugNode).build();
356
357         AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
358                 new AugmentationIdentifier(Collections.singleton(MY_CONTAINER_3)))
359                 .withChild(myContainer3Node).build();
360
361         ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
362                 new NodeIdentifier(OUTER_CONTAINER))
363                 .withChild(myContainer1Node)
364                 .withChild(myContainer2Node)
365                 .withChild(myContainer3AugNode).build();
366
367         return outerContainerNode;
368     }
369 }