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