Make tests use UntrustedXML instead of XMLInputFactory
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / Bug8083Test.java
1 /*
2  * Copyright (c) 2017 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.assertNotNull;
12
13 import java.io.InputStream;
14 import javax.xml.stream.XMLStreamReader;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.util.xml.UntrustedXML;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 public class Bug8083Test {
28
29     @Test
30     public void testInstanceIdentifierPathWithEmptyListKey() throws Exception {
31         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug8083/yang/baz.yang");
32         final Module bazModule = schemaContext.getModules().iterator().next();
33         final ContainerSchemaNode topCont = (ContainerSchemaNode) bazModule.getDataChildByName(
34                 QName.create(bazModule.getQNameModule(), "top-cont"));
35         assertNotNull(topCont);
36
37         final InputStream resourceAsStream = Bug8083Test.class.getResourceAsStream("/bug8083/xml/baz.xml");
38
39         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
40
41         // deserialization
42         final NormalizedNodeResult result = new NormalizedNodeResult();
43         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
44         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topCont);
45         xmlParser.parse(reader);
46         final NormalizedNode<?, ?> transformedInput = result.getResult();
47         assertNotNull(transformedInput);
48     }
49
50     @Test
51     public void testInstanceIdentifierPathWithIdentityrefListKey() throws Exception {
52         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug8083/yang/zab.yang");
53         final Module zabModule = schemaContext.getModules().iterator().next();
54         final ContainerSchemaNode topCont = (ContainerSchemaNode) zabModule.getDataChildByName(
55                 QName.create(zabModule.getQNameModule(), "top-cont"));
56         assertNotNull(topCont);
57
58         final InputStream resourceAsStream = Bug8083Test.class.getResourceAsStream("/bug8083/xml/zab.xml");
59
60         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
61
62         // deserialization
63         final NormalizedNodeResult result = new NormalizedNodeResult();
64         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
65         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topCont);
66         xmlParser.parse(reader);
67         final NormalizedNode<?, ?> transformedInput = result.getResult();
68         assertNotNull(transformedInput);
69     }
70
71     @Test
72     public void testInstanceIdentifierPathWithInstanceIdentifierListKey() throws Exception {
73         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug8083/yang/foobar.yang");
74         final Module foobarModule = schemaContext.getModules().iterator().next();
75         final ContainerSchemaNode topCont = (ContainerSchemaNode) foobarModule.getDataChildByName(
76                 QName.create(foobarModule.getQNameModule(), "top-cont"));
77         assertNotNull(topCont);
78
79         final InputStream resourceAsStream = Bug8083Test.class.getResourceAsStream("/bug8083/xml/foobar.xml");
80
81         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
82
83         // deserialization
84         final NormalizedNodeResult result = new NormalizedNodeResult();
85         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
86         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topCont);
87         xmlParser.parse(reader);
88         final NormalizedNode<?, ?> transformedInput = result.getResult();
89         assertNotNull(transformedInput);
90     }
91 }