Simplify ensureParentsByMerge 03/112303/13
authorOleksandr Zharov <oleksandr.zharov@pantheon.tech>
Thu, 27 Jun 2024 13:15:08 +0000 (15:15 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 23 Jul 2024 08:43:00 +0000 (08:43 +0000)
Refactored MdsalRestconfTransaction#ensureParentsByMerge by using
some of the YangInstanceIdentifier methods.
Added test to verify merged path for refactored method.

JIRA: NETCONF-1329
Change-Id: I21b08f11bdfc710e264a371afe1d30acfdfa1e2c
Signed-off-by: Oleksandr Zharov <oleksandr.zharov@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfTransaction.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategyTest.java

index 57b0dc5796ce0bbcd36ddb28d721deedc8e5f338..389e60ddf9e2272a8665b94efa1beebfd0148107 100644 (file)
@@ -13,7 +13,6 @@ import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.fr
 
 import com.google.common.base.Throwables;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.ArrayList;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
@@ -144,28 +143,11 @@ final class MdsalRestconfTransaction extends RestconfTransaction {
     // FIXME: this method should only be invoked if we are crossing an implicit list.
     @Override
     void ensureParentsByMerge(final YangInstanceIdentifier path) {
-        final var normalizedPathWithoutChildArgs = new ArrayList<YangInstanceIdentifier.PathArgument>();
-        YangInstanceIdentifier rootNormalizedPath = null;
-
-        final var it = path.getPathArguments().iterator();
-
-        while (it.hasNext()) {
-            final var pathArgument = it.next();
-            if (rootNormalizedPath == null) {
-                rootNormalizedPath = YangInstanceIdentifier.of(pathArgument);
-            }
-
-            if (it.hasNext()) {
-                normalizedPathWithoutChildArgs.add(pathArgument);
-            }
-        }
-
-        if (normalizedPathWithoutChildArgs.isEmpty()) {
-            return;
+        final var parent = path.getParent();
+        if (parent != null) {
+            final var rootNormalizedPath = path.getAncestor(1);
+            merge(rootNormalizedPath, fromInstanceId(databind.modelContext(), parent));
         }
-
-        merge(rootNormalizedPath,
-            fromInstanceId(databind.modelContext(), YangInstanceIdentifier.of(normalizedPathWithoutChildArgs)));
     }
 
     @Override
index 060ea3ca98f1de546b4dfeb08c30f9c71fe52c4b..cbbb5e2db7d581689efb46c66c2f402f45ae2707 100644 (file)
@@ -13,8 +13,11 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
@@ -162,6 +165,22 @@ final class MdsalRestconfStrategyTest extends AbstractRestconfStrategyTest {
         assertNotNull(dataPutRequest.getResult());
     }
 
+    @Test
+    void testEnsureParentsByMerge() throws Exception {
+        doReturn(readWrite).when(dataBroker).newReadWriteTransaction();
+        doReturn(read).when(dataBroker).newReadOnlyTransaction();
+        doReturn(immediateFalseFluentFuture()).when(read).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
+        doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, GAP_IID, GAP_LEAF);
+        doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
+        final var strategy = spy(jukeboxDataOperations());
+        final var tx = spy(jukeboxDataOperations().prepareWriteExecution());
+        doReturn(tx).when(strategy).prepareWriteExecution();
+
+        strategy.putData(dataPutRequest, GAP_PATH, GAP_LEAF);
+        verify(tx).merge(eq(GAP_IID.getAncestor(1)), any());
+        assertNotNull(dataPutRequest.getResult());
+    }
+
     @Test
     void testPutListData() throws Exception {
         doReturn(readWrite).when(dataBroker).newReadWriteTransaction();