diff options
author | Kanagaraj Manickam <kanagaraj.manickam@huawei.com> | 2020-03-19 11:59:02 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-03-19 11:59:02 +0000 |
commit | 3e90fc1056791e629916a29b12b7b14bcb1e2f15 (patch) | |
tree | 7ee7eadcdc665396f5456537a17f01b064841b06 | |
parent | 2b913c12ea8ff1108782132f540333219537c837 (diff) | |
parent | 427fb7539233ece7759e27e04fa3b11d17b66ea7 (diff) |
Merge "Migrate to gson"
18 files changed, 553 insertions, 266 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/pom.xml b/vnfmarket-be/vnf-sdk-marketplace/pom.xml index 0544e255..875710fb 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/pom.xml +++ b/vnfmarket-be/vnf-sdk-marketplace/pom.xml @@ -82,10 +82,18 @@ <version>2.1.0</version> </dependency> + <!--excluded com.fasterxml.jackson.core:jackson-databind:jar:2.9.5 + CVE-2018-11307--> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jersey2-jaxrs</artifactId> <version>1.5.19</version> + <exclusions> + <exclusion> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </exclusion> + </exclusions> </dependency> <!-- jersey --> <!-- excluded jetty-util and added invulnerable version --> 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; } } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutorTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutorTest.java index 04180322..0732b3f7 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutorTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/onboarding/hooks/validatelifecycle/LifecycleTestExceutorTest.java @@ -15,9 +15,18 @@ */ package org.onap.vnfsdk.marketplace.onboarding.hooks.validatelifecycle; +import mockit.Mock; +import mockit.MockUp; +import org.apache.http.HttpEntity; import org.junit.Before; import org.junit.Test; +import org.onap.vnfsdk.marketplace.common.FileUtil; +import org.onap.vnfsdk.marketplace.msb.MsbDetails; +import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder; +import org.onap.vnfsdk.marketplace.msb.MsbServer; import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest; +import org.onap.vnfsdk.marketplace.rest.RestResponse; +import org.onap.vnfsdk.marketplace.rest.RestfulClient; import static org.mockito.Mockito.mock; @@ -29,16 +38,72 @@ public class LifecycleTestExceutorTest { public void setUp() { // lifecycleTestExceutor = new LifecycleTestExceutor(); - onBoradingRequest = mock(OnBoradingRequest.class); lifeCycleTestReq = mock(LifeCycleTestReq.class); } @Test - public void testupLoadPackageToCatalouge() + public void testupLoadPackageToCatalougeForGson() { - // LifecycleTestExceutor.uploadPackageToCatalouge(onBoradingRequest); + new MockUp<FileUtil>(){ + @Mock + public boolean validatePath(String path) { + return true; + } + }; + new MockUp<LifecycleTestExceutor>(){ + @Mock + private boolean checkValidResponse(RestResponse rsp) { + return true; + } + }; + new MockUp<RestResponse>(){ + @Mock + public String getResult() { + return "{\"csarId\":\"huawei\"}"; + } + }; + new MockUp<MsbDetails>(){ + @Mock + public MsbServer getDefaultServer() { + MsbServer msbServer = new MsbServer(); + msbServer.setHost("0.0.0.0"); + msbServer.setPort("5005"); + return msbServer; + } + }; + new MockUp<RestfulClient>(){ + @Mock + public RestResponse post(String ip, int port, String url, HttpEntity requestBody) { + RestResponse rsp = new RestResponse(); + rsp.setStatusCode(200); + rsp.setResult("OK"); + return rsp; + } + }; + new MockUp<MsbServer>(){ + @Mock + public String getHost() { + return "0.0.0.0"; + } + @Mock + public String getPort() { + return "5005"; + } + }; + new MockUp<MsbDetailsHolder>(){ + @Mock + public synchronized MsbDetails getMsbDetails(){ + MsbDetails msbDetails = new MsbDetails(); + return msbDetails; + } + }; + onBoradingRequest = new OnBoradingRequest(); + onBoradingRequest.setCsarId("huawei"); + onBoradingRequest.setPackagePath(""); + onBoradingRequest.setPackageName("huawei"); + onBoradingRequest.setCsarIdCatalouge("catalog"); + LifecycleTestExceutor.uploadPackageToCatalouge(onBoradingRequest); } - @Test public void testExeclifecycleTest() { LifecycleTestExceutor.execlifecycleTest(onBoradingRequest,lifeCycleTestReq); @@ -46,4 +111,4 @@ public class LifecycleTestExceutorTest { } -}
\ No newline at end of file +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/rest/RestResponseTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/rest/RestResponseTest.java index 2e74b8f4..3669bbdd 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/rest/RestResponseTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/rest/RestResponseTest.java @@ -33,8 +33,8 @@ public class RestResponseTest { @Test public void testSetterGetter() { - restResponse.setResult("huawei"); - assertEquals(restResponse.getResult(), "huawei"); + restResponse.setResult("{\"csarId\":\"huawei\"}"); + assertEquals(restResponse.getResult(), "{\"csarId\":\"huawei\"}"); restResponse.setStatusCode(200); assertThat(restResponse.getStatusCode(),is(200)); } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java index 388d23e9..74f2d515 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java @@ -15,12 +15,8 @@ */ package org.onap.vtp; -import com.fasterxml.jackson.core.*; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.jsontype.TypeSerializer; -import com.fasterxml.jackson.databind.node.JsonNodeType; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import org.glassfish.jersey.media.multipart.ContentDisposition; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.junit.Before; @@ -57,9 +53,9 @@ public class VTPExecutionResourceTest { { VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList(); List<VTPTestExecution> list= new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); + JsonParser jsonParser = new JsonParser(); String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}"; - JsonNode rootNode = mapper.readTree(jsonString); + JsonElement rootNode = jsonParser.parse(jsonString); VTPTestExecution vtp=new VTPTestExecution(); vtp.setEndTime("2019-03-12T11:49:52.845"); diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java index ea5c397f..f0f0cce8 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java @@ -15,8 +15,8 @@ */ package org.onap.vtp; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -54,8 +54,8 @@ public class VTPScenarioResourceTest { VTPScenarioResource vtpScenarioResource2=mock(VTPScenarioResource.class); List<String> args = new ArrayList<>(); args.add("abc"); - ObjectMapper mapper = new ObjectMapper(); - JsonNode actualObj = mapper.readTree("{\"k1\":\"v1\"}"); + JsonParser jsonParser = new JsonParser(); + JsonElement actualObj = jsonParser.parse("{\"k1\":\"v1\"}"); vtpScenarioResource.listTestSutiesHandler("abc"); // when(vtpScenarioResource2.makeRpcAndGetJson(args)).thenReturn(actualObj); //vtpScenarioResource.listTestSutiesHandler("VTP Scenario 1"); diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResourceTest.java index c4a3ef4c..9dfb7567 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResourceTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResourceTest.java @@ -15,12 +15,10 @@ */ package org.onap.vtp.execution; -import com.fasterxml.jackson.core.*; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.jsontype.TypeSerializer; -import com.fasterxml.jackson.databind.node.JsonNodeType; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import mockit.MockUp; import org.glassfish.jersey.media.multipart.ContentDisposition; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.junit.Before; @@ -29,6 +27,10 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.onap.vtp.execution.model.VTPTestExecution; +import org.open.infc.grpc.Output; +import org.open.infc.grpc.Result; +import org.onap.vtp.VTPResource; +import org.onap.vtp.error.VTPError; import java.io.IOException; import java.util.*; @@ -37,15 +39,10 @@ import static org.junit.Assert.*; @RunWith(MockitoJUnitRunner.class) public class VTPExecutionResourceTest { - @Mock - FormDataBodyPart formDataBodyPart; - @Mock - ContentDisposition contentDisposition; String requestId; VTPExecutionResource vtpExecutionResource; @Before public void setUp() throws Exception { - vtpExecutionResource= new VTPExecutionResource(); requestId = UUID.randomUUID().toString(); } @Test(expected = Exception.class) @@ -53,9 +50,9 @@ public class VTPExecutionResourceTest { { VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList(); List<VTPTestExecution> list= new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); + JsonParser jsonParser = new JsonParser(); String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}"; - JsonNode rootNode = mapper.readTree(jsonString); + JsonElement rootNode = jsonParser.parse(jsonString); VTPTestExecution vtp=new VTPTestExecution(); vtp.setEndTime("2019-03-12T11:49:52.845"); @@ -73,44 +70,255 @@ public class VTPExecutionResourceTest { executions.setExecutions(list); //System.out.println(executions.getExecutions()); assertNotNull(executions.getExecutions()); + vtpExecutionResource = new VTPExecutionResource(); vtpExecutionResource.executeHandler(executions,null); // vtpExecutionResource.executeHandler(executions,requestId); } - @Test(expected = Exception.class) + @Test + public void testExecuteHandlerForGsonCoverage() throws Exception + { + new MockUp<VTPResource>(){ + @mockit.Mock + protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPError.VTPException { + String dummyValue = "{\"execution-id\":\"execution-id\"}"; + Gson gson = new Gson(); + return gson.fromJson(dummyValue,Output.class); + } + }; + new MockUp<Output>(){ + @mockit.Mock + public Map<String, String> getAddonsMap() { + String dummyValue = "{\"execution-id\":\"execution-id\"}"; + Gson gson = new Gson(); + return gson.fromJson(dummyValue,Map.class); + } + }; + new MockUp<Output>(){ + @mockit.Mock + public Map<String, String> getAttrsMap() { + String dummyValue = "{\"results\":[{\"execution-id\":\"execution-id\"}]}"; + Gson gson = new Gson(); + return gson.fromJson(dummyValue,Map.class); + } + }; + new MockUp<Output>(){ + @mockit.Mock + public boolean getSuccess() { + return true; + } + }; + VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList(); + List<VTPTestExecution> list= new ArrayList<>(); + JsonParser jsonParser = new JsonParser(); + String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}"; + JsonElement rootNode = jsonParser.parse(jsonString); + + VTPTestExecution vtp=new VTPTestExecution(); + vtp.setEndTime("2019-03-12T11:49:52.845"); + vtp.setProfile("abc"); + vtp.setStatus("pass"); + vtp.setRequestId(requestId); + vtp.setExecutionId("executionid"); + vtp.setParameters(rootNode); + vtp.setResults(rootNode); + vtp.setScenario("open-cli"); + vtp.setStartTime("2019-04-12T11:49:52.845"); + vtp.setTestCaseName("testcase"); + vtp.setTestSuiteName("testsuite"); + list.add(vtp); + executions.setExecutions(list); + //System.out.println(executions.getExecutions()); + assertNotNull(executions.getExecutions()); + vtpExecutionResource = new VTPExecutionResource(); + assertNotNull(vtpExecutionResource.executeHandler(executions,null)); + // vtpExecutionResource.executeHandler(executions,requestId); + + } + @Test + public void testExecuteHandlerForGsonCoverageNegative() throws Exception + { + new MockUp<VTPResource>(){ + @mockit.Mock + protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPError.VTPException { + String dummyValue = "{\"execution-id\":\"execution-id\"}"; + Gson gson = new Gson(); + return gson.fromJson(dummyValue,Output.class); + } + }; + new MockUp<Output>(){ + @mockit.Mock + public Map<String, String> getAddonsMap() { + String dummyValue = "{\"execution-id\":\"execution-id\"}"; + Gson gson = new Gson(); + return gson.fromJson(dummyValue,Map.class); + } + }; + new MockUp<Output>(){ + @mockit.Mock + public Map<String, String> getAttrsMap() { + String dummyValue = "{\"error\":\"DummyError occurs\"}"; + Gson gson = new Gson(); + return gson.fromJson(dummyValue,Map.class); + } + }; + new MockUp<Output>(){ + @mockit.Mock + public boolean getSuccess() { + return false; + } + }; + VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList(); + List<VTPTestExecution> list= new ArrayList<>(); + JsonParser jsonParser = new JsonParser(); + String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}"; + JsonElement rootNode = jsonParser.parse(jsonString); + + VTPTestExecution vtp=new VTPTestExecution(); + vtp.setEndTime("2019-03-12T11:49:52.845"); + vtp.setProfile("abc"); + vtp.setStatus("pass"); + vtp.setRequestId(requestId); + vtp.setExecutionId("executionid"); + vtp.setParameters(rootNode); + vtp.setResults(rootNode); + vtp.setScenario("open-cli"); + vtp.setStartTime("2019-04-12T11:49:52.845"); + vtp.setTestCaseName("testcase"); + vtp.setTestSuiteName("testsuite"); + list.add(vtp); + executions.setExecutions(list); + //System.out.println(executions.getExecutions()); + assertNotNull(executions.getExecutions()); + vtpExecutionResource = new VTPExecutionResource(); + assertNotNull(vtpExecutionResource.executeHandler(executions,null)); + // vtpExecutionResource.executeHandler(executions,requestId); + + } public void testListTestExecutionsHandler() throws Exception { + vtpExecutionResource = new VTPExecutionResource(); vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123"); } + @Test + public void testListTestExecutionsHandlerForGson() throws Exception + { + new MockUp<VTPResource>(){ + @mockit.Mock + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException { + String values = "[{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " + + "\"request-id\":\"request-id\", \"product\":\"product\"," + + "\"service\":\"service\", \"command\":\"command\", " + + "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"}]"; + JsonParser jsonParser = new JsonParser(); + return jsonParser.parse(values); + } + }; + vtpExecutionResource = new VTPExecutionResource(); + assertNotNull(vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123")); + } + @Test + public void testListTestExecutionsHandlerTestmakeRpcAndGetJson() throws Exception + { + VTPExecutionResource vtpExecutionResource1 = new VTPExecutionResource(); + VTPResource vtpResource = new VTPResource(); - @Test(expected = Exception.class) + new MockUp<VTPResource>(){ + @mockit.Mock + protected Result makeRpc(List <String> args, int timeout) throws VTPError.VTPException { + Result result = Result.newBuilder().build(); + return result; + } + }; + new MockUp<Result>(){ + @mockit.Mock + public String getOutput() { + return "[{\"product\":\"tutorial\"}]"; + } + }; + VTPTestExecution.VTPTestExecutionList vtpTestExecutionList = vtpExecutionResource1.listTestExecutionsHandler(requestId,"tutorial","ut","list-users","abc","123","123"); + assertTrue(vtpTestExecutionList.getExecutions().size()>0); + } public void testListTestExecutions() throws Exception { + vtpExecutionResource = new VTPExecutionResource(); vtpExecutionResource.listTestExecutions(requestId,"abc","abc","abc","abc","123","123"); } - @Test(expected = Exception.class) public void testGetTestExecution() throws Exception { //assertNotNull(vtpExecutionResource.getTestExecution("abc")); + vtpExecutionResource = new VTPExecutionResource(); assertNotNull(vtpExecutionResource.getTestExecution("1234")); } - @Test(expected = Exception.class) public void testGetTestExecutionHandler() throws Exception { //assertNotNull(vtpExecutionResource.getTestExecution("abc")); + vtpExecutionResource = new VTPExecutionResource(); + assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234")); + } + @Test + public void testGetTestExecutionHandlerForGson() throws Exception + { + new MockUp<VTPResource>(){ + @mockit.Mock + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException { + String values = "{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " + + "\"request-id\":\"request-id\", \"product\":\"product\"," + + "\"service\":\"service\", \"command\":\"command\", " + + "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"," + + "\"input\": \"[]\", \"output\":\"[]\"}"; + JsonParser jsonParser = new JsonParser(); + return jsonParser.parse(values); + } + }; + //assertNotNull(vtpExecutionResource.getTestExecution("abc")); + vtpExecutionResource = new VTPExecutionResource(); assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234")); } + @Test + public void testGetTestExecutionHandlerForGsonWithResultNull() throws Exception + { + new MockUp<VTPResource>(){ + @mockit.Mock + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException { + String values = "{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " + + "\"request-id\":\"request-id\", \"product\":\"product\"," + + "\"service\":\"service\", \"command\":\"command\", " + + "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"," + + "\"input\": \"[]\", \"output\":\"null\"}"; + JsonParser jsonParser = new JsonParser(); + return jsonParser.parse(values); + } + }; + VTPExecutionResource vtpExecutionResource10 = new VTPExecutionResource(); + assertNotNull(vtpExecutionResource10.getTestExecutionHandler("1234")); + } + @Test + public void testGetTestExecutionHandlerForGsonWithResultNullForCatchException() throws Exception + { + new MockUp<VTPResource>(){ + @mockit.Mock + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) + throws VTPError.VTPException, IOException { + String values = "{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " + + "\"request-id\":\"request-id\", \"product\":\"product\"," + + "\"service\":\"service\", \"command\":\"command\", " + + "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"," + + "\"input\": \"[]\", \"output\":null}"; + JsonParser jsonParser = new JsonParser(); + return jsonParser.parse(values); + } + }; + VTPExecutionResource vtpExecutionResource11 = new VTPExecutionResource(); + assertNotNull(vtpExecutionResource11.getTestExecutionHandler("1234")); + } - @Test(expected = NullPointerException.class) + @Test public void testExecuteTestcases() throws Exception { - - List<FormDataBodyPart> bodyParts= new ArrayList<>(); - formDataBodyPart.setName("abc"); - formDataBodyPart.setValue("123"); - formDataBodyPart.setContentDisposition(contentDisposition); - formDataBodyPart.getContentDisposition().getFileName(); - bodyParts.add(formDataBodyPart); - vtpExecutionResource.executeTestcases(requestId,bodyParts,"exeJson") ; + vtpExecutionResource = new VTPExecutionResource(); + String execJson = "[{\"scenario\":\"tutorial\",\"testCaseName\":\"list-users\",\"testSuiteName\":\"ut\"," + + "\"requestId\":\"1234567890\",\"executionId\":\"123\",\"profile\":\"http\"}]"; + vtpExecutionResource.executeTestcases(requestId,null,"exeJson") ; } -}
\ No newline at end of file +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/VTPScenarioResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/VTPScenarioResourceTest.java index 3893d56a..1da85f4d 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/VTPScenarioResourceTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/VTPScenarioResourceTest.java @@ -15,13 +15,14 @@ */ package org.onap.vtp.scenario; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import mockit.Mock; import mockit.MockUp; import org.junit.Before; import org.junit.Test; import org.onap.vtp.VTPResource; +import org.onap.vtp.error.VTPError; import java.io.IOException; import java.util.List; @@ -41,10 +42,10 @@ public class VTPScenarioResourceTest { public void testListTestScenariosHandler() throws Exception { new MockUp<VTPResource>() { @Mock - public JsonNode makeRpcAndGetJson(List<String> args) throws IOException { - ObjectMapper mapper = new ObjectMapper(); + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException { + JsonParser jsonParser = new JsonParser(); String jsonvalue = "[{\"product\":\"onap-dublin\",\"description\":\"its 4th release\"}]"; - JsonNode jsonNode = mapper.readTree(jsonvalue); + JsonElement jsonNode = jsonParser.parse(jsonvalue); return jsonNode; } }; @@ -55,22 +56,31 @@ public class VTPScenarioResourceTest { public void testListTestSutiesHandler() throws Exception { new MockUp<VTPResource>() { @Mock - public JsonNode makeRpcAndGetJson(List<String> args) throws IOException { - ObjectMapper mapper = new ObjectMapper(); + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException { + JsonParser jsonParser = new JsonParser(); String jsonvalue = "[{\"product\":\"onap-dublin\",\"service\":\"test\",\"description\":\"its 4th release\"}]"; - JsonNode jsonNode = mapper.readTree(jsonvalue); + JsonElement jsonNode = jsonParser.parse(jsonvalue); return jsonNode; } }; assertNotNull(vtpScenarioResource.listTestSutiesHandler("open-cli")); } - @Test(expected = Exception.class) + @Test public void testListTestcasesHandler() throws Exception { + new MockUp<VTPResource>() { + @Mock + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException { + JsonParser jsonParser = new JsonParser(); + String jsonvalue = "[{\"command\":\"list-users\",\"service\":\"ut\"}]"; + JsonElement jsonNode = jsonParser.parse(jsonvalue); + return jsonNode; + } + }; + vtpScenarioResource.listTestcasesHandler("testsuite", "open-cli"); } - @Test(expected = Exception.class) public void testListTestcases() throws Exception { vtpScenarioResource.listTestcases("open-cli", "testsuite"); } @@ -84,14 +94,13 @@ public class VTPScenarioResourceTest { public void testGetTestcaseHandler() throws Exception { new MockUp<VTPResource>() { @Mock - public JsonNode makeRpcAndGetJson(List<String> args) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException { + JsonParser jsonParser = new JsonParser(); String jsonvalue = "{\"schema\":{\"name\":\"cli\",\"product\":\"onap-dublin\",\"description\":\"its 4th release\"," + "\"service\":\"test\",\"author\":\"jitendra\",\"inputs\":[{\"name\":\"abc\",\"description\":\"abc\"," + "\"type\":\"abc\",\"is_optional\":\"yes\",\"default_value\":\"abc\",\"metadata\":\"abc\"}]," + "\"outputs\":[{\"name\":\"abc\",\"description\":\"abc\",\"type\":\"abc\"}]}}"; - JsonNode jsonNode = mapper.readTree(jsonvalue); + JsonElement jsonNode = jsonParser.parse(jsonvalue); return jsonNode; } }; diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/model/VTPTestCaseTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/model/VTPTestCaseTest.java index 67e85d7e..5e469640 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/model/VTPTestCaseTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/model/VTPTestCaseTest.java @@ -15,8 +15,8 @@ */ package org.onap.vtp.scenario.model; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import org.junit.Before; import org.junit.Test; @@ -33,8 +33,8 @@ public class VTPTestCaseTest { } @Test public void testGetterSetter() throws Exception - { ObjectMapper mapper = new ObjectMapper(); - JsonNode actualObj = mapper.readTree("{\"k1\":\"v1\"}"); + { JsonParser jsonParser = new JsonParser(); + JsonElement actualObj = jsonParser.parse("{\"k1\":\"v1\"}"); List<VTPTestCase.VTPTestCaseInput> list= new ArrayList<>(); List<VTPTestCase.VTPTestCaseOutput> list1=new ArrayList<>(); vtpTestCase.setAuthor("abc"); |