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