feat(接口测试): 接口管理模块复制接口定义同时复制文件

This commit is contained in:
lan-yonghui 2023-12-08 16:08:26 +08:00 committed by jianxing
parent c6723155a5
commit 07095ecd5a
3 changed files with 51 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -242,4 +243,37 @@ public class ApiFileResourceService {
}
return fileId;
}
public void copyFileByResourceId(String sourceId, String sourceFolder, String targetId, String targetFolder) {
List<ApiFileResource> files = getByResourceId(sourceId);
if(!files.isEmpty()){
FileRepository defaultRepository = FileCenter.getDefaultRepository();
List<ApiFileResource> apiFileResources = new ArrayList<>();
files.forEach(item->{
try {
// 按ID建文件夹避免文件名重复
FileCopyRequest fileCopyRequest = new FileCopyRequest();
fileCopyRequest.setCopyFolder(sourceFolder + "/" + item.getFileId());
fileCopyRequest.setCopyfileName(item.getFileName());
fileCopyRequest.setFileName(item.getFileName());
fileCopyRequest.setFolder(targetFolder + "/" + item.getFileId());
// 将文件从临时目录复制到资源目录
defaultRepository.copyFile(fileCopyRequest);
ApiFileResource apiFileResource = new ApiFileResource();
apiFileResource.setFileId(item.getFileId());
apiFileResource.setResourceId(targetId);
apiFileResource.setResourceType(item.getResourceType());
apiFileResource.setProjectId(item.getProjectId());
apiFileResource.setCreateTime(System.currentTimeMillis());
apiFileResource.setFileName(item.getFileName());
apiFileResources.add(apiFileResource);
} catch (Exception e) {
LogUtils.error(e);
throw new MSException(Translator.get("file_copy_fail"));
}
});
apiFileResourceMapper.batchInsert(apiFileResources);
}
}
}

View File

@ -249,7 +249,10 @@ public class ApiDefinitionService {
apiDefinitionBlob.setResponse(copyApiDefinitionBlob.getResponse());
apiDefinitionBlobMapper.insertSelective(apiDefinitionBlob);
}
// TODO 复制的时候文件需要复制一份 仅复制接口内容 不包含用例mock信息
String sourceDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinition.getProjectId(), request.getId());
String targetDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinition.getProjectId(), apiDefinition.getId());
apiFileResourceService.copyFileByResourceId(request.getId(), sourceDir, apiDefinition.getId(), targetDir);
return apiDefinition;
}
@ -671,7 +674,7 @@ public class ApiDefinitionService {
public ApiDefinitionDocDTO getDocInfo(ApiDefinitionDocRequest request, String userId) {
ApiDefinitionDocDTO apiDefinitionDocDTO = new ApiDefinitionDocDTO();
apiDefinitionDocDTO.setType(request.getType());
// @@TODO 下载所有/一个模块接口文档时不做分页数据量大的时候会不会有性能问题
// 下载所有/一个模块接口文档时不做分页数据量大的时候会不会有性能问题单独做接口
if (ApiDefinitionDocType.ALL.name().equals(request.getType()) || ApiDefinitionDocType.MODULE.name().equals(request.getType())) {
List<ApiDefinitionDTO> list = extApiDefinitionMapper.listDoc(request);
if (!list.isEmpty()) {

View File

@ -489,15 +489,26 @@ public class ApiDefinitionControllerTests extends BaseTest {
@Order(5)
public void copy() throws Exception {
LogUtils.info("copy api test");
ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001");
ApiDefinitionCopyRequest request = new ApiDefinitionCopyRequest();
request.setId(apiDefinition.getId());
MvcResult mvcResult = this.requestPostWithOkAndReturn(COPY, request);
ApiDefinition resultData = getResultData(mvcResult, ApiDefinition.class);
// @数据验证
List<ApiFileResource> sourceFiles = apiFileResourceService.getByResourceId(apiDefinition.getId());
List<ApiFileResource> copyFiles = apiFileResourceService.getByResourceId(resultData.getId());
if(!sourceFiles.isEmpty() && !copyFiles.isEmpty()){
Assertions.assertEquals(sourceFiles.size(), copyFiles.size());
}
Assertions.assertTrue(resultData.getName().contains("copy_"));
// @@校验日志
checkLog(resultData.getId(), OperationLogType.UPDATE);
request.setId("1001");
MvcResult mvcResultCopy = this.requestPostWithOkAndReturn(COPY, request);
ApiDefinition resultDataCopy = getResultData(mvcResultCopy, ApiDefinition.class);
// @数据验证
Assertions.assertTrue(resultDataCopy.getName().contains("copy_"));
request.setId("121");
assertErrorCode(this.requestPost(COPY, request), API_DEFINITION_NOT_EXIST);
// @@校验权限