Propagate EffectiveModelContext to more places
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / MandatoryLeafTest.java
index 98cd9aef4a5108ebee633a38fd0c3b982244ea52..99eb78755b66cb12e06edf8ec458e597d70beb7e 100644 (file)
@@ -8,42 +8,46 @@
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
 
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+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.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.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 
 public class MandatoryLeafTest {
+    private static EffectiveModelContext SCHEMA_CONTEXT;
 
-    private SchemaContext schemaContext;
+    @BeforeClass
+    public static void beforeClass() {
+        SCHEMA_CONTEXT = TestModel.createTestContext("/mandatory-leaf-test.yang");
+    }
 
-    @Before
-    public void prepare() throws ReactorException {
-        schemaContext = RetestModel.createTestContext("/mandatory-leaf-test.yang");
-        assertNotNull("Schema context must not be null.", schemaContext);
+    @AfterClass
+    public static void afterClass() {
+        SCHEMA_CONTEXT = null;
     }
 
-    private InMemoryDataTree initDataTree() {
-        InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
-                TreeType.CONFIGURATION);
-        inMemoryDataTree.setSchemaContext(schemaContext);
-        return inMemoryDataTree;
+    private static DataTree initDataTree(final boolean enableValidation) {
+        return new InMemoryDataTreeFactory().create(
+                new DataTreeConfiguration.Builder(TreeType.CONFIGURATION).setMandatoryNodesValidation(enableValidation)
+                        .build(), SCHEMA_CONTEXT);
     }
 
     @Test
     public void testCorrectMandatoryLeafWrite() throws DataValidationFailedException {
-        final InMemoryDataTree inMemoryDataTree = initDataTree();
+        final DataTree inMemoryDataTree = initDataTree(true);
         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
 
         final ContainerNode container = Builders
@@ -64,7 +68,7 @@ public class MandatoryLeafTest {
                                                         leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
                                                                 "leaf-value2")).build()).build()).build();
 
-        final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+        final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
         modificationTree.write(TestModel.TEST_PATH, container);
         modificationTree.ready();
 
@@ -75,12 +79,12 @@ public class MandatoryLeafTest {
 
     @Test
     public void testCorrectMandatoryLeafChoiceWrite() throws DataValidationFailedException {
-        final InMemoryDataTree inMemoryDataTree = initDataTree();
+        final DataTree inMemoryDataTree = initDataTree(true);
         // Container write
         final ContainerNode container = Builders.containerBuilder()
                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
 
-        final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
+        final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
         modificationTree1.write(TestModel.TEST_PATH, container);
         modificationTree1.ready();
 
@@ -101,7 +105,7 @@ public class MandatoryLeafTest {
                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
                                 .build()).build();
 
-        final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
+        final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
         modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
         modificationTree2.ready();
 
@@ -112,7 +116,7 @@ public class MandatoryLeafTest {
 
     @Test(expected = IllegalArgumentException.class)
     public void testMandatoryLeafViolation() throws DataValidationFailedException {
-        final InMemoryDataTree inMemoryDataTree = initDataTree();
+        final DataTree inMemoryDataTree = initDataTree(true);
         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
 
         final ContainerNode container = Builders
@@ -130,29 +134,58 @@ public class MandatoryLeafTest {
                                                         leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
                                                                 "leaf-value2")).build()).build()).build();
         try {
-            final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+            final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
             modificationTree.write(TestModel.TEST_PATH, container);
             modificationTree.ready();
 
             inMemoryDataTree.validate(modificationTree);
             final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
             inMemoryDataTree.commit(prepare);
-        } catch (IllegalArgumentException e) {
-            assertEquals(
-                    "Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?"
+                    + "revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:"
+                    + "yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
                     e.getMessage());
             throw e;
         }
     }
 
+    @Test
+    public void testDisabledValidation() throws DataValidationFailedException {
+        final DataTree inMemoryDataTree = initDataTree(false);
+        final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
+
+        final ContainerNode container = Builders
+                .containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
+                .withChild(
+                        Builders.choiceBuilder()
+                                .withNodeIdentifier(choice1Id)
+                                .withChild(
+                                        Builders.containerBuilder()
+                                                .withNodeIdentifier(
+                                                        new NodeIdentifier(QName.create(TestModel.TEST_QNAME,
+                                                                "case2-cont")))
+                                                .withChild(
+                                                        leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
+                                                                "leaf-value2")).build()).build()).build();
+        final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+        modificationTree.write(TestModel.TEST_PATH, container);
+        modificationTree.ready();
+
+        inMemoryDataTree.validate(modificationTree);
+        final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
+        inMemoryDataTree.commit(prepare);
+    }
+
     @Test(expected = IllegalArgumentException.class)
     public void testMandatoryLeafViolationChoiceWrite() throws DataValidationFailedException {
-        final InMemoryDataTree inMemoryDataTree = initDataTree();
+        final DataTree inMemoryDataTree = initDataTree(true);
         // Container write
         final ContainerNode container = Builders.containerBuilder()
                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
 
-        final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
+        final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
         modificationTree1.write(TestModel.TEST_PATH, container);
         modificationTree1.ready();
 
@@ -173,17 +206,53 @@ public class MandatoryLeafTest {
                                 .build()).build();
 
         try {
-            final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
+            final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
             modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
             modificationTree2.ready();
             inMemoryDataTree.validate(modificationTree2);
             final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
             inMemoryDataTree.commit(prepare2);
-        } catch (IllegalArgumentException e) {
-            assertEquals(
-                    "Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?"
+                    + "revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:"
+                    + "yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
                     e.getMessage());
             throw e;
         }
     }
+
+    @Test
+    public void testDisabledValidationChoiceWrite() throws DataValidationFailedException {
+        final DataTree inMemoryDataTree = initDataTree(false);
+        // Container write
+        final ContainerNode container = Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
+
+        final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
+        modificationTree1.write(TestModel.TEST_PATH, container);
+        modificationTree1.ready();
+
+        inMemoryDataTree.validate(modificationTree1);
+        final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
+        inMemoryDataTree.commit(prepare1);
+
+        // Choice write
+        final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
+        final ChoiceNode choice = Builders
+                .choiceBuilder()
+                .withNodeIdentifier(choice1Id)
+                .withChild(
+                        Builders.containerBuilder()
+                                .withNodeIdentifier(
+                                        new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
+                                .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
+                                .build()).build();
+
+        final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
+        modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
+        modificationTree2.ready();
+        inMemoryDataTree.validate(modificationTree2);
+        final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
+        inMemoryDataTree.commit(prepare2);
+    }
 }