2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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.Mockito;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
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.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.common.api.data.AsyncDataBroker;
43 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
44 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import scala.concurrent.ExecutionContextExecutor;
48 import scala.concurrent.Future;
49 import scala.concurrent.duration.FiniteDuration;
52 * Unit tests for DataChangeListenerRegistrationProxy.
54 * @author Thomas Pantelis
56 public class DataChangeListenerRegistrationProxyTest extends AbstractActorTest {
58 @SuppressWarnings("unchecked")
59 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockListener =
60 Mockito.mock(AsyncDataChangeListener.class);
63 public void testGetInstance() throws Exception {
64 DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
65 "shard", Mockito.mock(ActorContext.class), mockListener);
67 Assert.assertEquals(mockListener, proxy.getInstance());
71 public void testSuccessfulRegistration() {
72 new JavaTestKit(getSystem()) {{
73 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
74 mock(ClusterWrapper.class), mock(Configuration.class));
76 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
77 "shard-1", actorContext, mockListener);
79 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
80 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
84 proxy.init(path, scope);
89 FiniteDuration timeout = duration("5 seconds");
90 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
91 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
93 reply(new LocalShardFound(getRef()));
95 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
96 Assert.assertEquals("getPath", path, registerMsg.getPath());
97 Assert.assertEquals("getScope", scope, registerMsg.getScope());
99 reply(new RegisterChangeListenerReply(getRef()));
101 for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
102 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
105 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
106 proxy.getListenerRegistrationActor());
108 watch(proxy.getDataChangeListenerActor());
112 // The listener registration actor should get a Close message
113 expectMsgClass(timeout, CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
115 // The DataChangeListener actor should be terminated
116 expectMsgClass(timeout, Terminated.class);
125 public void testLocalShardNotFound() {
126 new JavaTestKit(getSystem()) {{
127 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
128 mock(ClusterWrapper.class), mock(Configuration.class));
130 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
131 "shard-1", actorContext, mockListener);
133 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
134 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
138 proxy.init(path, scope);
143 FiniteDuration timeout = duration("5 seconds");
144 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
145 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
147 reply(new LocalShardNotFound("shard-1"));
149 expectNoMsg(duration("1 seconds"));
154 public void testLocalShardNotInitialized() {
155 new JavaTestKit(getSystem()) {{
156 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
157 mock(ClusterWrapper.class), mock(Configuration.class));
159 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
160 "shard-1", actorContext, mockListener);
162 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
163 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
167 proxy.init(path, scope);
172 FiniteDuration timeout = duration("5 seconds");
173 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
174 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
176 reply(new NotInitializedException("not initialized"));
178 new Within(duration("1 seconds")) {
180 protected void run() {
188 public void testFailedRegistration() {
189 new JavaTestKit(getSystem()) {{
190 ActorSystem mockActorSystem = mock(ActorSystem.class);
192 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
193 "testFailedRegistration");
194 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
195 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
196 MoreExecutors.sameThreadExecutor());
199 ActorContext actorContext = mock(ActorContext.class);
201 doReturn(executor).when(actorContext).getClientDispatcher();
203 String shardName = "shard-1";
204 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
205 shardName, actorContext, mockListener);
207 doReturn(mockActorSystem).when(actorContext).getActorSystem();
208 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
209 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
210 doReturn(Futures.failed(new RuntimeException("mock"))).
211 when(actorContext).executeOperationAsync(any(ActorRef.class),
212 any(Object.class), any(Timeout.class));
213 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
215 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
216 AsyncDataBroker.DataChangeScope.ONE);
218 Assert.assertEquals("getListenerRegistrationActor", null,
219 proxy.getListenerRegistrationActor());
224 public void testCloseBeforeRegistration() {
225 new JavaTestKit(getSystem()) {{
226 ActorContext actorContext = mock(ActorContext.class);
228 String shardName = "shard-1";
229 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
230 shardName, actorContext, mockListener);
232 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
233 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
234 doReturn(getSystem()).when(actorContext).getActorSystem();
235 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
236 doReturn(getSystem().actorSelection(getRef().path())).
237 when(actorContext).actorSelection(getRef().path());
238 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
239 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
241 Answer<Future<Object>> answer = new Answer<Future<Object>>() {
243 public Future<Object> answer(InvocationOnMock invocation) {
245 return Futures.successful((Object)new RegisterChangeListenerReply(getRef()));
249 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
250 any(Object.class), any(Timeout.class));
252 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
253 AsyncDataBroker.DataChangeScope.ONE);
255 expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
257 Assert.assertEquals("getListenerRegistrationActor", null,
258 proxy.getListenerRegistrationActor());