From 4c0bd6816312f36bc806bd1d48d4a59d90762561 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 9 Dec 2024 18:42:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=96=B0=E5=A2=9E=E5=A4=8D=E5=88=B6=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=92=8C=E7=94=A8=E4=BE=8B=E6=97=B6=EF=BC=8C=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1016879 --user=陈建星 【Bug转需求】【接口测试】场景-场景步骤复制接口/CASE-接口内存在本地上传的文件-场景内转存-报错 https://www.tapd.cn/55049933/s/1626892 --- .../definition/ApiDefinitionController.java | 11 +++++++ .../definition/ApiTestCaseController.java | 10 +++++++ .../api/dto/scenario/ApiFileCopyRequest.java | 19 ++++++++++++ .../api/service/ApiCommonService.java | 29 ++++++++++++++++--- .../definition/ApiDefinitionService.java | 10 +++++++ .../definition/ApiTestCaseService.java | 11 +++++++ .../service/scenario/ApiScenarioService.java | 13 +-------- 7 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiFileCopyRequest.java diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java index 02b43ff7f1..acaf59d400 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java @@ -10,6 +10,8 @@ import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiEditPosRequest; import io.metersphere.api.dto.request.ApiTransferRequest; import io.metersphere.api.dto.request.ImportRequest; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; +import io.metersphere.api.dto.scenario.ApiScenarioStepFileCopyRequest; import io.metersphere.api.dto.schema.JsonSchemaItem; import io.metersphere.api.mapper.ExtApiDefinitionMapper; import io.metersphere.api.mapper.ExtApiScenarioStepMapper; @@ -49,6 +51,7 @@ import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** @@ -151,6 +154,14 @@ public class ApiDefinitionController { apiDefinitionService.batchUpdate(request, SessionUtils.getUserId()); } + @PostMapping("/file/copy") + @Operation(summary = "接口测试-接口管理-复制接口时,复制文件") + @RequiresPermissions(value = {PermissionConstants.PROJECT_API_DEFINITION_UPDATE, PermissionConstants.PROJECT_API_DEFINITION_ADD}, + logical = Logical.OR) + public Map copyFile(@Validated @RequestBody ApiFileCopyRequest request) { + return apiDefinitionService.copyFile(request); + } + @PostMapping(value = "/copy") @Operation(summary = "接口测试-接口管理-复制接口定义") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_UPDATE) 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 c39a8d6f41..d3cb52602e 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 @@ -9,6 +9,7 @@ import io.metersphere.api.dto.ReferenceDTO; import io.metersphere.api.dto.ReferenceRequest; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiTransferRequest; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; import io.metersphere.api.service.ApiFileResourceService; import io.metersphere.api.service.definition.*; import io.metersphere.plugin.api.spi.AbstractMsTestElement; @@ -42,6 +43,7 @@ import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; +import java.util.Map; @RestController @@ -140,6 +142,14 @@ public class ApiTestCaseController { return apiTestCaseService.update(request, SessionUtils.getUserId()); } + @PostMapping("/file/copy") + @Operation(summary = "接口测试-接口管理-复制用例时,复制文件") + @RequiresPermissions(value = {PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE, PermissionConstants.PROJECT_API_DEFINITION_CASE_ADD}, + logical = Logical.OR) + public Map copyFile(@Validated @RequestBody ApiFileCopyRequest request) { + return apiTestCaseService.copyFile(request); + } + @GetMapping(value = "/update-priority/{id}/{priority}") @Operation(summary = "接口测试-接口管理-接口用例-更新等级") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiFileCopyRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiFileCopyRequest.java new file mode 100644 index 0000000000..54c8f7d1e7 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiFileCopyRequest.java @@ -0,0 +1,19 @@ +package io.metersphere.api.dto.scenario; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; + +import java.util.List; + +@Data +public class ApiFileCopyRequest { + @Schema(description = "资源id") + @NotBlank + private String resourceId; + + @Schema(description = "文件数组") + @NotEmpty + private List fileIds; +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java index 0da718f01a..2e7d706aca 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java @@ -31,6 +31,7 @@ import io.metersphere.project.service.CustomFunctionService; import io.metersphere.project.service.FileAssociationService; import io.metersphere.project.service.FileMetadataService; import io.metersphere.sdk.constants.ApplicationNumScope; +import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.ExecStatus; import io.metersphere.sdk.constants.TaskItemErrorMessage; import io.metersphere.sdk.dto.api.task.GetRunScriptRequest; @@ -61,10 +62,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -84,6 +82,8 @@ public class ApiCommonService { @Resource private ExecTaskItemMapper execTaskItemMapper; @Resource + private ApiFileResourceService apiFileResourceService; + @Resource private ExecTaskMapper execTaskMapper; /** @@ -618,4 +618,25 @@ public class ApiCommonService { } return null; } + + /** + * 复制文件到临时目录 + * @param fileIds + * @param sourceDir + * @return + */ + public Map copyFiles2TempDir(List fileIds, String sourceDir) { + Map uploadFileMap = new HashMap<>(); + for (String fileId : fileIds) { + String newFileId = IDGenerator.nextStr(); + String targetDir = DefaultRepositoryDir.getSystemTempDir(); + String fileName = apiFileResourceService.getFileNameByFileId(fileId, sourceDir); + // 复制文件到临时目录 + apiFileResourceService.copyFile(sourceDir + "/" + fileId, + targetDir + "/" + newFileId, + fileName); + uploadFileMap.put(fileId, newFileId); + } + return uploadFileMap; + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index 77e6a40937..3963f0ce5f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -9,6 +9,7 @@ import io.metersphere.api.dto.debug.ApiResourceRunRequest; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiEditPosRequest; import io.metersphere.api.dto.request.ApiTransferRequest; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; import io.metersphere.api.dto.schema.JsonSchemaItem; import io.metersphere.api.mapper.*; import io.metersphere.api.service.ApiCommonService; @@ -1354,4 +1355,13 @@ public class ApiDefinitionService extends MoveNodeService { private boolean isObjectItem(JsonSchemaItem jsonSchemaItem) { return StringUtils.equals(jsonSchemaItem.getType(), PropertyConstant.OBJECT); } + + public Map copyFile(ApiFileCopyRequest request) { + // 从接口定义复制 + ApiDefinition apiDefinition = checkApiDefinition(request.getResourceId()); + String sourceDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinition.getProjectId(), + apiDefinition.getId()); + + return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir); + } } 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 af599aa3b5..4c8b6da56d 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 @@ -8,6 +8,7 @@ import io.metersphere.api.dto.debug.ApiFileResourceUpdateRequest; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiTransferRequest; import io.metersphere.api.dto.request.http.MsHTTPElement; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; import io.metersphere.api.mapper.*; import io.metersphere.api.service.ApiCommonService; import io.metersphere.api.service.ApiFileResourceService; @@ -52,6 +53,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.jetbrains.annotations.NotNull; import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -867,4 +869,13 @@ public class ApiTestCaseService extends MoveNodeService { extApiTestCaseMapper.clearApiChange(List.of(request.getId())); return HttpRequestParamDiffUtils.syncRequestDiff(request, apiMsTestElement, apiTestCaseMsTestElement); } + + public Map copyFile(ApiFileCopyRequest request) { + // 从接口定义复制 + ApiTestCase apiTestCase = checkResourceExist(request.getResourceId()); + String sourceDir = DefaultRepositoryDir.getApiCaseDir(apiTestCase.getProjectId(), + apiTestCase.getId()); + + return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index e057111b7d..62c1ab3ca6 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -2451,7 +2451,6 @@ public class ApiScenarioService extends MoveNodeService { public Map copyStepFile(ApiScenarioStepFileCopyRequest request) { String sourceDir; - Map uploadFileMap = new HashMap<>(); if (BooleanUtils.isTrue(request.getIsTempFile())) { sourceDir = DefaultRepositoryDir.getSystemTempDir(); } else { @@ -2477,16 +2476,6 @@ public class ApiScenarioService extends MoveNodeService { } } - for (String fileId : request.getFileIds()) { - String newFileId = IDGenerator.nextStr(); - String targetDir = DefaultRepositoryDir.getSystemTempDir(); - String fileName = apiFileResourceService.getFileNameByFileId(fileId, sourceDir); - // 复制文件到临时目录 - apiFileResourceService.copyFile(sourceDir + "/" + fileId, - targetDir + "/" + newFileId, - fileName); - uploadFileMap.put(fileId, newFileId); - } - return uploadFileMap; + return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir); } }