Convert yang-data-codec-xml to JUnit5
[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.jupiter.api.Assertions.assertFalse;
11
12 import javax.xml.stream.XMLOutputFactory;
13 import javax.xml.transform.dom.DOMResult;
14 import javax.xml.transform.dom.DOMSource;
15 import org.junit.jupiter.api.AfterAll;
16 import org.junit.jupiter.api.BeforeAll;
17 import org.junit.jupiter.params.provider.ArgumentsSource;
18 import org.junit.jupiter.params.provider.MethodSource;
19 import org.opendaylight.yangtools.util.xml.UntrustedXML;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 import org.xmlunit.builder.DiffBuilder;
25
26 class Bug8745Test extends AbstractXmlTest {
27     private static EffectiveModelContext SCHEMA_CONTEXT;
28
29     @BeforeAll
30     static void beforeClass() {
31         SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
32             module foo {
33               namespace foo;
34               prefix foo;
35
36               container cont-with-attributes {
37                 leaf leaf-with-attributes {
38                   type string;
39                 }
40                 leaf-list leaf-list-with-attributes {
41                   type string;
42                 }
43                 list list-with-attributes {
44                   key list-key;
45                   leaf list-key {
46                     type string;
47                   }
48                 }
49               }
50             }""");
51     }
52
53     @AfterAll
54     static void afterClass() {
55         SCHEMA_CONTEXT = null;
56     }
57
58     @MethodSource("data")
59     @ArgumentsSource(TestFactories.class)
60     void testParsingAttributes(final String factoryMode, final XMLOutputFactory factory) throws Exception {
61         final var doc = loadDocument("/bug8745/foo.xml");
62         final var domSource = new DOMSource(doc.getDocumentElement());
63         final var domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
64         final var xmlStreamWriter = factory.createXMLStreamWriter(domResult);
65         final var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, SCHEMA_CONTEXT);
66
67         final var reader = new DOMSourceXMLStreamReader(domSource);
68         final var xmlParser = XmlParserStream.create(streamWriter,
69             Inference.ofDataTreePath(SCHEMA_CONTEXT, QName.create("foo", "cont-with-attributes")));
70         xmlParser.parse(reader);
71
72         final var diff = DiffBuilder.compare(toString(doc.getDocumentElement()))
73             .withTest(toString(domResult.getNode()))
74             .ignoreWhitespace()
75             .checkForIdentical()
76             .build();
77         assertFalse(diff.hasDifferences(), diff.toString());
78     }
79 }