Add NormalizedNodeContainer.size()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ListConstraintsValidation.java
index 244fda31c5b30a9c9b836d10827a58a72810adc0..42fef9cb99cb6d09e0782a8538aa37caf650649a 100644 (file)
@@ -7,14 +7,16 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
-import com.google.common.base.Optional;
-import java.io.InputStream;
+import static org.junit.Assert.fail;
+
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -28,29 +30,21 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 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.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
 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.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class ListConstraintsValidation {
-    private static final Logger LOG = LoggerFactory.getLogger(ListConstraintsValidation.class);
-
-    private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
-    private SchemaContext schemaContext;
-
-    private InMemoryDataTree inMemoryDataTree;
-
     private static final QName MASTER_CONTAINER_QNAME = QName.create(
             "urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
             "master-container");
@@ -76,30 +70,30 @@ public class ListConstraintsValidation {
     private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier
             .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build();
 
-    @Before
-    public void prepare() throws ReactorException {
-        schemaContext = createTestContext();
-        assertNotNull("Schema context must not be null.", schemaContext);
-        inMemoryDataTree =  (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
-        inMemoryDataTree.setSchemaContext(schemaContext);
-        final InMemoryDataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
-        final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
+    private static SchemaContext schemaContext;
 
-        modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
-        modificationTree.ready();
-        inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
-    }
+    private DataTree inMemoryDataTree;
 
-    public static InputStream getDatastoreTestInputStream() {
-        return getInputStream(CONSTRAINTS_VALIDATION_TEST_YANG);
+    @BeforeClass
+    public static void beforeClass() {
+        schemaContext = YangParserTestUtils.parseYangResource("/list-constraints-validation-test-model.yang");
     }
 
-    private static InputStream getInputStream(final String resourceName) {
-        return TestModel.class.getResourceAsStream(CONSTRAINTS_VALIDATION_TEST_YANG);
+    @AfterClass
+    public static void afterClass() {
+        schemaContext = null;
     }
 
-    public static SchemaContext createTestContext() throws ReactorException {
-        return YangParserTestUtils.parseYangStreams(Collections.singletonList(getDatastoreTestInputStream()));
+    @Before
+    public void prepare() {
+        inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
+            schemaContext);
+        final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
+        final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
+
+        modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
+        modificationTree.ready();
+        inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
     }
 
     @Test
@@ -114,25 +108,24 @@ public class ListConstraintsValidation {
                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
                 .withChild(barEntryNode).build();
 
-        final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+        final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
         modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
         modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
-        // TODO: check why write and then merge on list commits only "bar" child
         modificationTree.ready();
 
         inMemoryDataTree.validate(modificationTree);
         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
         inMemoryDataTree.commit(prepare);
 
-        final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
+        final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
         final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
         assertTrue(minMaxListRead.isPresent());
-        assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
+        assertEquals(2, ((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).size());
     }
 
-    @Test(expected=DataValidationFailedException.class)
+    @Test(expected = DataValidationFailedException.class)
     public void minMaxListFail() throws DataValidationFailedException {
-        InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+        DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
 
         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
@@ -155,10 +148,10 @@ public class ListConstraintsValidation {
         DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
         inMemoryDataTree.commit(prepare1);
 
-        InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
+        DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
         Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
         assertTrue(minMaxListRead.isPresent());
-        assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
+        assertEquals(2, ((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).size());
 
         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
         modificationTree.write(gooPath, gooEntryNode);
@@ -171,7 +164,7 @@ public class ListConstraintsValidation {
         snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
         minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
         assertTrue(minMaxListRead.isPresent());
-        assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
+        assertEquals(3, ((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).size());
 
         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
 
@@ -210,20 +203,19 @@ public class ListConstraintsValidation {
         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
         inMemoryDataTree.commit(prepare1);
 
-        final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
+        final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
         final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
         assertTrue(masterContainer.isPresent());
-        final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get()).getChild(
-                new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
+        final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get())
+                .getChild(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
         assertTrue(leafList.isPresent());
-        assertTrue(leafList.get().getValue().size() == 2);
+        assertEquals(2, leafList.get().size());
     }
 
-    @Test(expected=DataValidationFailedException.class)
-    public void minMaxLeafListFail() throws DataValidationFailedException {
+    @Test
+    public void minMaxLeafListFail() {
         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
 
-        final NodeWithValue<Object> fooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "foo");
         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
         final NodeWithValue<Object> fuuPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "fuu");
@@ -246,9 +238,15 @@ public class ListConstraintsValidation {
         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
         modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
-        modificationTree.ready();
 
-        inMemoryDataTree.validate(modificationTree);
+        try {
+            modificationTree.ready();
+            fail("Should have failed with IAE");
+        } catch (IllegalArgumentException e) {
+            assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
+                    + "revision=2015-02-02)min-max-leaf-list has too many elements (4), can have at most 3",
+                    e.getMessage());
+        }
     }
 
     @Test
@@ -272,14 +270,14 @@ public class ListConstraintsValidation {
         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
         inMemoryDataTree.commit(prepare1);
 
-        final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
+        final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
         final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
         assertTrue(unkeyedListRead.isPresent());
         assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
     }
 
-    @Test(expected=DataValidationFailedException.class)
-    public void unkeyedListTestFail() throws DataValidationFailedException {
+    @Test
+    public void unkeyedListTestFail() {
         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
 
         final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
@@ -296,8 +294,12 @@ public class ListConstraintsValidation {
                 .withValue(unkeyedEntries).build();
 
         modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
-        modificationTree.ready();
-
-        inMemoryDataTree.validate(modificationTree);
+        try {
+            modificationTree.ready();
+            fail("Should have failed with IAE");
+        } catch (IllegalArgumentException e) {
+            assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
+                    + "revision=2015-02-02)unkeyed-list has too many elements (2), can have at most 1", e.getMessage());
+        }
     }
 }