NCMP: NCMP search API fixed to handle cps path that is ancestor axis as 02/138402/6
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Mon, 8 Jul 2024 16:40:38 +0000 (17:40 +0100)
committerSourabh Sourabh <sourabh.sourabh@est.tech>
Tue, 9 Jul 2024 15:20:30 +0000 (15:20 +0000)
well

- Cps path builder is fixed to handle ancestor axis that is target as well while quering.

Issue-ID: CPS-2308
Change-Id: Iaf215851ada17d21516ae83fa142ac77ff1c6c19
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java
cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathQuerySpec.groovy
integration-test/src/test/groovy/org/onap/cps/integration/functional/CpsQueryServiceIntegrationSpec.groovy

index de261e6..0bb0923 100644 (file)
@@ -165,6 +165,10 @@ public class CpsPathBuilder extends CpsPathBaseListener {
         cpsPathQuery.setContainerNames(containerNames);
         cpsPathQuery.setBooleanOperators(booleanOperators);
         cpsPathQuery.setComparativeOperators(comparativeOperators);
+        if (cpsPathQuery.hasAncestorAxis() && cpsPathQuery.getXpathPrefix()
+                .endsWith("/" + cpsPathQuery.getAncestorSchemaNodeIdentifier())) {
+            cpsPathQuery.setAncestorSchemaNodeIdentifier("");
+        }
         return cpsPathQuery;
     }
 
index ae7ee59..15f6b11 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2021-2023 Nordix Foundation
+ *  Copyright (C) 2021-2024 Nordix Foundation
  *  Modifications Copyright (C) 2023 TechMahindra Ltd
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -253,4 +253,21 @@ class CpsPathQuerySpec extends Specification {
             '/test[@name2="value2" and @name1="value1"]' || 'name2'               | 'name1'
     }
 
+    def 'Ancestor axis matching prefix'() {
+        when: 'building a cps path query'
+            def result = parseXPathAndBuild(xpath)
+        then: 'ancestor axis is removed when same as prefix'
+            assert result.hasAncestorAxis() == expectAncestorAxis
+        where: 'the following xpaths are used'
+            xpath                     || expectAncestorAxis
+            '//abc/def/ancestor::abc' || true
+            '//abc/def/ancestor::def' || false
+            '//abc/def/ancestor::ef'  || true
+        }
+
+    def parseXPathAndBuild(xpath) {
+        def cpsPathBuilder = CpsPathUtil.getCpsPathBuilder(xpath)
+        cpsPathBuilder.build()
+    }
+
 }
index 146ea95..ad3ebd8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2023 Nordix Foundation
+ *  Copyright (C) 2023-2024 Nordix Foundation
  *  Modifications Copyright (C) 2023 TechMahindra Ltd
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the 'License');
@@ -271,6 +271,7 @@ class CpsQueryServiceIntegrationSpec extends FunctionalSpecBase {
             'ancestor with parent list'                 | '//books/ancestor::bookstore/categories'              || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']", "/bookstore/categories[@code='5']"]
             'ancestor with parent list element'         | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
+            'ancestor same as target type'              | '//books/title[text()="Matilda"]/ancestor::books'     || ["/bookstore/categories[@code='1']/books[@title='Matilda']"]
     }
 
     def 'Cps Path query across anchors with #scenario descendants.'() {