Merge "Merge 2 'query' end points in NCMP"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / CmHandleQueryRestParametersValidatorSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.utils
22
23 import org.onap.cps.spi.exceptions.DataValidationException
24 import org.onap.cps.spi.model.CmHandleQueryParameters
25 import org.onap.cps.spi.model.ConditionProperties
26 import spock.lang.Specification
27
28 class CmHandleQueryRestParametersValidatorSpec extends Specification {
29     def 'CM Handle Query validation: empty query.'() {
30         given: 'a cm handle query'
31             def cmHandleQueryParameters = new CmHandleQueryParameters()
32         when: 'validator is invoked'
33             CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters)
34         then: 'data validation exception is not thrown'
35             noExceptionThrown()
36     }
37
38     def 'CM Handle Query validation: normal query.'() {
39         given: 'a cm handle query'
40             def cmHandleQueryParameters = new CmHandleQueryParameters()
41             def condition = new ConditionProperties()
42             condition.conditionName = 'hasAllProperties'
43             condition.conditionParameters = [[key1:'value1'],[key2:'value2']]
44             cmHandleQueryParameters.cmHandleQueryParameters = [condition]
45         when: 'validator is invoked'
46             CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters)
47         then: 'data validation exception is not thrown'
48             noExceptionThrown()
49     }
50
51     def 'CM Handle Query validation: #scenario.'() {
52         given: 'a cm handle query'
53             def cmHandleQueryParameters = new CmHandleQueryParameters()
54             def condition = new ConditionProperties()
55             condition.conditionName = conditionName
56             condition.conditionParameters = conditionParameters
57             cmHandleQueryParameters.cmHandleQueryParameters = [condition]
58         when: 'validator is invoked'
59             CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters)
60         then: 'a data validation exception is thrown'
61             thrown(DataValidationException)
62         where:
63             scenario               | conditionName      | conditionParameters
64             'empty properties'     | 'hasAllProperties' | [[ : ]]
65             'empty conditions'     | 'hasAllProperties' | []
66             'wrong condition name' | 'wrong'            | []
67             'no condition name'    | ''                 | []
68             'too many properties'  | 'hasAllProperties' | [[key1:'value1', key2:'value2']]
69             'wrong properties'     | 'hasAllProperties' | [['':'wrong']]
70     }
71
72     def 'CM Handle Query validation: validate module name condition properties - valid query.'() {
73         given: 'a condition property'
74             def conditionProperty = [moduleName: 'value']
75         when: 'validator is invoked'
76             CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties(conditionProperty)
77         then: 'data validation exception is not thrown'
78             noExceptionThrown()
79     }
80
81     def 'CM Handle Query validation: validate module name condition properties - #scenario.'() {
82         when: 'validator is invoked'
83             CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties(conditionProperty)
84         then: 'a data validation exception is thrown'
85             thrown(DataValidationException)
86         where:
87             scenario        | conditionProperty
88             'invalid value' | [moduleName: '']
89             'invalid name'  | [wrongName: 'value']
90     }
91 }