refactor(接口测试): 优化文件转存接口

This commit is contained in:
AgAngle 2024-04-25 16:43:17 +08:00 committed by jianxing
parent c50b7ead9c
commit e2c4dfa03d
17 changed files with 72 additions and 29 deletions

View File

@ -14,4 +14,8 @@ public class ApiTransferRequest extends ApiFileRequest {
@Schema(description = "转存的模块id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{functional_case.module_id.not_blank}")
private String moduleId;
@Schema(description = "原始文件名", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank
private String originalName;
}

View File

@ -105,7 +105,13 @@ public class ApiCommonService {
private void setLinkFileInfo(String resourceId, List<ApiFile> apiFiles) {
List<ApiFile> linkFiles = apiFiles.stream()
.filter(file -> !file.getLocal() && !file.getDelete())
.filter(file -> {
if (file.getLocal()) {
// 本地文件设置文件别名方便前端展示
file.setFileAlias(file.getFileName());
}
return !file.getLocal() && !file.getDelete();
})
.toList();
List<String> linkFileIds = linkFiles.stream()
.map(ApiFile::getFileId)
@ -116,20 +122,25 @@ public class ApiCommonService {
return;
}
Map<String, String> fileNameMap = fileMetadataService.selectByList(linkFileIds)
Map<String, FileMetadata> fileNameMap = fileMetadataService.selectByList(linkFileIds)
.stream()
.collect(Collectors.toMap(FileMetadata::getId, FileMetadata::getName));
.collect(Collectors.toMap(FileMetadata::getId, Function.identity()));
for (ApiFile linkFile : linkFiles) {
String fileName = fileNameMap.get(linkFile.getFileId());
if (StringUtils.isBlank(fileName)) {
// fileName 为空则文件被删除设置为已删除并且设置文件名
FileMetadata fileMetadata = fileNameMap.get(linkFile.getFileId());
if (fileMetadata == null) {
// fileMetadata null 则文件被删除设置为已删除并且设置文件名
linkFile.setDelete(true);
List<FileAssociation> fileAssociations = fileAssociationService.getByFileIdAndSourceId(resourceId, linkFile.getFileId());
if (CollectionUtils.isNotEmpty(fileAssociations)) {
linkFile.setFileAlias(fileAssociations.get(0).getDeletedFileName());
}
} else {
String fileName = fileMetadata.getName() + fileMetadata.getType();
if (StringUtils.isNotBlank(fileMetadata.getType())) {
// 前端展示别名加后缀
fileName += "." + fileMetadata.getType();
}
linkFile.setFileAlias(fileName);
}
}

View File

@ -422,7 +422,7 @@ public class ApiFileResourceService {
throw new MSException("download file error!");
}
try {
fileId = fileMetadataService.transferFile(request.getFileName(), request.getProjectId(), request.getModuleId(), currentUser, bytes);
fileId = fileMetadataService.transferFile(request.getFileName(), request.getOriginalName(), request.getProjectId(), request.getModuleId(), currentUser, bytes);
} catch (Exception e) {
throw new MSException(Translator.get("file.transfer.failed"));
}

View File

@ -709,11 +709,13 @@ public class ApiDebugControllerTests extends BaseTest {
request.setLocal(true);
uploadFileId = doUploadTempFile(getMockMultipartFile("test-debug-file.txt"));
request.setFileId(uploadFileId);
request.setFileName("test-debug-file.txt");
request.setFileName("test-debug-file");
request.setOriginalName("test-debug-file.txt");
this.requestPost(TRANSFER, request).andExpect(status().isOk());
//文件不存在
request.setFileId("111");
request.setFileName("test-debug-file.txt");
request.setFileName("test-debug-file");
request.setOriginalName("test-debug-file.txt");
this.requestPost(TRANSFER, request).andExpect(status().is5xxServerError());
//文件已经上传
ApiDebugAddRequest addRequest = new ApiDebugAddRequest();
@ -739,7 +741,8 @@ public class ApiDebugControllerTests extends BaseTest {
request.setSourceId(resultData.getId());
request.setProjectId(DEFAULT_PROJECT_ID);
request.setModuleId("root");
request.setFileName("test-debug-file1.txt");
request.setFileName("test-debug-file1");
request.setOriginalName("test-debug-file1.txt");
request.setFileId(apiFileResources.get(0).getFileId());
this.requestPost(TRANSFER, request).andExpect(status().isOk());
}

View File

@ -244,7 +244,8 @@ public class ApiDefinitionControllerTests extends BaseTest {
apiTransferRequest.setLocal(true);
String uploadFileId = doUploadTempFile(getMockMultipartFile("api-file_upload.JPG"));
apiTransferRequest.setFileId(uploadFileId);
apiTransferRequest.setFileName("api-file_upload.JPG");
apiTransferRequest.setFileName(StringUtils.EMPTY);
apiTransferRequest.setOriginalName("api-file_upload.JPG");
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
//文件不存在
apiTransferRequest.setFileId("111");
@ -255,7 +256,8 @@ public class ApiDefinitionControllerTests extends BaseTest {
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
apiTransferRequest.setFileId(apiFileResources.get(0).getFileId());
apiTransferRequest.setFileName("test-file_upload.JPG");
apiTransferRequest.setFileName("test-file_upload");
apiTransferRequest.setOriginalName("test-file_upload.JPG");
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
// 再插入一条数据便于修改时重名校验

View File

@ -815,7 +815,8 @@ public class ApiScenarioControllerTests extends BaseTest {
apiTransferRequest.setProjectId(DEFAULT_PROJECT_ID);
apiTransferRequest.setModuleId("root");
apiTransferRequest.setLocal(true);
apiTransferRequest.setFileName("test-scenario-file.txt");
apiTransferRequest.setFileName("test-scenario-file");
apiTransferRequest.setOriginalName("test-scenario-file.txt");
String uploadFileId = doUploadTempFile(getMockMultipartFile());
apiTransferRequest.setFileId(uploadFileId);
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
@ -828,7 +829,8 @@ public class ApiScenarioControllerTests extends BaseTest {
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
apiTransferRequest.setFileId(apiFileResources.get(0).getFileId());
apiTransferRequest.setFileName("test-scenario-file-1.txt");
apiTransferRequest.setFileName("test-scenario-file-1");
apiTransferRequest.setOriginalName("test-scenario-file-1.txt");
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
}

View File

@ -728,7 +728,8 @@ public class ApiTestCaseControllerTests extends BaseTest {
apiTransferRequest.setLocal(true);
String uploadFileId = doUploadTempFile(getMockMultipartFile());
apiTransferRequest.setFileId(uploadFileId);
apiTransferRequest.setFileName("test-api-test-case.txt");
apiTransferRequest.setFileName("test-api-test-case");
apiTransferRequest.setOriginalName("test-api-test-case.txt");
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
//文件不存在
apiTransferRequest.setFileId("111");
@ -739,7 +740,8 @@ public class ApiTestCaseControllerTests extends BaseTest {
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
apiTransferRequest.setFileId(apiFileResources.get(0).getFileId());
apiTransferRequest.setFileName("test-api-test-case-1.txt");
apiTransferRequest.setFileName("test-api-test-case-1");
apiTransferRequest.setOriginalName("test-api-test-case-1.txt");
this.requestPost("transfer", apiTransferRequest).andExpect(status().isOk());
}

View File

@ -12,4 +12,8 @@ public class BugFileTransferRequest extends BugFileSourceRequest{
@Schema(description = "转存的模块id",requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{functional_case.module_id.not_blank}")
private String moduleId;
@Schema(description = "文件别名", requiredMode = Schema.RequiredMode.REQUIRED)
private String fileName;
}

View File

@ -213,7 +213,7 @@ public class BugAttachmentService {
byte[] bytes = getLocalFileBytes(attachment, request.getProjectId(), request.getBugId());
String fileId;
try {
FileAssociationDTO association = new FileAssociationDTO(attachment.getFileName(), bytes, attachment.getBugId(),
FileAssociationDTO association = new FileAssociationDTO(request.getFileName(), attachment.getFileName(), bytes, attachment.getBugId(),
FileAssociationSourceUtil.SOURCE_TYPE_BUG, createFileLogRecord(currentUser, request.getProjectId()));
association.setModuleId(request.getModuleId());
fileId = fileAssociationService.transferAndAssociation(association);

View File

@ -28,6 +28,7 @@ import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -143,6 +144,7 @@ public class BugAttachmentControllerTests extends BaseTest {
request.setModuleId("root");
request.setAssociated(false);
request.setFileId("not-exist-file-id");
request.setFileName(UUID.randomUUID().toString());
this.requestPost(BUG_ATTACHMENT_TRANSFER, request).andExpect(status().is5xxServerError());
List<BugFileDTO> files = getBugFiles("default-attachment-bug-id");
files.stream().filter(BugFileDTO::getLocal).forEach(file -> {

View File

@ -135,7 +135,7 @@ public class FunctionalCaseAttachmentController {
String fileId = null;
try {
FileAssociationDTO fileAssociationDTO = new FileAssociationDTO(attachment.getFileName(), fileByte, attachment.getCaseId(), FileAssociationSourceUtil.SOURCE_TYPE_FUNCTIONAL_CASE, fileLogRecord);
FileAssociationDTO fileAssociationDTO = new FileAssociationDTO(request.getFileName(), attachment.getFileName(), fileByte, attachment.getCaseId(), FileAssociationSourceUtil.SOURCE_TYPE_FUNCTIONAL_CASE, fileLogRecord);
fileAssociationDTO.setModuleId(request.getModuleId());
fileId = fileAssociationService.transferAndAssociation(fileAssociationDTO);
functionalCaseAttachmentService.deleteCaseAttachment(Arrays.asList(request.getFileId()), request.getCaseId(), request.getProjectId());

View File

@ -14,4 +14,8 @@ public class AttachmentTransferRequest extends FunctionalCaseFileRequest{
@Schema(description = "转存的模块id",requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{functional_case.module_id.not_blank}")
private String moduleId;
@Schema(description = "文件别名", requiredMode = Schema.RequiredMode.REQUIRED)
private String fileName;
}

View File

@ -185,6 +185,7 @@ public class FunctionalCaseAttachmentControllerTests extends BaseTest {
request.setProjectId(DEFAULT_PROJECT_ID);
request.setFileId(FILE_ID);
request.setCaseId("TEST_FUNCTIONAL_CASE_ATTACHMENT_ID");
request.setFileName(UUID.randomUUID().toString());
request.setModuleId("root");
this.requestPost(ATTACHMENT_TRANSFER_URL, request);
request.setFileId("TEST_ATTACHMENT_FILE_ID");

View File

@ -16,6 +16,9 @@ public class FileAssociationDTO {
@NotBlank
private String fileName;
@NotBlank
private String originalName;
@NotBlank
private String sourceId;
@ -29,8 +32,9 @@ public class FileAssociationDTO {
private FileLogRecord fileLogRecord;
public FileAssociationDTO(String fileName, byte[] fileBytes, String sourceId, String sourceType, FileLogRecord fileLogRecord) {
public FileAssociationDTO(String fileName, String originalName, byte[] fileBytes, String sourceId, String sourceType, FileLogRecord fileLogRecord) {
this.fileName = fileName;
this.originalName = originalName;
this.sourceId = sourceId;
this.sourceType = sourceType;
this.fileBytes = fileBytes;

View File

@ -321,6 +321,7 @@ public class FileAssociationService {
this.validateSourceName(source);
String fileId = fileMetadataService.transferFile(
fileAssociationDTO.getFileName(),
fileAssociationDTO.getOriginalName(),
fileAssociationDTO.getFileLogRecord().getProjectId(),
fileAssociationDTO.getModuleId(),
fileAssociationDTO.getFileLogRecord().getOperator(),

View File

@ -203,22 +203,25 @@ public class FileMetadataService {
return fileMetadata.getId();
}
/**
* 文件转存
*
* @param fileName 文件名
* @param originFileName 原始文件名
* @param projectId 项目ID
* @param operator 操作人
* @param fileBytes 文件字节
* @return
* @throws Exception
*/
public String transferFile(String fileName, String projectId, String moduleId, String operator, byte[] fileBytes) throws Exception {
if (StringUtils.isBlank(fileName)) {
public String transferFile(String fileName, String originFileName, String projectId, String moduleId, String operator, byte[] fileBytes) throws Exception {
if (StringUtils.isBlank(originFileName)) {
throw new MSException(Translator.get("file.name.cannot.be.empty"));
}
FileMetadata fileMetadata = this.genFileMetadata(fileName, StorageType.MINIO.name(), fileBytes.length, false, projectId, moduleId, operator);
FileMetadata fileMetadata = this.genFileMetadata(originFileName, StorageType.MINIO.name(), fileBytes.length, false, projectId, moduleId, operator);
if (StringUtils.isNotBlank(fileName)) {
fileMetadata.setName(fileName);
}
FileRequest uploadFileRequest = new FileRequest();
uploadFileRequest.setFileName(fileMetadata.getId());
uploadFileRequest.setFolder(this.generateMinIOFilePath(projectId));

View File

@ -1770,28 +1770,28 @@ public class FileManagementControllerTests extends BaseTest {
.build();
//关联正常文件
String filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/file_upload.JPG")).getPath();
String fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile.jpg", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
String fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile", "testTransferFile.jpg", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
FileMetadataExample example = new FileMetadataExample();
example.createCriteria().andIdEqualTo(fileID).andNameEqualTo("testTransferFile").andTypeEqualTo("JPG");
Assertions.assertEquals(fileMetadataMapper.countByExample(example), 1);
//重复转存检查是否报错
boolean error = false;
try {
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile.jpg", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile", "testTransferFile.jpg", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
} catch (Exception e) {
error = true;
}
Assertions.assertTrue(error);
//测试没有后缀的文件名
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransfer", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransfer", "testTransfer", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
example.clear();
example.createCriteria().andIdEqualTo(fileID).andNameEqualTo("testTransfer");
Assertions.assertEquals(fileMetadataMapper.countByExample(example), 1);
//资源不存在
error = false;
try {
fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile.jpg", TempFileUtils.getFile(filePath), IDGenerator.nextStr(), FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile", "testTransferFile.jpg", TempFileUtils.getFile(filePath), IDGenerator.nextStr(), FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
} catch (Exception e) {
error = true;
}
@ -1799,7 +1799,7 @@ public class FileManagementControllerTests extends BaseTest {
//文件名称不合法
error = false;
try {
fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransfer/File.jpg", TempFileUtils.getFile(filePath), IDGenerator.nextStr(), FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransfer/File", "testTransfer/File.jpg", TempFileUtils.getFile(filePath), IDGenerator.nextStr(), FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
} catch (Exception e) {
error = true;
}
@ -1808,7 +1808,7 @@ public class FileManagementControllerTests extends BaseTest {
//文件名称非法
error = false;
try {
fileMetadataService.transferFile("", null, null, null, null);
fileMetadataService.transferFile(StringUtils.EMPTY, StringUtils.EMPTY, null, null, null, null);
} catch (Exception e) {
error = true;
}