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