Deprecate NodeIdentifierWithPredicates constructors
[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.hamcrest.CoreMatchers.containsString;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.fail;
16
17 import com.google.common.collect.ImmutableList;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import javax.xml.parsers.ParserConfigurationException;
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.util.xml.UntrustedXML;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
38 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
46 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
47 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
48 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
49 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
52 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53 import org.xml.sax.SAXException;
54
55 public class XmlToNormalizedNodesTest {
56
57     private static final QNameModule FOO_MODULE = QNameModule.create(URI.create("foo-namespace"));
58     private static final QName PARENT_CONTAINER = QName.create(FOO_MODULE, "parent-container");
59
60     private static final QNameModule BAZ_MODULE = QNameModule.create(URI.create("baz-namespace"));
61     private static final QName OUTER_CONTAINER = QName.create(BAZ_MODULE, "outer-container");
62
63     private static final QName MY_CONTAINER_1 = QName.create(BAZ_MODULE, "my-container-1");
64     private static final QName MY_KEYED_LIST = QName.create(BAZ_MODULE, "my-keyed-list");
65     private static final QName MY_KEY_LEAF = QName.create(BAZ_MODULE, "my-key-leaf");
66     private static final QName MY_LEAF_IN_LIST_1 = QName.create(BAZ_MODULE, "my-leaf-in-list-1");
67     private static final QName MY_LEAF_IN_LIST_2 = QName.create(BAZ_MODULE, "my-leaf-in-list-2");
68     private static final QName MY_LEAF_1 = QName.create(BAZ_MODULE, "my-leaf-1");
69     private static final QName MY_LEAFLIST = QName.create(BAZ_MODULE, "my-leaf-list");
70
71     private static final QName MY_CONTAINER_2 = QName.create(BAZ_MODULE, "my-container-2");
72     private static final QName INNER_CONTAINER = QName.create(BAZ_MODULE, "inner-container");
73     private static final QName MY_LEAF_2 = QName.create(BAZ_MODULE, "my-leaf-2");
74     private static final QName MY_LEAF_3 = QName.create(BAZ_MODULE, "my-leaf-3");
75     private static final QName MY_CHOICE = QName.create(BAZ_MODULE, "my-choice");
76     private static final QName MY_LEAF_IN_CASE_2 = QName.create(BAZ_MODULE, "my-leaf-in-case-2");
77
78     private static final QName MY_CONTAINER_3 = QName.create(BAZ_MODULE, "my-container-3");
79     private static final QName MY_DOUBLY_KEYED_LIST = QName.create(BAZ_MODULE, "my-doubly-keyed-list");
80     private static final QName MY_FIRST_KEY_LEAF = QName.create(BAZ_MODULE, "my-first-key-leaf");
81     private static final QName MY_SECOND_KEY_LEAF = QName.create(BAZ_MODULE, "my-second-key-leaf");
82     private static final QName MY_LEAF_IN_LIST_3 = QName.create(BAZ_MODULE, "my-leaf-in-list-3");
83
84     private static SchemaContext schemaContext;
85     private static ContainerSchemaNode outerContainerSchema;
86     private static ContainerSchemaNode parentContainerSchema;
87
88     @BeforeClass
89     public static void setup() {
90         schemaContext = YangParserTestUtils.parseYangResourceDirectory("/");
91         parentContainerSchema = (ContainerSchemaNode) SchemaContextUtil.findNodeInSchemaContext(schemaContext,
92                 ImmutableList.of(PARENT_CONTAINER));
93         outerContainerSchema = (ContainerSchemaNode) SchemaContextUtil.findNodeInSchemaContext(schemaContext,
94                 ImmutableList.of(OUTER_CONTAINER));
95     }
96
97     @AfterClass
98     public static void cleanup() {
99         schemaContext = null;
100         parentContainerSchema = null;
101         outerContainerSchema = null;
102     }
103
104     @Test
105     public void testComplexXmlParsing() throws IOException, SAXException, URISyntaxException, XMLStreamException,
106             ParserConfigurationException {
107         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/baz.xml");
108
109         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
110
111         final NormalizedNodeResult result = new NormalizedNodeResult();
112         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
113
114         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
115         xmlParser.parse(reader);
116
117         xmlParser.flush();
118         xmlParser.close();
119
120         final NormalizedNode<?, ?> transformedInput = result.getResult();
121         assertNotNull(transformedInput);
122
123         final NormalizedNode<?, ?> expectedNormalizedNode = buildOuterContainerNode();
124         assertNotNull(expectedNormalizedNode);
125
126         assertEquals(expectedNormalizedNode, transformedInput);
127     }
128
129     @Test
130     public void testSimpleXmlParsing() throws IOException, URISyntaxException, XMLStreamException,
131             ParserConfigurationException, SAXException {
132         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/foo.xml");
133
134         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
135
136         final NormalizedNodeResult result = new NormalizedNodeResult();
137         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
138
139         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
140         xmlParser.parse(reader);
141
142         final NormalizedNode<?, ?> transformedInput = result.getResult();
143         assertNotNull(transformedInput);
144     }
145
146     @Test
147     public void shouldFailOnDuplicateLeaf() throws XMLStreamException, IOException,
148             ParserConfigurationException, SAXException, URISyntaxException {
149         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo.xml");
150
151         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
152
153         final NormalizedNodeResult result = new NormalizedNodeResult();
154         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
155
156         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
157         try {
158             xmlParser.parse(reader);
159             fail("XMLStreamException should have been thrown because of duplicate leaf.");
160         } catch (XMLStreamException ex) {
161             assertThat(ex.getMessage(), containsString("Duplicate namespace \"foo-namespace\" element "
162                     + "\"decimal64-leaf\" in XML input"));
163         }
164     }
165
166     @Test
167     public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
168             ParserConfigurationException, SAXException, URISyntaxException {
169         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
170
171         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
172
173         final NormalizedNodeResult result = new NormalizedNodeResult();
174         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
175
176         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
177         try {
178             xmlParser.parse(reader);
179             fail("XMLStreamException should have been thrown because of duplicate anyxml");
180         } catch (XMLStreamException ex) {
181             assertThat(ex.getMessage(), containsString("Duplicate namespace \"foo-namespace\" element \"my-anyxml\""));
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("XMLStreamException should have been thrown because of duplicate container");
199         } catch (XMLStreamException ex) {
200             assertThat(ex.getMessage(), containsString("Duplicate namespace \"foo-namespace\" element "
201                     + "\"leaf-container\" in XML input"));
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             assertThat(ex.getMessage(), containsString(" START_ELEMENT "));
221         }
222     }
223
224     @Test
225     public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
226             ParserConfigurationException, SAXException, URISyntaxException {
227         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml");
228
229         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
230
231         final NormalizedNodeResult result = new NormalizedNodeResult();
232         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
233
234         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
235         try {
236             xmlParser.parse(reader);
237             fail("XMLStreamException should have been thrown because of unterminated leaf element.");
238         } catch (XMLStreamException ex) {
239             assertThat(ex.getMessage(), containsString("</my-leaf-1>"));
240         }
241     }
242
243     @Test
244     public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
245             ParserConfigurationException, SAXException, URISyntaxException {
246         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
247
248         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
249
250         final NormalizedNodeResult result = new NormalizedNodeResult();
251         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
252
253         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
254         try {
255             xmlParser.parse(reader);
256             fail("XMLStreamException should have been thrown because of unterminated container element.");
257         } catch (XMLStreamException ex) {
258             assertThat(ex.getMessage(), containsString("</my-container-1>"));
259         }
260     }
261
262     @Test
263     public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
264             ParserConfigurationException, SAXException, URISyntaxException {
265         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
266
267         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
268
269         final NormalizedNodeResult result = new NormalizedNodeResult();
270         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
271
272         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
273         try {
274             xmlParser.parse(reader);
275             fail("XMLStreamException should have been thrown because of an unknown child node.");
276         } catch (XMLStreamException ex) {
277             assertEquals("Schema for node with name my-container-1 and namespace baz-namespace does not exist at "
278                     + "AbsoluteSchemaPath{path=[(baz-namespace)outer-container, (baz-namespace)my-container-1]}",
279                     ex.getMessage());
280         }
281     }
282
283     private static NormalizedNode<?, ?> buildOuterContainerNode() {
284         // my-container-1
285         MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST))
286                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
287                         NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1"))
288                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
289                                 .withValue("listleafvalue1").build())
290                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
291                                 .withValue("listleafvalue2").build()).build())
292                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
293                         NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2"))
294                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1))
295                                 .withValue("listleafvalue12").build())
296                         .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2))
297                                 .withValue("listleafvalue22").build()).build()).build();
298
299         LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
300                 .withValue("value1").build();
301
302         LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST))
303                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
304                         new NodeWithValue<>(MY_LEAFLIST, "lflvalue1")).withValue("lflvalue1").build())
305                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(
306                         new NodeWithValue<>(MY_LEAFLIST, "lflvalue2")).withValue("lflvalue2").build()).build();
307
308         ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
309                 new NodeIdentifier(MY_CONTAINER_1))
310                 .withChild(myKeyedListNode)
311                 .withChild(myLeaf1Node)
312                 .withChild(myLeafListNode).build();
313
314         // my-container-2
315         ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(
316                 new NodeIdentifier(INNER_CONTAINER))
317                 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_2))
318                         .withValue("value2").build()).build();
319
320         LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_3))
321                 .withValue("value3").build();
322
323         ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(MY_CHOICE))
324                 .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_CASE_2))
325                         .withValue("case2value").build()).build();
326
327         ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
328                 new NodeIdentifier(MY_CONTAINER_2))
329                 .withChild(innerContainerNode)
330                 .withChild(myLeaf3Node)
331                 .withChild(myChoiceNode).build();
332
333         // my-container-3
334         Map<QName, Object> keys = new HashMap<>();
335         keys.put(MY_FIRST_KEY_LEAF, "listkeyvalue1");
336         keys.put(MY_SECOND_KEY_LEAF, "listkeyvalue2");
337
338         MapNode myDoublyKeyedListNode = Builders.mapBuilder()
339                 .withNodeIdentifier(new NodeIdentifier(MY_DOUBLY_KEYED_LIST))
340                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
341                         NodeIdentifierWithPredicates.of(MY_DOUBLY_KEYED_LIST, keys))
342                         .withChild(Builders.leafBuilder().withNodeIdentifier(
343                                 new NodeIdentifier(MY_LEAF_IN_LIST_3)).withValue("listleafvalue1").build()).build())
344                 .build();
345
346         AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
347                 new AugmentationIdentifier(Collections.singleton(MY_DOUBLY_KEYED_LIST)))
348                 .withChild(myDoublyKeyedListNode).build();
349
350         ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
351                 new NodeIdentifier(MY_CONTAINER_3))
352                 .withChild(myDoublyKeyedListAugNode).build();
353
354         AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
355                 new AugmentationIdentifier(Collections.singleton(MY_CONTAINER_3)))
356                 .withChild(myContainer3Node).build();
357
358         ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
359                 new NodeIdentifier(OUTER_CONTAINER))
360                 .withChild(myContainer1Node)
361                 .withChild(myContainer2Node)
362                 .withChild(myContainer3AugNode).build();
363
364         return outerContainerNode;
365     }
366 }