Notify listeners on applySnapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeTest.java
index 5d27224398381b2f5db26eebdb0c3c711efe09d1..9ae74bb47c1b8dd17a2f3f8b8f7a51d9ecbd68b7 100644 (file)
@@ -10,28 +10,42 @@ package org.opendaylight.controller.cluster.datastore;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.reset;
 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCanCommit;
 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCommit;
 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediatePreCommit;
 import com.google.common.base.Optional;
 import com.google.common.base.Ticker;
+import com.google.common.collect.Maps;
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
+import java.util.function.Consumer;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
 import org.mockito.Mockito;
 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
 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.ModificationType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
@@ -136,6 +150,61 @@ public class ShardDataTreeTest extends AbstractTest {
         assertEquals(expected, actual);
     }
 
+    @Test
+    public void testListenerNotifiedOnApplySnapshot() throws Exception {
+        final ShardDataTree shardDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+
+        DOMDataTreeChangeListener listener = mock(DOMDataTreeChangeListener.class);
+        shardDataTree.registerTreeChangeListener(CarsModel.CAR_LIST_PATH.node(CarsModel.CAR_QNAME), listener);
+
+        addCar(shardDataTree, "optima");
+
+        verifyOnDataTreeChanged(listener, dtc -> {
+            assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
+            assertEquals("getRootPath", CarsModel.newCarPath("optima"), dtc.getRootPath());
+        });
+
+        addCar(shardDataTree, "sportage");
+
+        verifyOnDataTreeChanged(listener, dtc -> {
+            assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
+            assertEquals("getRootPath", CarsModel.newCarPath("sportage"), dtc.getRootPath());
+        });
+
+        ShardDataTree newDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+        addCar(newDataTree, "optima");
+        addCar(newDataTree, "murano");
+
+        shardDataTree.applySnapshot(newDataTree.takeStateSnapshot());
+
+        Map<YangInstanceIdentifier, ModificationType> expChanges = Maps.newHashMap();
+        expChanges.put(CarsModel.newCarPath("optima"), ModificationType.WRITE);
+        expChanges.put(CarsModel.newCarPath("murano"), ModificationType.WRITE);
+        expChanges.put(CarsModel.newCarPath("sportage"), ModificationType.DELETE);
+        verifyOnDataTreeChanged(listener, dtc -> {
+            ModificationType expType = expChanges.remove(dtc.getRootPath());
+            assertNotNull("Got unexpected change for " + dtc.getRootPath(), expType);
+            assertEquals("getModificationType", expType, dtc.getRootNode().getModificationType());
+        });
+
+        if(!expChanges.isEmpty()) {
+            fail("Missing change notifications: " + expChanges);
+        }
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private static void verifyOnDataTreeChanged(DOMDataTreeChangeListener listener, Consumer<DataTreeCandidate> callback) {
+        ArgumentCaptor<Collection> changes = ArgumentCaptor.forClass(Collection.class);
+        verify(listener, atLeastOnce()).onDataTreeChanged(changes.capture());
+        for(Collection list: changes.getAllValues()) {
+            for(Object dtc: list) {
+                callback.accept((DataTreeCandidate)dtc);
+            }
+        }
+
+        reset(listener);
+    }
+
     private static NormalizedNode<?, ?> getCars(final ShardDataTree shardDataTree) {
         final ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction(nextTransactionId());
         final DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
@@ -148,10 +217,14 @@ public class ShardDataTreeTest extends AbstractTest {
     }
 
     private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree) throws ExecutionException, InterruptedException {
+        return addCar(shardDataTree, "altima");
+    }
+
+    private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree, String name) throws ExecutionException, InterruptedException {
         return doTransaction(shardDataTree, snapshot -> {
                 snapshot.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
                 snapshot.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
-                snapshot.write(CarsModel.newCarPath("altima"), CarsModel.newCarEntry("altima", new BigInteger("100")));
+                snapshot.write(CarsModel.newCarPath(name), CarsModel.newCarEntry(name, new BigInteger("100")));
             });
     }
 
@@ -196,4 +269,4 @@ public class ShardDataTreeTest extends AbstractTest {
         return candidate;
     }
 
-}
\ No newline at end of file
+}