X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Futils%2FMockDataTreeChangeListener.java;h=9ba38c2616ae5ce0cf22b80bcf54f6e7c937cf12;hb=HEAD;hp=f5a61599df4e2a5a326b7870927b96d1b23b1c0e;hpb=5464f50be733df1bbbe31cf05665d542d3b7c5e7;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/MockDataTreeChangeListener.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/MockDataTreeChangeListener.java index f5a61599df..9ba38c2616 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/MockDataTreeChangeListener.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/MockDataTreeChangeListener.java @@ -9,65 +9,121 @@ 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.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.schema.tree.DataTreeCandidate; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; +import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate; public class MockDataTreeChangeListener implements DOMDataTreeChangeListener { - private final List> changeList = - Lists.>newArrayList(); + private final List 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; + expChangeEventCount = newExpChangeEventCount; synchronized (changeList) { changeList.clear(); } } @Override - public void onDataTreeChanged(@Nonnull final Collection changes) { - synchronized (changeList) { - changeList.add(changes); + public void onDataTreeChanged(final List changes) { + if (changeLatch.getCount() > 0) { + synchronized (changeList) { + changeList.addAll(changes); + } + changeLatch.countDown(); } - changeLatch.countDown(); } - public void waitForChangeEvents() { + @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(final YangInstanceIdentifier... expPaths) { boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS); if (!done) { fail(String.format("Missing change notifications. Expected: %d. Actual: %d", expChangeEventCount, expChangeEventCount - changeLatch.getCount())); } + + for (int i = 0; i < expPaths.length; i++) { + final DataTreeCandidate candidate = changeList.get(i); + final NormalizedNode dataAfter = candidate.getRootNode().dataAfter(); + if (dataAfter == null) { + fail(String.format("Change %d does not contain data after. Actual: %s", i + 1, + candidate.getRootNode())); + } + + final Optional relativePath = expPaths[i].relativeTo(candidate.getRootPath()); + if (!relativePath.isPresent()) { + assertEquals(String.format("Change %d does not contain %s. Actual: %s", i + 1, expPaths[i], + dataAfter), expPaths[i].getLastPathArgument(), dataAfter.name()); + } else { + NormalizedNode nextChild = dataAfter; + for (PathArgument pathArg: relativePath.orElseThrow().getPathArguments()) { + boolean found = false; + if (nextChild instanceof DistinctNodeContainer) { + Optional maybeChild = ((DistinctNodeContainer)nextChild) + .findChildByArg(pathArg); + if (maybeChild.isPresent()) { + found = true; + nextChild = maybeChild.orElseThrow(); + } + } + + if (!found) { + fail(String.format("Change %d does not contain %s. Actual: %s", i + 1, expPaths[i], dataAfter)); + } + } + } + } } - public void verifyNotifiedData(YangInstanceIdentifier... paths) { + public void verifyNotifiedData(final YangInstanceIdentifier... paths) { Set pathSet = new HashSet<>(Arrays.asList(paths)); synchronized (changeList) { - for (Collection list : changeList) { - for (DataTreeCandidate c : list) { - pathSet.remove(c.getRootPath()); - } + for (DataTreeCandidate c : changeList) { + pathSet.remove(c.getRootPath()); } } @@ -76,21 +132,19 @@ 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 pathSet = new HashSet<>(Arrays.asList(paths)); synchronized (changeList) { - for (Collection list : changeList) { - for (DataTreeCandidate c : list) { - assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate", - pathSet.contains(c.getRootPath())); - } + for (DataTreeCandidate c : changeList) { + assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate", + pathSet.contains(c.getRootPath())); } } }