2 * Copyright © 2020 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.xml;
10 import static org.junit.Assert.assertFalse;
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;
29 @RunWith(Parameterized.class)
30 public class YT1108Test {
31 @Parameterized.Parameters(name = "{0}")
32 public static Collection<Object[]> data() {
33 return TestFactories.junitParameters();
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");
41 private static EffectiveModelContext MODEL_CONTEXT;
43 private final XMLOutputFactory factory;
45 public YT1108Test(final String factoryMode, final XMLOutputFactory factory) {
46 this.factory = factory;
50 public static void beforeClass() {
51 MODEL_CONTEXT = YangParserTestUtils.parseYang("""
53 namespace "foo-namespace";
71 container leaf-container {
72 leaf union-identityref-leaf {
75 leaf identityref-leaf {
85 public static void afterClass() {
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))
100 <?xml version="1.0" encoding="UTF-8"?>
102 <leaf-container xmlns="foo-namespace">
103 <identityref-leaf xmlns:prefix="foo-namespace">ident-one</identityref-leaf>
104 </leaf-container>""")
108 assertFalse(diff.toString(), diff.hasDifferences());
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)
122 <?xml version="1.0" encoding="UTF-8"?>
124 <leaf-container xmlns="foo-namespace">
125 <union-identityref-leaf xmlns:prefix="foo-namespace">ident-one</union-identityref-leaf>
126 </leaf-container>""")
130 assertFalse(diff.toString(), diff.hasDifferences());
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);
138 return sw.toString();