X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatabroker%2Factors%2Fdds%2FAbstractDataStoreClientBehaviorTest.java;h=14a29c5101394be7a971802769bfa7f1ecd40e25;hp=8a5b3f579475ef1c8210fe566e98627802c4210e;hb=438d5278050961f0773707e6508b1b736eac1c0f;hpb=e9fc7e7ed2b13d274518d6a872ab67749ef4507a diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractDataStoreClientBehaviorTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractDataStoreClientBehaviorTest.java index 8a5b3f5794..14a29c5101 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractDataStoreClientBehaviorTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractDataStoreClientBehaviorTest.java @@ -7,9 +7,12 @@ */ package org.opendaylight.controller.cluster.databroker.actors.dds; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID; import akka.actor.ActorRef; @@ -18,10 +21,9 @@ import akka.actor.ActorSystem; import akka.actor.Status; import akka.testkit.TestProbe; import akka.testkit.javadsl.TestKit; -import java.util.Collections; +import java.util.List; import java.util.Optional; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.opendaylight.controller.cluster.access.client.AbstractClientConnection; @@ -31,11 +33,11 @@ import org.opendaylight.controller.cluster.access.client.InternalCommand; import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest; import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess; import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo; -import org.opendaylight.controller.cluster.datastore.utils.ActorContext; +import org.opendaylight.controller.cluster.datastore.utils.ActorUtils; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; +import org.opendaylight.yangtools.yang.data.tree.api.CursorAwareDataTreeModification; +import org.opendaylight.yangtools.yang.data.tree.api.DataTree; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot; import scala.concurrent.Promise; public abstract class AbstractDataStoreClientBehaviorTest { @@ -50,11 +52,11 @@ public abstract class AbstractDataStoreClientBehaviorTest { private AbstractDataStoreClientBehavior behavior; @Before - public void setUp() throws Exception { + public void setUp() { system = ActorSystem.apply(); clientActorProbe = new TestProbe(system, "client"); actorContextProbe = new TestProbe(system, "actor-context"); - final ActorContext context = createActorContextMock(system, actorContextProbe.ref()); + final ActorUtils context = createActorContextMock(system, actorContextProbe.ref()); clientContext = AccessClientUtil.createClientActorContext(system, clientActorProbe.ref(), CLIENT_ID, PERSISTENCE_ID); behavior = createBehavior(clientContext, context); @@ -62,115 +64,111 @@ public abstract class AbstractDataStoreClientBehaviorTest { @SuppressWarnings("checkstyle:hiddenField") protected abstract AbstractDataStoreClientBehavior createBehavior(ClientActorContext clientContext, - ActorContext context); + ActorUtils context); @After - public void tearDown() throws Exception { + public void tearDown() { TestKit.shutdownActorSystem(system); } @Test - public void testResolveShardForPath() throws Exception { - Assert.assertEquals(0L, behavior.resolveShardForPath(YangInstanceIdentifier.EMPTY).longValue()); + public void testResolveShardForPath() { + assertEquals(0L, behavior.resolveShardForPath(YangInstanceIdentifier.empty()).longValue()); } @Test - public void testHaltClient() throws Exception { + public void testHaltClient() { behavior.haltClient(new RuntimeException()); } @Test - public void testOnCommand() throws Exception { + public void testOnCommand() { final TestProbe probe = new TestProbe(system); final GetClientRequest request = new GetClientRequest(probe.ref()); final AbstractDataStoreClientBehavior nextBehavior = behavior.onCommand(request); final Status.Success success = probe.expectMsgClass(Status.Success.class); - Assert.assertEquals(behavior, success.status()); - Assert.assertSame(behavior, nextBehavior); + assertEquals(behavior, success.status()); + assertSame(behavior, nextBehavior); } @Test - public void testOnCommandUnhandled() throws Exception { + public void testOnCommandUnhandled() { final AbstractDataStoreClientBehavior nextBehavior = behavior.onCommand("unhandled"); - Assert.assertSame(behavior, nextBehavior); + assertSame(behavior, nextBehavior); } @Test - public void testCreateLocalHistory() throws Exception { + public void testCreateLocalHistory() { final ClientLocalHistory history = behavior.createLocalHistory(); - Assert.assertEquals(behavior.getIdentifier(), history.getIdentifier().getClientId()); + assertEquals(behavior.getIdentifier(), history.getIdentifier().getClientId()); } @Test - public void testCreateTransaction() throws Exception { + public void testCreateTransaction() { final ClientTransaction transaction = behavior.createTransaction(); - Assert.assertEquals(behavior.getIdentifier(), transaction.getIdentifier().getHistoryId().getClientId()); + assertEquals(behavior.getIdentifier(), transaction.getIdentifier().getHistoryId().getClientId()); } @Test - public void testCreateSnapshot() throws Exception { + public void testCreateSnapshot() { final ClientSnapshot snapshot = behavior.createSnapshot(); - Assert.assertEquals(behavior.getIdentifier(), snapshot.getIdentifier().getHistoryId().getClientId()); + assertEquals(behavior.getIdentifier(), snapshot.getIdentifier().getHistoryId().getClientId()); } @Test - public void testClose() throws Exception { + public void testClose() { behavior.close(); final InternalCommand internalCommand = clientActorProbe.expectMsgClass(InternalCommand.class); internalCommand.execute(behavior); - try { - behavior.createLocalHistory(); - Assert.fail("Behavior is closed and shouldn't allow to create new history."); - } catch (final IllegalStateException e) { - //ok - } + + assertThrows(IllegalStateException.class, () -> behavior.createLocalHistory()); } @Test - public void testGetIdentifier() throws Exception { - Assert.assertEquals(CLIENT_ID, behavior.getIdentifier()); + public void testGetIdentifier() { + assertEquals(CLIENT_ID, behavior.getIdentifier()); } @Test - public void testGetConnection() throws Exception { + public void testGetConnection() { //set up data tree mock final CursorAwareDataTreeModification modification = mock(CursorAwareDataTreeModification.class); - when(modification.readNode(YangInstanceIdentifier.EMPTY)).thenReturn(Optional.empty()); + doReturn(Optional.empty()).when(modification).readNode(YangInstanceIdentifier.empty()); final DataTreeSnapshot snapshot = mock(DataTreeSnapshot.class); - when(snapshot.newModification()).thenReturn(modification); + doReturn(modification).when(snapshot).newModification(); final DataTree dataTree = mock(DataTree.class); - when(dataTree.takeSnapshot()).thenReturn(snapshot); + doReturn(snapshot).when(dataTree).takeSnapshot(); final TestProbe backendProbe = new TestProbe(system, "backend"); final long shard = 0L; - behavior.createTransaction().read(YangInstanceIdentifier.EMPTY); + + behavior.createTransaction().read(YangInstanceIdentifier.empty()); final AbstractClientConnection connection = behavior.getConnection(shard); //check cached connection for same shard - Assert.assertSame(connection, behavior.getConnection(shard)); + assertSame(connection, behavior.getConnection(shard)); final ConnectClientRequest connectClientRequest = actorContextProbe.expectMsgClass(ConnectClientRequest.class); - Assert.assertEquals(CLIENT_ID, connectClientRequest.getTarget()); + assertEquals(CLIENT_ID, connectClientRequest.getTarget()); final long sequence = 0L; - Assert.assertEquals(sequence, connectClientRequest.getSequence()); - actorContextProbe.reply(new ConnectClientSuccess(CLIENT_ID, sequence, backendProbe.ref(), - Collections.emptyList(), dataTree, 3)); - Assert.assertEquals(clientActorProbe.ref(), connection.localActor()); + assertEquals(sequence, connectClientRequest.getSequence()); + actorContextProbe.reply(new ConnectClientSuccess(CLIENT_ID, sequence, backendProbe.ref(), List.of(), dataTree, + 3)); + assertEquals(clientActorProbe.ref(), connection.localActor()); //capture and execute command passed to client context final InternalCommand command = clientActorProbe.expectMsgClass(InternalCommand.class); command.execute(behavior); //check, whether command was reaplayed - verify(modification).readNode(YangInstanceIdentifier.EMPTY); + verify(modification).readNode(YangInstanceIdentifier.empty()); } - private static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) { - final ActorContext mock = mock(ActorContext.class); + private static ActorUtils createActorContextMock(final ActorSystem system, final ActorRef actor) { + final ActorUtils mock = mock(ActorUtils.class); final Promise promise = new scala.concurrent.impl.Promise.DefaultPromise<>(); final ActorSelection selection = system.actorSelection(actor.path()); final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0); promise.success(shardInfo); - when(mock.findPrimaryShardAsync(SHARD)).thenReturn(promise.future()); + doReturn(promise.future()).when(mock).findPrimaryShardAsync(SHARD); return mock; } - }