diff options
author | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2023-04-17 14:19:46 +0530 |
---|---|---|
committer | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2023-04-21 13:44:18 +0530 |
commit | d7bc158cd274b3d6cd01bcad86aef258e6880c1c (patch) | |
tree | c94fa03528b6ed4fbb2672267482542dd59f7b73 /cps-path-parser/src | |
parent | c4621cbcb1f9aad97ba3389468546444d1e9f3dc (diff) |
Add contains condition support to cps-path
Issue-ID: CPS-1272
Change-Id: Ic81d1322cacc64a8752916324b801d02be47d34f
Signed-off-by: Rudrangi Anupriya <ra00745022@techmahindra.com>
Diffstat (limited to 'cps-path-parser/src')
4 files changed, 35 insertions, 1 deletions
diff --git a/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4 b/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4 index d4718111f6..86c1705617 100644 --- a/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4 +++ b/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4 @@ -21,7 +21,7 @@ grammar CpsPath ; -cpsPath : ( prefix | descendant | incorrectPrefix ) multipleLeafConditions? textFunctionCondition? ancestorAxis? invalidPostFix?; +cpsPath : ( prefix | descendant | incorrectPrefix ) multipleLeafConditions? textFunctionCondition? containsFunctionCondition? ancestorAxis? invalidPostFix?; ancestorAxis : SLASH KW_ANCESTOR COLONCOLON ancestorPath ; @@ -29,6 +29,8 @@ ancestorPath : yangElement ( SLASH yangElement)* ; textFunctionCondition : SLASH leafName OB KW_TEXT_FUNCTION EQ StringLiteral CB ; +containsFunctionCondition : OB KW_CONTAINS_FUNCTION OP AT leafName COMMA StringLiteral CP CB ; + parent : ( SLASH yangElement)* ; prefix : parent SLASH containerName ; @@ -65,6 +67,9 @@ COLONCOLON : '::' ; EQ : '=' ; OB : '[' ; SLASH : '/' ; +COMMA : ',' ; +OP : '(' ; +CP : ')' ; // KEYWORDS @@ -72,6 +77,7 @@ KW_ANCESTOR : 'ancestor' ; KW_AND : 'and' ; KW_TEXT_FUNCTION: 'text()' ; KW_OR : 'or' ; +KW_CONTAINS_FUNCTION: 'contains' ; IntegerLiteral : FragDigits ; // Add below type definitions for leafvalue comparision in https://jira.onap.org/browse/CPS-440 diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java index 4299d13081..854450c8bb 100644 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java +++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java @@ -150,6 +150,12 @@ public class CpsPathBuilder extends CpsPathBaseListener { } @Override + public void exitContainsFunctionCondition(final CpsPathParser.ContainsFunctionConditionContext ctx) { + cpsPathQuery.setContainsFunctionConditionLeafName(ctx.leafName().getText()); + cpsPathQuery.setContainsFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText())); + } + + @Override public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) { normalizedXpathBuilder.append(OPEN_BRACKET); if (processingAncestorAxis) { diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java index 2c96d91051..418b5ec55b 100644 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java +++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java @@ -44,6 +44,8 @@ public class CpsPathQuery { private String textFunctionConditionLeafName; private String textFunctionConditionValue; private List<String> booleanOperatorsType; + private String containsFunctionConditionLeafName; + private String containsFunctionConditionValue; /** * Returns a cps path query. @@ -83,6 +85,15 @@ public class CpsPathQuery { } /** + * Has contains function condition been included in cpsPath. + * + * @return boolean value. + */ + public boolean hasContainsFunctionCondition() { + return containsFunctionConditionLeafName != null; + } + + /** * Returns boolean indicating xpath is an absolute path to a list element. * * @return true if xpath is an absolute path to a list element diff --git a/cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathQuerySpec.groovy b/cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathQuerySpec.groovy index 153dfbe9ed..96fdf88cf6 100644 --- a/cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathQuerySpec.groovy +++ b/cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathQuerySpec.groovy @@ -136,6 +136,17 @@ class CpsPathQuerySpec extends Specification { 'descendant with leaf value and ancestor' | '//child[@other-leaf=1]/leaf-name[text()="search"]/ancestor::parent' || true | true } + def 'Parse #scenario cps path with contains function condition'() { + when: 'the given cps path is parsed' + def result = CpsPathQuery.createFrom('//someContainer[contains(@lang,"en")]') + then: 'the query has the right xpath type' + result.cpsPathPrefixType == DESCENDANT + and: 'the right contains function condition is set' + result.hasContainsFunctionCondition() + result.containsFunctionConditionLeafName == 'lang' + result.containsFunctionConditionValue == 'en' + } + def 'Parse cps path with error: #scenario.'() { when: 'the given cps path is parsed' CpsPathQuery.createFrom(cpsPath) |