Reduce use of getChildByName()
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / StrictParsingModeTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.fail;
14
15 import java.io.InputStream;
16 import javax.xml.stream.XMLStreamReader;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.util.xml.UntrustedXML;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class StrictParsingModeTest {
30
31     @Test
32     public void testLenientParsing() throws Exception {
33         // unknown child nodes in the top-level-container node will be skipped when the strictParsing is set to false
34         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
35                 "/strict-parsing-mode-test/foo.yang");
36         final Module fooModule = schemaContext.getModules().iterator().next();
37         final ContainerSchemaNode topLevelContainer = (ContainerSchemaNode) fooModule.findDataChildByName(
38                 QName.create(fooModule.getQNameModule(), "top-level-container")).get();
39
40         final InputStream resourceAsStream = StrictParsingModeTest.class.getResourceAsStream(
41                 "/strict-parsing-mode-test/foo.xml");
42
43         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
44
45         final NormalizedNodeResult result = new NormalizedNodeResult();
46         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
47
48         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topLevelContainer, false);
49         xmlParser.parse(reader);
50
51         final NormalizedNode<?, ?> transformedInput = result.getResult();
52         assertNotNull(transformedInput);
53     }
54
55     @Test
56     public void testStrictParsing() throws Exception {
57         // should fail because strictParsing is switched on and the top-level-container node contains child nodes
58         // which are not defined in the provided YANG model
59         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
60                 "/strict-parsing-mode-test/foo.yang");
61         final Module fooModule = schemaContext.getModules().iterator().next();
62         final ContainerSchemaNode topLevelContainer = (ContainerSchemaNode) fooModule.findDataChildByName(
63                 QName.create(fooModule.getQNameModule(), "top-level-container")).get();
64
65         final InputStream resourceAsStream = StrictParsingModeTest.class.getResourceAsStream(
66                 "/strict-parsing-mode-test/foo.xml");
67
68         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
69
70         final NormalizedNodeResult result = new NormalizedNodeResult();
71         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
72
73         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topLevelContainer, true);
74         try {
75             xmlParser.parse(reader);
76             fail("IllegalStateException should have been thrown because of an unknown child node.");
77         } catch (IllegalStateException ex) {
78             assertEquals("Schema for node with name unknown-container-a and namespace foo does not exist at "
79                     + "AbsoluteSchemaPath{path=[(foo)top-level-container]}", ex.getMessage());
80         }
81     }
82 }