a483ce76708459d744201dea34f9aa555849dd8d
[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.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;
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 XMLInputFactory factory = XMLInputFactory.newInstance();
109         final XMLStreamReader reader = factory.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 XMLInputFactory factory = XMLInputFactory.newInstance();
135         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
136
137         final NormalizedNodeResult result = new NormalizedNodeResult();
138         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
139
140         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
141         xmlParser.parse(reader);
142
143         final NormalizedNode<?, ?> transformedInput = result.getResult();
144         assertNotNull(transformedInput);
145     }
146
147     @Test
148     public void shouldFailOnDuplicateLeaf() throws XMLStreamException, IOException,
149             ParserConfigurationException, SAXException, URISyntaxException {
150         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo.xml");
151
152         final XMLInputFactory factory = XMLInputFactory.newInstance();
153         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
154
155         final NormalizedNodeResult result = new NormalizedNodeResult();
156         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
157
158         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
159         try {
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"));
164         }
165
166     }
167
168     @Test
169     public void shouldFailOnDuplicateAnyXml() throws XMLStreamException, IOException,
170             ParserConfigurationException, SAXException, URISyntaxException {
171         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml");
172
173         final XMLInputFactory factory = XMLInputFactory.newInstance();
174         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
175
176         final NormalizedNodeResult result = new NormalizedNodeResult();
177         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
178
179         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
180         try {
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"));
185         }
186     }
187
188     @Test
189     public void shouldFailOnDuplicateContainer() throws XMLStreamException, IOException,
190             ParserConfigurationException, SAXException, URISyntaxException {
191         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml");
192
193         final XMLInputFactory factory = XMLInputFactory.newInstance();
194         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
195
196         final NormalizedNodeResult result = new NormalizedNodeResult();
197         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
198
199         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema);
200         try {
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"));
205         }
206     }
207
208     @Test
209     public void shouldFailOnUnterminatedLeafElement() throws XMLStreamException, IOException,
210             ParserConfigurationException, SAXException, URISyntaxException {
211         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.xml");
212
213         final XMLInputFactory factory = XMLInputFactory.newInstance();
214         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
215
216         final NormalizedNodeResult result = new NormalizedNodeResult();
217         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
218
219         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
220         try {
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."));
226         }
227     }
228
229     @Test
230     public void shouldFailOnUnterminatedLeafElement2() throws XMLStreamException, IOException,
231             ParserConfigurationException, SAXException, URISyntaxException {
232         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml");
233
234         final XMLInputFactory factory = XMLInputFactory.newInstance();
235         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
236
237         final NormalizedNodeResult result = new NormalizedNodeResult();
238         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
239
240         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
241         try {
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>\"."));
247         }
248     }
249
250     @Test
251     public void shouldFailOnUnterminatedContainerElement() throws XMLStreamException, IOException,
252             ParserConfigurationException, SAXException, URISyntaxException {
253         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml");
254
255         final XMLInputFactory factory = XMLInputFactory.newInstance();
256         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
257
258         final NormalizedNodeResult result = new NormalizedNodeResult();
259         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
260
261         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
262         try {
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>\"."));
268         }
269     }
270
271     @Test
272     public void shouldFailOnUnknownChildNode() throws XMLStreamException, IOException,
273             ParserConfigurationException, SAXException, URISyntaxException {
274         final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml");
275
276         final XMLInputFactory factory = XMLInputFactory.newInstance();
277         final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
278
279         final NormalizedNodeResult result = new NormalizedNodeResult();
280         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
281
282         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
283         try {
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]}",
289                     ex.getMessage());
290         }
291     }
292
293     private static NormalizedNode<?, ?> buildOuterContainerNode() {
294         // my-container-1
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();
308
309         LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1))
310                 .withValue("value1").build();
311
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();
317
318         ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(
319                 new NodeIdentifier(MY_CONTAINER_1))
320                 .withChild(myKeyedListNode)
321                 .withChild(myLeaf1Node)
322                 .withChild(myLeafListNode).build();
323
324         // my-container-2
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();
329
330         LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_3))
331                 .withValue("value3").build();
332
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();
336
337         ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(
338                 new NodeIdentifier(MY_CONTAINER_2))
339                 .withChild(innerContainerNode)
340                 .withChild(myLeaf3Node)
341                 .withChild(myChoiceNode).build();
342
343         // my-container-3
344         Map<QName, Object> keys = new HashMap<>();
345         keys.put(MY_FIRST_KEY_LEAF, "listkeyvalue1");
346         keys.put(MY_SECOND_KEY_LEAF, "listkeyvalue2");
347
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())
354                 .build();
355
356         AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(
357                 new AugmentationIdentifier(Collections.singleton(MY_DOUBLY_KEYED_LIST)))
358                 .withChild(myDoublyKeyedListNode).build();
359
360         ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(
361                 new NodeIdentifier(MY_CONTAINER_3))
362                 .withChild(myDoublyKeyedListAugNode).build();
363
364         AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(
365                 new AugmentationIdentifier(Collections.singleton(MY_CONTAINER_3)))
366                 .withChild(myContainer3Node).build();
367
368         ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(
369                 new NodeIdentifier(OUTER_CONTAINER))
370                 .withChild(myContainer1Node)
371                 .withChild(myContainer2Node)
372                 .withChild(myContainer3AugNode).build();
373
374         return outerContainerNode;
375     }
376 }