Fixup nullness in DataTreeCandidates 91/88491/7
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 18 Mar 2020 13:53:44 +0000 (14:53 +0100)
committerRobert Varga <nite@hq.sk>
Fri, 14 Aug 2020 18:51:37 +0000 (18:51 +0000)
getParent() non-nullness is implied by the depth check, which
unfortunately is not caught by automated tools. Reformulate it so
that we have an explicit guard.

Change-Id: I42415387529d5adab04a15cb07baf2eb4568231d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidates.java
yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidatesTest.java

index 4920b22a4b526e83378375fb7225d8d6432c62e1..f386541bd014959bbe6d35f17fbce9292c6c8a11 100644 (file)
@@ -364,12 +364,13 @@ public final class DataTreeCandidates {
     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());
             }
         }
index c5944c2066f989bca52ccbbd1edec96c666a4805..8acb38fdee60d309d2d4b6fea9a995b6d94667e1 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.data.api.schema.tree;
 
 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;
@@ -77,24 +78,36 @@ public class DataTreeCandidatesTest {
 
         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