From 132177fb71c08f157fb42037f9dd26ceb2d0ba43 Mon Sep 17 00:00:00 2001 From: "priyanka.akhade" Date: Fri, 28 Feb 2020 05:47:34 +0000 Subject: Migrate to gson Issue-ID: CLI-247 Signed-off-by: priyanka.akhade Change-Id: If66134db5503382fb45210ece883402fc130ac93 --- .../org/onap/cli/http/mock/MockJsonGenerator.java | 21 ++++++++++++--------- .../java/org/onap/cli/http/mock/MockRequest.java | 16 ++++++++-------- .../java/org/onap/cli/http/mock/MockResponse.java | 15 ++++++++------- 3 files changed, 28 insertions(+), 24 deletions(-) (limited to 'validate/sample-mock-generator/src') diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java index 4b2e116c..c0258ae5 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java @@ -19,21 +19,24 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.FileWriter; + public class MockJsonGenerator { - public static void generateMocking(MockRequest mockRequest, MockResponse mockResponse, - String jsonFilePath) throws IOException { + private static Gson gson = new GsonBuilder().serializeNulls().create(); + + public static void generateMocking(MockRequest mockRequest, MockResponse mockResponse, String jsonFilePath) throws IOException { MockObject mockObject = new MockObject(); mockObject.setRequest(mockRequest); mockObject.setResponse(mockResponse); - ObjectMapper mapper = new ObjectMapper(); - ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); - writer.writeValue(new File(jsonFilePath), - Arrays.asList(mockObject)); + try(FileWriter writer = new FileWriter(jsonFilePath)){ + gson.toJson(Arrays.asList(mockObject), writer); + }catch (Exception e){ // NOSONAR + // + } } } diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java index d1d8b08d..2ad9b85c 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java @@ -20,15 +20,16 @@ import java.io.IOException; import java.net.URL; import java.util.Map; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; public class MockRequest { private String method; private String uri; private Map headers; - private JsonNode json; + private JsonElement json; + private static Gson gson = new GsonBuilder().serializeNulls().create(); public String getMethod() { return method; @@ -56,17 +57,16 @@ public class MockRequest { this.headers = headers; } - public JsonNode getJson() { + public JsonElement getJson() { return json; } public void setJson(String json) throws IOException { if (!json.isEmpty()) { try { - ObjectMapper objectMapper = new ObjectMapper(); - this.json = objectMapper.readTree(json); + this.json = gson.fromJson(json,JsonElement.class); } catch (Exception e) { - this.json = new ObjectMapper().readTree("{}"); + this.json = gson.fromJson("{}",JsonElement.class); } } diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java index e49e4d41..9184d3ad 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java @@ -18,12 +18,14 @@ package org.onap.cli.http.mock; import java.io.IOException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; public class MockResponse { private int status; - private JsonNode json; + private JsonElement json; + private static Gson gson = new GsonBuilder().serializeNulls().create(); public int getStatus() { return status; @@ -33,17 +35,16 @@ public class MockResponse { this.status = status; } - public JsonNode getJson() { + public JsonElement getJson() { return json; } public void setJson(String json) throws IOException { if (json != null && !json.isEmpty()) { try { - ObjectMapper objectMapper = new ObjectMapper(); - this.json = objectMapper.readTree(json); + this.json = gson.fromJson(json,JsonElement.class); } catch (Exception e) { - this.json = new ObjectMapper().readTree("{}"); + this.json = gson.fromJson("{}", JsonElement.class); } } } -- cgit 1.2.3-korg