Prepare for next CPS-NCMP release
[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  *  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             result.cpsPathPrefixType == ABSOLUTE
36         and: 'the right query parameters are set'
37             result.xpathPrefix == expectedXpathPrefix
38             result.hasLeafConditions()
39             result.leavesData.containsKey(expectedLeafName)
40             result.leavesData.get(expectedLeafName) == 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     }
50
51     def 'Parse cps path of type ends with a #scenario.'() {
52         when: 'the given cps path is parsed'
53             def result = CpsPathQuery.createFrom(cpsPath)
54         then: 'the query has the right xpath type'
55             result.cpsPathPrefixType == DESCENDANT
56         and: 'the right ends with parameters are set'
57             result.descendantName == expectedDescendantName
58         where: 'the following data is used'
59             scenario         | cpsPath          || expectedDescendantName
60             'yang container' | '//cps-path'     || 'cps-path'
61             'parent & child' | '//parent/child' || 'parent/child'
62     }
63
64     def 'Parse cps path to form the Normalized cps path containing #scenario.'() {
65         when: 'the given cps path is parsed'
66             def result = CpsPathUtil.getCpsPathQuery(cpsPath)
67         then: 'the query has the right normalized xpath type'
68             assert result.normalizedXpath == expectedNormalizedXPath
69         where: 'the following data is used'
70             scenario                                                      | cpsPath                                                        || expectedNormalizedXPath
71             'yang container'                                              | '/cps-path'                                                    || '/cps-path'
72             'descendant anywhere'                                         | '//cps-path'                                                   || '//cps-path'
73             'descendant with leaf condition'                              | '//cps-path[@key=1]'                                           || "//cps-path[@key='1']"
74             'descendant with leaf condition has ">" operator'             | '//cps-path[@key>9]'                                           || "//cps-path[@key>'9']"
75             'descendant with leaf condition has "<" operator'             | '//cps-path[@key<10]'                                          || "//cps-path[@key<'10']"
76             'descendant with leaf condition has ">=" operator'            | '//cps-path[@key>=8]'                                          || "//cps-path[@key>='8']"
77             'descendant with leaf condition has "<=" operator'            | '//cps-path[@key<=12]'                                         || "//cps-path[@key<='12']"
78             'descendant with leaf value and ancestor'                     | '//cps-path[@key=1]/ancestor:parent[@key=1]'                   || "//cps-path[@key='1']/ancestor:parent[@key='1']"
79             'parent & child'                                              | '/parent/child'                                                || '/parent/child'
80             'parent leaf of type Integer & child'                         | '/parent/child[@code=1]/child2'                                || "/parent/child[@code='1']/child2"
81             'parent leaf with double quotes'                              | '/parent/child[@code="1"]/child2'                              || "/parent/child[@code='1']/child2"
82             'parent leaf with double quotes inside single quotes'         | '/parent/child[@code=\'"1"\']/child2'                          || "/parent/child[@code='\"1\"']/child2"
83             'parent leaf with single quotes inside double quotes'         | '/parent/child[@code="\'1\'"]/child2'                          || "/parent/child[@code='\\\'1\\\'']/child2"
84             'leaf with single quotes inside double quotes'                | '/parent/child[@code="\'1\'"]'                                 || "/parent/child[@code='\\\'1\\\'']"
85             'leaf with more than one attribute'                           | '/parent/child[@key1=1 and @key2="abc"]'                       || "/parent/child[@key1='1' and @key2='abc']"
86             'parent & child with more than one attribute'                 | '/parent/child[@key1=1 and @key2="abc"]/child2'                || "/parent/child[@key1='1' and @key2='abc']/child2"
87             'leaf with more than one attribute has OR operator'           | '/parent/child[@key1=1 or @key2="abc"]'                        || "/parent/child[@key1='1' or @key2='abc']"
88             '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"
89             '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"
90             '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"
91             '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"
92             '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"
93     }
94
95     def 'Parse xpath to form the Normalized xpath containing #scenario.'() {
96         when: 'the given xpath is parsed'
97             def result = CpsPathUtil.getNormalizedXpath(xPath)
98         then: 'the query has the right normalized xpath type'
99             assert result == expectedNormalizedXPath
100         where: 'the following data is used'
101             scenario               | xPath      || expectedNormalizedXPath
102             'yang container'       | '/xpath'   || '/xpath'
103             'descendant anywhere'  | '//xpath'  || '//xpath'
104     }
105
106     def 'Parse cps path that ends with a yang list containing #scenario.'() {
107         when: 'the given cps path is parsed'
108             def result = CpsPathQuery.createFrom(cpsPath)
109         then: 'the query has the right xpath type'
110             result.cpsPathPrefixType == DESCENDANT
111         and: 'the right parameters are set'
112             result.descendantName == "child"
113             result.leavesData.size() == expectedNumberOfLeaves
114         and: 'the given operator(s) returns in the correct order'
115             result.booleanOperators == expectedOperators
116         and: 'the given comparativeOperator(s) returns in the correct order'
117             result.comparativeOperators == expectedComparativeOperator
118         where: 'the following data is used'
119             scenario                                                      | cpsPath                                                                                   || expectedNumberOfLeaves || expectedOperators || expectedComparativeOperator
120             'one attribute'                                               | '//child[@common-leaf-name-int=5]'                                                        || 1                      || []                || ['=']
121             'more than one attribute has AND operator'                    | '//child[@int-leaf=5 and @leaf-name="leaf value"]'                                        || 2                      || ['and']           || ['=', '=']
122             'more than one attribute has OR operator'                     | '//child[@int-leaf=5 or @leaf-name="leaf value"]'                                         || 2                      || ['or']            || ['=', '=']
123             'more than one attribute has combinations AND operators'      | '//child[@int-leaf=5 and @common-leaf-name="leaf value" and @leaf-name="leaf value1" ]'   || 3                      || ['and', 'and']    || ['=', '=', '=']
124             'more than one attribute has combinations OR operators'       | '//child[@int-leaf=5 or @common-leaf-name="leaf value" or @leaf-name="leaf value1" ]'     || 3                      || ['or', 'or']      || ['=', '=', '=']
125             'more than one attribute has combinations AND/OR combination' | '//child[@int-leaf=5 and @common-leaf-name="leaf value" or @leaf-name="leaf value1" ]'    || 3                      || ['and', 'or']     || ['=', '=', '=']
126             'more than one attribute has combinations OR/AND combination' | '//child[@int-leaf=5 or @common-leaf-name="leaf value" and @leaf-name="leaf value1" ]'    || 3                      || ['or', 'and']     || ['=', '=', '=']
127             'more than one attribute has AND/> operators'                 | '//child[@int-leaf>15 and @leaf-name="leaf value"]'                                       || 2                      || ['and']           || ['>', '=']
128             'more than one attribute has OR/< operators'                  | '//child[@int-leaf<5 or @leaf-name="leaf value"]'                                         || 2                      || ['or']            || ['<', '=']
129             'more than one attribute has combinations AND/>= operators'   | '//child[@int-leaf>=18 and @common-leaf-name="leaf value" and @leaf-name="leaf value1" ]' || 3                      || ['and', 'and']    || ['>=', '=', '=']
130             'more than one attribute has combinations OR/<= operators'    | '//child[@int-leaf<=25 or @common-leaf-name="leaf value" or @leaf-name="leaf value1" ]'   || 3                      || ['or', 'or']      || ['<=', '=', '=']
131     }
132
133     def 'Parse #scenario cps path with text function condition'() {
134         when: 'the given cps path is parsed'
135             def result = CpsPathQuery.createFrom(cpsPath)
136         then: 'the query has the right xpath type'
137             result.cpsPathPrefixType == DESCENDANT
138         and: 'leaf conditions are only present when expected'
139             result.hasLeafConditions() == expectLeafConditions
140         and: 'the right text function condition is set'
141             result.hasTextFunctionCondition()
142             result.textFunctionConditionLeafName == 'leaf-name'
143             result.textFunctionConditionValue == 'search'
144         and: 'the ancestor is only present when expected'
145             assert result.hasAncestorAxis() == expectHasAncestorAxis
146         where: 'the following data is used'
147             scenario                                  | cpsPath                                                              || expectLeafConditions | expectHasAncestorAxis
148             'descendant anywhere'                     | '//someContainer/leaf-name[text()="search"]'                         || false                | false
149             'descendant with leaf value'              | '//child[@other-leaf=1]/leaf-name[text()="search"]'                  || true                 | false
150             'descendant anywhere and ancestor'        | '//someContainer/leaf-name[text()="search"]/ancestor::parent'        || false                | true
151             'descendant with leaf value and ancestor' | '//child[@other-leaf=1]/leaf-name[text()="search"]/ancestor::parent' || true                 | true
152     }
153
154     def 'Parse cps path with contains function condition'() {
155         when: 'the given cps path is parsed'
156             def result = CpsPathQuery.createFrom('//someContainer[contains(@lang,"en")]')
157         then: 'the query has the right xpath type'
158             result.cpsPathPrefixType == DESCENDANT
159         and: 'the right contains function condition is set'
160             result.hasContainsFunctionCondition()
161             result.containsFunctionConditionLeafName == 'lang'
162             result.containsFunctionConditionValue == 'en'
163     }
164
165     def 'Parse cps path with error: #scenario.'() {
166         when: 'the given cps path is parsed'
167             CpsPathQuery.createFrom(cpsPath)
168         then: 'a CpsPathException is thrown'
169             thrown(PathParsingException)
170         where: 'the following data is used'
171             scenario                                 | cpsPath
172             'no / at the start'                      | 'invalid-cps-path/child'
173             'additional / after descendant option'   | '///cps-path'
174             'float value'                            | '/parent/child[@someFloat=5.0]'
175             'unmatched quotes, double quote first '  | '/parent/child[@someString="value with unmatched quotes\']'
176             'unmatched quotes, single quote first'   | '/parent/child[@someString=\'value with unmatched quotes"]'
177             'missing attribute value'                | '//child[@int-leaf=5 and @name]'
178             'incomplete ancestor value'              | '//books/ancestor::'
179             'invalid list element with missing ['    | '/parent-206/child-206/grand-child-206@key="A"]'
180             'invalid list element with incorrect ]'  | '/parent-206/child-206/grand-child-206]@key="A"]'
181             'invalid list element with incorrect ::' | '/parent-206/child-206/grand-child-206::@key"A"]'
182     }
183
184     def 'Parse cps path using ancestor by schema node identifier with a #scenario.'() {
185         when: 'the given cps path is parsed'
186             def result = CpsPathQuery.createFrom('//descendant/ancestor::' + ancestorPath)
187         then: 'the query has the right type'
188             result.cpsPathPrefixType == DESCENDANT
189         and: 'the result has ancestor axis'
190             result.hasAncestorAxis()
191         and: 'the correct ancestor schema node identifier is set'
192             result.ancestorSchemaNodeIdentifier == ancestorPath
193         and: 'there are no leaves conditions'
194             result.hasLeafConditions() == false
195         where:
196             scenario                                    | ancestorPath
197             'basic container'                           | 'someContainer'
198             'container with parent'                     | 'parent/child'
199             'ancestor that is a list'                   | "categories[@code='1']"
200             'ancestor that is a list with compound key' | "categories[@key1='1' and @key2='2']"
201             'parent that is a list'                     | "parent[@id='1']/child"
202     }
203
204     def 'Combinations #scenario.'() {
205         when: 'the given cps path is parsed'
206             def result = CpsPathQuery.createFrom(cpsPath + '/ancestor::someAncestor')
207         then: 'the query has the right type'
208             result.cpsPathPrefixType == DESCENDANT
209         and: 'leaf conditions are only present when expected'
210             result.hasLeafConditions() == expectLeafConditions
211         and: 'the result has ancestor axis'
212             result.hasAncestorAxis()
213         and: 'the correct ancestor schema node identifier is set'
214             result.ancestorSchemaNodeIdentifier == 'someAncestor'
215             result.descendantName == expectedDescendantName
216         where:
217             scenario                     | cpsPath                               || expectedDescendantName   | expectLeafConditions
218             'basic container'            | '//someContainer'                     || 'someContainer'          | false
219             'container with parent'      | '//parent/child'                      || 'parent/child'           | false
220             'container with list-parent' | '//parent[@id=1]/child'               || "parent[@id='1']/child"  | false
221             'container with list-parent' | '//parent[@id=1]/child[@name="test"]' || "parent[@id='1']/child"  | true
222     }
223 }