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.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;
48 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
49 @SuppressWarnings("unchecked")
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());
79 reply(new RegisterDataTreeChangeListenerReply(getRef()));
82 for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
83 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
86 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
87 proxy.getListenerRegistrationActor());
89 watch(proxy.getDataChangeListenerActor());
93 // The listener registration actor should get a Close message
94 expectMsgClass(timeout, CloseDataTreeChangeListenerRegistration.class);
96 // The DataChangeListener actor should be terminated
97 expectMsgClass(timeout, Terminated.class);
106 public void testLocalShardNotFound() {
107 new JavaTestKit(getSystem()) {{
108 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
109 mock(ClusterWrapper.class), mock(Configuration.class));
111 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
112 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
114 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
118 proxy.init("shard-1", path);
123 FiniteDuration timeout = duration("5 seconds");
124 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
125 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
127 reply(new LocalShardNotFound("shard-1"));
129 expectNoMsg(duration("1 seconds"));
134 public void testLocalShardNotInitialized() {
135 new JavaTestKit(getSystem()) {{
136 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
137 mock(ClusterWrapper.class), mock(Configuration.class));
139 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
140 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
142 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
146 proxy.init("shard-1", path);
151 FiniteDuration timeout = duration("5 seconds");
152 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
153 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
155 reply(new NotInitializedException("not initialized"));
157 new Within(duration("1 seconds")) {
159 protected void run() {
167 public void testFailedRegistration() {
168 new JavaTestKit(getSystem()) {{
169 ActorSystem mockActorSystem = mock(ActorSystem.class);
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());
178 ActorContext actorContext = mock(ActorContext.class);
179 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
181 doReturn(executor).when(actorContext).getClientDispatcher();
182 doReturn(mockActorSystem).when(actorContext).getActorSystem();
184 String shardName = "shard-1";
185 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
186 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
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();
195 proxy.init("shard-1", path);
197 Assert.assertEquals("getListenerRegistrationActor", null,
198 proxy.getListenerRegistrationActor());
203 public void testCloseBeforeRegistration() {
204 new JavaTestKit(getSystem()) {{
205 ActorContext actorContext = mock(ActorContext.class);
207 String shardName = "shard-1";
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));
218 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
219 new DataTreeChangeListenerProxy<>(actorContext, mockListener);
222 Answer<Future<Object>> answer = new Answer<Future<Object>>() {
224 public Future<Object> answer(InvocationOnMock invocation) {
226 return Futures.successful((Object)new RegisterDataTreeChangeListenerReply(getRef()));
230 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
231 any(Object.class), any(Timeout.class));
233 proxy.init(shardName, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
235 expectMsgClass(duration("5 seconds"), CloseDataTreeChangeListenerRegistration.class);
237 Assert.assertEquals("getListenerRegistrationActor", null,
238 proxy.getListenerRegistrationActor());