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