2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.utils;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.fail;
14 import com.google.common.collect.Lists;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.TimeUnit;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
28 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
30 private final List<Collection<DataTreeCandidate>> changeList =
31 Lists.<Collection<DataTreeCandidate>>newArrayList();
33 private volatile CountDownLatch changeLatch;
34 private int expChangeEventCount;
36 public MockDataTreeChangeListener(int expChangeEventCount) {
37 reset(expChangeEventCount);
40 public void reset(int newExpChangeEventCount) {
41 changeLatch = new CountDownLatch(newExpChangeEventCount);
42 this.expChangeEventCount = newExpChangeEventCount;
43 synchronized (changeList) {
49 public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
50 synchronized (changeList) {
51 changeList.add(changes);
53 changeLatch.countDown();
56 public void waitForChangeEvents() {
57 boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
59 fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
60 expChangeEventCount, expChangeEventCount - changeLatch.getCount()));
64 public void verifyNotifiedData(YangInstanceIdentifier... paths) {
65 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
66 synchronized (changeList) {
67 for (Collection<DataTreeCandidate> list : changeList) {
68 for (DataTreeCandidate c : list) {
69 pathSet.remove(c.getRootPath());
74 if (!pathSet.isEmpty()) {
75 fail(pathSet + " not present in " + changeList);
79 public void expectNoMoreChanges(String assertMsg) {
80 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
81 synchronized (changeList) {
82 assertEquals(assertMsg, expChangeEventCount, changeList.size());
86 public void verifyNoNotifiedData(YangInstanceIdentifier... paths) {
87 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
88 synchronized (changeList) {
89 for (Collection<DataTreeCandidate> list : changeList) {
90 for (DataTreeCandidate c : list) {
91 assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
92 pathSet.contains(c.getRootPath()));