Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / PruningDataTreeModificationTest.java
index c078c94637aea11b944ec48d6c5e0d32d8d3a9f1..18949ac21ecd5ec3d491933b9f15e0041a8f79fe 100644 (file)
@@ -22,11 +22,10 @@ import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.T
 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerNode;
 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNode;
 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNodeEntry;
-import com.google.common.base.Optional;
+
 import com.google.common.reflect.Reflection;
-import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
+import java.util.Optional;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
@@ -44,12 +43,12 @@ import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 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.DataTreeCandidateTip;
+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.DataTreeModificationCursor;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
 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.ImmutableContainerNodeBuilder;
@@ -67,28 +66,26 @@ public class PruningDataTreeModificationTest {
     @Mock
     private DataTreeModification mockModification;
 
-    private TipProducingDataTree dataTree;
+    private DataTree dataTree;
     private DataTreeModification realModification;
     private DataTreeModification proxyModification;
     private PruningDataTreeModification pruningDataTreeModification;
 
     @Before
-    public void setUp(){
+    @SuppressWarnings("checkstyle:avoidHidingCauseException")
+    public void setUp() {
         MockitoAnnotations.initMocks(this);
 
-        dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
-        dataTree.setSchemaContext(SCHEMA_CONTEXT);
+        dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
+            SCHEMA_CONTEXT);
 
         realModification = dataTree.takeSnapshot().newModification();
-        proxyModification = Reflection.newProxy(DataTreeModification.class, new InvocationHandler() {
-            @Override
-            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
-                try {
-                    method.invoke(mockModification, args);
-                    return method.invoke(realModification, args);
-                } catch (InvocationTargetException e) {
-                    throw e.getCause();
-                }
+        proxyModification = Reflection.newProxy(DataTreeModification.class, (proxy, method, args) -> {
+            try {
+                method.invoke(mockModification, args);
+                return method.invoke(realModification, args);
+            } catch (InvocationTargetException e) {
+                throw e.getCause();
             }
         });
 
@@ -96,14 +93,14 @@ public class PruningDataTreeModificationTest {
     }
 
     @Test
-    public void testDelete(){
+    public void testDelete() {
         pruningDataTreeModification.delete(CarsModel.BASE_PATH);
 
         verify(mockModification, times(1)).delete(CarsModel.BASE_PATH);
     }
 
     @Test
-    public void testDeleteOnException(){
+    public void testDeleteOnException() {
         YangInstanceIdentifier path = CarsModel.BASE_PATH;
         doThrow(SchemaValidationFailedException.class).when(mockModification).delete(path);
 
@@ -114,7 +111,7 @@ public class PruningDataTreeModificationTest {
 
 
     @Test
-    public void testMerge(){
+    public void testMerge() {
         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
         YangInstanceIdentifier path = CarsModel.BASE_PATH;
         pruningDataTreeModification.merge(path, normalizedNode);
@@ -123,7 +120,7 @@ public class PruningDataTreeModificationTest {
     }
 
     @Test
-    public void testMergeWithInvalidNamespace() throws DataValidationFailedException{
+    public void testMergeWithInvalidNamespace() throws DataValidationFailedException {
         NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
         YangInstanceIdentifier path = PeopleModel.BASE_PATH;
 
@@ -131,20 +128,20 @@ public class PruningDataTreeModificationTest {
 
         verify(mockModification, times(1)).merge(path, normalizedNode);
 
-        DataTreeCandidateTip candidate = getCandidate();
+        DataTreeCandidate candidate = getCandidate();
         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
     }
 
     @Test
-    public void testMergeWithInvalidChildNodeNames() throws DataValidationFailedException{
+    public void testMergeWithInvalidChildNodeNames() throws DataValidationFailedException {
         ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
                 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
                         ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
 
         DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
-        ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
-                new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).withChild(augContainer).
-                            withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).build();
+        ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
+                .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).build();
 
         YangInstanceIdentifier path = TestModel.TEST_PATH;
 
@@ -156,12 +153,12 @@ public class PruningDataTreeModificationTest {
                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).build();
 
         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
-        assertEquals("After pruning present", true, actual.isPresent());
+        assertTrue("After pruning present", actual.isPresent());
         assertEquals("After pruning", prunedNode, actual.get());
     }
 
     @Test
-    public void testMergeWithValidNamespaceAndInvalidNodeName() throws DataValidationFailedException{
+    public void testMergeWithValidNamespaceAndInvalidNodeName() throws DataValidationFailedException {
         NormalizedNode<?, ?> normalizedNode = ImmutableNodes.containerNode(INVALID_TEST_QNAME);
         YangInstanceIdentifier path = INVALID_TEST_PATH;
 
@@ -169,12 +166,12 @@ public class PruningDataTreeModificationTest {
 
         verify(mockModification, times(1)).merge(path, normalizedNode);
 
-        DataTreeCandidateTip candidate = getCandidate();
+        DataTreeCandidate candidate = getCandidate();
         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
     }
 
     @Test
-    public void testWrite(){
+    public void testWrite() {
         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
         YangInstanceIdentifier path = CarsModel.BASE_PATH;
         pruningDataTreeModification.write(path, normalizedNode);
@@ -184,8 +181,8 @@ public class PruningDataTreeModificationTest {
 
     @Test
     public void testWriteRootNode() throws Exception {
-        final DataTree localDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
-        localDataTree.setSchemaContext(SCHEMA_CONTEXT);
+        final DataTree localDataTree = new InMemoryDataTreeFactory().create(
+            DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
 
         DataTreeModification mod = localDataTree.takeSnapshot().newModification();
         mod.write(CarsModel.BASE_PATH, CarsModel.create());
@@ -198,7 +195,7 @@ public class PruningDataTreeModificationTest {
         dataTree.commit(getCandidate());
 
         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
-        assertEquals("Root present", true, actual.isPresent());
+        assertTrue("Root present", actual.isPresent());
         assertEquals("Root node", normalizedNode, actual.get());
     }
 
@@ -222,7 +219,7 @@ public class PruningDataTreeModificationTest {
     }
 
     @Test
-    public void testWriteWithInvalidNamespace() throws DataValidationFailedException{
+    public void testWriteWithInvalidNamespace() throws DataValidationFailedException {
         NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
         YangInstanceIdentifier path = PeopleModel.BASE_PATH;
 
@@ -230,21 +227,21 @@ public class PruningDataTreeModificationTest {
 
         verify(mockModification, times(1)).write(path, normalizedNode);
 
-        DataTreeCandidateTip candidate = getCandidate();
+        DataTreeCandidate candidate = getCandidate();
         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
     }
 
     @Test
-    public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException{
+    public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException {
         ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
                 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
                         ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
 
         DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
-        ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
-                new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).withChild(augContainer).
-                            withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).
-                                withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
+        ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
+                .withChild(augContainer).withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug"))
+                .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
 
         YangInstanceIdentifier path = TestModel.TEST_PATH;
 
@@ -252,24 +249,24 @@ public class PruningDataTreeModificationTest {
 
         dataTree.commit(getCandidate());
 
-        ContainerNode prunedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
-                new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).
-                        withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
+        ContainerNode prunedNode = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode)
+                .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
 
         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
-        assertEquals("After pruning present", true, actual.isPresent());
+        assertTrue("After pruning present", actual.isPresent());
         assertEquals("After pruning", prunedNode, actual.get());
     }
 
     @Test
-    public void testReady(){
+    public void testReady() {
         pruningDataTreeModification.ready();
 
         verify(mockModification).ready();
     }
 
     @Test
-    public void testApplyToCursor(){
+    public void testApplyToCursor() {
         DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
         pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
 
@@ -277,26 +274,26 @@ public class PruningDataTreeModificationTest {
     }
 
     @Test
-    public void testReadNode(){
+    public void testReadNode() {
         pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
 
         verify(mockModification).readNode(CarsModel.BASE_PATH);
     }
 
     @Test
-    public void testNewModification(){
+    public void testNewModification() {
         realModification.ready();
         DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
 
-        assertTrue("new modification not of type PruningDataTreeModification", dataTreeModification instanceof PruningDataTreeModification);
+        assertTrue("new modification not of type PruningDataTreeModification",
+                dataTreeModification instanceof PruningDataTreeModification);
     }
 
-    private DataTreeCandidateTip getCandidate() throws DataValidationFailedException {
+    private DataTreeCandidate getCandidate() throws DataValidationFailedException {
         pruningDataTreeModification.ready();
-        DataTreeModification mod = pruningDataTreeModification.getResultingModification();
+        DataTreeModification mod = pruningDataTreeModification.delegate();
         mod = mod == proxyModification ? realModification : mod;
         dataTree.validate(mod);
-        DataTreeCandidateTip candidate = dataTree.prepare(mod);
-        return candidate;
+        return dataTree.prepare(mod);
     }
 }