Bug 3899: Milestone: Increase test coverage for Yangtools 97/34397/1
authorJakub Toth <jatoth@cisco.com>
Mon, 8 Feb 2016 15:08:11 +0000 (16:08 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 10 Feb 2016 14:30:07 +0000 (14:30 +0000)
test InstanceIdentifierForXmlCodecTest

Change-Id: I3dc7f58309ae900825a10d5ea0a878cf037c24e5
Signed-off-by: Jakub Toth <jatoth@cisco.com>
(cherry picked from commit 5b4fa9654c9013694ab57b404f0798411fdbabc4)

yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/InstanceIdentifierForXmlCodecTest.java [new file with mode: 0644]
yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/iid-test.yang [new file with mode: 0644]

diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/InstanceIdentifierForXmlCodecTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/InstanceIdentifierForXmlCodecTest.java
new file mode 100644 (file)
index 0000000..d6e412d
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. 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.impl.codec.xml;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.RetestUtils;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+@RunWith(MockitoJUnitRunner.class)
+public class InstanceIdentifierForXmlCodecTest {
+
+    public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:iid:test\">" + "<a>/cont</a>"
+            + "</input>";
+
+    private static final String NS = "urn:opendaylight:controller:iid:test";
+    private static final String REVISION = "2014-07-28";
+
+    private SchemaContext schemaContext;
+    private Element elementOrig;
+
+    @Before
+    public void setup() throws Exception {
+        final File rpcTestYang = new File(getClass().getResource("iid-test.yang").toURI());
+        this.schemaContext = RetestUtils.parseYangSources(rpcTestYang);
+
+        final YangInstanceIdentifier.NodeIdentifier container = new YangInstanceIdentifier.NodeIdentifier(
+                QName.create(InstanceIdentifierForXmlCodecTest.NS, InstanceIdentifierForXmlCodecTest.REVISION, "cont"));
+        final NormalizedNode<?, ?> data = ImmutableNodes.fromInstanceId(this.schemaContext,
+                YangInstanceIdentifier.create(container));
+        assertNotNull(data);
+
+        final Document doc = XmlDocumentUtilsTest.readXmlToDocument(XML_CONTENT);
+        this.elementOrig = XmlDocumentUtils.createElementFor(doc, data);
+        assertNotNull(this.elementOrig);
+    }
+
+    @Test
+    public void deserializeTest() throws Exception {
+        final YangInstanceIdentifier deserialize = InstanceIdentifierForXmlCodec.deserialize(this.elementOrig,
+                this.schemaContext);
+        assertEquals("/", deserialize.toString());
+    }
+
+    @Test
+    public void serializeTest() throws Exception {
+
+        final QName name = QName.create(InstanceIdentifierForXmlCodecTest.NS,
+                InstanceIdentifierForXmlCodecTest.REVISION, "cont");
+        final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(name).build();
+
+        final Element el = InstanceIdentifierForXmlCodec.serialize(id, this.elementOrig, this.schemaContext);
+        assertEquals(this.elementOrig, el);
+    }
+}
diff --git a/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/iid-test.yang b/yang/yang-data-impl/src/test/resources/org/opendaylight/yangtools/yang/data/impl/codec/xml/iid-test.yang
new file mode 100644 (file)
index 0000000..2265d2d
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. 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
+ */
+module iid-test {
+    yang-version 1;
+    namespace "urn:opendaylight:controller:iid:test";
+    prefix "iidt";
+
+    revision 2014-07-28 {
+       description "Initial test";
+    }
+
+    container cont {
+
+        list l {
+            key "id";
+
+            leaf id {
+                type string;
+            }
+        }
+    }
+}