refactor(接口测试): 优化本地执行时附件的处理逻辑,不再进行本地附件拷贝操作

优化本地执行时附件的处理逻辑,不再进行本地附件拷贝操作
This commit is contained in:
song-tianyang 2023-02-09 18:40:03 +08:00 committed by fit2-zhao
parent 4cdc6561dc
commit a7a7a457a5
5 changed files with 48 additions and 52 deletions

View File

@ -162,7 +162,7 @@ public class JMeterService {
if (request.getHashTree() != null) {
ElementUtil.coverArguments(request.getHashTree());
//解析HashTree里的文件信息
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFileForNode(request.getHashTree(), request.getReportId());
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFile(request.getHashTree(), request.getReportId(), false);
if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) {
redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(request.getReportId()), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList));
}
@ -197,7 +197,7 @@ public class JMeterService {
//解析HashTree里的文件信息
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFileForNode(request.getHashTree(), request.getReportId());
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFile(request.getHashTree(), request.getReportId(), false);
if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) {
redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(request.getReportId()), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList));
}

View File

@ -16,7 +16,6 @@ import io.metersphere.service.RedisTemplateService;
import io.metersphere.service.TestResultService;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.utils.RetryResultUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.services.FileServer;
@ -37,7 +36,6 @@ public class MsApiBackendListener extends AbstractBackendListenerClient implemen
// 当前场景报告/用例结果状态
private ResultVO resultVO;
@Resource
private RedisTemplateService redisTemplateService;
/**
@ -54,6 +52,9 @@ public class MsApiBackendListener extends AbstractBackendListenerClient implemen
if (testResultService == null) {
testResultService = CommonBeanFactory.getBean(TestResultService.class);
}
if (redisTemplateService == null) {
redisTemplateService = CommonBeanFactory.getBean(RedisTemplateService.class);
}
resultVO = new ResultVO();
super.setupTest(context);
}

View File

@ -12,6 +12,7 @@ import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.metadata.vo.FileRequest;
import io.metersphere.request.BodyFile;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.utils.TemporaryFileUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@ -29,6 +30,7 @@ import java.util.List;
public class ApiFileUtil extends FileUtils {
private static FileManagerService fileManagerService;
private static FileMetadataService fileMetadataService;
private static TemporaryFileUtil temporaryFileUtil;
public static String getFilePath(BodyFile file) {
String type = StringUtils.isNotEmpty(file.getFileType()) ? file.getFileType().toLowerCase() : null;
@ -111,13 +113,16 @@ public class ApiFileUtil extends FileUtils {
}
}
public static List<AttachmentBodyFile> getExecuteFileForNode(HashTree tree, String reportId) {
public static List<AttachmentBodyFile> getExecuteFile(HashTree tree, String reportId, boolean isLocal) {
if (temporaryFileUtil == null) {
temporaryFileUtil = CommonBeanFactory.getBean(TemporaryFileUtil.class);
}
List<AttachmentBodyFile> fileList = new ArrayList<>();
formatFilePathForNode(tree, reportId, fileList);
formatFilePathForNode(tree, reportId, isLocal, fileList);
return fileList;
}
private static void formatFilePathForNode(HashTree tree, String reportId, List<AttachmentBodyFile> fileList) {
private static void formatFilePathForNode(HashTree tree, String reportId, boolean isLocal, List<AttachmentBodyFile> fileList) {
if (tree != null) {
if (fileMetadataService == null) {
fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
@ -129,35 +134,35 @@ public class ApiFileUtil extends FileUtils {
}
HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) {
getAttachmentBodyFileByHttp(key, reportId, fileList);
getAttachmentBodyFileByHttp(key, reportId, isLocal, fileList);
} else if (key instanceof CSVDataSet) {
getAttachmentBodyFileByCsv(key, reportId, fileList);
getAttachmentBodyFileByCsv(key, reportId, isLocal, fileList);
}
if (node != null) {
formatFilePathForNode(node, reportId, fileList);
formatFilePathForNode(node, reportId, isLocal, fileList);
}
}
}
}
public static void getAttachmentBodyFileByCsv(Object tree, String reportId, List<AttachmentBodyFile> bodyFileList) {
public static void getAttachmentBodyFileByCsv(Object tree, String reportId, boolean isLocal, List<AttachmentBodyFile> bodyFileList) {
CSVDataSet source = (CSVDataSet) tree;
if (StringUtils.isNotEmpty(source.getPropertyAsString(ElementConstants.FILENAME))) {
getAttachmentFileByTestElement(source, reportId, bodyFileList);
getAttachmentFileByTestElement(source, reportId, isLocal, bodyFileList);
}
}
public static void getAttachmentBodyFileByHttp(Object testElement, String reportId, List<AttachmentBodyFile> fileList) {
public static void getAttachmentBodyFileByHttp(Object testElement, String reportId, boolean isLocal, List<AttachmentBodyFile> fileList) {
if (testElement == null) {
return;
}
HTTPSamplerProxy source = (HTTPSamplerProxy) testElement;
for (HTTPFileArg httpFileArg : source.getHTTPFiles()) {
getAttachmentFileByTestElement(httpFileArg, reportId, fileList);
getAttachmentFileByTestElement(httpFileArg, reportId, isLocal, fileList);
}
}
private static void getAttachmentFileByTestElement(TestElement testElement, String reportId, List<AttachmentBodyFile> bodyFileList) {
private static void getAttachmentFileByTestElement(TestElement testElement, String reportId, boolean isLocal, List<AttachmentBodyFile> bodyFileList) {
if (testElement == null) {
return;
}
@ -186,15 +191,19 @@ public class ApiFileUtil extends FileUtils {
}
bodyFileList.add(attachmentBodyFile);
testElement.setProperty(ElementConstants.FILENAME, path);
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_STORAGE.name(), fileMetadata.getStorage());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_NAME.name(), fileMetadata.getName());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_UPDATE_TIME.name(), fileMetadata.getUpdateTime());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_PROJECT_ID.name(), fileMetadata.getProjectId());
if (StringUtils.isNotBlank(fileMetadata.getAttachInfo())) {
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_ATTACH_INFO.name(), fileMetadata.getAttachInfo());
if (!isLocal) {
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_STORAGE.name(), fileMetadata.getStorage());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_NAME.name(), fileMetadata.getName());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_UPDATE_TIME.name(), fileMetadata.getUpdateTime());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_PROJECT_ID.name(), fileMetadata.getProjectId());
if (StringUtils.isNotBlank(fileMetadata.getAttachInfo())) {
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_ATTACH_INFO.name(), fileMetadata.getAttachInfo());
}
} else {
path = temporaryFileUtil.generateFilePath(attachmentBodyFile.getProjectId(), attachmentBodyFile.getFileUpdateTime(), attachmentBodyFile.getName());
}
testElement.setProperty(ElementConstants.FILENAME, path);
if (testElement instanceof HTTPFileArg) {
((HTTPFileArg) testElement).setPath(path);
}

View File

@ -10,6 +10,7 @@ import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.dto.AttachmentBodyFile;
import io.metersphere.dto.FileInfoDTO;
import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.environment.service.BaseEnvironmentService;
@ -27,7 +28,10 @@ import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author song.tianyang
@ -238,37 +242,19 @@ public class HashTreeUtil {
if (runRequest.getPool().isPool() || runRequest.getPool().isK8s()) {
return;
}
List<BodyFile> files = new LinkedList<>();
ApiFileUtil.getExecuteFiles(runRequest.getHashTree(), runRequest.getReportId(), files);
if (CollectionUtils.isNotEmpty(files)) {
Map<String, String> repositoryFileMap = new HashMap<>();
for (BodyFile bodyFile : files) {
if (!StringUtils.equals(bodyFile.getStorage(), StorageConstants.LOCAL.name())
&& StringUtils.isNotBlank(bodyFile.getFileId())) {
repositoryFileMap.put(bodyFile.getFileId(), bodyFile.getName());
}
}
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
if (fileMetadataService != null) {
Map<String, byte[]> multipartFiles = new LinkedHashMap<>();
List<FileInfoDTO> fileInfoDTOList = fileMetadataService.downloadApiExecuteFilesByIds(repositoryFileMap.keySet());
fileInfoDTOList.forEach(dto -> {
if (ArrayUtils.isNotEmpty(dto.getFileByte())) {
String key = StringUtils.join(
FileUtils.BODY_FILE_DIR,
File.separator,
repositoryFileMap.get(dto.getId()));
multipartFiles.put(key, dto.getFileByte());
}
});
List<AttachmentBodyFile> downloadFileList = ApiFileUtil.getExecuteFile(runRequest.getHashTree(), runRequest.getReportId(), true);
for (Map.Entry<String, byte[]> fileEntry : multipartFiles.entrySet()) {
String fileName = fileEntry.getKey();
byte[] fileBytes = fileEntry.getValue();
FileUtils.createFile(fileName, fileBytes);
}
Map<String, String> repositoryFileMap = new HashMap<>();
for (AttachmentBodyFile bodyFile : downloadFileList) {
if (!StringUtils.equals(bodyFile.getFileStorage(), StorageConstants.LOCAL.name())
&& StringUtils.isNotBlank(bodyFile.getFileMetadataId())) {
repositoryFileMap.put(bodyFile.getFileMetadataId(), bodyFile.getName());
}
}
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
if (fileMetadataService != null) {
fileMetadataService.downloadApiExecuteFilesByIds(repositoryFileMap.keySet());
}
}
public static void downFile(

View File

@ -248,7 +248,7 @@ public class ApiJMeterFileService {
Map<String, byte[]> multipartFiles = this.getMultipartFiles(testId, hashTree);
转为解析jmx中附件节点赋予相关信息(例如文件关联类型路径更新时间等),并将文件信息存储在redis中为了进行连接ms下载时的安全校验
*/
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFileForNode(hashTree, reportId);
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFile(hashTree, reportId, false);
if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) {
redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(reportId), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList));
}