Editing of Nordix Licenses to ONAP guidelines
[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 class CpsPathQuerySpec extends Specification {
26
27     def 'Parse cps path with valid cps path and a filter with #scenario.'() {
28         when: 'the given cps path is parsed'
29             def result = CpsPathQuery.createFrom(cpsPath)
30         then: 'the query has the right type'
31             result.cpsPathQueryType == CpsPathQueryType.XPATH_LEAF_VALUE
32         and: 'the right query parameters are set'
33             result.xpathPrefix == expectedXpathPrefix
34             result.leafName == expectedLeafName
35             result.leafValue == expectedLeafValue
36         where: 'the following data is used'
37             scenario               | cpsPath                                                    || expectedXpathPrefix                         | expectedLeafName       | expectedLeafValue
38             'leaf of type String'  | '/parent/child[@common-leaf-name="common-leaf-value"]'     || '/parent/child'                             | 'common-leaf-name'     | 'common-leaf-value'
39             'leaf of type String'  | '/parent/child[@common-leaf-name=\'common-leaf-value\']'   || '/parent/child'                             | 'common-leaf-name'     | 'common-leaf-value'
40             'leaf of type Integer' | '/parent/child[@common-leaf-name-int=5]'                   || '/parent/child'                             | 'common-leaf-name-int' | 5
41             'spaces around ='      | '/parent/child[@common-leaf-name-int = 5]'                 || '/parent/child'                             | 'common-leaf-name-int' | 5
42             'key in top container' | '/parent[@common-leaf-name-int=5]'                         || '/parent'                                   | 'common-leaf-name-int' | 5
43             'parent list'          | '/shops/shop[@id=1]/categories[@id=1]/book[@title="Dune"]' || '/shops/shop[@id=1]/categories[@id=1]/book' | 'title'                | 'Dune'
44     }
45
46     def 'Parse cps path of type ends with a #scenario.'() {
47         when: 'the given cps path is parsed'
48             def result = CpsPathQuery.createFrom(cpsPath)
49         then: 'the query has the right type'
50             result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
51         and: 'the right ends with parameters are set'
52             result.descendantName == expectedDescendantName
53         where: 'the following data is used'
54             scenario         | cpsPath          || expectedDescendantName
55             'yang container' | '//cps-path'     || 'cps-path'
56             'parent & child' | '//parent/child' || 'parent/child'
57     }
58
59     def 'Parse cps path that ends with a yang list containing #scenario.'() {
60         when: 'the given cps path is parsed'
61             def result = CpsPathQuery.createFrom(cpsPath)
62         then: 'the query has the right type'
63             result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES
64         and: 'the right ends with parameters are set'
65             result.descendantName == "child"
66             result.leavesData.size() == expectedNumberOfLeaves
67         where: 'the following data is used'
68             scenario                  | cpsPath                                            || expectedNumberOfLeaves
69             'one attribute'           | '//child[@common-leaf-name-int=5]'                 || 1
70             'more than one attribute' | '//child[@int-leaf=5 and @leaf-name="leaf value"]' || 2
71     }
72
73     def 'Parse cps path with error: #scenario.'() {
74         when: 'the given cps path is parsed'
75             CpsPathQuery.createFrom(cpsPath)
76         then: 'a CpsPathException is thrown'
77             thrown(IllegalStateException)
78         where: 'the following data is used'
79             scenario                                                            | cpsPath
80             'no / at the start'                                                 | 'invalid-cps-path/child'
81             'additional / after descendant option'                              | '///cps-path'
82             'float value'                                                       | '/parent/child[@someFloat=5.0]'
83             'unmatched quotes, double quote first '                             | '/parent/child[@someString="value with unmatched quotes\']'
84             'unmatched quotes, single quote first'                              | '/parent/child[@someString=\'value with unmatched quotes"]'
85             'end with descendant and more than one attribute separated by "or"' | '//child[@int-leaf=5 or @leaf-name="leaf value"]'
86             'missing attribute value'                                           | '//child[@int-leaf=5 and @name]'
87             'incomplete ancestor value'                                         | '//books/ancestor::'
88             'unsupported postfix after single value condition (JIRA CPS-450)'   | '/parent/child[@id=1]/somePostFix'
89     }
90
91     def 'Parse cps path using ancestor by schema node identifier with a #scenario.'() {
92         when: 'the given cps path is parsed'
93             def result = CpsPathQuery.createFrom('//descendant/ancestor::' + ancestorPath)
94         then: 'the query has the right type'
95             result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
96         and: 'the result has ancestor axis'
97             result.hasAncestorAxis()
98         and: 'the correct ancestor schema node identifier is set'
99             result.ancestorSchemaNodeIdentifier == ancestorPath
100         where:
101             scenario                  | ancestorPath
102             'basic container'         | 'someContainer'
103             'container with parent'   | 'parent/child'
104             'ancestor that is a list' | 'categories[@code=1]'
105             'parent that is a list'   | 'parent[@id=1]/child'
106     }
107
108     def 'Combinations #scenario.'() {
109         when: 'the given cps path is parsed'
110             def result = CpsPathQuery.createFrom(cpsPath + '/ancestor::someAncestor')
111         then: 'the query has the right type'
112             result.cpsPathQueryType == expectedCpsPathQueryType
113         and: 'the result has ancestor axis'
114             result.hasAncestorAxis()
115         and: 'the correct ancestor schema node identifier is set'
116             result.ancestorSchemaNodeIdentifier == 'someAncestor'
117             result.descendantName == expectedDescendantName
118         where:
119             scenario                     | cpsPath                               || expectedDescendantName | expectedCpsPathQueryType
120             'basic container'            | '//someContainer'                     || 'someContainer'        | CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
121             'container with parent'      | '//parent/child'                      || 'parent/child'         | CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
122             'container with list-parent' | '//parent[@id=1]/child'               || 'parent[@id=1]/child'  | CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
123             'container with list-parent' | '//parent[@id=1]/child[@name="test"]' || 'parent[@id=1]/child'  | CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES
124     }
125
126 }