提交 98d885aa authored 作者: 黄夏豪's avatar 黄夏豪

feat: 新增了检查点的相关代码

上级 a06690ce
......@@ -190,6 +190,18 @@
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.9</version>
</dependency>
</dependencies>
......
package org.matrix.entity.checkPoint;
import lombok.Data;
import java.util.List;
/**
* 检验点
* @author Administrator
*/
@Data
public class CheckPoint {
/**
* 是否开启异常检测
*/
private Boolean useExceptionCheck = false;
/**
* 是否开启为空检测
*/
private Boolean useNullCheck = false;
/**
* 是否包含检测
*/
private List<ContainCheckPoint> containCheckPoints;
/**
* JsonPath检查点
*/
private List<JsonPathCheckPoint> jsonPathCheckPoints;
}
package org.matrix.entity.checkPoint;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* 检测结果
* @author Administrator
*/
@Data
public class CheckPointResult {
private List<CheckPointResultDetail> checkPointResultDetails = new ArrayList<>();
public Long getSuccessNum() {
return checkPointResultDetails.stream().filter(CheckPointResultDetail::getIsSuccess).count();
}
public Long getFailNum() {
return checkPointResultDetails.size() - getSuccessNum();
}
public Boolean addCheckPointResultDetail(CheckPointResultDetail checkPointResultDetail){
return checkPointResultDetails.add(checkPointResultDetail);
}
}
package org.matrix.entity.checkPoint;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 检测结果
* @author Administrator
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CheckPointResultDetail {
private Boolean isSuccess;
private String message;
}
package org.matrix.entity.checkPoint;
import lombok.Data;
/**
* 是否包含检查点
* @author Administrator
*/
@Data
public class ContainCheckPoint {
/**
* false为不包含检测,ture为包含检测
*/
private Boolean isContain = false;
private String value;
}
package org.matrix.entity.checkPoint;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Jsonpath检测点
* @author Administrator
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonPathCheckPoint {
private String expression;
}
package org.matrix.util;
import cn.hutool.script.ScriptUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.matrix.entity.checkPoint.*;
import org.matrix.entity.httpRequest.HttpResponseDetail;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 数据检测点工具
* todo 打印LOG
*
* @author Administrator
*/
public class CheckPointUtil {
private String baseJs;
CheckPointUtil(String path) {
ClassPathResource cpr = new ClassPathResource(path);
try {
this.baseJs = IOUtils.toString(cpr.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
//todo 抛出异常 初始JS加载失败
e.printStackTrace();
}
}
public void httpCheck(HttpResponseDetail httpResponseDetail, CheckPoint checkPoint) {
CheckPointResult checkPointResult = new CheckPointResult();
//根据checkPoint里的细节数据开始检测
//异常检查点检测
if (checkPoint.getUseExceptionCheck()) {
checkPointResult.addCheckPointResultDetail(exceptionCheck(httpResponseDetail));
}
//异常检查点检测
if (checkPoint.getUseNullCheck()) {
checkPointResult.addCheckPointResultDetail(nullCheck(httpResponseDetail.getResponseBody()));
}
//包含检查点检测
for (ContainCheckPoint containCheckPoint : checkPoint.getContainCheckPoints()) {
checkPointResult.addCheckPointResultDetail(containCheck(containCheckPoint, httpResponseDetail.getResponseBody()));
}
//todo 数据库参数检测
//JsonPath检查点检测
if (checkPoint.getJsonPathCheckPoints().size() > 0) {
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(httpResponseDetail.getResponseBody());
for (JsonPathCheckPoint jsonPathCheckPoint : checkPoint.getJsonPathCheckPoints()) {
checkPointResult.addCheckPointResultDetail(jsonPathCheck(jsonPathCheckPoint, jsonObject));
}
}
}
public CheckPointResultDetail jsonPathCheck(JsonPathCheckPoint jsonPathCheckPoint, Object jsonObject) {
String expression;
try {
expression = completeJsonPathExpression(jsonPathCheckPoint, jsonObject);
} catch (IllegalArgumentException e) {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,不能填写空的JsonPath,错误的jsonpath为:%s", jsonPathCheckPoint.getExpression())
);
} catch (PathNotFoundException e) {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,jsonpath取不到任何值,错误的path为:%s,http返回值为: %s", e.getMessage().substring(e.getMessage().indexOf(":")), jsonObject.toString())
);
}
try {
ScriptEngine jsEngine = getScriptEngine();
Object eval = jsEngine.eval(expression);
if (eval instanceof Boolean){
if ((Boolean) eval){
return new CheckPointResultDetail(
true,
String.format("JsonPath检查点,检查通过,表达式为:%s", expression)
);
}else {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,计算结果为false,错误的表达式为:%s", expression)
);
}
}else {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型,错误的表达式为:%s", expression)
);
}
} catch (ScriptException e) {
//todo 抛出异常初始化js引擎失败
e.printStackTrace();
}
return new CheckPointResultDetail(
false,
"JsonPath检查点,检查未通过,空的表达式"
);
}
public ScriptEngine getScriptEngine() throws ScriptException {
ScriptEngine jsEngine = ScriptUtil.createJsEngine();
jsEngine.eval(baseJs);
return jsEngine;
}
public String completeJsonPathExpression(JsonPathCheckPoint jsonPathCheckPoint, Object jsonObject) {
String result = jsonPathCheckPoint.getExpression();
//todo 先将动态变量都还原出来
//将jsonPath都还原出来
String regex = "(?=\\{)(.*?)(?<=})";
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)));
result = mat.replaceAll(read);
}
return result;
}
public static void main(String[] args) {
String json = "{\n" +
" \"school\": [\n" +
" {\n" +
" \"className\": \"一班\",\n" +
" \"student\":[\n" +
" \"一班的张三\",\"一班的李四\",\"一班的王五\",\"一班的赵六\",\"一班的田七\"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"className\": \"二班\",\n" +
" \"student\":[\n" +
" \"二班的张三\",\"二班的李四\",\"二班的王五\",\"二班的赵六\",\"二班的田七\"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"className\": \"三班\",\n" +
" \"student\":[\n" +
" \"三班的张三\",\"三班的李四\",\"三班的王五\",\"三班的赵六\",\"三班的田七\"\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(json);
CheckPointUtil checkPointUtil = new CheckPointUtil("test.js");
CheckPointResultDetail checkPointResultDetail = checkPointUtil.jsonPathCheck(new JsonPathCheckPoint(
"contains({$..className},\"一班\")"
),
jsonObject);
System.out.println(checkPointResultDetail);
}
public CheckPointResultDetail containCheck(ContainCheckPoint containCheckPoint, String responseBody) {
if (containCheckPoint.getIsContain()) {
if (responseBody.contains(containCheckPoint.getValue())) {
return new CheckPointResultDetail(
true,
"包含检查点,检查通过"
);
} else {
return new CheckPointResultDetail(
false,
String.format("包含检查点,检查未通过,结果:%s 中,不包含值:%s", responseBody, containCheckPoint.getValue())
);
}
} else {
if (responseBody.contains(containCheckPoint.getValue())) {
return new CheckPointResultDetail(
false,
String.format("不包含检查点,检查未通过,结果:%s 中,包含值:%s", responseBody, containCheckPoint.getValue())
);
} else {
return new CheckPointResultDetail(
true,
"不包含检查点,检查通过"
);
}
}
}
public CheckPointResultDetail nullCheck(String responseBody) {
boolean isNull = false;
if (StringUtils.isBlank(responseBody)) {
isNull = true;
} else {
try {
JSONObject jsonObject = JSON.parseObject(responseBody);
isNull = jsonObject.isEmpty();
} catch (JSONException ignored) {
}
try {
JSONArray jsonArray = JSON.parseArray(responseBody);
isNull = jsonArray.isEmpty();
} catch (JSONException ignored) {
}
}
if (isNull) {
return new CheckPointResultDetail(
false,
String.format("不为空检查点,检查未通过,请求结果:%s", responseBody)
);
} else {
return new CheckPointResultDetail(
true,
"异常检查点,检查通过"
);
}
}
public CheckPointResultDetail exceptionCheck(HttpResponseDetail httpResponseDetail) {
if (httpResponseDetail.getStatusCode() == HttpStatus.OK.value()) {
return new CheckPointResultDetail(
true,
"异常检查点,检查通过"
);
} else {
return new CheckPointResultDetail(
true,
String.format("异常检查点,检查未通过,Http请求错误,错误码:%d,请求结果:%s", httpResponseDetail.getStatusCode(), httpResponseDetail.getResponseBody())
);
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论