Update CursorAwareDataTree{Modification,Snapshot} API
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / DataTreeCandidatesTest.java
index c1ccfebfcb8e6dc3b83729c3cb01883951ea0d39..20361ddacff64cc772e23908b37f153153409459 100644 (file)
@@ -13,43 +13,34 @@ import static org.junit.Assert.fail;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 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.DataTreeCandidates;
+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.DataTreeModificationCursor;
+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.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class DataTreeCandidatesTest {
 
-    private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
+    private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatesTest.class);
 
-    private static final SchemaContext SCHEMA_CONTEXT;
-
-    static {
-        try {
-            SCHEMA_CONTEXT = TestModel.createTestContext();
-        } catch (ReactorException e) {
-            throw new ExceptionInInitializerError(e);
-        }
-    }
+    private static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
 
     private DataTree dataTree;
 
     @Before
     public void setUp() throws Exception {
-        dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
-        dataTree.setSchemaContext(SCHEMA_CONTEXT);
+        dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
 
         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
@@ -58,8 +49,9 @@ public class DataTreeCandidatesTest {
                         .build())
                 .build();
 
-        final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot().newModification();
-        final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
+        final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
+                .newModification();
+        final DataTreeModificationCursor cursor = modification.openCursor();
         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
         modification.ready();
 
@@ -69,16 +61,20 @@ public class DataTreeCandidatesTest {
     }
 
     @Test
-    public void testRootedCandidate() throws Exception {
-        final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
-        innerDataTree.setSchemaContext(SCHEMA_CONTEXT);
+    public void testRootedCandidate() throws DataValidationFailedException {
+        final DataTree innerDataTree = new InMemoryDataTreeFactory().create(
+            new DataTreeConfiguration.Builder(TreeType.OPERATIONAL)
+            .setMandatoryNodesValidation(true)
+            .setRootPath(TestModel.INNER_CONTAINER_PATH)
+            .setUniqueIndexes(true).build(), SCHEMA_CONTEXT);
 
         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
                 .withValue("testing-value")
                 .build();
 
-        final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot().newModification();
+        final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
+                .newModification();
         modification.write(TestModel.VALUE_PATH, leaf);
 
         modification.ready();
@@ -87,7 +83,8 @@ public class DataTreeCandidatesTest {
         dataTree.commit(candidate);
 
         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
-        final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH, candidate.getRootNode());
+        final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
+            candidate.getRootNode());
 
         try {
             // lets see if getting the identifier of the root node throws an exception
@@ -104,4 +101,4 @@ public class DataTreeCandidatesTest {
         final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
         assertEquals(readLeaf, leaf);
     }
-}
\ No newline at end of file
+}