Make tests use UntrustedXML instead of XMLInputFactory
[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.getDataChildByName(
38                 QName.create(fooModule.getQNameModule(),
39                 "top-level-container"));
40
41         final InputStream resourceAsStream = StrictParsingModeTest.class.getResourceAsStream(
42                 "/strict-parsing-mode-test/foo.xml");
43
44         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
45
46         final NormalizedNodeResult result = new NormalizedNodeResult();
47         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
48
49         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topLevelContainer, false);
50         xmlParser.parse(reader);
51
52         final NormalizedNode<?, ?> transformedInput = result.getResult();
53         assertNotNull(transformedInput);
54     }
55
56     @Test
57     public void testStrictParsing() throws Exception {
58         // should fail because strictParsing is switched on and the top-level-container node contains child nodes
59         // which are not defined in the provided YANG model
60         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
61                 "/strict-parsing-mode-test/foo.yang");
62         final Module fooModule = schemaContext.getModules().iterator().next();
63         final ContainerSchemaNode topLevelContainer = (ContainerSchemaNode) fooModule.getDataChildByName(
64                 QName.create(fooModule.getQNameModule(), "top-level-container"));
65
66         final InputStream resourceAsStream = StrictParsingModeTest.class.getResourceAsStream(
67                 "/strict-parsing-mode-test/foo.xml");
68
69         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
70
71         final NormalizedNodeResult result = new NormalizedNodeResult();
72         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
73
74         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topLevelContainer, true);
75         try {
76             xmlParser.parse(reader);
77             fail("IllegalStateException should have been thrown because of an unknown child node.");
78         } catch (IllegalStateException ex) {
79             assertEquals("Schema for node with name unknown-container-a and namespace foo does not exist at "
80                     + "AbsoluteSchemaPath{path=[(foo)top-level-container]}", ex.getMessage());
81         }
82     }
83 }