Fix ancestor cps path to recognize ancestor list value 66/121066/1
authorniamhcore <niamh.core@est.tech>
Tue, 4 May 2021 08:52:19 +0000 (09:52 +0100)
committerniamhcore <niamh.core@est.tech>
Tue, 4 May 2021 09:03:27 +0000 (10:03 +0100)
Issue-ID: CPS-305

Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: I28f7f0c120a39190068a3192dccf0a1a6fbfeaf2

cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy

index ab135fd..343a088 100644 (file)
@@ -175,8 +175,8 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         final CpsPathQuery cpsPathQuery) {
         final Set<String> ancestorXpath = new HashSet<>();
         final var pattern =
-            Pattern.compile("(\\S*\\/" + cpsPathQuery.getAncestorSchemaNodeIdentifier() + REG_EX_FOR_OPTIONAL_LIST_INDEX
-                + "\\/\\S*");
+            Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
+                + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*");
         for (final FragmentEntity fragmentEntity : fragmentEntities) {
             final var matcher = pattern.matcher(fragmentEntity.getXpath());
             if (matcher.matches()) {
index 4bebff9..45cd2e6 100644 (file)
@@ -107,8 +107,8 @@ class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
             }
         where: 'the following data is used'
             scenario                    | cpsPath                                                                          || expectedXPaths
-            'one leaf'                  | '//child-202[@common-leaf-name-int=5]'                                           || ['/parent-200/child-202','/parent-201/child-202']
-            'trailing "and" is ignored' | '//child-202[@common-leaf-name-int=5 and]'                                       || ['/parent-200/child-202','/parent-201/child-202']
+            'one leaf'                  | '//child-202[@common-leaf-name-int=5]'                                           || ['/parent-200/child-202', '/parent-201/child-202']
+            'trailing "and" is ignored' | '//child-202[@common-leaf-name-int=5 and]'                                       || ['/parent-200/child-202', '/parent-201/child-202']
             'more than one leaf'        | '//child-202[@common-leaf-name-int=5 and @common-leaf-name="common-leaf value"]' || ['/parent-200/child-202']
             'leaves reversed in order'  | '//child-202[@common-leaf-name="common-leaf value" and @common-leaf-name-int=5]' || ['/parent-200/child-202']
     }
@@ -120,14 +120,14 @@ class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
         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<result.size(); i++) {
+            for (int i = 0; i < result.size(); i++) {
                 assert result[i].getXpath() == expectedXPaths[i]
             }
         where: 'the following data is used'
-            scenario                               | cpsPath                                                || expectedXPaths
-            'one partial key leaf'                 | '//child-203[@key1="A"]'                               || ['/parent-201/child-203[@key1="A" and @key2=1]','/parent-201/child-203[@key1="A" and @key2=2]']
-            'one non key leaf'                     | '//child-203[@other-leaf="other value"]'               || ['/parent-201/child-203[@key1="A" and @key2=2]']
-            'mix of partial key and non key leaf'  | '//child-203[@key1="A" and @other-leaf="leaf value"]'  || ['/parent-201/child-203[@key1="A" and @key2=1]']
+            scenario                              | cpsPath                                               || expectedXPaths
+            'one partial key leaf'                | '//child-203[@key1="A"]'                              || ['/parent-201/child-203[@key1="A" and @key2=1]', '/parent-201/child-203[@key1="A" and @key2=2]']
+            'one non key leaf'                    | '//child-203[@other-leaf="other value"]'              || ['/parent-201/child-203[@key1="A" and @key2=2]']
+            'mix of partial key and non key leaf' | '//child-203[@key1="A" and @other-leaf="leaf value"]' || ['/parent-201/child-203[@key1="A" and @key2=1]']
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
@@ -152,8 +152,9 @@ class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
                 assert result[i].getXpath() == expectedXPaths[i]
             }
         where: 'the following data is used'
-            scenario                                  | cpsPath                                                || expectedXPaths
+            scenario                                    | cpsPath                                                || expectedXPaths
             'multiple list-ancestors'                   | '//books/ancestor::categories'                         || ['/bookstore/books/categories[@name="SciFi"]', '/bookstore/magazines/categories[@name="kids"]']
+            'ancestor with list value'                  | '//books/ancestor::categories[@name="SciFi"]'          || ['/bookstore/books/categories[@name="SciFi"]']
             'one ancestor value'                        | '//books/ancestor::books'                              || ['/bookstore/books']
             'top ancestor'                              | '//books/ancestor::bookstore'                          || ['/bookstore']
             'list with index value in the xpath prefix' | '//categories[@name="kids"]/books/ancestor::bookstore' || ['/bookstore']