+ @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);
+ }
+