提交 65d8366c authored 作者: 黄夏豪's avatar 黄夏豪

fix(base): 修改了checkpoint的返回结构

上级 4d9fd87f
......@@ -79,6 +79,7 @@ public class CheckPointActuator implements Actuator {
}
public CheckPointResultDetail jsonPathCheck(String jsonPathCheckPoint, Object jsonObject) {
String beforeExpression = jsonPathCheckPoint;
String expression = jsonPathCheckPoint;
try {
expression = CompleteExpressionUtil.completeDynamicVariable(
......@@ -94,12 +95,18 @@ public class CheckPointActuator implements Actuator {
} catch (IllegalArgumentException e) {
return new CheckPointResultDetail(
false,
CheckPointType.JSONPATH_CHECK,
expression,
"解析失败",
String.format("JsonPath检查点,检查未通过,不能填写空的JsonPath,错误的jsonpath为:%s", jsonPathCheckPoint)
);
} catch (PathNotFoundException e) {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,jsonpath取不到任何值,错误的path为:%s,http返回值为: %s", e.getMessage().substring(e.getMessage().indexOf(":")), jsonObject.toString())
CheckPointType.JSONPATH_CHECK,
expression,
"解析失败",
String.format("JsonPath检查点,检查未通过,jsonpath取不到任何值,错误的jsonpath为:%s", e.getMessage().substring(e.getMessage().indexOf(":")))
);
}
try {
......@@ -109,18 +116,27 @@ public class CheckPointActuator implements Actuator {
if ((Boolean) eval) {
return new CheckPointResultDetail(
true,
String.format("JsonPath检查点,检查通过,表达式为:%s", expression)
CheckPointType.JSONPATH_CHECK,
beforeExpression,
expression,
String.format("JsonPath检查点,检查通过" )
);
} else {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,计算结果为false,错误的表达式为:%s", expression)
CheckPointType.JSONPATH_CHECK,
beforeExpression,
expression,
String.format("JsonPath检查点,检查未通过,计算结果为false")
);
}
} else {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型,错误的表达式为:%s", expression)
CheckPointType.JSONPATH_CHECK,
beforeExpression,
expression,
String.format("JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型")
);
}
} catch (ScriptException e) {
......@@ -137,22 +153,28 @@ public class CheckPointActuator implements Actuator {
public List<CheckPointResultDetail> containCheck(String containCheckPoint, String responseBody) {
List<CheckPointResultDetail> resultDetails = new ArrayList<>();
containCheckPoint = CompleteExpressionUtil.completeDynamicVariable(
containCheckPoint,
envId,
projectId)
;
String[] split = containCheckPoint.split(",");
for (String containExpression : split) {
if (responseBody.contains(containExpression)) {
String parseContainExpression = CompleteExpressionUtil.completeDynamicVariable(
containExpression,
envId,
projectId)
;
if (!StringUtils.isEmpty(responseBody)&&responseBody.contains(parseContainExpression)) {
resultDetails.add(new CheckPointResultDetail(
true,
CheckPointType.CONTAIN_CHECK,
containExpression,
parseContainExpression,
"包含检查点,检查通过"
));
} else {
resultDetails.add(new CheckPointResultDetail(
false,
String.format("包含检查点,检查未通过,结果:%s 中,不包含值:%s", responseBody, containExpression)
CheckPointType.CONTAIN_CHECK,
containExpression,
parseContainExpression,
String.format("包含检查点,检查未通过,不包含目标值")
));
}
}
......@@ -161,21 +183,27 @@ public class CheckPointActuator implements Actuator {
public List<CheckPointResultDetail> noContainCheck(String noContainCheckPoint, String responseBody) {
List<CheckPointResultDetail> resultDetails = new ArrayList<>();
noContainCheckPoint = CompleteExpressionUtil.completeDynamicVariable(
noContainCheckPoint,
envId,
projectId)
;
String[] split = noContainCheckPoint.split(",");
for (String containExpression : split) {
if (responseBody.contains(noContainCheckPoint)) {
for (String noContainExpression : split) {
String parseNoContainExpression = CompleteExpressionUtil.completeDynamicVariable(
noContainExpression,
envId,
projectId)
;
if (!StringUtils.isEmpty(responseBody)&&responseBody.contains(parseNoContainExpression)) {
resultDetails.add( new CheckPointResultDetail(
false,
String.format("不包含检查点,检查未通过,结果:%s 中,包含值:%s", responseBody, noContainCheckPoint)
CheckPointType.NO_CONTAIN_CHECK,
noContainExpression,
parseNoContainExpression,
String.format("不包含检查点,检查未通过" )
));
} else {
resultDetails.add( new CheckPointResultDetail(
true,
CheckPointType.NO_CONTAIN_CHECK,
noContainExpression,
parseNoContainExpression,
"不包含检查点,检查通过"
));
}
......@@ -203,11 +231,17 @@ public class CheckPointActuator implements Actuator {
if (isNull) {
return new CheckPointResultDetail(
false,
CheckPointType.NULL_CHECK,
"",
"",
String.format("不为空检查点,检查未通过,请求结果:%s", responseBody)
);
} else {
return new CheckPointResultDetail(
true,
CheckPointType.NULL_CHECK,
"",
"",
"不为空检查点,检查通过"
);
}
......@@ -215,15 +249,21 @@ public class CheckPointActuator implements Actuator {
public CheckPointResultDetail exceptionCheck(HttpResponseDetail httpResponseDetail) {
if (httpResponseDetail.getStatusCode().value() == HttpStatus.OK.value()) {
if (httpResponseDetail.getStatusCode()!=null && httpResponseDetail.getStatusCode().value() == HttpStatus.OK.value()) {
return new CheckPointResultDetail(
true,
CheckPointType.EXCEPTION_CHECK,
"",
"",
"异常检查点,检查通过"
);
} else {
return new CheckPointResultDetail(
true,
String.format("异常检查点,检查未通过,Http请求错误,错误码:%d,请求结果:%s", httpResponseDetail.getStatusCode().value(), httpResponseDetail.getResponseBody())
CheckPointType.EXCEPTION_CHECK,
"",
"",
String.format("异常检查点,检查未通过,Http请求错误,错误码:%d,请求结果:%s", httpResponseDetail.getStatusCode()==null?0:httpResponseDetail.getStatusCode().value(), httpResponseDetail.getResponseBody())
);
}
}
......
......@@ -15,6 +15,12 @@ public class CheckPointResultDetail {
private Boolean isSuccess;
private CheckPointType type;
private String matchExpression;
private String parseExpression;
private String message;
......
package org.matrix.actuators.checkpoint;
/**
* @author huangxiahao
*/
public enum CheckPointType {
/**
* 异常检查点
*/
EXCEPTION_CHECK(1,"异常检查点"),
NULL_CHECK(2,"空值检查点"),
CONTAIN_CHECK(3,"包含检查点"),
NO_CONTAIN_CHECK(4,"不包含检查点"),
JSONPATH_CHECK(5,"Jsonpath检查点")
;
private int typeCode;
private String name;
CheckPointType(int typeCode, String name) {
this.typeCode = typeCode;
this.name = name;
}
public Integer getTypeCode() {
return typeCode;
}
public void setTypeCode(Integer typeCode) {
this.typeCode = typeCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
......@@ -2,6 +2,7 @@ package org.matrix.actuators.httpclient;
import io.netty.handler.codec.http.HttpHeaderValues;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.HttpHostConnectException;
import org.matrix.actuators.Actuator;
import org.matrix.actuators.util.CompleteExpressionUtil;
import org.matrix.config.HttpRequestConfig;
......@@ -147,8 +148,15 @@ public class HttpClientActuator implements Actuator {
HttpStatus.valueOf(response.getStatusLine().getStatusCode()),
endTime.getTime() - startTime.getTime()
);
}catch (HttpHostConnectException e){
Date endTime = new Date();
return new HttpResponseDetail(
response,
String.format("目标主机拒绝连接,本次请求URL: %s ",httpRequestDetail.getUrl()),
null,
endTime.getTime() - startTime.getTime()
);
} catch (IOException e) {
e.printStackTrace();
throw new HttpRequestException(String.format("解析返回值失败,本次请求详细参数如下: %s ",httpRequestDetail));
} finally {
//关闭请求request
......
......@@ -26,7 +26,7 @@ public class HttpResponseDetail extends BaseTestCaseResponseDetail {
private HttpStatus statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
@ApiModelProperty("响应时间,单位为 ms ")
private Long responseTime = 0l;
private Long responseTime = 0L;
}
......@@ -2,14 +2,10 @@ package org.matrix.actuators.sql;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.matrix.actuators.httpclient.HttpClientActuator;
import org.matrix.actuators.httpclient.HttpRequestDetail;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.actuators.usecase.CaseActuator;
import org.matrix.actuators.usecase.TestCaseExecuteResult;
import org.matrix.config.HttpRequestConfig;
import org.matrix.database.entity.TestCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
......@@ -36,6 +32,7 @@ class CaseActuatorTest {
" }\n" +
" ]\n" +
"}";
System.out.println(json);
TestCase testCase = new TestCase();
testCase.setName("name");
testCase.setType(1);
......@@ -43,7 +40,7 @@ class CaseActuatorTest {
testCase.setAbnormalCheckpoint(1);
testCase.setContainCheckpoint("张三,李四");
testCase.setNoContainCheckpoint("张三,李四");
testCase.setJsonpathCheckpoint("contains({$..category},${componentName}[0])");
testCase.setJsonpathCheckpoint("contains({$..category},'${componentName}[0]')");
TestCaseExecuteResult testCaseExecuteResult = httpClientActuator.executeTestCase(testCase);
System.out.println(testCaseExecuteResult);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论