2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 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, alternateId: 'my-alternate-id', moduleSetTag: 'my-module-set-tag', dataProducerIdentifier: 'my-data-producer-identifier')
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 and: 'other fields are mapped correctly'
57 result.createdCmHandles[0].alternateId == 'my-alternate-id'
58 result.createdCmHandles[0].moduleSetTag == 'my-module-set-tag'
59 result.createdCmHandles[0].registrationTrustLevel == mappedTrustLevel
60 result.createdCmHandles[0].dataProducerIdentifier == 'my-data-producer-identifier'
61 where: 'the following parameters are used'
62 scenario | registrationDmiProperties | registrationPublicProperties | registrationTrustLevel || mappedDmiProperties | mappedPublicProperties | mappedTrustLevel
63 '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
64 'no properties' | null | null | null || [:] | [:] | null
67 def 'Handling empty dmi registration'() {
68 given: 'a rest cm handle input without any cm handles'
69 def restDmiPluginRegistration = new RestDmiPluginRegistration()
70 when: 'to plugin dmi registration is called'
71 def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
72 then: 'unspecified lists remain as empty lists'
73 assert result.createdCmHandles == []
74 assert result.updatedCmHandles == []
75 assert result.removedCmHandles == []
78 def 'Handling non-empty dmi registration'() {
79 given: 'a rest cm handle input with cm handles'
80 def restDmiPluginRegistration = new RestDmiPluginRegistration(
81 createdCmHandles: [new RestInputCmHandle()],
82 updatedCmHandles: [new RestInputCmHandle()],
83 removedCmHandles: ["some-cmHandle"]
85 when: 'to dmi plugin registration is called'
86 def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
87 then: 'Lists contain values'
88 assert result.createdCmHandles[0].class == NcmpServiceCmHandle.class
89 assert result.updatedCmHandles[0].class == NcmpServiceCmHandle.class
90 assert result.removedCmHandles == ["some-cmHandle"]
93 def 'Convert a ModuleReference to a RestModuleReference'() {
94 given: 'a ModuleReference'
95 def moduleReference = new ModuleReference()
96 when: 'toRestModuleReference is called'
97 def result = objectUnderTest.toRestModuleReference(moduleReference)
98 then: 'the result is of the correct class RestModuleReference'
99 result.class == RestModuleReference.class
102 def 'Convert a ModuleDefinition to a RestModuleDefinition'() {
103 given: 'a ModuleDefinition'
104 def moduleDefinition = new ModuleDefinition('moduleName','revision', 'content')
105 when: 'toRestModuleDefinition is called'
106 def result = objectUnderTest.toRestModuleDefinition(moduleDefinition)
107 then: 'the result is of the correct class RestModuleDefinition'
108 result.class == RestModuleDefinition.class
109 and: 'all contents are mapped correctly'
110 result.toString()=='class RestModuleDefinition {\n' +
111 ' moduleName: moduleName\n' +
112 ' revision: revision\n' +
113 ' content: content\n' +
117 def 'Convert a CmHandle REST query to CmHandle query service parameters.'() {
118 given: 'a CmHandle REST query with two conditions'
119 def conditionParameter1 = new ConditionProperties(conditionName: 'some condition', conditionParameters: [[p1:1]] )
120 def conditionParameter2 = new ConditionProperties(conditionName: 'other condition', conditionParameters: [[p2:2]] )
121 def cmHandleQuery = new CmHandleQueryParameters()
122 cmHandleQuery.cmHandleQueryParameters = [conditionParameter1, conditionParameter2]
123 when: 'it is converted into CmHandle query service parameters'
124 def result = objectUnderTest.toCmHandleQueryServiceParameters(cmHandleQuery)
125 then: 'the result is of the correct class'
126 assert result instanceof CmHandleQueryServiceParameters
127 and: 'the result has the same conditions'
128 assert result.cmHandleQueryParameters.size() == 2
129 assert result.cmHandleQueryParameters[0].conditionName == 'some condition'
130 assert result.cmHandleQueryParameters[0].conditionParameters == [[p1:1]]
131 assert result.cmHandleQueryParameters[1].conditionName == 'other condition'
132 assert result.cmHandleQueryParameters[1].conditionParameters == [[p2:2]]