Separate out RootModificationApplyOperation
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ModificationMetadataTreeTest.java
index 0f1ead33089e0b08c1ec8db868116e3350545b44..3021952039bb19ae97585aa90f8e7085d1f88707 100644 (file)
@@ -16,31 +16,25 @@ import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.ma
 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
 
-import com.google.common.base.Optional;
+import java.util.Optional;
 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.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 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.TreeType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 
-/**
- *
+/*
  * Schema structure of document is
  *
- * <pre>
  * container root { 
  *      list list-a {
  *              key leaf-a;
@@ -60,53 +54,50 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
  *              }
  *      }
  * }
- * </pre>
- *
  */
-public class ModificationMetadataTreeTest {
+public class ModificationMetadataTreeTest extends AbstractTestModelTest {
 
     private static final Short ONE_ID = 1;
     private static final Short TWO_ID = 2;
     private static final String TWO_ONE_NAME = "one";
     private static final String TWO_TWO_NAME = "two";
 
-    private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
-            .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
+    private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
+            YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+            .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
             .build();
 
-    private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
-            .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
+    private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
+            YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+            .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
             .build();
 
     private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
-            .node(TestModel.INNER_LIST_QNAME) //
-            .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
+            .node(TestModel.INNER_LIST_QNAME)
+            .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
             .build();
 
     private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
-            .node(TestModel.VALUE_QNAME) //
+            .node(TestModel.VALUE_QNAME)
             .build();
 
-    private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
-            .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
-                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
-                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME)) //
-                    .build()) //
+    private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
+            .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
+                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
+                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
+                    .build())
                     .build();
 
-    private SchemaContext schemaContext;
-    private RootModificationApplyOperation rootOper;
+    private RootApplyStrategy rootOper;
 
     @Before
-    public void prepare() throws ReactorException {
-        schemaContext = TestModel.createTestContext();
-        assertNotNull("Schema context must not be null.", schemaContext);
-        rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext, DataTreeConfiguration.DEFAULT_OPERATIONAL));
+    public void prepare() {
+        rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
+            DataTreeConfiguration.DEFAULT_OPERATIONAL));
     }
 
     /**
-     * Returns a test document
-     *
+     * Returns a test document.
      * <pre>
      * test
      *     outer-list
@@ -125,7 +116,7 @@ public class ModificationMetadataTreeTest {
     public NormalizedNode<?, ?> createDocumentOne() {
         return ImmutableContainerNodeBuilder
                 .create()
-                .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
+                .withNodeIdentifier(new NodeIdentifier(SCHEMA_CONTEXT.getQName()))
                 .withChild(createTestContainer()).build();
 
     }
@@ -142,9 +133,9 @@ public class ModificationMetadataTreeTest {
 
     @Test
     public void basicReadWrites() {
-        final DataTreeModification modificationTree = new InMemoryDataTreeModification(new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper),
-                rootOper);
+        final DataTreeModification modificationTree = new InMemoryDataTreeModification(
+            new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper), rootOper);
         final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
         assertTrue(originalBarNode.isPresent());
         assertSame(BAR_NODE, originalBarNode.get());
@@ -169,8 +160,8 @@ public class ModificationMetadataTreeTest {
         /**
          * Creates empty Snapshot with associated schema context.
          */
-        final DataTree t = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
-        t.setSchemaContext(schemaContext);
+        final DataTree t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
+            SCHEMA_CONTEXT);
 
         /**
          *
@@ -185,38 +176,20 @@ public class ModificationMetadataTreeTest {
     public void createFromEmptyState() {
 
         final DataTreeModification modificationTree = createEmptyModificationTree();
-        /**
-         * Writes empty container node to /test
-         *
-         */
+        // Writes empty container node to /test
         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
 
-        /**
-         * Writes empty list node to /test/outer-list
-         */
-        modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
+        // Writes empty list node to /test/outer-list
+        modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
+            .build());
 
-        /**
-         * Reads list node from /test/outer-list
-         */
+        // Reads list node from /test/outer-list.
         final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
-        assertTrue(potentialOuterList.isPresent());
+        assertFalse(potentialOuterList.isPresent());
 
-        /**
-         * Reads container node from /test and verifies that it contains test
-         * node
-         */
+        // Reads container node from /test and verifies that it contains test node.
         final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
-        final ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
-
-        /**
-         *
-         * Gets list from returned snapshot of /test and verifies it contains
-         * outer-list
-         *
-         */
-        assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
-
+        assertPresentAndType(potentialTest, ContainerNode.class);
     }
 
     @Test
@@ -248,5 +221,4 @@ public class ModificationMetadataTreeTest {
         assertTrue(type.isInstance(potential.get()));
         return type.cast(potential.get());
     }
-
 }