*/
package org.opendaylight.controller.cluster.datastore.utils;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+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 org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
-
-import javax.annotation.Nonnull;
+import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import javax.annotation.Nonnull;
+import org.opendaylight.controller.md.sal.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;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
- private final List<Collection<DataTreeCandidate>> changeList =
- Collections.synchronizedList(Lists.<Collection<DataTreeCandidate>>newArrayList());
+ private final List<DataTreeCandidate> changeList = Lists.newArrayList();
private volatile CountDownLatch changeLatch;
private int expChangeEventCount;
reset(expChangeEventCount);
}
- public void reset(int expChangeEventCount) {
- changeLatch = new CountDownLatch(expChangeEventCount);
- this.expChangeEventCount = expChangeEventCount;
- changeList.clear();
+ public void reset(int newExpChangeEventCount) {
+ changeLatch = new CountDownLatch(newExpChangeEventCount);
+ this.expChangeEventCount = newExpChangeEventCount;
+ synchronized (changeList) {
+ changeList.clear();
+ }
}
@Override
public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
- changeList.add(changes);
- changeLatch.countDown();
+ if (changeLatch.getCount() > 0) {
+ synchronized (changeList) {
+ changeList.addAll(changes);
+ }
+ changeLatch.countDown();
+ }
}
- public void waitForChangeEvents() {
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ public void waitForChangeEvents(YangInstanceIdentifier... expPaths) {
boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
- if(!done) {
+ if (!done) {
fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
- expChangeEventCount, (expChangeEventCount - changeLatch.getCount())));
+ expChangeEventCount, expChangeEventCount - changeLatch.getCount()));
+ }
+
+ for (int i = 0; i < expPaths.length; i++) {
+ final DataTreeCandidate candidate = changeList.get(i);
+ final Optional<NormalizedNode<?, ?>> maybeDataAfter = candidate.getRootNode().getDataAfter();
+ if (!maybeDataAfter.isPresent()) {
+ fail(String.format("Change %d does not contain data after. Actual: %s", i + 1,
+ candidate.getRootNode()));
+ }
+
+ final NormalizedNode<?, ?> dataAfter = maybeDataAfter.get();
+ final Optional<YangInstanceIdentifier> 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.getIdentifier());
+ } else {
+ NormalizedNode<?, ?> nextChild = dataAfter;
+ for (PathArgument pathArg: relativePath.get().getPathArguments()) {
+ boolean found = false;
+ if (nextChild instanceof NormalizedNodeContainer) {
+ Optional<NormalizedNode<?, ?>> maybeChild = ((NormalizedNodeContainer)nextChild)
+ .getChild(pathArg);
+ if (maybeChild.isPresent()) {
+ found = true;
+ nextChild = maybeChild.get();
+ }
+ }
+
+ if (!found) {
+ fail(String.format("Change %d does not contain %s. Actual: %s", i + 1, expPaths[i], dataAfter));
+ }
+ }
+ }
+ }
+ }
+
+ public void verifyNotifiedData(YangInstanceIdentifier... paths) {
+ Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
+ synchronized (changeList) {
+ for (DataTreeCandidate c : changeList) {
+ pathSet.remove(c.getRootPath());
+ }
+ }
+
+ if (!pathSet.isEmpty()) {
+ fail(pathSet + " not present in " + changeList);
}
}
public void expectNoMoreChanges(String assertMsg) {
- Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
- assertEquals(assertMsg, expChangeEventCount, changeList.size());
+ Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
+ synchronized (changeList) {
+ assertEquals(assertMsg, expChangeEventCount, changeList.size());
+ }
+ }
+
+ public void verifyNoNotifiedData(YangInstanceIdentifier... paths) {
+ Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
+ synchronized (changeList) {
+ for (DataTreeCandidate c : changeList) {
+ assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
+ pathSet.contains(c.getRootPath()));
+ }
+ }
}
}