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%2FAbstractShardTest.java;h=31f2b6b0674a6be403468f364487e861f111c00b;hp=45d625768940780624e476643c1f3faed141b609;hb=805e9821a737d305f7f591ae51055e475e26fcdc;hpb=12fcdfe39aa26dcba7fd3bb4d4c68e3d02e65c51 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java index 45d6257689..31f2b6b067 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java @@ -11,7 +11,8 @@ 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.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -66,7 +67,6 @@ import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore; import org.opendaylight.controller.md.cluster.datastore.model.CarsModel; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -80,10 +80,10 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailed import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import scala.concurrent.Await; import scala.concurrent.Future; -import scala.concurrent.duration.Duration; +import scala.concurrent.duration.FiniteDuration; /** * Abstract base for shard unit tests. @@ -91,23 +91,22 @@ import scala.concurrent.duration.Duration; * @author Thomas Pantelis */ public abstract class AbstractShardTest extends AbstractActorTest { - protected static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext(); - - private static final AtomicInteger NEXT_SHARD_NUM = new AtomicInteger(); + protected static final EffectiveModelContext SCHEMA_CONTEXT = TestModel.createTestContext(); + protected static final AtomicInteger SHARD_NUM = new AtomicInteger(); protected static final int HEARTBEAT_MILLIS = 100; - protected final ShardIdentifier shardID = ShardIdentifier.create("inventory", MemberName.forName("member-1"), - "config" + NEXT_SHARD_NUM.getAndIncrement()); - protected final Builder dataStoreContextBuilder = DatastoreContext.newBuilder() .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000) .shardHeartbeatIntervalInMillis(HEARTBEAT_MILLIS); protected final TestActorFactory actorFactory = new TestActorFactory(getSystem()); + protected final int nextShardNum = SHARD_NUM.getAndIncrement(); + protected final ShardIdentifier shardID = ShardIdentifier.create("inventory", MemberName.forName("member-1"), + "config" + nextShardNum); @Before - public void setUp() { + public void setUp() throws Exception { InMemorySnapshotStore.clear(); InMemoryJournal.clear(); } @@ -132,7 +131,7 @@ public abstract class AbstractShardTest extends AbstractActorTest { .schemaContextProvider(() -> SCHEMA_CONTEXT); } - protected void testRecovery(final Set listEntryKeys) throws Exception { + protected void testRecovery(final Set listEntryKeys, final boolean stopActorOnFinish) throws Exception { // Create the actor and wait for recovery complete. final int nListEntries = listEntryKeys.size(); @@ -150,26 +149,25 @@ public abstract class AbstractShardTest extends AbstractActorTest { } }; - final TestActorRef shard = TestActorRef.create(getSystem(), - Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), - "testRecovery"); + final TestActorRef shard = TestActorRef.create(getSystem(), Props.create(Shard.class, + new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), "testRecovery"); - assertEquals("Recovery complete", true, recoveryComplete.await(5, TimeUnit.SECONDS)); + assertTrue("Recovery complete", recoveryComplete.await(5, TimeUnit.SECONDS)); // Verify data in the data store. - final NormalizedNode outerList = readStore(shard, TestModel.OUTER_LIST_PATH); + final NormalizedNode outerList = readStore(shard, TestModel.OUTER_LIST_PATH); assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList); assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable", - outerList.getValue() instanceof Iterable); - for (final Object entry: (Iterable) outerList.getValue()) { + outerList.body() instanceof Iterable); + for (final Object entry: (Iterable) outerList.body()) { assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode", entry instanceof MapEntryNode); final MapEntryNode mapEntry = (MapEntryNode)entry; - final Optional> idLeaf = - mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME)); + final Optional idLeaf = + mapEntry.findChildByArg(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME)); assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent()); - final Object value = idLeaf.get().getValue(); + final Object value = idLeaf.get().body(); assertTrue("Unexpected value for leaf " + TestModel.ID_QNAME.getLocalName() + ": " + value, listEntryKeys.remove(value)); } @@ -185,7 +183,9 @@ public abstract class AbstractShardTest extends AbstractActorTest { assertEquals("Last applied", nListEntries, shard.underlyingActor().getShardMBean().getLastApplied()); - shard.tell(PoisonPill.getInstance(), ActorRef.noSender()); + if (stopActorOnFinish) { + shard.tell(PoisonPill.getInstance(), ActorRef.noSender()); + } } protected void verifyLastApplied(final TestActorRef shard, final long expectedValue) { @@ -206,22 +206,22 @@ public abstract class AbstractShardTest extends AbstractActorTest { final DataTree mock = mock(DataTree.class); doAnswer(invocation -> { - actual.validate(invocation.getArgumentAt(0, DataTreeModification.class)); + actual.validate(invocation.getArgument(0)); return null; }).when(mock).validate(any(DataTreeModification.class)); - doAnswer(invocation -> actual.prepare(invocation.getArgumentAt(0, DataTreeModification.class))).when( + doAnswer(invocation -> actual.prepare(invocation.getArgument(0))).when( mock).prepare(any(DataTreeModification.class)); doAnswer(invocation -> { - actual.commit(invocation.getArgumentAt(0, DataTreeCandidate.class)); + actual.commit(invocation.getArgument(0)); return null; }).when(mock).commit(any(DataTreeCandidate.class)); doAnswer(invocation -> { - actual.setSchemaContext(invocation.getArgumentAt(0, SchemaContext.class)); + actual.setEffectiveModelContext(invocation.getArgument(0)); return null; - }).when(mock).setSchemaContext(any(SchemaContext.class)); + }).when(mock).setEffectiveModelContext(any(EffectiveModelContext.class)); doAnswer(invocation -> actual.takeSnapshot()).when(mock).takeSnapshot(); @@ -273,7 +273,7 @@ public abstract class AbstractShardTest extends AbstractActorTest { } protected static BatchedModifications prepareBatchedModifications(final TransactionIdentifier transactionID, - final YangInstanceIdentifier path, final NormalizedNode data, final boolean doCommitOnReady) { + final YangInstanceIdentifier path, final NormalizedNode data, final boolean doCommitOnReady) { final MutableCompositeModification modification = new MutableCompositeModification(); modification.addModification(new WriteModification(path, data)); return prepareBatchedModifications(transactionID, modification, doCommitOnReady); @@ -281,43 +281,43 @@ public abstract class AbstractShardTest extends AbstractActorTest { protected static ForwardedReadyTransaction prepareForwardedReadyTransaction(final TestActorRef shard, final TransactionIdentifier transactionID, final YangInstanceIdentifier path, - final NormalizedNode data, final boolean doCommitOnReady) { + final NormalizedNode data, final boolean doCommitOnReady) { ReadWriteShardDataTreeTransaction rwTx = shard.underlyingActor().getDataStore() .newReadWriteTransaction(transactionID); rwTx.getSnapshot().write(path, data); return new ForwardedReadyTransaction(transactionID, CURRENT_VERSION, rwTx, doCommitOnReady, Optional.empty()); } - public static NormalizedNode readStore(final TestActorRef shard, + public static NormalizedNode readStore(final TestActorRef shard, final YangInstanceIdentifier id) { - return shard.underlyingActor().getDataStore().readNode(id).orNull(); + return shard.underlyingActor().getDataStore().readNode(id).orElse(null); } - public static NormalizedNode readStore(final DataTree store, final YangInstanceIdentifier id) { + public static NormalizedNode readStore(final DataTree store, final YangInstanceIdentifier id) { return store.takeSnapshot().readNode(id).orElse(null); } public void writeToStore(final TestActorRef shard, final YangInstanceIdentifier id, - final NormalizedNode node) throws InterruptedException, ExecutionException { + final NormalizedNode node) throws InterruptedException, ExecutionException { Future future = Patterns.ask(shard, newBatchedModifications(nextTransactionId(), id, node, true, true, 1), new Timeout(5, TimeUnit.SECONDS)); try { - Await.ready(future, Duration.create(5, TimeUnit.SECONDS)); + Await.ready(future, FiniteDuration.create(5, TimeUnit.SECONDS)); } catch (TimeoutException e) { throw new ExecutionException(e); } } public static void writeToStore(final ShardDataTree store, final YangInstanceIdentifier id, - final NormalizedNode node) throws DataValidationFailedException { + final NormalizedNode node) throws DataValidationFailedException { BatchedModifications batched = newBatchedModifications(nextTransactionId(), id, node, true, true, 1); DataTreeModification modification = store.getDataTree().takeSnapshot().newModification(); batched.apply(modification); store.notifyListeners(commitTransaction(store.getDataTree(), modification)); } - public static void writeToStore(final DataTree store, final YangInstanceIdentifier id, - final NormalizedNode node) throws DataValidationFailedException { + public static void writeToStore(final DataTree store, final YangInstanceIdentifier id, final NormalizedNode node) + throws DataValidationFailedException { final DataTreeModification transaction = store.takeSnapshot().newModification(); transaction.write(id, node); @@ -327,8 +327,8 @@ public abstract class AbstractShardTest extends AbstractActorTest { store.commit(candidate); } - public void mergeToStore(final ShardDataTree store, final YangInstanceIdentifier id, - final NormalizedNode node) throws DataValidationFailedException { + public void mergeToStore(final ShardDataTree store, final YangInstanceIdentifier id, final NormalizedNode node) + throws DataValidationFailedException { final BatchedModifications batched = new BatchedModifications(nextTransactionId(), CURRENT_VERSION); batched.addModification(new MergeModification(id, node)); batched.setReady(); @@ -346,7 +346,7 @@ public abstract class AbstractShardTest extends AbstractActorTest { writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)); - final NormalizedNode root = readStore(testStore, YangInstanceIdentifier.EMPTY); + final NormalizedNode root = readStore(testStore, YangInstanceIdentifier.empty()); InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create( new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)), @@ -363,7 +363,7 @@ public abstract class AbstractShardTest extends AbstractActorTest { } static BatchedModifications newBatchedModifications(final TransactionIdentifier transactionID, - final YangInstanceIdentifier path, final NormalizedNode data, final boolean ready, + final YangInstanceIdentifier path, final NormalizedNode data, final boolean ready, final boolean doCommitOnReady, final int messagesSent) { final BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION); batched.addModification(new WriteModification(path, data)); @@ -376,7 +376,7 @@ public abstract class AbstractShardTest extends AbstractActorTest { } static BatchedModifications newReadyBatchedModifications(final TransactionIdentifier transactionID, - final YangInstanceIdentifier path, final NormalizedNode data, + final YangInstanceIdentifier path, final NormalizedNode data, final SortedSet participatingShardNames) { final BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION); batched.addModification(new WriteModification(path, data)); @@ -387,18 +387,18 @@ public abstract class AbstractShardTest extends AbstractActorTest { @SuppressWarnings("unchecked") static void verifyOuterListEntry(final TestActorRef shard, final Object expIDValue) { - final NormalizedNode outerList = readStore(shard, TestModel.OUTER_LIST_PATH); + final NormalizedNode outerList = readStore(shard, TestModel.OUTER_LIST_PATH); assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList); assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable", - outerList.getValue() instanceof Iterable); - final Object entry = ((Iterable)outerList.getValue()).iterator().next(); + outerList.body() instanceof Iterable); + final Object entry = ((Iterable)outerList.body()).iterator().next(); assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode", entry instanceof MapEntryNode); final MapEntryNode mapEntry = (MapEntryNode)entry; - final Optional> idLeaf = - mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME)); + final Optional idLeaf = + mapEntry.findChildByArg(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME)); assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent()); - assertEquals(TestModel.ID_QNAME.getLocalName() + " value", expIDValue, idLeaf.get().getValue()); + assertEquals(TestModel.ID_QNAME.getLocalName() + " value", expIDValue, idLeaf.get().body()); } public static DataTreeCandidateTip mockCandidate(final String name) { @@ -416,7 +416,7 @@ public abstract class AbstractShardTest extends AbstractActorTest { final DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class, name); final DataTreeCandidateNode mockCandidateNode = mock(DataTreeCandidateNode.class, name + "-node"); doReturn(ModificationType.UNMODIFIED).when(mockCandidateNode).getModificationType(); - doReturn(YangInstanceIdentifier.EMPTY).when(mockCandidate).getRootPath(); + doReturn(YangInstanceIdentifier.empty()).when(mockCandidate).getRootPath(); doReturn(mockCandidateNode).when(mockCandidate).getRootNode(); return mockCandidate; } @@ -506,14 +506,14 @@ public abstract class AbstractShardTest extends AbstractActorTest { private static FutureCallback mockFutureCallback(final FutureCallback actual) { FutureCallback mock = mock(FutureCallback.class); doAnswer(invocation -> { - actual.onFailure(invocation.getArgumentAt(0, Throwable.class)); + actual.onFailure(invocation.getArgument(0)); return null; }).when(mock).onFailure(any(Throwable.class)); doAnswer(invocation -> { - actual.onSuccess((T) invocation.getArgumentAt(0, Throwable.class)); + actual.onSuccess(invocation.getArgument(0)); return null; - }).when(mock).onSuccess((T) any(Object.class)); + }).when(mock).onSuccess((T) nullable(Object.class)); return mock; }