Introducing Antlr4 for cpsPath parsing
[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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.cpspath.parser
21
22 import spock.lang.Specification
23
24 class CpsPathQuerySpec extends Specification {
25
26     def 'Parse cps path with valid cps path and a filter with #scenario.'() {
27         when: 'the given cps path is parsed'
28             def result = CpsPathQuery.createFrom(cpsPath)
29         then: 'the query has the right type'
30             result.cpsPathQueryType == CpsPathQueryType.XPATH_LEAF_VALUE
31         and: 'the right query parameters are set'
32             result.xpathPrefix == expectedXpathPrefix
33             result.leafName == expectedLeafName
34             result.leafValue == expectedLeafValue
35         where: 'the following data is used'
36             scenario               | cpsPath                                                    || expectedXpathPrefix                         | expectedLeafName       | expectedLeafValue
37             'leaf of type String'  | '/parent/child[@common-leaf-name="common-leaf-value"]'     || '/parent/child'                             | 'common-leaf-name'     | 'common-leaf-value'
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 Integer' | '/parent/child[@common-leaf-name-int=5]'                   || '/parent/child'                             | 'common-leaf-name-int' | 5
40             'spaces around ='      | '/parent/child[@common-leaf-name-int = 5]'                 || '/parent/child'                             | 'common-leaf-name-int' | 5
41             'key in top container' | '/parent[@common-leaf-name-int=5]'                         || '/parent'                                   | 'common-leaf-name-int' | 5
42             'parent list'          | '/shops/shop[@id=1]/categories[@id=1]/book[@title="Dune"]' || '/shops/shop[@id=1]/categories[@id=1]/book' | 'title'                | 'Dune'
43     }
44
45     def 'Parse cps path of type ends with a #scenario.'() {
46         when: 'the given cps path is parsed'
47             def result = CpsPathQuery.createFrom(cpsPath)
48         then: 'the query has the right type'
49             result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
50         and: 'the right ends with parameters are set'
51             result.descendantName == expectedDescendantName
52         where: 'the following data is used'
53             scenario         | cpsPath          || expectedDescendantName
54             'yang container' | '//cps-path'     || 'cps-path'
55             'parent & child' | '//parent/child' || 'parent/child'
56     }
57
58     def 'Parse cps path that ends with a yang list containing #scenario.'() {
59         when: 'the given cps path is parsed'
60             def result = CpsPathQuery.createFrom(cpsPath)
61         then: 'the query has the right type'
62             result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES
63         and: 'the right ends with parameters are set'
64             result.descendantName == "child"
65             result.leavesData.size() == expectedNumberOfLeaves
66         where: 'the following data is used'
67             scenario                  | cpsPath                                            || expectedNumberOfLeaves
68             'one attribute'           | '//child[@common-leaf-name-int=5]'                 || 1
69             'more than one attribute' | '//child[@int-leaf=5 and @leaf-name="leaf value"]' || 2
70     }
71
72     def 'Parse cps path with error: #scenario.'() {
73         when: 'the given cps path is parsed'
74             CpsPathQuery.createFrom(cpsPath)
75         then: 'a CpsPathException is thrown'
76             thrown(IllegalStateException)
77         where: 'the following data is used'
78             scenario                                                            | cpsPath
79             'no / at the start'                                                 | 'invalid-cps-path/child'
80             'additional / after descendant option'                              | '///cps-path'
81             'float value'                                                       | '/parent/child[@someFloat=5.0]'
82             'unmatched quotes, double quote first '                             | '/parent/child[@someString="value with unmatched quotes\']'
83             'unmatched quotes, single quote first'                              | '/parent/child[@someString=\'value with unmatched quotes"]'
84             'end with descendant and more than one attribute separated by "or"' | '//child[@int-leaf=5 or @leaf-name="leaf value"]'
85             'missing attribute value'                                           | '//child[@int-leaf=5 and @name]'
86             'incomplete ancestor value'                                         | '//books/ancestor::'
87     }
88
89     def 'Parse cps path using ancestor by schema node identifier with a #scenario.'() {
90         when: 'the given cps path is parsed'
91             def result = CpsPathQuery.createFrom('//descendant/ancestor::' + ancestorPath)
92         then: 'the query has the right type'
93             result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
94         and: 'the result has ancestor axis'
95             result.hasAncestorAxis()
96         and: 'the correct ancestor schema node identifier is set'
97             result.ancestorSchemaNodeIdentifier == ancestorPath
98         where:
99             scenario                  | ancestorPath
100             'basic container'         | 'someContainer'
101             'container with parent'   | 'parent/child'
102             'ancestor that is a list' | 'categories[@code=1]'
103             'parent that is a list'   | 'parent[@id=1]/child'
104     }
105
106     def 'Combinations #scenario.'() {
107         when: 'the given cps path is parsed'
108             def result = CpsPathQuery.createFrom(cpsPath + '/ancestor::someAncestor')
109         then: 'the query has the right type'
110             result.cpsPathQueryType == expectedCpsPathQueryType
111         and: 'the result has ancestor axis'
112             result.hasAncestorAxis()
113         and: 'the correct ancestor schema node identifier is set'
114             result.ancestorSchemaNodeIdentifier == 'someAncestor'
115             result.descendantName == expectedDescendantName
116         where:
117             scenario                     | cpsPath                               || expectedDescendantName | expectedCpsPathQueryType
118             'basic container'            | '//someContainer'                     || 'someContainer'        | CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
119             'container with parent'      | '//parent/child'                      || 'parent/child'         | CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
120             'container with list-parent' | '//parent[@id=1]/child'               || 'parent[@id=1]/child'  | CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
121             'container with list-parent' | '//parent[@id=1]/child[@name="test"]' || 'parent[@id=1]/child'  | CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES
122     }
123
124 }