Skip to content

Commit

Permalink
Add minidev dependency (linked to the upgrade of the jayway dependenc…
Browse files Browse the repository at this point in the history
…y the changed the scope of this dependency)

Add possibility to sum values retrieved with getFromHTML and getFromJSON
  • Loading branch information
bcivel committed Jan 2, 2025
1 parent 172aac5 commit 3626b15
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 21 deletions.
6 changes: 6 additions & 0 deletions source/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<jackson-datatype-jsr310.version>2.13.2</jackson-datatype-jsr310.version>
<!-- jsonpath -->
<jsonxpath.version>2.9.0</jsonxpath.version>
<minidev.version>2.5.1</minidev.version>
<!-- Groovy Version -->
<groovy.version>3.0.11</groovy.version>
<!-- <groovy-dateutil.version>3.0.11</groovy-dateutil.version>-->
Expand Down Expand Up @@ -312,6 +313,11 @@
<artifactId>json-path</artifactId>
<version>${jsonxpath.version}</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>${minidev.version}</version>
</dependency>

<!-- Kafka -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public class TestCaseCountryProperties {
public static final String VALUE3_RAW = "raw";
public static final String VALUE3_COORDINATE = "coordinate";
public static final String VALUE3_COUNT = "count";
public static final String VALUE3_VALUESUM = "valueSum";
public static final String VALUE3_ATTRIBUTE = "attribute";
public static final String VALUE3_VALUELIST = "valueList";
public static final String VALUE3_RAWLIST = "rawList";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ private AnswerItem<Boolean> evaluateCondition_ifElementPresent(String conditionO
LOG.debug("Checking if Element Present - JSON");
}
try {
if (jsonService.getFromJson(responseBody, null, conditionValue1, false, 0, TestCaseCountryProperties.VALUE3_VALUELIST) != null) {
if (jsonService.getFromJson(tCExecution, responseBody, null, conditionValue1, false, 0, TestCaseCountryProperties.VALUE3_VALUELIST) != null) {
conditionResult = true;
mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_TRUE_IFELEMENTPRESENT);
mes.setDescription(mes.getDescription().replace("%ELEMENT%", conditionValue1));
Expand Down Expand Up @@ -703,7 +703,7 @@ private AnswerItem<Boolean> evaluateCondition_ifElementNotPresent(String conditi

case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON:
try {
if (jsonService.getFromJson(responseBody, null, conditionValue1, false, 0, TestCaseCountryProperties.VALUE3_VALUELIST) == null) {
if (jsonService.getFromJson(tCExecution, responseBody, null, conditionValue1, false, 0, TestCaseCountryProperties.VALUE3_VALUELIST) == null) {
conditionResult = true;
mes = new MessageEvent(MessageEventEnum.CONDITIONEVAL_TRUE_IFELEMENTNOTPRESENT);
mes.setDescription(mes.getDescription().replace("%ELEMENT%", conditionValue1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ private MessageEvent verifyElementPresent(TestCaseExecution tCExecution, String
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, false, 0, TestCaseCountryProperties.VALUE3_VALUELIST).equals("[]")) {
if (!jsonService.getFromJson(tCExecution, responseBody, null, elementPath, false, 0, TestCaseCountryProperties.VALUE3_VALUELIST).equals("[]")) {
mes = new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_PRESENT);
mes.resolveDescription("STRING1", elementPath);
return mes;
Expand Down Expand Up @@ -888,7 +888,7 @@ private MessageEvent verifyElementNotPresent(TestCaseExecution execution, String
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,false, 0, TestCaseCountryProperties.VALUE3_VALUELIST).equals("[]")) {
if (!jsonService.getFromJson(execution, responseBody, null, elementPath,false, 0, TestCaseCountryProperties.VALUE3_VALUELIST).equals("[]")) {
mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_NOTPRESENT);
mes.resolveDescription("STRING1", elementPath);
return mes;
Expand Down Expand Up @@ -1259,7 +1259,7 @@ public MessageEvent verifyElementXXX(String control, TestCaseExecution tCExecuti

case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON: {
try {
actual = jsonService.getFromJson(responseBody, null, path,false, 0, TestCaseCountryProperties.VALUE3_VALUELIST);
actual = jsonService.getFromJson(tCExecution, responseBody, null, path,false, 0, TestCaseCountryProperties.VALUE3_VALUELIST);
} catch (Exception ex) {
mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC);
mes.resolveDescription("ERROR", ex.toString());
Expand Down Expand Up @@ -1473,7 +1473,7 @@ private MessageEvent verifyElementTextMatchRegex(TestCaseExecution tCExecution,

case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON:
try {
pathContent = jsonService.getFromJson(responseBody, null, path,false, 0, TestCaseCountryProperties.VALUE3_VALUELIST);
pathContent = jsonService.getFromJson(tCExecution, responseBody, null, path,false, 0, TestCaseCountryProperties.VALUE3_VALUELIST);
} catch (Exception ex) {
mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC);
mes.resolveDescription("ERROR", ex.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.File;
import java.io.IOException;

import com.jayway.jsonpath.JsonPath;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -1147,7 +1148,7 @@ private TestCaseExecutionData property_getFromNetworkTraffic(TestCaseExecutionDa
//Record result in filessytem.
testCaseExecutionData.addFileList(recorderService.recordProperty(execution.getId(), testCaseExecutionData.getProperty(), 1, harRes.toString(1), execution.getSecrets()));

String valueFromJson = this.jsonService.getFromJson(harRes.toString(), null, jsonPath,
String valueFromJson = this.jsonService.getFromJson(execution, harRes.toString(), null, jsonPath,
testCaseExecutionData.getNature().equals(TestCaseCountryProperties.NATURE_RANDOM), testCaseExecutionData.getRank(), testCaseExecutionData.getValue3());

if (valueFromJson != null) {
Expand Down Expand Up @@ -1235,7 +1236,7 @@ private TestCaseExecutionData property_getFromExecutionObject(TestCaseExecutionD
recorderService.recordProperty(tCExecution.getId(), testCaseExecutionData.getProperty(), 1, executionObject, tCExecution.getSecrets());

String valueFromJson = this.jsonService
.getFromJson(executionObject, null, testCaseExecutionData.getValue1(),
.getFromJson(tCExecution, executionObject, null, testCaseExecutionData.getValue1(),
testCaseExecutionData.getNature().equals(TestCaseCountryProperties.NATURE_RANDOM), testCaseExecutionData.getRank(), testCaseExecutionData.getValue3());

if (valueFromJson == null) {
Expand Down Expand Up @@ -1352,7 +1353,10 @@ private TestCaseExecutionData property_getFromHtml(TestCaseExecutionData testCas
valueFromHTML = this.webdriverService.getElements(tCExecution.getSession(), identifier);
break;
case (TestCaseCountryProperties.VALUE3_VALUELIST):
valueFromHTML = this.webdriverService.getElements(tCExecution.getSession(), identifier);
valueFromHTML = this.webdriverService.getElementsValues(tCExecution.getSession(), identifier);
break;
case (TestCaseCountryProperties.VALUE3_VALUESUM):
valueFromHTML = this.webdriverService.getElementsValuesSum(tCExecution, identifier);
break;
case (TestCaseCountryProperties.VALUE3_ATTRIBUTE):
valueFromHTML = this.webdriverService.getAttributeFromHtml(tCExecution.getSession(), identifier, testCaseExecutionData.getValue2(), TestCaseCountryProperties.NATURE_RANDOM.equals(testCaseExecutionData.getNature()), testCaseExecutionData.getRank());
Expand Down Expand Up @@ -1756,7 +1760,7 @@ private TestCaseExecutionData property_getFromJson(TestCaseExecutionData testCas
recorderService.recordProperty(execution.getId(), testCaseExecutionData.getProperty(), 1, jsonResponse, execution.getSecrets());

String valueFromJSON = this.jsonService
.getFromJson(jsonResponse, null, testCaseExecutionData.getValue1(),
.getFromJson(execution, jsonResponse, null, testCaseExecutionData.getValue1(),
testCaseExecutionData.getNature().equals(TestCaseCountryProperties.NATURE_RANDOM), testCaseExecutionData.getRank(), testCaseExecutionData.getValue3());

if (valueFromJSON == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.jayway.jsonpath.InvalidPathException;
import org.cerberus.core.crud.entity.TestCaseExecution;

import java.net.MalformedURLException;
import java.util.List;
Expand All @@ -43,7 +44,7 @@ public interface IJsonService {
* @return
* @throws java.lang.Exception
*/
String getFromJson(String jsonMessage, String url, String attribute, boolean random, Integer rank, String output) throws InvalidPathException, JsonProcessingException;
String getFromJson(TestCaseExecution testCaseExecution, String jsonMessage, String url, String attribute, boolean random, Integer rank, String output) throws InvalidPathException, JsonProcessingException;

/**
* @param jsonMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import net.minidev.json.JSONStyle;
import net.minidev.json.JSONArray;
import org.cerberus.core.crud.entity.TestCaseCountryProperties;
import org.cerberus.core.crud.entity.TestCaseExecution;
import org.cerberus.core.engine.entity.ExecutionLog;
import org.cerberus.core.service.json.IJsonService;
import org.cerberus.core.util.StringUtil;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
Expand All @@ -39,7 +44,6 @@
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import org.json.JSONArray;

/**
* @author bcivel
Expand Down Expand Up @@ -83,7 +87,7 @@ public String callUrlAndGetJsonResponse(String url) throws MalformedURLException
* not found.
*/
@Override
public String getFromJson(String jsonMessage, String url, String attributeToFind, boolean random, Integer rank, String output) throws InvalidPathException, JsonProcessingException {
public String getFromJson(TestCaseExecution testCaseExecution, String jsonMessage, String url, String attributeToFind, boolean random, Integer rank, String output) throws InvalidPathException, JsonProcessingException {
if (attributeToFind == null) {
LOG.warn("Null argument");
return DEFAULT_GET_FROM_JSON_VALUE;
Expand All @@ -109,15 +113,29 @@ public String getFromJson(String jsonMessage, String url, String attributeToFind

switch (output) {
case (TestCaseCountryProperties.VALUE3_COUNT):
valueFromJSON = String.valueOf(((JSONArray) JsonPath.read(document, jsonPath)).length());
valueFromJSON = String.valueOf(((JSONArray) JsonPath.read(document, jsonPath)).size());
break;
case (TestCaseCountryProperties.VALUE3_VALUESUM):
JSONArray array = (JSONArray) JsonPath.read(document, jsonPath);
Double result = 0.0;
for (Object object : array){
String preparedString = StringUtil.prepareToNumeric(object.toString());
if (!StringUtil.isEmptyOrNull(preparedString)) {
result += Double.valueOf(preparedString);
testCaseExecution.addExecutionLog(ExecutionLog.STATUS_INFO, "[Property:GetFromJSON] : Adding ["+preparedString+"] from init value ["+object.toString()+"] to previous sum ["+ result+"].");
}else{
testCaseExecution.addExecutionLog(ExecutionLog.STATUS_INFO, "[Property:GetFromJSON] : Do not add empty value from init value ["+object.toString()+"] to previous sum ["+ result+"].");
}
}
valueFromJSON = result.toString();
break;
case (TestCaseCountryProperties.VALUE3_VALUELIST):
valueFromJSON = castObjectAccordingToJson(JsonPath.read(document, jsonPath));
break;
case (TestCaseCountryProperties.VALUE3_VALUE):
if (random) {
Random r = new Random();
rank = r.nextInt(((JSONArray) JsonPath.read(document, jsonPath)).length());
rank = r.nextInt(((JSONArray) JsonPath.read(document, jsonPath)).size());
}
valueFromJSON = ((JSONArray) JsonPath.read(document, jsonPath)).get(rank).toString();
break;
Expand Down Expand Up @@ -220,7 +238,7 @@ private String castObjectAccordingToJson(Object value) {
} else if (value instanceof Boolean) {
return ((Boolean) value).toString();
} else if (value instanceof JSONArray) {
return ((JSONArray) value).toString(1);
return ((JSONArray) value).toString(JSONStyle.LT_COMPRESS);
} else if (value instanceof Double) {
return ((Double) value).toString();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.cerberus.core.crud.entity.TestCaseExecution;
import org.cerberus.core.engine.entity.Identifier;
import org.cerberus.core.engine.entity.MessageEvent;
import org.cerberus.core.engine.entity.Session;
Expand All @@ -49,6 +51,8 @@ public interface IWebDriverService {

String getElementsValues(Session session, Identifier identifier);

String getElementsValuesSum(TestCaseExecution testCaseExecution, Identifier identifier);

String getAlertText(Session session);

String getValueFromJS(Session session, String script);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import mantu.lab.treematching.TreeMatcherResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.cerberus.core.crud.entity.TestCaseExecution;
import org.cerberus.core.crud.service.impl.ParameterService;
import org.cerberus.core.engine.entity.ExecutionLog;
import org.cerberus.core.engine.entity.Identifier;
import org.cerberus.core.engine.entity.MessageEvent;
import org.cerberus.core.engine.entity.Session;
Expand Down Expand Up @@ -578,6 +580,40 @@ public String getElementsValues(Session session, Identifier identifier) {
return result.toString();
}

@Override
public String getElementsValuesSum(TestCaseExecution testCaseExecution, Identifier identifier) {
WebDriver driver = testCaseExecution.getSession().getDriver();
Double resultSum = 0.0;

List<WebElement> elements = driver.findElements(this.getBy(identifier));
for (WebElement webElement : elements) {
String preparedString = "";
if (webElement != null) {
if (webElement.getTagName().equalsIgnoreCase("select")) {
Select select = (Select) webElement;
preparedString = StringUtil.prepareToNumeric(select.getFirstSelectedOption().getText());
if (!StringUtil.isEmptyOrNull(preparedString)) {
resultSum += Double.valueOf(preparedString);
testCaseExecution.addExecutionLog(ExecutionLog.STATUS_INFO, "[Property:GetFromHTML] : Adding ["+preparedString+"] from init value ["+select.getFirstSelectedOption().getText()+"] to previous sum ["+ resultSum+"].");
}
} else if (webElement.getTagName().equalsIgnoreCase("option") || webElement.getTagName().equalsIgnoreCase("input")) {
preparedString = StringUtil.prepareToNumeric(webElement.getAttribute("value"));
if (!StringUtil.isEmptyOrNull(preparedString)) {
resultSum += Double.valueOf(preparedString);
testCaseExecution.addExecutionLog(ExecutionLog.STATUS_INFO, "[Property:GetFromHTML] : Adding ["+preparedString+"] from init value ["+webElement.getAttribute("value")+"] to previous sum ["+ resultSum+"].");
}
} else {
preparedString = StringUtil.prepareToNumeric(webElement.getText());
if (!StringUtil.isEmptyOrNull(preparedString)) {
resultSum += Double.valueOf(preparedString);
testCaseExecution.addExecutionLog(ExecutionLog.STATUS_INFO, "[Property:GetFromHTML] : Adding ["+preparedString+"] from init value ["+webElement.getText()+"] to previous sum ["+ resultSum+"].");
}
}
}
}
return resultSum.toString();
}

@Override
public String getValueFromHTML(Session session, Identifier identifier, boolean random, Integer rank) {
AnswerItem answer = this.getWebElement(session, identifier, random, rank);
Expand Down
3 changes: 2 additions & 1 deletion source/src/main/webapp/js/testcase/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ function displayPropertyHelper(elementLine, property){
}
if (informationForHelper.indexOf("nature")!==-1){
// Do not use nature 3 if value is count, valueList or rawList
if (property.value3 === "count" || property.value3 === "valueList" || property.value3 === "rawList") {
if (property.value3 === "count" || property.value3 === "valueList" || property.value3 === "valueSum" || property.value3 === "rawList") {
} else {
helperString += "-" + property.nature;
}
Expand All @@ -701,6 +701,7 @@ function displayPropertyHelper(elementLine, property){
if (informationForHelper.indexOf("rank")!==-1){
if (property.value3 !== "count" &&
property.value3 !== "valueList" &&
property.value3 !== "valueSum" &&
property.value3 !== "rawList" &&
property.nature !== 'RANDOM') {
helperString += property.rank !== 0 ? "-x" : "-0";
Expand Down
Loading

0 comments on commit 3626b15

Please sign in to comment.