refactor(接口测试): 增加jsonpath断言日志

This commit is contained in:
wxg0103 2023-06-29 11:36:00 +08:00 committed by wxg0103
parent 6a13105e66
commit 845f952331
1 changed files with 10 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package org.apache.jmeter.assertions;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import io.metersphere.utils.DocumentUtils; import io.metersphere.utils.DocumentUtils;
import io.metersphere.utils.LoggerUtil;
import net.minidev.json.JSONArray; import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject; import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue; import net.minidev.json.JSONValue;
@ -130,7 +131,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
private void doAssert(String jsonString) { private void doAssert(String jsonString) {
Object value = JsonPath.read(jsonString, getJsonPath()); Object value = JsonPath.read(jsonString, getJsonPath());
LoggerUtil.info("json断言第一次拿到的值: " + value.toString());
if (!isJsonValidationBool()) { if (!isJsonValidationBool()) {
if (value instanceof JSONArray) { if (value instanceof JSONArray) {
JSONArray arrayValue = (JSONArray) value; JSONArray arrayValue = (JSONArray) value;
@ -236,6 +237,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
private boolean isEquals(Object subj) { private boolean isEquals(Object subj) {
String str = DocumentUtils.objectToString(subj, decimalFormatter); String str = DocumentUtils.objectToString(subj, decimalFormatter);
LoggerUtil.info("json断言拿到的值: " + str);
if (StringUtils.equals(str,KEY_PRE)) { if (StringUtils.equals(str,KEY_PRE)) {
return false; return false;
} }
@ -244,6 +246,7 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
return JMeterUtils.compilePattern(getExpectedValue()).matcher(str).matches(); return JMeterUtils.compilePattern(getExpectedValue()).matcher(str).matches();
} else { } else {
Pattern pattern = JMeterUtils.getPatternCache().getPattern(getExpectedValue()); Pattern pattern = JMeterUtils.getPatternCache().getPattern(getExpectedValue());
LoggerUtil.info("json断言正则表达式的结果: " + JMeterUtils.getMatcher().matches(str, pattern));
return JMeterUtils.getMatcher().matches(str, pattern); return JMeterUtils.getMatcher().matches(str, pattern);
} }
} else { } else {
@ -283,6 +286,9 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
try { try {
Number number1 = NumberUtils.createBigDecimal(v1); Number number1 = NumberUtils.createBigDecimal(v1);
Number number2 = NumberUtils.createBigDecimal(v2); Number number2 = NumberUtils.createBigDecimal(v2);
LoggerUtil.info("json断言的值 " + number1.toString());
LoggerUtil.info("期望的值 " + number2.toString());
LoggerUtil.info("json断言比较的结果: " + number1.equals(number2));
return number1.equals(number2); return number1.equals(number2);
} catch (Exception e) { } catch (Exception e) {
return StringUtils.equals(v1, v2); return StringUtils.equals(v1, v2);
@ -293,6 +299,9 @@ public class JSONPathAssertion extends AbstractTestElement implements Serializab
try { try {
Number number1 = NumberUtils.createBigDecimal(v1); Number number1 = NumberUtils.createBigDecimal(v1);
Number number2 = NumberUtils.createBigDecimal(v2); Number number2 = NumberUtils.createBigDecimal(v2);
LoggerUtil.info("json断言的值 " + number1.toString());
LoggerUtil.info("期望的值 " + number2.toString());
LoggerUtil.info("json断言比较的结果: " + !number1.equals(number2));
return !number1.equals(number2); return !number1.equals(number2);
} catch (Exception e) { } catch (Exception e) {
return !StringUtils.equals(v1, v2); return !StringUtils.equals(v1, v2);