From a4cc3554804540b49d9b778f75b158d6c1ec5899 Mon Sep 17 00:00:00 2001 From: "puthuparambil.aditya" Date: Wed, 28 Apr 2021 16:39:32 +0100 Subject: [PATCH] Fix Get descendent to support xpaths that end in list values Issue-ID: CPS-367 Signed-off-by: puthuparambil.aditya Change-Id: I3234afd8b8b69c5a3c87db1669d9b304f9fcaa49 --- .../cps/spi/repository/FragmentRepository.java | 5 ++- .../CpsDataPersistenceQueryDataNodeSpec.groovy | 44 +++++++++++++++------- cps-ri/src/test/resources/data/fragment.sql | 4 +- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index 74d04613d..b9874484c 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -76,8 +76,9 @@ public interface FragmentRepository extends JpaRepository List getByAnchorAndXpathEndsInDescendantName(@Param("anchor") int anchorId, @Param("descendantName") String descendantName); - @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE CONCAT('%/',:descendantName) " - + "AND attributes @> :leafDataAsJson\\:\\:jsonb", nativeQuery = true) + @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND (xpath LIKE CONCAT('%/',:descendantName) OR " + + "xpath LIKE CONCAT('%/', :descendantName,'\\[@%]')) AND attributes @> :leafDataAsJson\\:\\:jsonb", + nativeQuery = true) // Above query will match the anchor id, last descendant name and all parameters passed into leafDataASJson with the // attribute values of the requested data node eg: {"leaf_name":"value", "another_leaf_name":"another value"}​​​​​​ List getByAnchorAndDescendentNameAndLeafValues(@Param("anchor") int anchorId, diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy index bfc088db2..8acfe783d 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy @@ -76,7 +76,6 @@ class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase { return flatMap } - @Sql([CLEAR_DATA, SET_DATA]) def 'Cps Path query for single leaf value with type: #type.'() { when: 'a query is executed to get a data node by the given cps path' @@ -120,14 +119,14 @@ class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase { } @Sql([CLEAR_DATA, SET_DATA]) - def 'Cps Path query using descendant anywhere with %scenario '() { + def 'Cps Path query using descendant anywhere with #scenario '() { when: 'a query is executed to get a data node by the given cps path' def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS) then: 'the correct number of data nodes are retrieved' result.size() == expectedXPaths.size() and: 'xpaths of the retrieved data nodes are as expected' for(int i = 0; i