diff options
Diffstat (limited to 'cps-path-parser')
-rw-r--r-- | cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathUtil.java | 7 | ||||
-rw-r--r-- | cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathUtilSpec.groovy | 16 |
2 files changed, 18 insertions, 5 deletions
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 4ede0d9c90..2c896dc3cd 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 @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2022-2024 Nordix Foundation + * Modifications Copyright (C) 2025 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +40,9 @@ import org.onap.cps.cpspath.parser.antlr4.CpsPathParser; @NoArgsConstructor(access = AccessLevel.PACKAGE) public class CpsPathUtil { + public static final String ROOT_NODE_XPATH = "/"; + public static final String NO_PARENT_PATH = ""; + /** * Returns a normalized xpath path query. * @@ -46,6 +50,9 @@ public class CpsPathUtil { * @return a normalized xpath String. */ public static String getNormalizedXpath(final String xpathSource) { + if (ROOT_NODE_XPATH.equals(xpathSource)) { + return NO_PARENT_PATH; + } return getCpsPathBuilder(xpathSource).build().getNormalizedXpath(); } 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 29bb3c7b58..03aecc2acd 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 @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2022-2024 Nordix Foundation + * Modifications Copyright (C) 2025 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +25,11 @@ import spock.lang.Specification class CpsPathUtilSpec extends Specification { + def 'Normalized xpath for root.'() { + expect: 'root node xpath is parsed' + assert CpsPathUtil.getNormalizedXpath('/') == '' + } + def 'Normalized xpaths for list index values using #scenario'() { when: 'xpath with #scenario is parsed' def result = CpsPathUtil.getNormalizedXpath(xpath) @@ -36,7 +42,7 @@ class CpsPathUtilSpec extends Specification { 'single quotes' | "/parent/child[@common-leaf-name='123']" } - def 'Normalized parent paths of absolute paths'() { + def 'Normalized parent paths of absolute paths.'() { when: 'a given cps path is parsed' def result = CpsPathUtil.getNormalizedParentXpath(cpsPath) then: 'the result is the expected parent path' @@ -54,7 +60,7 @@ class CpsPathUtilSpec extends Specification { '/parent/child/name[text()="value"]' || '/parent' } - def 'Normalized parent paths of descendant paths'() { + def 'Normalized parent paths of descendant paths.'() { when: 'a given cps path is parsed' def result = CpsPathUtil.getNormalizedParentXpath(cpsPath) then: 'the result is the expected parent path' @@ -72,7 +78,7 @@ class CpsPathUtilSpec extends Specification { '//parent/child/name[text()="value"]' || '//parent' } - def 'Get node ID sequence for given xpath'() { + def 'Get node ID sequence for given xpath with #scenario.'() { when: 'a given xpath with #scenario is parsed' def result = CpsPathUtil.getXpathNodeIdSequence(xpath) then: 'the result is the expected node ID sequence' @@ -89,7 +95,7 @@ class CpsPathUtilSpec extends Specification { 'does not include ancestor node' | '/parent/child/ancestor::grandparent' || ["parent","child"] } - def 'Recognizing (absolute) xpaths to List elements'() { + def 'Recognizing (absolute) xpaths to List elements.'() { expect: 'check for list returns the correct values' assert CpsPathUtil.isPathToListElement(xpath) == expectList where: 'the following xpaths are used' @@ -101,7 +107,7 @@ class CpsPathUtilSpec extends Specification { '/parent/ancestor::grandparent[@id=1]' || false } - def 'Parsing Exception'() { + def 'Parsing Exception.'() { when: 'a invalid xpath is parsed' CpsPathUtil.getNormalizedXpath('///') then: 'a path parsing exception is thrown' |