Merge "CmHandle delete is failing with InternalServerError: Null key is not allowed...
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / RestQueryParametersValidatorSpec.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.ncmp.api.impl.utils
22
23 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
24 import org.onap.cps.spi.exceptions.DataValidationException
25 import org.onap.cps.spi.model.ConditionProperties
26 import spock.lang.Specification
27
28 class RestQueryParametersValidatorSpec extends Specification {
29
30
31     def 'CM Handle Query validation: empty query.'() {
32         given: 'a cm handle query'
33             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
34         when: 'validator is invoked'
35             RestQueryParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters, [])
36         then: 'data validation exception is not thrown'
37             noExceptionThrown()
38     }
39
40     def 'CM Handle Query validation: normal query.'() {
41         given: 'a cm handle query'
42             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
43             def condition = new ConditionProperties()
44             condition.conditionName = 'validConditionName'
45             condition.conditionParameters = [['key':'value']]
46         cmHandleQueryParameters.cmHandleQueryParameters = [condition]
47         when: 'validator is invoked'
48             RestQueryParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters, ['validConditionName'])
49         then: 'data validation exception is not thrown'
50             noExceptionThrown()
51     }
52
53     def 'CM Handle Query validation: #scenario.'() {
54         given: 'a cm handle query'
55             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
56             def condition = new ConditionProperties()
57             condition.conditionName = conditionName
58             condition.conditionParameters = conditionParameters
59             cmHandleQueryParameters.cmHandleQueryParameters = [condition]
60         when: 'validator is invoked'
61             RestQueryParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters, ['validConditionName'])
62         then: 'a data validation exception is thrown'
63             def thrown = thrown(DataValidationException)
64         and: 'the exception details contain the correct significant term '
65             thrown.details.contains(expectedWordInDetails)
66         where:
67             scenario                 | conditionName        | conditionParameters                || expectedWordInDetails
68             'unknown condition name' | 'unknownCondition'   | [['key': 'value']]                 || 'conditionName'
69             'no condition name'      | ''                   | [['key': 'value']]                 || 'conditionName'
70             'empty properties'       | 'validConditionName' | [[:]]                              || 'conditionsParameter'
71             'empty conditions'       | 'validConditionName' | [[:]]                              || 'conditionsParameter'
72             'too many properties'    | 'validConditionName' | [[key1: 'value1', key2: 'value2']] || 'conditionsParameter'
73             'empty key'              | 'validConditionName' | [['': 'wrong']]                    || 'conditionsParameter'
74     }
75
76     def 'CM Handle Query validation: validate module name condition properties - valid query.'() {
77         given: 'a condition property'
78             def conditionProperty = [moduleName: 'value']
79         when: 'validator is invoked'
80             RestQueryParametersValidator.validateModuleNameConditionProperties(conditionProperty)
81         then: 'data validation exception is not thrown'
82             noExceptionThrown()
83     }
84
85     def 'CM Handle Query validation: validate module name condition properties - #scenario.'() {
86         when: 'validator is invoked'
87             RestQueryParametersValidator.validateModuleNameConditionProperties(conditionProperty)
88         then: 'a data validation exception is thrown'
89             thrown(DataValidationException)
90         where:
91             scenario        | conditionProperty
92             'invalid value' | [moduleName: '']
93             'invalid name'  | [wrongName: 'value']
94     }
95
96     def 'Validate CmHandle where an exception is thrown due to #scenario.'() {
97         when: 'the validator is called on a cps path condition property'
98             RestQueryParametersValidator.validateCpsPathConditionProperties(conditionProperty)
99         then: 'a data validation exception is thrown'
100             def e = thrown(DataValidationException)
101         and: 'exception message matches the expected message'
102             e.details.contains(exceptionMessage)
103         where:
104             scenario                              | conditionProperty                               || exceptionMessage
105             'more than one condition is supplied' | ['cpsPath':'some-path', 'cpsPath2':'some-path'] || 'Only one condition property is allowed for the CPS path query.'
106             'cpsPath key not supplied'            | ['wrong-key':'some-path']                       || 'Wrong CPS path condition property. - expecting "cpsPath" as the condition property.'
107             'cpsPath not supplied'                | ['cpsPath':'']                                  || 'Wrong CPS path. - please supply a valid CPS path.'
108     }
109
110     def 'No conditions.'() {
111         expect: 'no conditions always returns true'
112             RestQueryParametersValidator.validateCpsPathConditionProperties([:]) == true
113     }
114
115     def 'Validate CmHandle where #scenario.'() {
116         when: 'the validator is called on a cps path condition property'
117             def result = RestQueryParametersValidator.validateCpsPathConditionProperties(['cpsPath':cpsPath])
118         then: 'the expected boolean value is returned'
119             result == expectedBoolean
120         where:
121             scenario                                       | cpsPath                                                || expectedBoolean
122             'cpsPath is valid'                             | '/some/valid/path'                                     || true
123             'cpsPath attempts to query private properties' | "//additional-properties[@some-property='some-value']" || false
124     }
125 }