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 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 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 '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'
61 '/parent[@id=1]' || true
62 '/parent[@id=1]/child' || false
63 '/parent/child[@id=1]' || true
64 '//child[@id=1]' || false
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)