238e83c445e3b00a6b6fc9ac3385667c1b62ce5c
[yangtools.git] / codec / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / Bug8745Test.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 package org.opendaylight.yangtools.yang.data.codec.xml;
9
10 import static org.junit.Assert.assertFalse;
11
12 import java.util.Collection;
13 import javax.xml.stream.XMLOutputFactory;
14 import javax.xml.transform.dom.DOMResult;
15 import javax.xml.transform.dom.DOMSource;
16 import org.junit.AfterClass;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.junit.runners.Parameterized;
21 import org.opendaylight.yangtools.util.xml.UntrustedXML;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 import org.xmlunit.builder.DiffBuilder;
27
28 @RunWith(Parameterized.class)
29 public class Bug8745Test extends AbstractXmlTest {
30     @Parameterized.Parameters(name = "{0}")
31     public static Collection<Object[]> data() {
32         return TestFactories.junitParameters();
33     }
34
35     private static EffectiveModelContext SCHEMA_CONTEXT;
36
37     private final XMLOutputFactory factory;
38
39     public Bug8745Test(final String factoryMode, final XMLOutputFactory factory) {
40         this.factory = factory;
41     }
42
43     @BeforeClass
44     public static void beforeClass() {
45         SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
46             module foo {
47               namespace foo;
48               prefix foo;
49
50               container cont-with-attributes {
51                 leaf leaf-with-attributes {
52                   type string;
53                 }
54                 leaf-list leaf-list-with-attributes {
55                   type string;
56                 }
57                 list list-with-attributes {
58                   key list-key;
59                   leaf list-key {
60                     type string;
61                   }
62                 }
63               }
64             }""");
65     }
66
67     @AfterClass
68     public static void afterClass() {
69         SCHEMA_CONTEXT = null;
70     }
71
72     @Test
73     public void testParsingAttributes() throws Exception {
74         final var doc = loadDocument("/bug8745/foo.xml");
75         final var domSource = new DOMSource(doc.getDocumentElement());
76         final var domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
77         final var xmlStreamWriter = factory.createXMLStreamWriter(domResult);
78         final var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, SCHEMA_CONTEXT);
79
80         final var reader = new DOMSourceXMLStreamReader(domSource);
81         final var xmlParser = XmlParserStream.create(streamWriter,
82             Inference.ofDataTreePath(SCHEMA_CONTEXT, QName.create("foo", "cont-with-attributes")));
83         xmlParser.parse(reader);
84
85         final var diff = DiffBuilder.compare(toString(doc.getDocumentElement()))
86             .withTest(toString(domResult.getNode()))
87             .ignoreWhitespace()
88             .checkForIdentical()
89             .build();
90         assertFalse(diff.toString(), diff.hasDifferences());
91     }
92 }