bfec574ebae3c6b0b4439e4d70c760e77691be33
[cps.git] / cps-path-parser / src / test / groovy / org / onap / cps / cpspath / parser / CpsPathQuerySpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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() == true
38             result.leavesData.containsKey(expectedLeafName) == true
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 that ends with a yang list containing #scenario.'() {
64         when: 'the given cps path is parsed'
65             def result = CpsPathQuery.createFrom(cpsPath)
66         then: 'the query has the right xpath type'
67             result.cpsPathPrefixType == DESCENDANT
68         and: 'the right parameters are set'
69             result.descendantName == "child"
70             result.leavesData.size() == expectedNumberOfLeaves
71         where: 'the following data is used'
72             scenario                  | cpsPath                                            || expectedNumberOfLeaves
73             'one attribute'           | '//child[@common-leaf-name-int=5]'                 || 1
74             'more than one attribute' | '//child[@int-leaf=5 and @leaf-name="leaf value"]' || 2
75     }
76
77     def 'Parse #scenario cps path with text function condition'() {
78         when: 'the given cps path is parsed'
79             def result = CpsPathQuery.createFrom(cpsPath)
80         then: 'the query has the right xpath type'
81             result.cpsPathPrefixType == DESCENDANT
82         and: 'leaf conditions are only present when expected'
83             result.hasLeafConditions() == expectLeafConditions
84         and: 'the right text function condition is set'
85             result.hasTextFunctionCondition()
86             result.textFunctionConditionLeafName == 'leaf-name'
87             result.textFunctionConditionValue == 'search'
88         and: 'the ancestor is only present when expected'
89             assert result.hasAncestorAxis() == expectHasAncestorAxis
90         where: 'the following data is used'
91             scenario                                  | cpsPath                                                              || expectLeafConditions | expectHasAncestorAxis
92             'descendant anywhere'                     | '//someContainer/leaf-name[text()="search"]'                         || false                | false
93             'descendant with leaf value'              | '//child[@other-leaf=1]/leaf-name[text()="search"]'                  || true                 | false
94             'descendant anywhere and ancestor'        | '//someContainer/leaf-name[text()="search"]/ancestor::parent'        || false                | true
95             'descendant with leaf value and ancestor' | '//child[@other-leaf=1]/leaf-name[text()="search"]/ancestor::parent' || true                 | true
96     }
97
98     def 'Parse cps path with error: #scenario.'() {
99         when: 'the given cps path is parsed'
100             CpsPathQuery.createFrom(cpsPath)
101         then: 'a CpsPathException is thrown'
102             thrown(IllegalStateException)
103         where: 'the following data is used'
104             scenario                                                            | cpsPath
105             'no / at the start'                                                 | 'invalid-cps-path/child'
106             'additional / after descendant option'                              | '///cps-path'
107             'float value'                                                       | '/parent/child[@someFloat=5.0]'
108             'unmatched quotes, double quote first '                             | '/parent/child[@someString="value with unmatched quotes\']'
109             'unmatched quotes, single quote first'                              | '/parent/child[@someString=\'value with unmatched quotes"]'
110             'end with descendant and more than one attribute separated by "or"' | '//child[@int-leaf=5 or @leaf-name="leaf value"]'
111             'missing attribute value'                                           | '//child[@int-leaf=5 and @name]'
112             'incomplete ancestor value'                                         | '//books/ancestor::'
113 //  DISCUSS WITH TEAM :           'unsupported postfix after value condition (JIRA CPS-450)'          | '/parent/child[@id=1]/somePostFix'
114     }
115
116     def 'Parse cps path using ancestor by schema node identifier with a #scenario.'() {
117         when: 'the given cps path is parsed'
118             def result = CpsPathQuery.createFrom('//descendant/ancestor::' + ancestorPath)
119         then: 'the query has the right type'
120             result.cpsPathPrefixType == DESCENDANT
121         and: 'the result has ancestor axis'
122             result.hasAncestorAxis()
123         and: 'the correct ancestor schema node identifier is set'
124             result.ancestorSchemaNodeIdentifier == ancestorPath
125         and: 'there are no leaves conditions'
126             result.hasLeafConditions() == false
127         where:
128             scenario                  | ancestorPath
129             'basic container'         | 'someContainer'
130             'container with parent'   | 'parent/child'
131             'ancestor that is a list' | 'categories[@code=1]'
132             'parent that is a list'   | 'parent[@id=1]/child'
133     }
134
135     def 'Combinations #scenario.'() {
136         when: 'the given cps path is parsed'
137             def result = CpsPathQuery.createFrom(cpsPath + '/ancestor::someAncestor')
138         then: 'the query has the right type'
139             result.cpsPathPrefixType == DESCENDANT
140         and: 'leaf conditions are only present when expected'
141             result.hasLeafConditions() == expectLeafConditions
142         and: 'the result has ancestor axis'
143             result.hasAncestorAxis()
144         and: 'the correct ancestor schema node identifier is set'
145             result.ancestorSchemaNodeIdentifier == 'someAncestor'
146             result.descendantName == expectedDescendantName
147         where:
148             scenario                     | cpsPath                               || expectedDescendantName | expectLeafConditions
149             'basic container'            | '//someContainer'                     || 'someContainer'        | false
150             'container with parent'      | '//parent/child'                      || 'parent/child'         | false
151             'container with list-parent' | '//parent[@id=1]/child'               || 'parent[@id=1]/child'  | false
152             'container with list-parent' | '//parent[@id=1]/child[@name="test"]' || 'parent[@id=1]/child'  | true
153     }
154
155 }