Convert yang-data-codec-xml to JUnit5
[yangtools.git] / codec / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / YT1108Test.java
1 /*
2  * Copyright © 2020 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 java.io.StringWriter;
13 import javax.xml.stream.XMLOutputFactory;
14 import org.junit.jupiter.api.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.params.ParameterizedTest;
17 import org.junit.jupiter.params.provider.ArgumentsSource;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
22 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 import org.xmlunit.builder.DiffBuilder;
26
27 class YT1108Test {
28     private static final QName IDENT_ONE = QName.create("foo-namespace", "ident-one");
29     private static final QName IDENTITYREF_LEAF = QName.create("foo-namespace", "identityref-leaf");
30     private static final QName LEAF_CONTAINER = QName.create("foo-namespace", "leaf-container");
31     private static final QName UNION_IDENTITYREF_LEAF = QName.create("foo-namespace", "union-identityref-leaf");
32
33     private static EffectiveModelContext MODEL_CONTEXT;
34
35     @BeforeAll
36     static void beforeClass() {
37         MODEL_CONTEXT = YangParserTestUtils.parseYang("""
38             module foo {
39               namespace "foo-namespace";
40               prefix "f";
41
42               identity ident-base;
43
44               identity ident-one {
45                 base ident-base;
46               }
47
48               typedef union-type {
49                 type union {
50                   type uint8;
51                   type identityref {
52                     base ident-base;
53                   }
54                 }
55               }
56
57               container leaf-container {
58                 leaf union-identityref-leaf {
59                   type union-type;
60                 }
61                 leaf identityref-leaf {
62                   type identityref {
63                     base ident-base;
64                   }
65                 }
66               }
67             }""");
68     }
69
70     @AfterAll
71     static void afterClass() {
72         MODEL_CONTEXT = null;
73     }
74
75     @ParameterizedTest(name = "{0}")
76     @ArgumentsSource(TestFactories.class)
77     void testLeafOfIdentityRefTypeNNToXmlSerialization(final String factoryMode, final XMLOutputFactory factory)
78             throws Exception {
79         final var diff = DiffBuilder
80             .compare(serializeToXml(factory, Builders.containerBuilder()
81                 .withNodeIdentifier(NodeIdentifier.create(LEAF_CONTAINER))
82                 .withChild(Builders.leafBuilder()
83                     .withNodeIdentifier(NodeIdentifier.create(IDENTITYREF_LEAF))
84                     .withValue(IDENT_ONE)
85                     .build())
86                 .build()))
87             .withTest("""
88                 <?xml version="1.0" encoding="UTF-8"?>
89
90                 <leaf-container xmlns="foo-namespace">
91                     <identityref-leaf xmlns:prefix="foo-namespace">ident-one</identityref-leaf>
92                 </leaf-container>""")
93             .ignoreWhitespace()
94             .checkForIdentical()
95             .build();
96         assertFalse(diff.hasDifferences(), diff.toString());
97     }
98
99     @ParameterizedTest(name = "{0}")
100     @ArgumentsSource(TestFactories.class)
101     void testLeafOfUnionWithIdentityRefNNToXmlSerialization(final String factoryMode, final XMLOutputFactory factory)
102             throws Exception {
103         final var diff = DiffBuilder
104             .compare(serializeToXml(factory, Builders.containerBuilder()
105                 .withNodeIdentifier(NodeIdentifier.create(LEAF_CONTAINER))
106                 .withChild(Builders.leafBuilder()
107                     .withNodeIdentifier(NodeIdentifier.create(UNION_IDENTITYREF_LEAF))
108                     .withValue(IDENT_ONE)
109                     .build())
110                 .build()))
111             .withTest("""
112                 <?xml version="1.0" encoding="UTF-8"?>
113
114                 <leaf-container xmlns="foo-namespace">
115                     <union-identityref-leaf xmlns:prefix="foo-namespace">ident-one</union-identityref-leaf>
116                 </leaf-container>""")
117             .ignoreWhitespace()
118             .checkForIdentical()
119             .build();
120         assertFalse(diff.hasDifferences(), diff.toString());
121     }
122
123     private static String serializeToXml(final XMLOutputFactory factory, final ContainerNode normalizedNode)
124             throws Exception {
125         final var sw = new StringWriter();
126         try (var nnsw = XMLStreamNormalizedNodeStreamWriter.create(factory.createXMLStreamWriter(sw), MODEL_CONTEXT)) {
127             NormalizedNodeWriter.forStreamWriter(nnsw).write(normalizedNode);
128         }
129         return sw.toString();
130     }
131 }