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

fix(base): 新增testJobMapper中的分页查询

上级 6b7891e2
......@@ -2,6 +2,7 @@ package org.matrix.actuators.usecase;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.matrix.actuators.Actuator;
import org.matrix.actuators.checkpoint.CheckPointActuator;
import org.matrix.actuators.httpclient.HttpClientActuator;
......@@ -23,6 +24,7 @@ import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
......@@ -56,7 +58,7 @@ public class CaseActuator implements Actuator {
*/
public TestCaseExecuteResult executeTestCase(TestCaseBTO testCaseBto, Long envId, Long projectId) {
try {
LogQueueRuntime.addNewLog("[用例执行器] 开始执行单条用例数据!!");
LogQueueRuntime.addNewLog("[用例执行器] 开始执行用例!!");
LogQueueRuntime.addNewLog(String.format("[用例执行器] 当前正在执行用例ID: %s 用例名: %s", testCaseBto.getTestCase().getId(), testCaseBto.getTestCase().getName()));
//执行前置动作
executeMove(testCaseBto.getTestCase().getMoveAfterCase()
......@@ -99,7 +101,9 @@ public class CaseActuator implements Actuator {
LogQueueRuntime.addNewLog("[用例执行器] 开始执行用例!!" );
try {
LogQueueRuntime.addNewLog(String.format("[用例执行器] 当前正在执行用例ID: %s 用例名: %s", testCaseBto.getTestCase().getId(), testCaseBto.getTestCase().getName()));
//todo 黄夏豪 执行前置动作
//执行前置动作
executeMove(testCaseBto.getTestCase().getMoveAfterCase()
, envId, projectId, null, MoveStrategy.PRE_MOVE);
LogQueueRuntime.addNewLog("[用例执行器] 开始执行前置动作!!");
//执行测试用例的本体内容
List<TestCaseExecuteResult> resultList = new ArrayList<>();
......@@ -114,7 +118,9 @@ public class CaseActuator implements Actuator {
testCaseBto.getTestCase(),
testData);
LogQueueRuntime.addNewLog("[用例执行器] 即将开始执行中置动作");
//todo 黄夏豪 执行测试后动作
//执行中置动作
executeMove(testCaseBto.getTestCase().getMoveAfterTest()
, envId, projectId, baseTestCaseResponseDetail.getResponseBody(), MoveStrategy.MID_MOVE);
CheckPointResult checkPointResult = getCheckPointResult(testCaseBto.getTestCase(),
testData,
envId,
......@@ -122,15 +128,18 @@ public class CaseActuator implements Actuator {
baseTestCaseResponseDetail);
TestCaseExecuteResult testCaseExecuteResult = new TestCaseExecuteResult(baseTestCaseResponseDetail, checkPointResult);
resultList.add(testCaseExecuteResult);
LogQueueRuntime.addNewLog(JSON.toJSONString(testCaseExecuteResult));
//todo 黄夏豪 执行后置动作
LogQueueRuntime.addNewLog("用例执行结果:" + JSON.toJSONString(testCaseExecuteResult));
//执行后置动作
executeMove(testCaseBto.getTestCase().getMoveAfterTest()
, envId, projectId, baseTestCaseResponseDetail.getResponseBody(), MoveStrategy.AFT_MOVE);
changeExecutionHistoryStatus(ExecutionHistoryStatus.FINISH);
}
//将线程中正在执行的DataId清除
LogQueueRuntime.clearTestData();
return resultList;
} catch (Exception e) {
throw e;
}finally {
//将线程中正在执行的DataId清除
LogQueueRuntime.clearTestData();
}
}
......@@ -144,7 +153,7 @@ public class CaseActuator implements Actuator {
* @param strategy 动作的策略,即是前置/中间/后置,具体查看{@link MoveStrategy}
*/
private void executeMove(String moveString, Long envId, Long projectId, String caseResultData, MoveStrategy strategy) {
String[] moveIds = moveString.split(",");
String[] moveIds = StringUtils.isEmpty(moveString) ? new String[]{} : moveString.split(",");
for (String moveId : moveIds) {
getMoveActuator().runMove(
Long.valueOf(moveId),
......@@ -208,6 +217,12 @@ public class CaseActuator implements Actuator {
//将websocketSession 加入到socket池子中
TestCaseExecuteSocketPool.add(currentThreadId, session);
List<TestCaseListDataBto> testCaseListDataBtoList = caseExecuteVo.getTestCaseListDataBtoList();
for (int i = 0; i < testCaseListDataBtoList.size(); i++) {
Long caseProjectId = testCaseListDataBtoList.get(i).getTestCase().getProjectId();
if (!caseExecuteVo.getProjectId().equals(caseProjectId)){
throw new GlobalException("本次执行中,存在非相同项目的测试用例");
}
}
//建立执行历史(ExecutionHistory)
insertExecutionHistory(unionKey, caseExecuteVo);
for (TestCaseListDataBto testCaseListDataBto : testCaseListDataBtoList) {
......@@ -219,7 +234,6 @@ public class CaseActuator implements Actuator {
, caseExecuteVo.getType()
, unionKey
);
//执行测试用例
return executeTestCases(testCaseListDataBto, caseExecuteVo.getEnvId(), caseExecuteVo.getProjectId());
}
......@@ -239,11 +253,12 @@ public class CaseActuator implements Actuator {
ioException.printStackTrace();
}
} finally {
//将数据库中的执行历史状态设置为完成
endExecutionHistory();
//将本次产生的数据清除
TestCaseExecuteSocketPool.remove(currentThreadId);
//将本次产生的日志从执行状态设为停止状态
LogQueueRuntime.remove(currentThreadId);
//todo 黄夏豪 将数据库中的执行历史状态设置为完成
}
return null;
}
......@@ -253,12 +268,15 @@ public class CaseActuator implements Actuator {
for (TestCaseListDataBto testCaseListDataBto : testCaseListDataBtoList) {
List<TestData> testDataList = testCaseListDataBto.getTestDataList();
TestCase testCase = testCaseListDataBto.getTestCase();
for (TestData testData : testDataList) {
for (int i = 0; i < testDataList.size(); i++) {
ExecutionHistory executionHistory = new ExecutionHistory();
executionHistory.setJobId(caseExecuteVo.getJobId());
executionHistory.setCaseId(testCase.getId());
executionHistory.setDataId(testData.getId());
executionHistory.setDataId(testDataList.get(i).getId());
executionHistory.setUnionKey(unionKey);
if (i==0){
executionHistory.setStartTime(LocalDateTime.now());
}
executionHistoryService.save(executionHistory);
}
}
......@@ -269,17 +287,39 @@ public class CaseActuator implements Actuator {
TestExecuteLog currentTestExecute = LogQueueRuntime.getCurrentTestExecute(ThreadUtil.currentThreadId());
if (currentTestExecute!=null){
ExecutionHistory executionHistory = new ExecutionHistory();
if (executionHistoryStatus.equals(ExecutionHistoryStatus.RUN)){
executionHistory.setStartTime(LocalDateTime.now());
}else if (executionHistoryStatus.equals(ExecutionHistoryStatus.FINISH)){
executionHistory.setEndTime(LocalDateTime.now());
}
executionHistory.setStatus(executionHistoryStatus);
executionHistoryService.update(executionHistory, Wrappers
.lambdaUpdate(ExecutionHistory.class)
.eq(ExecutionHistory::getJobId,currentTestExecute.getJobId())
.eq(ExecutionHistory::getJobId,currentTestExecute.getTestJobId())
.eq(ExecutionHistory::getCaseId,currentTestExecute.getTestCaseId())
.eq(ExecutionHistory::getDataId,currentTestExecute.getTestDataId())
.eq(ExecutionHistory::getUnionKey,currentTestExecute.getUnionKey())
);
}
}
/**
* 将所有还未完成的执行历史设置为ERROR
*/
public void endExecutionHistory() {
TestExecuteLog currentTestExecute = LogQueueRuntime.getCurrentTestExecute(ThreadUtil.currentThreadId());
if (currentTestExecute!=null){
ExecutionHistory executionHistory = new ExecutionHistory();
executionHistory.setStatus(ExecutionHistoryStatus.ERROR);
executionHistory.setEndTime(LocalDateTime.now());
executionHistoryService.update(executionHistory, Wrappers
.lambdaUpdate(ExecutionHistory.class)
.eq(ExecutionHistory::getJobId,currentTestExecute.getTestJobId())
.eq(ExecutionHistory::getCaseId,currentTestExecute.getTestCaseId())
.eq(ExecutionHistory::getUnionKey,currentTestExecute.getUnionKey())
.ne(ExecutionHistory::getStatus,ExecutionHistoryStatus.FINISH)
);
}
}
......
package org.matrix.database.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.matrix.enums.ExecutionHistoryStatus;
import java.time.LocalDateTime;
/**
* <p>
*
......@@ -32,6 +38,21 @@ public class ExecutionHistory extends BaseEntity {
private ExecutionHistoryStatus status = ExecutionHistoryStatus.READY;
/**
* 创建时间
*/
@ApiModelProperty("开始执行时间")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
private LocalDateTime startTime;
/**
* 更新时间
*/
@ApiModelProperty("结束执行时间")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
private LocalDateTime endTime;
}
......@@ -16,9 +16,9 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName("kt_test_task")
@ApiModel(value = "TestTask对象", description = "测试任务")
public class TestTask extends BaseEntity{
@TableName("kt_test_job")
@ApiModel(value = "TestJob对象", description = "测试任务")
public class TestJob extends BaseEntity{
@ApiModelProperty("名称")
private String name;
......
......@@ -35,9 +35,11 @@ public interface ExecutionHistoryMapper extends BaseMapper<ExecutionHistory> {
"\tkeh.case_id caseId,\n" +
"\tkeh.data_id dataId,\n" +
"\tkeh.job_id jobId,\n" +
"\tkeh.`status` `status`,\n" +
"\tIF(MIN(keh.`status`)=0,0,MAX(keh.`status`)) `status`,\n" +
"\tktc.`name` caseName,\n" +
"\tktd.`name` dataName \n" +
"\tktd.`name` dataName," +
"\tMIN(keh.create_time) startTime,\n" +
"\tMAX(keh.update_time) endTime " +
"FROM\n" +
"\tkt_execution_history keh\n" +
"\tLEFT JOIN kt_test_case ktc ON ktc.id = keh.case_id\n" +
......@@ -50,6 +52,7 @@ public interface ExecutionHistoryMapper extends BaseMapper<ExecutionHistory> {
"and keh.job_id= #{jobId} \n" +
" </if>" +
"</where>" +
"GROUP BY keh.union_key " +
"</script>")
IPage<ExecutionHistoryVo> pageByCaseIdAndJobId(IPage<ExecutionHistoryVo> page, Long caseId, Long jobId);
......
package org.matrix.database.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Select;
import org.matrix.database.entity.TestJob;
import org.matrix.database.vo.TestJobVo;
/**
* @author mry
*/
public interface TestJobMapper extends BaseMapper<TestJob> {
/**
* 分页查询TestJob
* @param page 分页信息
* @return 带有分页的测试任务列表
*/
@Select("SELECT\n" +
"\tktj.id,\n" +
"\tktj.`name`,\n" +
"\tktj.sql_statement,\n" +
"\tktj.remark,\n" +
"\t`status` \n" +
"FROM\n" +
"\t`kt_test_job` ktj\n" +
"\tLEFT JOIN ( SELECT job_id, union_key FROM kt_execution_history WHERE id IN ( SELECT max( id ) FROM kt_execution_history WHERE job_id != - 1 GROUP BY job_id ) ) keh ON ktj.id = keh.job_id\n" +
"\tLEFT JOIN ( SELECT union_key, IF ( MIN( `status` )= 0, 0, MAX( `status` )) `status` FROM kt_execution_history GROUP BY union_key ) us ON us.union_key = keh.union_key")
IPage<TestJobVo> pageTestJob(IPage<TestJob> page);
}
package org.matrix.database.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.matrix.database.entity.TestTask;
/**
* @author mry
*/
public interface TestTaskMapper extends BaseMapper<TestTask> {
}
package org.matrix.database.service;
import com.baomidou.mybatisplus.extension.service.IService;
import org.matrix.database.entity.TestTask;
import org.matrix.database.entity.TestJob;
/**
* @author mry
*/
public interface ITestTaskService extends IService<TestTask> {
public interface ITestJobService extends IService<TestJob> {
}
package org.matrix.database.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.matrix.database.entity.TestTask;
import org.matrix.database.mapper.TestTaskMapper;
import org.matrix.database.service.ITestTaskService;
import org.matrix.database.entity.TestJob;
import org.matrix.database.mapper.TestJobMapper;
import org.matrix.database.service.ITestJobService;
import org.springframework.stereotype.Service;
/**
* @author mry
*/
@Service
public class TestTaskServiceImpl extends ServiceImpl<TestTaskMapper, TestTask> implements ITestTaskService {
public class TestJobServiceImpl extends ServiceImpl<TestJobMapper, TestJob> implements ITestJobService {
}
......@@ -3,6 +3,8 @@ package org.matrix.database.vo;
import lombok.Data;
import org.matrix.database.entity.ExecutionHistory;
import java.time.LocalDateTime;
/**
* ExecutionHistory 给前端用于展示的VO
* @author huangxiahao
......@@ -14,4 +16,8 @@ public class ExecutionHistoryVo extends ExecutionHistory {
private String dataName;
private LocalDateTime startTime;
private LocalDateTime endTime;
}
package org.matrix.database.vo;
import org.matrix.database.entity.TestJob;
/**
* testJob给前端展示用的Vo
* @author huangxiahao
*/
public class TestJobVo extends TestJob {
private Integer status;
}
......@@ -14,17 +14,21 @@ import lombok.Getter;
public enum ExecutionHistoryStatus {
/**
* SQL类型动作,该动作执行SQL语句
* 准备阶段
*/
READY(0, "准备阶段"),
/**
* HTTP类型动作,该动作执行HTTP接口
* 执行阶段
*/
RUN(1, "执行阶段"),
/**
* 用例类型动作,该动作执行另外的测试用例
* 完成阶段
*/
FINISH(2, "完成阶段");
FINISH(2, "完成阶段"),
/**
* 异常
*/
ERROR(3, "异常");
/**
* 数据库里记录的字段使用该字段来记录
......
......@@ -35,8 +35,6 @@ public class HttpAuthHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.println("收到了: " + message.toString());
session.sendMessage(new TextMessage("message get success"));
String payload = message.getPayload();
//检验客户端是否进行测试用例调试
try {
......@@ -56,7 +54,6 @@ public class HttpAuthHandler extends TextWebSocketHandler {
} catch (JSONException e) {
session.sendMessage(new TextMessage("入参不符合规定"));
}
session.sendMessage(new TextMessage("message execute success"));
}
@Override
......
......@@ -5,7 +5,6 @@ import cn.hutool.log.level.Level;
import com.alibaba.fastjson.JSONObject;
import org.matrix.actuators.util.ThreadUtil;
import org.matrix.database.service.IExecutionRecordService;
import org.matrix.enums.ExecutionRecType;
import org.matrix.exception.GlobalException;
import org.matrix.util.SpringUtils;
import org.springframework.web.socket.TextMessage;
......@@ -79,7 +78,7 @@ public class LogQueueRuntime {
String logMessage =
String.format(
"%s.%s.%s.%s"
,testExecuteLog.getJobId()
,testExecuteLog.getTestJobId()
,testExecuteLog.getTestCaseId()
,testExecuteLog.getTestDataId()
,message.substring(1,message.length()-1)
......@@ -108,7 +107,7 @@ public class LogQueueRuntime {
public static void initTestCaseLog(Long jobId,Long userId, Long caseId,TestExecuteType type,String unionKey) {
TestExecuteLog testExecuteLog = new TestExecuteLog();
testExecuteLog.setJobId(jobId);
testExecuteLog.setTestJobId(jobId);
testExecuteLog.setTestCaseId(caseId);
testExecuteLog.setUserId(userId);
testExecuteLog.setType(type);
......
......@@ -14,7 +14,7 @@ import java.util.Objects;
public class TestExecuteLog {
private Long jobId = -1L;
private Long testJobId = -1L;
/**
* 用于记录执行记录的唯一KEY,同一批次的执行记录应该具有相同的KEY。
*/
......
......@@ -19,4 +19,7 @@ spring:
mybatis-plus:
type-enums-package: org.matrix.enums
baseJsPath: syntaxCheck.js
\ No newline at end of file
baseJsPath: syntaxCheck.js
......@@ -3,8 +3,7 @@ package org.matrix.autotest.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.*;
import org.matrix.autotest.utils.PageTools;
import org.matrix.database.entity.ExecutionRecord;
import org.matrix.database.service.IExecutionHistoryService;
......@@ -35,9 +34,16 @@ public class ExecutionHistoryController {
this.executionHistoryService = executionHistoryService;
}
@ApiOperation(value = "分页查询执行历史",notes = "假如查询测试用例的执行历史,请入参caseId,假如查询测试任务的执行历史,请入参jobId")
@GetMapping("/findExecutionHistory")
@ApiImplicitParams({
@ApiImplicitParam(name="pageSize",value="页码",required=true,paramType="query"),
@ApiImplicitParam(name="pageNum",value="当前页显示调试",required=true,paramType="query"),
@ApiImplicitParam(name="jobId",value="测试任务ID",paramType="query"),
@ApiImplicitParam(name="caseId",value="测试用例ID",paramType="query")
})
public ResponseEntity<CommonResultObj<IPage<ExecutionHistoryVo>>> findExecutionHistory(
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "-1") Long jobId,
Long caseId
......
......@@ -5,8 +5,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.matrix.autotest.utils.PageTools;
import org.matrix.database.entity.TestTask;
import org.matrix.database.service.ITestTaskService;
import org.matrix.database.entity.TestJob;
import org.matrix.database.service.ITestJobService;
import org.matrix.database.vo.CommonResult;
import org.matrix.database.vo.CommonResultObj;
import org.springframework.http.ResponseEntity;
......@@ -22,14 +22,14 @@ import java.util.Optional;
*/
@CrossOrigin
@RestController
@RequestMapping("/testTasks")
@Api(tags = "对测试任务test_task的基本操作")
public class TestTaskController {
@RequestMapping("/testJob")
@Api(tags = "对测试任务test_job的基本操作")
public class TestJobController {
private final ITestTaskService testTaskService;
private final ITestJobService testJobService;
public TestTaskController(ITestTaskService testTaskService) {
this.testTaskService = testTaskService;
public TestJobController(ITestJobService testJobService) {
this.testJobService = testJobService;
}
/**
......@@ -39,8 +39,8 @@ public class TestTaskController {
*/
@ApiOperation(value = "查询所有测试任务")
@GetMapping
public ResponseEntity<CommonResultObj<List<TestTask>>> findConnects() {
List<TestTask> results = Optional.ofNullable(testTaskService.list()).orElse(new ArrayList<>());
public ResponseEntity<CommonResultObj<List<TestJob>>> findConnects() {
List<TestJob> results = Optional.ofNullable(testJobService.list()).orElse(new ArrayList<>());
return results.size() != 0
? CommonResult.success(results, "查询成功")
: CommonResult.failed(results, "查询失败或无数据");
......@@ -56,12 +56,12 @@ public class TestTaskController {
*/
@ApiOperation(value = "分页查询测试任务")
@GetMapping("/page")
public ResponseEntity<CommonResultObj<Page<TestTask>>> findPageTestTasks(
public ResponseEntity<CommonResultObj<Page<TestJob>>> findPageTestTasks(
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam(defaultValue = "1") int pageNum,
String name) {
Page<TestTask> results = Optional.of(testTaskService.page(Page.of(pageNum, pageSize), Wrappers.lambdaQuery(TestTask.class)
.like(StringUtils.hasLength(name), TestTask::getName, name)))
Page<TestJob> results = Optional.of(testJobService.page(Page.of(pageNum, pageSize), Wrappers.lambdaQuery(TestJob.class)
.like(StringUtils.hasLength(name), TestJob::getName, name)))
.orElse(new Page<>());
PageTools.pageTool(pageSize, pageNum, results);
return results.getRecords().size() != 0
......@@ -72,26 +72,26 @@ public class TestTaskController {
/**
* 添加测试任务
*
* @param testTask 测试任务对象
* @param testJob 测试任务对象
* @return 添加的测试任务,以及是否添加成功的提示信息
*/
@ApiOperation(value = "添加测试任务")
@PostMapping
public ResponseEntity<CommonResultObj<TestTask>> insertTestTask(@RequestBody TestTask testTask) {
return CommonResult.pred(testTaskService::save, testTask
public ResponseEntity<CommonResultObj<TestJob>> insertTestTask(@RequestBody TestJob testJob) {
return CommonResult.pred(testJobService::save, testJob
, "添加成功", "添加失败");
}
/**
* 修改测试任务
*
* @param testTask 测试任务对象
* @param testJob 测试任务对象
* @return 修改后的测试任务,以及是否修改成功的提示信息
*/
@ApiOperation(value = "修改测试任务")
@PutMapping
public ResponseEntity<CommonResultObj<TestTask>> updateTestTask(@RequestBody TestTask testTask) {
return CommonResult.pred(testTaskService::updateById, testTask
public ResponseEntity<CommonResultObj<TestJob>> updateTestTask(@RequestBody TestJob testJob) {
return CommonResult.pred(testJobService::updateById, testJob
, "修改成功", "修改失败");
}
......@@ -104,7 +104,7 @@ public class TestTaskController {
@ApiOperation(value = "根据主键id删除测试任务")
@DeleteMapping("/{id}")
public ResponseEntity<CommonResultObj<Long>> deleteTestTask(@PathVariable Long id) {
return CommonResult.pred(testTaskService::removeById, id
return CommonResult.pred(testJobService::removeById, id
, "删除成功", "删除失败或id不存在");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论