Complete implementation of DataChangeListenerRegistration and related classes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerRegistrationTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
6 import com.google.common.util.concurrent.ListeningExecutorService;
7 import com.google.common.util.concurrent.MoreExecutors;
8 import org.junit.Test;
9 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
10 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistrationReply;
11 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
15 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 import static org.junit.Assert.assertEquals;
20
21 public class DataChangeListenerRegistrationTest extends AbstractActorTest {
22   private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
23
24   private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
25
26   static {
27     store.onGlobalContextUpdated(TestModel.createTestContext());
28   }
29
30
31   @Test
32   public void testOnReceiveCloseListenerRegistration() throws Exception {
33     new JavaTestKit(getSystem()) {{
34       final Props props = DataChangeListenerRegistration.props(store
35           .registerChangeListener(TestModel.TEST_PATH, noOpDataChangeListener(),
36               AsyncDataBroker.DataChangeScope.BASE));
37       final ActorRef subject = getSystem().actorOf(props, "testCloseListenerRegistration");
38
39       new Within(duration("1 seconds")) {
40         protected void run() {
41
42           subject.tell(new CloseDataChangeListenerRegistration(), getRef());
43
44           final String out = new ExpectMsg<String>("match hint") {
45             // do not put code outside this method, will run afterwards
46             protected String match(Object in) {
47               if (in instanceof CloseDataChangeListenerRegistrationReply) {
48                 return "match";
49               } else {
50                 throw noMatch();
51               }
52             }
53           }.get(); // this extracts the received message
54
55           assertEquals("match", out);
56
57           expectNoMsg();
58         }
59
60
61       };
62     }};
63   }
64
65   private  AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener(){
66     return new AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>() {
67       @Override
68       public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {
69
70       }
71     };
72   }
73
74 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.