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

feat(base): 将Http执行器和testCase执行器和checkPoint执行器做成的单例模式

上级 5747fd51
......@@ -14,8 +14,10 @@ import org.matrix.actuators.Actuator;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.actuators.util.CompleteExpressionUtil;
import org.matrix.exception.CheckPointException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
......@@ -31,27 +33,24 @@ import java.util.List;
*
* @author huangxiahao
*/
@Component
public class CheckPointActuator implements Actuator {
private final String baseJs;
@Value("baseJs")
private String baseJs;
private Long projectId;
private Long envId;
public CheckPointActuator(String baseJsPath, Long env, Long projectId) {
ClassPathResource cpr = new ClassPathResource(baseJsPath);
public CheckPointActuator() {
ClassPathResource cpr = new ClassPathResource(baseJs);
try {
this.baseJs = IOUtils.toString(cpr.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new CheckPointException("初始JS加载失败");
}
this.projectId = projectId;
this.envId = env;
}
public CheckPointResult httpCheck(HttpResponseDetail httpResponseDetail, CheckPoint checkPoint) {
public CheckPointResult httpCheck(HttpResponseDetail httpResponseDetail, CheckPoint checkPoint,Long envId,Long projectId) {
CheckPointResult checkPointResult = new CheckPointResult();
//根据checkPoint里的细节数据开始检测
//异常检查点检测
......@@ -64,22 +63,21 @@ public class CheckPointActuator implements Actuator {
}
//包含检查点检测
if (!StringUtils.isEmpty(checkPoint.getContainCheckPoint())){
checkPointResult.addCheckPointResultDetail(containCheck(checkPoint.getContainCheckPoint(), httpResponseDetail.getResponseBody()));
checkPointResult.addCheckPointResultDetail(containCheck(checkPoint.getContainCheckPoint(), httpResponseDetail.getResponseBody(),envId,projectId));
}
//不包含检查点检测
if (!StringUtils.isEmpty(checkPoint.getNoContainCheckPoint())){
checkPointResult.addCheckPointResultDetail(noContainCheck(checkPoint.getNoContainCheckPoint(), httpResponseDetail.getResponseBody()));
checkPointResult.addCheckPointResultDetail(noContainCheck(checkPoint.getNoContainCheckPoint(), httpResponseDetail.getResponseBody(),envId,projectId));
}
//JsonPath检查点检测
if (!StringUtils.isEmpty(checkPoint.getJsonPathCheckPoint())) {
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(httpResponseDetail.getResponseBody());
checkPointResult.addCheckPointResultDetail(jsonPathCheck(checkPoint.getJsonPathCheckPoint(), jsonObject));
checkPointResult.addCheckPointResultDetail(jsonPathCheck(checkPoint.getJsonPathCheckPoint(), jsonObject,envId,projectId));
}
return checkPointResult;
}
public CheckPointResultDetail jsonPathCheck(String jsonPathCheckPoint, Object jsonObject) {
String beforeExpression = jsonPathCheckPoint;
public CheckPointResultDetail jsonPathCheck(String jsonPathCheckPoint, Object jsonObject,Long envId,Long projectId) {
String expression = jsonPathCheckPoint;
try {
expression = CompleteExpressionUtil.completeDynamicVariable(
......@@ -117,26 +115,26 @@ public class CheckPointActuator implements Actuator {
return new CheckPointResultDetail(
true,
CheckPointType.JSONPATH_CHECK,
beforeExpression,
jsonPathCheckPoint,
expression,
String.format("JsonPath检查点,检查通过" )
"JsonPath检查点,检查通过"
);
} else {
return new CheckPointResultDetail(
false,
CheckPointType.JSONPATH_CHECK,
beforeExpression,
jsonPathCheckPoint,
expression,
String.format("JsonPath检查点,检查未通过,计算结果为false")
"JsonPath检查点,检查未通过,计算结果为false"
);
}
} else {
return new CheckPointResultDetail(
false,
CheckPointType.JSONPATH_CHECK,
beforeExpression,
jsonPathCheckPoint,
expression,
String.format("JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型")
"JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型"
);
}
} catch (ScriptException e) {
......@@ -151,7 +149,7 @@ public class CheckPointActuator implements Actuator {
}
public List<CheckPointResultDetail> containCheck(String containCheckPoint, String responseBody) {
public List<CheckPointResultDetail> containCheck(String containCheckPoint, String responseBody,Long envId,Long projectId) {
List<CheckPointResultDetail> resultDetails = new ArrayList<>();
String[] split = containCheckPoint.split(",");
for (String containExpression : split) {
......@@ -174,14 +172,14 @@ public class CheckPointActuator implements Actuator {
CheckPointType.CONTAIN_CHECK,
containExpression,
parseContainExpression,
String.format("包含检查点,检查未通过,不包含目标值")
"包含检查点,检查未通过,不包含目标值"
));
}
}
return resultDetails;
}
public List<CheckPointResultDetail> noContainCheck(String noContainCheckPoint, String responseBody) {
public List<CheckPointResultDetail> noContainCheck(String noContainCheckPoint, String responseBody,Long envId,Long projectId) {
List<CheckPointResultDetail> resultDetails = new ArrayList<>();
String[] split = noContainCheckPoint.split(",");
for (String noContainExpression : split) {
......@@ -196,7 +194,7 @@ public class CheckPointActuator implements Actuator {
CheckPointType.NO_CONTAIN_CHECK,
noContainExpression,
parseNoContainExpression,
String.format("不包含检查点,检查未通过" )
"不包含检查点,检查未通过"
));
} else {
resultDetails.add( new CheckPointResultDetail(
......
......@@ -22,8 +22,10 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.matrix.exception.HttpRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
......@@ -42,41 +44,20 @@ import java.util.List;
* todo 打印LOG
* @author HuangXiahao
**/
@Component
public class HttpClientActuator implements Actuator {
HttpRequestConfig config;
CookieStore cookieStore;
final
CloseableHttpClient client;
private final Long projectId;
private final Long envId;
public HttpClientActuator(HttpRequestConfig config, Long env, Long projectId) {
this.config = config;
this.cookieStore = config.cookieStore();
try {
this.client = config.client();
} catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
this.projectId = projectId;
this.envId = env;
}
public CloseableHttpClient getClient() {
return client;
public HttpClientActuator(CloseableHttpClient client) {
this.client = client;
}
/**
* 初始化请求内数据并补全动态变量
*/
private void completeHttpRequestDetail(HttpRequestDetail httpRequestDetail){
private void completeHttpRequestDetail(HttpRequestDetail httpRequestDetail,Long envId,Long projectId){
for (RequestHeader header : httpRequestDetail.getHeaders()) {
//todo 李迪凡 将header中的key和value里的 动态变量补全
//获取动态变量,并将动态变量替换进去
......@@ -128,8 +109,8 @@ public class HttpClientActuator implements Actuator {
* 发起Http请求
*
*/
public HttpResponseDetail sendHttpRequest(HttpRequestDetail httpRequestDetail) {
completeHttpRequestDetail(httpRequestDetail);
public HttpResponseDetail sendHttpRequest(HttpRequestDetail httpRequestDetail,Long envId,Long projectId) {
completeHttpRequestDetail(httpRequestDetail,envId,projectId);
CloseableHttpResponse response = null;
Date startTime = new Date();
String url = initUrlString(httpRequestDetail);
......@@ -140,7 +121,7 @@ public class HttpClientActuator implements Actuator {
}
requestBase.setHeaders(httpRequestDetail.getHeadersArray());
try {
response = getClient().execute(requestBase);
response = client.execute(requestBase);
Date endTime = new Date();
return new HttpResponseDetail(
response,
......
......@@ -11,6 +11,8 @@ import org.matrix.actuators.checkpoint.CheckPoint;
import org.matrix.actuators.checkpoint.CheckPointResult;
import org.matrix.actuators.httpclient.HttpRequestDetail;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 测试用例执行器
......@@ -18,41 +20,36 @@ import org.matrix.actuators.httpclient.HttpResponseDetail;
*
* @author huangxiahao
*/
@Component
public class CaseActuator implements Actuator {
private Long projectId;
private final CheckPointActuator checkPointActuator;
private Long envId;
private CheckPointActuator checkPointActuator;
private HttpClientActuator httpClientActuator;
private final HttpClientActuator httpClientActuator;
private final String baseJsPath = "syntaxCheck.js";
public CaseActuator(Long envId, Long projectId) {
this.projectId = projectId;
this.envId = envId;
checkPointActuator = new CheckPointActuator(baseJsPath, envId, projectId);
httpClientActuator = new HttpClientActuator(new HttpRequestConfig(), envId, projectId);
public CaseActuator(CheckPointActuator checkPointActuator, HttpClientActuator httpClientActuator) {
this.checkPointActuator = checkPointActuator;
this.httpClientActuator = httpClientActuator;
}
/**
* 执行测试用例
*/
public TestCaseExecuteResult executeTestCase(TestCase testCase) {
public TestCaseExecuteResult executeTestCase(TestCase testCase,Long envId,Long projectId) {
//todo 李迪凡 执行前置动作
//执行测试用例的本体内容
BaseTestCaseResponseDetail baseTestCaseResponseDetail = null;
HttpResponseDetail baseTestCaseResponseDetail = null;
if (testCase.getType().equals(TestCaseTypeEnum.HTTP.getValue())) {
HttpRequestDetail httpRequestDetail = JSON.parseObject(testCase.getDetail(), HttpRequestDetail.class);
baseTestCaseResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail);
baseTestCaseResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail,envId,projectId);
}
//todo 李迪凡 执行测试后动作
//进行检验
CheckPointResult checkPointResult = null;
if (testCase.getType().equals(TestCaseTypeEnum.HTTP.getValue())) {
checkPointResult = checkPointActuator.httpCheck((HttpResponseDetail) baseTestCaseResponseDetail, getCheckPointEntity(testCase));
checkPointResult = checkPointActuator.httpCheck(baseTestCaseResponseDetail, getCheckPointEntity(testCase),envId,projectId);
}
//todo 李迪凡 执行后置动作
return new TestCaseExecuteResult(baseTestCaseResponseDetail, checkPointResult);
......
package org.matrix.config;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
......@@ -16,6 +17,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.matrix.exception.GlobalException;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
......@@ -24,26 +28,26 @@ import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;
/**
* httpClient配置
* @author huangxiahao
*/
@Component
public class HttpRequestConfig {
/**
* HttpClient的Cookie管理器,可在其他类中调用该Bean清空Cookie缓存。
* 主要针对某些网站Cookie多次使用会造成Cookie失效的问题
* @return
*/
public CookieStore cookieStore(){
CookieStore cookieStore = new BasicCookieStore();
return cookieStore;
return new BasicCookieStore();
}
/**
* HttpClient的配置,所返回的CloseableHttpClient可以用来发送网络请求。
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public CloseableHttpClient client() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
public CloseableHttpClient client() {
try {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] x509Certificates, String s) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
......@@ -80,6 +84,15 @@ public class HttpRequestConfig {
.disableRedirectHandling()
.setDefaultCookieStore(cookieStore())
.build();
}catch (Exception e){
throw new GlobalException("初始化HttpClient引擎失败");
}
}
@Bean
CloseableHttpClient getHttpClient() {
return client();
}
}
......@@ -14,3 +14,4 @@ spring:
mybatis-plus:
type-enums-package: org.matrix.enums
baseJsPath: syntaxCheck.js
\ No newline at end of file
......@@ -3,9 +3,11 @@ 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.usecase.CaseActuator;
import org.matrix.actuators.usecase.TestCaseExecuteResult;
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;
......@@ -13,13 +15,11 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
class CaseActuatorTest {
@Autowired
CaseActuator caseActuator;
@Test
void test(){
CaseActuator httpClientActuator = new CaseActuator(
1l,
1l
);
String json = "{\n" +
" \"url\":\"http://127.0.0.1:8080/test/sendMessage\",\n" +
" \"method\":\"GET\",\n" +
......@@ -47,8 +47,7 @@ class CaseActuatorTest {
testCase.setContainCheckpoint("张三,李四");
testCase.setNoContainCheckpoint("张三,李四");
testCase.setJsonpathCheckpoint("contains({$..category},'${componentName}[0]')");
TestCaseExecuteResult testCaseExecuteResult = httpClientActuator.executeTestCase(testCase);
TestCaseExecuteResult testCaseExecuteResult = caseActuator.executeTestCase(testCase,1L,1L);
System.out.println(testCaseExecuteResult);
......
......@@ -7,6 +7,7 @@ import org.matrix.actuators.httpclient.HttpClientActuator;
import org.matrix.actuators.httpclient.HttpRequestDetail;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.config.HttpRequestConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
......@@ -14,13 +15,12 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
class HttpClientActuatorTest {
@Autowired
HttpClientActuator httpClientActuator;
@Test
void test(){
HttpClientActuator httpClientActuator = new HttpClientActuator(
new HttpRequestConfig(),
1l,
1l
);
String json = "{\n" +
" \"url\": \"http://127.0.0.1:8080/test/tableName\",\n" +
" \"method\": \"GET\",\n" +
......@@ -35,7 +35,7 @@ class HttpClientActuatorTest {
"}";
HttpRequestDetail httpRequestDetail1 = JSON.parseObject(json, HttpRequestDetail.class);
HttpRequestDetail httpRequestDetail = JSON.parseObject(json, HttpRequestDetail.class);
HttpResponseDetail httpResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail);
HttpResponseDetail httpResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail,1l,1l);
System.out.println(httpResponseDetail);
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论