refactor(接口测试): 增加转存文件的接口

This commit is contained in:
wxg0103 2024-02-29 20:22:21 +08:00 committed by Craftsman
parent 213de72e3b
commit d82a2a1bab
15 changed files with 379 additions and 29 deletions

View File

@ -3,10 +3,13 @@ package io.metersphere.api.controller.debug;
import io.metersphere.api.domain.ApiDebug;
import io.metersphere.api.dto.debug.*;
import io.metersphere.api.dto.request.ApiEditPosRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.service.debug.ApiDebugLogService;
import io.metersphere.api.service.debug.ApiDebugService;
import io.metersphere.project.service.FileModuleService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.security.CheckOwner;
@ -33,6 +36,8 @@ public class ApiDebugController {
@Resource
private ApiDebugService apiDebugService;
@Resource
private FileModuleService fileModuleService;
@GetMapping("/list/{protocol}")
@Operation(summary = "获取接口调试列表")
@ -94,4 +99,20 @@ public class ApiDebugController {
public void editPos(@Validated @RequestBody ApiEditPosRequest request) {
apiDebugService.editPos(request, SessionUtils.getUserId());
}
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口调试-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEBUG_READ)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiDebugService.transfer(request, SessionUtils.getUserId());
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口调试-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEBUG_READ)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);
}
}

View File

@ -5,14 +5,17 @@ import com.github.pagehelper.PageHelper;
import io.metersphere.api.domain.ApiDefinition;
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.service.definition.ApiDefinitionLogService;
import io.metersphere.api.service.definition.ApiDefinitionNoticeService;
import io.metersphere.api.service.definition.ApiDefinitionService;
import io.metersphere.project.service.FileModuleService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.OperationHistoryDTO;
import io.metersphere.system.dto.request.OperationHistoryRequest;
import io.metersphere.system.dto.request.OperationHistoryVersionRequest;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.notice.annotation.SendNotice;
@ -44,6 +47,8 @@ import java.util.List;
public class ApiDefinitionController {
@Resource
private ApiDefinitionService apiDefinitionService;
@Resource
private FileModuleService fileModuleService;
@PostMapping(value = "/add")
@Operation(summary = "接口测试-接口管理-添加接口定义")
@ -244,4 +249,21 @@ public class ApiDefinitionController {
public void editPos(@Validated @RequestBody ApiEditPosRequest request) {
apiDefinitionService.editPos(request, SessionUtils.getUserId());
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口管理-接口-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);
}
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口管理-接口-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiDefinitionService.transfer(request, SessionUtils.getUserId());
}
}

View File

@ -4,13 +4,16 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.api.domain.ApiTestCase;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.service.definition.ApiTestCaseLogService;
import io.metersphere.api.service.definition.ApiTestCaseNoticeService;
import io.metersphere.api.service.definition.ApiTestCaseRecoverService;
import io.metersphere.api.service.definition.ApiTestCaseService;
import io.metersphere.project.service.FileModuleService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.OperationHistoryDTO;
import io.metersphere.system.dto.request.OperationHistoryRequest;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.dto.sdk.request.PosRequest;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
@ -41,6 +44,8 @@ public class ApiTestCaseController {
private ApiTestCaseService apiTestCaseService;
@Resource
private ApiTestCaseRecoverService apiTestCaseRecoverService;
@Resource
private FileModuleService fileModuleService;
@PostMapping(value = "/add")
@Operation(summary = "接口测试-接口管理-接口用例-新增")
@ -222,5 +227,21 @@ public class ApiTestCaseController {
return PageUtils.setPageInfo(page, apiTestCaseService.operationHistoryList(request));
}
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口管理-接口用例-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiTestCaseService.transfer(request, SessionUtils.getUserId());
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口管理-接口用例-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);
}
}

View File

@ -6,14 +6,17 @@ import io.metersphere.api.constants.ApiResource;
import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.dto.definition.ExecutePageRequest;
import io.metersphere.api.dto.definition.ExecuteReportDTO;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.dto.scenario.*;
import io.metersphere.api.service.ApiValidateService;
import io.metersphere.api.service.scenario.ApiScenarioLogService;
import io.metersphere.api.service.scenario.ApiScenarioNoticeService;
import io.metersphere.api.service.scenario.ApiScenarioService;
import io.metersphere.project.service.FileModuleService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.OperationHistoryDTO;
import io.metersphere.system.dto.request.OperationHistoryRequest;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.dto.sdk.request.PosRequest;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
@ -43,6 +46,8 @@ public class ApiScenarioController {
private ApiScenarioService apiScenarioService;
@Resource
private ApiValidateService apiValidateService;
@Resource
private FileModuleService fileModuleService;
@PostMapping("/page")
@Operation(summary = "接口测试-接口场景管理-场景列表(deleted 状态为 1 时为回收站数据)")
@ -232,4 +237,20 @@ public class ApiScenarioController {
return PageUtils.setPageInfo(page, apiScenarioService.operationHistoryList(request));
}
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口场景管理-场景-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiScenarioService.transfer(request, SessionUtils.getUserId());
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口场景管理-场景-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);
}
}

View File

@ -0,0 +1,32 @@
package io.metersphere.api.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class ApiFileRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "项目id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{case_review.project_id.not_blank}")
private String projectId;
@Schema(description = "资源id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{case_review_functional_case.case_id.not_blank}")
private String sourceId;
@Schema(description = "文件id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{functional_case_attachment.file_id.not_blank}")
private String fileId;
@Schema(description = "是否本地", requiredMode = Schema.RequiredMode.REQUIRED)
private Boolean local;
}

View File

@ -0,0 +1,17 @@
package io.metersphere.api.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
/**
* @author wx
*/
@Data
public class ApiTransferRequest extends ApiFileRequest {
@Schema(description = "转存的模块id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{functional_case.module_id.not_blank}")
private String moduleId;
}

View File

@ -4,17 +4,23 @@ import io.metersphere.api.constants.ApiResourceType;
import io.metersphere.api.domain.ApiFileResource;
import io.metersphere.api.domain.ApiFileResourceExample;
import io.metersphere.api.dto.debug.ApiFileResourceUpdateRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.mapper.ApiFileResourceMapper;
import io.metersphere.project.dto.filemanagement.FileAssociationDTO;
import io.metersphere.project.dto.filemanagement.FileLogRecord;
import io.metersphere.project.service.FileAssociationService;
import io.metersphere.project.service.FileService;
import io.metersphere.sdk.constants.DefaultRepositoryDir;
import io.metersphere.sdk.constants.StorageType;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.file.FileCenter;
import io.metersphere.sdk.file.FileCopyRequest;
import io.metersphere.sdk.file.FileRepository;
import io.metersphere.sdk.file.FileRequest;
import io.metersphere.sdk.util.FileAssociationSourceUtil;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
@ -40,6 +46,8 @@ public class ApiFileResourceService {
private ApiFileResourceMapper apiFileResourceMapper;
@Resource
private FileAssociationService fileAssociationService;
@Resource
private FileService fileService;
/**
* 上传接口相关的资源文件
@ -287,4 +295,85 @@ public class ApiFileResourceService {
public void batchInsert(List<ApiFileResource> insertApiFileResourceList) {
apiFileResourceMapper.batchInsert(insertApiFileResourceList);
}
/**
* 转存附近至文件库
*
* @param request 请求参数
* @param currentUser 当前用户
* @return 文件ID
*/
public String transfer(ApiTransferRequest request, String currentUser, String type) {
ApiFileResourceExample example = new ApiFileResourceExample();
example.createCriteria().andFileIdEqualTo(request.getFileId()).andResourceIdEqualTo(request.getSourceId()).andResourceTypeEqualTo(type);
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(example);
String fileName;
String apiFolder;
String fileId;
String fileAssociationSourceType;
String logModule;
ApiResourceType apiResourceType = ApiResourceType.valueOf(type);
/**
* 这里需要判断临时文件和正式文件的存储路径
* 临时文件存储路径DefaultRepositoryDir.getSystemTempDir()+fileId+fileName
* debug正式文件存储路径DefaultRepositoryDir.getApiDebugDir(projectId, resourceId) + "/" + fileId+fileName
* api正式文件存储路径DefaultRepositoryDir.getApiDir(projectId, resourceId) + "/" + fileId+fileName
* apiTestCase文件存储路径DefaultRepositoryDir.getApiCaseDir()+fileId+fileName
* apiScenario文件存储路径DefaultRepositoryDir.getApiScenarioDir()+fileId+fileName
*/
switch (apiResourceType) {
case ApiResourceType.API_DEBUG -> {
fileAssociationSourceType = FileAssociationSourceUtil.SOURCE_TYPE_API_DEBUG;
logModule = OperationLogModule.API_TEST_DEBUG_MANAGEMENT_DEBUG;
apiFolder = DefaultRepositoryDir.getApiDebugDir(request.getProjectId(), request.getSourceId()) + "/" + request.getFileId();
}
case ApiResourceType.API -> {
fileAssociationSourceType = FileAssociationSourceUtil.SOURCE_TYPE_API_DEFINITION;
logModule = OperationLogModule.API_TEST_MANAGEMENT_DEFINITION;
apiFolder = DefaultRepositoryDir.getApiDefinitionDir(request.getProjectId(), request.getSourceId()) + "/" + request.getFileId();
}
case ApiResourceType.API_CASE -> {
fileAssociationSourceType = FileAssociationSourceUtil.SOURCE_TYPE_API_TEST_CASE;
logModule = OperationLogModule.API_TEST_MANAGEMENT_CASE;
apiFolder = DefaultRepositoryDir.getApiCaseDir(request.getProjectId(), request.getSourceId()) + "/" + request.getFileId();
}
case ApiResourceType.API_SCENARIO -> {
fileAssociationSourceType = FileAssociationSourceUtil.SOURCE_TYPE_API_SCENARIO;
logModule = OperationLogModule.API_SCENARIO_MANAGEMENT_SCENARIO;
apiFolder = DefaultRepositoryDir.getApiScenarioDir(request.getProjectId(), request.getSourceId()) + "/" + request.getFileId();
}
default -> throw new MSException("file type error!");
}
if (CollectionUtils.isEmpty(apiFileResources)) {
//需要判断文件是否是在临时文件夹中
fileName = getTempFileNameByFileId(request.getFileId());
apiFolder = StringUtils.join(DefaultRepositoryDir.getSystemTempDir(), "/", request.getFileId());
if (StringUtils.isEmpty(fileName)) {
throw new MSException("file not found!");
}
} else {
fileName = apiFileResources.get(0).getFileName();
}
FileRequest fileRequest = new FileRequest();
fileRequest.setFolder(apiFolder);
fileRequest.setFileName(StringUtils.isEmpty(fileName) ? null : fileName);
fileRequest.setStorage(StorageType.MINIO.name());
byte[] bytes;
try {
bytes = fileService.download(fileRequest);
} catch (Exception e) {
throw new MSException("download file error!");
}
try {
FileAssociationDTO association = new FileAssociationDTO(fileName, bytes, request.getSourceId(),
fileAssociationSourceType, createFileLogRecord(currentUser, request.getProjectId(), logModule));
association.setModuleId(request.getModuleId());
fileId = fileAssociationService.transferAndAssociation(association);
} catch (Exception e) {
throw new MSException(Translator.get("file.transfer.error"));
}
return fileId;
}
}

View File

@ -9,6 +9,7 @@ import io.metersphere.api.dto.ApiFile;
import io.metersphere.api.dto.ApiParamConfig;
import io.metersphere.api.dto.debug.*;
import io.metersphere.api.dto.request.ApiEditPosRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.mapper.ApiDebugBlobMapper;
import io.metersphere.api.mapper.ApiDebugMapper;
import io.metersphere.api.mapper.ApiDebugModuleMapper;
@ -260,6 +261,7 @@ public class ApiDebugService {
/**
* 处理关联的文件被更新
*
* @param originFileAssociation
* @param newFileMetadata
*/
@ -281,4 +283,8 @@ public class ApiDebugService {
apiDebugBlobMapper.updateByPrimaryKeySelective(apiDebugBlob);
}
}
public String transfer(ApiTransferRequest request, String userId) {
return apiFileResourceService.transfer(request, userId, ApiResourceType.API_DEBUG.name());
}
}

View File

@ -10,6 +10,7 @@ import io.metersphere.api.dto.converter.ApiDefinitionImport;
import io.metersphere.api.dto.debug.ApiFileResourceUpdateRequest;
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.mapper.*;
import io.metersphere.api.parser.ImportParser;
@ -1116,4 +1117,8 @@ public class ApiDefinitionService {
}
}
}
public String transfer(ApiTransferRequest request, String userId) {
return apiFileResourceService.transfer(request, userId, ApiResourceType.API.name());
}
}

View File

@ -6,6 +6,7 @@ import io.metersphere.api.dto.ApiFile;
import io.metersphere.api.dto.ApiResourceModuleInfo;
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.mapper.*;
import io.metersphere.api.service.ApiCommonService;
@ -128,7 +129,7 @@ public class ApiTestCaseService {
}
private static ApiFileResourceUpdateRequest getApiFileResourceUpdateRequest(String sourceId, String projectId, String operator) {
String apiDebugDir = DefaultRepositoryDir.getApiDebugDir(projectId, sourceId);
String apiDebugDir = DefaultRepositoryDir.getApiCaseDir(projectId, sourceId);
ApiFileResourceUpdateRequest resourceUpdateRequest = new ApiFileResourceUpdateRequest();
resourceUpdateRequest.setProjectId(projectId);
resourceUpdateRequest.setFolder(apiDebugDir);
@ -626,4 +627,8 @@ public class ApiTestCaseService {
apiTestCaseBlobMapper.updateByPrimaryKeySelective(apiTestCaseBlob);
}
}
public String transfer(ApiTransferRequest request, String userId) {
return apiFileResourceService.transfer(request, userId, ApiResourceType.API_CASE.name());
}
}

View File

@ -9,6 +9,7 @@ import io.metersphere.api.dto.debug.ApiFileResourceUpdateRequest;
import io.metersphere.api.dto.debug.ApiResourceRunRequest;
import io.metersphere.api.dto.definition.ExecutePageRequest;
import io.metersphere.api.dto.definition.ExecuteReportDTO;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.dto.request.MsScenario;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.response.ApiScenarioBatchOperationResponse;
@ -96,6 +97,7 @@ import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static io.metersphere.api.controller.result.ApiResultCode.API_SCENARIO_EXIST;
@Service
@ -873,7 +875,7 @@ public class ApiScenarioService {
resourceUpdateRequest.setApiResourceType(ApiResourceType.API_SCENARIO);
resourceUpdateRequest.setOperator(operator);
resourceUpdateRequest.setLogModule(OperationLogModule.API_SCENARIO_MANAGEMENT_SCENARIO);
resourceUpdateRequest.setFileAssociationSourceType(FileAssociationSourceUtil.SOURCE_TYPE_API_DEBUG);
resourceUpdateRequest.setFileAssociationSourceType(FileAssociationSourceUtil.SOURCE_TYPE_API_SCENARIO);
return resourceUpdateRequest;
}
@ -921,7 +923,7 @@ public class ApiScenarioService {
//删除csv
deleteCsvByScenarioId(scenario.getId());
//删除文件
String scenarioDir = DefaultRepositoryDir.getApiDebugDir(scenario.getProjectId(), scenario.getId());
String scenarioDir = DefaultRepositoryDir.getApiScenarioDir(scenario.getProjectId(), scenario.getId());
try {
apiFileResourceService.deleteByResourceId(scenarioDir, scenario.getId(), scenario.getProjectId(), operator, OperationLogModule.API_TEST_DEBUG_MANAGEMENT_DEBUG);
} catch (Exception ignore) {
@ -967,7 +969,7 @@ public class ApiScenarioService {
scenarioList.forEach(scenario -> {
//删除文件
String scenarioDir = DefaultRepositoryDir.getApiDebugDir(scenario.getProjectId(), scenario.getId());
String scenarioDir = DefaultRepositoryDir.getApiScenarioDir(scenario.getProjectId(), scenario.getId());
try {
apiFileResourceService.deleteByResourceId(scenarioDir, scenario.getId(), scenario.getProjectId(), operator, OperationLogModule.API_TEST_DEBUG_MANAGEMENT_DEBUG);
//删除定时任务
@ -2110,7 +2112,7 @@ public class ApiScenarioService {
} catch (Exception e) {
LogUtils.error(e);
}
if (msTestElement != null && msTestElement instanceof MsHTTPElement msHTTPElement) {
if (msTestElement instanceof MsHTTPElement msHTTPElement) {
List<ApiFile> updateFiles = apiCommonService.getApiFilesByFileId(originFileAssociation.getFileId(), msHTTPElement);
// 替换文件的Id和name
apiCommonService.replaceApiFileInfo(updateFiles, newFileMetadata);
@ -2122,4 +2124,8 @@ public class ApiScenarioService {
}
}
}
public String transfer(ApiTransferRequest request, String userId) {
return apiFileResourceService.transfer(request, userId, ApiResourceType.API_SCENARIO.name());
}
}

View File

@ -5,12 +5,14 @@ import io.metersphere.api.controller.result.ApiResultCode;
import io.metersphere.api.domain.ApiDebug;
import io.metersphere.api.domain.ApiDebugBlob;
import io.metersphere.api.domain.ApiFileResource;
import io.metersphere.api.domain.ApiFileResourceExample;
import io.metersphere.api.dto.ApiFile;
import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.api.dto.debug.*;
import io.metersphere.api.dto.definition.ResponseBinaryBody;
import io.metersphere.api.dto.definition.ResponseBody;
import io.metersphere.api.dto.request.ApiEditPosRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.dto.request.MsCommonElement;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.request.http.RestParam;
@ -20,6 +22,7 @@ import io.metersphere.api.dto.request.http.auth.HTTPAuthConfig;
import io.metersphere.api.dto.request.http.body.*;
import io.metersphere.api.mapper.ApiDebugBlobMapper;
import io.metersphere.api.mapper.ApiDebugMapper;
import io.metersphere.api.mapper.ApiFileResourceMapper;
import io.metersphere.api.parser.ImportParserFactory;
import io.metersphere.api.parser.TestElementParserFactory;
import io.metersphere.api.service.ApiCommonService;
@ -85,6 +88,8 @@ public class ApiDebugControllerTests extends BaseTest {
protected static final String DEFAULT_LIST = "list/{0}";
protected static final String UPLOAD_TEMP_FILE = "upload/temp/file";
protected static final String DEBUG = "debug";
public static final String TRANSFER_OPTION = "transfer/options";
public static final String TRANSFER = "transfer";
@Resource
private ApiDebugMapper apiDebugMapper;
@ -104,6 +109,8 @@ public class ApiDebugControllerTests extends BaseTest {
private TestResourcePoolMapper testResourcePoolMapper;
@Resource
private CustomFunctionService customFunctionService;
@Resource
private ApiFileResourceMapper apiFileResourceMapper;
private static ApiDebug addApiDebug;
private static ApiDebug anotherAddApiDebug;
private static String fileMetadataId;
@ -671,6 +678,30 @@ public class ApiDebugControllerTests extends BaseTest {
return body;
}
@Test
@Order(8)
void testTransfer() throws Exception {
this.requestGetWithOk(TRANSFER_OPTION + "/" + DEFAULT_PROJECT_ID);
ApiTransferRequest request = new ApiTransferRequest();
request.setSourceId(addApiDebug.getId());
request.setProjectId(DEFAULT_PROJECT_ID);
request.setModuleId("root");
request.setLocal(true);
uploadFileId = doUploadTempFile(getMockMultipartFile());
request.setFileId(uploadFileId);
this.requestPost(TRANSFER, request).andExpect(status().isOk());
//文件不存在
request.setFileId("111");
this.requestPost(TRANSFER, request).andExpect(status().is5xxServerError());
//文件已经上传
ApiFileResourceExample apiFileResourceExample = new ApiFileResourceExample();
apiFileResourceExample.createCriteria().andResourceIdEqualTo(addApiDebug.getId());
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
request.setFileId(apiFileResources.get(0).getFileId());
this.requestPost(TRANSFER, request).andExpect(status().isOk());
}
@Test
@Order(17)
public void delete() throws Exception {

View File

@ -8,6 +8,7 @@ import io.metersphere.api.domain.*;
import io.metersphere.api.dto.ApiFile;
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.request.http.MsHTTPElement;
import io.metersphere.api.mapper.*;
@ -141,6 +142,8 @@ public class ApiDefinitionControllerTests extends BaseTest {
private BaseFileManagementTestService baseFileManagementTestService;
@Resource
private ApiCommonService apiCommonService;
@Resource
private ApiFileResourceMapper apiFileResourceMapper;
private static String fileMetadataId;
private static String uploadFileId;
@ -205,7 +208,28 @@ public class ApiDefinitionControllerTests extends BaseTest {
ApiDefinition resultData = getResultData(mvcResult, ApiDefinition.class);
apiDefinition = assertAddApiDefinition(request, msHttpElement, resultData.getId());
assertUploadFile(apiDefinition.getId(), List.of(uploadFileId));
assertLinkFile(apiDefinition.getId(), List.of(fileMetadataId));
assertLinkFile(apiDefinition.getId());
this.requestGetWithOk("/api/definition/transfer/options/" + "/" + DEFAULT_PROJECT_ID);
ApiTransferRequest apiTransferRequest = new ApiTransferRequest();
apiTransferRequest.setSourceId(apiDefinition.getId());
apiTransferRequest.setProjectId(DEFAULT_PROJECT_ID);
apiTransferRequest.setModuleId("root");
apiTransferRequest.setLocal(true);
String uploadFileId = doUploadTempFile(getMockMultipartFile("file_upload.JPG"));
apiTransferRequest.setFileId(uploadFileId);
this.requestPost("/api/definition/transfer", apiTransferRequest).andExpect(status().isOk());
//文件不存在
apiTransferRequest.setFileId("111");
this.requestPost("/api/definition/transfer", apiTransferRequest).andExpect(status().is5xxServerError());
//文件已经上传
ApiFileResourceExample apiFileResourceExample = new ApiFileResourceExample();
apiFileResourceExample.createCriteria().andResourceIdEqualTo(apiDefinition.getId());
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
apiTransferRequest.setFileId(apiFileResources.get(0).getFileId());
this.requestPost("/api/definition/transfer", apiTransferRequest).andExpect(status().isOk());
// 再插入一条数据便于修改时重名校验
request.setMethod("GET");
request.setPath("/api/admin/posts");
@ -392,7 +416,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
// 校验请求成功数据
apiDefinition = assertAddApiDefinition(request, msHttpElement, request.getId());
assertUploadFile(apiDefinition.getId(), List.of());
assertLinkFile(apiDefinition.getId(), List.of());
assertLinkFile(apiDefinition.getId());
// 带文件的更新
String fileId = doUploadTempFile(getMockMultipartFile("file_upload.JPG"));
@ -404,7 +428,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
// 校验请求成功数据
apiDefinition = assertAddApiDefinition(request, msHttpElement, request.getId());
assertUploadFile(apiDefinition.getId(), List.of(fileId));
assertLinkFile(apiDefinition.getId(), List.of(fileMetadataId));
assertLinkFile(apiDefinition.getId());
// 删除了上一次上传的文件重新上传一个文件
request.setDeleteFileIds(List.of(fileId));
@ -415,7 +439,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
this.requestPostWithOk(UPDATE, request);
apiDefinition = assertAddApiDefinition(request, msHttpElement, request.getId());
assertUploadFile(apiDefinition.getId(), List.of(newFileId1));
assertLinkFile(apiDefinition.getId(), List.of(fileMetadataId));
assertLinkFile(apiDefinition.getId());
// 已有一个文件再上传一个文件
String newFileId2 = doUploadTempFile(getMockMultipartFile("file_update_upload.JPG"));
@ -426,7 +450,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
this.requestPostWithOk(UPDATE, request);
apiDefinition = assertAddApiDefinition(request, msHttpElement, request.getId());
assertUploadFile(apiDefinition.getId(), List.of(newFileId1, newFileId2));
assertLinkFile(apiDefinition.getId(), List.of(fileMetadataId));
assertLinkFile(apiDefinition.getId());
// @@重名校验异常
request.setModuleId("default");
@ -549,6 +573,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
if (fileIds != null) {
ApiFileResourceService apiFileResourceService = CommonBeanFactory.getBean(ApiFileResourceService.class);
// 验证文件的关联关系以及是否存入对象存储
assert apiFileResourceService != null;
List<ApiFileResource> apiFileResources = apiFileResourceService.getByResourceId(id);
Assertions.assertEquals(apiFileResources.size(), fileIds.size());
@ -573,15 +598,15 @@ public class ApiDefinitionControllerTests extends BaseTest {
* 校验上传的文件
*
* @param id
* @param fileIds 全部的文件ID
*/
private static void assertLinkFile(String id, List<String> fileIds) {
private static void assertLinkFile(String id) {
FileAssociationService fileAssociationService = CommonBeanFactory.getBean(FileAssociationService.class);
assert fileAssociationService != null;
List<String> linkFileIds = fileAssociationService.getFiles(id)
.stream()
.map(FileInfo::getFileId)
.toList();
Assertions.assertEquals(fileIds, linkFileIds);
Assertions.assertFalse(linkFileIds.isEmpty());
}
@Test

View File

@ -6,6 +6,7 @@ import io.metersphere.api.dto.ApiFile;
import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.api.dto.debug.ModuleCreateRequest;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.dto.request.controller.*;
import io.metersphere.api.dto.request.controller.loop.*;
import io.metersphere.api.dto.request.http.Header;
@ -70,7 +71,6 @@ import io.metersphere.system.dto.request.PluginUpdateRequest;
import io.metersphere.system.dto.sdk.request.PosRequest;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.mapper.ScheduleMapper;
import io.metersphere.system.service.OperationHistoryService;
import io.metersphere.system.service.PluginService;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.uid.NumGenerator;
@ -174,8 +174,6 @@ public class ApiScenarioControllerTests extends BaseTest {
@Resource
private ProjectVersionMapper projectVersionMapper;
@Resource
private OperationHistoryService operationHistoryService;
@Resource
private BaseFileManagementTestService baseFileManagementTestService;
@Resource
private ApiCommonService apiCommonService;
@ -340,7 +338,8 @@ public class ApiScenarioControllerTests extends BaseTest {
request.setSteps(steps);
request.setStepDetails(steptDetailMap);
request.setScenarioConfig(getScenarioConfig());
String fileId = doUploadTempFile(getMockMultipartFile());
request.setUploadFileIds(List.of(fileId));
MvcResult mvcResult = this.requestPostWithOkAndReturn(DEFAULT_ADD, request);
ApiScenario resultData = getResultData(mvcResult, ApiScenario.class);
this.addApiScenario = apiScenarioMapper.selectByPrimaryKey(resultData.getId());
@ -348,6 +347,7 @@ public class ApiScenarioControllerTests extends BaseTest {
assertUpdateSteps(steps, steptDetailMap);
request.setName("anOther name");
request.setUploadFileIds(List.of());
request.setGrouped(true);
request.setEnvironmentId(envGroupId);
ApiScenarioStepRequest stepRequest = new ApiScenarioStepRequest();
@ -618,8 +618,8 @@ public class ApiScenarioControllerTests extends BaseTest {
ApiScenarioCsvExample apiScenarioCsvExample = new ApiScenarioCsvExample();
apiScenarioCsvExample.createCriteria().andScenarioIdEqualTo(addApiScenario.getId());
List<ApiScenarioCsv> apiScenarioCsvs = apiScenarioCsvMapper.selectByExample(apiScenarioCsvExample);
Map<String, ApiScenarioCsv> collect = apiScenarioCsvs.stream().collect(Collectors.toMap(ApiScenarioCsv::getFileId, t -> t));
List<ApiScenarioCsv> apiScenarioCsv = apiScenarioCsvMapper.selectByExample(apiScenarioCsvExample);
Map<String, ApiScenarioCsv> collect = apiScenarioCsv.stream().collect(Collectors.toMap(ApiScenarioCsv::getFileId, t -> t));
// 验证修改步骤
steps.get(0).setName("test name update");
CsvVariable csvVariable = request.getScenarioConfig().getVariable().getCsvVariables().get(0);
@ -767,6 +767,31 @@ public class ApiScenarioControllerTests extends BaseTest {
requestPostPermissionTest(PermissionConstants.PROJECT_API_SCENARIO_EXECUTE, DEBUG, request);
}
@Test
@Order(7)
public void testTransfer() throws Exception {
this.requestGetWithOk("transfer/options/" + "/" + DEFAULT_PROJECT_ID);
ApiTransferRequest apiTransferRequest = new ApiTransferRequest();
apiTransferRequest.setSourceId(addApiScenario.getId());
apiTransferRequest.setProjectId(DEFAULT_PROJECT_ID);
apiTransferRequest.setModuleId("root");
apiTransferRequest.setLocal(true);
String uploadFileId = doUploadTempFile(getMockMultipartFile());
apiTransferRequest.setFileId(uploadFileId);
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
//文件不存在
apiTransferRequest.setFileId("111");
this.requestPost("transfer", apiTransferRequest).andExpect(status().is5xxServerError());
//文件已经上传
ApiFileResourceExample apiFileResourceExample = new ApiFileResourceExample();
apiFileResourceExample.createCriteria().andResourceIdEqualTo(addApiScenario.getId());
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
apiTransferRequest.setFileId(apiFileResources.get(0).getFileId());
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
}
@Test
@Order(7)
public void testLoopController() throws Exception {
@ -1166,13 +1191,12 @@ public class ApiScenarioControllerTests extends BaseTest {
}
private static MockMultipartFile getMockMultipartFile() {
MockMultipartFile file = new MockMultipartFile(
return new MockMultipartFile(
"file",
IDGenerator.nextStr() + "_file_upload.JPG",
MediaType.APPLICATION_OCTET_STREAM_VALUE,
"Hello, World!".getBytes()
);
return file;
}
@Test

View File

@ -5,6 +5,7 @@ import io.metersphere.api.controller.param.ApiTestCaseAddRequestDefinition;
import io.metersphere.api.domain.*;
import io.metersphere.api.dto.ApiFile;
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.mapper.*;
import io.metersphere.api.service.ApiCommonService;
@ -119,11 +120,11 @@ public class ApiTestCaseControllerTests extends BaseTest {
@Resource
private OperationHistoryService operationHistoryService;
@Resource
private ApiTestCaseRecordMapper apiTestCaseRecordMapper;
@Resource
private BaseFileManagementTestService baseFileManagementTestService;
@Resource
private ApiCommonService apiCommonService;
@Resource
private ApiFileResourceMapper apiFileResourceMapper;
public static <T> T parseObjectFromMvcResult(MvcResult mvcResult, Class<T> parseClass) {
try {
@ -210,13 +211,12 @@ public class ApiTestCaseControllerTests extends BaseTest {
}
private static MockMultipartFile getMockMultipartFile() {
MockMultipartFile file = new MockMultipartFile(
return new MockMultipartFile(
"file",
IDGenerator.nextStr() + "_file_upload.JPG",
MediaType.APPLICATION_OCTET_STREAM_VALUE,
"Hello, World!".getBytes()
);
return file;
}
/**
@ -232,7 +232,7 @@ public class ApiTestCaseControllerTests extends BaseTest {
List<ApiFileResource> apiFileResources = apiFileResourceService.getByResourceId(id);
Assertions.assertEquals(apiFileResources.size(), fileIds.size());
String apiDebugDir = DefaultRepositoryDir.getApiDebugDir(DEFAULT_PROJECT_ID, id);
String apiDebugDir = DefaultRepositoryDir.getApiCaseDir(DEFAULT_PROJECT_ID, id);
FileRequest fileRequest = new FileRequest();
if (fileIds.size() > 0) {
for (ApiFileResource apiFileResource : apiFileResources) {
@ -358,7 +358,6 @@ public class ApiTestCaseControllerTests extends BaseTest {
request.setApiDefinitionId("apiDefinitionId");
request.setName("test123");
this.requestPost(ADD, request).andExpect(ERROR_REQUEST_MATCHER);
// @@校验日志
checkLog(apiTestCase.getId(), OperationLogType.ADD);
// @@异常参数校验
@ -372,6 +371,7 @@ public class ApiTestCaseControllerTests extends BaseTest {
/**
* 测试关联的文件更新
*
* @throws Exception
*/
public void testHandleFileAssociationUpgrade() throws Exception {
@ -580,6 +580,31 @@ public class ApiTestCaseControllerTests extends BaseTest {
requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE, UPDATE, request);
}
@Test
@Order(9)
public void testTransfer() throws Exception {
this.requestGetWithOk("/api/case/transfer/options/" + "/" + DEFAULT_PROJECT_ID);
ApiTransferRequest apiTransferRequest = new ApiTransferRequest();
apiTransferRequest.setSourceId(apiTestCase.getId());
apiTransferRequest.setProjectId(DEFAULT_PROJECT_ID);
apiTransferRequest.setModuleId("root");
apiTransferRequest.setLocal(true);
String uploadFileId = doUploadTempFile(getMockMultipartFile());
apiTransferRequest.setFileId(uploadFileId);
this.requestPost("/api/case/transfer", apiTransferRequest).andExpect(status().isOk());
//文件不存在
apiTransferRequest.setFileId("111");
this.requestPost("/api/case/transfer", apiTransferRequest).andExpect(status().is5xxServerError());
//文件已经上传
ApiFileResourceExample apiFileResourceExample = new ApiFileResourceExample();
apiFileResourceExample.createCriteria().andResourceIdEqualTo(apiTestCase.getId());
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
apiTransferRequest.setFileId(apiFileResources.get(0).getFileId());
this.requestPost("/api/case/transfer", apiTransferRequest).andExpect(status().isOk());
}
@Test
@Order(9)
public void testPos() throws Exception {