diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java b/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java index 43abd81200..4d7f94ce02 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java @@ -66,7 +66,11 @@ public class MessageListener { } // 集合报告不发送通知 } else { - apiReportSendNoticeService.sendNotice(dto); + try { + apiReportSendNoticeService.sendNotice(dto); + } catch (Exception e) { + LogUtils.error(e); + } if (StringUtils.isNotBlank(dto.getParentQueueId()) && BooleanUtils.isTrue(dto.getChildCollectionExecuteOver())) { // 执行下一个测试集 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java index 5682ec8220..1702e06cdc 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java @@ -42,9 +42,9 @@ public class ApiExecuteResourceService { ApiExecuteResourceType apiExecuteResourceType = EnumValidator.validateEnum(ApiExecuteResourceType.class, request.getResourceType()); switch (apiExecuteResourceType) { - case API_SCENARIO, TEST_PLAN_API_SCENARIO -> + case API_SCENARIO, TEST_PLAN_API_SCENARIO, PLAN_RUN_API_SCENARIO -> apiScenarioReportService.updateReportStatus(reportId, ExecStatus.RUNNING.name()); - case API_CASE, TEST_PLAN_API_CASE -> + case API_CASE, TEST_PLAN_API_CASE, PLAN_RUN_API_CASE -> apiReportService.updateReportStatus(reportId, ExecStatus.RUNNING.name()); default -> throw new MSException("不支持的资源类型: " + request.getResourceType()); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java index 81ada24126..fde1fd8561 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java @@ -343,7 +343,7 @@ public class ApiTestCaseBatchRunService { public ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase, String userId) { ApiReport apiReport = getApiReport(runModeConfig, userId); - apiReport.setEnvironmentId(apiTestCaseService.getEnvId(runModeConfig, apiTestCase)); + apiReport.setEnvironmentId(apiTestCaseService.getEnvId(runModeConfig, apiTestCase.getEnvironmentId())); apiReport.setName(apiTestCase.getName() + "_" + DateUtils.getTimeString(System.currentTimeMillis())); apiReport.setProjectId(apiTestCase.getProjectId()); apiReport.setTriggerMode(TaskTriggerMode.BATCH.name()); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java index 0b65462231..3a621c0baf 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java @@ -786,7 +786,7 @@ public class ApiTestCaseService extends MoveNodeService { apiExecuteService.setTestElementParam(msTestElement, apiTestCase.getProjectId(), request.getTaskItem()); // 设置环境信息 - apiParamConfig.setEnvConfig(environmentService.get(getEnvId(request.getRunModeConfig(), apiTestCase))); + apiParamConfig.setEnvConfig(environmentService.get(getEnvId(request.getRunModeConfig(), apiTestCase.getEnvironmentId()))); GetRunScriptResult runScriptResult = new GetRunScriptResult(); // 记录请求数量 runScriptResult.setRequestCount(1L); @@ -804,12 +804,13 @@ public class ApiTestCaseService extends MoveNodeService { * 优先使用运行配置的环境 * 没有则使用用例自身的环境 * - * @param runModeConfig - * @param apiTestCase * @return */ - public String getEnvId(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase) { - return StringUtils.isBlank(runModeConfig.getEnvironmentId()) ? apiTestCase.getEnvironmentId() : runModeConfig.getEnvironmentId(); + public String getEnvId(ApiRunModeConfigDTO runModeConfig, String caseEnvId) { + if (StringUtils.isBlank(runModeConfig.getEnvironmentId()) || StringUtils.equals(runModeConfig.getEnvironmentId(), CommonConstants.DEFAULT_NULL_VALUE)) { + return caseEnvId; + } + return runModeConfig.getEnvironmentId(); } /** @@ -830,7 +831,7 @@ public class ApiTestCaseService extends MoveNodeService { apiReportService.insertApiReport(List.of(apiReport), List.of(apiTestCaseRecord)); //初始化步骤 - apiReportService.insertApiReportStep(List.of(getApiReportStep(apiTestCase, reportId, 1L))); + apiReportService.insertApiReportStep(List.of(getApiReportStep(apiTestCase.getId(), apiTestCase.getName(), reportId, 1L))); return apiTestCaseRecord; } @@ -846,12 +847,12 @@ public class ApiTestCaseService extends MoveNodeService { return apiReport; } - public ApiReportStep getApiReportStep(ApiTestCase apiTestCase, String reportId, long sort) { + public ApiReportStep getApiReportStep(String stepId, String stepName, String reportId, long sort) { ApiReportStep apiReportStep = new ApiReportStep(); apiReportStep.setReportId(reportId); - apiReportStep.setStepId(apiTestCase.getId()); + apiReportStep.setStepId(stepId); apiReportStep.setSort(sort); - apiReportStep.setName(apiTestCase.getName()); + apiReportStep.setName(stepName); apiReportStep.setStepType(ApiExecuteResourceType.API_CASE.name()); return apiReportStep; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java index d613e4e1cd..90d0384ef7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java @@ -327,7 +327,7 @@ public class ApiScenarioBatchRunService { private ApiScenarioReport getScenarioReport(ApiRunModeConfigDTO runModeConfig, ApiScenario apiScenario, String userId) { ApiScenarioReport apiScenarioReport = getScenarioReport(runModeConfig, userId); - apiScenarioReport.setEnvironmentId(apiScenarioRunService.getEnvId(runModeConfig, apiScenario)); + apiScenarioReport.setEnvironmentId(apiScenarioRunService.getEnvId(runModeConfig, apiScenario.getEnvironmentId())); apiScenarioReport.setName(apiScenario.getName() + "_" + DateUtils.getTimeString(System.currentTimeMillis())); apiScenarioReport.setProjectId(apiScenario.getProjectId()); apiScenarioReport.setTriggerMode(TaskTriggerMode.BATCH.name()); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java index 9b0b06a845..d0ce1b72a3 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java @@ -30,10 +30,7 @@ import io.metersphere.project.dto.environment.http.HttpConfigModuleMatchRule; import io.metersphere.project.dto.environment.http.SelectModule; import io.metersphere.project.service.EnvironmentGroupService; import io.metersphere.project.service.EnvironmentService; -import io.metersphere.sdk.constants.ApiBatchRunMode; -import io.metersphere.sdk.constants.ApiExecuteRunMode; -import io.metersphere.sdk.constants.ExecStatus; -import io.metersphere.sdk.constants.TaskTriggerMode; +import io.metersphere.sdk.constants.*; import io.metersphere.sdk.dto.api.task.*; import io.metersphere.sdk.util.DateUtils; import io.metersphere.sdk.util.JSON; @@ -265,6 +262,11 @@ public class ApiScenarioRunService { public GetRunScriptResult getRunScript(GetRunScriptRequest request, String id) { ApiScenarioDetail apiScenarioDetail = getForRun(id); + return getRunScript(request, apiScenarioDetail); + } + + public GetRunScriptResult getRunScript(GetRunScriptRequest request, ApiScenarioDetail apiScenarioDetail) { + String id = apiScenarioDetail.getId(); TaskItem taskItem = request.getTaskItem(); ApiRunModeConfigDTO runModeConfig = request.getRunModeConfig(); String reportId = taskItem.getReportId(); @@ -278,8 +280,8 @@ public class ApiScenarioRunService { return null; } - String envId = getEnvId(runModeConfig, apiScenarioDetail); - boolean envGroup = getEnvGroup(runModeConfig, apiScenarioDetail); + String envId = getEnvId(runModeConfig, apiScenarioDetail.getEnvironmentId()); + boolean envGroup = getEnvGroup(runModeConfig, apiScenarioDetail.getGrouped()); // 解析生成待执行的场景树 MsScenario msScenario = getMsScenario(apiScenarioDetail); @@ -329,12 +331,25 @@ public class ApiScenarioRunService { * @param apiScenario * @return */ - public String getEnvId(ApiRunModeConfigDTO runModeConfig, ApiScenario apiScenario) { - return StringUtils.isBlank(runModeConfig.getEnvironmentId()) ? apiScenario.getEnvironmentId() : runModeConfig.getEnvironmentId(); + /** + * 获取执行的环境ID + * 优先使用运行配置的环境 + * 没有则使用用例自身的环境 + * + * @return + */ + public String getEnvId(ApiRunModeConfigDTO runModeConfig, String caseEnvId) { + if (StringUtils.isBlank(runModeConfig.getEnvironmentId()) || StringUtils.equals(runModeConfig.getEnvironmentId(), CommonConstants.DEFAULT_NULL_VALUE)) { + return caseEnvId; + } + return runModeConfig.getEnvironmentId(); } - public boolean getEnvGroup(ApiRunModeConfigDTO runModeConfig, ApiScenario apiScenario) { - return StringUtils.isBlank(runModeConfig.getEnvironmentId()) ? apiScenario.getGrouped() : runModeConfig.getGrouped(); + public boolean getEnvGroup(ApiRunModeConfigDTO runModeConfig, boolean group) { + if (StringUtils.isBlank(runModeConfig.getEnvironmentId()) || StringUtils.equals(runModeConfig.getEnvironmentId(), CommonConstants.DEFAULT_NULL_VALUE)) { + return group; + } + return runModeConfig.getGrouped(); } private void updateReportWaitTime(String reportId, ApiScenarioParseParam parseParam) { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunApiCaseExecuteCallbackService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunApiCaseExecuteCallbackService.java index 6ae3a353b0..ca145eeab7 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunApiCaseExecuteCallbackService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunApiCaseExecuteCallbackService.java @@ -22,7 +22,7 @@ public class PlanRunApiCaseExecuteCallbackService implements ApiExecuteCallbackS private TestPlanExecuteService testPlanExecuteService; @Resource - private TestPlanApiCasePlanRunService testPlanApiCasePlanRunService; + private PlanRunTestPlanApiCaseService planRunTestPlanApiCaseService; public PlanRunApiCaseExecuteCallbackService() { ApiExecuteCallbackServiceInvoker.register(ApiExecuteResourceType.PLAN_RUN_API_CASE, this); @@ -33,7 +33,7 @@ public class PlanRunApiCaseExecuteCallbackService implements ApiExecuteCallbackS */ @Override public GetRunScriptResult getRunScript(GetRunScriptRequest request) { - return testPlanApiCasePlanRunService.getRunScript(request); + return planRunTestPlanApiCaseService.getRunScript(request); } /** @@ -43,7 +43,7 @@ public class PlanRunApiCaseExecuteCallbackService implements ApiExecuteCallbackS */ @Override public void executeNextTask(ExecutionQueue queue, ExecutionQueueDetail queueDetail) { - testPlanApiCasePlanRunService.executeNextTask(queue, queueDetail); + planRunTestPlanApiCaseService.executeNextTask(queue, queueDetail); } /** diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunApiScenarioExecuteCallbackService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunApiScenarioExecuteCallbackService.java new file mode 100644 index 0000000000..59789321fe --- /dev/null +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunApiScenarioExecuteCallbackService.java @@ -0,0 +1,58 @@ +package io.metersphere.plan.service; + +import io.metersphere.api.invoker.ApiExecuteCallbackServiceInvoker; +import io.metersphere.api.service.ApiExecuteCallbackService; +import io.metersphere.sdk.constants.ApiExecuteResourceType; +import io.metersphere.sdk.dto.api.task.GetRunScriptRequest; +import io.metersphere.sdk.dto.api.task.GetRunScriptResult; +import io.metersphere.sdk.dto.queue.ExecutionQueue; +import io.metersphere.sdk.dto.queue.ExecutionQueueDetail; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * @Author: jianxing + * @CreateTime: 2024-02-06 20:47 + */ +@Service +@Transactional(rollbackFor = Exception.class) +public class PlanRunApiScenarioExecuteCallbackService implements ApiExecuteCallbackService { + @Resource + private TestPlanExecuteService testPlanExecuteService; + + @Resource + private PlanRunTestPlanApiScenarioService planRunTestPlanApiScenarioService; + + public PlanRunApiScenarioExecuteCallbackService() { + ApiExecuteCallbackServiceInvoker.register(ApiExecuteResourceType.PLAN_RUN_API_SCENARIO, this); + } + + /** + * 解析并返回执行脚本 + */ + @Override + public GetRunScriptResult getRunScript(GetRunScriptRequest request) { + return planRunTestPlanApiScenarioService.getRunScript(request); + } + + /** + * 串行时,执行下一个任务 + * @param queue + * @param queueDetail + */ + @Override + public void executeNextTask(ExecutionQueue queue, ExecutionQueueDetail queueDetail) { + planRunTestPlanApiScenarioService.executeNextTask(queue, queueDetail); + } + + /** + * 批量串行的测试集执行时 + * 测试集下用例执行完成时回调 + * @param parentQueueId + */ + @Override + public void executeNextCollection(String parentQueueId) { + testPlanExecuteService.collectionExecuteQueueFinish(parentQueueId); + } +} diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCasePlanRunService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiCaseService.java similarity index 98% rename from backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCasePlanRunService.java rename to backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiCaseService.java index b9a717b4bf..d2abe7044b 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCasePlanRunService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiCaseService.java @@ -38,7 +38,7 @@ import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) -public class TestPlanApiCasePlanRunService { +public class PlanRunTestPlanApiCaseService { @Resource private ApiTestCaseMapper apiTestCaseMapper; @Resource @@ -153,6 +153,7 @@ public class TestPlanApiCasePlanRunService { TaskBatchRequestDTO taskRequest = apiTestCaseBatchRunService.getTaskBatchRequestDTO(testPlan.getProjectId(), runModeConfig); taskRequest.setTaskItems(taskItems); taskRequest.getTaskInfo().setParentQueueId(parentQueueId); + taskRequest.getTaskInfo().setUserId(userId); taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_CASE.name()); apiExecuteService.batchExecute(taskRequest); @@ -191,6 +192,7 @@ public class TestPlanApiCasePlanRunService { TaskRequestDTO taskRequest = testPlanApiCaseBatchRunService.getTaskRequestDTO(reportId, testPlanReportApiCase.getId(), apiTestCase, runModeConfig); taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_CASE.name()); taskRequest.getTaskInfo().setQueueId(queue.getQueueId()); + taskRequest.getTaskInfo().setUserId(queue.getUserId()); taskRequest.getTaskInfo().setParentQueueId(queue.getParentQueueId()); taskRequest.getTaskItem().setRequestCount(1L); diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioPlanRunService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiScenarioService.java similarity index 88% rename from backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioPlanRunService.java rename to backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiScenarioService.java index 72b461fbb5..ee26f57f44 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioPlanRunService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiScenarioService.java @@ -3,6 +3,7 @@ package io.metersphere.plan.service; import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenarioRecord; import io.metersphere.api.domain.ApiScenarioReport; +import io.metersphere.api.dto.scenario.ApiScenarioDetail; import io.metersphere.api.mapper.ApiScenarioMapper; import io.metersphere.api.mapper.ExtApiScenarioMapper; import io.metersphere.api.service.ApiBatchRunBaseService; @@ -10,18 +11,13 @@ import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.queue.ApiExecutionQueueService; import io.metersphere.api.service.queue.ApiExecutionSetService; import io.metersphere.api.service.scenario.ApiScenarioReportService; -import io.metersphere.plan.domain.TestPlan; -import io.metersphere.plan.domain.TestPlanCollection; -import io.metersphere.plan.domain.TestPlanReportApiScenario; -import io.metersphere.plan.domain.TestPlanReportApiScenarioExample; +import io.metersphere.api.service.scenario.ApiScenarioRunService; +import io.metersphere.plan.domain.*; import io.metersphere.plan.mapper.ExtTestPlanApiScenarioMapper; import io.metersphere.plan.mapper.TestPlanMapper; import io.metersphere.plan.mapper.TestPlanReportApiScenarioMapper; import io.metersphere.sdk.constants.ApiExecuteResourceType; -import io.metersphere.sdk.dto.api.task.ApiRunModeConfigDTO; -import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO; -import io.metersphere.sdk.dto.api.task.TaskItem; -import io.metersphere.sdk.dto.api.task.TaskRequestDTO; +import io.metersphere.sdk.dto.api.task.*; import io.metersphere.sdk.dto.queue.ExecutionQueue; import io.metersphere.sdk.dto.queue.ExecutionQueueDetail; import io.metersphere.sdk.dto.queue.TestPlanExecutionQueue; @@ -34,16 +30,13 @@ import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) -public class TestPlanApiScenarioPlanRunService { +public class PlanRunTestPlanApiScenarioService { @Resource private ApiScenarioMapper apiScenarioMapper; @Resource @@ -57,6 +50,8 @@ public class TestPlanApiScenarioPlanRunService { @Resource private TestPlanReportApiScenarioMapper testPlanReportApiScenarioMapper; @Resource + private ApiScenarioRunService apiScenarioRunService; + @Resource private TestPlanMapper testPlanMapper; @Resource private TestPlanApiBatchRunBaseService testPlanApiBatchRunBaseService; @@ -122,6 +117,7 @@ public class TestPlanApiScenarioPlanRunService { TaskBatchRequestDTO taskRequest = testPlanApiScenarioBatchRunService.getTaskBatchRequestDTO(testPlan.getProjectId(), runModeConfig); taskRequest.setTaskItems(taskItems); taskRequest.getTaskInfo().setParentQueueId(parentQueueId); + taskRequest.getTaskInfo().setUserId(userId); taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_SCENARIO.name()); // 如果有父队列,则初始化执行集合,以便判断是否执行完毕 @@ -151,6 +147,7 @@ public class TestPlanApiScenarioPlanRunService { Map apiScenarioMap, String userId) { List apiScenarioReports = new ArrayList<>(testPlanReportApiScenarios.size()); List apiScenarioRecords = new ArrayList<>(testPlanReportApiScenarios.size()); + Map resourceReportMap = new HashMap<>(); for (TestPlanReportApiScenario testPlanReportApiScenario : testPlanReportApiScenarios) { ApiScenario apiScenario = apiScenarioMap.get(testPlanReportApiScenario.getApiScenarioId()); // 初始化报告 @@ -168,9 +165,10 @@ public class TestPlanApiScenarioPlanRunService { scenarioRecord.setApiScenarioId(apiScenario.getId()); scenarioRecord.setApiScenarioReportId(apiScenarioReport.getId()); apiScenarioRecords.add(scenarioRecord); + resourceReportMap.put(testPlanReportApiScenario.getId(), apiScenarioReport.getId()); } apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords); - return apiScenarioRecords.stream().collect(Collectors.toMap(ApiScenarioRecord::getApiScenarioId, ApiScenarioRecord::getApiScenarioReportId)); + return resourceReportMap; } private List getTestPlanReportApiScenarios(String testPlanReportId, TestPlanCollection collection) { @@ -206,9 +204,19 @@ public class TestPlanApiScenarioPlanRunService { TaskItem taskItem = apiExecuteService.getTaskItem(reportId, queueDetail.getResourceId()); taskRequest.setTaskItem(taskItem); taskRequest.getTaskInfo().setQueueId(queue.getQueueId()); + taskRequest.getTaskInfo().setUserId(queue.getUserId()); taskRequest.getTaskInfo().setParentQueueId(queue.getParentQueueId()); taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_SCENARIO.name()); apiExecuteService.execute(taskRequest); } + + public GetRunScriptResult getRunScript(GetRunScriptRequest request) { + TaskItem taskItem = request.getTaskItem(); + TestPlanReportApiScenario testPlanReportApiScenario = testPlanReportApiScenarioMapper.selectByPrimaryKey(taskItem.getResourceId()); + ApiScenarioDetail apiScenarioDetail = apiScenarioRunService.getForRun(testPlanReportApiScenario.getApiScenarioId()); + apiScenarioDetail.setEnvironmentId(testPlanReportApiScenario.getEnvironmentId()); + apiScenarioDetail.setGrouped(testPlanReportApiScenario.getGrouped()); + return apiScenarioRunService.getRunScript(request, testPlanReportApiScenario.getApiScenarioId()); + } } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiBatchRunBaseService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiBatchRunBaseService.java index 55286f64af..9bed2ee7d7 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiBatchRunBaseService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiBatchRunBaseService.java @@ -3,6 +3,7 @@ package io.metersphere.plan.service; import io.metersphere.plan.domain.TestPlanCollection; import io.metersphere.plan.domain.TestPlanCollectionExample; import io.metersphere.plan.mapper.TestPlanCollectionMapper; +import io.metersphere.sdk.constants.CommonConstants; import io.metersphere.sdk.dto.api.task.ApiRunModeConfigDTO; import io.metersphere.sdk.dto.api.task.ApiRunRetryConfig; import io.metersphere.sdk.util.BeanUtils; @@ -25,23 +26,27 @@ public class TestPlanApiBatchRunBaseService { } public ApiRunModeConfigDTO getApiRunModeConfig(TestPlanCollection collection) { - TestPlanCollection rootCollection = null; if (collection == null) { ApiRunModeConfigDTO runModeConfig = new ApiRunModeConfigDTO(); runModeConfig.setPoolId(StringUtils.EMPTY); return runModeConfig; } + TestPlanCollection rootCollection = getExtendedRootCollection(collection); + ApiRunModeConfigDTO runModeConfig = getApiRunModeConfig(rootCollection, collection); + return runModeConfig; + } + + public TestPlanCollection getExtendedRootCollection(TestPlanCollection collection) { if (BooleanUtils.isTrue(collection.getExtended()) - && StringUtils.equalsIgnoreCase(collection.getParentId(), "NONE")) { + && !StringUtils.equals(collection.getParentId(), CommonConstants.DEFAULT_NULL_VALUE)) { TestPlanCollectionExample example = new TestPlanCollectionExample(); - example.createCriteria().andParentIdEqualTo(collection.getParentId()); - rootCollection = testPlanCollectionMapper.selectByExample(example) + example.createCriteria().andIdEqualTo(collection.getParentId()); + return testPlanCollectionMapper.selectByExample(example) .stream() .findFirst() .orElse(null); } - ApiRunModeConfigDTO runModeConfig = getApiRunModeConfig(rootCollection, collection); - return runModeConfig; + return null; } public ApiRunModeConfigDTO getApiRunModeConfig(TestPlanCollection rootCollection, TestPlanCollection collection) { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java index 9c2bbd3713..bdb5106f45 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java @@ -222,7 +222,7 @@ public class TestPlanApiCaseBatchRunService { .collect(Collectors.toMap(ApiTestCase::getId, Function.identity())); // 初始化报告,返回用例和报告的 map - Map caseReportMap = initParallelReport(runModeConfig, testPlanApiCases, apiCaseMap, userId); + Map caseReportMap = initApiReport(runModeConfig, testPlanApiCases, apiCaseMap, userId); List taskItems = new ArrayList<>(ids.size()); @@ -252,13 +252,6 @@ public class TestPlanApiCaseBatchRunService { apiExecuteService.batchExecute(taskRequest); } - private Map initParallelReport(ApiRunModeConfigDTO runModeConfig, List testPlanApiCases, Map caseMap, String userId) { - // 初始化非集成报告 - List apiTestCaseRecords = initApiReport(runModeConfig, testPlanApiCases, caseMap, userId); - return apiTestCaseRecords.stream() - .collect(Collectors.toMap(ApiTestCaseRecord::getApiTestCaseId, ApiTestCaseRecord::getApiReportId)); - } - public ApiReportStep getApiReportStep(String resourceId, ApiTestCase apiTestCase, String reportId, long sort) { ApiReportStep apiReportStep = new ApiReportStep(); apiReportStep.setReportId(reportId); @@ -289,7 +282,7 @@ public class TestPlanApiCaseBatchRunService { ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(testPlanApiCase.getApiCaseId()); // 独立报告,执行到当前任务时初始化报告 - String reportId = initApiReport(runModeConfig, List.of(testPlanApiCase), Map.of(apiTestCase.getId(), apiTestCase), queue.getUserId()).get(0).getApiReportId(); + String reportId = initApiReport(runModeConfig, List.of(testPlanApiCase), Map.of(apiTestCase.getId(), apiTestCase), queue.getUserId()).get(testPlanApiCase.getId()); TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, testPlanApiCase.getId(), apiTestCase, runModeConfig); taskRequest.getTaskInfo().setQueueId(queue.getQueueId()); taskRequest.getTaskInfo().setParentQueueId(queue.getParentQueueId()); @@ -326,11 +319,12 @@ public class TestPlanApiCaseBatchRunService { * @param runModeConfig * @return */ - public List initApiReport(ApiRunModeConfigDTO runModeConfig, List testPlanApiCases, + public Map initApiReport(ApiRunModeConfigDTO runModeConfig, List testPlanApiCases, Map caseMap, String userId) { List apiReports = new ArrayList<>(); List apiTestCaseRecords = new ArrayList<>(); List apiReportSteps = new ArrayList<>(); + Map resourceReportMap = new HashMap<>(); for (TestPlanApiCase testPlanApiCase : testPlanApiCases) { ApiTestCase apiTestCase = caseMap.get(testPlanApiCase.getApiCaseId()); @@ -341,10 +335,11 @@ public class TestPlanApiCaseBatchRunService { ApiTestCaseRecord apiTestCaseRecord = apiTestCaseService.getApiTestCaseRecord(apiTestCase, apiReport); apiTestCaseRecords.add(apiTestCaseRecord); apiReportSteps.add(getApiReportStep(testPlanApiCase.getId(), apiTestCase, apiReport.getId(), 1)); + resourceReportMap.put(testPlanApiCase.getId(), apiReport.getId()); } apiReportService.insertApiReport(apiReports, apiTestCaseRecords); apiReportService.insertApiReportStep(apiReportSteps); - return apiTestCaseRecords; + return resourceReportMap; } private ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, TestPlanApiCase testPlanApiCase, ApiTestCase apiTestCase, String userId) { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java index bb5fad6e67..13f678fc5b 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java @@ -665,6 +665,7 @@ public class TestPlanApiCaseService extends TestPlanResourceService { TaskItem taskItem = request.getTaskItem(); TestPlanApiCase testPlanApiCase = testPlanApiCaseMapper.selectByPrimaryKey(taskItem.getResourceId()); ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(testPlanApiCase.getApiCaseId()); + apiTestCase.setEnvironmentId(testPlanApiCase.getEnvironmentId()); return apiTestCaseService.getRunScript(request, apiTestCase); } @@ -684,12 +685,12 @@ public class TestPlanApiCaseService extends TestPlanResourceService { apiReportService.insertApiReport(List.of(apiReport), List.of(apiTestCaseRecord)); //初始化步骤 - apiReportService.insertApiReportStep(List.of(getApiReportStep(apiTestCase, reportId))); + apiReportService.insertApiReportStep(List.of(getApiReportStep(testPlanApiCase, apiTestCase, reportId))); return apiTestCaseRecord; } - public ApiReportStep getApiReportStep(ApiTestCase apiTestCase, String reportId) { - ApiReportStep apiReportStep = apiTestCaseService.getApiReportStep(apiTestCase, reportId, 1L); + public ApiReportStep getApiReportStep(TestPlanApiCase testPlanApiCase, ApiTestCase apiTestCase, String reportId) { + ApiReportStep apiReportStep = apiTestCaseService.getApiReportStep(testPlanApiCase.getId(), apiTestCase.getName(), reportId, 1L); apiReportStep.setStepType(ApiExecuteResourceType.TEST_PLAN_API_CASE.name()); return apiReportStep; } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java index 3e7d8ef528..6131617b52 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java @@ -210,6 +210,7 @@ public class TestPlanApiScenarioBatchRunService { TaskBatchRequestDTO taskRequest = getTaskBatchRequestDTO(projectId, runModeConfig); taskRequest.setTaskItems(taskItems); + taskRequest.getTaskInfo().setUserId(userId); taskRequest.getTaskInfo().setParentQueueId(parentQueueId); apiExecuteService.batchExecute(taskRequest); @@ -250,6 +251,7 @@ public class TestPlanApiScenarioBatchRunService { Map apiScenarioMap, String userId) { List apiScenarioReports = new ArrayList<>(testPlanApiScenarios.size()); List apiScenarioRecords = new ArrayList<>(testPlanApiScenarios.size()); + Map resourceReportMap = new HashMap<>(); for (TestPlanApiScenario testPlanApiScenario : testPlanApiScenarios) { ApiScenario apiScenario = apiScenarioMap.get(testPlanApiScenario.getApiScenarioId()); // 初始化报告 @@ -259,9 +261,10 @@ public class TestPlanApiScenarioBatchRunService { // 创建报告和用例的关联关系 ApiScenarioRecord apiScenarioRecord = apiScenarioRunService.getApiScenarioRecord(apiScenario, apiScenarioReport); apiScenarioRecords.add(apiScenarioRecord); + resourceReportMap.put(testPlanApiScenario.getId(), apiScenarioReport.getId()); } apiScenarioReportService.insertApiScenarioReport(apiScenarioReports, apiScenarioRecords); - return apiScenarioRecords.stream().collect(Collectors.toMap(ApiScenarioRecord::getApiScenarioId, ApiScenarioRecord::getApiScenarioReportId)); + return resourceReportMap; } /** @@ -282,6 +285,7 @@ public class TestPlanApiScenarioBatchRunService { TaskItem taskItem = apiExecuteService.getTaskItem(reportId, queueDetail.getResourceId()); taskRequest.setTaskItem(taskItem); taskRequest.getTaskInfo().setQueueId(queue.getQueueId()); + taskRequest.getTaskInfo().setUserId(queue.getUserId()); taskRequest.getTaskInfo().setParentQueueId(queue.getParentQueueId()); apiExecuteService.execute(taskRequest); diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java index 492fea4f7c..c6eb53eb5b 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java @@ -5,6 +5,7 @@ import io.metersphere.api.domain.ApiScenarioExample; import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiScenarioReportExample; import io.metersphere.api.dto.scenario.ApiScenarioDTO; +import io.metersphere.api.dto.scenario.ApiScenarioDetail; import io.metersphere.api.mapper.ApiScenarioMapper; import io.metersphere.api.mapper.ApiScenarioReportMapper; import io.metersphere.api.service.ApiBatchRunBaseService; @@ -335,6 +336,9 @@ public class TestPlanApiScenarioService extends TestPlanResourceService { public GetRunScriptResult getRunScript(GetRunScriptRequest request) { TaskItem taskItem = request.getTaskItem(); TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(taskItem.getResourceId()); + ApiScenarioDetail apiScenarioDetail = apiScenarioRunService.getForRun(testPlanApiScenario.getApiScenarioId()); + apiScenarioDetail.setEnvironmentId(testPlanApiScenario.getEnvironmentId()); + apiScenarioDetail.setGrouped(testPlanApiScenario.getGrouped()); return apiScenarioRunService.getRunScript(request, testPlanApiScenario.getApiScenarioId()); } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java index 460776468e..04b1ac679f 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java @@ -49,9 +49,11 @@ public class TestPlanExecuteService { @Resource private TestPlanCollectionMapper testPlanCollectionMapper; @Resource - private TestPlanApiCasePlanRunService testPlanApiCasePlanRunService; + private PlanRunTestPlanApiCaseService planRunTestPlanApiCaseService; @Resource - private TestPlanApiScenarioPlanRunService testPlanApiScenarioPlanRunService; + private PlanRunTestPlanApiScenarioService planRunTestPlanApiScenarioService; + @Resource + private TestPlanApiBatchRunBaseService testPlanApiBatchRunBaseService; @Resource private RedisTemplate redisTemplate; @@ -388,17 +390,19 @@ public class TestPlanExecuteService { try { boolean isFinish = false; TestPlanCollection collection = JSON.parseObject(testPlanExecutionQueue.getTestPlanCollectionJson(), TestPlanCollection.class); + TestPlanCollection extendedRootCollection = testPlanApiBatchRunBaseService.getExtendedRootCollection(collection); + String executeMethod = extendedRootCollection == null ? collection.getExecuteMethod() : extendedRootCollection.getExecuteMethod(); if (StringUtils.equalsIgnoreCase(collection.getType(), CaseType.API_CASE.getKey())) { - if (StringUtils.equals(collection.getExecuteMethod(), ApiBatchRunMode.PARALLEL.name())) { - isFinish = testPlanApiCasePlanRunService.parallelExecute(testPlanExecutionQueue); + if (StringUtils.equals(executeMethod, ApiBatchRunMode.PARALLEL.name())) { + isFinish = planRunTestPlanApiCaseService.parallelExecute(testPlanExecutionQueue); } else { - isFinish = testPlanApiCasePlanRunService.serialExecute(testPlanExecutionQueue); + isFinish = planRunTestPlanApiCaseService.serialExecute(testPlanExecutionQueue); } } else if (StringUtils.equalsIgnoreCase(collection.getType(), CaseType.SCENARIO_CASE.getKey())) { - if (StringUtils.equals(collection.getExecuteMethod(), ApiBatchRunMode.PARALLEL.name())) { - isFinish = testPlanApiScenarioPlanRunService.parallelExecute(testPlanExecutionQueue); + if (StringUtils.equals(executeMethod, ApiBatchRunMode.PARALLEL.name())) { + isFinish = planRunTestPlanApiScenarioService.parallelExecute(testPlanExecutionQueue); } else { - isFinish = testPlanApiScenarioPlanRunService.serialExecute(testPlanExecutionQueue); + isFinish = planRunTestPlanApiScenarioService.serialExecute(testPlanExecutionQueue); } } if (isFinish) { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportService.java index 5484581eb6..a77890668e 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportService.java @@ -39,6 +39,8 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; @@ -220,10 +222,12 @@ public class TestPlanReportService { /** * 执行生成报告 + * 新开事务,避免异步执行查不到数据 * * @param request 请求参数 * @param currentUser 当前用户 */ + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) public Map genReportByExecution(String prepareReportId,TestPlanReportGenRequest request, String currentUser) { return genReport(prepareReportId,request, false, currentUser, "/test-plan/report/gen"); }