feat(接口测试): 初始化任务和报告的中间表

This commit is contained in:
AgAngle 2024-10-18 10:57:52 +08:00 committed by Craftsman
parent 710d51a0bc
commit d6c94f3986
13 changed files with 213 additions and 80 deletions

View File

@ -102,7 +102,7 @@ public class ApiScenarioScheduleJob extends BaseScheduleJob {
scenarioReport.setEnvironmentId(parseParam.getEnvironmentId()); scenarioReport.setEnvironmentId(parseParam.getEnvironmentId());
scenarioReport.setWaitingTime(apiScenarioRunService.getGlobalWaitTime(parseParam.getScenarioConfig())); scenarioReport.setWaitingTime(apiScenarioRunService.getGlobalWaitTime(parseParam.getScenarioConfig()));
apiScenarioRunService.initApiReport(apiScenarioDetail, scenarioReport); apiScenarioRunService.initApiReport(taskItem.getId(), apiScenarioDetail, scenarioReport);
// 初始化报告步骤 // 初始化报告步骤
apiScenarioRunService.initScenarioReportSteps(apiScenarioDetail.getSteps(), taskItem.getReportId()); apiScenarioRunService.initScenarioReportSteps(apiScenarioDetail.getSteps(), taskItem.getReportId());

View File

@ -65,14 +65,27 @@ public class ApiReportService {
private EnvironmentGroupMapper environmentGroupMapper; private EnvironmentGroupMapper environmentGroupMapper;
@Resource @Resource
private ApiReportNoticeService apiReportNoticeService; private ApiReportNoticeService apiReportNoticeService;
@Resource
private ApiReportRelateTaskMapper apiReportRelateTaskMapper;
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiReport(ApiReport report) { public void insertApiReport(ApiReport report) {
apiReportMapper.insertSelective(report); apiReportMapper.insertSelective(report);
} }
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiReport(ApiReport report, ApiReportRelateTask taskRelation) {
apiReportMapper.insertSelective(report);
apiReportRelateTaskMapper.insertSelective(taskRelation);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiReport(List<ApiReport> reports, List<ApiTestCaseRecord> records) { public void insertApiReport(List<ApiReport> reports, List<ApiTestCaseRecord> records) {
this.insertApiReport(reports, records, null);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiReport(List<ApiReport> reports, List<ApiTestCaseRecord> records, List<ApiReportRelateTask> taskRelations) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
if (CollectionUtils.isNotEmpty(reports)) { if (CollectionUtils.isNotEmpty(reports)) {
ApiReportMapper reportMapper = sqlSession.getMapper(ApiReportMapper.class); ApiReportMapper reportMapper = sqlSession.getMapper(ApiReportMapper.class);
@ -86,6 +99,13 @@ public class ApiReportService {
subList.forEach(detailMapper::insertSelective); subList.forEach(detailMapper::insertSelective);
}); });
} }
if (CollectionUtils.isNotEmpty(taskRelations)) {
ApiReportRelateTaskMapper taskRelationMapper = sqlSession.getMapper(ApiReportRelateTaskMapper.class);
SubListUtils.dealForSubList(taskRelations, 1000, subList -> {
subList.forEach(taskRelationMapper::insertSelective);
});
}
sqlSession.flushStatements(); sqlSession.flushStatements();
if (sqlSessionFactory != null) { if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);

View File

@ -107,7 +107,7 @@ public class ApiTestCaseBatchRunService {
if (runModeConfig.isIntegratedReport()) { if (runModeConfig.isIntegratedReport()) {
// 初始化集成报告 // 初始化集成报告
initIntegratedReport(runModeConfig, userId, request.getProjectId()); initIntegratedReport(execTask.getId(), runModeConfig, userId, request.getProjectId());
} }
// 先初始化集成报告设置好报告ID再初始化执行队列 // 先初始化集成报告设置好报告ID再初始化执行队列
@ -178,16 +178,16 @@ public class ApiTestCaseBatchRunService {
ApiRunModeConfigDTO runModeConfig = getRunModeConfig(request); ApiRunModeConfigDTO runModeConfig = getRunModeConfig(request);
if (runModeConfig.isIntegratedReport()) {
// 初始化集成报告
initIntegratedReport(runModeConfig, ids, userId, request.getProjectId());
}
Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
// 初始化任务 // 初始化任务
ExecTask execTask = initExecTask(ids, runModeConfig, project, userId); ExecTask execTask = initExecTask(ids, runModeConfig, project, userId);
if (runModeConfig.isIntegratedReport()) {
// 初始化集成报告
initIntegratedReport(execTask.getId(), runModeConfig, ids, userId, request.getProjectId());
}
// 分批查询 // 分批查询
SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> { SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> {
List<ApiTestCase> apiTestCases = getOrderApiTestCases(subIds, runModeConfig); List<ApiTestCase> apiTestCases = getOrderApiTestCases(subIds, runModeConfig);
@ -197,7 +197,7 @@ public class ApiTestCaseBatchRunService {
.collect(Collectors.toMap(ExecTaskItem::getResourceId, ExecTaskItem::getId)); .collect(Collectors.toMap(ExecTaskItem::getResourceId, ExecTaskItem::getId));
// 初始化报告返回用例和报告的 map // 初始化报告返回用例和报告的 map
Map<String, String> caseReportMap = initParallelReport(runModeConfig, apiTestCases, userId); Map<String, String> caseReportMap = initParallelReport(resourceExecTaskItemMap, runModeConfig, apiTestCases, userId);
List<TaskItem> taskItems = new ArrayList<>(subIds.size()); List<TaskItem> taskItems = new ArrayList<>(subIds.size());
@ -254,7 +254,7 @@ public class ApiTestCaseBatchRunService {
return apiTestCases; return apiTestCases;
} }
private Map<String, String> initParallelReport(ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, String userId) { private Map<String, String> initParallelReport(Map<String, String> resourceExecTaskItemMap, ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, String userId) {
// 先初始化所有报告 // 先初始化所有报告
if (runModeConfig.isIntegratedReport()) { if (runModeConfig.isIntegratedReport()) {
// 获取集成报告ID // 获取集成报告ID
@ -263,7 +263,7 @@ public class ApiTestCaseBatchRunService {
return null; return null;
} else { } else {
// 初始化非集成报告 // 初始化非集成报告
List<ApiTestCaseRecord> apiTestCaseRecords = initApiReport(runModeConfig, apiTestCases, userId); List<ApiTestCaseRecord> apiTestCaseRecords = initApiReport(resourceExecTaskItemMap, runModeConfig, apiTestCases, userId);
return apiTestCaseRecords.stream() return apiTestCaseRecords.stream()
.collect(Collectors.toMap(ApiTestCaseRecord::getApiTestCaseId, ApiTestCaseRecord::getApiReportId)); .collect(Collectors.toMap(ApiTestCaseRecord::getApiTestCaseId, ApiTestCaseRecord::getApiReportId));
} }
@ -308,7 +308,7 @@ public class ApiTestCaseBatchRunService {
* @param ids * @param ids
* @return * @return
*/ */
private ApiReport initIntegratedReport(ApiRunModeConfigDTO runModeConfig, List<String> ids, String userId, String projectId) { private ApiReport initIntegratedReport(String taskId, ApiRunModeConfigDTO runModeConfig, List<String> ids, String userId, String projectId) {
ApiReport apiReport = getApiReport(runModeConfig, userId); ApiReport apiReport = getApiReport(runModeConfig, userId);
apiReport.setName(runModeConfig.getCollectionReport().getReportName() + "_" + DateUtils.getTimeString(System.currentTimeMillis())); apiReport.setName(runModeConfig.getCollectionReport().getReportName() + "_" + DateUtils.getTimeString(System.currentTimeMillis()));
apiReport.setIntegrated(true); apiReport.setIntegrated(true);
@ -320,7 +320,13 @@ public class ApiTestCaseBatchRunService {
record.setApiTestCaseId(id); record.setApiTestCaseId(id);
return record; return record;
}).toList(); }).toList();
apiReportService.insertApiReport(List.of(apiReport), records);
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiReport.getId());
apiReportRelateTask.setTaskResourceId(taskId);
apiReportService.insertApiReport(List.of(apiReport), records, List.of(apiReportRelateTask));
// 设置集成报告执行参数 // 设置集成报告执行参数
runModeConfig.getCollectionReport().setReportId(apiReport.getId()); runModeConfig.getCollectionReport().setReportId(apiReport.getId());
return apiReport; return apiReport;
@ -332,12 +338,18 @@ public class ApiTestCaseBatchRunService {
* @param runModeConfig * @param runModeConfig
* @return * @return
*/ */
private ApiReport initIntegratedReport(ApiRunModeConfigDTO runModeConfig, String userId, String projectId) { private ApiReport initIntegratedReport(String taskId, ApiRunModeConfigDTO runModeConfig, String userId, String projectId) {
ApiReport apiReport = getApiReport(runModeConfig, userId); ApiReport apiReport = getApiReport(runModeConfig, userId);
apiReport.setName(runModeConfig.getCollectionReport().getReportName() + "_" + DateUtils.getTimeString(System.currentTimeMillis())); apiReport.setName(runModeConfig.getCollectionReport().getReportName() + "_" + DateUtils.getTimeString(System.currentTimeMillis()));
apiReport.setIntegrated(true); apiReport.setIntegrated(true);
apiReport.setProjectId(projectId); apiReport.setProjectId(projectId);
apiReportService.insertApiReport(apiReport);
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiReport.getId());
apiReportRelateTask.setTaskResourceId(taskId);
apiReportService.insertApiReport(apiReport, apiReportRelateTask);
// 设置集成报告执行参数 // 设置集成报告执行参数
runModeConfig.getCollectionReport().setReportId(apiReport.getId()); runModeConfig.getCollectionReport().setReportId(apiReport.getId());
return apiReport; return apiReport;
@ -381,7 +393,7 @@ public class ApiTestCaseBatchRunService {
reportId = runModeConfig.getCollectionReport().getReportId() + IDGenerator.nextStr(); reportId = runModeConfig.getCollectionReport().getReportId() + IDGenerator.nextStr();
} else { } else {
// 独立报告执行到当前任务时初始化报告 // 独立报告执行到当前任务时初始化报告
reportId = initApiReport(runModeConfig, List.of(apiTestCase), queue.getUserId()).getFirst().getApiReportId(); reportId = initApiReport(Map.of(apiTestCase.getId(), taskItemId), runModeConfig, List.of(apiTestCase), queue.getUserId()).getFirst().getApiReportId();
} }
if (apiTestCase == null) { if (apiTestCase == null) {
@ -428,10 +440,12 @@ public class ApiTestCaseBatchRunService {
* @param apiTestCases * @param apiTestCases
* @return * @return
*/ */
public List<ApiTestCaseRecord> initApiReport(ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, String userId) { public List<ApiTestCaseRecord> initApiReport(Map<String, String> resourceExecTaskItemMap,
ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, String userId) {
List<ApiReport> apiReports = new ArrayList<>(); List<ApiReport> apiReports = new ArrayList<>();
List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>(); List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>();
List<ApiReportStep> apiReportSteps = new ArrayList<>(); List<ApiReportStep> apiReportSteps = new ArrayList<>();
List<ApiReportRelateTask> apiReportRelateTasks = new ArrayList<>();
for (ApiTestCase apiTestCase : apiTestCases) { for (ApiTestCase apiTestCase : apiTestCases) {
// 初始化报告 // 初始化报告
ApiReport apiReport = getApiReport(runModeConfig, apiTestCase, userId); ApiReport apiReport = getApiReport(runModeConfig, apiTestCase, userId);
@ -440,8 +454,15 @@ public class ApiTestCaseBatchRunService {
ApiTestCaseRecord apiTestCaseRecord = apiTestCaseService.getApiTestCaseRecord(apiTestCase, apiReport); ApiTestCaseRecord apiTestCaseRecord = apiTestCaseService.getApiTestCaseRecord(apiTestCase, apiReport);
apiTestCaseRecords.add(apiTestCaseRecord); apiTestCaseRecords.add(apiTestCaseRecord);
apiReportSteps.add(getApiReportStep(apiTestCase, apiReport.getId(), 1)); apiReportSteps.add(getApiReportStep(apiTestCase, apiReport.getId(), 1));
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiReport.getId());
apiReportRelateTask.setTaskResourceId(resourceExecTaskItemMap.get(apiTestCase.getId()));
apiReportRelateTasks.add(apiReportRelateTask);
} }
apiReportService.insertApiReport(apiReports, apiTestCaseRecords);
apiReportService.insertApiReport(apiReports, apiTestCaseRecords, apiReportRelateTasks);
apiReportService.insertApiReportStep(apiReportSteps); apiReportService.insertApiReportStep(apiReportSteps);
return apiTestCaseRecords; return apiTestCaseRecords;
} }

View File

@ -760,7 +760,7 @@ public class ApiTestCaseService extends MoveNodeService {
} }
// 初始化报告 // 初始化报告
initApiReport(apiTestCase, reportId, poolId, userId); initApiReport(taskItem.getId(), apiTestCase, reportId, poolId, userId);
return doExecute(taskRequest, runRequest, apiTestCase.getApiDefinitionId(), apiTestCase.getEnvironmentId()); return doExecute(taskRequest, runRequest, apiTestCase.getApiDefinitionId(), apiTestCase.getEnvironmentId());
} }
@ -861,7 +861,7 @@ public class ApiTestCaseService extends MoveNodeService {
* @param userId * @param userId
* @return * @return
*/ */
public ApiTestCaseRecord initApiReport(ApiTestCase apiTestCase, String reportId, String poolId, String userId) { public ApiTestCaseRecord initApiReport(String taskItemId, ApiTestCase apiTestCase, String reportId, String poolId, String userId) {
// 初始化报告 // 初始化报告
ApiReport apiReport = getApiReport(apiTestCase, reportId, poolId, userId); ApiReport apiReport = getApiReport(apiTestCase, reportId, poolId, userId);
@ -869,9 +869,15 @@ public class ApiTestCaseService extends MoveNodeService {
// 创建报告和用例的关联关系 // 创建报告和用例的关联关系
ApiTestCaseRecord apiTestCaseRecord = getApiTestCaseRecord(apiTestCase, apiReport); ApiTestCaseRecord apiTestCaseRecord = getApiTestCaseRecord(apiTestCase, apiReport);
apiReportService.insertApiReport(List.of(apiReport), List.of(apiTestCaseRecord)); // 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(reportId);
apiReportRelateTask.setTaskResourceId(taskItemId);
apiReportService.insertApiReport(List.of(apiReport), List.of(apiTestCaseRecord), List.of(apiReportRelateTask));
//初始化步骤 //初始化步骤
apiReportService.insertApiReportStep(List.of(getApiReportStep(apiTestCase.getId(), apiTestCase.getName(), reportId, 1L))); apiReportService.insertApiReportStep(List.of(getApiReportStep(apiTestCase.getId(), apiTestCase.getName(), reportId, 1L)));
return apiTestCaseRecord; return apiTestCaseRecord;
} }

View File

@ -1,9 +1,6 @@
package io.metersphere.api.service.scenario; package io.metersphere.api.service.scenario;
import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.*;
import io.metersphere.api.domain.ApiScenarioRecord;
import io.metersphere.api.domain.ApiScenarioReport;
import io.metersphere.api.domain.ApiScenarioReportStep;
import io.metersphere.api.dto.scenario.ApiScenarioBatchRunRequest; import io.metersphere.api.dto.scenario.ApiScenarioBatchRunRequest;
import io.metersphere.api.dto.scenario.ApiScenarioDetail; import io.metersphere.api.dto.scenario.ApiScenarioDetail;
import io.metersphere.api.mapper.ApiScenarioMapper; import io.metersphere.api.mapper.ApiScenarioMapper;
@ -115,7 +112,7 @@ public class ApiScenarioBatchRunService {
// 初始化集成报告 // 初始化集成报告
if (runModeConfig.isIntegratedReport()) { if (runModeConfig.isIntegratedReport()) {
initIntegratedReport(runModeConfig, userId, request.getProjectId()); initIntegratedReport(execTask.getId(), runModeConfig, userId, request.getProjectId());
} }
// 先初始化集成报告设置好报告ID再初始化执行队列 // 先初始化集成报告设置好报告ID再初始化执行队列
@ -136,8 +133,12 @@ public class ApiScenarioBatchRunService {
// 集合报告初始化一级步骤 // 集合报告初始化一级步骤
initApiScenarioReportStep(apiScenarios, reportId); initApiScenarioReportStep(apiScenarios, reportId);
} else { } else {
// 初始化任务项
Map<String, String> resourceExecTaskItemMap = execTaskItems
.stream()
.collect(Collectors.toMap(ExecTaskItem::getResourceId, ExecTaskItem::getId));
// 非集合报告初始化独立报告执行时初始化步骤 // 非集合报告初始化独立报告执行时初始化步骤
initScenarioReport(runModeConfig, apiScenarios, userId); initScenarioReport(resourceExecTaskItemMap, runModeConfig, apiScenarios, userId);
} }
// 初始化队列项 // 初始化队列项
apiBatchRunBaseService.initExecutionQueueDetails(queue.getQueueId(), execTaskItems); apiBatchRunBaseService.initExecutionQueueDetails(queue.getQueueId(), execTaskItems);
@ -165,26 +166,27 @@ public class ApiScenarioBatchRunService {
if (runModeConfig.isIntegratedReport()) { if (runModeConfig.isIntegratedReport()) {
// 初始化集成报告 // 初始化集成报告
initIntegratedReport(runModeConfig, userId, request.getProjectId()); initIntegratedReport(execTask.getId(), runModeConfig, userId, request.getProjectId());
} }
// 分批查询 // 分批查询
SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> { SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> {
List<ApiScenario> apiScenarios = getOrderScenarios(subIds, runModeConfig); List<ApiScenario> apiScenarios = getOrderScenarios(subIds, runModeConfig);
Map<String, String> caseReportMap = null; Map<String, String> caseReportMap = null;
if (runModeConfig.isIntegratedReport()) {
// 集合报告初始化一级步骤
initApiScenarioReportStep(apiScenarios, runModeConfig.getCollectionReport().getReportId());
} else {
// 非集合报告初始化独立报告执行时初始化步骤
caseReportMap = initScenarioReport(runModeConfig, apiScenarios, userId);
}
// 初始化任务项 // 初始化任务项
Map<String, String> resourceExecTaskItemMap = initExecTaskItem(subIds, apiScenarios, userId, project, execTask) Map<String, String> resourceExecTaskItemMap = initExecTaskItem(subIds, apiScenarios, userId, project, execTask)
.stream() .stream()
.collect(Collectors.toMap(ExecTaskItem::getResourceId, ExecTaskItem::getId)); .collect(Collectors.toMap(ExecTaskItem::getResourceId, ExecTaskItem::getId));
if (runModeConfig.isIntegratedReport()) {
// 集合报告初始化一级步骤
initApiScenarioReportStep(apiScenarios, runModeConfig.getCollectionReport().getReportId());
} else {
// 非集合报告初始化独立报告执行时初始化步骤
caseReportMap = initScenarioReport(resourceExecTaskItemMap, runModeConfig, apiScenarios, userId);
}
List<TaskItem> taskItems = new ArrayList<>(ids.size()); List<TaskItem> taskItems = new ArrayList<>(ids.size());
for (ApiScenario apiScenario : apiScenarios) { for (ApiScenario apiScenario : apiScenarios) {
@ -302,12 +304,18 @@ public class ApiScenarioBatchRunService {
* @param runModeConfig * @param runModeConfig
* @return * @return
*/ */
private ApiScenarioReport initIntegratedReport(ApiRunModeConfigDTO runModeConfig, String userId, String projectId) { private ApiScenarioReport initIntegratedReport(String taskId, ApiRunModeConfigDTO runModeConfig, String userId, String projectId) {
ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, userId); ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, userId);
apiScenarioReport.setName(runModeConfig.getCollectionReport().getReportName() + "_" + DateUtils.getTimeString(System.currentTimeMillis())); apiScenarioReport.setName(runModeConfig.getCollectionReport().getReportName() + "_" + DateUtils.getTimeString(System.currentTimeMillis()));
apiScenarioReport.setIntegrated(true); apiScenarioReport.setIntegrated(true);
apiScenarioReport.setProjectId(projectId); apiScenarioReport.setProjectId(projectId);
apiScenarioReportMapper.insertSelective(apiScenarioReport);
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiScenarioReport.getId());
apiReportRelateTask.setTaskResourceId(taskId);
apiScenarioReportService.insertApiScenarioReport(apiScenarioReport, apiReportRelateTask);
// 设置集成报告执行参数 // 设置集成报告执行参数
runModeConfig.getCollectionReport().setReportId(apiScenarioReport.getId()); runModeConfig.getCollectionReport().setReportId(apiScenarioReport.getId());
return apiScenarioReport; return apiScenarioReport;
@ -345,7 +353,7 @@ public class ApiScenarioBatchRunService {
reportId = runModeConfig.getCollectionReport().getReportId() + IDGenerator.nextStr(); reportId = runModeConfig.getCollectionReport().getReportId() + IDGenerator.nextStr();
} else { } else {
// 独立报告执行到当前任务时初始化报告 // 独立报告执行到当前任务时初始化报告
reportId = initScenarioReport(runModeConfig, apiScenario, queue.getUserId()).getApiScenarioReportId(); reportId = initScenarioReport(queueDetail.getTaskItemId(), runModeConfig, apiScenario, queue.getUserId()).getApiScenarioReportId();
} }
TaskRequestDTO taskRequest = getTaskRequestDTO(apiScenario.getProjectId(), queue.getRunModeConfig()); TaskRequestDTO taskRequest = getTaskRequestDTO(apiScenario.getProjectId(), queue.getRunModeConfig());
@ -379,9 +387,11 @@ public class ApiScenarioBatchRunService {
return apiBatchRunBaseService.setBatchRunTaskInfoParam(runModeConfig, taskInfo); return apiBatchRunBaseService.setBatchRunTaskInfoParam(runModeConfig, taskInfo);
} }
public Map<String, String> initScenarioReport(ApiRunModeConfigDTO runModeConfig, List<ApiScenario> apiScenarios, String userId) { public Map<String, String> initScenarioReport(Map<String, String> resourceExecTaskItemMap, ApiRunModeConfigDTO runModeConfig,
List<ApiScenario> apiScenarios, String userId) {
List<ApiScenarioReport> apiScenarioReports = new ArrayList<>(apiScenarios.size()); List<ApiScenarioReport> apiScenarioReports = new ArrayList<>(apiScenarios.size());
List<ApiScenarioRecord> apiScenarioRecords = new ArrayList<>(apiScenarios.size()); List<ApiScenarioRecord> apiScenarioRecords = new ArrayList<>(apiScenarios.size());
List<ApiReportRelateTask> apiReportRelateTasks = new ArrayList<>();
for (ApiScenario apiScenario : apiScenarios) { for (ApiScenario apiScenario : apiScenarios) {
// 初始化报告 // 初始化报告
ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, apiScenario, userId); ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, apiScenario, userId);
@ -390,8 +400,13 @@ public class ApiScenarioBatchRunService {
// 创建报告和用例的关联关系 // 创建报告和用例的关联关系
ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(apiScenario, apiScenarioReport); ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(apiScenario, apiScenarioReport);
apiScenarioRecords.add(apiScenarioRecord); apiScenarioRecords.add(apiScenarioRecord);
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiScenarioReport.getId());
apiReportRelateTask.setTaskResourceId(resourceExecTaskItemMap.get(apiScenario.getId()));
apiReportRelateTasks.add(apiReportRelateTask);
} }
apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords); apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords, apiReportRelateTasks);
return apiScenarioRecords.stream().collect(Collectors.toMap(ApiScenarioRecord::getApiScenarioId, ApiScenarioRecord::getApiScenarioReportId)); return apiScenarioRecords.stream().collect(Collectors.toMap(ApiScenarioRecord::getApiScenarioId, ApiScenarioRecord::getApiScenarioReportId));
} }
@ -402,13 +417,19 @@ public class ApiScenarioBatchRunService {
* @param apiScenario * @param apiScenario
* @return * @return
*/ */
public ApiScenarioRecord initScenarioReport(ApiRunModeConfigDTO runModeConfig, ApiScenario apiScenario, String userId) { public ApiScenarioRecord initScenarioReport(String taskItemId, ApiRunModeConfigDTO runModeConfig, ApiScenario apiScenario, String userId) {
// 初始化报告 // 初始化报告
ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, apiScenario, userId); ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, apiScenario, userId);
apiScenarioReport.setId(IDGenerator.nextStr()); apiScenarioReport.setId(IDGenerator.nextStr());
// 创建报告和用例的关联关系 // 创建报告和用例的关联关系
ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(apiScenario, apiScenarioReport); ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(apiScenario, apiScenarioReport);
apiScenarioReportService.insertApiScenarioReport(List.of(apiScenarioReport), List.of(apiScenarioRecord));
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiScenarioReport.getId());
apiReportRelateTask.setTaskResourceId(taskItemId);
apiScenarioReportService.insertApiScenarioReport(List.of(apiScenarioReport), List.of(apiScenarioRecord), List.of(apiReportRelateTask));
return apiScenarioRecord; return apiScenarioRecord;
} }

View File

@ -53,6 +53,8 @@ public class ApiScenarioReportService {
@Resource @Resource
private ApiScenarioReportMapper apiScenarioReportMapper; private ApiScenarioReportMapper apiScenarioReportMapper;
@Resource @Resource
private ApiReportRelateTaskMapper apiReportRelateTaskMapper;
@Resource
private ApiScenarioReportLogService apiScenarioReportLogService; private ApiScenarioReportLogService apiScenarioReportLogService;
@Resource @Resource
private ExtApiScenarioReportDetailBlobMapper extApiScenarioReportDetailBlobMapper; private ExtApiScenarioReportDetailBlobMapper extApiScenarioReportDetailBlobMapper;
@ -72,8 +74,19 @@ public class ApiScenarioReportService {
private static final int MAX = 50000; private static final int MAX = 50000;
private static final int BATCH_SIZE = 1000; private static final int BATCH_SIZE = 1000;
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiScenarioReport(ApiScenarioReport report, ApiReportRelateTask taskRelation) {
apiScenarioReportMapper.insertSelective(report);
apiReportRelateTaskMapper.insertSelective(taskRelation);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiScenarioReport(List<ApiScenarioReport> reports, List<ApiScenarioRecord> records) { public void insertApiScenarioReport(List<ApiScenarioReport> reports, List<ApiScenarioRecord> records) {
this.insertApiScenarioReport(reports, records, null);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiScenarioReport(List<ApiScenarioReport> reports, List<ApiScenarioRecord> records, List<ApiReportRelateTask> taskRelations) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
if (CollectionUtils.isNotEmpty(reports)) { if (CollectionUtils.isNotEmpty(reports)) {
ApiScenarioReportMapper reportMapper = sqlSession.getMapper(ApiScenarioReportMapper.class); ApiScenarioReportMapper reportMapper = sqlSession.getMapper(ApiScenarioReportMapper.class);
@ -87,6 +100,12 @@ public class ApiScenarioReportService {
subList.forEach(detailMapper::insertSelective); subList.forEach(detailMapper::insertSelective);
}); });
} }
if (CollectionUtils.isNotEmpty(taskRelations)) {
ApiReportRelateTaskMapper taskRelationMapper = sqlSession.getMapper(ApiReportRelateTaskMapper.class);
SubListUtils.dealForSubList(taskRelations, 1000, subList -> {
subList.forEach(taskRelationMapper::insertSelective);
});
}
sqlSession.flushStatements(); sqlSession.flushStatements();
if (sqlSessionFactory != null) { if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);

View File

@ -247,7 +247,7 @@ public class ApiScenarioRunService {
scenarioReport.setEnvironmentId(parseParam.getEnvironmentId()); scenarioReport.setEnvironmentId(parseParam.getEnvironmentId());
scenarioReport.setWaitingTime(getGlobalWaitTime(parseParam.getScenarioConfig())); scenarioReport.setWaitingTime(getGlobalWaitTime(parseParam.getScenarioConfig()));
initApiReport(apiScenario, scenarioReport); initApiReport(taskItem.getId(), apiScenario, scenarioReport);
// 初始化报告步骤 // 初始化报告步骤
initScenarioReportSteps(steps, taskItem.getReportId()); initScenarioReportSteps(steps, taskItem.getReportId());
@ -476,7 +476,7 @@ public class ApiScenarioRunService {
* @param apiScenario * @param apiScenario
* @return * @return
*/ */
public ApiScenarioRecord initApiReport(ApiScenario apiScenario, ApiScenarioReport scenarioReport) { public ApiScenarioRecord initApiReport(String taskItemId, ApiScenario apiScenario, ApiScenarioReport scenarioReport) {
// 初始化报告 // 初始化报告
scenarioReport.setName(apiScenario.getName() + "_" + DateUtils.getTimeString(System.currentTimeMillis())); scenarioReport.setName(apiScenario.getName() + "_" + DateUtils.getTimeString(System.currentTimeMillis()));
scenarioReport.setProjectId(apiScenario.getProjectId()); scenarioReport.setProjectId(apiScenario.getProjectId());
@ -484,7 +484,12 @@ public class ApiScenarioRunService {
// 创建报告和用例的关联关系 // 创建报告和用例的关联关系
ApiScenarioRecord scenarioRecord = getApiScenarioRecord(apiScenario, scenarioReport); ApiScenarioRecord scenarioRecord = getApiScenarioRecord(apiScenario, scenarioReport);
apiScenarioReportService.insertApiScenarioReport(List.of(scenarioReport), List.of(scenarioRecord)); // 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(scenarioReport.getId());
apiReportRelateTask.setTaskResourceId(taskItemId);
apiScenarioReportService.insertApiScenarioReport(List.of(scenarioReport), List.of(scenarioRecord), List.of(apiReportRelateTask));
return scenarioRecord; return scenarioRecord;
} }

View File

@ -1,9 +1,6 @@
package io.metersphere.plan.service; package io.metersphere.plan.service;
import io.metersphere.api.domain.ApiReport; import io.metersphere.api.domain.*;
import io.metersphere.api.domain.ApiReportStep;
import io.metersphere.api.domain.ApiTestCase;
import io.metersphere.api.domain.ApiTestCaseRecord;
import io.metersphere.api.mapper.ApiTestCaseMapper; import io.metersphere.api.mapper.ApiTestCaseMapper;
import io.metersphere.api.mapper.ExtApiTestCaseMapper; import io.metersphere.api.mapper.ExtApiTestCaseMapper;
import io.metersphere.api.service.ApiBatchRunBaseService; import io.metersphere.api.service.ApiBatchRunBaseService;
@ -146,7 +143,7 @@ public class PlanRunTestPlanApiCaseService {
ApiRunModeConfigDTO runModeConfig = testPlanApiBatchRunBaseService.getApiRunModeConfig(collection); ApiRunModeConfigDTO runModeConfig = testPlanApiBatchRunBaseService.getApiRunModeConfig(collection);
// 初始化报告返回用例和报告的 map // 初始化报告返回用例和报告的 map
Map<String, String> caseReportMap = initApiReport(runModeConfig, testPlanReportApiCases, apiCaseMap, userId); Map<String, String> caseReportMap = initApiReport(resourceTaskItemMap, runModeConfig, testPlanReportApiCases, apiCaseMap, userId);
List<TaskItem> taskItems = new ArrayList<>(testPlanReportApiCases.size()); List<TaskItem> taskItems = new ArrayList<>(testPlanReportApiCases.size());
@ -210,7 +207,7 @@ public class PlanRunTestPlanApiCaseService {
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(testPlanReportApiCase.getApiCaseId()); ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(testPlanReportApiCase.getApiCaseId());
// 独立报告执行到当前任务时初始化报告 // 独立报告执行到当前任务时初始化报告
String reportId = initApiReport(runModeConfig, List.of(testPlanReportApiCase), Map.of(apiTestCase.getId(), apiTestCase), queue.getUserId()).get(resourceId); String reportId = initApiReport(Map.of(apiTestCase.getId(), queueDetail.getTaskItemId()), runModeConfig, List.of(testPlanReportApiCase), Map.of(apiTestCase.getId(), apiTestCase), queue.getUserId()).get(resourceId);
TaskRequestDTO taskRequest = testPlanApiCaseBatchRunService.getTaskRequestDTO(reportId, testPlanReportApiCase.getId(), apiTestCase, runModeConfig); TaskRequestDTO taskRequest = testPlanApiCaseBatchRunService.getTaskRequestDTO(reportId, testPlanReportApiCase.getId(), apiTestCase, runModeConfig);
taskRequest.getTaskInfo().setTaskId(queue.getTaskId()); taskRequest.getTaskInfo().setTaskId(queue.getTaskId());
taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_CASE.name()); taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_CASE.name());
@ -229,13 +226,15 @@ public class PlanRunTestPlanApiCaseService {
* @param runModeConfig * @param runModeConfig
* @return * @return
*/ */
public Map<String, String> initApiReport(ApiRunModeConfigDTO runModeConfig, List<TestPlanReportApiCase> testPlanReportApiCases, public Map<String, String> initApiReport(Map<String, String> resourceExecTaskItemMap,
ApiRunModeConfigDTO runModeConfig, List<TestPlanReportApiCase> testPlanReportApiCases,
Map<String, ApiTestCase> caseMap, String userId) { Map<String, ApiTestCase> caseMap, String userId) {
Map<String, String> resourceReportMap = new HashMap<>(); Map<String, String> resourceReportMap = new HashMap<>();
List<ApiReport> apiReports = new ArrayList<>(); List<ApiReport> apiReports = new ArrayList<>();
List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>(); List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>();
List<ApiReportStep> apiReportSteps = new ArrayList<>(); List<ApiReportStep> apiReportSteps = new ArrayList<>();
List<ApiReportRelateTask> apiReportRelateTasks = new ArrayList<>();
for (TestPlanReportApiCase testPlanReportApiCase : testPlanReportApiCases) { for (TestPlanReportApiCase testPlanReportApiCase : testPlanReportApiCases) {
ApiTestCase apiTestCase = caseMap.get(testPlanReportApiCase.getApiCaseId()); ApiTestCase apiTestCase = caseMap.get(testPlanReportApiCase.getApiCaseId());
@ -253,8 +252,14 @@ public class PlanRunTestPlanApiCaseService {
apiTestCaseRecords.add(apiTestCaseRecord); apiTestCaseRecords.add(apiTestCaseRecord);
apiReportSteps.add(testPlanApiCaseBatchRunService.getApiReportStep(testPlanReportApiCase.getId(), apiTestCase.getName(), apiReport.getId(), 1)); apiReportSteps.add(testPlanApiCaseBatchRunService.getApiReportStep(testPlanReportApiCase.getId(), apiTestCase.getName(), apiReport.getId(), 1));
resourceReportMap.put(testPlanReportApiCase.getId(), apiReport.getId()); resourceReportMap.put(testPlanReportApiCase.getId(), apiReport.getId());
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiReport.getId());
apiReportRelateTask.setTaskResourceId(resourceExecTaskItemMap.get(apiReport.getId()));
apiReportRelateTasks.add(apiReportRelateTask);
} }
apiReportService.insertApiReport(apiReports, apiTestCaseRecords); apiReportService.insertApiReport(apiReports, apiTestCaseRecords, apiReportRelateTasks);
apiReportService.insertApiReportStep(apiReportSteps); apiReportService.insertApiReportStep(apiReportSteps);
return resourceReportMap; return resourceReportMap;
} }

View File

@ -1,5 +1,6 @@
package io.metersphere.plan.service; package io.metersphere.plan.service;
import io.metersphere.api.domain.ApiReportRelateTask;
import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.domain.ApiScenarioRecord; import io.metersphere.api.domain.ApiScenarioRecord;
import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiScenarioReport;
@ -135,7 +136,7 @@ public class PlanRunTestPlanApiScenarioService {
.forEach(execTaskItem -> resourceTaskItemMap.put(execTaskItem.getResourceId(), execTaskItem.getId())); .forEach(execTaskItem -> resourceTaskItemMap.put(execTaskItem.getResourceId(), execTaskItem.getId()));
}); });
Map<String, String> scenarioReportMap = initReport(testPlanReportApiScenarios, runModeConfig, userId); Map<String, String> scenarioReportMap = initReport(resourceTaskItemMap, testPlanReportApiScenarios, runModeConfig, userId);
List<TaskItem> taskItems = testPlanReportApiScenarios.stream() List<TaskItem> taskItems = testPlanReportApiScenarios.stream()
.map(item -> { .map(item -> {
TaskItem taskItem = apiExecuteService.getTaskItem(scenarioReportMap.get(item.getId()), item.getId()); TaskItem taskItem = apiExecuteService.getTaskItem(scenarioReportMap.get(item.getId()), item.getId());
@ -152,7 +153,9 @@ public class PlanRunTestPlanApiScenarioService {
return false; return false;
} }
private Map<String, String> initReport(List<TestPlanReportApiScenario> testPlanReportApiScenarios, ApiRunModeConfigDTO runModeConfig, String userId) { private Map<String, String> initReport(Map<String, String> resourceExecTaskItemMap,
List<TestPlanReportApiScenario> testPlanReportApiScenarios,
ApiRunModeConfigDTO runModeConfig, String userId) {
List<ApiScenario> apiScenarios = new ArrayList<>(testPlanReportApiScenarios.size()); List<ApiScenario> apiScenarios = new ArrayList<>(testPlanReportApiScenarios.size());
@ -165,13 +168,17 @@ public class PlanRunTestPlanApiScenarioService {
.collect(Collectors.toMap(ApiScenario::getId, Function.identity())); .collect(Collectors.toMap(ApiScenario::getId, Function.identity()));
// 初始化独立报告执行时初始化步骤 // 初始化独立报告执行时初始化步骤
return initScenarioReport(runModeConfig, testPlanReportApiScenarios, apiScenarioMap, userId); return initScenarioReport(resourceExecTaskItemMap, runModeConfig, testPlanReportApiScenarios, apiScenarioMap, userId);
} }
public Map<String, String> initScenarioReport(ApiRunModeConfigDTO runModeConfig, List<TestPlanReportApiScenario> testPlanReportApiScenarios, public Map<String, String> initScenarioReport(Map<String, String> resourceExecTaskItemMap,
ApiRunModeConfigDTO runModeConfig,
List<TestPlanReportApiScenario> testPlanReportApiScenarios,
Map<String, ApiScenario> apiScenarioMap, String userId) { Map<String, ApiScenario> apiScenarioMap, String userId) {
List<ApiScenarioReport> apiScenarioReports = new ArrayList<>(testPlanReportApiScenarios.size()); List<ApiScenarioReport> apiScenarioReports = new ArrayList<>(testPlanReportApiScenarios.size());
List<ApiScenarioRecord> apiScenarioRecords = new ArrayList<>(testPlanReportApiScenarios.size()); List<ApiScenarioRecord> apiScenarioRecords = new ArrayList<>(testPlanReportApiScenarios.size());
List<ApiReportRelateTask> apiReportRelateTasks = new ArrayList<>();
Map<String, String> resourceReportMap = new HashMap<>(); Map<String, String> resourceReportMap = new HashMap<>();
String projectId = ""; String projectId = "";
for (TestPlanReportApiScenario testPlanReportApiScenario : testPlanReportApiScenarios) { for (TestPlanReportApiScenario testPlanReportApiScenario : testPlanReportApiScenarios) {
@ -191,8 +198,14 @@ public class PlanRunTestPlanApiScenarioService {
scenarioRecord.setApiScenarioReportId(apiScenarioReport.getId()); scenarioRecord.setApiScenarioReportId(apiScenarioReport.getId());
apiScenarioRecords.add(scenarioRecord); apiScenarioRecords.add(scenarioRecord);
resourceReportMap.put(testPlanReportApiScenario.getId(), apiScenarioReport.getId()); resourceReportMap.put(testPlanReportApiScenario.getId(), apiScenarioReport.getId());
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiScenario.getId());
apiReportRelateTask.setTaskResourceId(resourceExecTaskItemMap.get(apiScenario.getId()));
apiReportRelateTasks.add(apiReportRelateTask);
} }
apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords); apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords, apiReportRelateTasks);
return resourceReportMap; return resourceReportMap;
} }
@ -239,7 +252,7 @@ public class PlanRunTestPlanApiScenarioService {
ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(testPlanReportApiScenario.getApiScenarioId()); ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(testPlanReportApiScenario.getApiScenarioId());
// 独立报告执行到当前任务时初始化报告 // 独立报告执行到当前任务时初始化报告
String reportId = initScenarioReport(runModeConfig, List.of(testPlanReportApiScenario), Map.of(apiScenario.getId(), apiScenario), queue.getUserId()).get(resourceId); String reportId = initScenarioReport(Map.of(testPlanReportApiScenario.getId(), queueDetail.getTaskItemId()), runModeConfig, List.of(testPlanReportApiScenario), Map.of(apiScenario.getId(), apiScenario), queue.getUserId()).get(resourceId);
TaskRequestDTO taskRequest = testPlanApiScenarioBatchRunService.getTaskRequestDTO(apiScenario.getProjectId(), queue.getRunModeConfig()); TaskRequestDTO taskRequest = testPlanApiScenarioBatchRunService.getTaskRequestDTO(apiScenario.getProjectId(), queue.getRunModeConfig());
TaskItem taskItem = apiExecuteService.getTaskItem(reportId, queueDetail.getResourceId()); TaskItem taskItem = apiExecuteService.getTaskItem(reportId, queueDetail.getResourceId());
taskItem.setId(queueDetail.getTaskItemId()); taskItem.setId(queueDetail.getTaskItemId());

View File

@ -1,9 +1,6 @@
package io.metersphere.plan.service; package io.metersphere.plan.service;
import io.metersphere.api.domain.ApiReport; import io.metersphere.api.domain.*;
import io.metersphere.api.domain.ApiReportStep;
import io.metersphere.api.domain.ApiTestCase;
import io.metersphere.api.domain.ApiTestCaseRecord;
import io.metersphere.api.mapper.ApiTestCaseMapper; import io.metersphere.api.mapper.ApiTestCaseMapper;
import io.metersphere.api.mapper.ExtApiTestCaseMapper; import io.metersphere.api.mapper.ExtApiTestCaseMapper;
import io.metersphere.api.service.ApiBatchRunBaseService; import io.metersphere.api.service.ApiBatchRunBaseService;
@ -259,11 +256,11 @@ public class TestPlanApiCaseBatchRunService {
Project project, Project project,
String userId) { String userId) {
// 初始化报告返回用例和报告的 map
Map<String, String> caseReportMap = initApiReport(runModeConfig, testPlanApiCases, project.getId(), userId);
Map<String, String> resourceTaskItemMap = getResourceTaskItemMap(taskId, testPlanApiCases); Map<String, String> resourceTaskItemMap = getResourceTaskItemMap(taskId, testPlanApiCases);
// 初始化报告返回用例和报告的 map
Map<String, String> caseReportMap = initApiReport(resourceTaskItemMap, runModeConfig, testPlanApiCases, project.getId(), userId);
List<TaskItem> taskItems = new ArrayList<>(testPlanApiCases.size()); List<TaskItem> taskItems = new ArrayList<>(testPlanApiCases.size());
// 这里ID顺序和队列的ID顺序保持一致 // 这里ID顺序和队列的ID顺序保持一致
@ -403,7 +400,7 @@ public class TestPlanApiCaseBatchRunService {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanId); TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanId);
// 独立报告执行到当前任务时初始化报告 // 独立报告执行到当前任务时初始化报告
String reportId = initApiReport(runModeConfig, List.of(BeanUtils.copyBean(new TestPlanApiCaseBatchRunDTO(), testPlanApiCase)), String reportId = initApiReport(Map.of(apiTestCase.getId(), queueDetail.getTaskItemId()), runModeConfig, List.of(BeanUtils.copyBean(new TestPlanApiCaseBatchRunDTO(), testPlanApiCase)),
testPlan.getProjectId(), queue.getUserId()).get(testPlanApiCase.getId()); testPlan.getProjectId(), queue.getUserId()).get(testPlanApiCase.getId());
TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, testPlanApiCase.getId(), apiTestCase, runModeConfig); TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, testPlanApiCase.getId(), apiTestCase, runModeConfig);
taskRequest.getTaskInfo().setTaskId(queue.getTaskId()); taskRequest.getTaskInfo().setTaskId(queue.getTaskId());
@ -446,10 +443,11 @@ public class TestPlanApiCaseBatchRunService {
* @param runModeConfig * @param runModeConfig
* @return * @return
*/ */
public Map<String, String> initApiReport(ApiRunModeConfigDTO runModeConfig, List<TestPlanApiCaseBatchRunDTO> testPlanApiCases, public Map<String, String> initApiReport(Map<String, String> resourceExecTaskItemMap, ApiRunModeConfigDTO runModeConfig, List<TestPlanApiCaseBatchRunDTO> testPlanApiCases,
String projectId, String userId) { String projectId, String userId) {
List<ApiReport> apiReports = new ArrayList<>(); List<ApiReport> apiReports = new ArrayList<>();
List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>(); List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>();
List<ApiReportRelateTask> apiReportRelateTasks = new ArrayList<>();
List<ApiReportStep> apiReportSteps = new ArrayList<>(); List<ApiReportStep> apiReportSteps = new ArrayList<>();
Map<String, String> resourceReportMap = new HashMap<>(); Map<String, String> resourceReportMap = new HashMap<>();
@ -462,8 +460,14 @@ public class TestPlanApiCaseBatchRunService {
apiTestCaseRecords.add(apiTestCaseRecord); apiTestCaseRecords.add(apiTestCaseRecord);
apiReportSteps.add(getApiReportStep(testPlanApiCase.getId(), testPlanApiCase.getName(), apiReport.getId(), 1)); apiReportSteps.add(getApiReportStep(testPlanApiCase.getId(), testPlanApiCase.getName(), apiReport.getId(), 1));
resourceReportMap.put(testPlanApiCase.getId(), apiReport.getId()); resourceReportMap.put(testPlanApiCase.getId(), apiReport.getId());
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiReport.getId());
apiReportRelateTask.setTaskResourceId(resourceExecTaskItemMap.get(apiReport.getId()));
apiReportRelateTasks.add(apiReportRelateTask);
} }
apiReportService.insertApiReport(apiReports, apiTestCaseRecords); apiReportService.insertApiReport(apiReports, apiTestCaseRecords, apiReportRelateTasks);
apiReportService.insertApiReportStep(apiReportSteps); apiReportService.insertApiReportStep(apiReportSteps);
return resourceReportMap; return resourceReportMap;
} }

View File

@ -786,7 +786,7 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
} }
// 初始化报告 // 初始化报告
initApiReport(apiTestCase, testPlanApiCase, reportId, runModeConfig, userId); initApiReport(taskItem.getId(), apiTestCase, testPlanApiCase, reportId, runModeConfig, userId);
return apiExecuteService.execute(taskRequest); return apiExecuteService.execute(taskRequest);
} }
@ -811,7 +811,7 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
* *
* @return * @return
*/ */
public ApiTestCaseRecord initApiReport(ApiTestCase apiTestCase, TestPlanApiCase testPlanApiCase, String reportId, ApiRunModeConfigDTO runModeConfig, String userId) { public ApiTestCaseRecord initApiReport(String taskItemId, ApiTestCase apiTestCase, TestPlanApiCase testPlanApiCase, String reportId, ApiRunModeConfigDTO runModeConfig, String userId) {
// 初始化报告 // 初始化报告
ApiReport apiReport = apiTestCaseService.getApiReport(apiTestCase, reportId, runModeConfig.getPoolId(), userId); ApiReport apiReport = apiTestCaseService.getApiReport(apiTestCase, reportId, runModeConfig.getPoolId(), userId);
apiReport.setEnvironmentId(runModeConfig.getEnvironmentId()); apiReport.setEnvironmentId(runModeConfig.getEnvironmentId());
@ -820,7 +820,12 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
// 创建报告和用例的关联关系 // 创建报告和用例的关联关系
ApiTestCaseRecord apiTestCaseRecord = apiTestCaseService.getApiTestCaseRecord(apiTestCase, apiReport); ApiTestCaseRecord apiTestCaseRecord = apiTestCaseService.getApiTestCaseRecord(apiTestCase, apiReport);
apiReportService.insertApiReport(List.of(apiReport), List.of(apiTestCaseRecord)); // 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiReport.getId());
apiReportRelateTask.setTaskResourceId(taskItemId);
apiReportService.insertApiReport(List.of(apiReport), List.of(apiTestCaseRecord), List.of(apiReportRelateTask));
//初始化步骤 //初始化步骤
apiReportService.insertApiReportStep(List.of(getApiReportStep(testPlanApiCase, apiTestCase, reportId))); apiReportService.insertApiReportStep(List.of(getApiReportStep(testPlanApiCase, apiTestCase, reportId)));

View File

@ -1,5 +1,6 @@
package io.metersphere.plan.service; package io.metersphere.plan.service;
import io.metersphere.api.domain.ApiReportRelateTask;
import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.domain.ApiScenarioRecord; import io.metersphere.api.domain.ApiScenarioRecord;
import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiScenarioReport;
@ -265,10 +266,10 @@ public class TestPlanApiScenarioBatchRunService {
Project project, Project project,
String userId) { String userId) {
Map<String, String> scenarioReportMap = initReport(testPlanApiScenarios, runModeConfig, project.getId(), userId);
Map<String, String> resourceTaskItemMap = getResourceTaskItemMap(taskId, testPlanApiScenarios); Map<String, String> resourceTaskItemMap = getResourceTaskItemMap(taskId, testPlanApiScenarios);
Map<String, String> scenarioReportMap = initReport(resourceTaskItemMap, testPlanApiScenarios, runModeConfig, project.getId(), userId);
List<TaskItem> taskItems = testPlanApiScenarios.stream() List<TaskItem> taskItems = testPlanApiScenarios.stream()
.map(testPlanApiScenario -> { .map(testPlanApiScenario -> {
String id = testPlanApiScenario.getId(); String id = testPlanApiScenario.getId();
@ -329,10 +330,12 @@ public class TestPlanApiScenarioBatchRunService {
return execTaskItems; return execTaskItems;
} }
public Map<String, String> initReport(List<TestPlanApiScenarioBatchRunDTO> testPlanApiScenarios, public Map<String, String> initReport(Map<String, String> resourceExecTaskItemMap,
ApiRunModeConfigDTO runModeConfig, String projectId, String userId) { List<TestPlanApiScenarioBatchRunDTO> testPlanApiScenarios,
ApiRunModeConfigDTO runModeConfig, String projectId, String userId) {
List<ApiScenarioReport> apiScenarioReports = new ArrayList<>(testPlanApiScenarios.size()); List<ApiScenarioReport> apiScenarioReports = new ArrayList<>(testPlanApiScenarios.size());
List<ApiScenarioRecord> apiScenarioRecords = new ArrayList<>(testPlanApiScenarios.size()); List<ApiScenarioRecord> apiScenarioRecords = new ArrayList<>(testPlanApiScenarios.size());
List<ApiReportRelateTask> apiReportRelateTasks = new ArrayList<>(testPlanApiScenarios.size());
Map<String, String> resourceReportMap = new HashMap<>(); Map<String, String> resourceReportMap = new HashMap<>();
for (TestPlanApiScenarioBatchRunDTO testPlanApiScenario : testPlanApiScenarios) { for (TestPlanApiScenarioBatchRunDTO testPlanApiScenario : testPlanApiScenarios) {
// 初始化报告 // 初始化报告
@ -343,8 +346,14 @@ public class TestPlanApiScenarioBatchRunService {
ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(testPlanApiScenario.getApiScenarioId(), apiScenarioReport); ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(testPlanApiScenario.getApiScenarioId(), apiScenarioReport);
apiScenarioRecords.add(apiScenarioRecord); apiScenarioRecords.add(apiScenarioRecord);
resourceReportMap.put(testPlanApiScenario.getId(), apiScenarioReport.getId()); resourceReportMap.put(testPlanApiScenario.getId(), apiScenarioReport.getId());
// 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiScenarioReport.getId());
apiReportRelateTask.setTaskResourceId(resourceExecTaskItemMap.get(apiScenarioReport.getId()));
apiReportRelateTasks.add(apiReportRelateTask);
} }
apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords); apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords, apiReportRelateTasks);
return resourceReportMap; return resourceReportMap;
} }
@ -362,7 +371,7 @@ public class TestPlanApiScenarioBatchRunService {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanId); TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanId);
// 独立报告执行到当前任务时初始化报告 // 独立报告执行到当前任务时初始化报告
String reportId = initScenarioReport(runModeConfig, BeanUtils.copyBean(new TestPlanApiScenarioBatchRunDTO(), testPlanApiScenario), testPlan.getId(), queue.getUserId()) String reportId = initScenarioReport(queueDetail.getTaskItemId(), runModeConfig, BeanUtils.copyBean(new TestPlanApiScenarioBatchRunDTO(), testPlanApiScenario), testPlan.getId(), queue.getUserId())
.getApiScenarioReportId(); .getApiScenarioReportId();
TaskRequestDTO taskRequest = getTaskRequestDTO(apiScenario.getProjectId(), queue.getRunModeConfig()); TaskRequestDTO taskRequest = getTaskRequestDTO(apiScenario.getProjectId(), queue.getRunModeConfig());
@ -444,13 +453,18 @@ public class TestPlanApiScenarioBatchRunService {
* @param testPlanApiScenario * @param testPlanApiScenario
* @return * @return
*/ */
public ApiScenarioRecord initScenarioReport(ApiRunModeConfigDTO runModeConfig, TestPlanApiScenarioBatchRunDTO testPlanApiScenario, String projectId, String userId) { public ApiScenarioRecord initScenarioReport(String taskItemId, ApiRunModeConfigDTO runModeConfig, TestPlanApiScenarioBatchRunDTO testPlanApiScenario, String projectId, String userId) {
// 初始化报告 // 初始化报告
ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, testPlanApiScenario, projectId, userId); ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, testPlanApiScenario, projectId, userId);
apiScenarioReport.setId(IDGenerator.nextStr()); apiScenarioReport.setId(IDGenerator.nextStr());
// 创建报告和用例的关联关系 // 创建报告和用例的关联关系
ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(testPlanApiScenario.getApiScenarioId(), apiScenarioReport); ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(testPlanApiScenario.getApiScenarioId(), apiScenarioReport);
apiScenarioReportService.insertApiScenarioReport(List.of(apiScenarioReport), List.of(apiScenarioRecord)); // 创建报告和任务的关联关系
ApiReportRelateTask apiReportRelateTask = new ApiReportRelateTask();
apiReportRelateTask.setReportId(apiScenarioReport.getId());
apiReportRelateTask.setTaskResourceId(taskItemId);
apiScenarioReportService.insertApiScenarioReport(List.of(apiScenarioReport), List.of(apiScenarioRecord), List.of(apiReportRelateTask));
return apiScenarioRecord; return apiScenarioRecord;
} }

View File

@ -379,7 +379,7 @@ public class TestPlanApiScenarioService extends TestPlanResourceService {
scenarioReport.setPoolId(runModeConfig.getPoolId()); scenarioReport.setPoolId(runModeConfig.getPoolId());
scenarioReport.setEnvironmentId(runModeConfig.getEnvironmentId()); scenarioReport.setEnvironmentId(runModeConfig.getEnvironmentId());
scenarioReport.setTestPlanScenarioId(testPlanApiScenario.getId()); scenarioReport.setTestPlanScenarioId(testPlanApiScenario.getId());
apiScenarioRunService.initApiReport(apiScenario, scenarioReport); apiScenarioRunService.initApiReport(taskItem.getId(), apiScenario, scenarioReport);
return apiExecuteService.execute(taskRequest); return apiExecuteService.execute(taskRequest);
} }