Fix unit test CS warnings in sal-distributed-datastore
[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<Collection<DataTreeCandidate>> changeList =
31             Lists.<Collection<DataTreeCandidate>>newArrayList();
32
33     private volatile CountDownLatch changeLatch;
34     private int expChangeEventCount;
35
36     public MockDataTreeChangeListener(int expChangeEventCount) {
37         reset(expChangeEventCount);
38     }
39
40     public void reset(int newExpChangeEventCount) {
41         changeLatch = new CountDownLatch(newExpChangeEventCount);
42         this.expChangeEventCount = newExpChangeEventCount;
43         synchronized (changeList) {
44             changeList.clear();
45         }
46     }
47
48     @Override
49     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
50         synchronized (changeList) {
51             changeList.add(changes);
52         }
53         changeLatch.countDown();
54     }
55
56     public void waitForChangeEvents() {
57         boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
58         if (!done) {
59             fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
60                     expChangeEventCount, expChangeEventCount - changeLatch.getCount()));
61         }
62     }
63
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());
70                 }
71             }
72         }
73
74         if (!pathSet.isEmpty()) {
75             fail(pathSet + " not present in " + changeList);
76         }
77     }
78
79     public void expectNoMoreChanges(String assertMsg) {
80         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
81         synchronized (changeList) {
82             assertEquals(assertMsg, expChangeEventCount, changeList.size());
83         }
84     }
85
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()));
93                 }
94             }
95         }
96     }
97 }