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;
13 import com.google.common.collect.Lists;
14 import com.google.common.util.concurrent.Uninterruptibles;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
29 private final List<Collection<DataTreeCandidate>> changeList =
30 Lists.<Collection<DataTreeCandidate>>newArrayList();
32 private volatile CountDownLatch changeLatch;
33 private int expChangeEventCount;
35 public MockDataTreeChangeListener(int expChangeEventCount) {
36 reset(expChangeEventCount);
39 public void reset(int expChangeEventCount) {
40 changeLatch = new CountDownLatch(expChangeEventCount);
41 this.expChangeEventCount = expChangeEventCount;
42 synchronized(changeList) {
48 public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
49 synchronized(changeList) {
50 changeList.add(changes);
52 changeLatch.countDown();
55 public void waitForChangeEvents() {
56 boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
58 fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
59 expChangeEventCount, (expChangeEventCount - changeLatch.getCount())));
63 public void verifyNotifiedData(YangInstanceIdentifier... paths) {
64 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
65 synchronized(changeList) {
66 for(Collection<DataTreeCandidate> list: changeList) {
67 for(DataTreeCandidate c: list) {
68 pathSet.remove(c.getRootPath());
73 if(!pathSet.isEmpty()) {
74 fail(pathSet + " not present in " + changeList);
78 public void expectNoMoreChanges(String assertMsg) {
79 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
80 synchronized(changeList) {
81 assertEquals(assertMsg, expChangeEventCount, changeList.size());
85 public void verifyNoNotifiedData(YangInstanceIdentifier... paths) {
86 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
87 synchronized(changeList) {
88 for(Collection<DataTreeCandidate> list: changeList) {
89 for(DataTreeCandidate c: list) {
90 assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
91 pathSet.contains(c.getRootPath()));