Merge "Stop SubtreeFitler from rereading reply even if no filter element is present."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerProxyTest.java
1 /*
2  * Copyright (c) 2015 Cisco 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.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;
46
47 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
48     @SuppressWarnings("unchecked")
49     private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
50
51     @Test(timeout=10000)
52     public void testSuccessfulRegistration() {
53         new JavaTestKit(getSystem()) {{
54             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
55                     mock(ClusterWrapper.class), mock(Configuration.class));
56
57             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
58                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
59
60             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
61             new Thread() {
62                 @Override
63                 public void run() {
64                     proxy.init("shard-1", path);
65                 }
66
67             }.start();
68
69             FiniteDuration timeout = duration("5 seconds");
70             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
71             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
72
73             reply(new LocalShardFound(getRef()));
74
75             RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
76             Assert.assertEquals("getPath", path, registerMsg.getPath());
77
78             reply(new RegisterDataTreeChangeListenerReply(getRef()));
79
80
81             for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
82                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
83             }
84
85             Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
86                     proxy.getListenerRegistrationActor());
87
88             watch(proxy.getDataChangeListenerActor());
89
90             proxy.close();
91
92             // The listener registration actor should get a Close message
93             expectMsgClass(timeout, CloseDataTreeChangeListenerRegistration.class);
94
95             // The DataChangeListener actor should be terminated
96             expectMsgClass(timeout, Terminated.class);
97
98             proxy.close();
99
100             expectNoMsg();
101         }};
102     }
103
104     @Test(timeout=10000)
105     public void testLocalShardNotFound() {
106         new JavaTestKit(getSystem()) {{
107             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
108                     mock(ClusterWrapper.class), mock(Configuration.class));
109
110             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
111                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
112
113             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
114             new Thread() {
115                 @Override
116                 public void run() {
117                     proxy.init("shard-1", path);
118                 }
119
120             }.start();
121
122             FiniteDuration timeout = duration("5 seconds");
123             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
124             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
125
126             reply(new LocalShardNotFound("shard-1"));
127
128             expectNoMsg(duration("1 seconds"));
129         }};
130     }
131
132     @Test(timeout=10000)
133     public void testLocalShardNotInitialized() {
134         new JavaTestKit(getSystem()) {{
135             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
136                     mock(ClusterWrapper.class), mock(Configuration.class));
137
138             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
139                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
140
141             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
142             new Thread() {
143                 @Override
144                 public void run() {
145                     proxy.init("shard-1", path);
146                 }
147
148             }.start();
149
150             FiniteDuration timeout = duration("5 seconds");
151             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
152             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
153
154             reply(new NotInitializedException("not initialized"));
155
156             new Within(duration("1 seconds")) {
157                 @Override
158                 protected void run() {
159                     expectNoMsg();
160                 }
161             };
162         }};
163     }
164
165     @Test
166     public void testFailedRegistration() {
167         new JavaTestKit(getSystem()) {{
168             ActorSystem mockActorSystem = mock(ActorSystem.class);
169
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());
175
176
177             ActorContext actorContext = mock(ActorContext.class);
178             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
179
180             doReturn(executor).when(actorContext).getClientDispatcher();
181             doReturn(mockActorSystem).when(actorContext).getActorSystem();
182
183             String shardName = "shard-1";
184             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
185                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
186
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();
193
194             proxy.init("shard-1", path);
195
196             Assert.assertEquals("getListenerRegistrationActor", null,
197                     proxy.getListenerRegistrationActor());
198         }};
199     }
200
201     @Test
202     public void testCloseBeforeRegistration() {
203         new JavaTestKit(getSystem()) {{
204             ActorContext actorContext = mock(ActorContext.class);
205
206             String shardName = "shard-1";
207
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));
216
217             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
218                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
219
220
221             Answer<Future<Object>> answer = new Answer<Future<Object>>() {
222                 @Override
223                 public Future<Object> answer(InvocationOnMock invocation) {
224                     proxy.close();
225                     return Futures.successful((Object)new RegisterDataTreeChangeListenerReply(getRef()));
226                 }
227             };
228
229             doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
230                     any(Object.class), any(Timeout.class));
231
232             proxy.init(shardName, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
233
234             expectMsgClass(duration("5 seconds"), CloseDataTreeChangeListenerRegistration.class);
235
236             Assert.assertEquals("getListenerRegistrationActor", null,
237                     proxy.getListenerRegistrationActor());
238         }};
239     }
240 }