refactor(项目管理): 文件关联接口逻辑优化

This commit is contained in:
song-tianyang 2023-11-23 19:05:10 +08:00 committed by 刘瑞斌
parent 864b9124ac
commit 8f221ee39b
7 changed files with 216 additions and 123 deletions

View File

@ -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()));
}
}

View File

@ -197,7 +197,7 @@ public class FunctionalCaseAttachmentService {
* @param projectId projectId
*/
public void association(List<String> 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<String> unLinkFilesIds, String logUrl, String userId, String projectId) {
fileAssociationService.deleteBySourceId(unLinkFilesIds, createFileLogRecord(logUrl, userId, projectId));
fileAssociationService.deleteByIds(unLinkFilesIds, createFileLogRecord(logUrl, userId, projectId));
}

View File

@ -59,6 +59,6 @@ public class FileAssociationController {
.projectId(request.getProjectId())
.build();
return fileAssociationService.deleteBySourceId(request.getAssociationIds(),fileLogRecord);
return fileAssociationService.deleteByIds(request.getAssociationIds(), fileLogRecord);
}
}

View File

@ -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<String> 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")));
}
}
}
}

View File

@ -77,6 +77,14 @@
<if test="fileType != null and fileType != ''">
AND f.type = #{fileType}
</if>
<if test="hiddenIds != null and hiddenIds.size()>0">
AND f.ref_id NOT IN
(SELECT ref_id FROM file_metadata WHERE id IN
<foreach collection="hiddenIds" item="item" open="(" separator="," close=")">
#{item}
</foreach>
)
</if>
<if test="operator != null and operator != ''">
AND f.create_user = #{operator}
</if>

View File

@ -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<String> association(String sourceId, String sourceType, List<String> fileIds, boolean isOverWrite, @Validated FileLogRecord fileLogRecord) {
public List<String> association(String sourceId, String sourceType, List<String> 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<FileMetadata> 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<FileAssociation> associationdList = fileAssociationMapper.selectByExample(example);
Map<String,FileAssociation> refIdFileAssociationMap = associationdList.stream().collect(Collectors.toMap(FileAssociation::getFileRefId, item -> item));
Map<FileAssociation,FileMetadata> updateAssociationMap = new HashMap<>();
List<FileMetadata> 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<String> 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<String> createFileAssociation(List<FileMetadata> addFileList, String sourceId, String sourceName,String sourceType, @Validated FileLogRecord logRecord) {
private List<String> createFileAssociation(List<FileMetadata> addFileList, String sourceId, String sourceName, String sourceType, @Validated FileLogRecord logRecord) {
FileAssociationSourceUtil.validate(sourceType);
if(CollectionUtils.isNotEmpty(addFileList)){
List<FileAssociation> createFile = new ArrayList<>();
@ -193,28 +182,6 @@ public class FileAssociationService {
return new ArrayList<>();
}
}
private Collection<String> updateFileAssociation(Map<FileAssociation, FileMetadata> 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<FileAssociation, FileMetadata> 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<String> idList, @Validated FileLogRecord logRecord){
public int deleteByIds(List<String> 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<String> 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<FileAssociation> fileAssociationList = fileAssociationMapper.selectByExample(example);
Map<String,List<String>> sourceToFileNameMap = this.genSourceNameFileNameMap(fileAssociationList);
Map<String, List<String>> 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<String> 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;
}

View File

@ -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<List<FileInformationResponse>> tableResult = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
List<String> hiddenIds = new ArrayList<>() {{
this.add(fileAssociationOldFileId);
}};
List<FileInformationResponse> 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<String> 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<String> 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<String> 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<String> 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<FileAssociation> bug1AssociationList = fileAssociationMapper.selectByExample(example);
List<String> 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<FileAssociation> 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<String> 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<String> fileIdList, List<String> oldFileIds) {
private void checkFileAssociation(String sourceId, List<String> 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)