Bug 8291 - Unit test 45/56045/3
authorPeter Kajsa <pkajsa@cisco.com>
Wed, 26 Apr 2017 09:08:56 +0000 (11:08 +0200)
committerPeter Kajsa <pkajsa@cisco.com>
Wed, 26 Apr 2017 09:42:30 +0000 (11:42 +0200)
Added unit test for Bug 8291.

Change-Id: Iaf41b01924f3d644b94620d2b72968725b2b2177
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/Bug8291Test.java [new file with mode: 0644]
yang/yang-data-impl/src/test/resources/bug8291/foo.yang [new file with mode: 0644]

diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/Bug8291Test.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/Bug8291Test.java
new file mode 100644 (file)
index 0000000..e26df9e
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2017 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.schema.tree;
+
+import static org.junit.Assert.assertNotNull;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+
+public class Bug8291Test {
+    private static final String NS = "foo";
+    private static final String REV = "1970-01-01";
+    private static final QName ROOT = QName.create(NS, REV, "root");
+    private static final QName OUTER_LIST = QName.create(NS, REV, "outer-list");
+    private static final QName OUTER_LIST_ID = QName.create(NS, REV, "id");
+    private static final QName INNER_LIST = QName.create(NS, REV, "inner-list");
+    private SchemaContext schemaContext;
+
+    @Before
+    public void init() throws ReactorException {
+        this.schemaContext = TestModel.createTestContext("/bug8291/foo.yang");
+        assertNotNull("Schema context must not be null.", this.schemaContext);
+    }
+
+    private static InMemoryDataTree initDataTree(final SchemaContext schemaContext)
+            throws DataValidationFailedException {
+        final DataTreeConfiguration config = new DataTreeConfiguration.Builder(TreeType.CONFIGURATION).setRootPath(
+                YangInstanceIdentifier.of(ROOT).node(OUTER_LIST)).build();
+        final InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
+                config, schemaContext);
+        return inMemoryDataTree;
+    }
+
+    @Test
+    public void test() throws DataValidationFailedException {
+        final InMemoryDataTree inMemoryDataTree = initDataTree(schemaContext);
+        writeOuterListMapEntry(inMemoryDataTree);
+        writeInnerList(inMemoryDataTree);
+    }
+
+    private void writeInnerList(final InMemoryDataTree inMemoryDataTree) throws DataValidationFailedException {
+        final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+        modificationTree.write(
+                YangInstanceIdentifier.create(
+                        new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap.of(OUTER_LIST_ID, 1))).node(
+                        INNER_LIST), Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(INNER_LIST)).build());
+
+        modificationTree.ready();
+        inMemoryDataTree.validate(modificationTree);
+        final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
+        inMemoryDataTree.commit(prepare);
+    }
+
+    private static void writeOuterListMapEntry(final InMemoryDataTree inMemoryDataTree)
+            throws DataValidationFailedException {
+        final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+
+        final MapEntryNode outerListMapEntry = Builders.mapEntryBuilder()
+                .withNodeIdentifier(new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap.of(OUTER_LIST_ID, 1)))
+                .withChild(ImmutableNodes.leafNode(OUTER_LIST_ID, 1)).build();
+
+        modificationTree.write(YangInstanceIdentifier.create(new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap
+                .of(OUTER_LIST_ID, 1))), outerListMapEntry);
+        modificationTree.ready();
+
+        inMemoryDataTree.validate(modificationTree);
+        final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
+        inMemoryDataTree.commit(prepare);
+    }
+}
diff --git a/yang/yang-data-impl/src/test/resources/bug8291/foo.yang b/yang/yang-data-impl/src/test/resources/bug8291/foo.yang
new file mode 100644 (file)
index 0000000..3c7fc93
--- /dev/null
@@ -0,0 +1,23 @@
+module odl-datastore-test {
+    yang-version 1;
+    namespace "foo";
+    prefix foo;
+
+    container root {
+        list outer-list {
+            key "id";
+            leaf id {
+                type uint16;
+            }
+           list inner-list {
+                key name;
+                leaf name {
+                    type string;
+                }
+                leaf value {
+                    type string;
+                }
+            }
+        }
+    }
+}
\ No newline at end of file