package org.opendaylight.controller.cluster.datastore;
import static org.junit.Assert.assertSame;
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
+
import com.google.common.primitives.UnsignedLong;
import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.ListenableFuture;
-import java.util.Collections;
import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
-import scala.concurrent.Promise;
/**
* Unit tests for SimpleShardDataTreeCohort.
private SimpleShardDataTreeCohort cohort;
@Before
- public void setup() throws Exception {
+ public void setup() {
MockitoAnnotations.initMocks(this);
- doNothing().when(mockUserCohorts).commit();
+ doReturn(Optional.empty()).when(mockUserCohorts).commit();
doReturn(Optional.empty()).when(mockUserCohorts).abort();
cohort = new SimpleShardDataTreeCohort(mockShardDataTree, mockModification, nextTransactionId(),
- mockUserCohorts);
+ mockUserCohorts, Optional.empty());
}
@Test
- public void testCanCommitSuccess() throws Exception {
+ public void testCanCommitSuccess() {
canCommitSuccess();
}
private void canCommitSuccess() {
doAnswer(invocation -> {
- invocation.getArgumentAt(0, SimpleShardDataTreeCohort.class).successfulCanCommit();
+ invocation.<SimpleShardDataTreeCohort>getArgument(0).successfulCanCommit();
return null;
}).when(mockShardDataTree).startCanCommit(cohort);
verifyNoMoreInteractions(callback);
}
- private void testValidatationPropagates(final Exception cause) throws DataValidationFailedException {
+ private void testValidatationPropagates(final Exception cause) {
doAnswer(invocation -> {
- invocation.getArgumentAt(0, SimpleShardDataTreeCohort.class).failedCanCommit(cause);
+ invocation.<SimpleShardDataTreeCohort>getArgument(0).failedCanCommit(cause);
return null;
}).when(mockShardDataTree).startCanCommit(cohort);
}
@Test
- public void testCanCommitWithConflictingModEx() throws DataValidationFailedException {
- testValidatationPropagates(new ConflictingModificationAppliedException(YangInstanceIdentifier.EMPTY, "mock"));
+ public void testCanCommitWithConflictingModEx() {
+ testValidatationPropagates(new ConflictingModificationAppliedException(YangInstanceIdentifier.empty(), "mock"));
}
@Test
- public void testCanCommitWithDataValidationEx() throws DataValidationFailedException {
- testValidatationPropagates(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock"));
+ public void testCanCommitWithDataValidationEx() {
+ testValidatationPropagates(new DataValidationFailedException(YangInstanceIdentifier.empty(), "mock"));
}
@Test
- public void testCanCommitWithIllegalArgumentEx() throws DataValidationFailedException {
+ public void testCanCommitWithIllegalArgumentEx() {
testValidatationPropagates(new IllegalArgumentException("mock"));
}
private DataTreeCandidateTip preCommitSuccess() {
final DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class);
doAnswer(invocation -> {
- invocation.getArgumentAt(0, SimpleShardDataTreeCohort.class).successfulPreCommit(mockCandidate);
+ invocation.<SimpleShardDataTreeCohort>getArgument(0).successfulPreCommit(mockCandidate);
return null;
}).when(mockShardDataTree).startPreCommit(cohort);
}
@Test
- public void testPreCommitAndCommitSuccess() throws Exception {
+ public void testPreCommitAndCommitSuccess() {
canCommitSuccess();
final DataTreeCandidateTip candidate = preCommitSuccess();
doAnswer(invocation -> {
- invocation.getArgumentAt(0, SimpleShardDataTreeCohort.class).successfulCommit(UnsignedLong.valueOf(0));
+ invocation.<SimpleShardDataTreeCohort>getArgument(0).successfulCommit(UnsignedLong.valueOf(0), () -> { });
return null;
}).when(mockShardDataTree).startCommit(cohort, candidate);
@SuppressWarnings("unchecked")
- final
- FutureCallback<UnsignedLong> mockCommitCallback = mock(FutureCallback.class);
+ final FutureCallback<UnsignedLong> mockCommitCallback = mock(FutureCallback.class);
cohort.commit(mockCommitCallback);
verify(mockCommitCallback).onSuccess(any(UnsignedLong.class));
}
@Test
- public void testPreCommitWithIllegalArgumentEx() throws Throwable {
+ public void testPreCommitWithIllegalArgumentEx() {
canCommitSuccess();
final Exception cause = new IllegalArgumentException("mock");
doAnswer(invocation -> {
- invocation.getArgumentAt(0, SimpleShardDataTreeCohort.class).failedPreCommit(cause);
+ invocation.<SimpleShardDataTreeCohort>getArgument(0).failedPreCommit(cause);
return null;
}).when(mockShardDataTree).startPreCommit(cohort);
}
@Test
- public void testPreCommitWithReportedFailure() throws Throwable {
+ public void testPreCommitWithReportedFailure() {
canCommitSuccess();
final Exception cause = new IllegalArgumentException("mock");
final Exception cause = new IllegalArgumentException("mock");
doAnswer(invocation -> {
- invocation.getArgumentAt(0, SimpleShardDataTreeCohort.class).failedCommit(cause);
+ invocation.<SimpleShardDataTreeCohort>getArgument(0).failedCommit(cause);
return null;
}).when(mockShardDataTree).startCommit(cohort, candidate);
verify(mockUserCohorts).abort();
}
+ private static Future<?> abort(final ShardDataTreeCohort cohort) {
+ final CompletableFuture<Void> f = new CompletableFuture<>();
+ cohort.abort(new FutureCallback<Void>() {
+ @Override
+ public void onSuccess(final Void result) {
+ f.complete(null);
+ }
+
+ @Override
+ public void onFailure(final Throwable failure) {
+ f.completeExceptionally(failure);
+ }
+ });
+
+ return f;
+ }
+
@Test
public void testAbort() throws Exception {
- doNothing().when(mockShardDataTree).startAbort(cohort);
-
- cohort.abort().get();
+ doReturn(Boolean.TRUE).when(mockShardDataTree).startAbort(cohort);
+ abort(cohort).get();
verify(mockShardDataTree).startAbort(cohort);
}
@Test
public void testAbortWithCohorts() throws Exception {
- doNothing().when(mockShardDataTree).startAbort(cohort);
-
- final Promise<Iterable<Object>> cohortFuture = akka.dispatch.Futures.promise();
- doReturn(Optional.of(cohortFuture.future())).when(mockUserCohorts).abort();
+ doReturn(true).when(mockShardDataTree).startAbort(cohort);
- final ListenableFuture<Void> abortFuture = cohort.abort();
+ doReturn(Optional.of(CompletableFuture.completedFuture(null))).when(mockUserCohorts).abort();
- cohortFuture.success(Collections.emptyList());
+ final Future<?> abortFuture = abort(cohort);
abortFuture.get();
verify(mockShardDataTree).startAbort(cohort);