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