+ @Test
+ public void bug4359AddRemoveCarOnce() throws ExecutionException, InterruptedException {
+ ShardDataTree shardDataTree = new ShardDataTree(fullSchema, TreeType.OPERATIONAL);
+
+ List<DataTreeCandidateTip> 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<DataTreeCandidateTip> 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<NormalizedNode<?, ?>> 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<DataTreeCandidateTip> 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;
+ }
+