Fix Delete uses case with '/' in path
[cps.git] / cps-path-parser / src / test / groovy / org / onap / cps / cpspath / parser / CpsPathUtilSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.cpspath.parser
22
23 import spock.lang.Specification
24
25 class CpsPathUtilSpec extends Specification {
26
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             result == "/parent/child[@common-leaf-name='123']"
32         where: 'the following xpaths are used'
33             scenario        | xpath
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']"
37     }
38
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             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']"
54     }
55
56     def 'Recognizing (absolute) xpaths to List elements'() {
57         expect: 'check for list returns the correct values'
58             assert CpsPathUtil.isPathToListElement(xpath) == expectList
59         where: 'the following xpaths are used'
60             xpath                  || expectList
61             '/parent[@id=1]'       || true
62             '/parent[@id=1]/child' || false
63             '/parent/child[@id=1]' || true
64             '//child[@id=1]'       || false
65     }
66
67     def 'Parsing Exception'() {
68         when: 'a invalid xpath is parsed'
69             CpsPathUtil.getNormalizedXpath('///')
70         then: 'a path parsing exception is thrown'
71             thrown(PathParsingException)
72     }
73
74 }