Merge "Cm Subscription: Predicates optional now"
[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 'Default values for the most basic cps query.'() {
32         when: 'the cps path is parsed'
33             def result = CpsPathQuery.createFrom('/parent')
34         then: 'the query has the correct default properties'
35             assert result.cpsPathPrefixType == ABSOLUTE
36             assert result.hasAncestorAxis() == false
37             assert result.hasLeafConditions() == false
38             assert result.hasTextFunctionCondition() == false
39             assert result.hasContainsFunctionCondition() == false
40             assert result.isPathToListElement() == false
41     }
42
43     def 'Parse cps path with valid cps path and a filter with #scenario.'() {
44         when: 'the given cps path is parsed'
45             def result = CpsPathQuery.createFrom(cpsPath)
46         then: 'the query has the right xpath type'
47             assert result.cpsPathPrefixType == ABSOLUTE
48         and: 'the right query parameters are set'
49             assert result.xpathPrefix == expectedXpathPrefix
50             assert result.hasLeafConditions()
51             assert result.leavesData[0].name == expectedLeafName
52             assert result.leavesData[0].value == expectedLeafValue
53         where: 'the following data is used'
54             scenario               | cpsPath                                                    || expectedXpathPrefix                             | expectedLeafName       | expectedLeafValue
55             'leaf of type String'  | '/parent/child[@common-leaf-name="common-leaf-value"]'     || '/parent/child'                                 | 'common-leaf-name'     | 'common-leaf-value'
56             'leaf of type String'  | '/parent/child[@common-leaf-name=\'common-leaf-value\']'   || '/parent/child'                                 | 'common-leaf-name'     | 'common-leaf-value'
57             'leaf of type Integer' | '/parent/child[@common-leaf-name-int=5]'                   || '/parent/child'                                 | 'common-leaf-name-int' | 5
58             'spaces around ='      | '/parent/child[@common-leaf-name-int = 5]'                 || '/parent/child'                                 | 'common-leaf-name-int' | 5
59             'key in top container' | '/parent[@common-leaf-name-int=5]'                         || '/parent'                                       | 'common-leaf-name-int' | 5
60             'parent list'          | '/shops/shop[@id=1]/categories[@id=1]/book[@title="Dune"]' || "/shops/shop[@id='1']/categories[@id='1']/book" | 'title'                | 'Dune'
61             "' in double quote"    | '/parent[@common-leaf-name="leaf\'value"]'                 || '/parent'                                       | 'common-leaf-name'     | "leaf'value"
62             "' in single quote"    | "/parent[@common-leaf-name='leaf''value']"                 || '/parent'                                       | 'common-leaf-name'     | "leaf'value"
63             '" in double quote'    | '/parent[@common-leaf-name="leaf""value"]'                 || '/parent'                                       | 'common-leaf-name'     | 'leaf"value'
64             '" in single quote'    | '/parent[@common-leaf-name=\'leaf"value\']'                || '/parent'                                       | 'common-leaf-name'     | 'leaf"value'
65     }
66
67     def 'Parse cps path of type ends with a #scenario.'() {
68         when: 'the given cps path is parsed'
69             def result = CpsPathQuery.createFrom(cpsPath)
70         then: 'the query has the right xpath type'
71             result.cpsPathPrefixType == DESCENDANT
72         and: 'the right ends with parameters are set'
73             result.descendantName == expectedDescendantName
74         where: 'the following data is used'
75             scenario         | cpsPath          || expectedDescendantName
76             'yang container' | '//cps-path'     || 'cps-path'
77             'parent & child' | '//parent/child' || 'parent/child'
78     }
79
80     def 'Parse cps path to form the Normalized cps path containing #scenario.'() {
81         when: 'the given cps path is parsed'
82             def result = CpsPathUtil.getCpsPathQuery(cpsPath)
83         then: 'the query has the right normalized xpath type'
84             assert result.normalizedXpath == expectedNormalizedXPath
85         where: 'the following data is used'
86             scenario                                                      | cpsPath                                                        || expectedNormalizedXPath
87             'yang container'                                              | '/cps-path'                                                    || '/cps-path'
88             'descendant anywhere'                                         | '//cps-path'                                                   || '//cps-path'
89             'descendant with leaf condition'                              | '//cps-path[@key=1]'                                           || "//cps-path[@key='1']"
90             'descendant with leaf condition has ">" operator'             | '//cps-path[@key>9]'                                           || "//cps-path[@key>'9']"
91             'descendant with leaf condition has "<" operator'             | '//cps-path[@key<10]'                                          || "//cps-path[@key<'10']"
92             'descendant with leaf condition has ">=" operator'            | '//cps-path[@key>=8]'                                          || "//cps-path[@key>='8']"
93             'descendant with leaf condition has "<=" operator'            | '//cps-path[@key<=12]'                                         || "//cps-path[@key<='12']"
94             'descendant with leaf value and ancestor'                     | '//cps-path[@key=1]/ancestor:parent[@key=1]'                   || "//cps-path[@key='1']/ancestor:parent[@key='1']"
95             'parent & child'                                              | '/parent/child'                                                || '/parent/child'
96             'parent leaf of type Integer & child'                         | '/parent/child[@code=1]/child2'                                || "/parent/child[@code='1']/child2"
97             'parent leaf with double quotes'                              | '/parent/child[@code="1"]/child2'                              || "/parent/child[@code='1']/child2"
98             'parent leaf with double quotes inside single quotes'         | '/parent/child[@code=\'"1"\']/child2'                          || "/parent/child[@code='\"1\"']/child2"
99             'parent leaf with single quotes inside double quotes'         | '/parent/child[@code="\'1\'"]/child2'                          || "/parent/child[@code='''1''']/child2"
100             'leaf with single quotes inside double quotes'                | '/parent/child[@code="\'1\'"]'                                 || "/parent/child[@code='''1''']"
101             'leaf with more than one attribute'                           | '/parent/child[@key1=1 and @key2="abc"]'                       || "/parent/child[@key1='1' and @key2='abc']"
102             'parent & child with more than one attribute'                 | '/parent/child[@key1=1 and @key2="abc"]/child2'                || "/parent/child[@key1='1' and @key2='abc']/child2"
103             'leaf with more than one attribute has OR operator'           | '/parent/child[@key1=1 or @key2="abc"]'                        || "/parent/child[@key1='1' or @key2='abc']"
104             '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"
105             '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"
106             '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"
107             '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"
108             '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"
109     }
110
111     def 'Parse xpath to form the Normalized xpath containing #scenario.'() {
112         when: 'the given xpath is parsed'
113             def result = CpsPathUtil.getNormalizedXpath(xPath)
114         then: 'the query has the right normalized xpath type'
115             assert result == expectedNormalizedXPath
116         where: 'the following data is used'
117             scenario               | xPath      || expectedNormalizedXPath
118             'yang container'       | '/xpath'   || '/xpath'
119             'descendant anywhere'  | '//xpath'  || '//xpath'
120     }
121
122     def 'Parse cps path that ends with a yang list containing multiple leaf conditions.'() {
123         when: 'the given cps path is parsed'
124             def result = CpsPathQuery.createFrom(cpsPath)
125         then: 'the expected number of leaves are returned'
126             result.leavesData.size() == expectedNumberOfLeaves
127         and: 'the given operator(s) returns in the correct order'
128             result.booleanOperators == expectedOperators
129         and: 'the given comparativeOperator(s) returns in the correct order'
130             result.comparativeOperators == expectedComparativeOperator
131         where: 'the following data is used'
132             cpsPath                                                                                   || expectedNumberOfLeaves || expectedOperators || expectedComparativeOperator
133             '/parent[@code=1]/child[@common-leaf-name-int=5]'                                         || 1                      || []                || ['=']
134             '//child[@int-leaf>15 and @leaf-name="leaf value"]'                                       || 2                      || ['and']           || ['>', '=']
135             '//child[@int-leaf<5 or @leaf-name="leaf value"]'                                         || 2                      || ['or']            || ['<', '=']
136             '//child[@int-leaf=5 and @common-leaf-name="leaf value" or @leaf-name="leaf value1" ]'    || 3                      || ['and', 'or']     || ['=', '=', '=']
137             '//child[@int-leaf=5 or @common-leaf-name="leaf value" and @leaf-name="leaf value1" ]'    || 3                      || ['or', 'and']     || ['=', '=', '=']
138             '//child[@int-leaf>=18 and @common-leaf-name="leaf value" and @leaf-name="leaf value1" ]' || 3                      || ['and', 'and']    || ['>=', '=', '=']
139             '//child[@int-leaf<=25 or @common-leaf-name="leaf value" or @leaf-name="leaf value1" ]'   || 3                      || ['or', 'or']      || ['<=', '=', '=']
140     }
141
142     def 'Parse #scenario cps path with text function condition'() {
143         when: 'the given cps path is parsed'
144             def result = CpsPathQuery.createFrom(cpsPath)
145         then: 'the query has the right xpath type'
146             assert result.cpsPathPrefixType == DESCENDANT
147         and: 'leaf conditions are only present when expected'
148             assert result.hasLeafConditions() == expectLeafConditions
149         and: 'the right text function condition is set'
150             assert result.hasTextFunctionCondition()
151             assert result.textFunctionConditionLeafName == 'leaf-name'
152             assert result.textFunctionConditionValue == 'search'
153         and: 'the ancestor is only present when expected'
154             assert result.hasAncestorAxis() == expectHasAncestorAxis
155         where: 'the following data is used'
156             scenario                                  | cpsPath                                                              || expectLeafConditions | expectHasAncestorAxis
157             'descendant anywhere'                     | '//someContainer/leaf-name[text()="search"]'                         || false                | false
158             'descendant with leaf value'              | '//child[@other-leaf=1]/leaf-name[text()="search"]'                  || true                 | false
159             'descendant anywhere and ancestor'        | '//someContainer/leaf-name[text()="search"]/ancestor::parent'        || false                | true
160             'descendant with leaf value and ancestor' | '//child[@other-leaf=1]/leaf-name[text()="search"]/ancestor::parent' || true                 | true
161     }
162
163     def 'Parse cps path with contains function condition'() {
164         when: 'the given cps path is parsed'
165             def result = CpsPathQuery.createFrom('//someContainer[contains(@lang,"en")]')
166         then: 'the query has the right xpath type'
167             assert result.cpsPathPrefixType == DESCENDANT
168         and: 'the right contains function condition is set'
169             assert result.hasContainsFunctionCondition()
170             assert result.containsFunctionConditionLeafName == 'lang'
171             assert result.containsFunctionConditionValue == 'en'
172     }
173
174     def 'Parse cps path with error: #scenario.'() {
175         when: 'the given cps path is parsed'
176             CpsPathQuery.createFrom(cpsPath)
177         then: 'a CpsPathException is thrown'
178             thrown(PathParsingException)
179         where: 'the following data is used'
180             scenario                                 | cpsPath
181             'no / at the start'                      | 'invalid-cps-path/child'
182             'additional / after descendant option'   | '///cps-path'
183             'float value'                            | '/parent/child[@someFloat=5.0]'
184             'unmatched quotes, double quote first '  | '/parent/child[@someString="value with unmatched quotes\']'
185             'unmatched quotes, single quote first'   | '/parent/child[@someString=\'value with unmatched quotes"]'
186             'missing attribute value'                | '//child[@int-leaf=5 and @name]'
187             'incomplete ancestor value'              | '//books/ancestor::'
188             'invalid list element with missing ['    | '/parent-206/child-206/grand-child-206@key="A"]'
189             'invalid list element with incorrect ]'  | '/parent-206/child-206/grand-child-206]@key="A"]'
190             'invalid list element with incorrect ::' | '/parent-206/child-206/grand-child-206::@key"A"]'
191     }
192
193     def 'Parse cps path using ancestor by schema node identifier with a #scenario.'() {
194         when: 'the given cps path is parsed'
195             def result = CpsPathQuery.createFrom('//descendant/ancestor::' + ancestorPath)
196         then: 'the query has the right type'
197             result.cpsPathPrefixType == DESCENDANT
198         and: 'the result has ancestor axis'
199             result.hasAncestorAxis()
200         and: 'the correct ancestor schema node identifier is set'
201             result.ancestorSchemaNodeIdentifier == ancestorPath
202         and: 'there are no leaves conditions'
203             assert result.hasLeafConditions() == false
204         where:
205             scenario                                    | ancestorPath
206             'basic container'                           | 'someContainer'
207             'container with parent'                     | 'parent/child'
208             'ancestor that is a list'                   | "categories[@code='1']"
209             'ancestor that is a list with compound key' | "categories[@key1='1' and @key2='2']"
210             'parent that is a list'                     | "parent[@id='1']/child"
211     }
212
213     def 'Combinations #scenario.'() {
214         when: 'the given cps path is parsed'
215             def result = CpsPathQuery.createFrom(cpsPath + '/ancestor::someAncestor')
216         then: 'the query has the right type'
217             assert result.cpsPathPrefixType == DESCENDANT
218         and: 'leaf conditions are only present when expected'
219             assert result.hasLeafConditions() == expectLeafConditions
220         and: 'the result has ancestor axis'
221             assert result.hasAncestorAxis()
222         and: 'the correct ancestor schema node identifier is set'
223             assert result.ancestorSchemaNodeIdentifier == 'someAncestor'
224             assert result.descendantName == expectedDescendantName
225         where:
226             scenario                     | cpsPath                               || expectedDescendantName   | expectLeafConditions
227             'basic container'            | '//someContainer'                     || 'someContainer'          | false
228             'container with parent'      | '//parent/child'                      || 'parent/child'           | false
229             'container with list-parent' | '//parent[@id=1]/child'               || "parent[@id='1']/child"  | false
230             'container with list-parent' | '//parent[@id=1]/child[@name="test"]' || "parent[@id='1']/child"  | true
231     }
232
233     def 'Parse cps path with multiple conditions on same leaf.'() {
234         when: 'the given cps path is parsed using multiple conditions on same leaf'
235             def result = CpsPathQuery.createFrom('/test[@same-name="value1" or @same-name="value2"]')
236         then: 'two leaves are present with correct values'
237             assert result.leavesData.size() == 2
238             assert result.leavesData[0].name == "same-name"
239             assert result.leavesData[0].value == "value1"
240             assert result.leavesData[1].name == "same-name"
241             assert result.leavesData[1].value == "value2"
242     }
243
244     def 'Ordering of data leaves is preserved.'() {
245         when: 'the given cps path is parsed'
246             def result = CpsPathQuery.createFrom(cpsPath)
247         then: 'the order of the data leaves is preserved'
248             assert result.leavesData[0].name == expectedFirstLeafName
249             assert result.leavesData[1].name == expectedSecondLeafName
250         where: 'the following data is used'
251             cpsPath                                      || expectedFirstLeafName | expectedSecondLeafName
252             '/test[@name1="value1" and @name2="value2"]' || 'name1'               | 'name2'
253             '/test[@name2="value2" and @name1="value1"]' || 'name2'               | 'name1'
254     }
255
256 }