fix(用例管理): 修复复制用例附件无法复制成功问题

--bug=1036216 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001036216
This commit is contained in:
guoyuqi 2024-02-29 10:37:17 +08:00 committed by 刘瑞斌
parent 0a8d374fef
commit 03a30337f2
3 changed files with 56 additions and 2 deletions

View File

@ -122,7 +122,6 @@ public class FunctionalCaseAttachmentService {
caseAttachment.setCaseId(caseId);
caseAttachment.setFileId(fileId);
caseAttachment.setFileName(fileName);
caseAttachment.setFileSource(CaseFileSourceType.ATTACHMENT.toString());
caseAttachment.setSize(fileSize);
caseAttachment.setLocal(isLocal);
caseAttachment.setCreateUser(userId);

View File

@ -23,8 +23,12 @@ import io.metersphere.project.service.ProjectTemplateService;
import io.metersphere.provider.BaseCaseProvider;
import io.metersphere.sdk.constants.*;
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.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.CustomFieldOption;
import io.metersphere.system.domain.OperationHistoryExample;
@ -205,15 +209,45 @@ public class FunctionalCaseService {
FunctionalCaseAttachmentExample functionalCaseAttachmentExample = new FunctionalCaseAttachmentExample();
functionalCaseAttachmentExample.createCriteria().andCaseIdEqualTo(caseId).andFileIdIn(attachmentFileIds);
List<FunctionalCaseAttachment> functionalCaseAttachments = functionalCaseAttachmentMapper.selectByExample(functionalCaseAttachmentExample);
functionalCaseAttachmentExample = new FunctionalCaseAttachmentExample();
functionalCaseAttachmentExample.createCriteria().andCaseIdNotEqualTo(caseId).andFileIdIn(attachmentFileIds);
List<FunctionalCaseAttachment> oldFiles = functionalCaseAttachmentMapper.selectByExample(functionalCaseAttachmentExample);
Map<String, List<FunctionalCaseAttachment>> oldFileMap = oldFiles.stream().collect(Collectors.groupingBy(FunctionalCaseAttachment::getFileId));
List<String> attachmentFileIdInDBs = functionalCaseAttachments.stream().map(FunctionalCaseAttachment::getFileId).toList();
List<String> saveAttachmentFileIds = attachmentFileIds.stream().filter(t -> !attachmentFileIdInDBs.contains(t)).toList();
FileRepository defaultRepository = FileCenter.getDefaultRepository();
for (String saveAttachmentFileId : saveAttachmentFileIds) {
FunctionalCaseAttachmentDTO functionalCaseAttachmentDTO = attachmentDTOMap.get(saveAttachmentFileId);
FunctionalCaseAttachment caseAttachment = functionalCaseAttachmentService.creatAttachment(saveAttachmentFileId, functionalCaseAttachmentDTO.getFileName(), functionalCaseAttachmentDTO.getSize(), caseId, Boolean.TRUE, userId);
FunctionalCaseAttachment caseAttachment = functionalCaseAttachmentService.creatAttachment(saveAttachmentFileId, functionalCaseAttachmentDTO.getFileName(), functionalCaseAttachmentDTO.getSize(), caseId, functionalCaseAttachmentDTO.getLocal(), userId);
if(functionalCaseAttachmentDTO.getLocal()) {
caseAttachment.setFileSource(CaseFileSourceType.ATTACHMENT.toString());
copyFile(request, caseId, saveAttachmentFileId, oldFileMap, functionalCaseAttachmentDTO, defaultRepository);
}
functionalCaseAttachmentMapper.insertSelective(caseAttachment);
}
}
private static void copyFile(FunctionalCaseAddRequest request, String caseId, String saveAttachmentFileId, Map<String, List<FunctionalCaseAttachment>> oldFileMap, FunctionalCaseAttachmentDTO functionalCaseAttachmentDTO, FileRepository defaultRepository) {
List<FunctionalCaseAttachment> oldFunctionalCaseAttachments = oldFileMap.get(saveAttachmentFileId);
if (CollectionUtils.isNotEmpty(oldFunctionalCaseAttachments)) {
FunctionalCaseAttachment functionalCaseAttachment = oldFunctionalCaseAttachments.get(0);
// 复制文件
FileCopyRequest fileCopyRequest = new FileCopyRequest();
fileCopyRequest.setCopyFolder(DefaultRepositoryDir.getFunctionalCaseDir(request.getProjectId(), functionalCaseAttachment.getCaseId()) + "/" + saveAttachmentFileId);
fileCopyRequest.setCopyfileName(functionalCaseAttachmentDTO.getFileName());
fileCopyRequest.setFileName(functionalCaseAttachmentDTO.getFileName());
fileCopyRequest.setFolder(DefaultRepositoryDir.getFunctionalCaseDir(request.getProjectId(), caseId) + "/" + saveAttachmentFileId);
// 将文件从上一个用例复制到当前用例资源目录
try {
defaultRepository.copyFile(fileCopyRequest);
} catch (Exception e) {
LogUtils.error("复制文件失败:{}",e);
}
}
}
/**
* 添加用例评审和用例关联关系

View File

@ -199,6 +199,27 @@ public class FunctionalCaseControllerTests extends BaseTest {
functionalCaseAttachmentExample.createCriteria().andCaseIdEqualTo(functionalCase.getId()).andFileIdEqualTo("12345677");
List<FunctionalCaseAttachment> functionalCaseAttachments = functionalCaseAttachmentMapper.selectByExample(functionalCaseAttachmentExample);
Assertions.assertEquals(1, functionalCaseAttachments.size());
functionalCaseAttachmentDTO.setId(newFileId);
attachmentDTOS = new ArrayList<>();
attachmentDTOS.add(functionalCaseAttachmentDTO);
request = creatFunctionalCase();
request.setAttachments(attachmentDTOS);
paramMap = new LinkedMultiValueMap<>();
paramMap.add("request", JSON.toJSONString(request));
paramMap.add("files", new LinkedMultiValueMap<>());
functionalCaseMvcResult = this.requestMultipartWithOkAndReturn(FUNCTIONAL_CASE_ADD_URL, paramMap);
returnData = functionalCaseMvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
functionalCase = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), FunctionalCase.class);
functionalCaseAttachmentExample = new FunctionalCaseAttachmentExample();
functionalCaseAttachmentExample.createCriteria().andCaseIdEqualTo(functionalCase.getId()).andFileIdEqualTo(newFileId);
functionalCaseAttachments = functionalCaseAttachmentMapper.selectByExample(functionalCaseAttachmentExample);
Assertions.assertEquals(1, functionalCaseAttachments.size());
}
public String uploadTemp(MultipartFile file) {