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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils
23 import org.onap.cps.spi.exceptions.DataValidationException
24 import org.onap.cps.spi.model.CmHandleQueryServiceParameters
25 import org.onap.cps.spi.model.ConditionProperties
26 import spock.lang.Specification
28 class CmHandleQueryRestParametersValidatorSpec extends Specification {
29 def 'CM Handle Query validation: empty query.'() {
30 given: 'a cm handle query'
31 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
32 when: 'validator is invoked'
33 CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters)
34 then: 'data validation exception is not thrown'
38 def 'CM Handle Query validation: normal query.'() {
39 given: 'a cm handle query'
40 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
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'
51 def 'CM Handle Query validation: #scenario.'() {
52 given: 'a cm handle query'
53 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
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)
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']]
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'
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)
87 scenario | conditionProperty
88 'invalid value' | [moduleName: '']
89 'invalid name' | [wrongName: 'value']
92 def 'Validate CmHandle where an exception is thrown due to #scenario.'() {
93 when: 'the validator is called on a cps path condition property'
94 CmHandleQueryRestParametersValidator.validateCpsPathConditionProperties(conditionProperty)
95 then: 'a data validation exception is thrown'
96 def e = thrown(DataValidationException)
97 and: 'exception message matches the expected message'
98 e.details.contains(exceptionMessage)
100 scenario | conditionProperty || exceptionMessage
101 'more than one condition is supplied' | ['cpsPath':'some-path', 'cpsPath2':'some-path'] || 'Only one condition property is allowed for the CPS path query.'
102 'cpsPath key not supplied' | ['wrong-key':'some-path'] || 'Wrong CPS path condition property. - expecting "cpsPath" as the condition property.'
103 'cpsPath not supplied' | ['cpsPath':''] || 'Wrong CPS path. - please supply a valid CPS path.'
106 def 'No conditions.'() {
107 expect: 'no conditions always returns true'
108 CmHandleQueryRestParametersValidator.validateCpsPathConditionProperties([:]) == true
111 def 'Validate CmHandle where #scenario.'() {
112 when: 'the validator is called on a cps path condition property'
113 def result = CmHandleQueryRestParametersValidator.validateCpsPathConditionProperties(['cpsPath':cpsPath])
114 then: 'the expected boolean value is returned'
115 result == expectedBoolean
117 scenario | cpsPath || expectedBoolean
118 'cpsPath is valid' | '/some/valid/path' || true
119 'cpsPath attempts to query private properties' | "//additional-properties[@some-property='some-value']" || false