diff options
author | 2020-03-19 11:59:02 +0000 | |
---|---|---|
committer | 2020-03-19 11:59:02 +0000 | |
commit | 3e90fc1056791e629916a29b12b7b14bcb1e2f15 (patch) | |
tree | 7ee7eadcdc665396f5456537a17f01b064841b06 /vnfmarket-be/vnf-sdk-marketplace/src/main | |
parent | 2b913c12ea8ff1108782132f540333219537c837 (diff) | |
parent | 427fb7539233ece7759e27e04fa3b11d17b66ea7 (diff) |
Merge "Migrate to gson"
Diffstat (limited to 'vnfmarket-be/vnf-sdk-marketplace/src/main')
10 files changed, 203 insertions, 202 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java index ce6ae72e..7f5ad88e 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java @@ -30,13 +30,10 @@ import java.util.zip.ZipFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonGenerationException; -import com.fasterxml.jackson.core.JsonParseException; -/** note jackson has security vulnerabilities */ -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - +import com.google.gson.Gson; +import com.google.gson.stream.JsonReader; +import java.io.FileWriter; +import java.io.FileReader; public final class FileUtil { @@ -45,6 +42,7 @@ public final class FileUtil { private static final int BUFFER_SIZE = 2 * 1024 * 1024; private static final int MAX_PACKAGE_SIZE = 50 * 1024 * 1024; + private static Gson gson = new Gson(); private FileUtil() { //Empty constructor @@ -150,16 +148,11 @@ public final class FileUtil { deleteFile(fileAbsPath); } - ObjectMapper mapper = new ObjectMapper(); - try { - mapper.writeValue(new File(fileAbsPath), obj); + try(FileWriter writer = new FileWriter(new File(fileAbsPath))) { + gson.toJson(obj, writer); bResult = true; - } catch (JsonGenerationException e) { - logger.info("JsonGenerationException Exception: writeJsonDatatoFile-->" + fileAbsPath, e); - } catch (JsonMappingException e) { - logger.info("JsonMappingException Exception: writeJsonDatatoFile-->" + fileAbsPath, e); - } catch (IOException e) { - logger.info("IOException Exception: writeJsonDatatoFile-->" + fileAbsPath, e); + } catch (Exception e) { //NOSONAR + logger.info("Exception: writeJsonDatatoFile-->" + fileAbsPath, e); } return bResult; } @@ -173,15 +166,16 @@ public final class FileUtil { logger.info("read JsonData from file : {}" , fileAbsPath); T obj = null; - ObjectMapper mapper = new ObjectMapper(); - mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - try { - obj = mapper.readValue(new File(fileAbsPath), clazz); - } catch (JsonParseException e1) { - logger.info("JsonParseException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1); - } catch (JsonMappingException e1) { - logger.info("JsonMappingException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1); - } catch (IOException e1) { + /* + Gson will ignore the unknown fields and simply match the fields that it's able to. + ref: https://www.baeldung.com/gson-deserialization-guide + + By default, Gson just ignores extra JSON elements that do not have matching Java fields. + ref: https://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html + */ + try(JsonReader jsonReader = new JsonReader(new FileReader(fileAbsPath))) { + obj = gson.fromJson(jsonReader, clazz); + } catch (Exception e1) { //NOSONAR logger.info("IOException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1); } return obj; diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutor.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutor.java index 8d467de2..bf6d2879 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutor.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutor.java @@ -32,16 +32,14 @@ import org.onap.vnfsdk.marketplace.rest.RestfulClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonParseException; -/** note jackson has security vulnerabilities. use with care */ -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; /** CALL Flow: onBoardingHandler --> LifecycleTestHook--> LifecycleTestExecutor */ public class LifecycleTestExceutor { private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class); + private static Gson gson = new Gson(); public static final String CATALOUGE_UPLOAD_URL_IN = "{0}:{1}/onapapi/catalog/v1/csars"; private LifecycleTestExceutor() { @@ -165,18 +163,18 @@ public class LifecycleTestExceutor { * @return empty(failure), or csarId(success) */ private static String getCsarIdValue(String strJsonData) { - ObjectMapper mapper = new ObjectMapper(); - mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + /* + Gson will ignore the unknown fields and simply match the fields that it's able to. + ref: https://www.baeldung.com/gson-deserialization-guide + By default, Gson just ignores extra JSON elements that do not have matching Java fields. + ref: https://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html + */ Map<String, String> dataMap = null; try { - dataMap = mapper.readValue(strJsonData, Map.class); - } catch (JsonParseException e) { - logger.error("JsonParseException:Failed to upload package to catalouge:", e); - } catch (JsonMappingException e) { - logger.error("JsonMappingException:Failed to upload package to catalouge:", e); - } catch (IOException e) { - logger.error("IOException:Failed to upload package to catalouge:", e); + dataMap = gson.fromJson(strJsonData, new TypeToken<Map<String,String>>(){}.getType()); + } catch (Exception e) { //NOSONAR + logger.error("Exception:Failed to upload package to catalouge:", e); } try { if (null != dataMap) { diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java index daa58031..9f9974e0 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java @@ -39,11 +39,14 @@ import org.onap.vnfsdk.marketplace.model.parser.EnumPackageFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; import com.google.gson.internal.LinkedTreeMap; public class PackageWrapperUtil { private static final Logger LOG = LoggerFactory.getLogger(PackageWrapperUtil.class); + private static Gson gson = new Gson(); private PackageWrapperUtil() { } @@ -150,7 +153,7 @@ public class PackageWrapperUtil { packageMeta.setCreateTime(currentTime); packageMeta.setModifyTime(currentTime); if(null != details) { - LinkedTreeMap<String, String> csarDetails = ToolUtil.fromJson(details, LinkedTreeMap.class); + LinkedTreeMap<String, String> csarDetails = gson.fromJson(details, LinkedTreeMap.class); packageMeta.setDetails(csarDetails.get("details")); packageMeta.setShortDesc(csarDetails.get("shortDesc")); packageMeta.setRemarks(csarDetails.get("remarks")); diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPModelBase.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPModelBase.java index 218d0ad8..c747c65f 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPModelBase.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPModelBase.java @@ -18,12 +18,11 @@ package org.onap.vtp; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; public class VTPModelBase { public static final Logger logger = LoggerFactory.getLogger(VTPModelBase.class); + private static Gson gson = new Gson(); public String toJsonString() { return toJsonString(this); @@ -31,12 +30,16 @@ public class VTPModelBase { public static String toJsonString(Object obj) { try { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.setSerializationInclusion(Include.NON_NULL); - objectMapper.setSerializationInclusion(Include.NON_EMPTY); - return objectMapper.writeValueAsString(obj); - } catch (JsonProcessingException e) { - logger.error("JsonProcessingException occurs ",e); + /* + The default behaviour implemented in Gson is that null object fields are ignored. + Means Gson object does not serialize fields with null values to JSON. + If a field in a Java object is null, Gson excludes it. + ref: http://tutorialtous.com/gson/serializingNullFields.php + ref: https://howtodoinjava.com/gson/serialize-null-values/ + */ + return gson.toJson(obj); + } catch (Exception e) { //NOSONAR + logger.error("Exception occurs ",e); return "{}"; } } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPResource.java index f6139066..254cc6ad 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPResource.java @@ -35,12 +35,15 @@ import org.open.infc.grpc.client.OpenRemoteCli; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; public class VTPResource { protected static final Logger LOG = LoggerFactory.getLogger(VTPResource.class); + private static Gson gson = new Gson(); protected static String VTP_TEST_CENTER_IP; // NOSONAR protected static int VTP_TEST_CENTER_PORT; // NOSONAR @@ -100,24 +103,23 @@ public class VTPResource { return VTP_ARTIFACT_STORE; } - protected JsonNode makeRpcAndGetJson(List<String> args) throws VTPException, IOException { + protected JsonElement makeRpcAndGetJson(List<String> args) throws VTPException, IOException { return this.makeRpcAndGetJson(args, VTP_EXECUTION_GRPC_TIMEOUT); } - protected JsonNode makeRpcAndGetJson(List<String> args, int timeout) throws VTPException, IOException { + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPException, IOException { Result result = this.makeRpc(args, timeout); - ObjectMapper mapper = new ObjectMapper(); - return mapper.readTree(result.getOutput()); + JsonParser jsonParser = new JsonParser(); + return jsonParser.parse(result.getOutput()); } - protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonNode argsJsonNode) throws VTPException { + protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPException { return this.makeRpc(scenario, requestId, profile, testCase, argsJsonNode, VTP_EXECUTION_GRPC_TIMEOUT); } - protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonNode argsJsonNode, int timeout) throws VTPException { + protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode, int timeout) throws VTPException { Output output = null; - ObjectMapper mapper = new ObjectMapper(); - Map <String, String> args = mapper.convertValue(argsJsonNode, Map.class); + Map <String, String> args = gson.fromJson(argsJsonNode, new TypeToken<Map<String,String>>(){}.getType()); try { output = new OpenRemoteCli( VTP_TEST_CENTER_IP, diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/error/VTPExceptionMapper.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/error/VTPExceptionMapper.java index 93f08a4f..0287f339 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/error/VTPExceptionMapper.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/error/VTPExceptionMapper.java @@ -26,8 +26,7 @@ import org.onap.vtp.error.VTPError.VTPException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.node.JsonNodeFactory; -import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.gson.JsonObject; @Provider @Singleton @@ -41,9 +40,9 @@ public class VTPExceptionMapper implements ExceptionMapper<Exception> { VTPException ex = (VTPException) e; return Response.status(ex.getVTPError().getHttpStatus()).entity(ex.getVTPError().toString()).build(); } else { - ObjectNode node = JsonNodeFactory.instance.objectNode(); - node.put("message", e.getMessage()); - node.put("code", "UNKNOWN"); + JsonObject node = new JsonObject(); + node.addProperty("message", e.getMessage()); + node.addProperty("code", "UNKNOWN"); return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(node.toString()).build(); } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java index 9eb2a7b7..77888c27 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java @@ -56,12 +56,12 @@ import org.onap.vtp.execution.model.VTPTestExecution.VTPTestExecutionList; import org.open.infc.grpc.Output; import org.open.infc.grpc.Result; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.JsonNodeFactory; -import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonArray; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -85,6 +85,7 @@ public class VTPExecutionResource extends VTPResource{ private static final String INPUT = "input"; private static final String ERROR = "error"; private static final String FILE = "file://"; + private static Gson gson = new Gson(); public VTPTestExecutionList executeHandler(VTPTestExecutionList executions, String requestId) throws VTPException { if (requestId == null) { @@ -117,20 +118,20 @@ public class VTPExecutionResource extends VTPResource{ // set the results from what is available in the output independent of status. // tests can fail but still produce results. - ObjectMapper mapper = new ObjectMapper(); + JsonParser jsonParser = new JsonParser(); Map<String,String> m = output.getAttrsMap(); if ((m.containsKey(ERROR)) && (!StringUtils.equals(m.get(ERROR), "{}"))) { try { - execution.setResults(mapper.readTree(m.get(ERROR))); - } catch (IOException e) { - LOG.error("IOException occurs",e); + execution.setResults(jsonParser.parse(m.get(ERROR))); + } catch (Exception e) { //NOSONAR + LOG.error("Exception occurs",e); } } else if (m.containsKey("results")) { try { - execution.setResults(mapper.readTree(m.get("results"))); - } catch (IOException e) { - LOG.error("IOException occurs",e); + execution.setResults(jsonParser.parse(m.get("results"))); + } catch (Exception e) { //NOSONAR + LOG.error("Exception occurs",e); } } } @@ -207,9 +208,9 @@ public class VTPExecutionResource extends VTPResource{ try { executions.setExecutions( - new ObjectMapper().readValue(executionsJson, new TypeReference<List<VTPTestExecution>>(){})); - } catch (IOException e) { - LOG.error("IOException occurs",e); + gson.fromJson(executionsJson, new TypeToken<List<VTPTestExecution>>(){}.getType())); + } catch (Exception e) { //NOSONAR + LOG.error("Exception occurs",e); } executions = this.executeHandler(executions, requestId); @@ -268,49 +269,48 @@ public class VTPExecutionResource extends VTPResource{ args.add(testCaseName); } - JsonNode results = this.makeRpcAndGetJson(args); + JsonElement results = this.makeRpcAndGetJson(args); VTPTestExecutionList list = new VTPTestExecutionList(); - if (results != null && results.isArray()) { - ArrayNode resultsArray = (ArrayNode)results; - if (resultsArray.size() >= 0) { - for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) { - JsonNode n = it.next(); - if (n.elements().hasNext()) { + if (results != null && results.isJsonArray() && results.getAsJsonArray().size() > 0) { + JsonArray resultsArray = results.getAsJsonArray(); + for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + if (n.entrySet().iterator().hasNext()) { VTPTestExecution exec = new VTPTestExecution(); if (n.get(START_TIME) != null) - exec.setStartTime(n.get(START_TIME).asText()); + exec.setStartTime(n.get(START_TIME).getAsString()); if (n.get(END_TIME) != null) - exec.setEndTime(n.get(END_TIME).asText()); + exec.setEndTime(n.get(END_TIME).getAsString()); if (n.get(EXECUTION_ID) != null) - exec.setExecutionId(n.get(EXECUTION_ID).asText()); + exec.setExecutionId(n.get(EXECUTION_ID).getAsString()); if (n.get(REQUEST_ID) != null) - exec.setRequestId(n.get(REQUEST_ID).asText()); + exec.setRequestId(n.get(REQUEST_ID).getAsString()); if (n.get(PRODUCT) != null) - exec.setScenario(n.get(PRODUCT).asText()); + exec.setScenario(n.get(PRODUCT).getAsString()); if (n.get(SERVICE) != null) - exec.setTestSuiteName(n.get(SERVICE).asText()); + exec.setTestSuiteName(n.get(SERVICE).getAsString()); if (n.get(COMMAND) != null) - exec.setTestCaseName(n.get(COMMAND).asText()); + exec.setTestCaseName(n.get(COMMAND).getAsString()); if (n.get(PROFILE) != null) - exec.setProfile(n.get(PROFILE).asText()); + exec.setProfile(n.get(PROFILE).getAsString()); if (n.get(STATUS) != null) - exec.setStatus(n.get(STATUS).asText()); + exec.setStatus(n.get(STATUS).getAsString()); list.getExecutions().add(exec); } } - } } return list; @@ -346,54 +346,57 @@ public class VTPExecutionResource extends VTPResource{ })); - JsonNode result = this.makeRpcAndGetJson(args); + JsonElement result = this.makeRpcAndGetJson(args); VTPTestExecution exec = new VTPTestExecution(); - if (result != null && result.elements().hasNext()) { - if (result.get(START_TIME) != null) - exec.setStartTime(result.get(START_TIME).asText()); - - if (result.get(END_TIME) != null) - exec.setEndTime(result.get(END_TIME).asText()); - - if (result.get(EXECUTION_ID) != null) - exec.setExecutionId(result.get(EXECUTION_ID).asText()); - if (result.get(REQUEST_ID) != null) - exec.setExecutionId(result.get(REQUEST_ID).asText()); - - if (result.get(PRODUCT) != null) - exec.setScenario(result.get(PRODUCT).asText()); - if (result.get(SERVICE) != null) - exec.setTestSuiteName(result.get(SERVICE).asText()); - if (result.get(COMMAND) != null) - exec.setTestCaseName(result.get(COMMAND).asText()); - if (result.get(PROFILE) != null) - exec.setExecutionId(result.get(PROFILE).asText()); - if (result.get(STATUS) != null) - exec.setStatus(result.get(STATUS).asText()); - if (result.get(INPUT) != null) { - exec.setParameters(result.get(INPUT)); - } - if (result.get(OUTPUT) != null) { - ObjectMapper mapper = new ObjectMapper(); - JsonNode resultJson = null; - try { - resultJson = mapper.readTree(result.get(OUTPUT).asText()); + if (result != null && result.isJsonObject()) { + + JsonObject resultObj = result.getAsJsonObject(); + if (resultObj.entrySet().iterator().hasNext()){ + if (resultObj.get(START_TIME) != null) + exec.setStartTime(resultObj.get(START_TIME).getAsString()); + + if (resultObj.get(END_TIME) != null) + exec.setEndTime(resultObj.get(END_TIME).getAsString()); + + if (resultObj.get(EXECUTION_ID) != null) + exec.setExecutionId(resultObj.get(EXECUTION_ID).getAsString()); + if (resultObj.get(REQUEST_ID) != null) + exec.setExecutionId(resultObj.get(REQUEST_ID).getAsString()); + + if (resultObj.get(PRODUCT) != null) + exec.setScenario(resultObj.get(PRODUCT).getAsString()); + if (resultObj.get(SERVICE) != null) + exec.setTestSuiteName(resultObj.get(SERVICE).getAsString()); + if (resultObj.get(COMMAND) != null) + exec.setTestCaseName(resultObj.get(COMMAND).getAsString()); + if (resultObj.get(PROFILE) != null) + exec.setExecutionId(resultObj.get(PROFILE).getAsString()); + if (resultObj.get(STATUS) != null) + exec.setStatus(resultObj.get(STATUS).getAsString()); + if (resultObj.get(INPUT) != null ) { + exec.setParameters(resultObj.get(INPUT)); + } + if (resultObj.get(OUTPUT) != null) { + JsonParser jsonParser = new JsonParser(); + JsonElement resultJson = null; + try { + resultJson = jsonParser.parse(resultObj.get(OUTPUT).getAsString()); //workarround, sometimes its null. - if (resultJson == null) { - resultJson = mapper.readTree(result.get(OUTPUT).toString()); + if (resultJson == null || resultJson.isJsonNull()) { + resultJson = jsonParser.parse(resultObj.get(OUTPUT).toString()); + } + } catch (Exception e) { + LOG.error("Exception occurs", e); + JsonObject node = new JsonObject(); + node.addProperty(ERROR, resultObj.get(OUTPUT).toString()); + resultJson = node; } - } catch (Exception e) { - LOG.error("Exception occurs", e); - ObjectNode node = JsonNodeFactory.instance.objectNode(); - node.put(ERROR, result.get(OUTPUT).asText()); - resultJson = node; + exec.setResults(resultJson); } - - exec.setResults(resultJson); } } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/model/VTPTestExecution.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/model/VTPTestExecution.java index e56333be..b5dc7399 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/model/VTPTestExecution.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/model/VTPTestExecution.java @@ -22,9 +22,8 @@ import java.util.List; import org.onap.vtp.VTPModelBase; -import com.fasterxml.jackson.databind.JsonNode; - -import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; public class VTPTestExecution extends VTPModelBase{ private String scenario; @@ -33,8 +32,8 @@ public class VTPTestExecution extends VTPModelBase{ private String executionId; private String requestId; private String profile; - private JsonNode parameters = JsonNodeFactory.instance.objectNode(); - private JsonNode results = JsonNodeFactory.instance.objectNode(); + private JsonElement parameters = new JsonObject(); + private JsonElement results = new JsonObject(); public enum Status { IN_PROGRESS, COMPLETED, FAILED; } @@ -76,10 +75,10 @@ public class VTPTestExecution extends VTPModelBase{ return this; } - public JsonNode getResults() { + public JsonElement getResults() { return results; } - public VTPTestExecution setResults(JsonNode results) { + public VTPTestExecution setResults(JsonElement results) { this.results = results; return this; } @@ -125,10 +124,10 @@ public class VTPTestExecution extends VTPModelBase{ this.requestId = requestId; } - public JsonNode getParameters() { + public JsonElement getParameters() { return parameters; } - public void setParameters(JsonNode parameters) { + public void setParameters(JsonElement parameters) { this.parameters = parameters; } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java index 2329d1bc..47a7696d 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java @@ -43,8 +43,9 @@ import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseOutput; import org.onap.vtp.scenario.model.VTPTestScenario.VTPTestScenarioList; import org.onap.vtp.scenario.model.VTPTestSuite.VTPTestSuiteList; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -63,7 +64,7 @@ public class VTPScenarioResource extends VTPResource{ "--product", "open-cli", "product-list", "--format", "json" })); - JsonNode results = null; + JsonElement results = null; try { results = this.makeRpcAndGetJson(args); } catch (IOException e) { @@ -72,21 +73,20 @@ public class VTPScenarioResource extends VTPResource{ VTPTestScenarioList list = new VTPTestScenarioList(); - if (results != null && results.isArray()) { - ArrayNode resultsArray = (ArrayNode)results; - if (resultsArray.size() >= 0) { - for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) { - JsonNode n = it.next(); - if (n.elements().hasNext()) { - String name = n.get("product").asText(); + if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { + JsonArray resultsArray = results.getAsJsonArray(); + for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + if (n.entrySet().iterator().hasNext()) { + String name = n.get("product").getAsString(); if ("open-cli".equalsIgnoreCase(name)) continue; list.getScenarios().add(new VTPTestScenario().setName(name).setDescription( - n.get(DESCRIPTION).asText())); + n.get(DESCRIPTION).getAsString())); } - } } } @@ -112,7 +112,7 @@ public class VTPScenarioResource extends VTPResource{ "--product", "open-cli", "service-list", "--product", scenario, "--format", "json" })); - JsonNode results = null; + JsonElement results = null; try { results = this.makeRpcAndGetJson(args); } catch (IOException e) { @@ -121,17 +121,16 @@ public class VTPScenarioResource extends VTPResource{ VTPTestSuiteList list = new VTPTestSuiteList(); - if (results != null && results.isArray()) { - ArrayNode resultsArray = (ArrayNode)results; - if (resultsArray.size() >= 0) { - for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) { - JsonNode n = it.next(); - if (n.elements().hasNext()) { - list.getSuites().add(new VTPTestSuite().setName(n.get("service").asText()).setDescription( - n.get(DESCRIPTION).asText())); + if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { + JsonArray resultsArray = results.getAsJsonArray(); + for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + if (n.entrySet().iterator().hasNext()) { + list.getSuites().add(new VTPTestSuite().setName(n.get("service").getAsString()).setDescription( + n.get(DESCRIPTION).getAsString())); } } - } } return list; @@ -162,7 +161,7 @@ public class VTPScenarioResource extends VTPResource{ args.add(testSuiteName); } - JsonNode results = null; + JsonElement results = null; try { results = this.makeRpcAndGetJson(args); } catch (IOException e) { @@ -171,18 +170,17 @@ public class VTPScenarioResource extends VTPResource{ VTPTestCaseList list = new VTPTestCaseList(); - if (results != null && results.isArray()) { - ArrayNode resultsArray = (ArrayNode)results; - if (resultsArray.size() >= 0) { - for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) { - JsonNode n = it.next(); - if (n.elements().hasNext()) + if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { + JsonArray resultsArray = results.getAsJsonArray(); + for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + if (n.entrySet().iterator().hasNext()) list.getTestCases().add( new VTPTestCase().setTestCaseName( - n.get("command").asText()).setTestSuiteName( - n.get("service").asText())); + n.get("command").getAsString()).setTestSuiteName( + n.get("service").getAsString())); } - } } return list; @@ -209,34 +207,35 @@ public class VTPScenarioResource extends VTPResource{ args.addAll(Arrays.asList(new String[] { "--product", "open-cli", "schema-show", "--product", scenario, "--service", testSuiteName, "--command", testCaseName , "--format", "json" })); - JsonNode results = null; + JsonElement results = null; try { results = this.makeRpcAndGetJson(args); } catch (IOException e) { LOG.error("IOException occurs",e); } - JsonNode schema = results.get("schema"); + JsonObject schema = results.getAsJsonObject().getAsJsonObject("schema"); VTPTestCase tc = new VTPTestCase(); - tc.setTestCaseName(schema.get("name").asText()); - tc.setDescription(schema.get(DESCRIPTION).asText()); - tc.setTestSuiteName(schema.get("service").asText()); - tc.setAuthor(schema.get("author").asText()); - JsonNode inputsJson = schema.get("inputs"); - if (inputsJson != null && inputsJson.isArray()) { - for (final JsonNode inputJson: inputsJson) { + tc.setTestCaseName(schema.get("name").getAsString()); + tc.setDescription(schema.get(DESCRIPTION).getAsString()); + tc.setTestSuiteName(schema.get("service").getAsString()); + tc.setAuthor(schema.get("author").getAsString()); + JsonElement inputsJson = schema.get("inputs"); + if (inputsJson != null && inputsJson.isJsonArray()) { + for (final JsonElement jsonElement: inputsJson.getAsJsonArray()) { + JsonObject inputJson = jsonElement.getAsJsonObject(); VTPTestCaseInput input = new VTPTestCaseInput(); - input.setName(inputJson.get("name").asText()); - input.setDescription(inputJson.get(DESCRIPTION).asText()); - input.setType(inputJson.get("type").asText()); + input.setName(inputJson.get("name").getAsString()); + input.setDescription(inputJson.get(DESCRIPTION).getAsString()); + input.setType(inputJson.get("type").getAsString()); if (inputJson.get("is_optional") != null) - input.setIsOptional(inputJson.get("is_optional").asBoolean()); + input.setIsOptional(inputJson.get("is_optional").getAsBoolean()); if (inputJson.get("default_value") != null) - input.setDefaultValue(inputJson.get("default_value").asText()); + input.setDefaultValue(inputJson.get("default_value").getAsString()); if (inputJson.get("metadata") != null) input.setMetadata(inputJson.get("metadata")); @@ -245,13 +244,14 @@ public class VTPScenarioResource extends VTPResource{ } } - JsonNode outputsJson = schema.get("outputs"); - if (outputsJson != null && outputsJson.isArray()) { - for (final JsonNode outputJson: outputsJson) { + JsonElement outputsJson = schema.get("outputs"); + if (outputsJson != null && outputsJson.isJsonArray() && outputsJson.getAsJsonArray().size()>0) { + for (final JsonElement jsonElement: outputsJson.getAsJsonArray()) { + JsonObject outputJson = jsonElement.getAsJsonObject(); VTPTestCaseOutput output = new VTPTestCaseOutput(); - output.setName(outputJson.get("name").asText()); - output.setDescription(outputJson.get(DESCRIPTION).asText()); - output.setType(outputJson.get("type").asText()); + output.setName(outputJson.get("name").getAsString()); + output.setDescription(outputJson.get(DESCRIPTION).getAsString()); + output.setType(outputJson.get("type").getAsString()); tc.getOutputs().add(output); } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/model/VTPTestCase.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/model/VTPTestCase.java index 8e7d76c2..e3647ad8 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/model/VTPTestCase.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/model/VTPTestCase.java @@ -21,7 +21,7 @@ import java.util.List; import org.onap.vtp.VTPModelBase; -import com.fasterxml.jackson.databind.JsonNode; +import com.google.gson.JsonElement; public class VTPTestCase extends VTPModelBase{ private String scenario; @@ -104,7 +104,7 @@ public class VTPTestCase extends VTPModelBase{ private String type; private String defaultValue; private Boolean isOptional; - private JsonNode metadata; + private JsonElement metadata; public String getName() { return name; @@ -136,10 +136,10 @@ public class VTPTestCase extends VTPModelBase{ public void setIsOptional(Boolean isOptional) { this.isOptional = isOptional; } - public JsonNode getMetadata() { + public JsonElement getMetadata() { return metadata; } - public void setMetadata(JsonNode metadata) { + public void setMetadata(JsonElement metadata) { this.metadata = metadata; } } |