private static void applyToCursorAwareModification(final CursorAwareDataTreeModification modification,
final DataTreeCandidate candidate) {
final YangInstanceIdentifier candidatePath = candidate.getRootPath();
- if (candidatePath.isEmpty()) {
+ final YangInstanceIdentifier parent = candidatePath.getParent();
+ if (parent == null) {
try (DataTreeModificationCursor cursor = modification.openCursor()) {
DataTreeCandidateNodes.applyRootToCursor(cursor, candidate.getRootNode());
}
} else {
- try (DataTreeModificationCursor cursor = modification.openCursor(candidatePath.getParent()).get()) {
+ try (DataTreeModificationCursor cursor = modification.openCursor(parent).orElseThrow()) {
DataTreeCandidateNodes.applyRootedNodeToCursor(cursor, candidatePath, candidate.getRootNode());
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
doReturn(mockedRootPath).when(mockedDataTreeCandidate).getRootPath();
+ final YangInstanceIdentifier mockedRootPathParent = mock(YangInstanceIdentifier.class);
+ doReturn(mockedRootPathParent).when(mockedRootPath).getParent();
+
final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
- doReturn(Optional.of(mockedCursor)).when(mockedModification).openCursor(isNull());
+ doReturn(Optional.of(mockedCursor)).when(mockedModification).openCursor(mockedRootPathParent);
final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
doReturn(mockedDataTreeCandidateNode).when(mockedDataTreeCandidate).getRootNode();
doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
DataTreeCandidates.applyToModification(mockedModification, mockedDataTreeCandidate);
- verify(mockedModification, times(1)).openCursor(isNull());
+ verify(mockedRootPath, times(1)).getParent();
+ verify(mockedModification, times(1)).openCursor(mockedRootPathParent);
verify(mockedCursor, times(1)).delete(isNull());
+ }
- doReturn(Boolean.TRUE).when(mockedRootPath).isEmpty();
- try {
- DataTreeCandidates.applyToModification(mockedModification, mockedDataTreeCandidate);
- fail("An IllegalArgumentException should have been thrown!");
- } catch (IllegalArgumentException ex) {
- assertTrue(ex.getMessage().contains("Can not delete root"));
- }
+ @Test
+ public void testApplyToCursorAwareModificationRoot() {
+ final DataTreeCandidate mockedDataTreeCandidate = mock(DataTreeCandidate.class);
+ final CursorAwareDataTreeModification mockedModification = mock(CursorAwareDataTreeModification.class);
+ final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
+ final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
+ doReturn(mockedRootPath).when(mockedDataTreeCandidate).getRootPath();
+ doReturn(null).when(mockedRootPath).getParent();
+ doReturn(mockedDataTreeCandidateNode).when(mockedDataTreeCandidate).getRootNode();
+ doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
+
+ IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
+ () -> DataTreeCandidates.applyToModification(mockedModification, mockedDataTreeCandidate));
+ assertEquals("Can not delete root.", thrown.getMessage());
}
@Test