4be431554118484f6760a0e78f72f0f82bfba86d
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / MockDataTreeChangeListener.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.fail;
13
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;
20 import java.util.Set;
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;
27
28 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
29
30     private final List<DataTreeCandidate> changeList = Lists.newArrayList();
31
32     private volatile CountDownLatch changeLatch;
33     private int expChangeEventCount;
34
35     public MockDataTreeChangeListener(int expChangeEventCount) {
36         reset(expChangeEventCount);
37     }
38
39     public void reset(int newExpChangeEventCount) {
40         changeLatch = new CountDownLatch(newExpChangeEventCount);
41         this.expChangeEventCount = newExpChangeEventCount;
42         synchronized (changeList) {
43             changeList.clear();
44         }
45     }
46
47     @Override
48     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
49         if (changeLatch.getCount() > 0) {
50             synchronized (changeList) {
51                 changeList.addAll(changes);
52             }
53             changeLatch.countDown();
54         }
55     }
56
57     public void waitForChangeEvents() {
58         boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
59         if (!done) {
60             fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
61                     expChangeEventCount, expChangeEventCount - changeLatch.getCount()));
62         }
63     }
64
65     public void verifyNotifiedData(YangInstanceIdentifier... paths) {
66         Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
67         synchronized (changeList) {
68             for (DataTreeCandidate c : changeList) {
69                 pathSet.remove(c.getRootPath());
70             }
71         }
72
73         if (!pathSet.isEmpty()) {
74             fail(pathSet + " not present in " + changeList);
75         }
76     }
77
78     public void expectNoMoreChanges(String assertMsg) {
79         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
80         synchronized (changeList) {
81             assertEquals(assertMsg, expChangeEventCount, changeList.size());
82         }
83     }
84
85     public void verifyNoNotifiedData(YangInstanceIdentifier... paths) {
86         Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
87         synchronized (changeList) {
88             for (DataTreeCandidate c : changeList) {
89                 assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
90                         pathSet.contains(c.getRootPath()));
91             }
92         }
93     }
94 }