refactor(接口测试): 新增复制接口和用例时,复制文件接口

--story=1016879 --user=陈建星 【Bug转需求】【接口测试】场景-场景步骤复制接口/CASE-接口内存在本地上传的文件-场景内转存-报错 https://www.tapd.cn/55049933/s/1626892
This commit is contained in:
AgAngle 2024-12-09 18:42:25 +08:00 committed by Craftsman
parent 29b84693cd
commit 4c0bd68163
7 changed files with 87 additions and 16 deletions

View File

@ -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<String, String> copyFile(@Validated @RequestBody ApiFileCopyRequest request) {
return apiDefinitionService.copyFile(request);
}
@PostMapping(value = "/copy")
@Operation(summary = "接口测试-接口管理-复制接口定义")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_UPDATE)

View File

@ -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<String, String> copyFile(@Validated @RequestBody ApiFileCopyRequest request) {
return apiTestCaseService.copyFile(request);
}
@GetMapping(value = "/update-priority/{id}/{priority}")
@Operation(summary = "接口测试-接口管理-接口用例-更新等级")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE)

View File

@ -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<String> fileIds;
}

View File

@ -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<String, String> copyFiles2TempDir(List<String> fileIds, String sourceDir) {
Map<String, String> 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;
}
}

View File

@ -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<String, String> copyFile(ApiFileCopyRequest request) {
// 从接口定义复制
ApiDefinition apiDefinition = checkApiDefinition(request.getResourceId());
String sourceDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinition.getProjectId(),
apiDefinition.getId());
return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir);
}
}

View File

@ -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<String, String> copyFile(ApiFileCopyRequest request) {
// 从接口定义复制
ApiTestCase apiTestCase = checkResourceExist(request.getResourceId());
String sourceDir = DefaultRepositoryDir.getApiCaseDir(apiTestCase.getProjectId(),
apiTestCase.getId());
return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir);
}
}

View File

@ -2451,7 +2451,6 @@ public class ApiScenarioService extends MoveNodeService {
public Map<String, String> copyStepFile(ApiScenarioStepFileCopyRequest request) {
String sourceDir;
Map<String, String> 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);
}
}