2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.cpspath.parser
23 import spock.lang.Specification
25 class CpsPathUtilSpec extends Specification {
27 def 'Normalized xpaths for list index values using #scenario'() {
28 when: 'xpath with #scenario is parsed'
29 def result = CpsPathUtil.getNormalizedXpath(xpath)
30 then: 'normalized path uses single quotes for leave values'
31 assert result == "/parent/child[@common-leaf-name='123']"
32 where: 'the following xpaths are used'
34 'no quotes' | '/parent/child[@common-leaf-name=123]'
35 'double quotes' | '/parent/child[@common-leaf-name="123"]'
36 'single quotes' | "/parent/child[@common-leaf-name='123']"
39 def 'Normalized parent xpaths'() {
40 when: 'a given xpath with #scenario is parsed'
41 def result = CpsPathUtil.getNormalizedParentXpath(xpath)
42 then: 'the result is the expected parent path'
43 assert result == expectedParentPath
44 where: 'the following xpaths are used'
45 scenario | xpath || expectedParentPath
46 'no child' | '/parent' || ''
47 'child and parent' | '/parent/child' || '/parent'
48 'grand child' | '/parent/child/grandChild' || '/parent/child'
49 'parent & top is list element' | '/parent[@id=1]/child' || "/parent[@id='1']"
50 'parent is list element' | '/parent/child[@id=1]/grandChild' || "/parent/child[@id='1']"
51 'parent is list element with /' | "/parent/child[@id='a/b']/grandChild" || "/parent/child[@id='a/b']"
52 'parent is list element with [' | "/parent/child[@id='a[b']/grandChild" || "/parent/child[@id='a[b']"
53 'parent is list element using "' | '/parent/child[@id="x"]/grandChild' || "/parent/child[@id='x']"
56 def 'Get node ID sequence for given xpath'() {
57 when: 'a given xpath with #scenario is parsed'
58 def result = CpsPathUtil.getXpathNodeIdSequence(xpath)
59 then: 'the result is the expected node ID sequence'
60 assert result == expectedNodeIdSequence
61 where: 'the following xpaths are used'
62 scenario | xpath || expectedNodeIdSequence
63 'no child' | '/parent' || ["parent"]
64 'child and parent' | '/parent/child' || ["parent","child"]
65 'grand child' | '/parent/child/grandChild' || ["parent","child","grandChild"]
66 'parent & top is list element' | '/parent[@id=1]/child' || ["parent","child"]
67 'parent is list element' | '/parent/child[@id=1]/grandChild' || ["parent","child","grandChild"]
68 'parent is list element with /' | "/parent/child[@id='a/b']/grandChild" || ["parent","child","grandChild"]
69 'parent is list element with [' | "/parent/child[@id='a[b']/grandChild" || ["parent","child","grandChild"]
72 def 'Recognizing (absolute) xpaths to List elements'() {
73 expect: 'check for list returns the correct values'
74 assert CpsPathUtil.isPathToListElement(xpath) == expectList
75 where: 'the following xpaths are used'
77 '/parent[@id=1]' || true
78 '/parent[@id=1]/child' || false
79 '/parent/child[@id=1]' || true
80 '//child[@id=1]' || false
83 def 'Parsing Exception'() {
84 when: 'a invalid xpath is parsed'
85 CpsPathUtil.getNormalizedXpath('///')
86 then: 'a path parsing exception is thrown'
87 thrown(PathParsingException)