From ede42ea3e267d321713cff1daf44d8627ada933d Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Mon, 26 Jun 2023 11:15:57 +0100 Subject: NCMP : Handle non-existing and non-ready cm handles - Modified data operation schema to contains cm handle as steing and response content field name as result. - Added a new common cloud event builder for NCMP to create an event. - Added data operation event creater that uses cloud event builder to create a cloud event. - Introduced a new method onto json object mapper to convert json object to bytes. - Modified EventDateTimeFormatter and added a new method to convert date timestamp into offsetdateTime. - Added a new code into ResourceRequestUtil to identify non-ready cm handle and non-existing cm handle and later publish it as cloud event to tha client given topic. - Introduced CpsApplicationContext to get spring mannaged bean into non spring managed java object. Issue-ID: CPS-1583, CPS-1614 Signed-off-by: sourabh_sourabh Change-Id: I24a39d2cb2c54dea25cd2f17e7748e21cd83a088 Signed-off-by: sourabh_sourabh --- .../main/java/org/onap/cps/utils/JsonObjectMapper.java | 16 ++++++++++++++++ .../org/onap/cps/utils/JsonObjectMapperSpec.groovy | 8 +++++--- 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'cps-service/src') diff --git a/cps-service/src/main/java/org/onap/cps/utils/JsonObjectMapper.java b/cps-service/src/main/java/org/onap/cps/utils/JsonObjectMapper.java index 338a841a7..60a6e16fe 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/JsonObjectMapper.java +++ b/cps-service/src/main/java/org/onap/cps/utils/JsonObjectMapper.java @@ -89,6 +89,22 @@ public class JsonObjectMapper { } } + /** + * Serializing generic json object to bytes using Jackson. + * + * @param jsonObject any json object value + * @return the generated JSON as a byte array. + */ + public byte[] asJsonBytes(final Object jsonObject) { + try { + return objectMapper.writeValueAsBytes(jsonObject); + } catch (final JsonProcessingException jsonProcessingException) { + log.error("Parsing error occurred while converting JSON object to bytes."); + throw new DataValidationException("Parsing error occurred while converting given JSON object to bytes.", + jsonProcessingException.getMessage()); + } + } + /** * Deserialize JSON content from given JSON content String to JsonNode. * diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/JsonObjectMapperSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/JsonObjectMapperSpec.groovy index b70c43795..2332282e2 100644 --- a/cps-service/src/test/groovy/org/onap/cps/utils/JsonObjectMapperSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/utils/JsonObjectMapperSpec.groovy @@ -33,15 +33,17 @@ class JsonObjectMapperSpec extends Specification { def spiedObjectMapper = Spy(ObjectMapper) def jsonObjectMapper = new JsonObjectMapper(spiedObjectMapper) - def 'Map a structured object to json String.'() { + def 'Map a structured object to json #type.'() { given: 'an object model' def object = spiedObjectMapper.readValue(TestUtils.getResourceFileContent('bookstore.json'), Object) when: 'the object is mapped to string' - def content = jsonObjectMapper.asJsonString(object); + def content = type == 'String' ? jsonObjectMapper.asJsonString(object) : jsonObjectMapper.asJsonBytes(object) then: 'the result is a valid json string (can be parsed)' - def contentMap = new JsonSlurper().parseText(content) + def contentMap = new JsonSlurper().parseText(new String(content)) and: 'the parsed content is as expected' assert contentMap.'test:bookstore'.'bookstore-name' == 'Chapters/Easons' + where: 'the following data stores are used' + type << ['String', 'bytes'] } def 'Map a structured object to json String error.'() { -- cgit 1.2.3-korg