CPS-265 - Update cps path query to support 'ends with'
[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 for a leaf of type : #type.'() {
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 == '/parent-200/child-202'
38             result.leafName == expectedLeafName
39             result.leafValue == expectedLeafValue
40         where: 'the following data is used'
41             type                        | cpsPath                                                          || expectedLeafName       | expectedLeafValue
42             'String'                    | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' || 'common-leaf-name'     | 'common-leaf-value'
43             'Integer'                   | '/parent-200/child-202[@common-leaf-name-int=5]'                 || 'common-leaf-name-int' | 5
44             'Integer value with spaces' | '/parent-200/child-202[@common-leaf-name-int = 5]'               || 'common-leaf-name-int' | 5
45     }
46
47     @Unroll
48     def 'Parse cps path of type ends with a #scenario.'() {
49         when: 'the given cps path is parsed'
50             def result = objectUnderTest.createFrom(cpsPath)
51         then: 'the query has the right type'
52             result.cpsPathQueryType == CpsPathQueryType.XPATH_ENDS_WITH
53         and: 'the right ends with parameters are set'
54             result.endsWith == expectedEndsWithValue
55         where: 'the following data is used'
56             scenario         | cpsPath                   || expectedEndsWithValue
57             'yang container' | '///cps-path'             || '/cps-path'
58             'yang list'      | '///cps-path[@key=value]' || '/cps-path[@key=value]'
59     }
60
61     @Unroll
62     def 'Parse cps path with #scenario.'() {
63         when: 'the given cps path is parsed'
64             objectUnderTest.createFrom(cpsPath)
65         then: 'a CpsPathException is thrown'
66             thrown(CpsPathException)
67         where: 'the following data is used'
68             scenario            | cpsPath
69             'no / at the start' | 'invalid-cps-path/child'
70             'float value'       | '/parent-200/child-202[@common-leaf-name-float=5.0]'
71     }
72 }