diff options
author | Eylon Malin <eylon.malin@intl.att.com> | 2020-01-16 07:26:55 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-01-16 07:26:55 +0000 |
commit | adef7f90d338e0e93e3a3d0a6f714f0270a9b7f1 (patch) | |
tree | 3e90c68be8b4f8b3c2d48c8b351ec37ca46d275f | |
parent | b2051451afacc4ab025f6ce10bb5b8e0191e9e1f (diff) | |
parent | a46f5b6345622acd863ea41b61b607b847732151 (diff) |
Merge "Async MSO error format: handle more cases"
3 files changed, 52 insertions, 19 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/MsoUtil.java b/vid-app-common/src/main/java/org/onap/vid/mso/MsoUtil.java index 626816f7f..8de534a8e 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/MsoUtil.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/MsoUtil.java @@ -21,15 +21,15 @@ package org.onap.vid.mso; +import static org.apache.commons.lang3.StringUtils.firstNonBlank; +import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER; -import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import io.joshworks.restclient.http.HttpResponse; import java.io.IOException; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; -import org.onap.vid.exceptions.GenericUncheckedException; public class MsoUtil { @@ -76,22 +76,31 @@ public class MsoUtil { } public static String formatExceptionAdditionalInfo(int statusCode, String msoResponse) { - String errorMsg = "Http Code:" + statusCode; - if (!StringUtils.isEmpty(msoResponse)) { - String filteredJson; - try { - filteredJson = StringUtils.defaultIfEmpty( - JACKSON_OBJECT_MAPPER.readTree(msoResponse).path("serviceException").toString().replaceAll("[\\{\\}]","") , - msoResponse - ); - } catch (JsonParseException e) { - filteredJson = msoResponse; - } catch (IOException e) { - throw new GenericUncheckedException(e); - } + final String errorMsg = "Http Code:" + statusCode; + + if (isEmpty(msoResponse)) { + return errorMsg; + } + + try { + JsonNode jsonNode = JACKSON_OBJECT_MAPPER.readTree(msoResponse); - errorMsg = errorMsg + ", " + filteredJson; + return errorMsg + ", " + firstNonBlank( + removeBraces(jsonNode.get("serviceException")), + removeBraces(jsonNode.path("requestError").get("serviceException")), + msoResponse + ); + + } catch (Exception e) { + return errorMsg + ", " + msoResponse; } - return errorMsg; + } + + private static String removeBraces(JsonNode jsonNode) { + if (jsonNode == null || jsonNode.isMissingNode()) { + return null; + } + + return jsonNode.toString().replaceAll("[\\{\\}]", ""); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java index 10456bebf..cfde52a56 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java @@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat; import io.joshworks.restclient.http.HttpResponse; import org.onap.vid.testUtils.TestUtils; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class MsoUtilTest { @@ -67,4 +68,26 @@ public class MsoUtilTest { assertThat(result.getStatus()).isEqualTo(SC_OK); } + @DataProvider + public static Object[][] formatExceptionAdditionalInfo() { + return new Object[][]{ + {"message", "Http Code:400, message"}, + + {null, "Http Code:400"}, + + {"{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC0002\",\"text\":\"message\"}}}", + "Http Code:400, \"messageId\":\"SVC0002\",\"text\":\"message\""}, + + {"{\"validJson\": \"Error: message\"}", "Http Code:400, {\"validJson\": \"Error: message\"}"}, + + {"{\"serviceException\":{\"messageId\":\"SVC0002\",\"text\":\"Error: message\"}}", + "Http Code:400, \"messageId\":\"SVC0002\",\"text\":\"Error: message\""}, + }; + } + + @Test(dataProvider = "formatExceptionAdditionalInfo") + public void formatExceptionAdditionalInfo_payloadWithError400_doNotReturnNull(String payload, String expected) { + assertThat(MsoUtil.formatExceptionAdditionalInfo(400, payload)) + .isEqualTo(expected); + } } diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AsyncInstantiationBusinessLogicTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AsyncInstantiationBusinessLogicTest.java index 0749aaf82..a1e4af2c4 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/AsyncInstantiationBusinessLogicTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/AsyncInstantiationBusinessLogicTest.java @@ -1223,7 +1223,8 @@ public class AsyncInstantiationBusinessLogicTest extends AsyncInstantiationBaseT String message = "Failed to create service instance"; return new Object[][]{ {500, message}, - {199, "{\"serviceException\":{\"messageId\":\"SVC2000\",\"text\":\"Error: " + message + "\"}}"} + {400, "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC0002\",\"text\":\"" + message + "\"}}}"}, + {199, "{\"serviceException\":{\"messageId\":\"SVC2000\",\"text\":\"Error: " + message + "\"}}"}, }; } |