Use MockitoJUnitRunner and clean up imports/mocking a bit.
Change-Id: I854f95e37e503d28903c8aa83aecafdef4c4c1fa
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
* asked to do so via {@link CloseDataTreeNotificationListenerRegistration}.
*/
public final class DataTreeNotificationListenerRegistrationActor extends AbstractUntypedActor {
+ // FIXME: rework this constant to a duration and its injection
@VisibleForTesting
static long killDelay = TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS);
package org.opendaylight.controller.cluster.databroker;
import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
import org.opendaylight.controller.cluster.access.concepts.FrontendType;
import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
import org.opendaylight.controller.cluster.datastore.DatastoreContext;
import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
-import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ClientBackedDataStoreTest {
private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
MemberName.forName("member"), FrontendType.forName("frontend"));
private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
- private static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
- private static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier(HISTORY_ID, 0);
-
- private static EffectiveModelContext SCHEMA_CONTEXT;
+ private static final TransactionIdentifier TRANSACTION_IDENTIFIER =
+ new TransactionIdentifier(new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0), 0);
@Mock
private DataStoreClient clientActor;
@Mock
private ClientSnapshot clientSnapshot;
- @BeforeClass
- public static void beforeClass() {
- SCHEMA_CONTEXT = TestModel.createTestContext();
- }
-
- @AfterClass
- public static void afterClass() {
- SCHEMA_CONTEXT = null;
- }
-
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
- when(actorUtils.getSchemaContext()).thenReturn(SCHEMA_CONTEXT);
- when(actorUtils.getDatastoreContext()).thenReturn(DatastoreContext.newBuilder().build());
- when(clientTransaction.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
- when(clientSnapshot.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
+ doReturn(DatastoreContext.newBuilder().build()).when(actorUtils).getDatastoreContext();
+ doReturn(TRANSACTION_IDENTIFIER).when(clientTransaction).getIdentifier();
+ doReturn(TRANSACTION_IDENTIFIER).when(clientSnapshot).getIdentifier();
- when(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER);
- when(clientActor.createTransaction()).thenReturn(clientTransaction);
- when(clientActor.createLocalHistory()).thenReturn(clientLocalHistory);
- when(clientActor.createSnapshot()).thenReturn(clientSnapshot);
+ doReturn(clientTransaction).when(clientActor).createTransaction();
+ doReturn(clientLocalHistory).when(clientActor).createLocalHistory();
+ doReturn(clientSnapshot).when(clientActor).createSnapshot();
}
@Test
actorUtils, UNKNOWN_ID, clientActor)) {
final DOMStoreTransactionChain txChain = clientBackedDataStore.createTransactionChain();
assertNotNull(txChain);
- verify(clientActor, Mockito.times(1)).createLocalHistory();
+ verify(clientActor, times(1)).createLocalHistory();
}
}
actorUtils, UNKNOWN_ID, clientActor)) {
final DOMStoreReadTransaction tx = clientBackedDataStore.newReadOnlyTransaction();
assertNotNull(tx);
- verify(clientActor, Mockito.times(1)).createSnapshot();
+ verify(clientActor, times(1)).createSnapshot();
}
}
actorUtils, UNKNOWN_ID, clientActor)) {
final DOMStoreWriteTransaction tx = clientBackedDataStore.newWriteOnlyTransaction();
assertNotNull(tx);
- verify(clientActor, Mockito.times(1)).createTransaction();
+ verify(clientActor, times(1)).createTransaction();
}
}
actorUtils, UNKNOWN_ID, clientActor)) {
final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
assertNotNull(tx);
- verify(clientActor, Mockito.times(1)).createTransaction();
+ verify(clientActor, times(1)).createTransaction();
}
}
}
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
-import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ClientBackedReadWriteTransactionTest
extends ClientBackedTransactionTest<ClientBackedReadWriteTransaction> {
private ClientBackedReadWriteTransaction object;
private ClientTransaction delegate;
@Mock
private NormalizedNode data;
- @Mock
- private DOMStoreThreePhaseCommitCohort readyCohort;
@Override
ClientBackedReadWriteTransaction object() {
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
doReturn(TRANSACTION_ID).when(delegate).getIdentifier();
- doReturn(readyCohort).when(delegate).ready();
doReturn(immediateTrueFluentFuture()).when(delegate).exists(YangInstanceIdentifier.empty());
doReturn(immediateFluentFuture(Optional.of(data))).when(delegate).read(YangInstanceIdentifier.empty());
*/
package org.opendaylight.controller.cluster.databroker;
-import org.junit.Assert;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
import org.opendaylight.controller.cluster.access.concepts.FrontendType;
import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ClientBackedTransactionChainTest {
private ClientBackedTransactionChain chain;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
final FrontendIdentifier frontendId = FrontendIdentifier.create(
MemberName.forName("member"), FrontendType.forName("frontend"));
final ClientIdentifier clientId = ClientIdentifier.create(frontendId, 0);
final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(clientId, 0);
final TransactionIdentifier transactionId = new TransactionIdentifier(historyId, 0);
- Mockito.when(history.getIdentifier()).thenReturn(historyId);
- Mockito.when(transaction.getIdentifier()).thenReturn(transactionId);
- Mockito.when(snapshot.getIdentifier()).thenReturn(transactionId);
- Mockito.when(history.takeSnapshot()).thenReturn(snapshot);
- Mockito.when(history.createTransaction()).thenReturn(transaction);
+ doReturn(transactionId).when(transaction).getIdentifier();
+ doReturn(transactionId).when(snapshot).getIdentifier();
+ doReturn(snapshot).when(history).takeSnapshot();
+ doReturn(transaction).when(history).createTransaction();
chain = new ClientBackedTransactionChain(history, false);
}
@Test
public void testNewReadOnlyTransaction() {
- Assert.assertNotNull(chain.newReadOnlyTransaction());
- Mockito.verify(history).takeSnapshot();
+ assertNotNull(chain.newReadOnlyTransaction());
+ verify(history).takeSnapshot();
}
@Test
public void testNewReadWriteTransaction() {
- Assert.assertNotNull(chain.newReadWriteTransaction());
- Mockito.verify(history).createTransaction();
+ assertNotNull(chain.newReadWriteTransaction());
+ verify(history).createTransaction();
}
@Test
public void testNewWriteOnlyTransaction() {
- Assert.assertNotNull(chain.newWriteOnlyTransaction());
- Mockito.verify(history).createTransaction();
+ assertNotNull(chain.newWriteOnlyTransaction());
+ verify(history).createTransaction();
}
@Test
public void testClose() {
chain.newReadOnlyTransaction();
chain.close();
- Mockito.verify(snapshot).abort();
- Mockito.verify(history).close();
+ verify(snapshot).abort();
+ verify(history).close();
}
@Test
chain.snapshotClosed(snapshot);
// snap is removed, so cannot be aborted
chain.close();
- Mockito.verify(snapshot, Mockito.never()).abort();
- Mockito.verify(history).close();
+ verify(snapshot, never()).abort();
+ verify(history).close();
}
}
\ No newline at end of file
import org.opendaylight.controller.cluster.databroker.actors.dds.AbstractClientHandle;
public abstract class ClientBackedTransactionTest<T extends ClientBackedTransaction<?>> {
- private static FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(
+ private static final FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(
MemberName.forName("member"), FrontendType.forName("frontend"));
protected static final ClientIdentifier CLIENT_ID = ClientIdentifier.create(FRONTEND_ID, 0);
- private static LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 0);
+ private static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 0);
protected static final TransactionIdentifier TRANSACTION_ID = new TransactionIdentifier(HISTORY_ID, 0);
abstract T object();
*/
package org.opendaylight.controller.cluster.databroker;
-import org.junit.Assert;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ClientBackedWriteTransactionTest extends ClientBackedTransactionTest<ClientBackedWriteTransaction> {
private ClientBackedWriteTransaction object;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
- Mockito.doReturn(TRANSACTION_ID).when(delegate).getIdentifier();
- Mockito.doReturn(readyCohort).when(delegate).ready();
+ doReturn(TRANSACTION_ID).when(delegate).getIdentifier();
+ doReturn(readyCohort).when(delegate).ready();
object = new ClientBackedWriteTransaction(delegate, null);
}
@Test
public void testWrite() {
object().write(path, data);
- Mockito.verify(delegate).write(path, data);
+ verify(delegate).write(path, data);
}
@Test
public void testMerge() {
object().merge(path, data);
- Mockito.verify(delegate).merge(path, data);
+ verify(delegate).merge(path, data);
}
@Test
public void testDelete() {
object().delete(path);
- Mockito.verify(delegate).delete(path);
+ verify(delegate).delete(path);
}
@Test
public void testReady() {
final DOMStoreThreePhaseCommitCohort result = object().ready();
- Assert.assertNotNull(result);
- Mockito.verify(delegate).ready();
+ assertNotNull(result);
+ verify(delegate).ready();
}
}
*/
package org.opendaylight.controller.cluster.databroker.actors.dds;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
+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 static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.TRANSACTION_ID;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import com.google.common.primitives.UnsignedLong;
import java.util.Optional;
-import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mock;
-import org.mockito.Mockito;
import org.opendaylight.controller.cluster.access.ABIVersion;
import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
import org.opendaylight.controller.cluster.access.client.ClientActorContext;
public abstract class AbstractClientHistoryTest<T extends AbstractClientHistory> {
protected static final String SHARD_NAME = "default";
protected static final String PERSISTENCE_ID = "per-1";
- protected static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 1L);
+ protected static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(TestUtils.CLIENT_ID, 1L);
@Mock
private DataTree tree;
@Test
public void testCreateSnapshotProxy() {
- final AbstractProxyTransaction snapshotProxy = object().createSnapshotProxy(TRANSACTION_ID, 0L);
- Assert.assertNotNull(snapshotProxy);
- Assert.assertNotEquals(TRANSACTION_ID, snapshotProxy.getIdentifier());
+ final AbstractProxyTransaction snapshotProxy = object().createSnapshotProxy(TestUtils.TRANSACTION_ID, 0L);
+ assertNotNull(snapshotProxy);
+ assertNotEquals(TestUtils.TRANSACTION_ID, snapshotProxy.getIdentifier());
}
@Test
public void testCreateTransactionProxy() {
- AbstractProxyTransaction transactionProxy = object().createTransactionProxy(TRANSACTION_ID, 0L);
- Assert.assertNotNull(transactionProxy);
- Assert.assertNotEquals(TRANSACTION_ID, transactionProxy.getIdentifier());
+ AbstractProxyTransaction transactionProxy = object().createTransactionProxy(TestUtils.TRANSACTION_ID, 0L);
+ assertNotNull(transactionProxy);
+ assertNotEquals(TestUtils.TRANSACTION_ID, transactionProxy.getIdentifier());
}
@Test
public void testState() {
- Assert.assertEquals(AbstractClientHistory.State.IDLE, object().state());
+ assertEquals(AbstractClientHistory.State.IDLE, object().state());
}
@Test
public void testUpdateState() {
object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED);
- Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
+ assertEquals(AbstractClientHistory.State.CLOSED, object().state());
}
@Test
public void testDoClose() {
- object().createTransactionProxy(TRANSACTION_ID, 0L);
+ object().createTransactionProxy(TestUtils.TRANSACTION_ID, 0L);
object().doClose();
- Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
+ assertEquals(AbstractClientHistory.State.CLOSED, object().state());
}
@Test
public void testGetIdentifier() {
- Assert.assertEquals(HISTORY_ID, object().getIdentifier());
+ assertEquals(HISTORY_ID, object().getIdentifier());
}
@Test
public void testNextTx() {
- Assert.assertTrue(object().nextTx() + 1 == object().nextTx());
+ assertEquals(object().nextTx() + 1, object().nextTx());
}
@Test
public void testResolveShardForPath() {
final Long shardForPath = object().resolveShardForPath(YangInstanceIdentifier.empty());
- Assert.assertEquals(0L, shardForPath.longValue());
+ assertNotNull(shardForPath);
+ assertEquals(0L, (long) shardForPath);
}
@Test
public void testLocalAbort() {
object().localAbort(new Throwable());
- Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
+ assertEquals(AbstractClientHistory.State.CLOSED, object().state());
}
@Test
public void testOnProxyDestroyed() {
- final ProxyHistory proxyHistory = Mockito.mock(ProxyHistory.class);
- when(proxyHistory.getIdentifier()).thenReturn(HISTORY_ID);
+ final ProxyHistory proxyHistory = mock(ProxyHistory.class);
+ doReturn(HISTORY_ID).when(proxyHistory).getIdentifier();
object().onProxyDestroyed(proxyHistory);
verify(proxyHistory).getIdentifier();
@Test
public void testCreateTransaction() {
final ClientTransaction transaction = object().createTransaction();
- Assert.assertNotNull(transaction);
+ assertNotNull(transaction);
}
@Test
public void testTakeSnapshot() {
final ClientSnapshot clientSnapshot = object().takeSnapshot();
- Assert.assertEquals(object().getIdentifier(), clientSnapshot.getIdentifier().getHistoryId());
+ assertEquals(object().getIdentifier(), clientSnapshot.getIdentifier().getHistoryId());
}
@Test
SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10);
final ConnectedClientConnection<ShardBackendInfo> newConn = AccessClientUtil.createConnectedConnection(
clientActorContext(), cookie, info);
- object().createSnapshotProxy(TRANSACTION_ID, shard);
+ object().createSnapshotProxy(TestUtils.TRANSACTION_ID, shard);
final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn);
- Assert.assertNotNull(reconnectCohort);
+ assertNotNull(reconnectCohort);
}
@Test
SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10);
final ConnectedClientConnection<ShardBackendInfo> newConn = AccessClientUtil.createConnectedConnection(
clientActorContext(), cookie, info);
- object().createSnapshotProxy(TRANSACTION_ID, shard);
+ object().createSnapshotProxy(TestUtils.TRANSACTION_ID, shard);
final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn);
- Assert.assertNull(reconnectCohort);
+ assertNull(reconnectCohort);
}
protected static ActorUtils createActorUtilsMock(final ActorSystem system, final ActorRef actor) {
final ActorSelection selection = system.actorSelection(actor.path());
final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
promise.success(shardInfo);
- when(mock.findPrimaryShardAsync(any())).thenReturn(promise.future());
+ doReturn(promise.future()).when(mock).findPrimaryShardAsync(any());
return mock;
}
}
\ No newline at end of file
*/
package org.opendaylight.controller.cluster.databroker.actors.dds;
-import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.endsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
import akka.actor.ActorSystem;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
import org.opendaylight.controller.cluster.access.client.ClientActorContext;
import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ClientLocalHistoryTest extends AbstractClientHistoryTest<ClientLocalHistory> {
private ActorSystem system;
private AbstractDataStoreClientBehavior behavior;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
system = ActorSystem.apply();
final TestProbe clientContextProbe = new TestProbe(system, "client");
final TestProbe actorContextProbe = new TestProbe(system, "actor-context");
clientActorContext = AccessClientUtil.createClientActorContext(
- system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
+ system, clientContextProbe.ref(), TestUtils.CLIENT_ID, PERSISTENCE_ID);
final ActorUtils actorUtilsMock = createActorUtilsMock(system, actorContextProbe.ref());
behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorUtilsMock, SHARD_NAME);
@Test
public void testClose() {
object().close();
- Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
+ assertEquals(AbstractClientHistory.State.CLOSED, object().state());
}
@Override
@Test
public void testDoCreateTransaction() {
final ClientTransaction clientTransaction = object().doCreateTransaction();
- Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
+ assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
}
@Override
@Test
public void testOnTransactionAbort() {
final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
- Assert.assertTrue(clientSnapshot.abort());
+ assertTrue(clientSnapshot.abort());
}
@Override
public void testCreateHistoryProxy() {
final AbstractClientConnection<ShardBackendInfo> clientConnection = behavior.getConnection(0L);
final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection);
- Assert.assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
+ assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
}
@Override
@Test
public void testDoCreateSnapshot() {
final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
- Assert.assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
+ assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
clientSnapshot.getIdentifier().getHistoryId());
}
// make transaction ready
object().onTransactionReady(tx, cohort);
// state should be set to IDLE
- Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state());
+ assertEquals(AbstractClientHistory.State.IDLE, object.state());
// complete transaction
object().onTransactionComplete(tx.getIdentifier());
// state is still IDLE
- Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state());
+ assertEquals(AbstractClientHistory.State.IDLE, object.state());
}
@Override
@Test
public void testOnTransactionReady() {
- final AbstractTransactionCommitCohort result = object().onTransactionReady(
- object().createTransaction(), cohort);
- Assert.assertEquals(result, cohort);
+ final AbstractTransactionCommitCohort result = object().onTransactionReady(object().createTransaction(),
+ cohort);
+ assertEquals(result, cohort);
}
@Override
- @Test(expected = IllegalStateException.class)
+ @Test
public void testOnTransactionReadyDuplicate() {
final ClientTransaction tx = object().createTransaction();
object().onTransactionReady(tx, cohort);
- object().onTransactionReady(tx, cohort);
+ final IllegalStateException ise = assertThrows(IllegalStateException.class,
+ () -> object().onTransactionReady(tx, cohort));
+ assertThat(ise.getMessage(), containsString(" is idle when readying transaction "));
}
@Test
object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.TX_OPEN);
final AbstractTransactionCommitCohort transactionCommitCohort =
object().onTransactionReady(transaction, cohort);
- Assert.assertEquals(cohort, transactionCommitCohort);
+ assertEquals(cohort, transactionCommitCohort);
}
@Test
object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED);
final AbstractTransactionCommitCohort transactionCommitCohort =
object().onTransactionReady(transaction, cohort);
- Assert.assertEquals(cohort, transactionCommitCohort);
+ assertEquals(cohort, transactionCommitCohort);
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void testOnTransactionReadyAndCompleteIdleFail() {
- object().onTransactionReady(transaction, cohort);
+ final IllegalStateException ise = assertThrows(IllegalStateException.class,
+ () -> object().onTransactionReady(transaction, cohort));
+ assertThat(ise.getMessage(), endsWith(" is idle when readying transaction null"));
}
}
*/
package org.opendaylight.controller.cluster.databroker.actors.dds;
+import static org.junit.Assert.assertEquals;
import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.HISTORY_ID;
import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.TRANSACTION_ID;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.access.ABIVersion;
import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ClientTransactionCommitCohortTest {
private static final String PERSISTENCE_ID = "per-1";
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
system = ActorSystem.apply();
final TestProbe clientContextProbe = new TestProbe(system, "clientContext");
final ClientActorContext context =
private void expectCanCommit(final TransactionTester<RemoteProxyTransaction> tester) {
final ModifyTransactionRequest request = tester.expectTransactionRequest(ModifyTransactionRequest.class);
- Assert.assertTrue(request.getPersistenceProtocol().isPresent());
- Assert.assertEquals(PersistenceProtocol.THREE_PHASE, request.getPersistenceProtocol().get());
+ assertEquals(Optional.of(PersistenceProtocol.THREE_PHASE), request.getPersistenceProtocol());
}
void expectPreCommit(final TransactionTester<?> tester) {
final T expectedResult) throws Exception {
final ListenableFuture<T> result = operation.apply(cohort);
replySuccess(transactions, expectFunction, replyFunction);
- Assert.assertEquals(expectedResult, getWithTimeout(result));
+ assertEquals(expectedResult, getWithTimeout(result));
}
/**
//check future fail
final ExecutionException exception =
assertOperationThrowsException(() -> getWithTimeout(canCommit), ExecutionException.class);
- Assert.assertEquals(e, exception.getCause());
+ assertEquals(e, exception.getCause());
}
}
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.access.ABIVersion;
import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class DirectTransactionCommitCohortTest {
private static final String PERSISTENCE_ID = "per-1";
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
system = ActorSystem.apply();
final TestProbe clientContextProbe = new TestProbe(system, "clientContext");
final ClientActorContext context =
import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.getWithTimeout;
import com.google.common.util.concurrent.ListenableFuture;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class EmptyTransactionCommitCohortTest {
-
@Mock
private AbstractClientHistory history;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
cohort = new EmptyTransactionCommitCohort(history, TRANSACTION_ID);
}
public void testCommit() throws Exception {
final ListenableFuture<Void> commit = cohort.commit();
verify(history).onTransactionComplete(TRANSACTION_ID);
- Assert.assertNull(getWithTimeout(commit));
+ assertNull(getWithTimeout(commit));
}
}
\ No newline at end of file
*/
package org.opendaylight.controller.cluster.databroker.actors.dds;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
import com.google.common.util.concurrent.Uninterruptibles;
-import java.util.Collections;
+import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.access.commands.ConnectClientFailure;
import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
import scala.concurrent.Promise;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ModuleShardBackendResolverTest {
private static final MemberName MEMBER_NAME = MemberName.forName("member-1");
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
system = ActorSystem.apply();
contextProbe = new TestProbe(system, "context");
shardManagerProbe = new TestProbe(system, "ShardManager");
final ActorUtils actorUtils = createActorUtilsMock(system, contextProbe.ref());
- when(actorUtils.getShardManager()).thenReturn(shardManagerProbe.ref());
+ doReturn(shardManagerProbe.ref()).when(actorUtils).getShardManager();
moduleShardBackendResolver = new ModuleShardBackendResolver(CLIENT_ID, actorUtils);
- when(actorUtils.getShardStrategyFactory()).thenReturn(shardStrategyFactory);
- when(shardStrategyFactory.getStrategy(YangInstanceIdentifier.empty())).thenReturn(shardStrategy);
+ doReturn(shardStrategyFactory).when(actorUtils).getShardStrategyFactory();
+ doReturn(shardStrategy).when(shardStrategyFactory).getStrategy(YangInstanceIdentifier.empty());
final PrimaryShardInfoFutureCache cache = new PrimaryShardInfoFutureCache();
- when(actorUtils.getPrimaryShardInfoCache()).thenReturn(cache);
+ doReturn(cache).when(actorUtils).getPrimaryShardInfoCache();
}
@After
@Test
public void testResolveShardForPathNonNullCookie() {
- when(shardStrategy.findShard(YangInstanceIdentifier.empty())).thenReturn(DefaultShardStrategy.DEFAULT_SHARD);
+ doReturn(DefaultShardStrategy.DEFAULT_SHARD).when(shardStrategy).findShard(YangInstanceIdentifier.empty());
final Long cookie = moduleShardBackendResolver.resolveShardForPath(YangInstanceIdentifier.empty());
- Assert.assertEquals(0L, cookie.longValue());
+ assertEquals(0L, (long) cookie);
}
@Test
public void testResolveShardForPathNullCookie() {
- when(shardStrategy.findShard(YangInstanceIdentifier.empty())).thenReturn("foo");
+ doReturn("foo").when(shardStrategy).findShard(YangInstanceIdentifier.empty());
final Long cookie = moduleShardBackendResolver.resolveShardForPath(YangInstanceIdentifier.empty());
- Assert.assertEquals(1L, cookie.longValue());
+ assertEquals(1L, (long) cookie);
}
@Test
contextProbe.expectMsgClass(ConnectClientRequest.class);
final TestProbe backendProbe = new TestProbe(system, "backend");
final ConnectClientSuccess msg = new ConnectClientSuccess(CLIENT_ID, 0L, backendProbe.ref(),
- Collections.emptyList(), dataTree, 3);
+ List.of(), dataTree, 3);
contextProbe.reply(msg);
final CompletionStage<ShardBackendInfo> stage = moduleShardBackendResolver.getBackendInfo(0L);
final ShardBackendInfo shardBackendInfo = TestUtils.getWithTimeout(stage.toCompletableFuture());
- Assert.assertEquals(0L, shardBackendInfo.getCookie().longValue());
- Assert.assertEquals(dataTree, shardBackendInfo.getDataTree().get());
- Assert.assertEquals(DefaultShardStrategy.DEFAULT_SHARD, shardBackendInfo.getName());
+ assertEquals(0L, shardBackendInfo.getCookie().longValue());
+ assertEquals(dataTree, shardBackendInfo.getDataTree().get());
+ assertEquals(DefaultShardStrategy.DEFAULT_SHARD, shardBackendInfo.getName());
}
@Test
final ExecutionException caught =
TestUtils.assertOperationThrowsException(() -> TestUtils.getWithTimeout(stage.toCompletableFuture()),
ExecutionException.class);
- Assert.assertEquals(cause, caught.getCause());
+ assertEquals(cause, caught.getCause());
}
@Test
contextProbe.expectMsgClass(ConnectClientRequest.class);
final TestProbe staleBackendProbe = new TestProbe(system, "staleBackend");
final ConnectClientSuccess msg = new ConnectClientSuccess(CLIENT_ID, 0L, staleBackendProbe.ref(),
- Collections.emptyList(), dataTree, 3);
+ List.of(), dataTree, 3);
contextProbe.reply(msg);
//get backend info
final ShardBackendInfo staleBackendInfo = TestUtils.getWithTimeout(backendInfo.toCompletableFuture());
contextProbe.expectMsgClass(ConnectClientRequest.class);
final TestProbe refreshedBackendProbe = new TestProbe(system, "refreshedBackend");
final ConnectClientSuccess msg2 = new ConnectClientSuccess(CLIENT_ID, 1L, refreshedBackendProbe.ref(),
- Collections.emptyList(), dataTree, 3);
+ List.of(), dataTree, 3);
contextProbe.reply(msg2);
final ShardBackendInfo refreshedBackendInfo = TestUtils.getWithTimeout(refreshed.toCompletableFuture());
- Assert.assertEquals(staleBackendInfo.getCookie(), refreshedBackendInfo.getCookie());
- Assert.assertEquals(refreshedBackendProbe.ref(), refreshedBackendInfo.getActor());
+ assertEquals(staleBackendInfo.getCookie(), refreshedBackendInfo.getCookie());
+ assertEquals(refreshedBackendProbe.ref(), refreshedBackendInfo.getActor());
}
@SuppressWarnings("unchecked")
final Registration callbackReg = moduleShardBackendResolver.notifyWhenBackendInfoIsStale(mockCallback);
regMessage.getCallback().accept(DefaultShardStrategy.DEFAULT_SHARD);
- verify(mockCallback, timeout(5000)).accept(Long.valueOf(0));
+ verify(mockCallback, timeout(5000)).accept((long) 0);
reset(mockCallback);
callbackReg.close();
final ActorSelection selection = system.actorSelection(actor.path());
final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
promise.success(shardInfo);
- when(mock.findPrimaryShardAsync(DefaultShardStrategy.DEFAULT_SHARD)).thenReturn(promise.future());
- when(mock.getClientDispatcher()).thenReturn(system.dispatchers().defaultGlobalDispatcher());
+ doReturn(promise.future()).when(mock).findPrimaryShardAsync(DefaultShardStrategy.DEFAULT_SHARD);
return mock;
}
}
*/
package org.opendaylight.controller.cluster.databroker.actors.dds;
-import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
import akka.actor.ActorSystem;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
import org.opendaylight.controller.cluster.access.client.ClientActorContext;
import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class SingleClientHistoryTest extends AbstractClientHistoryTest<SingleClientHistory> {
private ActorSystem system;
private AbstractDataStoreClientBehavior behavior;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
-
system = ActorSystem.apply();
final TestProbe clientContextProbe = new TestProbe(system, "client");
final TestProbe actorContextProbe = new TestProbe(system, "actor-context");
clientActorContext = AccessClientUtil.createClientActorContext(
- system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
+ system, clientContextProbe.ref(), TestUtils.CLIENT_ID, PERSISTENCE_ID);
final ActorUtils actorUtilsMock = createActorUtilsMock(system, actorContextProbe.ref());
behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorUtilsMock, SHARD_NAME);
@Test
public void testDoCreateTransaction() {
final ClientTransaction clientTransaction = object().doCreateTransaction();
- Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
+ assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
}
@Override
public void testCreateHistoryProxy() {
final AbstractClientConnection<ShardBackendInfo> clientConnection = behavior.getConnection(0L);
final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection);
- Assert.assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
+ assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
}
@Override
@Test
public void testDoCreateSnapshot() {
final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
- Assert.assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
+ assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
clientSnapshot.getIdentifier().getHistoryId());
}
object().onTransactionComplete(transaction.getIdentifier());
// it is possible to make transaction ready again
final AbstractTransactionCommitCohort result = object().onTransactionReady(transaction, cohort);
- Assert.assertEquals(result, cohort);
+ assertEquals(result, cohort);
}
@Override
@Test
public void testOnTransactionAbort() {
final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
- Assert.assertTrue(clientSnapshot.abort());
+ assertTrue(clientSnapshot.abort());
}
@Override
public void testOnTransactionReady() {
final AbstractTransactionCommitCohort result = object().onTransactionReady(
object().createTransaction(), cohort);
- Assert.assertEquals(result, cohort);
+ assertEquals(result, cohort);
}
@Override
- @Test(expected = IllegalStateException.class)
+ @Test
public void testOnTransactionReadyDuplicate() {
final ClientTransaction transaction = object().createTransaction();
object().onTransactionReady(transaction, cohort);
- object().onTransactionReady(transaction, cohort);
+ final IllegalStateException ise = assertThrows(IllegalStateException.class,
+ () -> object().onTransactionReady(transaction, cohort));
+ assertThat(ise.getMessage(), startsWith("Duplicate cohort "));
}
}
\ No newline at end of file
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.datastore.messages.DataExists;
import org.opendaylight.controller.cluster.datastore.messages.ReadData;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import scala.concurrent.Future;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class LocalTransactionContextTest {
@Mock
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
localTransactionContext = new LocalTransactionContext(readWriteTransaction, limiter.getIdentifier(),
mockReadySupport) {
@Override
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.cluster.datastore;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class TransactionContextWrapperTest {
@Mock
private ActorUtils actorUtils;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
doReturn(DatastoreContext.newBuilder().build()).when(actorUtils).getDatastoreContext();
transactionContextWrapper = new TransactionContextWrapper(MockIdentifiers.transactionIdentifier(
TransactionContextWrapperTest.class, "mock"), actorUtils, "mock");
import java.time.Duration;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class DataTreeNotificationListenerRegistrationActorTest extends AbstractActorTest {
@Mock
private ListenerRegistration<?> mockListenerReg;
@Before
public void setup() {
- MockitoAnnotations.initMocks(this);
DataTreeNotificationListenerRegistrationActor.killDelay = 100;
kit = new TestKit(getSystem());
}