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;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
-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;
@Test
public void testGetInstance() throws Exception {
- DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
- "shard", Mockito.mock(ActorContext.class), mockListener);
-
- Assert.assertEquals(mockListener, proxy.getInstance());
+ try (DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
+ "shard", Mockito.mock(ActorContext.class), mockListener)) {
+ Assert.assertEquals(mockListener, proxy.getInstance());
+ }
}
- @Test(timeout=10000)
+ @Test(timeout = 10000)
public void testSuccessfulRegistration() {
- new JavaTestKit(getSystem()) {{
- ActorContext actorContext = new ActorContext(getSystem(), getRef(),
- mock(ClusterWrapper.class), mock(Configuration.class));
+ new JavaTestKit(getSystem()) {
+ {
+ ActorContext actorContext = new ActorContext(getSystem(), getRef(),
+ mock(ClusterWrapper.class), mock(Configuration.class));
- final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
- "shard-1", actorContext, mockListener);
+ final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
+ "shard-1", actorContext, mockListener);
- final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
- final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
- new Thread() {
- @Override
- public void run() {
- proxy.init(path, scope);
- }
+ final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+ final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
+ new Thread() {
+ @Override
+ public void run() {
+ proxy.init(path, scope);
+ }
- }.start();
+ }.start();
- FiniteDuration timeout = duration("5 seconds");
- FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
- Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+ FiniteDuration timeout = duration("5 seconds");
+ FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
+ Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
- reply(new LocalShardFound(getRef()));
+ reply(new LocalShardFound(getRef()));
- RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
- Assert.assertEquals("getPath", path, registerMsg.getPath());
- Assert.assertEquals("getScope", scope, registerMsg.getScope());
- Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
+ RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
+ Assert.assertEquals("getPath", path, registerMsg.getPath());
+ Assert.assertEquals("getScope", scope, registerMsg.getScope());
+ Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
- reply(new RegisterChangeListenerReply(getRef()));
+ reply(new RegisterChangeListenerReply(getRef()));
- for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
- Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
- }
+ for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
+ Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
+ }
- Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
- proxy.getListenerRegistrationActor());
+ Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
+ proxy.getListenerRegistrationActor());
- watch(proxy.getDataChangeListenerActor());
+ watch(proxy.getDataChangeListenerActor());
- proxy.close();
+ proxy.close();
- // The listener registration actor should get a Close message
- expectMsgClass(timeout, CloseDataChangeListenerRegistration.class);
+ // The listener registration actor should get a Close message
+ expectMsgClass(timeout, CloseDataChangeListenerRegistration.class);
- // The DataChangeListener actor should be terminated
- expectMsgClass(timeout, Terminated.class);
+ // The DataChangeListener actor should be terminated
+ expectMsgClass(timeout, Terminated.class);
- proxy.close();
+ proxy.close();
- expectNoMsg();
- }};
+ expectNoMsg();
+ }
+ };
}
- @Test(timeout=10000)
+ @Test(timeout = 10000)
public void testSuccessfulRegistrationForClusteredListener() {
- new JavaTestKit(getSystem()) {{
- ActorContext actorContext = new ActorContext(getSystem(), getRef(),
- mock(ClusterWrapper.class), mock(Configuration.class));
-
- AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockClusteredListener =
- Mockito.mock(ClusteredDOMDataChangeListener.class);
-
- final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
- "shard-1", actorContext, mockClusteredListener);
-
- final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
- final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
- new Thread() {
- @Override
- public void run() {
- proxy.init(path, scope);
- }
-
- }.start();
-
- FiniteDuration timeout = duration("5 seconds");
- FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
- Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+ new JavaTestKit(getSystem()) {
+ {
+ ActorContext actorContext = new ActorContext(getSystem(), getRef(),
+ mock(ClusterWrapper.class), mock(Configuration.class));
- reply(new LocalShardFound(getRef()));
+ AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockClusteredListener =
+ Mockito.mock(ClusteredDOMDataChangeListener.class);
- RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
- Assert.assertEquals("getPath", path, registerMsg.getPath());
- Assert.assertEquals("getScope", scope, registerMsg.getScope());
- Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
+ final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
+ "shard-1", actorContext, mockClusteredListener);
- reply(new RegisterChangeListenerReply(getRef()));
+ final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+ final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
+ new Thread() {
+ @Override
+ public void run() {
+ proxy.init(path, scope);
+ }
- 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();
+ RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
+ Assert.assertEquals("getPath", path, registerMsg.getPath());
+ Assert.assertEquals("getScope", scope, registerMsg.getScope());
+ Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
- // The listener registration actor should get a Close message
- expectMsgClass(timeout, CloseDataChangeListenerRegistration.class);
+ reply(new RegisterChangeListenerReply(getRef()));
- // The DataChangeListener actor should be terminated
- expectMsgClass(timeout, Terminated.class);
+ for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
+ Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
+ }
- proxy.close();
+ Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
+ proxy.getListenerRegistrationActor());
- expectNoMsg();
- }};
- }
+ watch(proxy.getDataChangeListenerActor());
- @Test(timeout=10000)
- public void testLocalShardNotFound() {
- new JavaTestKit(getSystem()) {{
- ActorContext actorContext = new ActorContext(getSystem(), getRef(),
- mock(ClusterWrapper.class), mock(Configuration.class));
+ proxy.close();
- final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
- "shard-1", actorContext, mockListener);
+ // The listener registration actor should get a Close message
+ expectMsgClass(timeout, CloseDataChangeListenerRegistration.class);
- final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
- final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
- new Thread() {
- @Override
- public void run() {
- proxy.init(path, scope);
- }
+ // The DataChangeListener actor should be terminated
+ expectMsgClass(timeout, Terminated.class);
- }.start();
+ proxy.close();
- FiniteDuration timeout = duration("5 seconds");
- FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
- Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+ expectNoMsg();
+ }
+ };
+ }
- reply(new LocalShardNotFound("shard-1"));
+ @Test(timeout = 10000)
+ public void testLocalShardNotFound() {
+ new JavaTestKit(getSystem()) {
+ {
+ ActorContext actorContext = new ActorContext(getSystem(), getRef(),
+ mock(ClusterWrapper.class), mock(Configuration.class));
- expectNoMsg(duration("1 seconds"));
- }};
- }
+ final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
+ "shard-1", actorContext, mockListener);
- @Test(timeout=10000)
- public void testLocalShardNotInitialized() {
- new JavaTestKit(getSystem()) {{
- ActorContext actorContext = new ActorContext(getSystem(), getRef(),
- mock(ClusterWrapper.class), mock(Configuration.class));
+ final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+ final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
+ new Thread() {
+ @Override
+ public void run() {
+ proxy.init(path, scope);
+ }
- final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
- "shard-1", actorContext, mockListener);
+ }.start();
- final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
- final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
- new Thread() {
- @Override
- public void run() {
- proxy.init(path, scope);
- }
+ FiniteDuration timeout = duration("5 seconds");
+ FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
+ Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
- }.start();
+ reply(new LocalShardNotFound("shard-1"));
- FiniteDuration timeout = duration("5 seconds");
- FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
- Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
+ expectNoMsg(duration("1 seconds"));
- reply(new NotInitializedException("not initialized"));
+ proxy.close();
+ }
+ };
+ }
- new Within(duration("1 seconds")) {
- @Override
- protected void run() {
- expectNoMsg();
- }
- };
- }};
+ @Test(timeout = 10000)
+ public void testLocalShardNotInitialized() {
+ new JavaTestKit(getSystem()) {
+ {
+ ActorContext actorContext = new ActorContext(getSystem(), getRef(),
+ mock(ClusterWrapper.class), mock(Configuration.class));
+
+ final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
+ "shard-1", actorContext, mockListener);
+
+ final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
+ final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
+ new Thread() {
+ @Override
+ public void run() {
+ proxy.init(path, scope);
+ }
+
+ }.start();
+
+ 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.directExecutor());
+ 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);
+ ActorContext actorContext = mock(ActorContext.class);
- doReturn(executor).when(actorContext).getClientDispatcher();
+ doReturn(executor).when(actorContext).getClientDispatcher();
- String shardName = "shard-1";
- final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
- shardName, actorContext, mockListener);
+ String shardName = "shard-1";
+ final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
+ shardName, actorContext, mockListener);
- doReturn(mockActorSystem).when(actorContext).getActorSystem();
- 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(mockActorSystem).when(actorContext).getActorSystem();
+ 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(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
- AsyncDataBroker.DataChangeScope.ONE);
+ proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
+ AsyncDataBroker.DataChangeScope.ONE);
- 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);
-
- String shardName = "shard-1";
- final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
- shardName, actorContext, mockListener);
-
- 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));
-
- Answer<Future<Object>> answer = new Answer<Future<Object>>() {
- @Override
- public Future<Object> answer(InvocationOnMock invocation) {
+ new JavaTestKit(getSystem()) {
+ {
+ ActorContext actorContext = mock(ActorContext.class);
+
+ String shardName = "shard-1";
+ final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
+ shardName, actorContext, mockListener);
+
+ 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));
+
+ Answer<Future<Object>> answer = invocation -> {
proxy.close();
return Futures.successful((Object)new RegisterChangeListenerReply(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(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
- AsyncDataBroker.DataChangeScope.ONE);
+ proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
+ AsyncDataBroker.DataChangeScope.ONE);
- expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.class);
+ expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.class);
- Assert.assertEquals("getListenerRegistrationActor", null,
- proxy.getListenerRegistrationActor());
- }};
+ Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
+ proxy.close();
+ }
+ };
}
}