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

feat(base): 修复了socket调试无法使用的问题

上级 2a4b8912
...@@ -20,6 +20,7 @@ import org.apache.http.impl.client.CloseableHttpClient; ...@@ -20,6 +20,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.matrix.exception.HttpRequestException; import org.matrix.exception.HttpRequestException;
import org.matrix.socket.LogQueue;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -56,7 +57,6 @@ public class HttpClientActuator implements Actuator { ...@@ -56,7 +57,6 @@ public class HttpClientActuator implements Actuator {
*/ */
private void completeHttpRequestDetail(HttpRequestDetail httpRequestDetail,Long envId,Long projectId){ private void completeHttpRequestDetail(HttpRequestDetail httpRequestDetail,Long envId,Long projectId){
for (RequestHeader header : httpRequestDetail.getHeaders()) { for (RequestHeader header : httpRequestDetail.getHeaders()) {
//todo 李迪凡 将header中的key和value里的 动态变量补全
//获取动态变量,并将动态变量替换进去 //获取动态变量,并将动态变量替换进去
header.setName( header.setName(
completeExpressionUtil.completeDynamicVariable( completeExpressionUtil.completeDynamicVariable(
...@@ -107,6 +107,7 @@ public class HttpClientActuator implements Actuator { ...@@ -107,6 +107,7 @@ public class HttpClientActuator implements Actuator {
* *
*/ */
public HttpResponseDetail sendHttpRequest(HttpRequestDetail httpRequestDetail,Long envId,Long projectId) { public HttpResponseDetail sendHttpRequest(HttpRequestDetail httpRequestDetail,Long envId,Long projectId) {
LogQueue.add(Thread.currentThread().getId(),String.format("[HTTP执行] 目标URL :%S", httpRequestDetail.getUrl()));
completeHttpRequestDetail(httpRequestDetail,envId,projectId); completeHttpRequestDetail(httpRequestDetail,envId,projectId);
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
Date startTime = new Date(); Date startTime = new Date();
......
...@@ -13,7 +13,6 @@ import org.matrix.actuators.httpclient.HttpRequestDetail; ...@@ -13,7 +13,6 @@ import org.matrix.actuators.httpclient.HttpRequestDetail;
import org.matrix.actuators.httpclient.HttpResponseDetail; import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.actuators.usecase.CaseActuator; import org.matrix.actuators.usecase.CaseActuator;
import org.matrix.database.entity.DynamicVariable; import org.matrix.database.entity.DynamicVariable;
import org.matrix.database.entity.TestCase;
import org.matrix.database.entity.TestCaseBTO; import org.matrix.database.entity.TestCaseBTO;
import org.matrix.database.service.IConnectService; import org.matrix.database.service.IConnectService;
import org.matrix.database.service.IDynamicVariableService; import org.matrix.database.service.IDynamicVariableService;
...@@ -21,8 +20,6 @@ import org.matrix.database.service.ITestCaseService; ...@@ -21,8 +20,6 @@ import org.matrix.database.service.ITestCaseService;
import org.matrix.database.service.ITestDataService; import org.matrix.database.service.ITestDataService;
import org.matrix.enums.DynamicVarType; import org.matrix.enums.DynamicVarType;
import org.matrix.exception.GlobalException; import org.matrix.exception.GlobalException;
import org.matrix.socket.LogQueue;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
......
...@@ -3,9 +3,8 @@ package org.matrix.actuators.usecase; ...@@ -3,9 +3,8 @@ package org.matrix.actuators.usecase;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import org.matrix.actuators.Actuator; import org.matrix.actuators.Actuator;
import org.matrix.actuators.checkpoint.CheckPointActuator; import org.matrix.actuators.checkpoint.CheckPointActuator;
import org.matrix.actuators.checkpoint.JsonPathCheckPoint;
import org.matrix.actuators.httpclient.HttpClientActuator; import org.matrix.actuators.httpclient.HttpClientActuator;
import org.matrix.config.HttpRequestConfig; import org.matrix.actuators.util.ThreadUtil;
import org.matrix.database.entity.TestCase; import org.matrix.database.entity.TestCase;
import org.matrix.actuators.checkpoint.CheckPoint; import org.matrix.actuators.checkpoint.CheckPoint;
import org.matrix.actuators.checkpoint.CheckPointResult; import org.matrix.actuators.checkpoint.CheckPointResult;
...@@ -14,7 +13,6 @@ import org.matrix.actuators.httpclient.HttpResponseDetail; ...@@ -14,7 +13,6 @@ import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.database.entity.TestCaseBTO; import org.matrix.database.entity.TestCaseBTO;
import org.matrix.database.entity.TestData; import org.matrix.database.entity.TestData;
import org.matrix.socket.LogQueue; import org.matrix.socket.LogQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
...@@ -40,25 +38,32 @@ public class CaseActuator implements Actuator { ...@@ -40,25 +38,32 @@ public class CaseActuator implements Actuator {
/** /**
* 执行测试用例 * 执行测试用例
*/ */
public TestCaseExecuteResult executeTestCase(TestCaseBTO testCaseBTO, Long envId, Long projectId) { public TestCaseExecuteResult executeTestCase(TestCaseBTO testCaseBto, Long envId, Long projectId) {
LogQueue.add(Thread.currentThread().getId(),String.format("[用例解析器] 当前线程ID为: %S", Thread.currentThread().getId())); try {
//todo 李迪凡 执行前置动作 LogQueue.add(ThreadUtil.currentThreadId(),String.format("[用例解析器] 当前线程ID为: %S", ThreadUtil.currentThreadId()));
//执行测试用例的本体内容 //todo 李迪凡 执行前置动作
HttpResponseDetail baseTestCaseResponseDetail = null; //执行测试用例的本体内容
TestCase testCase = testCaseBTO.getTestCase(); HttpResponseDetail baseTestCaseResponseDetail = null;
TestData testData = testCaseBTO.getTestData(); TestCase testCase = testCaseBto.getTestCase();
if (testCase.getType().equals(TestCaseTypeEnum.HTTP.getValue())) { TestData testData = testCaseBto.getTestData();
HttpRequestDetail httpRequestDetail = JSON.parseObject(testData.getDetail(), HttpRequestDetail.class); if (testCase.getType().equals(TestCaseTypeEnum.HTTP.getValue())) {
baseTestCaseResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail,envId,projectId); HttpRequestDetail httpRequestDetail = JSON.parseObject(testData.getDetail(), HttpRequestDetail.class);
baseTestCaseResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail,envId,projectId);
}
//todo 李迪凡 执行测试后动作
//进行检验
CheckPointResult checkPointResult = null;
if (testCase.getType().equals(TestCaseTypeEnum.HTTP.getValue())) {
checkPointResult = checkPointActuator.httpCheck(baseTestCaseResponseDetail, getCheckPointEntity(testData),envId,projectId);
}
//todo 李迪凡 执行后置动作
return new TestCaseExecuteResult(baseTestCaseResponseDetail, checkPointResult);
} catch (Exception e){
throw e;
}finally {
LogQueue.remove(ThreadUtil.currentThreadId());
} }
//todo 李迪凡 执行测试后动作
//进行检验
CheckPointResult checkPointResult = null;
if (testCase.getType().equals(TestCaseTypeEnum.HTTP.getValue())) {
checkPointResult = checkPointActuator.httpCheck(baseTestCaseResponseDetail, getCheckPointEntity(testData),envId,projectId);
}
//todo 李迪凡 执行后置动作
return new TestCaseExecuteResult(baseTestCaseResponseDetail, checkPointResult);
} }
private CheckPoint getCheckPointEntity(TestData testData) { private CheckPoint getCheckPointEntity(TestData testData) {
......
package org.matrix.actuators.util;
/**
* 放了一些跟线程有关的工具
* @author huangxiahao
*/
public class ThreadUtil {
public static Long currentThreadId(){
return Thread.currentThread().getId();
}
}
package org.matrix.socket; package org.matrix.config;
import org.matrix.socket.HttpAuthHandler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
/**
* @author huangxiahao
*/
@Configuration @Configuration
@EnableWebSocket @EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer { public class WebSocketConfig implements WebSocketConfigurer {
@Autowired @Autowired
private HttpAuthHandler httpAuthHandler; private HttpAuthHandler httpAuthHandler;
// @Autowired
// private MyInterceptor myInterceptor;
@Override @Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry registry
.addHandler(httpAuthHandler, "myWS") .addHandler(httpAuthHandler, "ws")
.setAllowedOrigins("*"); .setAllowedOrigins("*");
} }
} }
\ No newline at end of file
...@@ -104,8 +104,8 @@ public class TestController { ...@@ -104,8 +104,8 @@ public class TestController {
TestCase testCase = new TestCase(); TestCase testCase = new TestCase();
testCase.setName("name"); testCase.setName("name");
testCase.setType(1); testCase.setType(1);
testCase.setDetail(json);
TestData testData = new TestData(); TestData testData = new TestData();
testData.setDetail(json);
testData.setAbnormalCheckpoint(1); testData.setAbnormalCheckpoint(1);
testData.setContainCheckpoint("张三,李四"); testData.setContainCheckpoint("张三,李四");
testData.setNoContainCheckpoint("张三,李四"); testData.setNoContainCheckpoint("张三,李四");
......
...@@ -39,9 +39,6 @@ public class TestCase extends BaseEntity { ...@@ -39,9 +39,6 @@ public class TestCase extends BaseEntity {
@ApiModelProperty("测试执行后行动ID组,例如:1,2,3") @ApiModelProperty("测试执行后行动ID组,例如:1,2,3")
private String moveAferTest; private String moveAferTest;
@ApiModelProperty("详细参数")
private String detail;
@ApiModelProperty("用例描述") @ApiModelProperty("用例描述")
private String des; private String des;
......
package org.matrix.socket;
import lombok.Data;
import java.util.List;
/**
* 接收前端传入的用例执行信息
* @author huangxiahao
*/
@Data
public class CaseExecuteVo extends SocketVo{
private List<Long> testDateId;
}
package org.matrix.socket; package org.matrix.socket;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import org.junit.platform.commons.util.StringUtils;
import org.matrix.actuators.usecase.CaseActuator;
import org.matrix.actuators.usecase.TestCaseExecuteResult;
import org.matrix.database.entity.TestCaseBTO;
import org.matrix.database.service.ITestDataService;
import org.matrix.exception.GlobalException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler; import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.util.List;
/**
* webSocket处理
* @author huangxiahao
*/
@Component @Component
public class HttpAuthHandler extends TextWebSocketHandler { public class HttpAuthHandler extends TextWebSocketHandler {
@Override final CaseActuator caseActuator;
public void afterConnectionEstablished(WebSocketSession session) throws Exception { final ITestDataService testDataService;
System.out.println(session);
public HttpAuthHandler(CaseActuator caseActuator, ITestDataService testDataService) {
this.caseActuator = caseActuator;
this.testDataService = testDataService;
} }
@Override @Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.println(message); session.sendMessage(new TextMessage("message get success"));
session.sendMessage(new TextMessage("收到了")); String payload = message.getPayload();
//检验客户端是否进行测试用例调试
try {
SocketVo socketVo = JSON.parseObject(payload, SocketVo.class);
//如果客户端发送测试用例调试命令,则进行测试用例的执行
if (socketVo!=null&& StringUtils.isNotBlank(socketVo.getType())){
if (SocketType.TEST_CASE.toString().equals(socketVo.getType())){
//将websocketSession 加入到socket池子中
SocketPool.add(Thread.currentThread().getId(),session);
CaseExecuteVo caseExecuteVo = JSON.parseObject(payload, CaseExecuteVo.class);
List<Long> testDateIds = caseExecuteVo.getTestDateId();
for (Long testDateId : testDateIds) {
try {
TestCaseBTO testCaseBto = testDataService.toCaseBTO(testDateId);
//执行测试用例
TestCaseExecuteResult testCaseExecuteResult = caseActuator.executeTestCase(testCaseBto,1L,1L);
session.sendMessage(new TextMessage(JSON.toJSONString(testCaseExecuteResult)));
}catch (GlobalException e){
session.sendMessage(new TextMessage(e.getMessage()));
}
}
SocketPool.remove(Thread.currentThread().getId());
}else {
session.sendMessage(new TextMessage("入参不符合规定"));
}
}
}catch (JSONException e ){
session.sendMessage(new TextMessage("入参不符合规定"));
}
} }
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
super.afterConnectionClosed(session, status);
}
} }
package org.matrix.socket; package org.matrix.socket;
import org.matrix.exception.GlobalException;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
* 日志消息队列 * 日志消息队列
*
* @author huangxiahao * @author huangxiahao
*/ */
public class LogQueue { public class LogQueue {
private static final ConcurrentHashMap<Long, List<String>> LOG_MAP = new ConcurrentHashMap<>(); private static final ConcurrentHashMap<Long, List<String>> LOG_MAP = new ConcurrentHashMap<>();
public static void add(Long threadId,String log){ public static void add(Long threadId, String log) {
List<String> logList = LOG_MAP.get(threadId); List<String> logList = LOG_MAP.get(threadId);
if (logList!=null){ if (logList != null) {
logList.add(log); logList.add(log);
}else { } else {
logList = new ArrayList<>(); logList = new ArrayList<>();
logList.add(log); logList.add(log);
LOG_MAP.put(threadId,logList); LOG_MAP.put(threadId, logList);
} }
//将log发送出去 //将log发送出去
// sendMessage(threadId,log); sendMessage(threadId, log);
}
public static void remove(Long threadId) {
LOG_MAP.remove(threadId);
} }
// public static void sendMessage(Long threadId,String log){
// SocketHandler.sendMessage(SocketPool.get(threadId),log); public static void sendMessage(Long threadId, String log) {
// } try {
WebSocketSession webSocketSession = SocketPool.get(threadId);
if (webSocketSession != null) {
webSocketSession.sendMessage(new TextMessage(log));
}
} catch (IOException e) {
throw new GlobalException("发送socket消息失败,socket已经断开连接");
}
}
} }
package org.matrix.socket; package org.matrix.socket;
import org.springframework.web.socket.WebSocketSession;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
...@@ -7,22 +9,22 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -7,22 +9,22 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class SocketPool { public class SocketPool {
// private static final ConcurrentHashMap<Long, ClientSocket> ONLINE_SOCKET_MAP = new ConcurrentHashMap<>(); private static final ConcurrentHashMap<Long, WebSocketSession> SOCKET_MAP = new ConcurrentHashMap<>();
//
//
// public static void add(ClientSocket clientSocket){ public static void add(Long key,WebSocketSession clientSocket){
// if (clientSocket != null && clientSocket.getKey()!=null) { if (clientSocket != null &key !=null) {
// ONLINE_SOCKET_MAP.put(clientSocket.getKey(), clientSocket); SOCKET_MAP.put(key, clientSocket);
// } }
// } }
//
// public static void remove(Long key){ public static void remove(Long key){
// if (key!=null) { if (key!=null) {
// ONLINE_SOCKET_MAP.remove(key); SOCKET_MAP.remove(key);
// } }
// } }
//
// public static ClientSocket get(Long key){ public static WebSocketSession get(Long key){
// return ONLINE_SOCKET_MAP.get(key); return SOCKET_MAP.get(key);
// } }
} }
\ No newline at end of file
package org.matrix.socket;
/**
* @author huangxiahao
*/
public enum SocketType {
/**
* 测试用例类型
*/
TEST_CASE;
}
package org.matrix.socket;
import lombok.Data;
/**
* 接收socket获取的信息的基本类
* @author Administrator
*/
@Data
public class SocketVo {
/**
* 用于区分前端传入的内容的类型
* 只有执行类型为,EST_CASE 时 执行测试用例
*/
private String type ;
}
...@@ -77,7 +77,7 @@ public class TestPigeon extends AbstractTestNGSpringContextTests { ...@@ -77,7 +77,7 @@ public class TestPigeon extends AbstractTestNGSpringContextTests {
TestCase testCase = java.util.Optional.of(caseService.getById(caseId)) TestCase testCase = java.util.Optional.of(caseService.getById(caseId))
.orElseThrow(() -> new GlobalException(String.format("没有找到id = %d 的TestCase", caseId))); .orElseThrow(() -> new GlobalException(String.format("没有找到id = %d 的TestCase", caseId)));
//todo 苗润雨 将这个NULL填一下,里面放testData //todo 苗润雨 将这个NULL填一下,里面放testData
TestCaseExecuteResult testCaseExecuteResult = caseActuator.executeTestCase(testCase,null, envId, projectId); TestCaseExecuteResult testCaseExecuteResult = caseActuator.executeTestCase(null, envId, projectId);
CheckPointResult checkPointResult = testCaseExecuteResult.getCheckPointResult(); CheckPointResult checkPointResult = testCaseExecuteResult.getCheckPointResult();
Reporter.log(checkPointResult.toString()); Reporter.log(checkPointResult.toString());
} }
......
...@@ -44,8 +44,8 @@ class CaseActuatorTest { ...@@ -44,8 +44,8 @@ class CaseActuatorTest {
TestCase testCase = new TestCase(); TestCase testCase = new TestCase();
testCase.setName("name"); testCase.setName("name");
testCase.setType(1); testCase.setType(1);
testCase.setDetail(json);
TestData testData = new TestData(); TestData testData = new TestData();
testData.setDetail(json);
testData.setAbnormalCheckpoint(1); testData.setAbnormalCheckpoint(1);
testData.setContainCheckpoint("张三,李四"); testData.setContainCheckpoint("张三,李四");
testData.setNoContainCheckpoint("张三,李四"); testData.setNoContainCheckpoint("张三,李四");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论