refactor(接口测试): 本地附件统一存入MinIO

This commit is contained in:
fit2-zhao 2023-01-05 16:21:22 +08:00 committed by fit2-zhao
parent d105c23bda
commit b73093967a
18 changed files with 233 additions and 107 deletions

View File

@ -135,9 +135,10 @@ public class ElementUtil {
if (!config.isOperating() && !isRepository && !new File(path).exists()) { if (!config.isOperating() && !isRepository && !new File(path).exists()) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]"); MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
} }
csvDataSet.setProperty("filename", path); csvDataSet.setProperty(ElementConstants.FILENAME, path);
csvDataSet.setProperty("isRef", isRef); csvDataSet.setProperty(ElementConstants.IS_REF, isRef);
csvDataSet.setProperty("fileId", fileId); csvDataSet.setProperty(ElementConstants.FILE_ID, fileId);
csvDataSet.setProperty(ElementConstants.RESOURCE_ID, file.getId());
} }
csvDataSet.setIgnoreFirstLine(false); csvDataSet.setIgnoreFirstLine(false);
csvDataSet.setProperty("shareMode", shareMode); csvDataSet.setProperty("shareMode", shareMode);
@ -182,9 +183,10 @@ public class ElementUtil {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]"); MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
} }
} }
csvDataSet.setProperty("filename", path); csvDataSet.setProperty(ElementConstants.FILENAME, path);
csvDataSet.setProperty("isRef", isRef); csvDataSet.setProperty(ElementConstants.IS_REF, isRef);
csvDataSet.setProperty("fileId", fileId); csvDataSet.setProperty(ElementConstants.FILE_ID, fileId);
csvDataSet.setProperty(ElementConstants.RESOURCE_ID, file.getId());
} }
csvDataSet.setIgnoreFirstLine(false); csvDataSet.setIgnoreFirstLine(false);
csvDataSet.setProperty("shareMode", shareMode); csvDataSet.setProperty("shareMode", shareMode);

View File

@ -56,18 +56,18 @@ public class MsJmeterElement extends MsTestElement {
} }
// csv 检查处理 // csv 检查处理
if (!config.isOperating() && scriptWrapper instanceof CSVDataSet && ((CSVDataSet) scriptWrapper).isEnabled()) { if (!config.isOperating() && scriptWrapper instanceof CSVDataSet && ((CSVDataSet) scriptWrapper).isEnabled()) {
String path = ((CSVDataSet) scriptWrapper).getPropertyAsString("filename"); String path = ((CSVDataSet) scriptWrapper).getPropertyAsString(ElementConstants.FILENAME);
if (!new File(path).exists()) { if (!new File(path).exists()) {
// 检查场景变量中的csv文件是否存在 // 检查场景变量中的csv文件是否存在
String pathArr[] = path.split("\\/"); String pathArr[] = path.split("\\/");
String csvPath = this.getCSVPath(config, pathArr[pathArr.length - 1]); String csvPath = this.getCSVPath(config, pathArr[pathArr.length - 1]);
if (StringUtils.isNotEmpty(csvPath)) { if (StringUtils.isNotEmpty(csvPath)) {
((CSVDataSet) scriptWrapper).setProperty("filename", csvPath); ((CSVDataSet) scriptWrapper).setProperty(ElementConstants.FILENAME, csvPath);
} else { } else {
MSException.throwException(StringUtils.isEmpty(((CSVDataSet) scriptWrapper).getName()) ? "CSVDataSet" : ((CSVDataSet) scriptWrapper).getName() + "[ CSV文件不存在 ]"); MSException.throwException(StringUtils.isEmpty(((CSVDataSet) scriptWrapper).getName()) ? "CSVDataSet" : ((CSVDataSet) scriptWrapper).getName() + "[ CSV文件不存在 ]");
} }
} }
String csvPath = ((CSVDataSet) scriptWrapper).getPropertyAsString("filename"); String csvPath = ((CSVDataSet) scriptWrapper).getPropertyAsString(ElementConstants.FILENAME);
if (config.getCsvFilePaths().contains(csvPath)) { if (config.getCsvFilePaths().contains(csvPath)) {
return; return;
} else { } else {

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.scenario; package io.metersphere.api.dto.scenario;
import io.metersphere.api.exec.generator.JSONSchemaRunTest; import io.metersphere.api.exec.generator.JSONSchemaRunTest;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.StorageConstants; import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
@ -202,8 +203,9 @@ public class Body {
mimetype = ContentType.APPLICATION_OCTET_STREAM.getMimeType(); mimetype = ContentType.APPLICATION_OCTET_STREAM.getMimeType();
} }
HTTPFileArg fileArg = new HTTPFileArg(path, isBinary ? StringUtils.EMPTY : paramName, mimetype); HTTPFileArg fileArg = new HTTPFileArg(path, isBinary ? StringUtils.EMPTY : paramName, mimetype);
fileArg.setProperty("isRef", isRef); fileArg.setProperty(ElementConstants.IS_REF, isRef);
fileArg.setProperty("fileId", fileId); fileArg.setProperty(ElementConstants.FILE_ID, fileId);
fileArg.setProperty(ElementConstants.RESOURCE_ID, requestId);
list.add(fileArg); list.add(fileArg);
}); });
} }

View File

@ -183,7 +183,7 @@ public class ApiScenarioImportUtil {
structureCaseByJson(object, testCaseService, apiDefinition, apiTestCaseMapper, apiIdCaseNameMap); structureCaseByJson(object, testCaseService, apiDefinition, apiTestCaseMapper, apiIdCaseNameMap);
} else { } else {
object.put("id", sameCase.getId()); object.put("id", sameCase.getId());
object.put("resourceId", sameCase.getId()); object.put(ElementConstants.RESOURCE_ID, sameCase.getId());
object.put("projectId", projectId); object.put("projectId", projectId);
object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY); object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY);
object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY); object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY);
@ -242,7 +242,7 @@ public class ApiScenarioImportUtil {
test.setLatest(true); test.setLatest(true);
test.setVersionId(versionId); test.setVersionId(versionId);
object.put("id", test.getId()); object.put("id", test.getId());
object.put("resourceId", test.getId()); object.put(ElementConstants.RESOURCE_ID, test.getId());
object.put("projectId", projectId); object.put("projectId", projectId);
object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY); object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY);
object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY); object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY);
@ -298,7 +298,7 @@ public class ApiScenarioImportUtil {
} }
ApiDefinitionImportUtilService apiDefinitionImportUtilService = CommonBeanFactory.getBean(ApiDefinitionImportUtilService.class); ApiDefinitionImportUtilService apiDefinitionImportUtilService = CommonBeanFactory.getBean(ApiDefinitionImportUtilService.class);
object.put("id", apiTestCase.getId()); object.put("id", apiTestCase.getId());
object.put("resourceId", apiTestCase.getId()); object.put(ElementConstants.RESOURCE_ID, apiTestCase.getId());
object.put("projectId", projectId); object.put("projectId", projectId);
object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY); object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY);
object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY); object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY);
@ -326,7 +326,7 @@ public class ApiScenarioImportUtil {
for (int i = 0; i < hashTree.length(); i++) { for (int i = 0; i < hashTree.length(); i++) {
JSONObject object = (JSONObject) hashTree.get(i); JSONObject object = (JSONObject) hashTree.get(i);
object.put("index", i + 1); object.put("index", i + 1);
object.put("resourceId", UUID.randomUUID().toString()); object.put(ElementConstants.RESOURCE_ID, UUID.randomUUID().toString());
hashTree.put(i, object); hashTree.put(i, object);
if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) { if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) {
formatHashTree(object.optJSONArray(ElementConstants.HASH_TREE)); formatHashTree(object.optJSONArray(ElementConstants.HASH_TREE));

View File

@ -52,5 +52,8 @@ public class ElementConstants {
public static final String SCRIPT = "script"; public static final String SCRIPT = "script";
public static final String BEANSHELL = "beanshell"; public static final String BEANSHELL = "beanshell";
public static final String IS_REF = "isRef";
public static final String FILE_ID = "fileId";
public static final String RESOURCE_ID = "resourceId";
public static final String FILENAME = "filename";
} }

View File

@ -1,13 +1,29 @@
package io.metersphere.commons.utils; package io.metersphere.commons.utils;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.dto.FileInfoDTO; import io.metersphere.dto.FileInfoDTO;
import io.metersphere.metadata.service.FileManagerService;
import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.metadata.vo.FileRequest;
import io.metersphere.request.BodyFile; import io.metersphere.request.BodyFile;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.CSVDataSet;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPFileArg;
import org.apache.jorphan.collections.HashTree;
import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
public class ApiFileUtil extends FileUtils { public class ApiFileUtil extends FileUtils {
private static FileManagerService fileManagerService;
private static FileMetadataService fileMetadataService;
public static String getFilePath(BodyFile file) { public static String getFilePath(BodyFile file) {
String type = StringUtils.isNotEmpty(file.getFileType()) ? file.getFileType().toLowerCase() : null; String type = StringUtils.isNotEmpty(file.getFileType()) ? file.getFileType().toLowerCase() : null;
String name = file.getName(); String name = file.getName();
@ -22,4 +38,163 @@ public class ApiFileUtil extends FileUtils {
createFile(item.getPath(), item.getFileByte()); createFile(item.getPath(), item.getFileByte());
}); });
} }
public static void copyBodyFiles(String sourceId, String targetId) {
// 本地存储
FileUtils.copyBodyFiles(sourceId, targetId);
}
public static void createBodyFiles(String requestId, List<MultipartFile> bodyFiles) {
FileUtils.createBodyFiles(requestId, bodyFiles);
// MinIO存储
if (fileManagerService == null) {
fileManagerService = CommonBeanFactory.getBean(FileManagerService.class);
}
for (MultipartFile bodyFile : bodyFiles) {
fileManagerService.upload(bodyFile, getRequest(requestId));
}
}
public static void createBodyFiles(List<String> bodyUploadIds, List<MultipartFile> bodyFiles) {
if (CollectionUtils.isNotEmpty(bodyUploadIds) && CollectionUtils.isNotEmpty(bodyFiles)
&& bodyUploadIds.size() == bodyFiles.size()) {
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
// MinIO存储
if (fileManagerService == null) {
fileManagerService = CommonBeanFactory.getBean(FileManagerService.class);
}
for (int i = 0; i < bodyUploadIds.size(); i++) {
MultipartFile bodyFile = bodyFiles.get(i);
fileManagerService.upload(bodyFile, getRequest(bodyUploadIds.get(i)));
}
}
}
public static void deleteBodyFiles(String requestId) {
if (StringUtils.isBlank(requestId)) {
return;
}
FileUtils.deleteBodyFiles(requestId);
// MinIO文件删除
if (fileManagerService == null) {
fileManagerService = CommonBeanFactory.getBean(FileManagerService.class);
}
fileManagerService.delete(getRequest(requestId));
}
private static FileRequest getRequest(String requestId) {
FileRequest request = new FileRequest();
String path = StringUtils.join(BODY_FILE_DIR, File.separator, requestId);
request.setProjectId(path);
request.setFileName(requestId);
request.setStorage(StorageConstants.MINIO.name());
return request;
}
public static void downloadFile(String requestId, String path) {
// MinIO文件下载
if (fileManagerService == null) {
fileManagerService = CommonBeanFactory.getBean(FileManagerService.class);
}
byte[] content = fileManagerService.downloadFile(getRequest(requestId));
if (ArrayUtils.isNotEmpty(content)) {
FileUtils.createFile(path, content);
}
}
/**
* 获取当前jmx 涉及到的文件 执行时
*
* @param tree
*/
public static void getExecuteFiles(HashTree tree, String reportId, List<BodyFile> files) {
if (fileMetadataService == null) {
fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
}
for (Object key : tree.keySet()) {
if (key == null) {
continue;
}
HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) {
dealWithHttp(key, reportId, files);
} else if (key instanceof CSVDataSet) {
dealWithCsv(key, reportId, files);
}
if (node != null) {
getExecuteFiles(node, reportId, files);
}
}
}
private static void dealWithCsv(Object key, String reportId, List<BodyFile> files) {
CSVDataSet source = (CSVDataSet) key;
if (StringUtils.isNotEmpty(source.getPropertyAsString(ElementConstants.FILENAME))) {
BodyFile file = new BodyFile();
file.setId(source.getPropertyAsString(ElementConstants.FILENAME));
file.setName(source.getPropertyAsString(ElementConstants.FILENAME));
if (source.getPropertyAsBoolean(ElementConstants.IS_REF)) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(
source.getPropertyAsString(ElementConstants.FILE_ID));
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
file.setStorage(fileMetadata.getStorage());
file.setFileId(source.getPropertyAsString(ElementConstants.FILE_ID));
String fileName = StringUtils.join(reportId, File.separator, fileMetadata.getName());
file.setName(fileName);
String path = StringUtils.join(
BODY_FILE_DIR,
File.separator,
reportId,
File.separator,
fileMetadata.getName());
((CSVDataSet) key).setProperty(ElementConstants.FILENAME, path);
}
} else if (!new File(source.getPropertyAsString(ElementConstants.FILENAME)).exists()
&& StringUtils.isNotBlank(source.getPropertyAsString(ElementConstants.RESOURCE_ID))) {
// 从MinIO下载
downloadFile(source.getPropertyAsString(ElementConstants.RESOURCE_ID),
source.getPropertyAsString(ElementConstants.FILENAME));
}
files.add(file);
}
}
private static void dealWithHttp(Object key, String reportId, List<BodyFile> files) {
HTTPSamplerProxy source = (HTTPSamplerProxy) key;
if (source == null || source.getHTTPFiles().length == 0) {
return;
}
for (HTTPFileArg httpFileArg : source.getHTTPFiles()) {
BodyFile file = new BodyFile();
file.setId(httpFileArg.getParamName());
file.setName(httpFileArg.getPath());
if (httpFileArg.getPropertyAsBoolean(ElementConstants.IS_REF)) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(
httpFileArg.getPropertyAsString(ElementConstants.FILE_ID));
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
file.setStorage(fileMetadata.getStorage());
file.setFileId(httpFileArg.getPropertyAsString(ElementConstants.FILE_ID));
file.setName(reportId + File.separator + fileMetadata.getName());
String path = StringUtils.join(
BODY_FILE_DIR,
File.separator,
reportId,
File.separator,
fileMetadata.getName());
httpFileArg.setPath(path);
}
} else if (!new File(httpFileArg.getPath()).exists()
&& StringUtils.isNotBlank(httpFileArg.getPropertyAsString(ElementConstants.RESOURCE_ID))) {
// 从MinIO下载
downloadFile(httpFileArg.getPropertyAsString(ElementConstants.RESOURCE_ID), httpFileArg.getPath());
}
files.add(file);
}
}
} }

View File

@ -102,7 +102,7 @@ public class DataFormattingUtil {
try { try {
List<Element> propElementList = parentElement.elements(); List<Element> propElementList = parentElement.elements();
for (Element propElement : propElementList) { for (Element propElement : propElementList) {
if (StringUtils.equals("filename", propElement.attributeValue("name"))) { if (StringUtils.equals(ElementConstants.FILENAME, propElement.attributeValue("name"))) {
String filePath = propElement.getText(); String filePath = propElement.getText();
File file = new File(filePath); File file = new File(filePath);
if (file.exists() && file.isFile()) { if (file.exists() && file.isFile()) {

View File

@ -237,7 +237,7 @@ public class HashTreeUtil {
return; return;
} }
List<BodyFile> files = new LinkedList<>(); List<BodyFile> files = new LinkedList<>();
FileUtils.getExecuteFiles(runRequest.getHashTree(), runRequest.getReportId(), files); ApiFileUtil.getExecuteFiles(runRequest.getHashTree(), runRequest.getReportId(), files);
if (CollectionUtils.isNotEmpty(files)) { if (CollectionUtils.isNotEmpty(files)) {
Map<String, String> repositoryFileMap = new HashMap<>(); Map<String, String> repositoryFileMap = new HashMap<>();
for (BodyFile bodyFile : files) { for (BodyFile bodyFile : files) {

View File

@ -3,6 +3,7 @@ package io.metersphere.commons.utils;
import io.metersphere.api.exec.engine.EngineSourceParser; import io.metersphere.api.exec.engine.EngineSourceParser;
import io.metersphere.api.exec.engine.EngineSourceParserFactory; import io.metersphere.api.exec.engine.EngineSourceParserFactory;
import io.metersphere.base.domain.TestResourcePool; import io.metersphere.base.domain.TestResourcePool;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.config.KafkaProperties; import io.metersphere.config.KafkaProperties;
import io.metersphere.engine.EngineContext; import io.metersphere.engine.EngineContext;
@ -227,7 +228,7 @@ public class JmeterDocumentParser implements EngineSourceParser {
for (Element item : childNodes) { for (Element item : childNodes) {
if (nodeNameEquals(item, STRING_PROP)) { if (nodeNameEquals(item, STRING_PROP)) {
String filenameTag = item.attributeValue("name"); String filenameTag = item.attributeValue("name");
if (StringUtils.equals(filenameTag, "filename")) { if (StringUtils.equals(filenameTag, ElementConstants.FILENAME)) {
// 截取文件名 // 截取文件名
handleFilename(item); handleFilename(item);
// 切割CSV文件 // 切割CSV文件

View File

@ -2,8 +2,8 @@ package io.metersphere.sechedule;
import io.metersphere.api.dto.automation.ExecuteType; import io.metersphere.api.dto.automation.ExecuteType;
import io.metersphere.api.dto.automation.RunScenarioRequest; import io.metersphere.api.dto.automation.RunScenarioRequest;
import io.metersphere.service.scenario.ApiScenarioService;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.ReportTriggerMode; import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.constants.ScheduleGroup;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
@ -11,6 +11,7 @@ import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.dto.RunModeConfigDTO;
import io.metersphere.service.scenario.ApiScenarioService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.quartz.*; import org.quartz.*;
@ -42,7 +43,7 @@ public class ApiScenarioTestJob extends MsScheduleJob {
JobKey jobKey = context.getTrigger().getJobKey(); JobKey jobKey = context.getTrigger().getJobKey();
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
String resourceId = jobDataMap.getString("resourceId"); String resourceId = jobDataMap.getString(ElementConstants.RESOURCE_ID);
this.userId = jobDataMap.getString("userId"); this.userId = jobDataMap.getString("userId");
this.expression = jobDataMap.getString("expression"); this.expression = jobDataMap.getString("expression");
this.projectID = jobDataMap.getString("projectId"); this.projectID = jobDataMap.getString("projectId");

View File

@ -3,11 +3,12 @@ package io.metersphere.sechedule;
import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager; import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.service.definition.ApiDefinitionService;
import io.metersphere.base.domain.SwaggerUrlProject; import io.metersphere.base.domain.SwaggerUrlProject;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.constants.ScheduleGroup;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.service.definition.ApiDefinitionService;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject; import org.json.JSONObject;
@ -28,7 +29,7 @@ public class SwaggerUrlImportJob extends MsScheduleJob {
@Override @Override
protected void businessExecute(JobExecutionContext context) { protected void businessExecute(JobExecutionContext context) {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
String resourceId = jobDataMap.getString("resourceId"); String resourceId = jobDataMap.getString(ElementConstants.RESOURCE_ID);
SwaggerUrlProject swaggerUrlProject = apiDefinitionService.getSwaggerInfo(resourceId); SwaggerUrlProject swaggerUrlProject = apiDefinitionService.getSwaggerInfo(resourceId);
ApiTestImportRequest request = new ApiTestImportRequest(); ApiTestImportRequest request = new ApiTestImportRequest();
// 获取鉴权设置 // 获取鉴权设置

View File

@ -5,21 +5,17 @@ import io.metersphere.api.dto.EnvironmentType;
import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.api.dto.definition.request.MsTestPlan;
import io.metersphere.api.exec.api.ApiCaseSerialService; import io.metersphere.api.exec.api.ApiCaseSerialService;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper; import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper; import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.StorageConstants; import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.*;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.FileInfoDTO; import io.metersphere.dto.FileInfoDTO;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.request.BodyFile; import io.metersphere.request.BodyFile;
import io.metersphere.commons.utils.GenerateHashTreeUtil;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
@ -221,7 +217,7 @@ public class ApiJMeterFileService {
Map<String, byte[]> multipartFiles = new LinkedHashMap<>(); Map<String, byte[]> multipartFiles = new LinkedHashMap<>();
// 获取附件 // 获取附件
List<BodyFile> files = new LinkedList<>(); List<BodyFile> files = new LinkedList<>();
FileUtils.getExecuteFiles(hashTree, reportId, files); ApiFileUtil.getExecuteFiles(hashTree, reportId, files);
if (CollectionUtils.isNotEmpty(files)) { if (CollectionUtils.isNotEmpty(files)) {
Map<String, String> repositoryFileMap = new HashMap<>(); Map<String, String> repositoryFileMap = new HashMap<>();
for (BodyFile bodyFile : files) { for (BodyFile bodyFile : files) {

View File

@ -82,7 +82,7 @@ public class MsHashTreeService {
public static final String CUSTOM_NUM = "customNum"; public static final String CUSTOM_NUM = "customNum";
public static final String SHOW_CUSTOM_NUM = "showCustomNum"; public static final String SHOW_CUSTOM_NUM = "showCustomNum";
public static final String VERSION_ID = "versionId"; public static final String VERSION_ID = "versionId";
public static final String RESOURCE_ID = "resourceId"; public static final String RESOURCE_ID = ElementConstants.RESOURCE_ID;
public void setHashTree(JSONArray hashTree) { public void setHashTree(JSONArray hashTree) {
// 将引用转成复制 // 将引用转成复制

View File

@ -389,9 +389,9 @@ public class ApiDefinitionService {
} }
if (StringUtils.isNotEmpty(request.getSourceId())) { if (StringUtils.isNotEmpty(request.getSourceId())) {
// 检查附件复制出附件 // 检查附件复制出附件
FileUtils.copyBodyFiles(request.getSourceId(), request.getId()); ApiFileUtil.copyBodyFiles(request.getSourceId(), request.getId());
} else { } else {
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles); ApiFileUtil.createBodyFiles(request.getRequest().getId(), bodyFiles);
} }
request.setNewCreate(true); request.setNewCreate(true);
return createTest(request); return createTest(request);
@ -415,7 +415,7 @@ public class ApiDefinitionService {
MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class); MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class);
mockConfigService.updateMockReturnMsgByApi(returnModel); mockConfigService.updateMockReturnMsgByApi(returnModel);
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles); ApiFileUtil.createBodyFiles(request.getRequest().getId(), bodyFiles);
String context = SessionUtils.getUserId().concat(Translator.get("update_api")).concat(":").concat(returnModel.getName()); String context = SessionUtils.getUserId().concat(Translator.get("update_api")).concat(":").concat(returnModel.getName());
BeanMap beanMap = new BeanMap(returnModel); BeanMap beanMap = new BeanMap(returnModel);
Map paramMap = new HashMap<>(beanMap); Map paramMap = new HashMap<>(beanMap);
@ -456,7 +456,7 @@ public class ApiDefinitionService {
customFieldApiService.deleteByResourceId(api.getId()); customFieldApiService.deleteByResourceId(api.getId());
// 删除关系图 // 删除关系图
relationshipEdgeService.delete(api.getId()); relationshipEdgeService.delete(api.getId());
FileUtils.deleteBodyFiles(api.getId()); ApiFileUtil.deleteBodyFiles(api.getId());
deleteFollows(api.getId()); deleteFollows(api.getId());
}); });
// 删除附件关系 // 删除附件关系
@ -1822,7 +1822,7 @@ public class ApiDefinitionService {
api.setUpdateTime(System.currentTimeMillis()); api.setUpdateTime(System.currentTimeMillis());
api.setRefId(api.getId()); api.setRefId(api.getId());
// 检查附件复制出附件 // 检查附件复制出附件
FileUtils.copyBodyFiles(sourceId, api.getId()); ApiFileUtil.copyBodyFiles(sourceId, api.getId());
mapper.insert(api); mapper.insert(api);
if (i % 50 == 0) sqlSession.flushStatements(); if (i % 50 == 0) sqlSession.flushStatements();

View File

@ -47,7 +47,6 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.aspectj.util.FileUtil;
import org.json.JSONObject; import org.json.JSONObject;
import org.mybatis.spring.SqlSessionUtils; import org.mybatis.spring.SqlSessionUtils;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@ -56,7 +55,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -302,9 +300,9 @@ public class ApiTestCaseService {
ApiTestCase test = updateTest(request); ApiTestCase test = updateTest(request);
if (request.getRequest() != null) { if (request.getRequest() != null) {
// requestID 跟接口id 不一致的情况 // requestID 跟接口id 不一致的情况
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles); ApiFileUtil.createBodyFiles(request.getRequest().getId(), bodyFiles);
} else { } else {
FileUtils.createBodyFiles(request.getId(), bodyFiles); ApiFileUtil.createBodyFiles(request.getId(), bodyFiles);
} }
// 发送通知 // 发送通知
ApiCaseBatchSyncService apiCaseBatchSyncService = CommonBeanFactory.getBean(ApiCaseBatchSyncService.class); ApiCaseBatchSyncService apiCaseBatchSyncService = CommonBeanFactory.getBean(ApiCaseBatchSyncService.class);
@ -323,8 +321,7 @@ public class ApiTestCaseService {
deleteFollows(testId); deleteFollows(testId);
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(testId); ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(testId);
if (apiTestCase != null) { if (apiTestCase != null) {
String filePath = StringUtils.join(BODY_FILE_DIR, File.separator, apiTestCase.getId()); ApiFileUtil.deleteBodyFiles(apiTestCase.getId());
FileUtil.deleteContents(new File(filePath));
} }
} }
@ -439,9 +436,9 @@ public class ApiTestCaseService {
private ApiTestCase createTest(SaveApiTestCaseRequest request, List<MultipartFile> bodyFiles) { private ApiTestCase createTest(SaveApiTestCaseRequest request, List<MultipartFile> bodyFiles) {
checkNameExist(request); checkNameExist(request);
FileUtils.createBodyFiles(request.getId(), bodyFiles); ApiFileUtil.createBodyFiles(request.getId(), bodyFiles);
request.setRequest(tcpApiParamService.parseMsTestElement(request.getRequest())); request.setRequest(tcpApiParamService.parseMsTestElement(request.getRequest()));
FileUtils.copyBdyFile(request.getApiDefinitionId(), request.getId()); ApiFileUtil.copyBdyFile(request.getApiDefinitionId(), request.getId());
final ApiTestCaseWithBLOBs test = new ApiTestCaseWithBLOBs(); final ApiTestCaseWithBLOBs test = new ApiTestCaseWithBLOBs();
test.setId(request.getId()); test.setId(request.getId());

View File

@ -48,7 +48,7 @@ public class ApiScenarioReportStructureService {
ElementConstants.IF_CONTROLLER, ElementConstants.IF_CONTROLLER,
ElementConstants.CONSTANT_TIMER ElementConstants.CONSTANT_TIMER
); );
private static final String RESOURCE_ID = "resourceId"; private static final String RESOURCE_ID = ElementConstants.RESOURCE_ID;
private static final String REFERENCED = "referenced"; private static final String REFERENCED = "referenced";
private static final String ERROR_CODE = "errorCode"; private static final String ERROR_CODE = "errorCode";
private static final String ID = "id"; private static final String ID = "id";

View File

@ -304,11 +304,11 @@ public class ApiScenarioService {
} }
private void uploadFiles(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) { private void uploadFiles(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) {
FileUtils.createBodyFiles(request.getScenarioFileIds(), scenarioFiles); ApiFileUtil.createBodyFiles(request.getScenarioFileIds(), scenarioFiles);
List<String> bodyFileRequestIds = request.getBodyFileRequestIds(); List<String> bodyFileRequestIds = request.getBodyFileRequestIds();
if (CollectionUtils.isNotEmpty(bodyFileRequestIds)) { if (CollectionUtils.isNotEmpty(bodyFileRequestIds)) {
bodyFileRequestIds.forEach(requestId -> { bodyFileRequestIds.forEach(requestId -> {
FileUtils.createBodyFiles(requestId, bodyFiles); ApiFileUtil.createBodyFiles(requestId, bodyFiles);
}); });
} }
} }
@ -444,7 +444,7 @@ public class ApiScenarioService {
List<MsHTTPSamplerProxy> oldRequests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement); List<MsHTTPSamplerProxy> oldRequests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement);
oldRequests.forEach(item -> { oldRequests.forEach(item -> {
if (item.isCustomizeReq() && !newRequestIds.contains(item.getId())) { if (item.isCustomizeReq() && !newRequestIds.contains(item.getId())) {
FileUtils.deleteBodyFiles(item.getId()); ApiFileUtil.deleteBodyFiles(item.getId());
} }
}); });
} catch (Exception e) { } catch (Exception e) {
@ -558,7 +558,7 @@ public class ApiScenarioService {
List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement); List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement);
httpSampleFromHashTree.forEach((httpSamplerProxy) -> { httpSampleFromHashTree.forEach((httpSamplerProxy) -> {
if (httpSamplerProxy.isCustomizeReq()) { if (httpSamplerProxy.isCustomizeReq()) {
FileUtils.deleteBodyFiles(httpSamplerProxy.getId()); ApiFileUtil.deleteBodyFiles(httpSamplerProxy.getId());
} }
}); });
} }
@ -831,7 +831,7 @@ public class ApiScenarioService {
public byte[] loadFileAsBytes(FileOperationRequest fileOperationRequest) { public byte[] loadFileAsBytes(FileOperationRequest fileOperationRequest) {
if (fileOperationRequest.getId().contains("/") || fileOperationRequest.getName().contains("/")) if (fileOperationRequest.getId().contains("/") || fileOperationRequest.getName().contains("/"))
MSException.throwException(Translator.get("invalid_parameter")); MSException.throwException(Translator.get("invalid_parameter"));
File file = new File(FileUtils.BODY_FILE_DIR + "/" + fileOperationRequest.getId() + "_" + fileOperationRequest.getName()); File file = new File(ApiFileUtil.BODY_FILE_DIR + "/" + fileOperationRequest.getId() + "_" + fileOperationRequest.getName());
try (FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);) { try (FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);) {
byte[] b = new byte[1000]; byte[] b = new byte[1000];
int n; int n;
@ -889,7 +889,7 @@ public class ApiScenarioService {
if (isUseElement) { if (isUseElement) {
scenario.toHashTree(jmeterHashTree, scenario.getHashTree(), config); scenario.toHashTree(jmeterHashTree, scenario.getHashTree(), config);
ElementUtil.accuracyHashTree(jmeterHashTree); ElementUtil.accuracyHashTree(jmeterHashTree);
repositoryMetadata = FileUtils.getRepositoryFileMetadata(jmeterHashTree); repositoryMetadata = ApiFileUtil.getRepositoryFileMetadata(jmeterHashTree);
jmx = scenario.getJmx(jmeterHashTree); jmx = scenario.getJmx(jmeterHashTree);
} else { } else {
MsThreadGroup group = new MsThreadGroup(); MsThreadGroup group = new MsThreadGroup();
@ -902,7 +902,7 @@ public class ApiScenarioService {
}}); }});
testPlan.getHashTree().add(group); testPlan.getHashTree().add(group);
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), config); testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), config);
repositoryMetadata = FileUtils.getRepositoryFileMetadata(jmeterHashTree); repositoryMetadata = ApiFileUtil.getRepositoryFileMetadata(jmeterHashTree);
jmx = testPlan.getJmx(jmeterHashTree); jmx = testPlan.getJmx(jmeterHashTree);
} }
@ -1654,7 +1654,7 @@ public class ApiScenarioService {
List<String> ids = scenarios.stream().map(ApiScenarioWithBLOBs::getId).collect(Collectors.toList()); List<String> ids = scenarios.stream().map(ApiScenarioWithBLOBs::getId).collect(Collectors.toList());
request.setId(JSON.toJSONString(ids)); request.setId(JSON.toJSONString(ids));
} }
return FileUtils.listBytesToZip(files); return ApiFileUtil.listBytesToZip(files);
} }
private void checkExportEnv(List<ApiScenarioWithBLOBs> scenarios) { private void checkExportEnv(List<ApiScenarioWithBLOBs> scenarios) {
@ -2171,7 +2171,7 @@ public class ApiScenarioService {
object.put("referenced", "Copy"); object.put("referenced", "Copy");
} else { } else {
object.put("id", bloBs.getId()); object.put("id", bloBs.getId());
object.put("resourceId", bloBs.getId()); object.put(ElementConstants.RESOURCE_ID, bloBs.getId());
} }
} }
} }

View File

@ -388,58 +388,6 @@ public class FileUtils {
} }
} }
/**
* 获取当前jmx 涉及到的文件 执行时
*
* @param tree
*/
public static void getExecuteFiles(HashTree tree, String reportId, List<BodyFile> files) {
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
for (Object key : tree.keySet()) {
HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) {
HTTPSamplerProxy source = (HTTPSamplerProxy) key;
if (source != null && source.getHTTPFiles().length > 0) {
for (HTTPFileArg arg : source.getHTTPFiles()) {
BodyFile file = new BodyFile();
file.setId(arg.getParamName());
file.setName(arg.getPath());
if (arg.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(arg.getPropertyAsString("fileId"));
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
file.setStorage(fileMetadata.getStorage());
file.setFileId(arg.getPropertyAsString("fileId"));
file.setName(reportId + File.separator + fileMetadata.getName());
arg.setPath(BODY_FILE_DIR + File.separator + reportId + File.separator + fileMetadata.getName());
}
}
files.add(file);
}
}
} else if (key instanceof CSVDataSet) {
CSVDataSet source = (CSVDataSet) key;
if (source != null && StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) {
BodyFile file = new BodyFile();
file.setId(source.getPropertyAsString("filename"));
file.setName(source.getPropertyAsString("filename"));
if (source.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(source.getPropertyAsString("fileId"));
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
file.setStorage(fileMetadata.getStorage());
file.setFileId(source.getPropertyAsString("fileId"));
file.setName(reportId + File.separator + fileMetadata.getName());
((CSVDataSet) key).setProperty("filename", BODY_FILE_DIR + File.separator + reportId + File.separator + fileMetadata.getName());
}
}
files.add(file);
}
}
if (node != null) {
getExecuteFiles(node, reportId, files);
}
}
}
public static byte[] fileToByte(File tradeFile) { public static byte[] fileToByte(File tradeFile) {
byte[] buffer = null; byte[] buffer = null;
try (FileInputStream fis = new FileInputStream(tradeFile); try (FileInputStream fis = new FileInputStream(tradeFile);