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