Convert yang-data-codec-xml to JUnit5
[yangtools.git] / codec / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / NormalizedNodesToXmlTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.yangtools.yang.data.codec.xml;
9
10 import java.util.Map;
11 import javax.xml.stream.XMLOutputFactory;
12 import javax.xml.transform.dom.DOMResult;
13 import org.custommonkey.xmlunit.Diff;
14 import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
15 import org.custommonkey.xmlunit.XMLAssert;
16 import org.custommonkey.xmlunit.XMLUnit;
17 import org.junit.jupiter.api.AfterAll;
18 import org.junit.jupiter.api.BeforeAll;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.params.ParameterizedTest;
21 import org.junit.jupiter.params.provider.ArgumentsSource;
22 import org.opendaylight.yangtools.util.xml.UntrustedXML;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35
36 public class NormalizedNodesToXmlTest extends AbstractXmlTest {
37     private QNameModule bazModule;
38
39     private QName outerContainer;
40
41     private QName myContainer1;
42     private QName myKeyedList;
43     private QName myKeyLeaf;
44     private QName myLeafInList1;
45     private QName myLeafInList2;
46     private QName myLeaf1;
47     private QName myLeafList;
48
49     private QName myContainer2;
50     private QName innerContainer;
51     private QName myLeaf2;
52     private QName myLeaf3;
53     private QName myChoice;
54     private QName myLeafInCase2;
55
56     private QName myContainer3;
57     private QName myDoublyKeyedList;
58     private QName myFirstKeyLeaf;
59     private QName mySecondKeyLeaf;
60     private QName myLeafInList3;
61
62     private static EffectiveModelContext SCHEMA_CONTEXT;
63
64     @BeforeAll
65     static void beforeClass() {
66         SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/baz.yang");
67     }
68
69     @AfterAll
70     static void afterClass() {
71         SCHEMA_CONTEXT = null;
72     }
73
74     @BeforeEach
75     void setup() {
76         bazModule = QNameModule.create(XMLNamespace.of("baz-namespace"));
77
78         outerContainer = QName.create(bazModule, "outer-container");
79
80         myContainer1 = QName.create(bazModule, "my-container-1");
81         myKeyedList = QName.create(bazModule, "my-keyed-list");
82         myKeyLeaf = QName.create(bazModule, "my-key-leaf");
83         myLeafInList1 = QName.create(bazModule, "my-leaf-in-list-1");
84         myLeafInList2 = QName.create(bazModule, "my-leaf-in-list-2");
85         myLeaf1 = QName.create(bazModule, "my-leaf-1");
86         myLeafList = QName.create(bazModule, "my-leaf-list");
87
88         myContainer2 = QName.create(bazModule, "my-container-2");
89         innerContainer = QName.create(bazModule, "inner-container");
90         myLeaf2 = QName.create(bazModule, "my-leaf-2");
91         myLeaf3 = QName.create(bazModule, "my-leaf-3");
92         myChoice = QName.create(bazModule, "my-choice");
93         myLeafInCase2 = QName.create(bazModule, "my-leaf-in-case-2");
94
95         myContainer3 = QName.create(bazModule, "my-container-3");
96         myDoublyKeyedList = QName.create(bazModule, "my-doubly-keyed-list");
97         myFirstKeyLeaf = QName.create(bazModule, "my-first-key-leaf");
98         mySecondKeyLeaf = QName.create(bazModule, "my-second-key-leaf");
99         myLeafInList3 = QName.create(bazModule, "my-leaf-in-list-3");
100     }
101
102     @ParameterizedTest(name = "{0}")
103     @ArgumentsSource(TestFactories.class)
104     void testNormalizedNodeToXmlSerialization(final String factoryMode, final XMLOutputFactory factory)
105             throws Exception {
106         final var doc = loadDocument("/baz.xml");
107         final var domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
108         final var xmlStreamWriter = factory.createXMLStreamWriter(domResult);
109         final var xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter,
110             SCHEMA_CONTEXT);
111         final var normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(xmlNormalizedNodeStreamWriter);
112
113         normalizedNodeWriter.write(buildOuterContainerNode());
114
115         XMLUnit.setIgnoreWhitespace(true);
116         XMLUnit.setNormalize(true);
117
118         final String expectedXml = toString(doc.getDocumentElement());
119         final String serializedXml = toString(domResult.getNode());
120         final Diff diff = new Diff(expectedXml, serializedXml);
121         diff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
122
123         XMLAssert.assertXMLEqual(diff, true);
124     }
125
126     private ContainerNode buildOuterContainerNode() {
127         return Builders.containerBuilder()
128             .withNodeIdentifier(new NodeIdentifier(outerContainer))
129             .withChild(Builders.containerBuilder()
130                 .withNodeIdentifier(new NodeIdentifier(myContainer1))
131                 .withChild(Builders.mapBuilder()
132                     .withNodeIdentifier(new NodeIdentifier(myKeyedList))
133                     .withChild(Builders.mapEntryBuilder()
134                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(myKeyedList, myKeyLeaf, "listkeyvalue1"))
135                         .withChild(ImmutableNodes.leafNode(myLeafInList1, "listleafvalue1"))
136                         .withChild(ImmutableNodes.leafNode(myLeafInList2, "listleafvalue2"))
137                         .build())
138                     .withChild(Builders.mapEntryBuilder()
139                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(myKeyedList, myKeyLeaf, "listkeyvalue2"))
140                         .withChild(ImmutableNodes.leafNode(myLeafInList1, "listleafvalue12"))
141                         .withChild(ImmutableNodes.leafNode(myLeafInList2, "listleafvalue22"))
142                         .build())
143                     .build())
144                 .withChild(ImmutableNodes.leafNode(myLeaf1, "value1"))
145                 .withChild(Builders.leafSetBuilder()
146                     .withNodeIdentifier(new NodeIdentifier(myLeafList))
147                     .withChild(Builders.leafSetEntryBuilder()
148                         .withNodeIdentifier(new NodeWithValue<>(myLeafList, "lflvalue1"))
149                         .withValue("lflvalue1")
150                         .build())
151                     .withChild(Builders.leafSetEntryBuilder()
152                         .withNodeIdentifier(new NodeWithValue<>(myLeafList, "lflvalue2"))
153                         .withValue("lflvalue2")
154                         .build())
155                     .build())
156                 .build())
157             .withChild(Builders.containerBuilder()
158                 .withNodeIdentifier(new NodeIdentifier(myContainer2))
159                 .withChild(Builders.containerBuilder()
160                     .withNodeIdentifier(new NodeIdentifier(innerContainer))
161                     .withChild(ImmutableNodes.leafNode(myLeaf2, "value2"))
162                     .build())
163                 .withChild(ImmutableNodes.leafNode(myLeaf3, "value3"))
164                 .withChild(Builders.choiceBuilder()
165                     .withNodeIdentifier(new NodeIdentifier(myChoice))
166                     .withChild(ImmutableNodes.leafNode(myLeafInCase2, "case2value"))
167                     .build())
168                 .build())
169             .withChild(Builders.containerBuilder()
170                 .withNodeIdentifier(new NodeIdentifier(myContainer3))
171                 .withChild(Builders.mapBuilder()
172                     .withNodeIdentifier(new NodeIdentifier(myDoublyKeyedList))
173                     .withChild(Builders.mapEntryBuilder()
174                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(myDoublyKeyedList,
175                             Map.of(myFirstKeyLeaf, "listkeyvalue1", mySecondKeyLeaf, "listkeyvalue2")))
176                         .withChild(ImmutableNodes.leafNode(myLeafInList3, "listleafvalue1"))
177                         .build())
178                     .build())
179                 .build())
180             .build();
181     }
182 }