Skip to content

Commit

Permalink
fix: json service not handled null values (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucashimpens committed Sep 12, 2023
1 parent eaab4a5 commit 741b5a8
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -468,18 +465,21 @@ private AnswerItem<Boolean> 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;

Expand Down Expand Up @@ -621,15 +621,18 @@ private AnswerItem<Boolean> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 741b5a8

Please sign in to comment.