0bc0c1e553e0b649e07e701c5a9f0504635a9b22
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / NcmpRestInputMapperSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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.rest.controller
22
23 import org.mapstruct.factory.Mappers
24 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel
25 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
26 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters
27 import org.onap.cps.ncmp.rest.model.ConditionProperties
28 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration
29 import org.onap.cps.ncmp.rest.model.RestInputCmHandle
30 import org.onap.cps.ncmp.rest.model.RestModuleDefinition
31 import org.onap.cps.ncmp.rest.model.RestModuleReference
32 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
33 import org.onap.cps.spi.model.ModuleDefinition
34 import org.onap.cps.spi.model.ModuleReference
35 import spock.lang.Specification
36
37 class NcmpRestInputMapperSpec extends Specification {
38
39     def objectUnderTest = Mappers.getMapper(NcmpRestInputMapper.class)
40
41     def 'Convert a created REST CM Handle Input to an NCMP Service CM Handle with #scenario'() {
42         given: 'a rest cm handle input'
43             def inputRestCmHandle = new RestInputCmHandle(cmHandle : 'example-id', cmHandleProperties: registrationDmiProperties,
44                 publicCmHandleProperties: registrationPublicProperties, trustLevel: registrationTrustLevel, alternateId: 'my-alternate-id', moduleSetTag: 'my-module-set-tag')
45             def restDmiPluginRegistration = new RestDmiPluginRegistration(
46                 createdCmHandles: [inputRestCmHandle])
47         when: 'to plugin dmi registration is called'
48             def result  = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
49         then: 'the result returns the correct number of cm handles'
50             result.createdCmHandles.size() == 1
51         and: 'the converted cm handle has the same id'
52             result.createdCmHandles[0].cmHandleId == 'example-id'
53         and: '(empty) properties are converted correctly'
54             result.createdCmHandles[0].dmiProperties == mappedDmiProperties
55             result.createdCmHandles[0].publicProperties == mappedPublicProperties
56             result.createdCmHandles[0].registrationTrustLevel == mappedTrustLevel
57         and: 'alternate ID and module set tag converted correctly'
58             result.createdCmHandles[0].alternateId == 'my-alternate-id'
59             result.createdCmHandles[0].moduleSetTag == 'my-module-set-tag'
60         where: 'the following parameters are used'
61             scenario                    | registrationDmiProperties                | registrationPublicProperties                           | registrationTrustLevel || mappedDmiProperties                      | mappedPublicProperties                                 | mappedTrustLevel
62             'dmi and public properties' | ['Property-Example': 'example property'] | ['Public-Property-Example': 'public example property'] | 'COMPLETE'             || ['Property-Example': 'example property'] | ['Public-Property-Example': 'public example property'] | TrustLevel.COMPLETE
63             'no properties'             | null                                     | null                                                   | null                   || [:]                                      | [:]                                                    | null
64     }
65
66     def 'Handling empty dmi registration'() {
67         given: 'a rest cm handle input without any cm handles'
68             def restDmiPluginRegistration = new RestDmiPluginRegistration()
69         when: 'to plugin dmi registration is called'
70             def result  = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
71         then: 'unspecified lists remain as empty lists'
72             assert result.createdCmHandles == []
73             assert result.updatedCmHandles == []
74             assert result.removedCmHandles == []
75     }
76
77     def 'Handling non-empty dmi registration'() {
78         given: 'a rest cm handle input with cm handles'
79             def restDmiPluginRegistration = new RestDmiPluginRegistration(
80                 createdCmHandles: [new RestInputCmHandle()],
81                 updatedCmHandles: [new RestInputCmHandle()],
82                 removedCmHandles: ["some-cmHandle"]
83             )
84         when: 'to dmi plugin registration is called'
85             def result  = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
86         then: 'Lists contain values'
87             assert result.createdCmHandles[0].class == NcmpServiceCmHandle.class
88             assert result.updatedCmHandles[0].class == NcmpServiceCmHandle.class
89             assert result.removedCmHandles == ["some-cmHandle"]
90     }
91
92     def 'Convert a ModuleReference to a RestModuleReference'() {
93         given: 'a ModuleReference'
94             def moduleReference = new ModuleReference()
95         when: 'toRestModuleReference is called'
96             def result = objectUnderTest.toRestModuleReference(moduleReference)
97         then: 'the result is of the correct class RestModuleReference'
98             result.class == RestModuleReference.class
99     }
100
101     def 'Convert a ModuleDefinition to a RestModuleDefinition'() {
102         given: 'a ModuleDefinition'
103             def moduleDefinition = new ModuleDefinition('moduleName','revision', 'content')
104         when: 'toRestModuleDefinition is called'
105             def result = objectUnderTest.toRestModuleDefinition(moduleDefinition)
106         then: 'the result is of the correct class RestModuleDefinition'
107             result.class == RestModuleDefinition.class
108         and: 'all contents are mapped correctly'
109             result.toString()=='class RestModuleDefinition {\n' +
110                     '    moduleName: moduleName\n' +
111                     '    revision: revision\n' +
112                     '    content: content\n' +
113                     '}'
114     }
115
116     def 'Convert a CmHandle REST query to CmHandle query service parameters.'() {
117         given: 'a CmHandle REST query with two conditions'
118             def conditionParameter1 = new ConditionProperties(conditionName: 'some condition', conditionParameters: [[p1:1]] )
119             def conditionParameter2 = new ConditionProperties(conditionName: 'other condition', conditionParameters: [[p2:2]] )
120             def cmHandleQuery = new CmHandleQueryParameters()
121             cmHandleQuery.cmHandleQueryParameters = [conditionParameter1, conditionParameter2]
122         when: 'it is converted into CmHandle query service parameters'
123             def result = objectUnderTest.toCmHandleQueryServiceParameters(cmHandleQuery)
124         then: 'the result is of the correct class'
125             assert result instanceof CmHandleQueryServiceParameters
126         and: 'the result has the same conditions'
127             assert result.cmHandleQueryParameters.size() == 2
128             assert result.cmHandleQueryParameters[0].conditionName == 'some condition'
129             assert result.cmHandleQueryParameters[0].conditionParameters == [[p1:1]]
130             assert result.cmHandleQueryParameters[1].conditionName == 'other condition'
131             assert result.cmHandleQueryParameters[1].conditionParameters == [[p2:2]]
132     }
133 }