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
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.ncmp.rest.controller
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
37 class NcmpRestInputMapperSpec extends Specification {
39 def objectUnderTest = Mappers.getMapper(NcmpRestInputMapper.class)
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)
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 where: 'the following parameters are used'
58 scenario | registrationDmiProperties | registrationPublicProperties | registrationTrustLevel || mappedDmiProperties | mappedPublicProperties | mappedTrustLevel
59 '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
60 'no properties' | null | null | null || [:] | [:] | null
63 def 'Handling empty dmi registration'() {
64 given: 'a rest cm handle input without any cm handles'
65 def restDmiPluginRegistration = new RestDmiPluginRegistration()
66 when: 'to plugin dmi registration is called'
67 def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
68 then: 'unspecified lists remain as empty lists'
69 assert result.createdCmHandles == []
70 assert result.updatedCmHandles == []
71 assert result.removedCmHandles == []
74 def 'Handling non-empty dmi registration'() {
75 given: 'a rest cm handle input with cm handles'
76 def restDmiPluginRegistration = new RestDmiPluginRegistration(
77 createdCmHandles: [new RestInputCmHandle()],
78 updatedCmHandles: [new RestInputCmHandle()],
79 removedCmHandles: ["some-cmHandle"]
81 when: 'to dmi plugin registration is called'
82 def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
83 then: 'Lists contain values'
84 assert result.createdCmHandles[0].class == NcmpServiceCmHandle.class
85 assert result.updatedCmHandles[0].class == NcmpServiceCmHandle.class
86 assert result.removedCmHandles == ["some-cmHandle"]
89 def 'Convert a ModuleReference to a RestModuleReference'() {
90 given: 'a ModuleReference'
91 def moduleReference = new ModuleReference()
92 when: 'toRestModuleReference is called'
93 def result = objectUnderTest.toRestModuleReference(moduleReference)
94 then: 'the result is of the correct class RestModuleReference'
95 result.class == RestModuleReference.class
98 def 'Convert a ModuleDefinition to a RestModuleDefinition'() {
99 given: 'a ModuleDefinition'
100 def moduleDefinition = new ModuleDefinition('moduleName','revision', 'content')
101 when: 'toRestModuleDefinition is called'
102 def result = objectUnderTest.toRestModuleDefinition(moduleDefinition)
103 then: 'the result is of the correct class RestModuleDefinition'
104 result.class == RestModuleDefinition.class
105 and: 'all contents are mapped correctly'
106 result.toString()=='class RestModuleDefinition {\n' +
107 ' moduleName: moduleName\n' +
108 ' revision: revision\n' +
109 ' content: content\n' +
113 def 'Convert a CmHandle REST query to CmHandle query service parameters.'() {
114 given: 'a CmHandle REST query with two conditions'
115 def conditionParameter1 = new ConditionProperties(conditionName: 'some condition', conditionParameters: [[p1:1]] )
116 def conditionParameter2 = new ConditionProperties(conditionName: 'other condition', conditionParameters: [[p2:2]] )
117 def cmHandleQuery = new CmHandleQueryParameters()
118 cmHandleQuery.cmHandleQueryParameters = [conditionParameter1, conditionParameter2]
119 when: 'it is converted into CmHandle query service parameters'
120 def result = objectUnderTest.toCmHandleQueryServiceParameters(cmHandleQuery)
121 then: 'the result is of the correct class'
122 assert result instanceof CmHandleQueryServiceParameters
123 and: 'the result has the same conditions'
124 assert result.cmHandleQueryParameters.size() == 2
125 assert result.cmHandleQueryParameters[0].conditionName == 'some condition'
126 assert result.cmHandleQueryParameters[0].conditionParameters == [[p1:1]]
127 assert result.cmHandleQueryParameters[1].conditionName == 'other condition'
128 assert result.cmHandleQueryParameters[1].conditionParameters == [[p2:2]]