Fix Absolute Path to list with Integer/String key
[cps.git] / cps-path-parser / src / test / groovy / org / onap / cps / cpspath / parser / CpsPathQuerySpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-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 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.ABSOLUTE
26 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT
27
28 class CpsPathQuerySpec extends Specification {
29
30     def 'Parse cps path with valid cps path and a filter with #scenario.'() {
31         when: 'the given cps path is parsed'
32             def result = CpsPathQuery.createFrom(cpsPath)
33         then: 'the query has the right xpath type'
34             result.cpsPathPrefixType == ABSOLUTE
35         and: 'the right query parameters are set'
36             result.xpathPrefix == expectedXpathPrefix
37             result.hasLeafConditions()
38             result.leavesData.containsKey(expectedLeafName)
39             result.leavesData.get(expectedLeafName) == expectedLeafValue
40         where: 'the following data is used'
41             scenario               | cpsPath                                                    || expectedXpathPrefix                             | expectedLeafName       | expectedLeafValue
42             'leaf of type String'  | '/parent/child[@common-leaf-name="common-leaf-value"]'     || '/parent/child'                                 | 'common-leaf-name'     | 'common-leaf-value'
43             'leaf of type String'  | '/parent/child[@common-leaf-name=\'common-leaf-value\']'   || '/parent/child'                                 | 'common-leaf-name'     | 'common-leaf-value'
44             'leaf of type Integer' | '/parent/child[@common-leaf-name-int=5]'                   || '/parent/child'                                 | 'common-leaf-name-int' | 5
45             'spaces around ='      | '/parent/child[@common-leaf-name-int = 5]'                 || '/parent/child'                                 | 'common-leaf-name-int' | 5
46             'key in top container' | '/parent[@common-leaf-name-int=5]'                         || '/parent'                                       | 'common-leaf-name-int' | 5
47             'parent list'          | '/shops/shop[@id=1]/categories[@id=1]/book[@title="Dune"]' || "/shops/shop[@id='1']/categories[@id='1']/book" | 'title'                | 'Dune'
48     }
49
50     def 'Parse cps path of type ends with a #scenario.'() {
51         when: 'the given cps path is parsed'
52             def result = CpsPathQuery.createFrom(cpsPath)
53         then: 'the query has the right xpath type'
54             result.cpsPathPrefixType == DESCENDANT
55         and: 'the right ends with parameters are set'
56             result.descendantName == expectedDescendantName
57         where: 'the following data is used'
58             scenario         | cpsPath          || expectedDescendantName
59             'yang container' | '//cps-path'     || 'cps-path'
60             'parent & child' | '//parent/child' || 'parent/child'
61     }
62
63     def 'Parse cps path to form the Normalized cps path containing #scenario.'() {
64         when: 'the given cps path is parsed'
65             def result = CpsPathUtil.getCpsPathQuery(cpsPath)
66         then: 'the query has the right normalized xpath type'
67             assert result.normalizedXpath == expectedNormalizedXPath
68         where: 'the following data is used'
69             scenario                                              | cpsPath                                         || expectedNormalizedXPath
70             'yang container'                                      | '/cps-path'                                     || '/cps-path'
71             'descendant anywhere'                                 | '//cps-path'                                    || '//cps-path'
72             'descendant with leaf condition'                      | '//cps-path[@key=1]'                            || "//cps-path[@key='1']"
73             'descendant with leaf value and ancestor'             | '//cps-path[@key=1]/ancestor:parent[@key=1]'    || "//cps-path[@key='1']/ancestor:parent[@key='1']"
74             'parent & child'                                      | '/parent/child'                                 || '/parent/child'
75             'parent leaf of type Integer & child'                 | '/parent/child[@code=1]/child2'                 || "/parent/child[@code='1']/child2"
76             'parent leaf with double quotes'                      | '/parent/child[@code="1"]/child2'               || "/parent/child[@code='1']/child2"
77             'parent leaf with double quotes inside single quotes' | '/parent/child[@code=\'"1"\']/child2'           || "/parent/child[@code='\"1\"']/child2"
78             'parent leaf with single quotes inside double quotes' | '/parent/child[@code="\'1\'"]/child2'           || "/parent/child[@code='\\\'1\\\'']/child2"
79             'leaf with single quotes inside double quotes'        | '/parent/child[@code="\'1\'"]'                  || "/parent/child[@code='\\\'1\\\'']"
80             'leaf with more than one attribute'                   | '/parent/child[@key1=1 and @key2="abc"]'        || "/parent/child[@key1='1' and @key2='abc']"
81             'parent & child with more than one attribute'         | '/parent/child[@key1=1 and @key2="abc"]/child2' || "/parent/child[@key1='1' and @key2='abc']/child2"
82     }
83
84     def 'Parse xpath to form the Normalized xpath containing #scenario.'() {
85         when: 'the given xpath is parsed'
86             def result = CpsPathUtil.getNormalizedXpath(xPath)
87         then: 'the query has the right normalized xpath type'
88             assert result == expectedNormalizedXPath
89         where: 'the following data is used'
90             scenario               | xPath      || expectedNormalizedXPath
91             'yang container'       | '/xpath'   || '/xpath'
92             'descendant anywhere'  | '//xpath'  || '//xpath'
93     }
94
95     def 'Parse cps path that ends with a yang list containing #scenario.'() {
96         when: 'the given cps path is parsed'
97             def result = CpsPathQuery.createFrom(cpsPath)
98         then: 'the query has the right xpath type'
99             result.cpsPathPrefixType == DESCENDANT
100         and: 'the right parameters are set'
101             result.descendantName == "child"
102             result.leavesData.size() == expectedNumberOfLeaves
103         where: 'the following data is used'
104             scenario                  | cpsPath                                            || expectedNumberOfLeaves
105             'one attribute'           | '//child[@common-leaf-name-int=5]'                 || 1
106             'more than one attribute' | '//child[@int-leaf=5 and @leaf-name="leaf value"]' || 2
107     }
108
109     def 'Parse #scenario cps path with text function condition'() {
110         when: 'the given cps path is parsed'
111             def result = CpsPathQuery.createFrom(cpsPath)
112         then: 'the query has the right xpath type'
113             result.cpsPathPrefixType == DESCENDANT
114         and: 'leaf conditions are only present when expected'
115             result.hasLeafConditions() == expectLeafConditions
116         and: 'the right text function condition is set'
117             result.hasTextFunctionCondition()
118             result.textFunctionConditionLeafName == 'leaf-name'
119             result.textFunctionConditionValue == 'search'
120         and: 'the ancestor is only present when expected'
121             assert result.hasAncestorAxis() == expectHasAncestorAxis
122         where: 'the following data is used'
123             scenario                                  | cpsPath                                                              || expectLeafConditions | expectHasAncestorAxis
124             'descendant anywhere'                     | '//someContainer/leaf-name[text()="search"]'                         || false                | false
125             'descendant with leaf value'              | '//child[@other-leaf=1]/leaf-name[text()="search"]'                  || true                 | false
126             'descendant anywhere and ancestor'        | '//someContainer/leaf-name[text()="search"]/ancestor::parent'        || false                | true
127             'descendant with leaf value and ancestor' | '//child[@other-leaf=1]/leaf-name[text()="search"]/ancestor::parent' || true                 | true
128     }
129
130     def 'Parse cps path with error: #scenario.'() {
131         when: 'the given cps path is parsed'
132             CpsPathQuery.createFrom(cpsPath)
133         then: 'a CpsPathException is thrown'
134             thrown(PathParsingException)
135         where: 'the following data is used'
136             scenario                                                            | cpsPath
137             'no / at the start'                                                 | 'invalid-cps-path/child'
138             'additional / after descendant option'                              | '///cps-path'
139             'float value'                                                       | '/parent/child[@someFloat=5.0]'
140             'unmatched quotes, double quote first '                             | '/parent/child[@someString="value with unmatched quotes\']'
141             'unmatched quotes, single quote first'                              | '/parent/child[@someString=\'value with unmatched quotes"]'
142             'end with descendant and more than one attribute separated by "or"' | '//child[@int-leaf=5 or @leaf-name="leaf value"]'
143             'missing attribute value'                                           | '//child[@int-leaf=5 and @name]'
144             'incomplete ancestor value'                                         | '//books/ancestor::'
145             'invalid list element with missing ['                               | '/parent-206/child-206/grand-child-206@key="A"]'
146             'invalid list element with incorrect ]'                             | '/parent-206/child-206/grand-child-206]@key="A"]'
147             'invalid list element with incorrect ::'                            | '/parent-206/child-206/grand-child-206::@key"A"]'
148     }
149
150     def 'Parse cps path using ancestor by schema node identifier with a #scenario.'() {
151         when: 'the given cps path is parsed'
152             def result = CpsPathQuery.createFrom('//descendant/ancestor::' + ancestorPath)
153         then: 'the query has the right type'
154             result.cpsPathPrefixType == DESCENDANT
155         and: 'the result has ancestor axis'
156             result.hasAncestorAxis()
157         and: 'the correct ancestor schema node identifier is set'
158             result.ancestorSchemaNodeIdentifier == ancestorPath
159         and: 'there are no leaves conditions'
160             result.hasLeafConditions() == false
161         where:
162             scenario                                    | ancestorPath
163             'basic container'                           | 'someContainer'
164             'container with parent'                     | 'parent/child'
165             'ancestor that is a list'                   | "categories[@code='1']"
166             'ancestor that is a list with compound key' | "categories[@key1='1' and @key2='2']"
167             'parent that is a list'                     | "parent[@id='1']/child"
168     }
169
170     def 'Combinations #scenario.'() {
171         when: 'the given cps path is parsed'
172             def result = CpsPathQuery.createFrom(cpsPath + '/ancestor::someAncestor')
173         then: 'the query has the right type'
174             result.cpsPathPrefixType == DESCENDANT
175         and: 'leaf conditions are only present when expected'
176             result.hasLeafConditions() == expectLeafConditions
177         and: 'the result has ancestor axis'
178             result.hasAncestorAxis()
179         and: 'the correct ancestor schema node identifier is set'
180             result.ancestorSchemaNodeIdentifier == 'someAncestor'
181             result.descendantName == expectedDescendantName
182         where:
183             scenario                     | cpsPath                               || expectedDescendantName   | expectLeafConditions
184             'basic container'            | '//someContainer'                     || 'someContainer'          | false
185             'container with parent'      | '//parent/child'                      || 'parent/child'           | false
186             'container with list-parent' | '//parent[@id=1]/child'               || "parent[@id='1']/child"  | false
187             'container with list-parent' | '//parent[@id=1]/child[@name="test"]' || "parent[@id='1']/child"  | true
188     }
189 }