Merge "Apostrophe handling in CpsPathParser"
[cps.git] / cps-path-parser / src / test / groovy / org / onap / cps / cpspath / parser / CpsPathQuerySpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2023 TechMahindra Ltd
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.cpspath.parser
23
24 import spock.lang.Specification
25
26 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.ABSOLUTE
27 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT
28
29 class CpsPathQuerySpec extends Specification {
30
31     def 'Parse cps path with valid cps path and a filter with #scenario.'() {
32         when: 'the given cps path is parsed'
33             def result = CpsPathQuery.createFrom(cpsPath)
34         then: 'the query has the right xpath type'
35             assert result.cpsPathPrefixType == ABSOLUTE
36         and: 'the right query parameters are set'
37             assert result.xpathPrefix == expectedXpathPrefix
38             assert result.hasLeafConditions()
39             assert result.leavesData[0].name == expectedLeafName
40             assert result.leavesData[0].value == expectedLeafValue
41         where: 'the following data is used'
42             scenario               | cpsPath                                                    || expectedXpathPrefix                             | expectedLeafName       | expectedLeafValue
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 String'  | '/parent/child[@common-leaf-name=\'common-leaf-value\']'   || '/parent/child'                                 | 'common-leaf-name'     | 'common-leaf-value'
45             'leaf of type Integer' | '/parent/child[@common-leaf-name-int=5]'                   || '/parent/child'                                 | 'common-leaf-name-int' | 5
46             'spaces around ='      | '/parent/child[@common-leaf-name-int = 5]'                 || '/parent/child'                                 | 'common-leaf-name-int' | 5
47             'key in top container' | '/parent[@common-leaf-name-int=5]'                         || '/parent'                                       | 'common-leaf-name-int' | 5
48             'parent list'          | '/shops/shop[@id=1]/categories[@id=1]/book[@title="Dune"]' || "/shops/shop[@id='1']/categories[@id='1']/book" | 'title'                | 'Dune'
49             "' in double quote"    | '/parent[@common-leaf-name="leaf\'value"]'                 || '/parent'                                       | 'common-leaf-name'     | "leaf'value"
50             "' in single quote"    | "/parent[@common-leaf-name='leaf''value']"                 || '/parent'                                       | 'common-leaf-name'     | "leaf'value"
51             '" in double quote'    | '/parent[@common-leaf-name="leaf""value"]'                 || '/parent'                                       | 'common-leaf-name'     | 'leaf"value'
52             '" in single quote'    | '/parent[@common-leaf-name=\'leaf"value\']'                || '/parent'                                       | 'common-leaf-name'     | 'leaf"value'
53     }
54
55     def 'Parse cps path of type ends with a #scenario.'() {
56         when: 'the given cps path is parsed'
57             def result = CpsPathQuery.createFrom(cpsPath)
58         then: 'the query has the right xpath type'
59             result.cpsPathPrefixType == DESCENDANT
60         and: 'the right ends with parameters are set'
61             result.descendantName == expectedDescendantName
62         where: 'the following data is used'
63             scenario         | cpsPath          || expectedDescendantName
64             'yang container' | '//cps-path'     || 'cps-path'
65             'parent & child' | '//parent/child' || 'parent/child'
66     }
67
68     def 'Parse cps path to form the Normalized cps path containing #scenario.'() {
69         when: 'the given cps path is parsed'
70             def result = CpsPathUtil.getCpsPathQuery(cpsPath)
71         then: 'the query has the right normalized xpath type'
72             assert result.normalizedXpath == expectedNormalizedXPath
73         where: 'the following data is used'
74             scenario                                                      | cpsPath                                                        || expectedNormalizedXPath
75             'yang container'                                              | '/cps-path'                                                    || '/cps-path'
76             'descendant anywhere'                                         | '//cps-path'                                                   || '//cps-path'
77             'descendant with leaf condition'                              | '//cps-path[@key=1]'                                           || "//cps-path[@key='1']"
78             'descendant with leaf condition has ">" operator'             | '//cps-path[@key>9]'                                           || "//cps-path[@key>'9']"
79             'descendant with leaf condition has "<" operator'             | '//cps-path[@key<10]'                                          || "//cps-path[@key<'10']"
80             'descendant with leaf condition has ">=" operator'            | '//cps-path[@key>=8]'                                          || "//cps-path[@key>='8']"
81             'descendant with leaf condition has "<=" operator'            | '//cps-path[@key<=12]'                                         || "//cps-path[@key<='12']"
82             'descendant with leaf value and ancestor'                     | '//cps-path[@key=1]/ancestor:parent[@key=1]'                   || "//cps-path[@key='1']/ancestor:parent[@key='1']"
83             'parent & child'                                              | '/parent/child'                                                || '/parent/child'
84             'parent leaf of type Integer & child'                         | '/parent/child[@code=1]/child2'                                || "/parent/child[@code='1']/child2"
85             'parent leaf with double quotes'                              | '/parent/child[@code="1"]/child2'                              || "/parent/child[@code='1']/child2"
86             'parent leaf with double quotes inside single quotes'         | '/parent/child[@code=\'"1"\']/child2'                          || "/parent/child[@code='\"1\"']/child2"
87             'parent leaf with single quotes inside double quotes'         | '/parent/child[@code="\'1\'"]/child2'                          || "/parent/child[@code='''1''']/child2"
88             'leaf with single quotes inside double quotes'                | '/parent/child[@code="\'1\'"]'                                 || "/parent/child[@code='''1''']"
89             'leaf with more than one attribute'                           | '/parent/child[@key1=1 and @key2="abc"]'                       || "/parent/child[@key1='1' and @key2='abc']"
90             'parent & child with more than one attribute'                 | '/parent/child[@key1=1 and @key2="abc"]/child2'                || "/parent/child[@key1='1' and @key2='abc']/child2"
91             'leaf with more than one attribute has OR operator'           | '/parent/child[@key1=1 or @key2="abc"]'                        || "/parent/child[@key1='1' or @key2='abc']"
92             'parent & child with more than one attribute has OR operator' | '/parent/child[@key1=1 or @key2="abc"]/child2'                 || "/parent/child[@key1='1' or @key2='abc']/child2"
93             'parent & child with multiple AND  operators'                 | '/parent/child[@key1=1 and @key2="abc" and @key="xyz"]/child2' || "/parent/child[@key1='1' and @key2='abc' and @key='xyz']/child2"
94             'parent & child with multiple OR  operators'                  | '/parent/child[@key1=1 or @key2="abc" or @key="xyz"]/child2'   || "/parent/child[@key1='1' or @key2='abc' or @key='xyz']/child2"
95             'parent & child with multiple AND/OR combination'             | '/parent/child[@key1=1 and @key2="abc" or @key="xyz"]/child2'  || "/parent/child[@key1='1' and @key2='abc' or @key='xyz']/child2"
96             'parent & child with multiple OR/AND combination'             | '/parent/child[@key1=1 or @key2="abc" and @key="xyz"]/child2'  || "/parent/child[@key1='1' or @key2='abc' and @key='xyz']/child2"
97     }
98
99     def 'Parse xpath to form the Normalized xpath containing #scenario.'() {
100         when: 'the given xpath is parsed'
101             def result = CpsPathUtil.getNormalizedXpath(xPath)
102         then: 'the query has the right normalized xpath type'
103             assert result == expectedNormalizedXPath
104         where: 'the following data is used'
105             scenario               | xPath      || expectedNormalizedXPath
106             'yang container'       | '/xpath'   || '/xpath'
107             'descendant anywhere'  | '//xpath'  || '//xpath'
108     }
109
110     def 'Parse cps path that ends with a yang list containing multiple leaf conditions.'() {
111         when: 'the given cps path is parsed'
112             def result = CpsPathQuery.createFrom(cpsPath)
113         then: 'the expected number of leaves are returned'
114             result.leavesData.size() == expectedNumberOfLeaves
115         and: 'the given operator(s) returns in the correct order'
116             result.booleanOperators == expectedOperators
117         and: 'the given comparativeOperator(s) returns in the correct order'
118             result.comparativeOperators == expectedComparativeOperator
119         where: 'the following data is used'
120             cpsPath                                                                                   || expectedNumberOfLeaves || expectedOperators || expectedComparativeOperator
121             '/parent[@code=1]/child[@common-leaf-name-int=5]'                                         || 1                      || []                || ['=']
122             '//child[@int-leaf>15 and @leaf-name="leaf value"]'                                       || 2                      || ['and']           || ['>', '=']
123             '//child[@int-leaf<5 or @leaf-name="leaf value"]'                                         || 2                      || ['or']            || ['<', '=']
124             '//child[@int-leaf=5 and @common-leaf-name="leaf value" or @leaf-name="leaf value1" ]'    || 3                      || ['and', 'or']     || ['=', '=', '=']
125             '//child[@int-leaf=5 or @common-leaf-name="leaf value" and @leaf-name="leaf value1" ]'    || 3                      || ['or', 'and']     || ['=', '=', '=']
126             '//child[@int-leaf>=18 and @common-leaf-name="leaf value" and @leaf-name="leaf value1" ]' || 3                      || ['and', 'and']    || ['>=', '=', '=']
127             '//child[@int-leaf<=25 or @common-leaf-name="leaf value" or @leaf-name="leaf value1" ]'   || 3                      || ['or', 'or']      || ['<=', '=', '=']
128     }
129
130     def 'Parse #scenario cps path with text function condition'() {
131         when: 'the given cps path is parsed'
132             def result = CpsPathQuery.createFrom(cpsPath)
133         then: 'the query has the right xpath type'
134             result.cpsPathPrefixType == DESCENDANT
135         and: 'leaf conditions are only present when expected'
136             result.hasLeafConditions() == expectLeafConditions
137         and: 'the right text function condition is set'
138             result.hasTextFunctionCondition()
139             result.textFunctionConditionLeafName == 'leaf-name'
140             result.textFunctionConditionValue == 'search'
141         and: 'the ancestor is only present when expected'
142             assert result.hasAncestorAxis() == expectHasAncestorAxis
143         where: 'the following data is used'
144             scenario                                  | cpsPath                                                              || expectLeafConditions | expectHasAncestorAxis
145             'descendant anywhere'                     | '//someContainer/leaf-name[text()="search"]'                         || false                | false
146             'descendant with leaf value'              | '//child[@other-leaf=1]/leaf-name[text()="search"]'                  || true                 | false
147             'descendant anywhere and ancestor'        | '//someContainer/leaf-name[text()="search"]/ancestor::parent'        || false                | true
148             'descendant with leaf value and ancestor' | '//child[@other-leaf=1]/leaf-name[text()="search"]/ancestor::parent' || true                 | true
149     }
150
151     def 'Parse cps path with contains function condition'() {
152         when: 'the given cps path is parsed'
153             def result = CpsPathQuery.createFrom('//someContainer[contains(@lang,"en")]')
154         then: 'the query has the right xpath type'
155             result.cpsPathPrefixType == DESCENDANT
156         and: 'the right contains function condition is set'
157             result.hasContainsFunctionCondition()
158             result.containsFunctionConditionLeafName == 'lang'
159             result.containsFunctionConditionValue == 'en'
160     }
161
162     def 'Parse cps path with error: #scenario.'() {
163         when: 'the given cps path is parsed'
164             CpsPathQuery.createFrom(cpsPath)
165         then: 'a CpsPathException is thrown'
166             thrown(PathParsingException)
167         where: 'the following data is used'
168             scenario                                 | cpsPath
169             'no / at the start'                      | 'invalid-cps-path/child'
170             'additional / after descendant option'   | '///cps-path'
171             'float value'                            | '/parent/child[@someFloat=5.0]'
172             'unmatched quotes, double quote first '  | '/parent/child[@someString="value with unmatched quotes\']'
173             'unmatched quotes, single quote first'   | '/parent/child[@someString=\'value with unmatched quotes"]'
174             'missing attribute value'                | '//child[@int-leaf=5 and @name]'
175             'incomplete ancestor value'              | '//books/ancestor::'
176             'invalid list element with missing ['    | '/parent-206/child-206/grand-child-206@key="A"]'
177             'invalid list element with incorrect ]'  | '/parent-206/child-206/grand-child-206]@key="A"]'
178             'invalid list element with incorrect ::' | '/parent-206/child-206/grand-child-206::@key"A"]'
179     }
180
181     def 'Parse cps path using ancestor by schema node identifier with a #scenario.'() {
182         when: 'the given cps path is parsed'
183             def result = CpsPathQuery.createFrom('//descendant/ancestor::' + ancestorPath)
184         then: 'the query has the right type'
185             result.cpsPathPrefixType == DESCENDANT
186         and: 'the result has ancestor axis'
187             result.hasAncestorAxis()
188         and: 'the correct ancestor schema node identifier is set'
189             result.ancestorSchemaNodeIdentifier == ancestorPath
190         and: 'there are no leaves conditions'
191             result.hasLeafConditions() == false
192         where:
193             scenario                                    | ancestorPath
194             'basic container'                           | 'someContainer'
195             'container with parent'                     | 'parent/child'
196             'ancestor that is a list'                   | "categories[@code='1']"
197             'ancestor that is a list with compound key' | "categories[@key1='1' and @key2='2']"
198             'parent that is a list'                     | "parent[@id='1']/child"
199     }
200
201     def 'Combinations #scenario.'() {
202         when: 'the given cps path is parsed'
203             def result = CpsPathQuery.createFrom(cpsPath + '/ancestor::someAncestor')
204         then: 'the query has the right type'
205             result.cpsPathPrefixType == DESCENDANT
206         and: 'leaf conditions are only present when expected'
207             result.hasLeafConditions() == expectLeafConditions
208         and: 'the result has ancestor axis'
209             result.hasAncestorAxis()
210         and: 'the correct ancestor schema node identifier is set'
211             result.ancestorSchemaNodeIdentifier == 'someAncestor'
212             result.descendantName == expectedDescendantName
213         where:
214             scenario                     | cpsPath                               || expectedDescendantName   | expectLeafConditions
215             'basic container'            | '//someContainer'                     || 'someContainer'          | false
216             'container with parent'      | '//parent/child'                      || 'parent/child'           | false
217             'container with list-parent' | '//parent[@id=1]/child'               || "parent[@id='1']/child"  | false
218             'container with list-parent' | '//parent[@id=1]/child[@name="test"]' || "parent[@id='1']/child"  | true
219     }
220
221     def 'Parse cps path with multiple conditions on same leaf.'() {
222         when: 'the given cps path is parsed using multiple conditions on same leaf'
223             def result = CpsPathQuery.createFrom('/test[@same-name="value1" or @same-name="value2"]')
224         then: 'two leaves are present with correct values'
225             assert result.leavesData.size() == 2
226             assert result.leavesData[0].name == "same-name"
227             assert result.leavesData[0].value == "value1"
228             assert result.leavesData[1].name == "same-name"
229             assert result.leavesData[1].value == "value2"
230     }
231
232     def 'Ordering of data leaves is preserved.'() {
233         when: 'the given cps path is parsed'
234             def result = CpsPathQuery.createFrom(cpsPath)
235         then: 'the order of the data leaves is preserved'
236             assert result.leavesData[0].name == expectedFirstLeafName
237             assert result.leavesData[1].name == expectedSecondLeafName
238         where: 'the following data is used'
239             cpsPath                                      || expectedFirstLeafName | expectedSecondLeafName
240             '/test[@name1="value1" and @name2="value2"]' || 'name1'               | 'name2'
241             '/test[@name2="value2" and @name1="value1"]' || 'name2'               | 'name1'
242     }
243
244 }