From 741b5a8d274a2b7c189944bc24db423790dcdd6f Mon Sep 17 00:00:00 2001 From: lucashimpens Date: Tue, 12 Sep 2023 16:57:28 +0100 Subject: [PATCH] fix: json service not handled null values (#2459) --- .../execution/impl/ConditionService.java | 55 +++++---- .../core/engine/gwt/impl/ControlService.java | 112 +++++++++--------- .../core/engine/gwt/impl/PropertyService.java | 109 +++++++++-------- .../cerberus/core/enums/MessageEventEnum.java | 9 +- .../core/service/json/impl/JsonService.java | 17 ++- 5 files changed, 154 insertions(+), 148 deletions(-) diff --git a/source/src/main/java/org/cerberus/core/engine/execution/impl/ConditionService.java b/source/src/main/java/org/cerberus/core/engine/execution/impl/ConditionService.java index 0df870f3c6..6e3f19aeae 100644 --- a/source/src/main/java/org/cerberus/core/engine/execution/impl/ConditionService.java +++ b/source/src/main/java/org/cerberus/core/engine/execution/impl/ConditionService.java @@ -19,14 +19,11 @@ */ package org.cerberus.core.engine.execution.impl; +import com.jayway.jsonpath.InvalidPathException; +import com.jayway.jsonpath.PathNotFoundException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.cerberus.core.crud.entity.AppService; -import org.cerberus.core.crud.entity.Application; -import org.cerberus.core.crud.entity.TestCaseCountryProperties; -import org.cerberus.core.crud.entity.TestCaseExecution; -import org.cerberus.core.crud.entity.TestCaseStepAction; -import org.cerberus.core.crud.entity.TestCaseStepActionControl; +import org.cerberus.core.crud.entity.*; import org.cerberus.core.engine.entity.Identifier; import org.cerberus.core.engine.entity.MessageEvent; import org.cerberus.core.engine.execution.IConditionService; @@ -468,18 +465,21 @@ private AnswerItem evaluateCondition_ifElementPresent(String conditionO LOG.debug("Checking if Element Present - JSON"); } try { - if (jsonService.getFromJson(responseBody, null, conditionValue1) != null) { - conditionResult = true; - mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_TRUE_IFELEMENTPRESENT); - mes.setDescription(mes.getDescription().replace("%ELEMENT%", conditionValue1)); - } else { - conditionResult = false; - mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FALSE_IFELEMENTPRESENT); - mes.setDescription(mes.getDescription().replace("%ELEMENT%", conditionValue1)); - } + jsonService.getFromJson(responseBody, null, conditionValue1); + conditionResult = true; + mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_TRUE_IFELEMENTPRESENT) + .resolveDescription("ELEMENT", conditionValue1); + } catch (PathNotFoundException ex) { + conditionResult = false; + mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FALSE_IFELEMENTPRESENT) + .resolveDescription("ELEMENT", conditionValue1); + } catch (InvalidPathException ex) { + conditionResult = false; + mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FAILED_INVALIDJSONPATH) + .resolveDescription("PATH", conditionValue1); } catch (Exception ex) { - mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FAILED_GENERIC); - mes.setDescription(mes.getDescription().replace("%ERROR%", ex.toString())); + mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FAILED_GENERIC) + .resolveDescription("ERROR", ex.toString()); } break; @@ -621,15 +621,18 @@ private AnswerItem evaluateCondition_ifElementNotPresent(String conditi case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON: try { - if (jsonService.getFromJson(responseBody, null, conditionValue1) == null) { - conditionResult = true; - mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_TRUE_IFELEMENTNOTPRESENT); - mes.setDescription(mes.getDescription().replace("%ELEMENT%", conditionValue1)); - } else { - conditionResult = false; - mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FALSE_IFELEMENTNOTPRESENT); - mes.setDescription(mes.getDescription().replace("%ELEMENT%", conditionValue1)); - } + jsonService.getFromJson(responseBody, null, conditionValue1); + conditionResult = true; + mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FALSE_IFELEMENTNOTPRESENT) + .resolveDescription("ELEMENT", conditionValue1); + } catch (PathNotFoundException ex) { + conditionResult = false; + mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_TRUE_IFELEMENTNOTPRESENT) + .resolveDescription("ELEMENT", conditionValue1); + } catch (InvalidPathException ex) { + conditionResult = false; + mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FAILED_INVALIDJSONPATH) + .resolveDescription("PATH", conditionValue1); } catch (Exception ex) { mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_FAILED_GENERIC); mes.setDescription(mes.getDescription().replace("%ERROR%", ex.toString())); diff --git a/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java b/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java index 111837e183..bfb4e454c5 100644 --- a/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java +++ b/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java @@ -21,16 +21,11 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.google.common.primitives.Ints; +import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.PathNotFoundException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.cerberus.core.crud.entity.AppService; -import org.cerberus.core.crud.entity.Application; -import org.cerberus.core.crud.entity.TestCaseExecution; -import org.cerberus.core.crud.entity.TestCaseExecutionFile; -import org.cerberus.core.crud.entity.TestCaseStepActionControl; -import org.cerberus.core.crud.entity.TestCaseStepActionControlExecution; -import org.cerberus.core.crud.entity.TestCaseStepActionExecution; +import org.cerberus.core.crud.entity.*; import org.cerberus.core.engine.entity.Identifier; import org.cerberus.core.engine.entity.MessageEvent; import org.cerberus.core.engine.entity.MessageGeneral; @@ -58,12 +53,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.regex.Matcher; @@ -654,29 +644,25 @@ private MessageEvent verifyElementPresent(TestCaseExecution tCExecution, String return mes; case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON: { try { - //Return of getFromJson can be "[]" in case when the path has this pattern "$..ex" and no elements found. Two dots after $ return a list. - if (!jsonService.getFromJson(responseBody, null, elementPath).equals("[]")) { - mes = new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_PRESENT); - mes.resolveDescription("STRING1", elementPath); - return mes; - } else { - throw new PathNotFoundException(); - } + jsonService.getFromJson(responseBody, null, elementPath); + return new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_PRESENT) + .resolveDescription("STRING1", elementPath); } catch (PathNotFoundException ex) { - mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_PRESENT); - mes.resolveDescription("STRING1", elementPath); - return mes; + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_PRESENT) + .resolveDescription("STRING1", elementPath); + } catch (InvalidPathException ex) { + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENT_INVALIDJSONPATH) + .resolveDescription("PATH", elementPath) + .resolveDescription("ERROR", ""); } catch (Exception ex) { - mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC); - mes.resolveDescription("ERROR", ex.toString()); - return mes; + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC) + .resolveDescription("ERROR", ex.toString()); } } default: - mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE); - mes.resolveDescription("TYPE", tCExecution.getLastServiceCalled().getResponseHTTPBodyContentType()); - mes.resolveDescription("CONTROL", TestCaseStepActionControl.CONTROL_VERIFYELEMENTPRESENT); - return mes; + return new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE) + .resolveDescription("TYPE", tCExecution.getLastServiceCalled().getResponseHTTPBodyContentType()) + .resolveDescription("CONTROL", TestCaseStepActionControl.CONTROL_VERIFYELEMENTPRESENT); } } else { @@ -773,30 +759,26 @@ private MessageEvent verifyElementNotPresent(TestCaseExecution execution, String return mes; case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON: { try { - //Return of getFromJson can be "[]" in case when the path has this pattern "$..ex" and no elements found. Two dots after $ return a list. - if (!jsonService.getFromJson(responseBody, null, elementPath).equals("[]")) { - mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_NOTPRESENT); - mes.resolveDescription("STRING1", elementPath); - return mes; - } else { - throw new PathNotFoundException(); - } + jsonService.getFromJson(responseBody, null, elementPath); + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_NOTPRESENT) + .resolveDescription("STRING1", elementPath); } catch (PathNotFoundException ex) { - mes = new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_NOTPRESENT); - mes.resolveDescription("STRING1", elementPath); - return mes; + return new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_NOTPRESENT) + .resolveDescription("STRING1", elementPath); + } catch (InvalidPathException ex) { + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENT_INVALIDJSONPATH) + .resolveDescription("PATH", elementPath) + .resolveDescription("ERROR", ""); } catch (Exception ex) { - mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC); - mes.resolveDescription("ERROR", ex.toString()); - return mes; + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC) + .resolveDescription("ERROR", ex.toString()); } } default: - mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE); - mes.resolveDescription("TYPE", execution.getLastServiceCalled().getResponseHTTPBodyContentType()); - mes.resolveDescription("CONTROL", TestCaseStepActionControl.CONTROL_VERIFYELEMENTNOTPRESENT); - return mes; + return new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE) + .resolveDescription("TYPE", execution.getLastServiceCalled().getResponseHTTPBodyContentType()) + .resolveDescription("CONTROL", TestCaseStepActionControl.CONTROL_VERIFYELEMENTNOTPRESENT); } } else { @@ -1089,10 +1071,16 @@ public MessageEvent verifyElementXXX(String control, TestCaseExecution tCExecuti case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON: { try { actual = jsonService.getFromJson(responseBody, null, path); + } catch (PathNotFoundException ex) { + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENT_NOSUCHELEMENT) + .resolveDescription("ELEMENT", path); + } catch (InvalidPathException exception) { + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENT_INVALIDJSONPATH) + .resolveDescription("PATH", path) + .resolveDescription("ERROR", ""); } catch (Exception ex) { - mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC); - mes.resolveDescription("ERROR", ex.toString()); - return mes; + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC) + .resolveDescription("ERROR", ex.toString()); } } // In case of null actual value then we alert user @@ -1285,13 +1273,19 @@ private MessageEvent verifyElementTextMatchRegex(TestCaseExecution tCExecution, case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON: try { - pathContent = jsonService.getFromJson(responseBody, null, path); - } catch (Exception ex) { - mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC); - mes.resolveDescription("ERROR", ex.toString()); - return mes; - } - break; + pathContent = jsonService.getFromJson(responseBody, null, path); + } catch (PathNotFoundException ex) { + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENT_NOSUCHELEMENT) + .resolveDescription("ELEMENT", path); + } catch (InvalidPathException exception) { + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENT_INVALIDJSONPATH) + .resolveDescription("PATH", path) + .resolveDescription("ERROR", ""); + } catch (Exception ex) { + return new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC) + .resolveDescription("ERROR", ex.toString()); + } + break; default: mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE); diff --git a/source/src/main/java/org/cerberus/core/engine/gwt/impl/PropertyService.java b/source/src/main/java/org/cerberus/core/engine/gwt/impl/PropertyService.java index fd79ad08b5..de0510ce2c 100644 --- a/source/src/main/java/org/cerberus/core/engine/gwt/impl/PropertyService.java +++ b/source/src/main/java/org/cerberus/core/engine/gwt/impl/PropertyService.java @@ -19,24 +19,13 @@ */ package org.cerberus.core.engine.gwt.impl; -import com.fasterxml.jackson.core.JsonProcessingException; import com.jayway.jsonpath.InvalidPathException; +import com.jayway.jsonpath.PathNotFoundException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.cerberus.core.crud.entity.AppService; -import org.cerberus.core.crud.entity.Application; -import org.cerberus.core.crud.entity.TestCaseCountryProperties; -import org.cerberus.core.crud.entity.TestCaseExecution; -import org.cerberus.core.crud.entity.TestCaseExecutionData; -import org.cerberus.core.crud.entity.TestCaseStepActionExecution; -import org.cerberus.core.crud.entity.TestDataLib; +import org.cerberus.core.crud.entity.*; import org.cerberus.core.crud.factory.IFactoryTestCaseExecutionData; -import org.cerberus.core.crud.service.IAppServiceService; -import org.cerberus.core.crud.service.ILogEventService; -import org.cerberus.core.crud.service.IParameterService; -import org.cerberus.core.crud.service.ISqlLibraryService; -import org.cerberus.core.crud.service.ITestCaseExecutionDataService; -import org.cerberus.core.crud.service.ITestDataLibService; +import org.cerberus.core.crud.service.*; import org.cerberus.core.engine.entity.Identifier; import org.cerberus.core.engine.entity.MessageEvent; import org.cerberus.core.engine.execution.IIdentifierService; @@ -52,6 +41,7 @@ import org.cerberus.core.service.groovy.IGroovyService; import org.cerberus.core.service.har.IHarService; import org.cerberus.core.service.json.IJsonService; +import org.cerberus.core.service.robotproxy.IRobotProxyService; import org.cerberus.core.service.soap.ISoapService; import org.cerberus.core.service.sql.ISQLService; import org.cerberus.core.service.webdriver.IWebDriverService; @@ -74,7 +64,6 @@ import java.text.SimpleDateFormat; import java.util.*; import java.util.logging.Level; -import org.cerberus.core.service.robotproxy.IRobotProxyService; /** * {Insert class description here} @@ -1010,22 +999,22 @@ private TestCaseExecutionData property_getFromNetworkTraffic(TestCaseExecutionDa String valueFromJson = this.jsonService.getFromJson(harRes.toString(), null, jsonPath); - if (valueFromJson != null) { - testCaseExecutionData.setValue(valueFromJson); - MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMNETWORKTRAFFIC) - .resolveDescription("PARAM", jsonPath) - .resolveDescription("VALUE", valueFromJson) - .resolveDescription("INDEX", String.valueOf(execution.getNetworkTrafficIndexList().size())) - .resolveDescription("NBHITS", String.valueOf(indexFrom)); - testCaseExecutionData.setPropertyResultMessage(res); - - } else { - MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_PATHNOTFOUND); - res.setDescription(res.getDescription().replace("%PARAM%", jsonPath)); - testCaseExecutionData.setPropertyResultMessage(res); - - } + testCaseExecutionData.setValue(valueFromJson); + MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMNETWORKTRAFFIC) + .resolveDescription("PARAM", jsonPath) + .resolveDescription("VALUE", valueFromJson) + .resolveDescription("INDEX", String.valueOf(execution.getNetworkTrafficIndexList().size())) + .resolveDescription("NBHITS", String.valueOf(indexFrom)); + testCaseExecutionData.setPropertyResultMessage(res); + } catch (PathNotFoundException exception) { + MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_PATHNOTFOUND) + .resolveDescription("PARAM", jsonPath); + testCaseExecutionData.setPropertyResultMessage(res); + } catch (InvalidPathException exception) { + MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_INVALIDPATH) + .resolveDescription("PATH", testCaseExecutionData.getValue1()); + testCaseExecutionData.setPropertyResultMessage(res); } catch (Exception ex) { LOG.warn("Exception when getting property from Network Traffic.", ex); MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_PROXYNOTACTIVE); @@ -1516,6 +1505,7 @@ private TestCaseExecutionData property_getDifferencesFromXml(TestCaseExecutionDa private TestCaseExecutionData property_getFromJson(TestCaseExecutionData testCaseExecutionData, TestCaseExecution execution, boolean forceRecalculation) { String jsonResponse = ""; + MessageEvent res; if (null != execution.getLastServiceCalled()) { jsonResponse = execution.getLastServiceCalled().getResponseHTTPBody(); @@ -1543,24 +1533,30 @@ private TestCaseExecutionData property_getFromJson(TestCaseExecutionData testCas } testCaseExecutionData.setValue(valueFromJson); - MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMJSON); - res.setDescription(res.getDescription().replace("%URL%", testCaseExecutionData.getValue2())); - res.setDescription(res.getDescription().replace("%PARAM%", testCaseExecutionData.getValue1())); - res.setDescription(res.getDescription().replace("%VALUE%", valueFromJson)); - testCaseExecutionData.setPropertyResultMessage(res); - } catch (InvalidPathException exception) { //Path not found, invalid path syntax or empty path - MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON_PARAMETERNOTFOUND); - res.setDescription(res.getDescription().replace("%URL%", testCaseExecutionData.getValue2())); - res.setDescription(res.getDescription().replace("%PARAM%", testCaseExecutionData.getValue1())); - res.setDescription(res.getDescription().replace("%ERROR%", "")); - testCaseExecutionData.setPropertyResultMessage(res); + res = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMJSON) + .resolveDescription("PARAM", testCaseExecutionData.getValue1()) + .resolveDescription("VALUE", valueFromJson); + } catch (PathNotFoundException exception) { + res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON_PARAMETERNOTFOUND) + .resolveDescription("PARAM", testCaseExecutionData.getValue1()) + .resolveDescription("ERROR", ""); + } catch (InvalidPathException exception) { + res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON_INVALIDPATH) + .resolveDescription("PATH", testCaseExecutionData.getValue1()); + } catch (Exception exception) { + res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON) + .resolveDescription("PARAM", testCaseExecutionData.getValue1()) + .resolveDescription("ERROR", exception.toString()); + LOG.error("Exception when trying to calculate getFromJson property: {}", exception.toString(), exception); } + testCaseExecutionData.setPropertyResultMessage(res); return testCaseExecutionData; } private TestCaseExecutionData property_getRawFromJson(TestCaseExecutionData testCaseExecutionData, TestCaseExecution execution) { String jsonResponse = ""; + MessageEvent res; //If tCExecution LastServiceCalled exist, get the response if (execution.getLastServiceCalled() != null) { @@ -1586,22 +1582,23 @@ private TestCaseExecutionData property_getRawFromJson(TestCaseExecutionData test String valueFromJson = this.jsonService.getRawFromJson(jsonResponse, testCaseExecutionData.getValue1()); testCaseExecutionData.setValue(valueFromJson); - MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMJSON); - res.setDescription(res.getDescription().replace("%URL%", testCaseExecutionData.getValue2())); - res.setDescription(res.getDescription().replace("%PARAM%", testCaseExecutionData.getValue1())); - res.setDescription(res.getDescription().replace("%VALUE%", valueFromJson)); - testCaseExecutionData.setPropertyResultMessage(res); - - } catch (JsonProcessingException | InvalidPathException exception) { //Path not found, invalid path syntax or empty path - if (LOG.isDebugEnabled()) { - LOG.error("Exception when getting property from JSON.", exception); - } - MessageEvent res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON_PARAMETERNOTFOUND); - res.setDescription(res.getDescription().replace("%URL%", testCaseExecutionData.getValue2())); - res.setDescription(res.getDescription().replace("%PARAM%", testCaseExecutionData.getValue1())); - res.setDescription(res.getDescription().replace("%ERROR%", "")); - testCaseExecutionData.setPropertyResultMessage(res); + res = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMJSON) + .resolveDescription("PARAM", testCaseExecutionData.getValue1()) + .resolveDescription("VALUE", valueFromJson); + } catch (PathNotFoundException exception) { + res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON_PARAMETERNOTFOUND) + .resolveDescription("PARAM", testCaseExecutionData.getValue1()) + .resolveDescription("ERROR", ""); + } catch (InvalidPathException exception) { + res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON_INVALIDPATH) + .resolveDescription("PATH", testCaseExecutionData.getValue1()); + } catch (Exception exception) { + LOG.error("Exception when trying to calculate getRawFromJson property: {}", exception.toString(), exception); + res = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMJSON) + .resolveDescription("PARAM", testCaseExecutionData.getValue1()) + .resolveDescription("ERROR", exception.toString()); } + testCaseExecutionData.setPropertyResultMessage(res); return testCaseExecutionData; } diff --git a/source/src/main/java/org/cerberus/core/enums/MessageEventEnum.java b/source/src/main/java/org/cerberus/core/enums/MessageEventEnum.java index 2ec76c071e..8be18c2426 100644 --- a/source/src/main/java/org/cerberus/core/enums/MessageEventEnum.java +++ b/source/src/main/java/org/cerberus/core/enums/MessageEventEnum.java @@ -48,7 +48,7 @@ public enum MessageEventEnum { PROPERTY_SUCCESS_RANDOM_NEW(100, "OK", "Random New property calculated with '%VALUE%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), PROPERTY_SUCCESS_GETATTRIBUTEFROMHTML(100, "OK", "Attribute '%ATTRIBUTE%' has returned '%VALUE%' for element '%ELEMENT%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), PROPERTY_SUCCESS_GETFROMCOOKIE(100, "OK", "Parameter '%PARAM%' from cookie '%COOKIE%' has been found and returned '%VALUE%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), - PROPERTY_SUCCESS_GETFROMJSON(100, "OK", "Path '%PARAM%' from Json has been found and returned '%VALUE%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), + PROPERTY_SUCCESS_GETFROMJSON(100, "OK", "Path '%PARAM%' from JSON has been found and returned '%VALUE%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), PROPERTY_SUCCESS_GETFROMNETWORKTRAFFIC(100, "OK", "Path '%PARAM%' from Network Traffic JSON (from index %INDEX% - from request nb : %NBHITS%) has been found and returned '%VALUE%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), PROPERTY_SUCCESS_GETFROMGROOVY(100, "OK", "Groovy script property calculated with '%VALUE%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), PROPERTY_SUCCESS_GETFROMCOMMAND(100, "OK", "Command calculated with '%VALUE%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), @@ -100,7 +100,9 @@ public enum MessageEventEnum { PROPERTY_FAILED_GETFROMCOOKIE_COOKIENOTFOUND(187, "FA", "Failed to get Parameter '%PARAM%' because could not find cookie '%COOKIE%'!", true, true, true, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETFROMCOOKIE_PARAMETERNOTFOUND(187, "FA", "Cannot find Parameter '%PARAM%' form Cookie '%COOKIE%'. Parameter do not exist or is not supported!", true, true, true, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETDIFFERENCESFROMXML(188, "FA", "Failed to compute differences from '%VALUE1%' and '%VALUE2%'", true, true, false, MessageGeneralEnum.EXECUTION_FA), - PROPERTY_FAILED_GETFROMJSON_PARAMETERNOTFOUND(190, "FA", "Value '%PARAM%' not found in Json. %ERROR%", true, true, true, MessageGeneralEnum.EXECUTION_FA), + PROPERTY_FAILED_GETFROMJSON(190, "FA", "Failed to get the value '%PARAM%' from JSON. %ERROR%", true, true, true, MessageGeneralEnum.EXECUTION_FA), + PROPERTY_FAILED_GETFROMJSON_PARAMETERNOTFOUND(190, "FA", "Value '%PARAM%' not found in JSON. %ERROR%", true, true, true, MessageGeneralEnum.EXECUTION_FA), + PROPERTY_FAILED_GETFROMJSON_INVALIDPATH(190, "FA", "The JSON path '%PATH%' is invalid.", true, true, true, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_CALCULATE_OBJECTPROPERTYNULL(191, "FA", "Both object and property are null. Please specify one of them.", true, false, false, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETFROMDATALIB_NOT_FOUND_ERROR(153, "FA", "The test data library entry %ITEM% is not available for the selected system %SYSTEM%, environment %ENVIRONMENT% and country %COUNTRY%.", true, false, false, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETFROMDATALIB_NOT_EXIST_ERROR(153, "FA", "The test data library entry %ITEM% do not exist.", true, false, false, MessageGeneralEnum.EXECUTION_FA), @@ -161,6 +163,7 @@ public enum MessageEventEnum { PROPERTY_FAILED_GETELEMENTPOSITION_EXCEPTION(904, "FA", "Position evaluation error: %REASON%", true, true, false, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_MISSINGJSONPATH(904, "FA", "JSONPath is mandatory when using GetFromNetworkTraffic property. Please specify it.", true, false, false, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_PROXYNOTACTIVE(904, "FA", "This type of property is only supported if robot executor (%ROBOT% - %EXECUTOR%) has proxy activated.", true, false, false, MessageGeneralEnum.EXECUTION_FA), + PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_INVALIDPATH(904, "FA", "The JSON path '%PATH%' is invalid.", true, true, true, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_PATHNOTFOUND(190, "FA", "JSON Path '%PARAM%' not found in Json.", true, true, true, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETFROMNETWORKTRAFFIC_EXCEPTION(904, "FA", "Failed to get property from Network Traffic Statistics. %DETAIL%", true, false, false, MessageGeneralEnum.EXECUTION_FA), PROPERTY_FAILED_GETOTP_MISSINGPARAMETER(191, "FA", "Unable to calculate the OTP. Missing secret key parameter.", true, false, false, MessageGeneralEnum.EXECUTION_FA), @@ -469,6 +472,7 @@ public enum MessageEventEnum { CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE(362, "FA", "Message type %TYPE% not supported for control '%CONTROL%'.", true, false, true, MessageGeneralEnum.EXECUTION_FA), CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION_AND_IDENTIFIER(384, "FA", "Control '%CONTROL%' is not supported for application type '%APPLICATIONTYPE% with Identifier %IDENTIFIER%'.", true, true, false, MessageGeneralEnum.EXECUTION_FA), CONTROL_FAILED_NOOBJECTINMEMORY(362, "FA", "Cannot perform the control because no successful Service/Page/Screen was accessed previously.", true, false, true, MessageGeneralEnum.EXECUTION_FA), + CONTROL_FAILED_ELEMENT_INVALIDJSONPATH(367, "FA", "The JSON path '%PATH%' is invalid. %ERROR%", true, false, true, MessageGeneralEnum.EXECUTION_FA), CONTROL_FAILED_GENERIC(362, "FA", "Cannot perform the control. %ERROR%", true, false, true, MessageGeneralEnum.EXECUTION_FA), CONTROL_WAITINGEXECUTION(398, "WE", "", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), CONTROL_PENDING(399, "PE", "Control beeing performed...", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), @@ -496,6 +500,7 @@ public enum MessageEventEnum { CONDITIONEVAL_FAILED_IFNUMERIC_GENERICCONVERSIONERROR(1230, "FA", "Cannot convert %STRINGVALUE% to numeric.", false, false, false, MessageGeneralEnum.EXECUTION_FA_CONDITION), CONDITIONEVAL_FAILED_SELENIUM_CONNECTIVITY(1230, "FA", "Lost connection to Selenium Server! Detailed error : %ERROR%.", false, false, false, MessageGeneralEnum.EXECUTION_FA_CONDITION), CONDITIONEVAL_FAILED_GENERIC(1230, "FA", "Failed to evaluate condition. Detailed error : %ERROR%.", false, false, false, MessageGeneralEnum.EXECUTION_FA_CONDITION), + CONDITIONEVAL_FAILED_INVALIDJSONPATH(1230, "FA", "The JSON path '%PATH%' is invalid.", false, false, false, MessageGeneralEnum.EXECUTION_FA_CONDITION), CONDITIONEVAL_FAILED_TEXTINELEMENT(1240, "FA", "%ERRORMESS%", true, true, true, MessageGeneralEnum.EXECUTION_KO), CONDITIONEVAL_FAILED_TEXTNOTINELEMENT(1240, "FA", "%ERRORMESS%", true, true, true, MessageGeneralEnum.EXECUTION_KO), CONDITIONEVAL_FALSE_NEVER(1210, "NA", "", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED), diff --git a/source/src/main/java/org/cerberus/core/service/json/impl/JsonService.java b/source/src/main/java/org/cerberus/core/service/json/impl/JsonService.java index 8c4de41ef6..a591f9699a 100644 --- a/source/src/main/java/org/cerberus/core/service/json/impl/JsonService.java +++ b/source/src/main/java/org/cerberus/core/service/json/impl/JsonService.java @@ -25,6 +25,7 @@ import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.PathNotFoundException; import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider; import net.minidev.json.JSONArray; import net.minidev.json.JSONStyle; @@ -78,14 +79,14 @@ public String callUrlAndGetJsonResponse(String url) throws MalformedURLException * @param jsonMessage JSON Content * @param url URL of the Json file to parse * @param attributeToFind The path of the searched element - * @return Value of the element from the Json File or null if the element is + * @return Value of the element from the Json File or PathNotFoundException if the element is * not found. */ @Override public String getFromJson(String jsonMessage, String url, String attributeToFind) throws InvalidPathException { if (attributeToFind == null) { LOG.warn("Null argument"); - return DEFAULT_GET_FROM_JSON_VALUE; + throw new InvalidPathException(); } //Get the Json File in string format @@ -103,8 +104,14 @@ public String getFromJson(String jsonMessage, String url, String attributeToFind //Get the value Object document = Configuration.defaultConfiguration().jsonProvider().parse(json); String jsonPath = checkJsonPathFormat(attributeToFind); - - return castObjectAccordingToJson(JsonPath.read(document, jsonPath)); + Object value = JsonPath.read(document, jsonPath); + //Value "null" in the json. + if (value == null) value = "null"; + //When using $.. syntax, it will return an empty array [] in case of no element found + if (value.toString().equals("[]")) { + throw new PathNotFoundException(); + } + return castObjectAccordingToJson(value); } /** @@ -120,7 +127,7 @@ public String getRawFromJson(String jsonMessage, String attributeToFind) throws String jsonPath = checkJsonPathFormat(attributeToFind); ObjectMapper objectMapper = new ObjectMapper(); - //Exception InavlidPathException throwed by read method when not elements found + //Exception PathNotFoundException throwed by read method when not elements found JsonNode jsonElementsSearched = JsonPath.using( Configuration .defaultConfiguration()