Add test for union with identity XML codec issue 36/90236/6
authorAnna Bencurova <Anna.Bencurova@pantheon.tech>
Wed, 3 Jun 2020 16:28:36 +0000 (18:28 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 4 Jun 2020 14:06:11 +0000 (14:06 +0000)
JIRA: YANGTOOLS-1108
Change-Id: Ib33d1865c138474eae8b6fbf04c50688d8a4809a
Signed-off-by: Anna Bencurova <Anna.Bencurova@pantheon.tech>
yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1108Test.java [new file with mode: 0644]
yang/yang-data-codec-xml/src/test/resources/yt1108/xml/foo-leaf-of-identity-ref-type.xml [new file with mode: 0644]
yang/yang-data-codec-xml/src/test/resources/yt1108/xml/foo-leaf-of-union-with-identity-ref-type.xml [new file with mode: 0644]
yang/yang-data-codec-xml/src/test/resources/yt1108/yang/foo.yang [new file with mode: 0644]

diff --git a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1108Test.java b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1108Test.java
new file mode 100644 (file)
index 0000000..75242ef
--- /dev/null
@@ -0,0 +1,187 @@
+/*
+ * Copyright © 2020 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.codec.xml;
+
+import static java.util.Objects.requireNonNull;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.net.URI;
+import java.util.Collection;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLTestCase;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.opendaylight.yangtools.util.xml.UntrustedXML;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+@RunWith(Parameterized.class)
+public class YT1108Test {
+    @Parameterized.Parameters(name = "{0}")
+    public static Collection<Object[]> data() {
+        return TestFactories.junitParameters();
+    }
+
+    private QNameModule fooModule;
+    private QName fooLeafContainer;
+    private QName fooIdentity;
+    private QName fooUnionIdentityRefLeaf;
+    private QName fooIdentityRefLeaf;
+
+    private static EffectiveModelContext SCHEMA_CONTEXT;
+
+    private final XMLOutputFactory factory;
+
+    public YT1108Test(final String factoryMode, final XMLOutputFactory factory) {
+        this.factory = factory;
+    }
+
+    @BeforeClass
+    public static void beforeClass() {
+        SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yt1108/yang/foo.yang");
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        SCHEMA_CONTEXT = null;
+    }
+
+    @Before
+    public void setup() {
+        fooModule = QNameModule.create(URI.create("foo-namespace"));
+        fooLeafContainer = QName.create(fooModule, "leaf-container");
+
+        fooIdentity = QName.create(fooModule, "ident-one");
+        fooUnionIdentityRefLeaf = QName.create(fooModule, "union-identityref-leaf");
+        fooIdentityRefLeaf = QName.create(fooModule, "identityref-leaf");
+    }
+
+    @Test
+    public void testLeafOfIdentityRefTypeNNToXmlSerialization()
+            throws XMLStreamException, IOException, SAXException {
+        final Document doc = loadDocument("/yt1108/xml/foo-leaf-of-identity-ref-type.xml");
+        final DOMResult domResult = convertNormalizedNodeToXml(buildLeafContainerNodeWithIdentityRefLeaf());
+
+        XMLUnit.setIgnoreWhitespace(true);
+        XMLUnit.setNormalize(true);
+
+        final String expectedXml = toString(doc.getDocumentElement());
+        final String serializedXml = toString(domResult.getNode());
+        final Diff diff = new Diff(expectedXml, serializedXml);
+
+        new XMLTestCase() {}.assertXMLEqual(diff, true);
+    }
+
+    @Test
+    public void testLeafOfUnionWithIdentityRefNNToXmlSerialization()
+            throws XMLStreamException, IOException, SAXException {
+        final Document doc = loadDocument("/yt1108/xml/foo-leaf-of-union-with-identity-ref-type.xml");
+        final DOMResult domResult = convertNormalizedNodeToXml(buildLeafContainerNodeWithUnionIdentityRefLeaf());
+
+        XMLUnit.setIgnoreWhitespace(true);
+        XMLUnit.setNormalize(true);
+
+        final String expectedXml = toString(doc.getDocumentElement());
+        final String serializedXml = toString(domResult.getNode());
+        final Diff diff = new Diff(expectedXml, serializedXml);
+
+        new XMLTestCase() {}.assertXMLEqual(diff, true);
+    }
+
+    private DOMResult convertNormalizedNodeToXml(NormalizedNode<?, ?> normalizedNode)
+            throws XMLStreamException, IOException {
+        final DOMResult domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
+
+        final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);
+
+        final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(
+                xmlStreamWriter, SCHEMA_CONTEXT);
+
+        final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
+                xmlNormalizedNodeStreamWriter);
+
+        normalizedNodeWriter.write(normalizedNode);
+        return domResult;
+    }
+
+    private NormalizedNode<?, ?> buildLeafContainerNodeWithIdentityRefLeaf() {
+        return Builders.containerBuilder()
+                .withNodeIdentifier(NodeIdentifier.create(fooLeafContainer))
+                .withChild(Builders.leafBuilder()
+                        .withNodeIdentifier(NodeIdentifier.create(fooIdentityRefLeaf))
+                        .withValue(fooIdentity)
+                        .build())
+                .build();
+    }
+
+    private NormalizedNode<?, ?> buildLeafContainerNodeWithUnionIdentityRefLeaf() {
+        return Builders.containerBuilder()
+                .withNodeIdentifier(NodeIdentifier.create(fooLeafContainer))
+                .withChild(Builders.leafBuilder()
+                        .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifier.create(fooUnionIdentityRefLeaf))
+                        .withValue(fooIdentity)
+                        .build())
+                .build();
+    }
+
+    private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
+        final InputStream resourceAsStream = NormalizedNodesToXmlTest.class.getResourceAsStream(xmlPath);
+        return requireNonNull(readXmlToDocument(resourceAsStream));
+    }
+
+    private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
+        final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
+        doc.getDocumentElement().normalize();
+        return doc;
+    }
+
+    private static String toString(final Node xml) {
+        try {
+            final Transformer transformer = TransformerFactory.newInstance().newTransformer();
+            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
+
+            final StreamResult result = new StreamResult(new StringWriter());
+            final DOMSource source = new DOMSource(xml);
+            transformer.transform(source, result);
+
+            return result.getWriter().toString();
+        } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
+            throw new RuntimeException("Unable to serialize xml element " + xml, e);
+        }
+    }
+}
diff --git a/yang/yang-data-codec-xml/src/test/resources/yt1108/xml/foo-leaf-of-identity-ref-type.xml b/yang/yang-data-codec-xml/src/test/resources/yt1108/xml/foo-leaf-of-identity-ref-type.xml
new file mode 100644 (file)
index 0000000..c5672f4
--- /dev/null
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<leaf-container xmlns="foo-namespace">
+    <identityref-leaf xmlns:prefix="foo-namespace">ident-one</identityref-leaf>
+</leaf-container>
\ No newline at end of file
diff --git a/yang/yang-data-codec-xml/src/test/resources/yt1108/xml/foo-leaf-of-union-with-identity-ref-type.xml b/yang/yang-data-codec-xml/src/test/resources/yt1108/xml/foo-leaf-of-union-with-identity-ref-type.xml
new file mode 100644 (file)
index 0000000..677c1b8
--- /dev/null
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<leaf-container xmlns="foo-namespace">
+    <union-identityref-leaf xmlns:prefix="foo-namespace">ident-one</union-identityref-leaf>
+</leaf-container>
\ No newline at end of file
diff --git a/yang/yang-data-codec-xml/src/test/resources/yt1108/yang/foo.yang b/yang/yang-data-codec-xml/src/test/resources/yt1108/yang/foo.yang
new file mode 100644 (file)
index 0000000..403a81b
--- /dev/null
@@ -0,0 +1,30 @@
+module foo {
+    namespace "foo-namespace";
+    prefix "f";
+
+    identity ident-base;
+    identity ident-one {
+        base ident-base;
+    }
+
+    typedef union-type {
+        type union {
+            type uint8;
+            type identityref {
+                base ident-base;
+            }
+        }
+    }
+
+    container leaf-container {
+        leaf union-identityref-leaf {
+            type union-type;
+        }
+
+        leaf identityref-leaf {
+            type identityref {
+                base ident-base;
+            }
+        }
+    }
+}
\ No newline at end of file