refactor(接口测试): 优化批量执行报告生成逻辑

This commit is contained in:
AgAngle 2024-04-10 10:11:18 +08:00 committed by 刘瑞斌
parent 8af35fe57c
commit e799f147f5
5 changed files with 57 additions and 42 deletions

View File

@ -19,9 +19,4 @@ public class ExecutionQueueDetail implements Serializable {
* 排序
*/
private Integer sort;
/**
* 当前资源产生的执行报告id
*/
private String reportId;
}

View File

@ -23,22 +23,20 @@ public class ApiBatchRunBaseService {
* @param runModeConfig
* @return
*/
public ExecutionQueue initExecutionqueue(List<String> resourceIds, ApiRunModeConfigDTO runModeConfig, String resourceType, Map<String, String> caseReportMap, String userId) {
public ExecutionQueue initExecutionqueue(List<String> resourceIds, ApiRunModeConfigDTO runModeConfig, String resourceType, String userId) {
ExecutionQueue queue = getExecutionQueue(runModeConfig, resourceType, userId);
List<ExecutionQueueDetail> queueDetails = getExecutionQueueDetails(resourceIds, caseReportMap);
List<ExecutionQueueDetail> queueDetails = getExecutionQueueDetails(resourceIds);
apiExecutionQueueService.insertQueue(queue, queueDetails);
return queue;
}
public List<ExecutionQueueDetail> getExecutionQueueDetails(List<String> resourceIds, Map<String, String> caseReportMap) {
public List<ExecutionQueueDetail> getExecutionQueueDetails(List<String> resourceIds) {
List<ExecutionQueueDetail> queueDetails = new ArrayList<>();
AtomicInteger sort = new AtomicInteger(1);
for (String resourceId : resourceIds) {
ExecutionQueueDetail queueDetail = new ExecutionQueueDetail();
queueDetail.setResourceId(resourceId);
queueDetail.setSort(sort.getAndIncrement());
// caseReportMap null 说明是集合报告生成一个虚拟的报告ID
queueDetail.setReportId(caseReportMap == null ? UUID.randomUUID().toString() : caseReportMap.get(resourceId));
queueDetails.add(queueDetail);
}
return queueDetails;

View File

@ -27,6 +27,7 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.DateUtils;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@ -105,24 +106,25 @@ public class ApiTestCaseBatchRunService {
public void serialExecute(ApiTestCaseBatchRunRequest request, String userId) throws Exception {
List<String> ids = apiTestCaseService.doSelectIds(request, false);
ApiRunModeConfigDTO runModeConfig = getRunModeConfig(request);
// 初始化集成报告
if (runModeConfig.isIntegratedReport()) {
// 初始化集成报告
initIntegratedReport(runModeConfig, ids, userId, request.getProjectId());
List<ApiTestCase> apiTestCases = new ArrayList<>(ids.size());
// 分批查询
SubListUtils.dealForSubList(ids, 100, subIds -> apiTestCases.addAll(extApiTestCaseMapper.getApiCaseExecuteInfoByIds(subIds)));
Map<String, ApiTestCase> apiCaseMap = apiTestCases.stream()
.collect(Collectors.toMap(ApiTestCase::getId, Function.identity()));
// 初始化集成报告步骤
initApiReportSteps(ids, apiCaseMap, runModeConfig.getCollectionReport().getReportId());
}
List<ApiTestCase> apiTestCases = new ArrayList<>(ids.size());
// 分批查询
SubListUtils.dealForSubList(ids, 100, subIds -> apiTestCases.addAll(extApiTestCaseMapper.getApiCaseExecuteInfoByIds(subIds)));
Map<String, ApiTestCase> apiCaseMap = apiTestCases.stream()
.collect(Collectors.toMap(ApiTestCase::getId, Function.identity()));
// 初始化报告返回用例和报告的 map
Map<String, String> caseReportMap = initReport(ids, runModeConfig, apiTestCases, apiCaseMap, userId);
// 先初始化集成报告设置好报告ID再初始化执行队列
ExecutionQueue queue = apiBatchRunBaseService.initExecutionqueue(ids, runModeConfig, ApiExecuteResourceType.API_CASE.name(), caseReportMap, userId);
ExecutionQueue queue = apiBatchRunBaseService.initExecutionqueue(ids, runModeConfig, ApiExecuteResourceType.API_CASE.name(), userId);
// 执行第一个任务
ExecutionQueueDetail nextDetail = apiExecutionQueueService.getNextDetail(queue.getQueueId());
@ -159,7 +161,7 @@ public class ApiTestCaseBatchRunService {
.collect(Collectors.toMap(ApiTestCase::getId, Function.identity()));
// 初始化报告返回用例和报告的 map
Map<String, String> caseReportMap = initReport(ids, runModeConfig, apiTestCases, apiCaseMap, userId);
Map<String, String> caseReportMap = initParallelReport(ids, runModeConfig, apiTestCases, apiCaseMap, userId);
// 集成报告执行前先设置成 RUNNING
setRunningIntegrateReport(runModeConfig);
@ -219,7 +221,7 @@ public class ApiTestCaseBatchRunService {
}
}
private Map<String, String> initReport(List<String> ids, ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, Map<String, ApiTestCase> apiCaseMap, String userId) {
private Map<String, String> initParallelReport(List<String> ids, ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, Map<String, ApiTestCase> apiCaseMap, String userId) {
// 先初始化所有报告
if (runModeConfig.isIntegratedReport()) {
// 获取集成报告ID
@ -302,11 +304,18 @@ public class ApiTestCaseBatchRunService {
public void executeNextTask(ExecutionQueue queue, ExecutionQueueDetail queueDetail) {
ApiRunModeConfigDTO runModeConfig = queue.getRunModeConfig();
String resourceId = queueDetail.getResourceId();
String reportId = queueDetail.getReportId();
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(resourceId);
ApiTestCaseBlob apiTestCaseBlob = apiTestCaseBlobMapper.selectByPrimaryKey(resourceId);
String reportId;
if (runModeConfig.isIntegratedReport()) {
reportId = IDGenerator.nextStr();
} else {
// 独立报告执行到当前任务时初始化报告
reportId = initApiReport(runModeConfig, List.of(apiTestCase), queue.getUserId()).get(0).getApiReportId();
}
if (apiTestCase == null) {
LogUtils.info("当前执行任务的用例已删除 {}", resourceId);
return;

View File

@ -104,15 +104,16 @@ public class ApiScenarioBatchRunService {
// 初始化集成报告
if (runModeConfig.isIntegratedReport()) {
initIntegratedReport(runModeConfig, ids, userId, request.getProjectId());
}
Map<String, String> scenarioReportMap = initReport(ids, runModeConfig, userId);
// 初始化集合报告步骤
initReport(ids, runModeConfig, userId);
}
// 集成报告执行前先设置成 RUNNING
setRunningIntegrateReport(runModeConfig);
// 先初始化集成报告设置好报告ID再初始化执行队列
ExecutionQueue queue = apiBatchRunBaseService.initExecutionqueue(ids, runModeConfig, ApiExecuteResourceType.API_SCENARIO.name(), scenarioReportMap, userId);
ExecutionQueue queue = apiBatchRunBaseService.initExecutionqueue(ids, runModeConfig, ApiExecuteResourceType.API_SCENARIO.name(), userId);
// 执行第一个任务
ExecutionQueueDetail nextDetail = apiExecutionQueueService.getNextDetail(queue.getQueueId());
executeNextTask(queue, nextDetail);
@ -289,12 +290,21 @@ public class ApiScenarioBatchRunService {
* @param queueDetail
*/
public void executeNextTask(ExecutionQueue queue, ExecutionQueueDetail queueDetail) {
ApiRunModeConfigDTO runModeConfig = queue.getRunModeConfig();
ApiScenarioDetail apiScenarioDetail = apiScenarioService.getForRun(queueDetail.getResourceId());
if (apiScenarioDetail == null) {
LogUtils.info("当前执行任务的用例已删除 {}", queueDetail.getResourceId());
return;
}
TaskRequestDTO taskRequest = getTaskRequestDTO(queueDetail.getReportId(), apiScenarioDetail, queue.getRunModeConfig());
String reportId;
if (runModeConfig.isIntegratedReport()) {
reportId = IDGenerator.nextStr();
} else {
// 独立报告执行到当前任务时初始化报告
reportId = initScenarioReport(runModeConfig, apiScenarioDetail, queue.getUserId()).getApiScenarioReportId();
}
TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, apiScenarioDetail, queue.getRunModeConfig());
taskRequest.setQueueId(queue.getQueueId());
execute(taskRequest, apiScenarioDetail);
}
@ -319,14 +329,7 @@ public class ApiScenarioBatchRunService {
if (runModeConfig.isIntegratedReport()) {
apiScenarioService.initScenarioReportSteps(apiScenarioDetail.getId(), apiScenarioDetail.getSteps(), runModeConfig.getCollectionReport().getReportId());
} else {
if (parseParam.getScenarioConfig() != null
&& parseParam.getScenarioConfig().getOtherConfig() != null
&& BooleanUtils.isTrue(parseParam.getScenarioConfig().getOtherConfig().getEnableStepWait())) {
ApiScenarioReport apiScenarioReport = new ApiScenarioReport();
apiScenarioReport.setId(reportId);
apiScenarioReport.setWaitingTime(parseParam.getScenarioConfig().getOtherConfig().getStepWaitTime());
apiScenarioReportMapper.updateByPrimaryKeySelective(apiScenarioReport);
}
updateReportWaitTime(reportId, parseParam);
apiScenarioService.initScenarioReportSteps(apiScenarioDetail.getSteps(), reportId);
}
@ -344,6 +347,17 @@ public class ApiScenarioBatchRunService {
apiExecuteService.execute(runRequest, taskRequest, parseConfig);
}
private void updateReportWaitTime(String reportId, ApiScenarioParseParam parseParam) {
if (parseParam.getScenarioConfig() != null
&& parseParam.getScenarioConfig().getOtherConfig() != null
&& BooleanUtils.isTrue(parseParam.getScenarioConfig().getOtherConfig().getEnableStepWait())) {
ApiScenarioReport apiScenarioReport = new ApiScenarioReport();
apiScenarioReport.setId(reportId);
apiScenarioReport.setWaitingTime(parseParam.getScenarioConfig().getOtherConfig().getStepWaitTime());
apiScenarioReportMapper.updateByPrimaryKeySelective(apiScenarioReport);
}
}
private TaskRequestDTO getTaskRequestDTO(String reportId, ApiScenarioDetail apiScenarioDetail, ApiRunModeConfigDTO runModeConfig) {
TaskRequestDTO taskRequest = apiScenarioService.getTaskRequest(reportId, apiScenarioDetail.getId(), apiScenarioDetail.getProjectId(), ApiExecuteRunMode.RUN.name());
taskRequest.setSaveResult(true);
@ -428,9 +442,8 @@ public class ApiScenarioBatchRunService {
// 初始化报告步骤
if (runModeConfig.isIntegratedReport()) {
apiScenarioService.initScenarioReportSteps(apiScenarioDetail.getId(), apiScenarioDetail.getSteps(), runModeConfig.getCollectionReport().getReportId());
} else {
apiScenarioService.initScenarioReportSteps(apiScenarioDetail.getSteps(), queueDetail.getReportId());
}
queueDetail = apiExecutionQueueService.getNextDetail(queue.getQueueId());
}
if (runModeConfig.isIntegratedReport()) {

View File

@ -2003,14 +2003,14 @@ public class ApiScenarioService extends MoveNodeService {
public ApiScenarioDetail getForRun(String scenarioId) {
ApiScenarioDetail apiScenarioDetail = get(scenarioId);
filerDisableSteps(apiScenarioDetail.getSteps());
apiScenarioDetail.setSteps(filerDisableSteps(apiScenarioDetail.getSteps()));
return apiScenarioDetail;
}
/**
* 过滤掉禁用的步骤
*/
public List<? extends ApiScenarioStepCommonDTO> filerDisableSteps(List<? extends ApiScenarioStepCommonDTO> steps) {
public List<ApiScenarioStepDTO> filerDisableSteps(List<ApiScenarioStepDTO> steps) {
if (CollectionUtils.isEmpty(steps)) {
return List.of();
}