diff options
author | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-02-28 05:47:34 +0000 |
---|---|---|
committer | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-02-29 11:26:08 +0000 |
commit | 132177fb71c08f157fb42037f9dd26ceb2d0ba43 (patch) | |
tree | f47d3c4d950e98d5bcf104aa8952f39a84e3ba6d /validate | |
parent | 4f1d279e42a3326cdc97e1eacaf0fdb58ee944e7 (diff) |
Migrate to gson
Issue-ID: CLI-247
Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com>
Change-Id: If66134db5503382fb45210ece883402fc130ac93
Diffstat (limited to 'validate')
5 files changed, 43 insertions, 27 deletions
diff --git a/validate/sample-mock-generator/pom.xml b/validate/sample-mock-generator/pom.xml index 63bf1e47..a4bee09c 100644 --- a/validate/sample-mock-generator/pom.xml +++ b/validate/sample-mock-generator/pom.xml @@ -60,9 +60,9 @@ <scope>test</scope> </dependency> <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <version>2.9.4</version> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.2</version> </dependency> </dependencies> <build><plugins> 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<String, String> 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); } } } diff --git a/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java b/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java index 181ae515..e94de60a 100644 --- a/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java +++ b/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java @@ -17,6 +17,7 @@ package org.onap.cli.validation; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.io.File; @@ -48,6 +49,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + public class OnapValidationTest { public static final String SAMPLE_VERSION = "open_cli_sample_version"; @@ -64,6 +68,7 @@ public class OnapValidationTest { OnapCli cli = new OnapCli(); private static Logger LOG = LoggerFactory.getLogger(OnapValidationTest.class); + private static Gson gson = new GsonBuilder().serializeNulls().create(); private void handle(String[] args) { cli.resetExitCode(); @@ -261,4 +266,11 @@ public class OnapValidationTest { onapCli.handle(); assertEquals(OnapCliConstants.EXIT_SUCCESS, onapCli.getExitCode()); } + @Test + public void testOnapCommandSchemaInfoForUnknownFields(){ + OnapCommandSchemaInfo ocsi = new OnapCommandSchemaInfo(); + String testExp = "{\"schemaName\":\"testSchema\",\"schemaURI\":\"testUri\",\"unknownField\":\"unknown\"}"; + ocsi= gson.fromJson(testExp,OnapCommandSchemaInfo.class); + assertNotNull(ocsi); + } } |