7f558dd10b3fc10b913e40ffa69280d2e83d0653
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / query / 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.spi.query
21
22 import org.onap.cps.spi.exceptions.CpsPathException
23 import spock.lang.Specification
24 import spock.lang.Unroll
25
26 class CpsPathQuerySpec extends Specification {
27
28     def objectUnderTest = new CpsPathQuery()
29
30     @Unroll
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 = objectUnderTest.createFrom(cpsPath)
34         then: 'the query has the right type'
35             result.cpsPathQueryType == CpsPathQueryType.XPATH_LEAF_VALUE
36         and: 'the right query parameters are set'
37             result.xpathPrefix == expectedXpathPrefix
38             result.leafName == expectedLeafName
39             result.leafValue == 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 Integer' | '/parent/child[@common-leaf-name-int=5]'                 || '/parent/child'     |'common-leaf-name-int'  | 5
44             'spaces around ='      | '/parent/child[@common-leaf-name-int = 5]'               || '/parent/child'     |'common-leaf-name-int'  | 5
45             'key in top container' | '/parent[@common-leaf-name-int=5]'                       || '/parent'           |'common-leaf-name-int'  | 5
46     }
47
48     @Unroll
49     def 'Parse cps path of type ends with a #scenario.'() {
50         when: 'the given cps path is parsed'
51             def result = objectUnderTest.createFrom(cpsPath)
52         then: 'the query has the right type'
53             result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE
54         and: 'the right ends with parameters are set'
55             result.descendantName == expectedEndsWithValue
56         where: 'the following data is used'
57             scenario         | cpsPath                  || expectedEndsWithValue
58             'yang container' | '//cps-path'             || 'cps-path'
59             'yang list'      | '//cps-path[@key=value]' || 'cps-path[@key=value]'
60             'parent & child' | '//parent/child'         || 'parent/child'
61     }
62
63     @Unroll
64     def 'Parse cps path with #scenario.'() {
65         when: 'the given cps path is parsed'
66             objectUnderTest.createFrom(cpsPath)
67         then: 'a CpsPathException is thrown'
68             thrown(CpsPathException)
69         where: 'the following data is used'
70             scenario                               | cpsPath
71             'no / at the start'                    | 'invalid-cps-path/child'
72             'additional / after descendant option' | '///cps-path'
73             'float value'                          | '/parent-200/child-202[@common-leaf-name-float=5.0]'
74             'too many containers'                  | '/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100[@a=1]'
75     }
76 }