Migrate from YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / MockDataTreeChangeListener.java
index b414e0f269f9b19ea868a05e61d0692f9a5f573d..fecfc52fb6a57a036b009ef89502c8ed26c199a9 100644 (file)
@@ -9,20 +9,21 @@ package org.opendaylight.controller.cluster.datastore.utils;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.Uninterruptibles;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
-import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -31,16 +32,19 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 
 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
 
-    private final List<DataTreeCandidate> changeList = Lists.newArrayList();
+    private final List<DataTreeCandidate> changeList = new ArrayList<>();
+
+    private final CountDownLatch onInitialDataLatch = new CountDownLatch(1);
+    private final AtomicInteger onInitialDataEventCount = new AtomicInteger();
 
     private volatile CountDownLatch changeLatch;
     private int expChangeEventCount;
 
-    public MockDataTreeChangeListener(int expChangeEventCount) {
+    public MockDataTreeChangeListener(final int expChangeEventCount) {
         reset(expChangeEventCount);
     }
 
-    public void reset(int newExpChangeEventCount) {
+    public void reset(final int newExpChangeEventCount) {
         changeLatch = new CountDownLatch(newExpChangeEventCount);
         this.expChangeEventCount = newExpChangeEventCount;
         synchronized (changeList) {
@@ -49,7 +53,7 @@ public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
     }
 
     @Override
-    public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
+    public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
         if (changeLatch.getCount() > 0) {
             synchronized (changeList) {
                 changeList.addAll(changes);
@@ -58,8 +62,25 @@ public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
         }
     }
 
+    @Override
+    public void onInitialData() {
+        onInitialDataEventCount.incrementAndGet();
+        onInitialDataLatch.countDown();
+    }
+
+    public void verifyOnInitialDataEvent() {
+        assertTrue("onInitialData was not triggered",
+                Uninterruptibles.awaitUninterruptibly(onInitialDataLatch, 5, TimeUnit.SECONDS));
+        assertEquals("onInitialDataEventCount", 1, onInitialDataEventCount.get());
+    }
+
+    public void verifyNoOnInitialDataEvent() {
+        assertFalse("onInitialData was triggered unexpectedly",
+                Uninterruptibles.awaitUninterruptibly(onInitialDataLatch, 500, TimeUnit.MILLISECONDS));
+    }
+
     @SuppressWarnings({ "unchecked", "rawtypes" })
-    public void waitForChangeEvents(YangInstanceIdentifier... expPaths) {
+    public void waitForChangeEvents(final YangInstanceIdentifier... expPaths) {
         boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
         if (!done) {
             fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
@@ -100,7 +121,7 @@ public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
         }
     }
 
-    public void verifyNotifiedData(YangInstanceIdentifier... paths) {
+    public void verifyNotifiedData(final YangInstanceIdentifier... paths) {
         Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
         synchronized (changeList) {
             for (DataTreeCandidate c : changeList) {
@@ -113,14 +134,14 @@ public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
         }
     }
 
-    public void expectNoMoreChanges(String assertMsg) {
+    public void expectNoMoreChanges(final String assertMsg) {
         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
         synchronized (changeList) {
             assertEquals(assertMsg, expChangeEventCount, changeList.size());
         }
     }
 
-    public void verifyNoNotifiedData(YangInstanceIdentifier... paths) {
+    public void verifyNoNotifiedData(final YangInstanceIdentifier... paths) {
         Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
         synchronized (changeList) {
             for (DataTreeCandidate c : changeList) {