Silence an NPE in the DataChangeListenerRegistrationProxyTest
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerRegistrationProxyTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.Mockito.any;
11 import static org.mockito.Mockito.doAnswer;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.eq;
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.Mockito;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
32 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
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.RegisterChangeListener;
37 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
38 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
39 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
40 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
41 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
42 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
43 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import scala.concurrent.ExecutionContextExecutor;
47 import scala.concurrent.Future;
48 import scala.concurrent.duration.FiniteDuration;
49
50 /**
51  * Unit tests for DataChangeListenerRegistrationProxy.
52  *
53  * @author Thomas Pantelis
54  */
55 public class DataChangeListenerRegistrationProxyTest extends AbstractActorTest {
56
57     @SuppressWarnings("unchecked")
58     private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockListener =
59             Mockito.mock(AsyncDataChangeListener.class);
60
61     @Test
62     public void testGetInstance() throws Exception {
63         DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
64                 "shard", Mockito.mock(ActorContext.class), mockListener);
65
66         Assert.assertEquals(mockListener, proxy.getInstance());
67     }
68
69     @Test(timeout=10000)
70     public void testSuccessfulRegistration() {
71         new JavaTestKit(getSystem()) {{
72             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
73                     mock(ClusterWrapper.class), mock(Configuration.class));
74
75             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
76                     "shard-1", actorContext, mockListener);
77
78             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
79             final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
80             new Thread() {
81                 @Override
82                 public void run() {
83                     proxy.init(path, scope);
84                 }
85
86             }.start();
87
88             FiniteDuration timeout = duration("5 seconds");
89             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
90             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
91
92             reply(new LocalShardFound(getRef()));
93
94             RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
95             Assert.assertEquals("getPath", path, registerMsg.getPath());
96             Assert.assertEquals("getScope", scope, registerMsg.getScope());
97
98             reply(new RegisterChangeListenerReply(getRef().path()));
99
100             for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
101                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
102             }
103
104             Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
105                     proxy.getListenerRegistrationActor());
106
107             watch(proxy.getDataChangeListenerActor());
108
109             proxy.close();
110
111             // The listener registration actor should get a Close message
112             expectMsgClass(timeout, CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
113
114             // The DataChangeListener actor should be terminated
115             expectMsgClass(timeout, Terminated.class);
116
117             proxy.close();
118
119             expectNoMsg();
120         }};
121     }
122
123     @Test(timeout=10000)
124     public void testLocalShardNotFound() {
125         new JavaTestKit(getSystem()) {{
126             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
127                     mock(ClusterWrapper.class), mock(Configuration.class));
128
129             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
130                     "shard-1", actorContext, mockListener);
131
132             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
133             final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
134             new Thread() {
135                 @Override
136                 public void run() {
137                     proxy.init(path, scope);
138                 }
139
140             }.start();
141
142             FiniteDuration timeout = duration("5 seconds");
143             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
144             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
145
146             reply(new LocalShardNotFound("shard-1"));
147
148             expectNoMsg(duration("1 seconds"));
149         }};
150     }
151
152     @Test(timeout=10000)
153     public void testLocalShardNotInitialized() {
154         new JavaTestKit(getSystem()) {{
155             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
156                     mock(ClusterWrapper.class), mock(Configuration.class));
157
158             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
159                     "shard-1", actorContext, mockListener);
160
161             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
162             final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
163             new Thread() {
164                 @Override
165                 public void run() {
166                     proxy.init(path, scope);
167                 }
168
169             }.start();
170
171             FiniteDuration timeout = duration("5 seconds");
172             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
173             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
174
175             reply(new ActorNotInitialized());
176
177             new Within(duration("1 seconds")) {
178                 @Override
179                 protected void run() {
180                     expectNoMsg();
181                 }
182             };
183         }};
184     }
185
186     @Test
187     public void testFailedRegistration() {
188         new JavaTestKit(getSystem()) {{
189             ActorSystem mockActorSystem = mock(ActorSystem.class);
190
191             ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
192                     "testFailedRegistration");
193             doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
194             ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
195                     MoreExecutors.sameThreadExecutor());
196             doReturn(executor).when(mockActorSystem).dispatcher();
197
198             ActorContext actorContext = mock(ActorContext.class);
199
200             String shardName = "shard-1";
201             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
202                     shardName, actorContext, mockListener);
203
204             doReturn(mockActorSystem).when(actorContext).getActorSystem();
205             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
206             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
207             doReturn(Futures.failed(new RuntimeException("mock"))).
208                     when(actorContext).executeOperationAsync(any(ActorRef.class),
209                             any(Object.class), any(Timeout.class));
210             doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
211
212             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
213                     AsyncDataBroker.DataChangeScope.ONE);
214
215             Assert.assertEquals("getListenerRegistrationActor", null,
216                     proxy.getListenerRegistrationActor());
217         }};
218     }
219
220     @Test
221     public void testCloseBeforeRegistration() {
222         new JavaTestKit(getSystem()) {{
223             ActorContext actorContext = mock(ActorContext.class);
224
225             String shardName = "shard-1";
226             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
227                     shardName, actorContext, mockListener);
228
229             doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
230             doReturn(getSystem()).when(actorContext).getActorSystem();
231             doReturn(getSystem().actorSelection(getRef().path())).
232                     when(actorContext).actorSelection(getRef().path());
233             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
234             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
235
236             Answer<Future<Object>> answer = new Answer<Future<Object>>() {
237                 @Override
238                 public Future<Object> answer(InvocationOnMock invocation) {
239                     proxy.close();
240                     return Futures.successful((Object)new RegisterChangeListenerReply(getRef().path()));
241                 }
242             };
243
244             doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
245                     any(Object.class), any(Timeout.class));
246
247             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
248                     AsyncDataBroker.DataChangeScope.ONE);
249
250             expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
251
252             Assert.assertEquals("getListenerRegistrationActor", null,
253                     proxy.getListenerRegistrationActor());
254         }};
255     }
256 }

©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.