X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FShardDataTreeTest.java;h=d35b2dbc34a5d1dce7fcd7c02cf18dd94dcef07b;hb=0c60b58f901657a17dc2b2ad487e05c19a57298b;hp=f001bc372e25d461fdfa44cdc67ba9b67ebbf1fe;hpb=2b0ddf57e4d3d2e78637a6762ae6b5a05128a3a1;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java index f001bc372e..d35b2dbc34 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java @@ -1,8 +1,19 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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 static org.junit.Assert.assertNotNull; import com.google.common.base.Optional; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ExecutionException; import org.junit.Before; import org.junit.Test; @@ -10,11 +21,14 @@ import org.opendaylight.controller.md.cluster.datastore.model.CarsModel; import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel; import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -public class ShardDataTreeTest { +public class ShardDataTreeTest extends AbstractTest { SchemaContext fullSchema; @@ -25,12 +39,12 @@ public class ShardDataTreeTest { @Test public void testWrite() throws ExecutionException, InterruptedException { - modify(new ShardDataTree(fullSchema), false, true, true); + modify(new ShardDataTree(fullSchema, TreeType.OPERATIONAL), false, true, true); } @Test public void testMerge() throws ExecutionException, InterruptedException { - modify(new ShardDataTree(fullSchema), true, true, true); + modify(new ShardDataTree(fullSchema, TreeType.OPERATIONAL), true, true, true); } @@ -38,7 +52,7 @@ public class ShardDataTreeTest { assertEquals(fullSchema, shardDataTree.getSchemaContext()); - ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction("txn-1", null); + ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction(nextTransactionId()); DataTreeModification snapshot = transaction.getSnapshot(); @@ -58,7 +72,7 @@ public class ShardDataTreeTest { cohort.commit().get(); - ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction("txn-2", null); + ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction(nextTransactionId()); DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot(); @@ -72,4 +86,100 @@ public class ShardDataTreeTest { } + @Test + public void bug4359AddRemoveCarOnce() throws ExecutionException, InterruptedException { + ShardDataTree shardDataTree = new ShardDataTree(fullSchema, TreeType.OPERATIONAL); + + List candidates = new ArrayList<>(); + candidates.add(addCar(shardDataTree)); + candidates.add(removeCar(shardDataTree)); + + NormalizedNode expected = getCars(shardDataTree); + + applyCandidates(shardDataTree, candidates); + + NormalizedNode actual = getCars(shardDataTree); + + assertEquals(expected, actual); + } + + @Test + public void bug4359AddRemoveCarTwice() throws ExecutionException, InterruptedException { + ShardDataTree shardDataTree = new ShardDataTree(fullSchema, TreeType.OPERATIONAL); + + List candidates = new ArrayList<>(); + candidates.add(addCar(shardDataTree)); + candidates.add(removeCar(shardDataTree)); + candidates.add(addCar(shardDataTree)); + candidates.add(removeCar(shardDataTree)); + + NormalizedNode expected = getCars(shardDataTree); + + applyCandidates(shardDataTree, candidates); + + NormalizedNode actual = getCars(shardDataTree); + + assertEquals(expected, actual); + } + + private static NormalizedNode getCars(ShardDataTree shardDataTree) { + ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction(nextTransactionId()); + DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot(); + + Optional> optional = snapshot1.readNode(CarsModel.BASE_PATH); + + assertEquals(true, optional.isPresent()); + + return optional.get(); + } + + private static DataTreeCandidateTip addCar(ShardDataTree shardDataTree) throws ExecutionException, InterruptedException { + return doTransaction(shardDataTree, snapshot -> { + snapshot.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer()); + snapshot.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()); + snapshot.write(CarsModel.newCarPath("altima"), CarsModel.newCarEntry("altima", new BigInteger("100"))); + }); + } + + private static DataTreeCandidateTip removeCar(ShardDataTree shardDataTree) throws ExecutionException, InterruptedException { + return doTransaction(shardDataTree, snapshot -> snapshot.delete(CarsModel.newCarPath("altima"))); + } + + @FunctionalInterface + private static interface DataTreeOperation { + void execute(DataTreeModification snapshot); + } + + private static DataTreeCandidateTip doTransaction(ShardDataTree shardDataTree, DataTreeOperation operation) + throws ExecutionException, InterruptedException { + ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction(nextTransactionId()); + DataTreeModification snapshot = transaction.getSnapshot(); + operation.execute(snapshot); + ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction); + + cohort.canCommit().get(); + cohort.preCommit().get(); + DataTreeCandidateTip candidate = cohort.getCandidate(); + cohort.commit().get(); + + return candidate; + } + + private static DataTreeCandidateTip applyCandidates(ShardDataTree shardDataTree, List candidates) + throws ExecutionException, InterruptedException { + ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction(nextTransactionId()); + DataTreeModification snapshot = transaction.getSnapshot(); + for(DataTreeCandidateTip candidateTip : candidates){ + DataTreeCandidates.applyToModification(snapshot, candidateTip); + } + ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction); + + cohort.canCommit().get(); + cohort.preCommit().get(); + DataTreeCandidateTip candidate = cohort.getCandidate(); + cohort.commit().get(); + + return candidate; + } + } \ No newline at end of file