From 70f1455000a9da189bdc30bb0c11e79754d6e2e1 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Wed, 13 Mar 2024 21:26:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=94=A8=E4=BE=8B=E6=89=A7=E8=A1=8C=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/dto/api/task/ApiRunModeConfigDTO.java | 4 +- .../sdk/dto/api/task/TaskRequestDTO.java | 4 +- .../definition/ApiTestCaseController.java | 10 ++- .../api/service/ApiExecuteService.java | 14 +++-- .../ApiTestCaseBatchRunService.java | 30 +++------ .../definition/ApiTestCaseService.java | 63 +++++++++++++++++-- .../ApiTestCaseControllerTests.java | 12 +++- 7 files changed, 100 insertions(+), 37 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java index 34aba9b453..795ba4023e 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java @@ -17,12 +17,12 @@ public class ApiRunModeConfigDTO implements Serializable { * 是否并行执行 * {@link io.metersphere.sdk.constants.ApiBatchRunMode} */ - private String runMode; + private String runMode = ApiBatchRunMode.PARALLEL.name(); /** * 是否是集成报告 */ - private Boolean integratedReport; + private Boolean integratedReport = false; /** * 集合报告配置 diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java index d8f368ddcb..0320275351 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java @@ -88,7 +88,7 @@ public class TaskRequestDTO implements Serializable { private String projectId; /** - * {@link io.metersphere.sdk.constants.ApiBatchRunMode} + * {@link io.metersphere.sdk.constants.ApiExecuteRunMode} */ @NotBlank private String runMode; @@ -97,7 +97,7 @@ public class TaskRequestDTO implements Serializable { * 运行配置 */ @Valid - private ApiRunModeConfigDTO runModeConfig; + private ApiRunModeConfigDTO runModeConfig = new ApiRunModeConfigDTO(); /** * TODO 要执行的请求总量,用于计算执行各种指标 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java index 941e49f044..aa17ce13d2 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java @@ -20,6 +20,7 @@ import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.notice.annotation.SendNotice; import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.security.CheckOwner; +import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.SessionUtils; @@ -254,7 +255,14 @@ public class ApiTestCaseController { @Operation(summary = "用例执行,获取获取执行结果") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE) public TaskRequestDTO run(@PathVariable String id, @PathVariable String reportId) { - return apiTestCaseService.run(id, reportId); + return apiTestCaseService.run(id, reportId, SessionUtils.getUserId()); + } + + @GetMapping("/run/{id}") + @Operation(summary = "用例执行") + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE) + public TaskRequestDTO run(@PathVariable String id) { + return apiTestCaseService.run(id, IDGenerator.nextStr(), SessionUtils.getUserId()); } @PostMapping("/debug") diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index ee93e631e2..c37565ae51 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -23,6 +23,7 @@ import io.metersphere.sdk.constants.ApiExecuteRunMode; import io.metersphere.sdk.constants.ProjectApplicationType; import io.metersphere.sdk.constants.StorageType; import io.metersphere.sdk.dto.api.task.ApiExecuteFileInfo; +import io.metersphere.sdk.dto.api.task.ApiRunModeConfigDTO; import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.file.FileCenter; @@ -183,7 +184,7 @@ public class ApiExecuteService { */ private TaskRequestDTO doExecute(TaskRequestDTO taskRequest) throws Exception { // 获取资源池 - TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(taskRequest.getProjectId()); + TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(taskRequest.getRunModeConfig(), taskRequest.getProjectId()); TestResourceNodeDTO testResourceNodeDTO = getProjectExecuteNode(testResourcePoolDTO); if (StringUtils.isNotBlank(testResourcePoolDTO.getServerUrl())) { // 如果资源池配置了当前站点,则使用资源池的 @@ -217,9 +218,12 @@ public class ApiExecuteService { } } - private TestResourcePoolReturnDTO getGetResourcePoolNodeDTO(String projectId) { - String resourcePoolId = getProjectApiResourcePoolId(projectId); - return getAvailableResourcePoolDTO(projectId, resourcePoolId); + private TestResourcePoolReturnDTO getGetResourcePoolNodeDTO(ApiRunModeConfigDTO runModeConfig, String projectId) { + String poolId = runModeConfig.getPoolId(); + if (StringUtils.isBlank(poolId)) { + poolId = getProjectApiResourcePoolId(projectId); + } + return getAvailableResourcePoolDTO(projectId, poolId); } /** @@ -436,7 +440,7 @@ public class ApiExecuteService { return testResourcePoolService.getTestResourcePoolDetail(resourcePoolId); } - private String getProjectApiResourcePoolId(String projectId) { + public String getProjectApiResourcePoolId(String projectId) { // 查询接口默认资源池 ProjectApplication resourcePoolConfig = projectApplicationService.getByType(projectId, ProjectApplicationType.API.API_RESOURCE_POOL_ID.name()); // 没有配置接口默认资源池 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 4d65e12b2b..c87ccba510 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 @@ -308,6 +308,7 @@ public class ApiTestCaseBatchRunService { return taskRequest; } + /** * 预生成用例的执行报告 * @@ -315,48 +316,37 @@ public class ApiTestCaseBatchRunService { * @param apiTestCases * @return */ - private List initApiReport(ApiRunModeConfigDTO runModeConfig, List apiTestCases, String userId) { + public List initApiReport(ApiRunModeConfigDTO runModeConfig, List apiTestCases, String userId) { List apiReports = new ArrayList<>(); List apiTestCaseRecords = new ArrayList<>(); for (ApiTestCase apiTestCase : apiTestCases) { + // 初始化报告 ApiReport apiReport = getApiReport(runModeConfig, apiTestCase, userId); - ApiTestCaseRecord apiTestCaseRecord = getApiTestCaseRecord(apiTestCase, apiReport); apiReports.add(apiReport); + // 创建报告和用例的关联关系 + ApiTestCaseRecord apiTestCaseRecord = apiTestCaseService.getApiTestCaseRecord(apiTestCase, apiReport); apiTestCaseRecords.add(apiTestCaseRecord); } apiReportService.insertApiReport(apiReports, apiTestCaseRecords); return apiTestCaseRecords; } - private ApiTestCaseRecord getApiTestCaseRecord(ApiTestCase apiTestCase, ApiReport apiReport) { - ApiTestCaseRecord apiTestCaseRecord = new ApiTestCaseRecord(); - apiTestCaseRecord.setApiTestCaseId(apiTestCase.getId()); - apiTestCaseRecord.setApiReportId(apiReport.getId()); - return apiTestCaseRecord; - } private ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase, String userId) { ApiReport apiReport = getApiReport(runModeConfig, userId); apiReport.setEnvironmentId(getEnvId(runModeConfig, apiTestCase)); apiReport.setName(apiTestCase.getName()); apiReport.setProjectId(apiTestCase.getProjectId()); + apiReport.setTriggerMode(TaskTriggerMode.BATCH.name()); return apiReport; } - private ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, String userId) { - ApiReport apiReport = new ApiReport(); - apiReport.setId(IDGenerator.nextStr()); - apiReport.setDeleted(false); - apiReport.setIntegrated(false); + public ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, String userId) { + ApiReport apiReport = apiTestCaseService.getApiReport(userId); apiReport.setEnvironmentId(runModeConfig.getEnvironmentId()); apiReport.setRunMode(runModeConfig.getRunMode()); - apiReport.setStatus(ApiReportStatus.PENDING.name()); - apiReport.setStartTime(System.currentTimeMillis()); - apiReport.setUpdateTime(System.currentTimeMillis()); - apiReport.setTriggerMode(TaskTriggerMode.BATCH.name()); - apiReport.setUpdateUser(userId); - apiReport.setCreateUser(userId); apiReport.setPoolId(runModeConfig.getPoolId()); + apiReport.setTriggerMode(TaskTriggerMode.BATCH.name()); return apiReport; } @@ -369,7 +359,7 @@ public class ApiTestCaseBatchRunService { * @param apiTestCase * @return */ - private String getEnvId(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase) { + public String getEnvId(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase) { return StringUtils.isBlank(runModeConfig.getEnvironmentId()) ? apiTestCase.getEnvironmentId() : runModeConfig.getEnvironmentId(); } 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 085b3de37f..b18a04e2ed 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 @@ -21,9 +21,7 @@ import io.metersphere.project.dto.MoveNodeSortDTO; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.service.EnvironmentService; import io.metersphere.project.service.MoveNodeService; -import io.metersphere.sdk.constants.ApiExecuteRunMode; -import io.metersphere.sdk.constants.ApplicationNumScope; -import io.metersphere.sdk.constants.DefaultRepositoryDir; +import io.metersphere.sdk.constants.*; import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.domain.EnvironmentExample; import io.metersphere.sdk.dto.api.task.TaskRequestDTO; @@ -99,6 +97,8 @@ public class ApiTestCaseService extends MoveNodeService { @Resource private ApiExecuteService apiExecuteService; @Resource + private ApiReportService apiReportService; + @Resource private EnvironmentService environmentService; private static final String CASE_TABLE = "api_test_case"; @@ -626,14 +626,19 @@ public class ApiTestCaseService extends MoveNodeService { return apiFileResourceService.transfer(request, userId, ApiResourceType.API_CASE.name()); } - public TaskRequestDTO run(String id, String reportId) { + public TaskRequestDTO run(String id, String reportId, String userId) { ApiTestCase apiTestCase = checkResourceExist(id); ApiTestCaseBlob apiTestCaseBlob = apiTestCaseBlobMapper.selectByPrimaryKey(id); ApiResourceRunRequest runRequest = new ApiResourceRunRequest(); runRequest.setTestElement(ApiDataUtils.parseObject(new String(apiTestCaseBlob.getRequest()), AbstractMsTestElement.class)); + String poolId = apiExecuteService.getProjectApiResourcePoolId(apiTestCase.getProjectId()); + // 初始化报告 + initApiReport(apiTestCase, reportId, poolId, userId); + TaskRequestDTO taskRequest = getTaskRequest(reportId, apiTestCase.getId(), apiTestCase.getProjectId(), ApiExecuteRunMode.RUN.name()); + taskRequest.getRunModeConfig().setPoolId(poolId); taskRequest.setSaveResult(true); taskRequest.setRealTime(true); @@ -644,6 +649,56 @@ public class ApiTestCaseService extends MoveNodeService { return apiExecuteService.apiExecute(runRequest, taskRequest, apiParamConfig); } + /** + * 预生成用例的执行报告 + * + * @param apiTestCase + * @param poolId + * @param userId + * @return + */ + public List initApiReport(ApiTestCase apiTestCase, String reportId, String poolId, String userId) { + List apiReports = new ArrayList<>(); + List apiTestCaseRecords = new ArrayList<>(); + + // 初始化报告 + ApiReport apiReport = getApiReport(userId); + apiReport.setId(reportId); + apiReport.setTriggerMode(TaskTriggerMode.MANUAL.name()); + apiReports.add(apiReport); + apiReport.setName(apiTestCase.getName()); + apiReport.setRunMode(ApiBatchRunMode.PARALLEL.name()); + apiReport.setPoolId(poolId); + apiReport.setProjectId(apiTestCase.getProjectId()); + + // 创建报告和用例的关联关系 + ApiTestCaseRecord apiTestCaseRecord = getApiTestCaseRecord(apiTestCase, apiReport); + apiTestCaseRecords.add(apiTestCaseRecord); + + apiReportService.insertApiReport(apiReports, apiTestCaseRecords); + return apiTestCaseRecords; + } + + public ApiTestCaseRecord getApiTestCaseRecord(ApiTestCase apiTestCase, ApiReport apiReport) { + ApiTestCaseRecord apiTestCaseRecord = new ApiTestCaseRecord(); + apiTestCaseRecord.setApiTestCaseId(apiTestCase.getId()); + apiTestCaseRecord.setApiReportId(apiReport.getId()); + return apiTestCaseRecord; + } + + public ApiReport getApiReport(String userId) { + ApiReport apiReport = new ApiReport(); + apiReport.setId(IDGenerator.nextStr()); + apiReport.setDeleted(false); + apiReport.setIntegrated(false); + apiReport.setStatus(ApiReportStatus.PENDING.name()); + apiReport.setStartTime(System.currentTimeMillis()); + apiReport.setUpdateTime(System.currentTimeMillis()); + apiReport.setUpdateUser(userId); + apiReport.setCreateUser(userId); + return apiReport; + } + public TaskRequestDTO debug(ApiRunRequest request) { ApiResourceRunRequest runRequest = apiExecuteService.getApiResourceRunRequest(request); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java index 5adf4c7c8c..a0b5d2877f 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java @@ -97,7 +97,8 @@ public class ApiTestCaseControllerTests extends BaseTest { private static final String EXECUTE = "execute/page"; private static final String HISTORY = "operation-history/page"; private static final String DEBUG = "debug"; - private static final String RUN = "run/{0}/{1}"; + private static final String RUN_REAL_TIME = "run/{0}/{1}"; + private static final String RUN = "run/{0}"; private static final String BATCH_RUN = "batch/run"; private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); @@ -429,10 +430,15 @@ public class ApiTestCaseControllerTests extends BaseTest { @Test @Order(3) public void run() throws Exception { - assertErrorCode(this.requestGet(RUN, apiTestCase.getId(), "111"), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR); + assertErrorCode(this.requestGet(RUN_REAL_TIME, apiTestCase.getId(), "111"), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR); // @@校验权限 - requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN, apiTestCase.getId(), "11111"); + requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN_REAL_TIME, apiTestCase.getId(), "11111"); + + assertErrorCode(this.requestGet(RUN, apiTestCase.getId()), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR); + + // @@校验权限 + requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN, apiTestCase.getId()); } @Test