From d5bda8848a661465f214b0bf11211e63b272cfd6 Mon Sep 17 00:00:00 2001 From: lukegleeson Date: Wed, 2 Mar 2022 14:32:47 +0000 Subject: Replacing ModelMapper with MapStruct - Removed Model Mapper from pom files - Replaced ModelMapper with MapStruct - Added Tests for MapStruct - Changed mapstruct annotations for individual variables to be null safe rather than all variables - Excluded generated code from code coverage - Set ModuleReferences input to required for SchemaSet so that ModuleReferences list set to empty list rather than null Issue-ID: CPS-127 Signed-off-by: lukegleeson Change-Id: I43f874aea79f58dda5526c6fdead27d8474d90af --- .../rest/controller/NcmpRestInputMapperSpec.groovy | 90 ++++++++++++++++++++++ .../controller/NetworkCmProxyControllerSpec.groovy | 7 +- .../NetworkCmProxyInventoryControllerSpec.groovy | 4 +- .../rest/controller/RestInputMapperSpec.groovy | 64 --------------- .../NetworkCmProxyRestExceptionHandlerSpec.groovy | 11 ++- 5 files changed, 100 insertions(+), 76 deletions(-) create mode 100644 cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NcmpRestInputMapperSpec.groovy delete mode 100644 cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/RestInputMapperSpec.groovy (limited to 'cps-ncmp-rest/src/test') diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NcmpRestInputMapperSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NcmpRestInputMapperSpec.groovy new file mode 100644 index 000000000..3d54a0b08 --- /dev/null +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NcmpRestInputMapperSpec.groovy @@ -0,0 +1,90 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.rest.controller + +import org.mapstruct.factory.Mappers +import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle +import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration +import org.onap.cps.ncmp.rest.model.RestInputCmHandle +import org.onap.cps.ncmp.rest.model.RestModuleReference +import org.onap.cps.spi.model.ModuleReference +import spock.lang.Specification + +class NcmpRestInputMapperSpec extends Specification { + + def objectUnderTest = Mappers.getMapper(NcmpRestInputMapper.class) + + def 'Convert a created REST CM Handle Input to an NCMP Service CM Handle with #scenario'() { + given: 'a rest cm handle input' + def inputRestCmHandle = new RestInputCmHandle(cmHandle : 'example-id', cmHandleProperties: dmiProperties, + publicCmHandleProperties: publicProperties) + def restDmiPluginRegistration = new RestDmiPluginRegistration( + createdCmHandles: [inputRestCmHandle]) + when: 'to plugin dmi registration is called' + def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration) + then: 'the result returns the correct number of cm handles' + result.createdCmHandles.size() == 1 + and: 'the converted cm handle has the same id' + result.createdCmHandles[0].cmHandleID == 'example-id' + and: '(empty) properties are converted correctly' + result.createdCmHandles[0].dmiProperties == expectedDmiProperties + result.createdCmHandles[0].publicProperties == expectedPublicProperties + where: 'the following parameters are used' + scenario | dmiProperties | publicProperties || expectedDmiProperties | expectedPublicProperties + 'dmi and public properties' | ['Property-Example': 'example property'] | ['Public-Property-Example': 'public example property'] || ['Property-Example': 'example property'] | ['Public-Property-Example': 'public example property'] + 'no properties' | null | null || [:] | [:] + } + + def 'Handling empty dmi registration'() { + given: 'a rest cm handle input without any cm handles' + def restDmiPluginRegistration = new RestDmiPluginRegistration() + when: 'to plugin dmi registration is called' + def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration) + then: 'unspecified lists remain as empty lists' + assert result.createdCmHandles == [] + assert result.updatedCmHandles == [] + assert result.removedCmHandles == [] + } + + def 'Handling non-empty dmi registration'() { + given: 'a rest cm handle input with cm handles' + def restDmiPluginRegistration = new RestDmiPluginRegistration( + createdCmHandles: [new RestInputCmHandle()], + updatedCmHandles: [new RestInputCmHandle()], + removedCmHandles: ["some-cmHandle"] + ) + when: 'to dmi plugin registration is called' + def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration) + then: 'Lists contain values' + assert result.createdCmHandles[0].class == NcmpServiceCmHandle.class + assert result.updatedCmHandles[0].class == NcmpServiceCmHandle.class + assert result.removedCmHandles == ["some-cmHandle"] + } + + def 'Convert a ModuleReference to a RestModuleReference'() { + given: 'a ModuleReference' + def moduleReference = new ModuleReference() + when: 'toRestModuleReference is called' + def result = objectUnderTest.toRestModuleReference(moduleReference) + then: 'the result is of the correct class RestModuleReference' + result.class == RestModuleReference.class + } +} diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy index c99771443..a506e6ae0 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy @@ -22,7 +22,7 @@ package org.onap.cps.ncmp.rest.controller - +import org.mapstruct.factory.Mappers import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH @@ -36,7 +36,6 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE import com.fasterxml.jackson.databind.ObjectMapper -import org.modelmapper.ModelMapper import org.onap.cps.TestUtils import org.onap.cps.spi.model.ModuleReference import org.onap.cps.utils.JsonObjectMapper @@ -60,10 +59,10 @@ class NetworkCmProxyControllerSpec extends Specification { NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock() @SpringBean - ModelMapper modelMapper = new ModelMapper() + JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) @SpringBean - JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) + NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper) @Value('${rest.api.ncmp-base-path}/v1') def ncmpBasePathV1 diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyInventoryControllerSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyInventoryControllerSpec.groovy index 079554a22..9b1c2e87c 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyInventoryControllerSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyInventoryControllerSpec.groovy @@ -49,7 +49,7 @@ class NetworkCmProxyInventoryControllerSpec extends Specification { NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock() @SpringBean - RestInputMapper restInputMapper = Mock() + NcmpRestInputMapper ncmpRestInputMapper = Mock() DmiPluginRegistration mockDmiPluginRegistration = Mock() @@ -64,7 +64,7 @@ class NetworkCmProxyInventoryControllerSpec extends Specification { and: 'the expected rest input as an object' def expectedRestDmiPluginRegistration = jsonObjectMapper.convertJsonString(jsonData, RestDmiPluginRegistration) and: 'the converter returns a dmi registration (only for the expected input object)' - restInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration + ncmpRestInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration when: 'post request is performed & registration is called with correct DMI plugin information' def response = mvc.perform( post("$ncmpBasePathV1/ch") diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/RestInputMapperSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/RestInputMapperSpec.groovy deleted file mode 100644 index ed938810c..000000000 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/RestInputMapperSpec.groovy +++ /dev/null @@ -1,64 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022 Nordix Foundation - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.rest.controller - -import org.mapstruct.factory.Mappers -import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration -import org.onap.cps.ncmp.rest.model.RestInputCmHandle -import spock.lang.Specification - -class RestInputMapperSpec extends Specification { - - def objectUnderTest = Mappers.getMapper(RestInputMapper.class) - - def 'Convert a created REST CM Handle Input to an NCMP Service CM Handle with #scenario'() { - given: 'a rest cm handle input' - def inputRestCmHandle = new RestInputCmHandle(cmHandle : 'example-id', cmHandleProperties: dmiProperties, - publicCmHandleProperties: publicProperties) - def restDmiPluginRegistration = new RestDmiPluginRegistration( - createdCmHandles: [inputRestCmHandle]) - when: 'to plugin dmi registration is called' - def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration) - then: 'the result returns the correct number of cm handles' - result.createdCmHandles.size() == 1 - and: 'the converted cm handle has the same id' - result.createdCmHandles[0].cmHandleID == 'example-id' - and: '(empty) properties are converted correctly' - result.createdCmHandles[0].dmiProperties == expectedDmiProperties - result.createdCmHandles[0].publicProperties == expectedPublicProperties - where: 'the following parameters are used' - scenario | dmiProperties | publicProperties || expectedDmiProperties | expectedPublicProperties - 'dmi and public properties' | ['Property-Example': 'example property'] | ['Public-Property-Example': 'public example property'] || ['Property-Example': 'example property'] | ['Public-Property-Example': 'public example property'] - 'no properties' | null | null || [:] | [:] - } - - def 'Handling empty dmi registration'() { - given: 'a rest cm handle input without any cm handles' - def restDmiPluginRegistration = new RestDmiPluginRegistration() - when: 'to plugin dmi registration is called' - def result = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration) - then: 'unspecified lists remain as empty lists' - assert result.createdCmHandles == [] - assert result.updatedCmHandles == [] - assert result.removedCmHandles == [] - } - -} diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy index 8004328bc..3c39a3307 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy @@ -21,13 +21,15 @@ package org.onap.cps.ncmp.rest.exceptions +import com.fasterxml.jackson.databind.ObjectMapper import groovy.json.JsonSlurper -import org.modelmapper.ModelMapper +import org.mapstruct.factory.Mappers import org.onap.cps.TestUtils import org.onap.cps.ncmp.api.NetworkCmProxyDataService import org.onap.cps.ncmp.api.impl.exception.DmiRequestException import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException -import org.onap.cps.ncmp.rest.controller.RestInputMapper +import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle +import org.onap.cps.ncmp.rest.controller.NcmpRestInputMapper import org.onap.cps.spi.exceptions.CpsException import org.onap.cps.spi.exceptions.DataNodeNotFoundException import org.onap.cps.spi.exceptions.DataValidationException @@ -57,14 +59,11 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification { @SpringBean NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock() - @SpringBean - ModelMapper modelMapper = Stub() - @SpringBean JsonObjectMapper jsonObjectMapper = Stub() @SpringBean - RestInputMapper restInputMapper = Mock() + NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper) @Value('${rest.api.ncmp-base-path}') def basePathNcmp -- cgit 1.2.3-korg