From 05e7b8a207b1c641468e77d754fe1a5ae9e1a51e Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Tue, 18 Jan 2022 21:57:46 +0530 Subject: 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/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 ++++++----- 5 files changed, 45 insertions(+), 36 deletions(-) (limited to 'cps-rest') diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java index a55b1ba7a1..fc2818b6f6 100755 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2020-2021 Bell Canada. * Modifications Copyright (C) 2021 Pantheon.tech - * Modifications Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2021-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. @@ -22,7 +22,6 @@ package org.onap.cps.rest.controller; -import com.google.gson.Gson; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import javax.validation.ValidationException; @@ -32,6 +31,7 @@ import org.onap.cps.api.CpsDataService; import org.onap.cps.rest.api.CpsDataApi; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.utils.DataMapUtils; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -47,17 +47,18 @@ public class DataRestController implements CpsDataApi { private static final DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_FORMAT); private final CpsDataService cpsDataService; - private final Gson gson; + private final JsonObjectMapper jsonObjectMapper; @Override public ResponseEntity createNode(final String dataspaceName, final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) { + final String jsonDataAsString = jsonObjectMapper.asJsonString(jsonData); if (isRootXpath(parentNodeXpath)) { - cpsDataService.saveData(dataspaceName, anchorName, gson.toJson(jsonData), - toOffsetDateTime(observedTimestamp)); + cpsDataService.saveData(dataspaceName, anchorName, jsonDataAsString, + toOffsetDateTime(observedTimestamp)); } else { - cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), - toOffsetDateTime(observedTimestamp)); + cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, + jsonDataAsString, toOffsetDateTime(observedTimestamp)); } return new ResponseEntity<>(HttpStatus.CREATED); } @@ -73,8 +74,8 @@ public class DataRestController implements CpsDataApi { @Override public ResponseEntity addListElements(final String parentNodeXpath, final String dataspaceName, final String anchorName, final Object jsonData, final String observedTimestamp) { - cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), - toOffsetDateTime(observedTimestamp)); + cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, + jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.CREATED); } @@ -91,8 +92,8 @@ public class DataRestController implements CpsDataApi { @Override public ResponseEntity updateNodeLeaves(final String dataspaceName, final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) { - cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), - toOffsetDateTime(observedTimestamp)); + cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, + jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.OK); } @@ -100,8 +101,8 @@ public class DataRestController implements CpsDataApi { public ResponseEntity replaceNode(final String dataspaceName, final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) { cpsDataService - .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), - toOffsetDateTime(observedTimestamp)); + .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, + jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.OK); } @@ -109,8 +110,8 @@ public class DataRestController implements CpsDataApi { public ResponseEntity replaceListContent(final String parentNodeXpath, final String dataspaceName, final String anchorName, final Object jsonData, final String observedTimestamp) { - cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), - toOffsetDateTime(observedTimestamp)); + cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, + jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.OK); } diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java index 8aa65a0057..eb422dc69e 100644 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-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. @@ -20,18 +20,18 @@ package org.onap.cps.rest.controller; -import com.google.gson.Gson; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.onap.cps.api.CpsQueryService; import org.onap.cps.rest.api.CpsQueryApi; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; import org.onap.cps.utils.DataMapUtils; -import org.springframework.beans.factory.annotation.Autowired; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -39,10 +39,11 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("${rest.api.cps-base-path}") +@RequiredArgsConstructor public class QueryRestController implements CpsQueryApi { - @Autowired - private CpsQueryService cpsQueryService; + private final CpsQueryService cpsQueryService; + private final JsonObjectMapper jsonObjectMapper; @Override public ResponseEntity getNodesByDataspaceAndAnchorAndCpsPath(final String dataspaceName, @@ -55,6 +56,6 @@ public class QueryRestController implements CpsQueryApi { for (final DataNode dataNode : dataNodes) { dataNodeList.add(DataMapUtils.toDataMap(dataNode)); } - return new ResponseEntity<>(new Gson().toJson(dataNodeList), HttpStatus.OK); + return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataNodeList), HttpStatus.OK); } } diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy index fbb5636566..4d75848cf7 100755 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech * Modifications Copyright (C) 2021 Bell Canada. * ================================================================================ @@ -22,10 +22,12 @@ package org.onap.cps.rest.controller +import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.api.CpsDataService import org.onap.cps.spi.model.DataNode import org.onap.cps.spi.model.DataNodeBuilder import org.onap.cps.utils.DateTimeUtility +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 @@ -50,6 +52,9 @@ class DataRestControllerSpec extends Specification { @SpringBean CpsDataService mockCpsDataService = Mock() + @SpringBean + JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) + @Autowired MockMvc mvc diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy index 550dec972e..1e42a0060a 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021 Bell Canada. * Modifications Copyright (C) 2021 Pantheon.tech * ================================================================================ @@ -22,17 +22,12 @@ package org.onap.cps.rest.controller -import org.onap.cps.spi.model.DataNode - import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get -import com.google.gson.Gson -import org.modelmapper.ModelMapper -import org.onap.cps.api.CpsAdminService -import org.onap.cps.api.CpsDataService -import org.onap.cps.api.CpsModuleService +import com.fasterxml.jackson.databind.ObjectMapper +import org.onap.cps.utils.JsonObjectMapper import org.onap.cps.api.CpsQueryService import org.onap.cps.spi.model.DataNodeBuilder import org.spockframework.spring.SpringBean @@ -49,6 +44,9 @@ class QueryRestControllerSpec extends Specification { @SpringBean CpsQueryService mockCpsQueryService = Mock() + @SpringBean + JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) + @Autowired MockMvc mvc diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy index 658dcaa70e..a2eaa525e0 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech - * Modifications Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -38,6 +38,7 @@ import org.onap.cps.spi.exceptions.ModelValidationException import org.onap.cps.spi.exceptions.NotFoundInDataspaceException import org.onap.cps.spi.exceptions.SchemaSetInUseException import org.onap.cps.spi.exceptions.DataspaceInUseException +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 @@ -58,19 +59,22 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder class CpsRestExceptionHandlerSpec extends Specification { @SpringBean - CpsAdminService mockCpsAdminService = Mock() + CpsAdminService mockCpsAdminService = Stub() @SpringBean - CpsModuleService mockCpsModuleService = Mock() + CpsModuleService mockCpsModuleService = Stub() @SpringBean - CpsDataService mockCpsDataService = Mock() + CpsDataService mockCpsDataService = Stub() @SpringBean - CpsQueryService mockCpsQueryService = Mock() + CpsQueryService mockCpsQueryService = Stub() @SpringBean - ModelMapper modelMapper = Mock() + ModelMapper modelMapper = Stub() + + @SpringBean + JsonObjectMapper jsonObjectMapper = Stub() @Autowired MockMvc mvc -- cgit 1.2.3-korg