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%2Fdatastore%2FAbstractTransactionProxyTest.java;h=a40739f1145f6aba1f0e348968025f95c0913bce;hp=a998fbffcb55d0bc38bd9920436c50a44fcf91cb;hb=ccca30bbb1545643c427fc59c23329c5d49f8d4b;hpb=731e7284cf0895fdb1b89427f91762e80e67c2ff diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java index a998fbffcb..a40739f114 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java @@ -9,11 +9,12 @@ package org.opendaylight.controller.cluster.datastore; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isA; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -29,15 +30,15 @@ import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.Timer; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableMap; -import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.FluentFuture; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; -import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.junit.AfterClass; import org.junit.Before; @@ -70,7 +71,7 @@ import org.opendaylight.controller.cluster.datastore.modification.WriteModificat import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy; import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy; import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory; -import org.opendaylight.controller.cluster.datastore.utils.ActorContext; +import org.opendaylight.controller.cluster.datastore.utils.ActorUtils; import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration; import org.opendaylight.controller.cluster.raft.utils.DoNothingActor; import org.opendaylight.controller.md.cluster.datastore.model.CarsModel; @@ -84,7 +85,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import scala.concurrent.Await; import scala.concurrent.Future; -import scala.concurrent.duration.Duration; +import scala.concurrent.duration.FiniteDuration; /** * Abstract base class for TransactionProxy unit tests. @@ -95,6 +96,7 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { protected final Logger log = LoggerFactory.getLogger(getClass()); private static ActorSystem system; + private static SchemaContext SCHEMA_CONTEXT; private final Configuration configuration = new MockConfiguration() { Map strategyMap = ImmutableMap.builder().put( @@ -106,7 +108,7 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { @Override public YangInstanceIdentifier getPrefixForPath(final YangInstanceIdentifier path) { - return YangInstanceIdentifier.EMPTY; + return YangInstanceIdentifier.empty(); } }).put( CarsModel.BASE_QNAME.getLocalName(), new ShardStrategy() { @@ -117,7 +119,7 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { @Override public YangInstanceIdentifier getPrefixForPath(final YangInstanceIdentifier path) { - return YangInstanceIdentifier.EMPTY; + return YangInstanceIdentifier.empty(); } }).build(); @@ -138,12 +140,10 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { }; @Mock - protected ActorContext mockActorContext; + protected ActorUtils mockActorContext; protected TransactionContextFactory mockComponentFactory; - private SchemaContext schemaContext; - @Mock private ClusterWrapper mockClusterWrapper; @@ -154,37 +154,38 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { .operationTimeoutInSeconds(operationTimeoutInSeconds); @BeforeClass - public static void setUpClass() throws IOException { + public static void setUpClass() { Config config = ConfigFactory.parseMap(ImmutableMap.builder() .put("akka.actor.default-dispatcher.type", "akka.testkit.CallingThreadDispatcherConfigurator").build()) .withFallback(ConfigFactory.load()); system = ActorSystem.create("test", config); + SCHEMA_CONTEXT = TestModel.createTestContext(); } @AfterClass - public static void tearDownClass() throws IOException { + public static void tearDownClass() { TestKit.shutdownActorSystem(system); system = null; + SCHEMA_CONTEXT = null; } @Before public void setUp() { MockitoAnnotations.initMocks(this); - schemaContext = TestModel.createTestContext(); - doReturn(getSystem()).when(mockActorContext).getActorSystem(); doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher(); doReturn(MemberName.forName(memberName)).when(mockActorContext).getCurrentMemberName(); doReturn(new ShardStrategyFactory(configuration, LogicalDatastoreType.CONFIGURATION)).when(mockActorContext).getShardStrategyFactory(); - doReturn(schemaContext).when(mockActorContext).getSchemaContext(); + doReturn(SCHEMA_CONTEXT).when(mockActorContext).getSchemaContext(); doReturn(new Timeout(operationTimeoutInSeconds, TimeUnit.SECONDS)).when(mockActorContext).getOperationTimeout(); doReturn(mockClusterWrapper).when(mockActorContext).getClusterWrapper(); doReturn(mockClusterWrapper).when(mockActorContext).getClusterWrapper(); doReturn(dataStoreContextBuilder.build()).when(mockActorContext).getDatastoreContext(); + doReturn(new Timeout(5, TimeUnit.SECONDS)).when(mockActorContext).getTransactionCommitOperationTimeout(); final ClientIdentifier mockClientId = MockIdentifiers.clientIdentifier(getClass(), memberName); mockComponentFactory = new TransactionContextFactory(mockActorContext, mockClientId); @@ -199,31 +200,26 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { protected CreateTransaction eqCreateTransaction(final String expMemberName, final TransactionType type) { - ArgumentMatcher matcher = new ArgumentMatcher() { + class CreateTransactionArgumentMatcher implements ArgumentMatcher { @Override - public boolean matches(final Object argument) { - if (CreateTransaction.class.equals(argument.getClass())) { - CreateTransaction obj = CreateTransaction.fromSerializable(argument); - return obj.getTransactionId().getHistoryId().getClientId().getFrontendId().getMemberName() - .getName().equals(expMemberName) && obj.getTransactionType() == type.ordinal(); - } - - return false; + public boolean matches(CreateTransaction argument) { + return argument.getTransactionId().getHistoryId().getClientId().getFrontendId().getMemberName() + .getName().equals(expMemberName) && argument.getTransactionType() == type.ordinal(); } - }; + } - return argThat(matcher); + return argThat(new CreateTransactionArgumentMatcher()); } protected DataExists eqDataExists() { - ArgumentMatcher matcher = new ArgumentMatcher() { + class DataExistsArgumentMatcher implements ArgumentMatcher { @Override - public boolean matches(final Object argument) { - return argument instanceof DataExists && ((DataExists)argument).getPath().equals(TestModel.TEST_PATH); + public boolean matches(DataExists argument) { + return argument.getPath().equals(TestModel.TEST_PATH); } - }; + } - return argThat(matcher); + return argThat(new DataExistsArgumentMatcher()); } protected ReadData eqReadData() { @@ -231,14 +227,14 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { } protected ReadData eqReadData(final YangInstanceIdentifier path) { - ArgumentMatcher matcher = new ArgumentMatcher() { + class ReadDataArgumentMatcher implements ArgumentMatcher { @Override - public boolean matches(final Object argument) { - return argument instanceof ReadData && ((ReadData)argument).getPath().equals(path); + public boolean matches(ReadData argument) { + return argument.getPath().equals(path); } - }; + } - return argThat(matcher); + return argThat(new ReadDataArgumentMatcher()); } protected Future readyTxReply(final String path) { @@ -386,23 +382,15 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { shardName); } - @SuppressWarnings("checkstyle:avoidHidingCauseException") - protected void propagateReadFailedExceptionCause(final CheckedFuture future) - throws Exception { + @SuppressWarnings({"checkstyle:avoidHidingCauseException", "checkstyle:IllegalThrows"}) + protected void propagateReadFailedExceptionCause(final FluentFuture future) throws Throwable { try { - future.checkedGet(5, TimeUnit.SECONDS); + future.get(5, TimeUnit.SECONDS); fail("Expected ReadFailedException"); - } catch (ReadFailedException e) { - assertNotNull("Expected a cause", e.getCause()); - Throwable cause; - if (e.getCause().getCause() != null) { - cause = e.getCause().getCause(); - } else { - cause = e.getCause(); - } - - Throwables.propagateIfPossible(cause, Exception.class); - throw new RuntimeException(cause); + } catch (ExecutionException e) { + final Throwable cause = e.getCause(); + assertTrue("Unexpected cause: " + cause.getClass(), cause instanceof ReadFailedException); + throw Throwables.getRootCause(cause); } } @@ -470,7 +458,7 @@ public abstract class AbstractTransactionProxyTest extends AbstractTest { for (Future future : proxy.getCohortFutures()) { assertNotNull("Ready operation Future is null", future); try { - futureResults.add(Await.result(future, Duration.create(5, TimeUnit.SECONDS))); + futureResults.add(Await.result(future, FiniteDuration.create(5, TimeUnit.SECONDS))); } catch (Exception e) { futureResults.add(e); }