+ @Test
+ public void bug4359AddRemoveCarOnce() throws ExecutionException, InterruptedException {
+ final ShardDataTree shardDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+
+ final List<DataTreeCandidateTip> candidates = new ArrayList<>();
+ candidates.add(addCar(shardDataTree));
+ candidates.add(removeCar(shardDataTree));
+
+ final NormalizedNode<?, ?> expected = getCars(shardDataTree);
+
+ applyCandidates(shardDataTree, candidates);
+
+ final NormalizedNode<?, ?> actual = getCars(shardDataTree);
+
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void bug4359AddRemoveCarTwice() throws ExecutionException, InterruptedException {
+ final ShardDataTree shardDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+
+ final List<DataTreeCandidateTip> candidates = new ArrayList<>();
+ candidates.add(addCar(shardDataTree));
+ candidates.add(removeCar(shardDataTree));
+ candidates.add(addCar(shardDataTree));
+ candidates.add(removeCar(shardDataTree));
+
+ final NormalizedNode<?, ?> expected = getCars(shardDataTree);
+
+ applyCandidates(shardDataTree, candidates);
+
+ final NormalizedNode<?, ?> actual = getCars(shardDataTree);
+
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testListenerNotifiedOnApplySnapshot() throws Exception {
+ final ShardDataTree shardDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+
+ DOMDataTreeChangeListener listener = mock(DOMDataTreeChangeListener.class);
+ shardDataTree.registerTreeChangeListener(CarsModel.CAR_LIST_PATH.node(CarsModel.CAR_QNAME), listener);
+
+ addCar(shardDataTree, "optima");
+
+ verifyOnDataTreeChanged(listener, dtc -> {
+ assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
+ assertEquals("getRootPath", CarsModel.newCarPath("optima"), dtc.getRootPath());
+ });
+
+ addCar(shardDataTree, "sportage");
+
+ verifyOnDataTreeChanged(listener, dtc -> {
+ assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
+ assertEquals("getRootPath", CarsModel.newCarPath("sportage"), dtc.getRootPath());
+ });
+
+ ShardDataTree newDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+ addCar(newDataTree, "optima");
+ addCar(newDataTree, "murano");
+
+ shardDataTree.applySnapshot(newDataTree.takeStateSnapshot());
+
+ Map<YangInstanceIdentifier, ModificationType> expChanges = Maps.newHashMap();
+ expChanges.put(CarsModel.newCarPath("optima"), ModificationType.WRITE);
+ expChanges.put(CarsModel.newCarPath("murano"), ModificationType.WRITE);
+ expChanges.put(CarsModel.newCarPath("sportage"), ModificationType.DELETE);
+ verifyOnDataTreeChanged(listener, dtc -> {
+ ModificationType expType = expChanges.remove(dtc.getRootPath());
+ assertNotNull("Got unexpected change for " + dtc.getRootPath(), expType);
+ assertEquals("getModificationType", expType, dtc.getRootNode().getModificationType());
+ });
+
+ if(!expChanges.isEmpty()) {
+ fail("Missing change notifications: " + expChanges);
+ }
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ private static void verifyOnDataTreeChanged(DOMDataTreeChangeListener listener, Consumer<DataTreeCandidate> callback) {
+ ArgumentCaptor<Collection> changes = ArgumentCaptor.forClass(Collection.class);
+ verify(listener, atLeastOnce()).onDataTreeChanged(changes.capture());
+ for(Collection list: changes.getAllValues()) {
+ for(Object dtc: list) {
+ callback.accept((DataTreeCandidate)dtc);
+ }
+ }
+
+ reset(listener);
+ }
+
+ private static NormalizedNode<?, ?> getCars(final ShardDataTree shardDataTree) {
+ final ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction(nextTransactionId());
+ final DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
+
+ final Optional<NormalizedNode<?, ?>> optional = snapshot1.readNode(CarsModel.BASE_PATH);
+
+ assertEquals(true, optional.isPresent());
+
+ return optional.get();
+ }
+
+ private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree) throws ExecutionException, InterruptedException {
+ return addCar(shardDataTree, "altima");
+ }
+
+ private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree, String name) 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(name), CarsModel.newCarEntry(name, new BigInteger("100")));
+ });
+ }
+
+ private static DataTreeCandidateTip removeCar(final 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(final ShardDataTree shardDataTree, final DataTreeOperation operation)
+ throws ExecutionException, InterruptedException {
+ final ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction(nextTransactionId());
+ final DataTreeModification snapshot = transaction.getSnapshot();
+ operation.execute(snapshot);
+ final ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction);
+
+ immediateCanCommit(cohort);
+ immediatePreCommit(cohort);
+ final DataTreeCandidateTip candidate = cohort.getCandidate();
+ immediateCommit(cohort);
+
+ return candidate;
+ }
+
+ private static DataTreeCandidateTip applyCandidates(final ShardDataTree shardDataTree, final List<DataTreeCandidateTip> candidates)
+ throws ExecutionException, InterruptedException {
+ final ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction(nextTransactionId());
+ final DataTreeModification snapshot = transaction.getSnapshot();
+ for(final DataTreeCandidateTip candidateTip : candidates){
+ DataTreeCandidates.applyToModification(snapshot, candidateTip);
+ }
+ final ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction);