From d7bc158cd274b3d6cd01bcad86aef258e6880c1c Mon Sep 17 00:00:00 2001 From: Rudrangi Anupriya Date: Mon, 17 Apr 2023 14:19:46 +0530 Subject: Add contains condition support to cps-path Issue-ID: CPS-1272 Change-Id: Ic81d1322cacc64a8752916324b801d02be47d34f Signed-off-by: Rudrangi Anupriya --- .../main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4 | 8 +++++++- .../main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java | 6 ++++++ .../main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java | 11 +++++++++++ .../org/onap/cps/cpspath/parser/CpsPathQuerySpec.groovy | 11 +++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) (limited to 'cps-path-parser') 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 @@ -149,6 +149,12 @@ public class CpsPathBuilder extends CpsPathBaseListener { cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText())); } + @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); 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 booleanOperatorsType; + private String containsFunctionConditionLeafName; + private String containsFunctionConditionValue; /** * Returns a cps path query. @@ -82,6 +84,15 @@ public class CpsPathQuery { return textFunctionConditionLeafName != null; } + /** + * 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. * 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) -- cgit 1.2.3-korg