提交 826e8fa5 authored 作者: 黄夏豪's avatar 黄夏豪

refactor(base): 优化!使代码更简洁了

上级 fb244ef8
......@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import org.apache.commons.io.IOUtils;
......@@ -21,6 +22,8 @@ import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -122,21 +125,35 @@ public class CheckPointActuator implements Actuator {
return jsEngine;
}
/**
*
* @param jsonObject 这个Object 是由 Configuration.defaultConfiguration().jsonProvider().parse(“json字符串”); 这个方法转换过来的 请不要乱传
* @return
*/
public String completeJsonPathExpression(JsonPathCheckPoint jsonPathCheckPoint, Object jsonObject) {
String result = jsonPathCheckPoint.getExpression();
//todo 李迪凡 先将result中的动态变量都还原出来 (先后顺序不能乱)得先还原动态变量
//将jsonPath都还原出来
String regex = "(?=\\{)(.*?)(?<=})";
String jsonPathRegex = "\\{(\\s)*\\$.*?}";
result = regexExpression(result, jsonPathRegex, jsonObject, (o, s) -> JSON.toJSONString(JsonPath.read(o, s.substring(1, s.length() - 1))));
return result;
}
public static String regexExpression(String expression, String regex,Object sourceContent, BiFunction<Object,String,String> biFunction){
String result = expression;
Pattern pattern = Pattern.compile(regex);
Matcher mat = pattern.matcher(result);
while (mat.find()) {
String group = mat.group();
String read = JSON.toJSONString(JsonPath.read(jsonObject, group.substring(1, group.length() - 1)));
String read = biFunction.apply(sourceContent,group);
result = mat.replaceAll(read);
}
return result;
}
public CheckPointResultDetail containCheck(ContainCheckPoint containCheckPoint, String responseBody) {
if (responseBody.contains(containCheckPoint.getValue())) {
return new CheckPointResultDetail(
......@@ -205,9 +222,13 @@ public class CheckPointActuator implements Actuator {
} else {
return new CheckPointResultDetail(
true,
String.format("异常检查点,检查未通过,Http请求错误,错误码:%d,请求结果:%s", httpResponseDetail.getStatusCode(), httpResponseDetail.getResponseBody())
String.format("异常检查点,检查未通过,Http请求错误,错误码:%d,请求结果:%s", httpResponseDetail.getStatusCode().value(), httpResponseDetail.getResponseBody())
);
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论