aboutsummaryrefslogtreecommitdiffstats
path: root/cps-path-parser/src
diff options
context:
space:
mode:
authoremaclee <lee.anjella.macabuhay@est.tech>2022-12-21 09:29:24 +0000
committeremaclee <lee.anjella.macabuhay@est.tech>2022-12-22 09:44:40 +0000
commit5452b5ea786a3e7def19c1f328c43ec264da38d5 (patch)
treeb7988ce69ee08cc3121c50785b7baac887dcf9ad /cps-path-parser/src
parente52d0cbf970f1de982fb64f1a052646457b81f52 (diff)
Add fix for posting nodes with xPath with '/'
- YangUtils method changed from using REGEX to cps path parser - unit test added for cps path util Issue-ID: CPS-1433 Signed-off-by: emaclee <lee.anjella.macabuhay@est.tech> Change-Id: Ibb9efdd09423f9bade4a4a557d7d9ed49aa44ef4
Diffstat (limited to 'cps-path-parser/src')
-rw-r--r--cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java11
-rw-r--r--cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java2
-rw-r--r--cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathUtil.java6
-rw-r--r--cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathUtilSpec.groovy20
4 files changed, 35 insertions, 4 deletions
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 7183120120..3a9d70ebbc 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
@@ -22,7 +22,9 @@ package org.onap.cps.cpspath.parser;
import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
import org.onap.cps.cpspath.parser.antlr4.CpsPathParser;
@@ -50,6 +52,8 @@ public class CpsPathBuilder extends CpsPathBaseListener {
boolean processingAncestorAxis = false;
+ private List<String> containerNames = new ArrayList<>();
+
@Override
public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) {
throw new PathParsingException(ctx.getText());
@@ -146,6 +150,7 @@ public class CpsPathBuilder extends CpsPathBaseListener {
CpsPathQuery build() {
cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
+ cpsPathQuery.setContainerNames(containerNames);
return cpsPathQuery;
}
@@ -155,10 +160,12 @@ public class CpsPathBuilder extends CpsPathBaseListener {
@Override
public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
+ final String containerName = ctx.getText();
normalizedXpathBuilder.append("/")
- .append(ctx.getText());
+ .append(containerName);
+ containerNames.add(containerName);
if (processingAncestorAxis) {
- normalizedAncestorPathBuilder.append("/").append(ctx.getText());
+ normalizedAncestorPathBuilder.append("/").append(containerName);
}
}
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 a9bd5d81c3..c9df8df904 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
@@ -22,6 +22,7 @@ package org.onap.cps.cpspath.parser;
import static org.onap.cps.cpspath.parser.CpsPathPrefixType.ABSOLUTE;
+import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Getter;
@@ -34,6 +35,7 @@ public class CpsPathQuery {
private String xpathPrefix;
private String normalizedParentPath;
private String normalizedXpath;
+ private List<String> containerNames;
private CpsPathPrefixType cpsPathPrefixType = ABSOLUTE;
private String descendantName;
private Map<String, Object> leavesData;
diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathUtil.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathUtil.java
index 283463b512..60f0e2efcd 100644
--- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathUtil.java
+++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathUtil.java
@@ -22,6 +22,7 @@ package org.onap.cps.cpspath.parser;
import static org.onap.cps.cpspath.parser.CpsPathPrefixType.ABSOLUTE;
+import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -60,6 +61,11 @@ public class CpsPathUtil {
return getCpsPathBuilder(xpathSource).build().getNormalizedParentPath();
}
+ public static String[] getXpathNodeIdSequence(final String xpathSource) {
+ final List<String> containerNames = getCpsPathBuilder(xpathSource).build().getContainerNames();
+ return containerNames.toArray(new String[containerNames.size()]);
+ }
+
/**
* 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/CpsPathUtilSpec.groovy b/cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathUtilSpec.groovy
index 31c1ed4a3a..d62f337b75 100644
--- a/cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathUtilSpec.groovy
+++ b/cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathUtilSpec.groovy
@@ -28,7 +28,7 @@ class CpsPathUtilSpec extends Specification {
when: 'xpath with #scenario is parsed'
def result = CpsPathUtil.getNormalizedXpath(xpath)
then: 'normalized path uses single quotes for leave values'
- result == "/parent/child[@common-leaf-name='123']"
+ assert result == "/parent/child[@common-leaf-name='123']"
where: 'the following xpaths are used'
scenario | xpath
'no quotes' | '/parent/child[@common-leaf-name=123]'
@@ -40,7 +40,7 @@ class CpsPathUtilSpec extends Specification {
when: 'a given xpath with #scenario is parsed'
def result = CpsPathUtil.getNormalizedParentXpath(xpath)
then: 'the result is the expected parent path'
- result == expectedParentPath
+ assert result == expectedParentPath
where: 'the following xpaths are used'
scenario | xpath || expectedParentPath
'no child' | '/parent' || ''
@@ -53,6 +53,22 @@ class CpsPathUtilSpec extends Specification {
'parent is list element using "' | '/parent/child[@id="x"]/grandChild' || "/parent/child[@id='x']"
}
+ def 'Get node ID sequence for given xpath'() {
+ when: 'a given xpath with #scenario is parsed'
+ def result = CpsPathUtil.getXpathNodeIdSequence(xpath)
+ then: 'the result is the expected node ID sequence'
+ assert result == expectedNodeIdSequence
+ where: 'the following xpaths are used'
+ scenario | xpath || expectedNodeIdSequence
+ 'no child' | '/parent' || ["parent"]
+ 'child and parent' | '/parent/child' || ["parent","child"]
+ 'grand child' | '/parent/child/grandChild' || ["parent","child","grandChild"]
+ 'parent & top is list element' | '/parent[@id=1]/child' || ["parent","child"]
+ 'parent is list element' | '/parent/child[@id=1]/grandChild' || ["parent","child","grandChild"]
+ 'parent is list element with /' | "/parent/child[@id='a/b']/grandChild" || ["parent","child","grandChild"]
+ 'parent is list element with [' | "/parent/child[@id='a[b']/grandChild" || ["parent","child","grandChild"]
+ }
+
def 'Recognizing (absolute) xpaths to List elements'() {
expect: 'check for list returns the correct values'
assert CpsPathUtil.isPathToListElement(xpath) == expectList