From 05e7b8a207b1c641468e77d754fe1a5ae9e1a51e Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Tue, 18 Jan 2022 21:57:46 +0530 Subject: [PATCH] Replace gson mapper with jackson mapper We introduced JsonObjectMapper (wapper) as Spring component. Issue-ID: CPS-751 Change-Id: I536b0771a3a263325e6907717baf6941d70c0d6c Signed-off-by: sourabh_sourabh --- cps-ncmp-rest/pom.xml | 6 +- .../rest/controller/NetworkCmProxyController.java | 23 ++---- .../controller/NetworkCmProxyControllerSpec.groovy | 24 ++++-- .../NetworkCmProxyRestExceptionHandlerSpec.groovy | 8 ++ cps-ncmp-service/pom.xml | 6 +- .../api/impl/NetworkCmProxyDataServiceImpl.java | 40 +++------- .../api/impl/operations/DmiDataOperations.java | 12 +-- .../api/impl/operations/DmiModelOperations.java | 17 ++-- .../ncmp/api/impl/operations/DmiOperations.java | 49 +++--------- ...tworkCmProxyDataServiceImplModelSyncSpec.groovy | 11 ++- ...rkCmProxyDataServiceImplRegistrationSpec.groovy | 17 ++-- .../impl/NetworkCmProxyDataServiceImplSpec.groovy | 13 ++-- .../impl/operations/DmiDataOperationsSpec.groovy | 9 ++- .../impl/operations/DmiModelOperationsSpec.groovy | 13 +++- .../cps/rest/controller/DataRestController.java | 31 ++++---- .../cps/rest/controller/QueryRestController.java | 13 ++-- .../rest/controller/DataRestControllerSpec.groovy | 7 +- .../rest/controller/QueryRestControllerSpec.groovy | 14 ++-- .../exceptions/CpsRestExceptionHandlerSpec.groovy | 16 ++-- .../spi/impl/CpsDataPersistenceServiceImpl.java | 61 +++++---------- .../FragmentRepositoryCpsPathQueryImpl.java | 13 ++-- ...CpsDataPersistenceServiceIntegrationSpec.groovy | 10 +-- .../spi/impl/CpsDataPersistenceServiceSpec.groovy | 5 +- .../cps/spi/impl/CpsPersistenceSpecBase.groovy | 8 +- cps-service/pom.xml | 7 +- .../java/org/onap/cps/utils/JsonObjectMapper.java | 90 ++++++++++++++++++++++ .../org/onap/cps/utils/JsonObjectMapperSpec.groovy | 87 +++++++++++++++++++++ 27 files changed, 387 insertions(+), 223 deletions(-) create mode 100644 cps-service/src/main/java/org/onap/cps/utils/JsonObjectMapper.java create mode 100644 cps-service/src/test/groovy/org/onap/cps/utils/JsonObjectMapperSpec.groovy diff --git a/cps-ncmp-rest/pom.xml b/cps-ncmp-rest/pom.xml index 93ee680da..26b83bef8 100644 --- a/cps-ncmp-rest/pom.xml +++ b/cps-ncmp-rest/pom.xml @@ -1,7 +1,7 @@ org.codehaus.groovy diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java index f2f43c989..419f6e926 100755 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java @@ -27,8 +27,6 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH; import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -36,6 +34,7 @@ import java.util.List; import java.util.stream.Collectors; import javax.validation.Valid; import javax.validation.constraints.NotNull; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.modelmapper.ModelMapper; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; @@ -48,6 +47,7 @@ import org.onap.cps.ncmp.rest.model.Conditions; import org.onap.cps.ncmp.rest.model.ModuleNameAsJsonObject; import org.onap.cps.ncmp.rest.model.ModuleNamesAsJsonArray; import org.onap.cps.ncmp.rest.model.ModuleReference; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -56,21 +56,14 @@ import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController @RequestMapping("${rest.api.ncmp-base-path}") +@RequiredArgsConstructor public class NetworkCmProxyController implements NetworkCmProxyApi { - private static final Gson GSON = new GsonBuilder().create(); private static final String NO_BODY = null; - private final ModelMapper modelMapper = new ModelMapper(); + private final ModelMapper modelMapper; private final NetworkCmProxyDataService networkCmProxyDataService; - - /** - * Constructor Injection for Dependencies. - * @param networkCmProxyDataService Data Service Interface - */ - public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) { - this.networkCmProxyDataService = networkCmProxyDataService; - } + private final JsonObjectMapper jsonObjectMapper; /** * Get resource data from operational datastore. @@ -119,7 +112,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { final String cmHandle, final Object requestBody, final String contentType) { final Object responseObject = networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, - resourceIdentifier, PATCH, GSON.toJson(requestBody), contentType); + resourceIdentifier, PATCH, jsonObjectMapper.asJsonString(requestBody), contentType); return ResponseEntity.ok(responseObject); } @@ -136,7 +129,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { public ResponseEntity createResourceDataRunningForCmHandle(final String resourceIdentifier, final String cmHandle, final Object requestBody, final String contentType) { networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, - resourceIdentifier, CREATE, GSON.toJson(requestBody), contentType); + resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType); return new ResponseEntity<>(HttpStatus.CREATED); } @@ -155,7 +148,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { final Object requestBody, final String contentType) { networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, - resourceIdentifier, UPDATE, GSON.toJson(requestBody), contentType); + resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType); return new ResponseEntity<>(HttpStatus.OK); } 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 b5dc2eabb..0c8b2227d 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,9 +22,6 @@ package org.onap.cps.ncmp.rest.controller -import org.onap.cps.TestUtils -import org.onap.cps.spi.model.ModuleReference - import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get @@ -35,6 +32,11 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE 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 import org.onap.cps.ncmp.api.NetworkCmProxyDataService import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired @@ -54,10 +56,16 @@ class NetworkCmProxyControllerSpec extends Specification { @SpringBean NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock() + @SpringBean + ModelMapper modelMapper = new ModelMapper() + + @SpringBean + JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) + @Value('${rest.api.ncmp-base-path}/v1') def ncmpBasePathV1 - def jsonString = '{"some-key":"some-value"}' + def requestBody = '{"some-key":"some-value"}' def 'Get Resource Data from pass-through operational.' () { given: 'resource data url' @@ -115,11 +123,11 @@ class NetworkCmProxyControllerSpec extends Specification { def response = mvc.perform( put(updateUrl) .contentType(MediaType.APPLICATION_JSON_VALUE) - .accept(MediaType.APPLICATION_JSON_VALUE).content(jsonString) + .accept(MediaType.APPLICATION_JSON_VALUE).content(requestBody) ).andReturn().response then: 'ncmp service method to update resource is called' 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle', - 'parent/child', UPDATE, jsonString, 'application/json;charset=UTF-8') + 'parent/child', UPDATE, requestBody, 'application/json;charset=UTF-8') and: 'the response status is OK' response.status == HttpStatus.OK.value() } @@ -192,11 +200,11 @@ class NetworkCmProxyControllerSpec extends Specification { def response = mvc.perform( patch(url) .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON).content(jsonString) + .accept(MediaType.APPLICATION_JSON).content(requestBody) ).andReturn().response then: 'ncmp service method to update resource is called' 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle', - 'parent/child', PATCH, jsonString, 'application/json;charset=UTF-8') + 'parent/child', PATCH, requestBody, 'application/json;charset=UTF-8') and: 'the response status is OK' response.status == HttpStatus.OK.value() } 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 f36a70693..7b3cd8914 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,9 +21,11 @@ package org.onap.cps.ncmp.rest.exceptions import groovy.json.JsonSlurper +import org.modelmapper.ModelMapper import org.onap.cps.ncmp.api.NetworkCmProxyDataService import org.onap.cps.ncmp.api.impl.exception.NcmpException import org.onap.cps.spi.exceptions.CpsException +import org.onap.cps.utils.JsonObjectMapper import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value @@ -44,6 +46,12 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification { @SpringBean NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock() + @SpringBean + ModelMapper modelMapper = Stub() + + @SpringBean + JsonObjectMapper jsonObjectMapper = Stub() + @Value('${rest.api.ncmp-base-path}') def basePath diff --git a/cps-ncmp-service/pom.xml b/cps-ncmp-service/pom.xml index b70279303..871bd14c5 100644 --- a/cps-ncmp-service/pom.xml +++ b/cps-ncmp-service/pom.xml @@ -1,7 +1,7 @@