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.exceptions.NotInitializedException;
31 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration;
32 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
33 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
34 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
35 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
36 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
37 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
38 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
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.dom.api.DOMDataTreeChangeListener;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import scala.concurrent.ExecutionContextExecutor;
44 import scala.concurrent.Future;
45 import scala.concurrent.duration.FiniteDuration;
47 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
48 @SuppressWarnings("unchecked")
49 private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
52 public void testSuccessfulRegistration() {
53 new JavaTestKit(getSystem()) {{
54 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
55 mock(ClusterWrapper.class), mock(Configuration.class));
57 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
58 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
60 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
64 proxy.init("shard-1", path);
69 FiniteDuration timeout = duration("5 seconds");
70 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
71 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
73 reply(new LocalShardFound(getRef()));
75 RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
76 Assert.assertEquals("getPath", path, registerMsg.getPath());
78 reply(new RegisterDataTreeChangeListenerReply(getRef()));
81 for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
82 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
85 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
86 proxy.getListenerRegistrationActor());
88 watch(proxy.getDataChangeListenerActor());
92 // The listener registration actor should get a Close message
93 expectMsgClass(timeout, CloseDataTreeChangeListenerRegistration.class);
95 // The DataChangeListener actor should be terminated
96 expectMsgClass(timeout, Terminated.class);
105 public void testLocalShardNotFound() {
106 new JavaTestKit(getSystem()) {{
107 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
108 mock(ClusterWrapper.class), mock(Configuration.class));
110 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
111 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
113 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
117 proxy.init("shard-1", path);
122 FiniteDuration timeout = duration("5 seconds");
123 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
124 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
126 reply(new LocalShardNotFound("shard-1"));
128 expectNoMsg(duration("1 seconds"));
133 public void testLocalShardNotInitialized() {
134 new JavaTestKit(getSystem()) {{
135 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
136 mock(ClusterWrapper.class), mock(Configuration.class));
138 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
139 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
141 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
145 proxy.init("shard-1", path);
150 FiniteDuration timeout = duration("5 seconds");
151 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
152 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
154 reply(new NotInitializedException("not initialized"));
156 new Within(duration("1 seconds")) {
158 protected void run() {
166 public void testFailedRegistration() {
167 new JavaTestKit(getSystem()) {{
168 ActorSystem mockActorSystem = mock(ActorSystem.class);
170 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
171 "testFailedRegistration");
172 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
173 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
174 MoreExecutors.sameThreadExecutor());
177 ActorContext actorContext = mock(ActorContext.class);
178 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
180 doReturn(executor).when(actorContext).getClientDispatcher();
181 doReturn(mockActorSystem).when(actorContext).getActorSystem();
183 String shardName = "shard-1";
184 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
185 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
187 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
188 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
189 doReturn(Futures.failed(new RuntimeException("mock"))).
190 when(actorContext).executeOperationAsync(any(ActorRef.class),
191 any(Object.class), any(Timeout.class));
192 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
194 proxy.init("shard-1", path);
196 Assert.assertEquals("getListenerRegistrationActor", null,
197 proxy.getListenerRegistrationActor());
202 public void testCloseBeforeRegistration() {
203 new JavaTestKit(getSystem()) {{
204 ActorContext actorContext = mock(ActorContext.class);
206 String shardName = "shard-1";
208 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
209 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
210 doReturn(getSystem()).when(actorContext).getActorSystem();
211 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
212 doReturn(getSystem().actorSelection(getRef().path())).
213 when(actorContext).actorSelection(getRef().path());
214 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
215 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
217 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
218 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
221 Answer<Future<Object>> answer = new Answer<Future<Object>>() {
223 public Future<Object> answer(InvocationOnMock invocation) {
225 return Futures.successful((Object)new RegisterDataTreeChangeListenerReply(getRef()));
229 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
230 any(Object.class), any(Timeout.class));
232 proxy.init(shardName, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
234 expectMsgClass(duration("5 seconds"), CloseDataTreeChangeListenerRegistration.class);
236 Assert.assertEquals("getListenerRegistrationActor", null,
237 proxy.getListenerRegistrationActor());