fix(接口测试): 接口测试执行附件时重复文件判断逻辑处理

接口测试执行附件时重复文件判断逻辑处理:执行目录设置文件ID层,用于判断是否是重复文件
This commit is contained in:
song-tianyang 2023-02-15 18:57:34 +08:00 committed by 建国
parent 6a633e28ef
commit b30702bde7
3 changed files with 35 additions and 27 deletions

View File

@ -215,7 +215,7 @@ public class ApiFileUtil extends FileUtils {
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_UPDATE_TIME.name(), fileMetadata.getUpdateTime());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_PROJECT_ID.name(), fileMetadata.getProjectId());
} else {
path = temporaryFileUtil.generateFilePath(attachmentBodyFile.getProjectId(), attachmentBodyFile.getFileUpdateTime(), attachmentBodyFile.getName());
path = temporaryFileUtil.generateFilePath(attachmentBodyFile.getProjectId(), attachmentBodyFile.getFileMetadataId(), attachmentBodyFile.getFileUpdateTime(), attachmentBodyFile.getName());
}
testElement.setProperty(ElementConstants.FILENAME, path);

View File

@ -27,24 +27,24 @@ public class TemporaryFileUtil {
+ File.separator;
}
public String generateFileDir(String folder, long updateTime) {
public String generateFileDir(String folder, String fileMetadataId, long updateTime) {
if (StringUtils.isBlank(folder)) {
folder = DEFAULT_FILE_FOLDER;
}
if (updateTime == 0) {
if (StringUtils.isBlank(fileMetadataId)) {
return fileFolder + folder + File.separator;
} else {
return fileFolder + folder + File.separator + updateTime + File.separator;
String metadataIdFolder = fileFolder + folder + File.separator + fileMetadataId + File.separator;
return updateTime == 0 ? metadataIdFolder : metadataIdFolder + updateTime + File.separator;
}
}
public String generateFilePath(String folder, long updateTime, String fileName) {
return generateFileDir(folder, updateTime) + fileName;
public String generateFilePath(String folder, String fileMetadataId, long updateTime, String fileName) {
return generateFileDir(folder, fileMetadataId, updateTime) + fileName;
}
public File getFile(String folder, long updateTime, String fileName) {
File file = new File(generateFilePath(folder, updateTime, fileName));
public File getFile(String folder, String fileMetadataId, long updateTime, String fileName) {
File file = new File(generateFilePath(folder, fileMetadataId, updateTime, fileName));
if (file.exists()) {
return file;
} else {
@ -52,26 +52,30 @@ public class TemporaryFileUtil {
}
}
public void saveFile(String folder, long updateTime, String fileName, byte[] fileBytes) {
public void saveFile(String folder, String fileMetadataId, long updateTime, String fileName, byte[] fileBytes) {
//删除过期文件
deleteOldFile(folder, updateTime, fileName);
this.createFile(generateFilePath(folder, updateTime, fileName), fileBytes);
deleteOldFile(folder, fileMetadataId, updateTime, fileName);
this.createFile(generateFilePath(folder, fileMetadataId, updateTime, fileName), fileBytes);
}
public void saveFileByParamCheck(String folder, long updateTime, String fileName, byte[] fileBytes) {
public void saveFileByParamCheck(String folder, String fileMetadataId, long updateTime, String fileName, byte[] fileBytes) {
if (fileBytes != null && StringUtils.isNotBlank(folder) && updateTime > 0
&& StringUtils.isNotBlank(fileName) && fileBytes.length > 0) {
//删除过期文件
deleteOldFile(folder, updateTime, fileName);
this.createFile(generateFilePath(folder, updateTime, fileName), fileBytes);
deleteOldFile(folder, fileMetadataId, updateTime, fileName);
this.createFile(generateFilePath(folder, fileMetadataId, updateTime, fileName), fileBytes);
}
}
//node也调用了该方法
public void deleteOldFile(String folder, long lastUpdateTime, String deleteFileName) {
public void deleteOldFile(String folder, String fileMetadataId, long lastUpdateTime, String deleteFileName) {
if (StringUtils.isEmpty(fileMetadataId)) {
//本地文件不涉及到判断 原因在ms上不会将本地文件放在执行文件中在node上本地文件再更新会存到Minio里
return;
}
String newFileFolderName = String.valueOf(lastUpdateTime);
List<File> deleteFileList = new ArrayList<>();
File file = new File(generateFileDir(folder, 0));
File file = new File(generateFileDir(folder, fileMetadataId, 0));
//当前目录下存放的是以时间戳命名的文件夹文件夹里存放着具体的文件所以要删除这个
if (file.isDirectory()) {
File[] checkFileFolders = file.listFiles();
@ -81,9 +85,8 @@ public class TemporaryFileUtil {
File[] checkFiles = checkFileFolder.listFiles();
if (checkFiles != null) {
for (File checkFile : checkFiles) {
if (StringUtils.equals(checkFile.getName(), deleteFileName)
&& !StringUtils.equals(checkFileFolder.getName(), newFileFolderName)) {
//文件名称相同但是所属的时间戳文件夹与本次不相同的文件是过期文件
if (!StringUtils.equals(checkFileFolder.getName(), newFileFolderName)) {
//MinIO文件名称可能会修改所以这里只判断文件所处的时间戳文件夹是否对应
deleteFileList.add(checkFile);
}
}

View File

@ -325,6 +325,8 @@ public class FileMetadataService {
if (StringUtils.isNotEmpty(fileMetadata.getStorage()) && StringUtils.isEmpty(fileMetadata.getResourceType())) {
fileMetadata.setPath(FileUtils.getFilePath(fileMetadata));
}
//latest字段只能在git/pull时更新
fileMetadata.setLatest(null);
fileMetadataMapper.updateByPrimaryKeySelective(fileMetadata);
}
@ -570,7 +572,7 @@ public class FileMetadataService {
List<FileRequest> downloadFileRequest = new ArrayList<>();
//检查是否存在已下载的文件
fileMetadataWithBLOBList.forEach(fileMetadata -> {
File file = temporaryFileUtil.getFile(fileMetadata.getProjectId(), fileMetadata.getUpdateTime(), fileMetadata.getName());
File file = temporaryFileUtil.getFile(fileMetadata.getProjectId(), fileMetadata.getId(), fileMetadata.getUpdateTime(), fileMetadata.getName());
if (file != null) {
LoggerUtil.info("文件【" + fileMetadata.getUpdateTime() + "_" + fileMetadata.getName() + "】在执行目录【" + fileMetadata.getProjectId() + "】已找到,无需下载");
FileInfoDTO fileInfoDTO = new FileInfoDTO(fileMetadata.getId(), fileMetadata.getName(), fileMetadata.getProjectId(), fileMetadata.getUpdateTime(), fileMetadata.getStorage(), fileMetadata.getPath(), FileUtils.fileToByte(file));
@ -583,7 +585,7 @@ public class FileMetadataService {
List<FileInfoDTO> repositoryFileDTOList = fileManagerService.downloadFileBatch(downloadFileRequest);
//将文件存储到执行文件目录中避免多次执行时触发多次下载
if (CollectionUtils.isNotEmpty(repositoryFileDTOList)) {
repositoryFileDTOList.forEach(repositoryFile -> temporaryFileUtil.saveFileByParamCheck(repositoryFile.getProjectId(), repositoryFile.getFileLastUpdateTime(), repositoryFile.getFileName(), repositoryFile.getFileByte()));
repositoryFileDTOList.forEach(repositoryFile -> temporaryFileUtil.saveFileByParamCheck(repositoryFile.getProjectId(), repositoryFile.getId(), repositoryFile.getFileLastUpdateTime(), repositoryFile.getFileName(), repositoryFile.getFileByte()));
fileInfoDTOList.addAll(repositoryFileDTOList);
}
return fileInfoDTOList;
@ -631,8 +633,8 @@ public class FileMetadataService {
}
public FileMetadata pullFromRepository(FileMetadata request) {
FileMetadata returnModel = null;
FileMetadataWithBLOBs baseMetadata = fileMetadataMapper.selectByPrimaryKey(request.getId());
FileMetadata returnModel = baseMetadata;
if (StringUtils.equals(baseMetadata.getStorage(), StorageConstants.GIT.name()) && StringUtils.isNotEmpty(baseMetadata.getAttachInfo())) {
RemoteFileAttachInfo baseAttachInfo = JSON.parseObject(baseMetadata.getAttachInfo(), RemoteFileAttachInfo.class);
FileModule fileModule = fileModuleService.get(baseMetadata.getModuleId());
@ -646,10 +648,13 @@ public class FileMetadataService {
FileMetadataWithBLOBs newMetadata = this.genOtherVersionFileMetadata(baseMetadata, thisTime, gitFileAttachInfo);
fileMetadataMapper.insert(newMetadata);
baseMetadata.setUpdateTime(thisTime);
baseMetadata.setLatest(false);
baseMetadata.setUpdateUser(SessionUtils.getUserId());
fileMetadataMapper.updateByPrimaryKeySelective(baseMetadata);
FileMetadataWithBLOBs updateOldData = new FileMetadataWithBLOBs();
updateOldData.setLatest(Boolean.FALSE);
FileMetadataExample example = new FileMetadataExample();
example.createCriteria().andIdEqualTo(baseMetadata.getId());
fileMetadataMapper.updateByExampleSelective(updateOldData, example);
returnModel = newMetadata;
}
}
}