2 * Copyright (c) 2015 Cisco 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.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.raft.utils.DoNothingActor;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
43 import org.opendaylight.controller.md.sal.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;
49 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
50 private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
53 public void testSuccessfulRegistration() {
54 new JavaTestKit(getSystem()) {{
55 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
56 mock(ClusterWrapper.class), mock(Configuration.class));
58 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
59 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
61 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
65 proxy.init("shard-1", path);
70 FiniteDuration timeout = duration("5 seconds");
71 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
72 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
74 reply(new LocalShardFound(getRef()));
76 RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
77 Assert.assertEquals("getPath", path, registerMsg.getPath());
78 Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
80 reply(new RegisterDataTreeChangeListenerReply(getRef()));
83 for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
84 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
87 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
88 proxy.getListenerRegistrationActor());
90 watch(proxy.getDataChangeListenerActor());
94 // The listener registration actor should get a Close message
95 expectMsgClass(timeout, CloseDataTreeChangeListenerRegistration.class);
97 // The DataChangeListener actor should be terminated
98 expectMsgClass(timeout, Terminated.class);
107 public void testSuccessfulRegistrationForClusteredListener() {
108 new JavaTestKit(getSystem()) {{
109 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
110 mock(ClusterWrapper.class), mock(Configuration.class));
112 ClusteredDOMDataTreeChangeListener mockClusteredListener = mock(ClusteredDOMDataTreeChangeListener.class);
114 final DataTreeChangeListenerProxy<ClusteredDOMDataTreeChangeListener> proxy =
115 new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener);
117 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
121 proxy.init("shard-1", path);
126 FiniteDuration timeout = duration("5 seconds");
127 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
128 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
130 reply(new LocalShardFound(getRef()));
132 RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
133 Assert.assertEquals("getPath", path, registerMsg.getPath());
134 Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
139 public void testLocalShardNotFound() {
140 new JavaTestKit(getSystem()) {{
141 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
142 mock(ClusterWrapper.class), mock(Configuration.class));
144 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
145 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
147 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
151 proxy.init("shard-1", path);
156 FiniteDuration timeout = duration("5 seconds");
157 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
158 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
160 reply(new LocalShardNotFound("shard-1"));
162 expectNoMsg(duration("1 seconds"));
167 public void testLocalShardNotInitialized() {
168 new JavaTestKit(getSystem()) {{
169 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
170 mock(ClusterWrapper.class), mock(Configuration.class));
172 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
173 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
175 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
179 proxy.init("shard-1", path);
184 FiniteDuration timeout = duration("5 seconds");
185 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
186 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
188 reply(new NotInitializedException("not initialized"));
190 new Within(duration("1 seconds")) {
192 protected void run() {
200 public void testFailedRegistration() {
201 new JavaTestKit(getSystem()) {{
202 ActorSystem mockActorSystem = mock(ActorSystem.class);
204 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
205 "testFailedRegistration");
206 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
207 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
208 MoreExecutors.directExecutor());
211 ActorContext actorContext = mock(ActorContext.class);
212 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
214 doReturn(executor).when(actorContext).getClientDispatcher();
215 doReturn(mockActorSystem).when(actorContext).getActorSystem();
217 String shardName = "shard-1";
218 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
219 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
221 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
222 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
223 doReturn(Futures.failed(new RuntimeException("mock"))).
224 when(actorContext).executeOperationAsync(any(ActorRef.class),
225 any(Object.class), any(Timeout.class));
226 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
228 proxy.init("shard-1", path);
230 Assert.assertEquals("getListenerRegistrationActor", null,
231 proxy.getListenerRegistrationActor());
236 public void testCloseBeforeRegistration() {
237 new JavaTestKit(getSystem()) {{
238 ActorContext actorContext = mock(ActorContext.class);
240 String shardName = "shard-1";
242 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
243 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
244 doReturn(getSystem()).when(actorContext).getActorSystem();
245 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
246 doReturn(getSystem().actorSelection(getRef().path())).
247 when(actorContext).actorSelection(getRef().path());
248 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
249 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
251 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
252 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
255 Answer<Future<Object>> answer = new Answer<Future<Object>>() {
257 public Future<Object> answer(InvocationOnMock invocation) {
259 return Futures.successful((Object)new RegisterDataTreeChangeListenerReply(getRef()));
263 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
264 any(Object.class), any(Timeout.class));
266 proxy.init(shardName, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
268 expectMsgClass(duration("5 seconds"), CloseDataTreeChangeListenerRegistration.class);
270 Assert.assertEquals("getListenerRegistrationActor", null,
271 proxy.getListenerRegistrationActor());