Remove use of {String,UUID}Identifier
[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 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;
19 import java.util.Set;
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;
26
27 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
28
29     private final List<Collection<DataTreeCandidate>> changeList =
30             Lists.<Collection<DataTreeCandidate>>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 expChangeEventCount) {
40         changeLatch = new CountDownLatch(expChangeEventCount);
41         this.expChangeEventCount = expChangeEventCount;
42         synchronized(changeList) {
43             changeList.clear();
44         }
45     }
46
47     @Override
48     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
49         synchronized(changeList) {
50             changeList.add(changes);
51         }
52         changeLatch.countDown();
53     }
54
55     public void waitForChangeEvents() {
56         boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
57         if(!done) {
58             fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
59                     expChangeEventCount, (expChangeEventCount - changeLatch.getCount())));
60         }
61     }
62
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());
69                 }
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(Collection<DataTreeCandidate> list: changeList) {
89                 for(DataTreeCandidate c: list) {
90                     assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
91                             pathSet.contains(c.getRootPath()));
92                 }
93             }
94         }
95     }
96 }