Handle root xpaths in getDataNodes 83/132983/5
authordanielhanrahan <daniel.hanrahan@est.tech>
Thu, 19 Jan 2023 18:20:11 +0000 (18:20 +0000)
committerDaniel Hanrahan <daniel.hanrahan@est.tech>
Tue, 24 Jan 2023 10:24:09 +0000 (10:24 +0000)
Issue-ID: CPS-1458
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: I64abf97317afe4335c8d04169689ee1396e75860

cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy
cps-ri/src/test/groovy/org/onap/cps/spi/performance/CpsDataPersistenceServicePerfTest.groovy

index b85b30d..d2b7273 100644 (file)
@@ -265,17 +265,26 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
 
-        final Set<String> normalizedXpaths = new HashSet<>(xpaths.size());
-        for (final String xpath : xpaths) {
+        final Collection<String> nonRootXpaths = new HashSet<>(xpaths);
+        final boolean haveRootXpath = nonRootXpaths.removeIf(CpsDataPersistenceServiceImpl::isRootXpath);
+
+        final Collection<String> normalizedXpaths = new HashSet<>(nonRootXpaths.size());
+        for (final String xpath : nonRootXpaths) {
             try {
                 normalizedXpaths.add(CpsPathUtil.getNormalizedXpath(xpath));
             } catch (final PathParsingException e) {
                 log.warn("Error parsing xpath \"{}\" in getDataNodes: {}", xpath, e.getMessage());
             }
         }
+        final Collection<FragmentEntity> fragmentEntities =
+            new HashSet<>(fragmentRepository.findByAnchorAndMultipleCpsPaths(anchorEntity.getId(), normalizedXpaths));
+
+        if (haveRootXpath) {
+            final List<FragmentExtract> fragmentExtracts = fragmentRepository.getTopLevelFragments(dataspaceEntity,
+                anchorEntity);
+            fragmentEntities.addAll(FragmentEntityArranger.toFragmentEntityTrees(anchorEntity, fragmentExtracts));
+        }
 
-        final List<FragmentEntity> fragmentEntities =
-                fragmentRepository.findByAnchorAndMultipleCpsPaths(anchorEntity.getId(), normalizedXpaths);
         return toDataNodes(fragmentEntities, fetchDescendantsOption);
     }
 
index 6252fff..5f48469 100755 (executable)
@@ -20,6 +20,7 @@
  *  SPDX-License-Identifier: Apache-2.0
  *  ============LICENSE_END=========================================================
  */
+
 package org.onap.cps.spi.impl
 
 import com.fasterxml.jackson.databind.ObjectMapper
@@ -303,6 +304,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
             assert results.size() == expectedResultSize
         where: 'following parameters were used'
             scenario                               | inputXpaths                                     || expectedResultSize
+            '0 nodes'                              | []                                              || 0
             '1 node'                               | ["/parent-200"]                                 || 1
             '2 unique nodes'                       | ["/parent-200", "/parent-201"]                  || 2
             '3 unique nodes'                       | ["/parent-200", "/parent-201", "/parent-202"]   || 3
@@ -314,6 +316,10 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
             'existing and non-existing xpaths'     | ["/parent-200", "/NO-XPATH", "/parent-201"]     || 2
             'invalid xpath'                        | ["INVALID XPATH"]                               || 0
             'valid and invalid xpaths'             | ["/parent-200", "INVALID XPATH", "/parent-201"] || 2
+            'root xpath'                           | ["/"]                                           || 7
+            'empty (root) xpath'                   | [""]                                            || 7
+            'root and top-level xpaths'            | ["/", "/parent-200", "/parent-201"]             || 7
+            'root and child xpaths'                | ["/", "/parent-200/child-201"]                  || 8
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
index 2346239..0407490 100644 (file)
@@ -29,8 +29,6 @@ import org.onap.cps.spi.repository.FragmentRepository
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.test.context.jdbc.Sql
 
-import java.util.concurrent.TimeUnit
-
 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS