Renamed controller.md.sal.binding.api to mdsal.binding.api
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / AbstractDataChangeListenerTest.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.dom.adapter.test;
9
10 import org.opendaylight.mdsal.binding.api.DataChangeListener;
11
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import com.google.common.util.concurrent.SettableFuture;
21
22 public abstract class AbstractDataChangeListenerTest extends AbstractDataBrokerTest {
23
24     protected static final class TestListener implements DataChangeListener {
25
26         private final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject>> event;
27         private boolean capture = false;
28
29         private TestListener() {
30             event = SettableFuture.create();
31         }
32
33         @Override
34         public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg) {
35             if (capture) {
36                 event.set(arg);
37             }
38         }
39
40         public AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event() {
41             try {
42                 return event.get(500, TimeUnit.MILLISECONDS);
43             } catch (InterruptedException | TimeoutException | ExecutionException e) {
44                 throw new IllegalStateException(e);
45             }
46         }
47
48         public boolean hasEvent() {
49             return event.isDone();
50         }
51
52         public void startCapture() {
53             this.capture = true;
54         }
55     }
56
57     protected final TestListener createListener(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
58             final DataChangeScope scope) {
59         TestListener listener = new TestListener();
60         getDataBroker().registerDataChangeListener(store, path, listener, scope);
61         listener.startCapture();
62         return listener;
63     }
64 }