diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java index b7d8fab13b..19475a0f80 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java @@ -495,7 +495,7 @@ public class BugService { } if (CollectionUtils.isNotEmpty(request.getUnLinkRefIds())) { // 取消关联的附件, FILE_ASSOCIATION表 - fileAssociationService.deleteBySourceId(request.getUnLinkRefIds(), createFileLogRecord(fileLogUrl, currentUser, request.getProjectId())); + fileAssociationService.deleteByIds(request.getUnLinkRefIds(), createFileLogRecord(fileLogUrl, currentUser, request.getProjectId())); } // 新本地上传的附件 @@ -526,7 +526,7 @@ public class BugService { } // 新关联的附件 if (CollectionUtils.isNotEmpty(request.getLinkFileIds())) { - fileAssociationService.association(request.getId(), FileAssociationSourceUtil.SOURCE_TYPE_BUG, request.getLinkFileIds(), false, + fileAssociationService.association(request.getId(), FileAssociationSourceUtil.SOURCE_TYPE_BUG, request.getLinkFileIds(), createFileLogRecord(fileLogUrl, currentUser, request.getProjectId())); } } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseAttachmentService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseAttachmentService.java index ea084b8fa8..44ec5293df 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseAttachmentService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseAttachmentService.java @@ -197,7 +197,7 @@ public class FunctionalCaseAttachmentService { * @param projectId projectId */ public void association(List relateFileMetaIds, String caseId, String userId, String logUrl, String projectId) { - fileAssociationService.association(caseId, FileAssociationSourceUtil.SOURCE_TYPE_FUNCTIONAL_CASE, relateFileMetaIds, false, createFileLogRecord(logUrl, userId, projectId)); + fileAssociationService.association(caseId, FileAssociationSourceUtil.SOURCE_TYPE_FUNCTIONAL_CASE, relateFileMetaIds, createFileLogRecord(logUrl, userId, projectId)); } private FileLogRecord createFileLogRecord(String logUrl, String operator, String projectId) { @@ -220,7 +220,7 @@ public class FunctionalCaseAttachmentService { * @param projectId projectId */ public void unAssociation(List unLinkFilesIds, String logUrl, String userId, String projectId) { - fileAssociationService.deleteBySourceId(unLinkFilesIds, createFileLogRecord(logUrl, userId, projectId)); + fileAssociationService.deleteByIds(unLinkFilesIds, createFileLogRecord(logUrl, userId, projectId)); } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/FileAssociationController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/FileAssociationController.java index d402443dbe..7a5d2f3af5 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/FileAssociationController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/FileAssociationController.java @@ -59,6 +59,6 @@ public class FileAssociationController { .projectId(request.getProjectId()) .build(); - return fileAssociationService.deleteBySourceId(request.getAssociationIds(),fileLogRecord); + return fileAssociationService.deleteByIds(request.getAssociationIds(), fileLogRecord); } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/FileManagementQuery.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/FileManagementQuery.java index 75d1116041..14e415e39c 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/FileManagementQuery.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/FileManagementQuery.java @@ -3,10 +3,12 @@ package io.metersphere.project.dto.filemanagement; import io.metersphere.project.dto.filemanagement.request.FileBatchProcessRequest; import io.metersphere.project.dto.filemanagement.request.FileMetadataTableRequest; import io.metersphere.sdk.constants.StorageType; +import io.metersphere.sdk.util.JSON; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.commons.collections4.MapUtils; +import java.util.ArrayList; import java.util.List; @Data @@ -18,6 +20,7 @@ public class FileManagementQuery { public String fileType; public String operator; public String storage = StorageType.MINIO.name(); + public List hiddenIds = new ArrayList<>(); public FileManagementQuery(FileBatchProcessRequest batchProcessDTO) { this.projectId = batchProcessDTO.getProjectId(); @@ -46,6 +49,10 @@ public class FileManagementQuery { if (batchProcessDTO.getCombine().get("storage") != null) { this.storage = batchProcessDTO.getCombine().get("storage").toString(); } + if (batchProcessDTO.getCombine().get("hiddenIds") != null) { + this.hiddenIds = JSON.parseArray( + JSON.toJSONString(batchProcessDTO.getCombine().get("hiddenIds"))); + } } } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFileMetadataMapper.xml b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFileMetadataMapper.xml index 723f2ece75..c8d7b9073a 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFileMetadataMapper.xml +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFileMetadataMapper.xml @@ -77,6 +77,14 @@ AND f.type = #{fileType} + + AND f.ref_id NOT IN + (SELECT ref_id FROM file_metadata WHERE id IN + + #{item} + + ) + AND f.create_user = #{operator} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileAssociationService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileAssociationService.java index cc23280b3a..b1c979c7c0 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileAssociationService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileAssociationService.java @@ -18,15 +18,15 @@ import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; 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.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Service @@ -131,18 +131,14 @@ public class FileAssociationService { * @param fileLogRecord 日志记录相关。包含操作人、日志所属模块、触发日志记录的请求路径和请求方法 * @return 本次涉及到关联的关联表ID */ - public List association(String sourceId, String sourceType, List fileIds, boolean isOverWrite, @Validated FileLogRecord fileLogRecord) { + public List association(String sourceId, String sourceType, List fileIds, @Validated FileLogRecord fileLogRecord) { if (CollectionUtils.isEmpty(fileIds)) { throw new MSException(Translator.get("file.not.exist")); } FileAssociationSource source = extFileAssociationMapper.selectNameBySourceTableAndId(FileAssociationSourceUtil.getQuerySql(sourceType),sourceId); this.validateSourceName(source); - List fileMetadataList = fileMetadataService.selectByList(fileIds); - if (fileMetadataList.size() != fileIds.size()) { - throw new MSException(Translator.get("file.some.not.exist")); - } //校验文件是否已经关联 FileAssociationExample example = new FileAssociationExample(); @@ -150,24 +146,17 @@ public class FileAssociationService { List associationdList = fileAssociationMapper.selectByExample(example); Map refIdFileAssociationMap = associationdList.stream().collect(Collectors.toMap(FileAssociation::getFileRefId, item -> item)); - Map updateAssociationMap = new HashMap<>(); List addFileList = new ArrayList<>(); for (FileMetadata fileMetadata : fileMetadataList) { FileAssociation fileAssociation = refIdFileAssociationMap.get(fileMetadata.getRefId()); if(fileAssociation == null){ addFileList.add(fileMetadata); - }else if(!StringUtils.equals(fileAssociation.getFileId(),fileMetadata.getId())){ - updateAssociationMap.put(fileAssociation,fileMetadata); } } - List associationId = new ArrayList<>(); - associationId.addAll(this.createFileAssociation(addFileList,sourceId,source.getSourceName(),sourceType,fileLogRecord)); - if(isOverWrite){ - associationId.addAll(this.updateFileAssociation(updateAssociationMap,source.getSourceName(),fileLogRecord)); - } - return associationId; + return this.createFileAssociation(addFileList, sourceId, source.getSourceName(), sourceType, fileLogRecord); } - private Collection createFileAssociation(List addFileList, String sourceId, String sourceName,String sourceType, @Validated FileLogRecord logRecord) { + + private List createFileAssociation(List addFileList, String sourceId, String sourceName, String sourceType, @Validated FileLogRecord logRecord) { FileAssociationSourceUtil.validate(sourceType); if(CollectionUtils.isNotEmpty(addFileList)){ List createFile = new ArrayList<>(); @@ -193,28 +182,6 @@ public class FileAssociationService { return new ArrayList<>(); } } - private Collection updateFileAssociation(Map updateAssociationMap,String sourceName,@Validated FileLogRecord logRecord) { - if(MapUtils.isNotEmpty(updateAssociationMap)){ - long operatorTime = System.currentTimeMillis(); - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - FileAssociationMapper batchUpdateMapper = sqlSession.getMapper(FileAssociationMapper.class); - for (Map.Entry entry : updateAssociationMap.entrySet()) { - FileAssociation association = entry.getKey(); - FileMetadata metadata = entry.getValue(); - association.setFileId(metadata.getId()); - association.setFileVersion(metadata.getFileVersion()); - association.setUpdateUser(logRecord.getOperator()); - association.setUpdateTime(operatorTime); - batchUpdateMapper.updateByPrimaryKeySelective(association); - } - sqlSession.flushStatements(); - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - fileAssociationLogService.saveBatchUpdateLog(sourceName,updateAssociationMap.values(),logRecord); - return updateAssociationMap.keySet().stream().map(FileAssociation::getId).collect(Collectors.toList()); - }else { - return new ArrayList<>(); - } - } /** * 将资源关联的文件更新到最新版本 @@ -257,17 +224,37 @@ public class FileAssociationService { * @param logRecord 日志记录相关 * @return */ - public int deleteBySourceId(List idList, @Validated FileLogRecord logRecord){ + public int deleteByIds(List idList, @Validated FileLogRecord logRecord) { if(CollectionUtils.isEmpty(idList)){ return 0; } FileAssociationExample example = new FileAssociationExample(); example.createCriteria().andIdIn(idList); + return this.deleteAndSelectExample(example, logRecord); + } + + /** + * 取消关联 + * + * @param sourceIds 取消关联的资源id + * @param logRecord 日志记录相关 + * @return + */ + public int deleteBySourceIds(List sourceIds, @Validated FileLogRecord logRecord) { + if (CollectionUtils.isEmpty(sourceIds)) { + return 0; + } + FileAssociationExample example = new FileAssociationExample(); + example.createCriteria().andSourceIdIn(sourceIds); + return this.deleteAndSelectExample(example, logRecord); + } + + private int deleteAndSelectExample(FileAssociationExample example, FileLogRecord logRecord) { List fileAssociationList = fileAssociationMapper.selectByExample(example); - Map> sourceToFileNameMap = this.genSourceNameFileNameMap(fileAssociationList); + Map> sourceToFileNameMap = this.genSourceNameFileNameMap(fileAssociationList); int deleteCount = fileAssociationMapper.deleteByExample(example); - if(MapUtils.isNotEmpty(sourceToFileNameMap)){ - fileAssociationLogService.saveDeleteLog(sourceToFileNameMap,logRecord); + if (MapUtils.isNotEmpty(sourceToFileNameMap)) { + fileAssociationLogService.saveDeleteLog(sourceToFileNameMap, logRecord); } return deleteCount; } @@ -317,7 +304,7 @@ public class FileAssociationService { String fileId = fileMetadataService.transferFile(fileName, fileLogRecord.getProjectId(), fileLogRecord.getOperator(),fileBytes); List accociationList = new ArrayList<>(); accociationList.add(fileId); - this.association(sourceId, sourceType, accociationList, false, fileLogRecord); + this.association(sourceId, sourceType, accociationList, fileLogRecord); fileAssociationLogService.saveTransferAssociationLog(sourceId,fileName,source.getSourceName(),fileLogRecord); return fileId; } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java index 66f6a8bd22..f6adc1d7e1 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java @@ -1218,13 +1218,12 @@ public class FileManagementControllerTests extends BaseTest { oldFileIdList.add(this.addFileAssociation("sty-file-association-bug-id-2", "BUG", fileAssociationOldFileId)); this.saveSourceAssociationId("sty-file-association-bug-id-2", oldFileIdList); - oldFileIdList = new ArrayList<>(); - oldFileIdList.add(this.addFileAssociation("sty-file-association-bug-id-3", "BUG", fileAssociationOldFileId)); - this.saveSourceAssociationId("sty-file-association-bug-id-3", oldFileIdList); - oldFileIdList = new ArrayList<>(); oldFileIdList.add(this.addFileAssociation("sty-file-association-bug-id-4", "BUG", fileAssociationOldFileId)); this.saveSourceAssociationId("sty-file-association-bug-id-4", oldFileIdList); + + //文件关联展示接口测试 + this.associationFileTableShow(); //文件关联 this.associationFile(); //文件更新 @@ -1276,11 +1275,91 @@ public class FileManagementControllerTests extends BaseTest { FileMetadataExample example = new FileMetadataExample(); example.createCriteria().andProjectIdEqualTo(project.getId()); Assertions.assertEquals(fileMetadataMapper.countByExample(example), 0); - + + //检查fileAssociation表中的数据是否被删除 + FileAssociationExample fileAssociationExample = new FileAssociationExample(); + fileAssociationExample.createCriteria().andFileIdIn(new ArrayList<>() {{ + this.add(fileAssociationNewFilesOne); + this.add(fileAssociationNewFilesTwo); + this.add(fileAssociationNewFilesThree); + this.add(fileAssociationNewFilesFour); + this.add(fileAssociationNewFileId); + this.add(fileAssociationOldFileId); + }}); + Assertions.assertEquals(fileAssociationMapper.countByExample(fileAssociationExample), 0); + //重新上传,用于后续的测试 this.fileUploadTestSuccess(); } + private void associationFileTableShow() throws Exception { + //从没关联过,则除了oldId都不展示 + FileMetadataTableRequest tableRequest = new FileMetadataTableRequest() {{ + this.setCurrent(1); + this.setPageSize(50); + this.setProjectId(project.getId()); + }}; + MvcResult pageResult = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_PAGE, tableRequest); + String returnData = pageResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + Pager> tableResult = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + List hiddenIds = new ArrayList<>() {{ + this.add(fileAssociationOldFileId); + }}; + List list = JSON.parseArray(JSON.toJSONString(tableResult.getList()), FileInformationResponse.class); + for (FileInformationResponse fileInformationResponse : list) { + Assertions.assertFalse(hiddenIds.contains(fileInformationResponse.getId())); + } + //关联过oldFileID,则oldFileID和NewId都不展示 + tableRequest = new FileMetadataTableRequest() {{ + this.setCurrent(1); + this.setPageSize(50); + this.setProjectId(project.getId()); + this.setCombine(new HashMap<>() {{ + this.put("hiddenIds", new ArrayList<>() {{ + this.add(fileAssociationOldFileId); + }}); + }}); + }}; + pageResult = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_PAGE, tableRequest); + returnData = pageResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + tableResult = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + hiddenIds = new ArrayList<>() {{ + this.add(fileAssociationOldFileId); + this.add(fileAssociationNewFileId); + }}; + list = JSON.parseArray(JSON.toJSONString(tableResult.getList()), FileInformationResponse.class); + for (FileInformationResponse fileInformationResponse : list) { + Assertions.assertFalse(hiddenIds.contains(fileInformationResponse.getId())); + } + //关联过newId,则关联的newId和oldId都不展示 + tableRequest = new FileMetadataTableRequest() {{ + this.setCurrent(1); + this.setPageSize(50); + this.setProjectId(project.getId()); + this.setCombine(new HashMap<>() {{ + this.put("hiddenIds", new ArrayList<>() {{ + this.add(fileAssociationNewFilesOne); + this.add(fileAssociationNewFilesTwo); + }}); + }}); + }}; + pageResult = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_PAGE, tableRequest); + returnData = pageResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + tableResult = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + hiddenIds = new ArrayList<>() {{ + this.add(fileAssociationOldFileId); + this.add(fileAssociationNewFilesOne); + this.add(fileAssociationNewFilesTwo); + }}; + list = JSON.parseArray(JSON.toJSONString(tableResult.getList()), FileInformationResponse.class); + for (FileInformationResponse fileInformationResponse : list) { + Assertions.assertFalse(hiddenIds.contains(fileInformationResponse.getId())); + } + } + private void fileAssociationControllerDelete() throws Exception { //删除bug-id-4的 FileAssociationDeleteRequest deleteRequest = new FileAssociationDeleteRequest(); @@ -1306,10 +1385,10 @@ public class FileManagementControllerTests extends BaseTest { this.requestPost(FileManagementRequestUtils.URL_FILE_ASSOCIATION_DELETE, deleteRequest).andExpect(status().isBadRequest()); - FileAssociationDeleteRequest permisionRequest = new FileAssociationDeleteRequest(); - permisionRequest.setAssociationIds(sourceAssociationFileMap.get("sty-file-association-bug-id-4")); - permisionRequest.setProjectId(DEFAULT_PROJECT_ID); - this.requestPostPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_UPDATE, FileManagementRequestUtils.URL_FILE_ASSOCIATION_DELETE, permisionRequest); + FileAssociationDeleteRequest permissionRequest = new FileAssociationDeleteRequest(); + permissionRequest.setAssociationIds(sourceAssociationFileMap.get("sty-file-association-bug-id-4")); + permissionRequest.setProjectId(DEFAULT_PROJECT_ID); + this.requestPostPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_UPDATE, FileManagementRequestUtils.URL_FILE_ASSOCIATION_DELETE, permissionRequest); } private void fileAssociationControllerPage() throws Exception { @@ -1359,8 +1438,8 @@ public class FileManagementControllerTests extends BaseTest { public void associationFile() { //关联id-1和id-3 (不覆盖) List fileIdList = new ArrayList<>() {{ - this.add(fileAssociationNewFileId); this.add(fileAssociationNewFilesOne); + this.add(fileAssociationNewFilesTwo); }}; FileLogRecord fileLogRecord = FileLogRecord.builder() .logModule(OperationLogModule.PROJECT_FILE_MANAGEMENT) @@ -1369,41 +1448,45 @@ public class FileManagementControllerTests extends BaseTest { .operator("admin") .projectId(project.getId()) .build(); - List associationIdList = fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileIdList, false, fileLogRecord); - this.checkFileAssociation("sty-file-association-bug-id-3", fileIdList, new ArrayList<>() {{ - this.add(fileAssociationOldFileId); - }}); - this.saveSourceAssociationId("sty-file-association-bug-id-3", associationIdList); - //关联id-3 (覆盖) - associationIdList = fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileIdList, true, fileLogRecord); - this.checkFileAssociation("sty-file-association-bug-id-3", fileIdList, null); + List associationIdList = fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileIdList, fileLogRecord); + this.checkFileAssociation("sty-file-association-bug-id-3", fileIdList); this.saveSourceAssociationId("sty-file-association-bug-id-3", associationIdList); //重新关联(检查是否会重复插入数据) - associationIdList = fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileIdList, true, fileLogRecord); - this.checkFileAssociation("sty-file-association-bug-id-3", fileIdList, null); + associationIdList = fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileIdList, fileLogRecord); + this.checkFileAssociation("sty-file-association-bug-id-3", fileIdList); this.saveSourceAssociationId("sty-file-association-bug-id-3", associationIdList); - //关联id-2 关联所有正常文件 + + //关联id-3 关联旧文件 + fileIdList = new ArrayList<>() {{ + this.add(fileAssociationOldFileId); + }}; + associationIdList = fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileIdList, fileLogRecord); + this.checkFileAssociation("sty-file-association-bug-id-3", fileIdList); + this.saveSourceAssociationId("sty-file-association-bug-id-3", associationIdList); + + //关联id-2 (含有不存在的ID) List bug2IdList = new ArrayList<>() {{ this.add(fileAssociationNewFilesOne); this.add(fileAssociationNewFilesTwo); this.add(fileAssociationNewFilesThree); + this.add(IDGenerator.nextStr()); }}; - associationIdList = fileAssociationService.association("sty-file-association-bug-id-2", FileAssociationSourceUtil.SOURCE_TYPE_BUG, bug2IdList, true, fileLogRecord); - this.checkFileAssociation("sty-file-association-bug-id-2", bug2IdList, null); + associationIdList = fileAssociationService.association("sty-file-association-bug-id-2", FileAssociationSourceUtil.SOURCE_TYPE_BUG, bug2IdList, fileLogRecord); + Assertions.assertEquals(bug2IdList.size() - 1, associationIdList.size()); this.saveSourceAssociationId("sty-file-association-bug-id-2", associationIdList); //反例: // 文件参数为空 boolean error = false; try { - fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, null, true, fileLogRecord); + fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, null, fileLogRecord); } catch (Exception e) { error = true; } Assertions.assertTrue(error); error = false; try { - fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, new ArrayList<>(), true, fileLogRecord); + fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, new ArrayList<>(), fileLogRecord); } catch (Exception e) { error = true; } @@ -1413,20 +1496,12 @@ public class FileManagementControllerTests extends BaseTest { try { fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, new ArrayList<>() {{ - }}, true, fileLogRecord); - } catch (Exception e) { - error = true; - } - Assertions.assertTrue(error); - //文件数量对不上(含有不存在的文件ID) - error = false; - fileIdList.add(IDGenerator.nextStr()); - try { - fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileIdList, true, fileLogRecord); + }}, fileLogRecord); } catch (Exception e) { error = true; } Assertions.assertTrue(error); + } public void associationUpgrade() { @@ -1557,18 +1632,30 @@ public class FileManagementControllerTests extends BaseTest { .projectId(project.getId()) .build(); - //1.正常删除 资源为bug-1 + //1.正常删除 资源为bug-2 FileAssociationExample example = new FileAssociationExample(); example.clear(); - example.createCriteria().andSourceIdEqualTo("sty-file-association-bug-id-1"); + example.createCriteria().andSourceIdEqualTo("sty-file-association-bug-id-2"); List bug1AssociationList = fileAssociationMapper.selectByExample(example); List idList = bug1AssociationList.stream().map(FileAssociation::getId).collect(Collectors.toList()); - int deleteCount = fileAssociationService.deleteBySourceId(idList, fileLogRecord); + int deleteCount = fileAssociationService.deleteByIds(idList, fileLogRecord); Assertions.assertEquals(idList.size(), deleteCount); + example.clear(); + example.createCriteria().andIdIn(idList); + Assertions.assertEquals(fileAssociationMapper.countByExample(example), 0); + + //删除完了重新关联,继续做测试 + fileAssociationService.association("sty-file-association-bug-id-2", FileAssociationSourceUtil.SOURCE_TYPE_BUG, new ArrayList<>() {{ + this.add(fileAssociationNewFilesOne); + this.add(fileAssociationNewFilesTwo); + this.add(fileAssociationNewFilesThree); + this.add(fileAssociationNewFilesFour); + }}, fileLogRecord); + //2.入参集合为空 - deleteCount = fileAssociationService.deleteBySourceId(new ArrayList<>(), fileLogRecord); + deleteCount = fileAssociationService.deleteByIds(new ArrayList<>(), fileLogRecord); Assertions.assertEquals(0, deleteCount); - deleteCount = fileAssociationService.deleteBySourceId(null, fileLogRecord); + deleteCount = fileAssociationService.deleteByIds(null, fileLogRecord); Assertions.assertEquals(0, deleteCount); //3.里面包含一条已经文件已经删除了的ID 资源为bug-2 @@ -1582,7 +1669,10 @@ public class FileManagementControllerTests extends BaseTest { idList = new ArrayList<>() {{ this.add(association.getId()); }}; - fileAssociationService.deleteBySourceId(idList, fileLogRecord); + fileAssociationService.deleteByIds(idList, fileLogRecord); + example.clear(); + example.createCriteria().andIdIn(idList); + Assertions.assertEquals(fileAssociationMapper.countByExample(example), 0); //4.入参集合包括1条不存在的关联ID 资源为bug-2 example.clear(); @@ -1590,28 +1680,49 @@ public class FileManagementControllerTests extends BaseTest { List bug2AssociationList = fileAssociationMapper.selectByExample(example); idList = bug2AssociationList.stream().map(FileAssociation::getId).collect(Collectors.toList()); idList.add(IDGenerator.nextStr()); - deleteCount = fileAssociationService.deleteBySourceId(idList, fileLogRecord); + deleteCount = fileAssociationService.deleteByIds(idList, fileLogRecord); Assertions.assertEquals(idList.size() - 1, deleteCount); + example.clear(); + example.createCriteria().andIdIn(idList); + Assertions.assertEquals(fileAssociationMapper.countByExample(example), 0); + + //5.直接删除sourceId为bug-3的 + List bug2IdList = new ArrayList<>() {{ + this.add(fileAssociationNewFilesOne); + this.add(fileAssociationNewFilesTwo); + this.add(fileAssociationNewFilesThree); + this.add(fileAssociationNewFilesFour); + }}; + fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, bug2IdList, fileLogRecord); + fileAssociationService.association("sty-file-association-bug-id-2", FileAssociationSourceUtil.SOURCE_TYPE_BUG, bug2IdList, fileLogRecord); + example.clear(); + example.createCriteria().andSourceIdEqualTo("sty-file-association-bug-id-3"); + long beforeDelete = fileAssociationMapper.countByExample(example); + fileAssociationService.deleteBySourceIds(new ArrayList<>() {{ + this.add("sty-file-association-bug-id-2"); + this.add("sty-file-association-bug-id-3"); + }}, fileLogRecord); + long afterDelete = fileAssociationMapper.countByExample(example); + Assertions.assertTrue(beforeDelete > afterDelete); + Assertions.assertEquals(afterDelete, 0); + + //参数测试 + Assertions.assertEquals(fileAssociationService.deleteBySourceIds(null, fileLogRecord), 0); + //重新关联,用来测试文件删除是否会级联删除。使用bug-3 + fileAssociationService.association("sty-file-association-bug-id-3", FileAssociationSourceUtil.SOURCE_TYPE_BUG, bug2IdList, fileLogRecord); + fileAssociationService.association("sty-file-association-bug-id-2", FileAssociationSourceUtil.SOURCE_TYPE_BUG, bug2IdList, fileLogRecord); } /** * @param sourceId 资源ID * @param fileIdList 关联的文件集合 - * @param oldFileIds 检查关联的文件中是否有过期文件 */ - private void checkFileAssociation(String sourceId, List fileIdList, List oldFileIds) { + private void checkFileAssociation(String sourceId, List fileIdList) { FileAssociationExample example = new FileAssociationExample(); example.createCriteria().andSourceIdEqualTo(sourceId).andFileIdIn(fileIdList); long count = fileAssociationMapper.countByExample(example); - if (CollectionUtils.isEmpty(oldFileIds)) { - Assertions.assertEquals(count, fileIdList.size()); - } else { - Assertions.assertEquals(count, fileIdList.size() - oldFileIds.size()); - example.clear(); - example.createCriteria().andSourceIdEqualTo(sourceId).andFileIdIn(oldFileIds); - Assertions.assertEquals(fileAssociationMapper.countByExample(example), oldFileIds.size()); - } + Assertions.assertEquals(count, fileIdList.size()); } private String addFileAssociation(String sourceId, String sourceType, String fileId) { @@ -1630,26 +1741,6 @@ public class FileManagementControllerTests extends BaseTest { return fileAssociation.getId(); } - @Test - @Order(31) - public void upgradeFileAssociationTest() throws Exception { - //预备:在fileAssociationTest中已经将【sty-file-association-bug-id-1】的文件关联过过期文件 - //1.首先测试反例:手动修改过期文件所有的版本latest都为false。测试之后改过来 - //2.升级id-3的 - //3.升级id-1的 - - //反例: - //文件不存在 - } - - @Test - @Order(32) - public void deleteFileAssociationTest() throws Exception { - //删除id-1和id-2的 - //参数校验:空集合 - //集合里有个假文件id - //id-3里关联的文件数据里删除1条源文件数据 - } @Test @Order(40)