Add DataSchemaContextTreeTest 15/71515/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Apr 2018 14:59:41 +0000 (16:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Apr 2018 15:04:48 +0000 (17:04 +0200)
DataSchemaContextTree is completely untested, this patch adds some
unit tests.

Change-Id: I68b3c77080fa1b155ec28ca6ced4d15039437ec8
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-data-util/src/test/java/org/opendaylight/yangtools/yang/data/util/codec/DataSchemaContextTreeTest.java [new file with mode: 0644]
yang/yang-data-util/src/test/resources/dataschemacontext.yang [new file with mode: 0644]

diff --git a/yang/yang-data-util/src/test/java/org/opendaylight/yangtools/yang/data/util/codec/DataSchemaContextTreeTest.java b/yang/yang-data-util/src/test/java/org/opendaylight/yangtools/yang/data/util/codec/DataSchemaContextTreeTest.java
new file mode 100644 (file)
index 0000000..cca8dcd
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2018 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.util.codec;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.net.URI;
+import java.util.Optional;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+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.util.DataSchemaContextTree;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class DataSchemaContextTreeTest {
+    private static final QNameModule MODULE = QNameModule.create(URI.create("dataschemacontext"));
+    private static final QName FOO = QName.create(MODULE, "foo");
+    private static final QName BAR = QName.create(MODULE, "bar");
+    private static final QName BAZ = QName.create(MODULE, "baz");
+    private static DataSchemaContextTree CONTEXT;
+
+    @BeforeClass
+    public static void init() {
+        CONTEXT = DataSchemaContextTree.from(YangParserTestUtils.parseYangResource("/dataschemacontext.yang"));
+    }
+
+    @AfterClass
+    public static void cleanup() {
+        CONTEXT = null;
+    }
+
+    @Test
+    public void testCorrectInput() {
+        assertTrue(CONTEXT.findChild(YangInstanceIdentifier.of(FOO)).isPresent());
+        assertTrue(CONTEXT.findChild(YangInstanceIdentifier.of(FOO).node(BAR)).isPresent());
+        assertTrue(CONTEXT.findChild(YangInstanceIdentifier.of(FOO).node(BAR).node(BAZ)).isPresent());
+
+        assertNotNull(CONTEXT.getChild(YangInstanceIdentifier.of(FOO)));
+        assertNotNull(CONTEXT.getChild(YangInstanceIdentifier.of(FOO).node(BAR)));
+        assertNotNull(CONTEXT.getChild(YangInstanceIdentifier.of(FOO).node(BAR).node(BAZ)));
+    }
+
+    @Test
+    public void testSimpleBad() {
+        assertEquals(Optional.empty(), CONTEXT.findChild(YangInstanceIdentifier.of(BAR)));
+        assertNull(CONTEXT.getChild(YangInstanceIdentifier.of(BAR)));
+    }
+
+    @Test
+    public void testNestedBad() {
+        assertEquals(Optional.empty(), CONTEXT.findChild(YangInstanceIdentifier.of(BAR).node(BAZ)));
+        assertNull(CONTEXT.getChild(YangInstanceIdentifier.of(BAR).node(BAZ)));
+    }
+}
diff --git a/yang/yang-data-util/src/test/resources/dataschemacontext.yang b/yang/yang-data-util/src/test/resources/dataschemacontext.yang
new file mode 100644 (file)
index 0000000..a8ec4cf
--- /dev/null
@@ -0,0 +1,12 @@
+module dataschemacontext {
+    namespace "dataschemacontext";
+    prefix dsc;
+
+    container foo {
+        choice bar {
+            leaf baz {
+                type string;
+            }
+        }
+    }
+}