Refactor Register*ListenerReply classes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerProxyTest.java
index 0dc0706a24f588f1c753a1d1b0ae7d55d22cb667..927f9297979453589d2e2fa59c96ce82910df3c1 100644 (file)
@@ -12,6 +12,7 @@ import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.actor.Props;
@@ -25,19 +26,20 @@ import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.concurrent.TimeUnit;
 import org.junit.Assert;
 import org.junit.Test;
-import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.opendaylight.controller.cluster.datastore.config.Configuration;
 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
-import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration;
+import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
-import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
+import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
+import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import scala.concurrent.ExecutionContextExecutor;
@@ -45,196 +47,241 @@ import scala.concurrent.Future;
 import scala.concurrent.duration.FiniteDuration;
 
 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
-    @SuppressWarnings("unchecked")
     private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
 
-    @Test(timeout=10000)
+    @Test(timeout = 10000)
     public void testSuccessfulRegistration() {
-        new JavaTestKit(getSystem()) {{
-            ActorContext actorContext = new ActorContext(getSystem(), getRef(),
-                    mock(ClusterWrapper.class), mock(Configuration.class));
-
-            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
-                    new DataTreeChangeListenerProxy<>(actorContext, mockListener);
-
-            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
-            new Thread() {
-                @Override
-                public void run() {
-                    proxy.init("shard-1", path);
+        new JavaTestKit(getSystem()) {
+            {
+                ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
+                        mock(Configuration.class));
+
+                final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+                final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
+                        actorContext, mockListener, path);
+
+                new Thread() {
+                    @Override
+                    public void run() {
+                        proxy.init("shard-1");
+                    }
+
+                }.start();
+
+                FiniteDuration timeout = duration("5 seconds");
+                FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
+                Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+
+                reply(new LocalShardFound(getRef()));
+
+                RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout,
+                        RegisterDataTreeChangeListener.class);
+                Assert.assertEquals("getPath", path, registerMsg.getPath());
+                Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
+
+                reply(new RegisterDataTreeNotificationListenerReply(getRef()));
+
+                for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
+                    Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
                 }
 
-            }.start();
+                Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
+                        proxy.getListenerRegistrationActor());
 
-            FiniteDuration timeout = duration("5 seconds");
-            FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
-            Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+                watch(proxy.getDataChangeListenerActor());
 
-            reply(new LocalShardFound(getRef()));
+                proxy.close();
 
-            RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
-            Assert.assertEquals("getPath", path, registerMsg.getPath());
+                // The listener registration actor should get a Close message
+                expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
 
-            reply(new RegisterDataTreeChangeListenerReply(getRef()));
+                // The DataChangeListener actor should be terminated
+                expectMsgClass(timeout, Terminated.class);
 
+                proxy.close();
 
-            for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
-                Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
+                expectNoMsg();
             }
+        };
+    }
+
+    @Test(timeout = 10000)
+    public void testSuccessfulRegistrationForClusteredListener() {
+        new JavaTestKit(getSystem()) {
+            {
+                ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
+                        mock(Configuration.class));
 
-            Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
-                    proxy.getListenerRegistrationActor());
+                ClusteredDOMDataTreeChangeListener mockClusteredListener = mock(
+                        ClusteredDOMDataTreeChangeListener.class);
 
-            watch(proxy.getDataChangeListenerActor());
+                final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+                final DataTreeChangeListenerProxy<ClusteredDOMDataTreeChangeListener> proxy =
+                        new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener, path);
 
-            proxy.close();
+                new Thread() {
+                    @Override
+                    public void run() {
+                        proxy.init("shard-1");
+                    }
 
-            // The listener registration actor should get a Close message
-            expectMsgClass(timeout, CloseDataTreeChangeListenerRegistration.class);
+                }.start();
 
-            // The DataChangeListener actor should be terminated
-            expectMsgClass(timeout, Terminated.class);
+                FiniteDuration timeout = duration("5 seconds");
+                FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
+                Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
 
-            proxy.close();
+                reply(new LocalShardFound(getRef()));
 
-            expectNoMsg();
-        }};
+                RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout,
+                        RegisterDataTreeChangeListener.class);
+                Assert.assertEquals("getPath", path, registerMsg.getPath());
+                Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
+
+                proxy.close();
+            }
+        };
     }
 
-    @Test(timeout=10000)
+    @Test(timeout = 10000)
     public void testLocalShardNotFound() {
-        new JavaTestKit(getSystem()) {{
-            ActorContext actorContext = new ActorContext(getSystem(), getRef(),
-                    mock(ClusterWrapper.class), mock(Configuration.class));
-
-            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
-                    new DataTreeChangeListenerProxy<>(actorContext, mockListener);
-
-            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
-            new Thread() {
-                @Override
-                public void run() {
-                    proxy.init("shard-1", path);
-                }
+        new JavaTestKit(getSystem()) {
+            {
+                ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
+                        mock(Configuration.class));
 
-            }.start();
+                final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+                final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
+                        actorContext, mockListener, path);
 
-            FiniteDuration timeout = duration("5 seconds");
-            FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
-            Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+                new Thread() {
+                    @Override
+                    public void run() {
+                        proxy.init("shard-1");
+                    }
 
-            reply(new LocalShardNotFound("shard-1"));
+                }.start();
 
-            expectNoMsg(duration("1 seconds"));
-        }};
+                FiniteDuration timeout = duration("5 seconds");
+                FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
+                Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+
+                reply(new LocalShardNotFound("shard-1"));
+
+                expectNoMsg(duration("1 seconds"));
+
+                proxy.close();
+            }
+        };
     }
 
-    @Test(timeout=10000)
+    @Test(timeout = 10000)
     public void testLocalShardNotInitialized() {
-        new JavaTestKit(getSystem()) {{
-            ActorContext actorContext = new ActorContext(getSystem(), getRef(),
-                    mock(ClusterWrapper.class), mock(Configuration.class));
-
-            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
-                    new DataTreeChangeListenerProxy<>(actorContext, mockListener);
-
-            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
-            new Thread() {
-                @Override
-                public void run() {
-                    proxy.init("shard-1", path);
-                }
+        new JavaTestKit(getSystem()) {
+            {
+                ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
+                        mock(Configuration.class));
 
-            }.start();
+                final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+                final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
+                        actorContext, mockListener, path);
 
-            FiniteDuration timeout = duration("5 seconds");
-            FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
-            Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+                new Thread() {
+                    @Override
+                    public void run() {
+                        proxy.init("shard-1");
+                    }
 
-            reply(new NotInitializedException("not initialized"));
+                }.start();
 
-            new Within(duration("1 seconds")) {
-                @Override
-                protected void run() {
-                    expectNoMsg();
-                }
-            };
-        }};
+                FiniteDuration timeout = duration("5 seconds");
+                FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
+                Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+
+                reply(new NotInitializedException("not initialized"));
+
+                new Within(duration("1 seconds")) {
+                    @Override
+                    protected void run() {
+                        expectNoMsg();
+                    }
+                };
+
+                proxy.close();
+            }
+        };
     }
 
     @Test
     public void testFailedRegistration() {
-        new JavaTestKit(getSystem()) {{
-            ActorSystem mockActorSystem = mock(ActorSystem.class);
+        new JavaTestKit(getSystem()) {
+            {
+                ActorSystem mockActorSystem = mock(ActorSystem.class);
 
-            ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
-                    "testFailedRegistration");
-            doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
-            ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
-                    MoreExecutors.sameThreadExecutor());
+                ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), "testFailedRegistration");
+                doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
+                ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
 
+                ActorContext actorContext = mock(ActorContext.class);
+                final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
 
-            ActorContext actorContext = mock(ActorContext.class);
-            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+                doReturn(executor).when(actorContext).getClientDispatcher();
+                doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
+                doReturn(mockActorSystem).when(actorContext).getActorSystem();
 
-            doReturn(executor).when(actorContext).getClientDispatcher();
-            doReturn(mockActorSystem).when(actorContext).getActorSystem();
+                String shardName = "shard-1";
+                final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
+                        actorContext, mockListener, path);
 
-            String shardName = "shard-1";
-            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
-                    new DataTreeChangeListenerProxy<>(actorContext, mockListener);
+                doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
+                doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
+                doReturn(Futures.failed(new RuntimeException("mock"))).when(actorContext)
+                        .executeOperationAsync(any(ActorRef.class), any(Object.class), any(Timeout.class));
+                doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
 
-            doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
-            doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
-            doReturn(Futures.failed(new RuntimeException("mock"))).
-                    when(actorContext).executeOperationAsync(any(ActorRef.class),
-                    any(Object.class), any(Timeout.class));
-            doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
+                proxy.init("shard-1");
 
-            proxy.init("shard-1", path);
+                Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
 
-            Assert.assertEquals("getListenerRegistrationActor", null,
-                    proxy.getListenerRegistrationActor());
-        }};
+                proxy.close();
+            }
+        };
     }
 
     @Test
     public void testCloseBeforeRegistration() {
-        new JavaTestKit(getSystem()) {{
-            ActorContext actorContext = mock(ActorContext.class);
+        new JavaTestKit(getSystem()) {
+            {
+                ActorContext actorContext = mock(ActorContext.class);
 
-            String shardName = "shard-1";
+                String shardName = "shard-1";
 
-            doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
-            doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
-            doReturn(getSystem()).when(actorContext).getActorSystem();
-            doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
-            doReturn(getSystem().actorSelection(getRef().path())).
-                    when(actorContext).actorSelection(getRef().path());
-            doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
-            doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
+                doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
+                doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
+                doReturn(getSystem()).when(actorContext).getActorSystem();
+                doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
+                doReturn(getSystem().actorSelection(getRef().path())).when(actorContext)
+                        .actorSelection(getRef().path());
+                doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
+                doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
 
-            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
-                    new DataTreeChangeListenerProxy<>(actorContext, mockListener);
+                final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
+                        actorContext, mockListener, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
 
-
-            Answer<Future<Object>> answer = new Answer<Future<Object>>() {
-                @Override
-                public Future<Object> answer(InvocationOnMock invocation) {
+                Answer<Future<Object>> answer = invocation -> {
                     proxy.close();
-                    return Futures.successful((Object)new RegisterDataTreeChangeListenerReply(getRef()));
-                }
-            };
+                    return Futures.successful((Object) new RegisterDataTreeNotificationListenerReply(getRef()));
+                };
 
-            doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
-                    any(Object.class), any(Timeout.class));
+                doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class),
+                        any(Timeout.class));
 
-            proxy.init(shardName, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
+                proxy.init(shardName);
 
-            expectMsgClass(duration("5 seconds"), CloseDataTreeChangeListenerRegistration.class);
+                expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
 
-            Assert.assertEquals("getListenerRegistrationActor", null,
-                    proxy.getListenerRegistrationActor());
-        }};
+                Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
+            }
+        };
     }
 }