Convert CDS implementation to use msdal APIs
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerProxyTest.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;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15
16 import akka.actor.ActorRef;
17 import akka.actor.ActorSystem;
18 import akka.actor.Props;
19 import akka.actor.Terminated;
20 import akka.dispatch.ExecutionContexts;
21 import akka.dispatch.Futures;
22 import akka.testkit.javadsl.TestKit;
23 import akka.util.Timeout;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import com.google.common.util.concurrent.Uninterruptibles;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.mockito.stubbing.Answer;
30 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
31 import org.opendaylight.controller.cluster.datastore.config.Configuration;
32 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
33 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
34 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
35 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
36 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
37 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
38 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
39 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
40 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
43 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import scala.concurrent.ExecutionContextExecutor;
46 import scala.concurrent.Future;
47 import scala.concurrent.duration.FiniteDuration;
48
49 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
50     private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
51
52     @Test(timeout = 10000)
53     public void testSuccessfulRegistration() {
54         new TestKit(getSystem()) {
55             {
56                 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
57                         mock(Configuration.class));
58
59                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
60                 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
61                         actorContext, mockListener, path);
62
63                 new Thread(() -> proxy.init("shard-1")).start();
64
65                 FiniteDuration timeout = duration("5 seconds");
66                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
67                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
68
69                 reply(new LocalShardFound(getRef()));
70
71                 RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout,
72                         RegisterDataTreeChangeListener.class);
73                 Assert.assertEquals("getPath", path, registerMsg.getPath());
74                 Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
75
76                 reply(new RegisterDataTreeNotificationListenerReply(getRef()));
77
78                 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
79                     Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
80                 }
81
82                 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
83                         proxy.getListenerRegistrationActor());
84
85                 watch(proxy.getDataChangeListenerActor());
86
87                 proxy.close();
88
89                 // The listener registration actor should get a Close message
90                 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
91
92                 // The DataChangeListener actor should be terminated
93                 expectMsgClass(timeout, Terminated.class);
94
95                 proxy.close();
96
97                 expectNoMsg();
98             }
99         };
100     }
101
102     @Test(timeout = 10000)
103     public void testSuccessfulRegistrationForClusteredListener() {
104         new TestKit(getSystem()) {
105             {
106                 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
107                         mock(Configuration.class));
108
109                 ClusteredDOMDataTreeChangeListener mockClusteredListener = mock(
110                         ClusteredDOMDataTreeChangeListener.class);
111
112                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
113                 final DataTreeChangeListenerProxy<ClusteredDOMDataTreeChangeListener> proxy =
114                         new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener, path);
115
116                 new Thread(() -> proxy.init("shard-1")).start();
117
118                 FiniteDuration timeout = duration("5 seconds");
119                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
120                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
121
122                 reply(new LocalShardFound(getRef()));
123
124                 RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout,
125                         RegisterDataTreeChangeListener.class);
126                 Assert.assertEquals("getPath", path, registerMsg.getPath());
127                 Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
128
129                 proxy.close();
130             }
131         };
132     }
133
134     @Test(timeout = 10000)
135     public void testLocalShardNotFound() {
136         new TestKit(getSystem()) {
137             {
138                 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
139                         mock(Configuration.class));
140
141                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
142                 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
143                         actorContext, mockListener, path);
144
145                 new Thread(() -> proxy.init("shard-1")).start();
146
147                 FiniteDuration timeout = duration("5 seconds");
148                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
149                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
150
151                 reply(new LocalShardNotFound("shard-1"));
152
153                 expectNoMsg(duration("1 seconds"));
154
155                 proxy.close();
156             }
157         };
158     }
159
160     @Test(timeout = 10000)
161     public void testLocalShardNotInitialized() {
162         new TestKit(getSystem()) {
163             {
164                 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
165                         mock(Configuration.class));
166
167                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
168                 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
169                         actorContext, mockListener, path);
170
171                 new Thread(() -> proxy.init("shard-1")).start();
172
173                 FiniteDuration timeout = duration("5 seconds");
174                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
175                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
176
177                 reply(new NotInitializedException("not initialized"));
178
179                 within(duration("1 seconds"), () ->  {
180                     expectNoMsg();
181                     return null;
182                 });
183
184                 proxy.close();
185             }
186         };
187     }
188
189     @Test
190     public void testFailedRegistration() {
191         new TestKit(getSystem()) {
192             {
193                 ActorSystem mockActorSystem = mock(ActorSystem.class);
194
195                 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), "testFailedRegistration");
196                 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
197                 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
198
199                 ActorContext actorContext = mock(ActorContext.class);
200                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
201
202                 doReturn(executor).when(actorContext).getClientDispatcher();
203                 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
204                 doReturn(mockActorSystem).when(actorContext).getActorSystem();
205
206                 String shardName = "shard-1";
207                 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
208                         actorContext, mockListener, path);
209
210                 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
211                 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
212                 doReturn(Futures.failed(new RuntimeException("mock"))).when(actorContext)
213                         .executeOperationAsync(any(ActorRef.class), any(Object.class), any(Timeout.class));
214                 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
215
216                 proxy.init("shard-1");
217
218                 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
219
220                 proxy.close();
221             }
222         };
223     }
224
225     @Test
226     public void testCloseBeforeRegistration() {
227         new TestKit(getSystem()) {
228             {
229                 ActorContext actorContext = mock(ActorContext.class);
230
231                 String shardName = "shard-1";
232
233                 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
234                 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
235                 doReturn(getSystem()).when(actorContext).getActorSystem();
236                 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
237                 doReturn(getSystem().actorSelection(getRef().path())).when(actorContext)
238                         .actorSelection(getRef().path());
239                 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
240                 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
241
242                 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
243                         actorContext, mockListener, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
244
245                 Answer<Future<Object>> answer = invocation -> {
246                     proxy.close();
247                     return Futures.successful((Object) new RegisterDataTreeNotificationListenerReply(getRef()));
248                 };
249
250                 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class),
251                         any(Timeout.class));
252
253                 proxy.init(shardName);
254
255                 expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
256
257                 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
258             }
259         };
260     }
261 }