diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-10-08 11:48:34 +0200 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-10-08 12:07:15 +0200 |
commit | c184cf1bda7a8a5d1cd489fb8080d2ee507611de (patch) | |
tree | 220770e6930f70d3b9ba8f61441aa96cc384fc5e | |
parent | 15c39f44fb16ca5e16b522b32eea79b4f73193c3 (diff) |
Fix get executions JSON parsing error.
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I1ee1c070cc0c97eebfad5cdb5e737d8136631cbf
Issue-ID: VNFSDK-697
5 files changed, 53 insertions, 22 deletions
diff --git a/Changelog.md b/Changelog.md index 698057f3..14147cc7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -11,3 +11,7 @@ All notable changes to this project will be documented in this file. - https://jira.onap.org/browse/VNFSDK-650 ## [1.6.1] + +### Fix +- Fix JSON parsing error returned from GET request + - https://jira.onap.org/browse/VNFSDK-697 diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResultsSupplier.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResultsSupplier.java index 3f1e4c57..e2d43f14 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResultsSupplier.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResultsSupplier.java @@ -19,6 +19,7 @@ package org.onap.vtp.execution; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,21 +66,21 @@ public class VTPExecutionResultsSupplier { .orElse(createNoOutputFileErrorMessageInJsonFormat()); } - private JsonArray loadOutputJsonFromFile(File file) { - JsonArray outputJson; + private JsonElement loadOutputJsonFromFile(File file) { + JsonElement outputJson; try { String executionResult = Files.readString(file.toPath()); - outputJson = gson.fromJson(executionResult, JsonArray.class); + outputJson = gson.fromJson(executionResult, JsonElement.class); } catch (IOException | JsonParseException e) { logger.error(e.getMessage(),e); String errorMessage = "" + - "[{ \"error\": \"fail to load execution result\",\"reason\":\"" + e.getMessage() + "\"}]"; - outputJson = gson.fromJson(errorMessage, JsonArray.class); + "{ \"error\": \"fail to load execution result\",\"reason\":\"" + e.getMessage() + "\"}"; + outputJson = gson.fromJson(errorMessage, JsonObject.class); } return outputJson; } - private JsonArray createNoOutputFileErrorMessageInJsonFormat() { - return gson.fromJson("[{ \"error\": \"unable to find execution results\"}]", JsonArray.class); + private JsonElement createNoOutputFileErrorMessageInJsonFormat() { + return gson.fromJson("{ \"error\": \"unable to find execution results\"}", JsonObject.class); } } 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 1a3200ab..4f15fcf5 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 @@ -20,6 +20,7 @@ import com.google.common.collect.Lists; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.glassfish.jersey.media.multipart.ContentDisposition; import org.glassfish.jersey.media.multipart.FormDataBodyPart; @@ -153,12 +154,12 @@ public class VTPExecutionResourceTest { String testProduct = "VTP Scenario 2"; String testCommand = "s1.ts1.testcase-2"; String testSuiteName = "testsuite-2"; - String testRequestId = "test-02-request-id"; + String testRequestId = "test-wrong-request-id"; String testExecutionId = testRequestId + "-execution-id"; String testProfile = "open-cli-schema"; String expectedStatus = "FAIL"; JsonElement expectedResult = new Gson().fromJson("" + - "[{ \"error\": \"unable to find execution results\"}]", JsonArray.class); + "{ \"error\": \"unable to find execution results\"}", JsonObject.class); prepareMockRpcMethods( testStartTime, testEndTime, testProduct, testCommand, testSuiteName, @@ -193,11 +194,11 @@ public class VTPExecutionResourceTest { String testProfile = "open-cli-schema"; String expectedStatus = "FAIL"; JsonElement expectedResult = new Gson().fromJson("" + - "[{ " + + "{ " + "\"error\": \"fail to load execution result\"," + - "\"reason\":\"Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive\"" + - "}]", - JsonArray.class + "\"reason\":\"com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 8 path $\"" + + "}", + JsonObject.class ); prepareMockRpcMethods( diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResultsSupplierTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResultsSupplierTest.java index 7be8a7d4..83ee1739 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResultsSupplierTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResultsSupplierTest.java @@ -19,6 +19,7 @@ package org.onap.vtp.execution; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -28,7 +29,7 @@ public class VTPExecutionResultsSupplierTest { private static final String TEST_PATH_TO_EXECUTION = "src/test/resources/executions"; @Test - public void whenGetExecutionOutputsFromFileIsCalledWithPathToCorrectFileThenContentShouldBeLoadedAsJson() { + public void whenGetExecutionOutputsFromFileIsCalledWithPathToCorrectFileThenContentShouldBeLoadedAsJsonArray() { // given VTPExecutionResultsSupplier vtpExecutionResultsSupplier = new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION); @@ -48,20 +49,40 @@ public class VTPExecutionResultsSupplierTest { } @Test - public void whenGetExecutionOutputsFromFileIsCalledWithPathToNonExistingFileThenProperMessageShouldBeReturned() { + public void whenGetExecutionOutputsFromFileIsCalledWithPathToCorrectFileThenContentShouldBeLoadedAsJsonObject() { // given VTPExecutionResultsSupplier vtpExecutionResultsSupplier = new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION); String pathToCorrectFile = "test-02-request-id-execution-id"; + JsonElement expectedResult = new Gson().fromJson("" + + "{" + + "\"test_1\": \"error01\"," + + "\"test_2\": \"error02\" " + + "}", JsonObject.class); + + // when + JsonElement executionOutputsFromFile = + vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile); + + // then + assertEquals(executionOutputsFromFile, expectedResult); + } + + @Test + public void whenGetExecutionOutputsFromFileIsCalledWithPathToNonExistingFileThenProperMessageShouldBeReturned() { + // given + VTPExecutionResultsSupplier vtpExecutionResultsSupplier = + new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION); + String pathToCorrectFile = "test-wrong-request-id-execution-id"; JsonElement expectedErrorMessage = new Gson().fromJson("" + - "[{ \"error\": \"unable to find execution results\"}]", JsonArray.class); + "{ \"error\": \"unable to find execution results\"}", JsonObject.class); // when JsonElement executionOutputsFromFile = vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile); // then - assertEquals(executionOutputsFromFile, expectedErrorMessage); + assertEquals(expectedErrorMessage, executionOutputsFromFile); } @Test @@ -71,11 +92,11 @@ public class VTPExecutionResultsSupplierTest { new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION); String pathToCorrectFile = "test-incorrect-request-id-execution-id-data"; JsonElement expectedErrorMessage = new Gson().fromJson("" + - "[{ " + + "{ " + "\"error\": \"fail to load execution result\"," + - "\"reason\":\"Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive\"" + - "}]", - JsonArray.class + "\"reason\":\"com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 8 path $\"" + + "}", + JsonObject.class ); // when @@ -83,6 +104,6 @@ public class VTPExecutionResultsSupplierTest { vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile); // then - assertEquals(executionOutputsFromFile, expectedErrorMessage); + assertEquals(expectedErrorMessage, executionOutputsFromFile); } } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-02-request-id-execution-id-data/output b/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-02-request-id-execution-id-data/output new file mode 100644 index 00000000..a0ba3e91 --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-02-request-id-execution-id-data/output @@ -0,0 +1,4 @@ +{ + "test_1": "error01", + "test_2": "error02" +} |