fix(接口测试): 修复接口复制附件丢失问题

--bug=1011224 --user=赵勇 【接口测试】复制接口(请求体中含有文件)-执行复制的接口报错:找不到文件 https://www.tapd.cn/55049933/s/1118072
This commit is contained in:
fit2-zhao 2022-03-15 15:58:02 +08:00 committed by fit2-zhao
parent 82cbad0947
commit cd4a08d4e3
4 changed files with 74 additions and 1 deletions

View File

@ -64,4 +64,6 @@ public class SaveApiDefinitionRequest {
// 创建新版本时用到的
private boolean newVersionRemark;
private boolean newVersionDeps;
// 复制的请求Id
private String sourceId;
}

View File

@ -307,8 +307,13 @@ public class ApiDefinitionService {
if (StringUtils.equals(request.getProtocol(), "DUBBO")) {
request.setMethod("dubbo://");
}
if (StringUtils.isNotEmpty(request.getSourceId())) {
// 检查附件复制出附件
FileUtils.copyBodyFiles(request.getSourceId(), request.getId());
} else {
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles);
}
ApiDefinitionWithBLOBs returnModel = createTest(request);
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles);
return returnModel;
}
@ -1987,6 +1992,7 @@ public class ApiDefinitionService {
try {
for (int i = 0; i < apis.size(); i++) {
ApiDefinitionWithBLOBs api = apis.get(i);
String sourceId = api.getId();
api.setId(UUID.randomUUID().toString());
api.setName(ServiceUtils.getCopyName(api.getName()));
api.setModuleId(request.getModuleId());
@ -1996,6 +2002,9 @@ public class ApiDefinitionService {
api.setCreateTime(System.currentTimeMillis());
api.setUpdateTime(System.currentTimeMillis());
api.setRefId(api.getId());
// 检查附件复制出附件
FileUtils.copyBodyFiles(sourceId, api.getId());
mapper.insert(api);
if (i % 50 == 0)
sqlSession.flushStatements();

View File

@ -5,6 +5,7 @@ import io.metersphere.base.domain.JarConfig;
import io.metersphere.commons.exception.MSException;
import io.metersphere.i18n.Translator;
import io.metersphere.service.JarConfigService;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.CSVDataSet;
@ -118,6 +119,64 @@ public class FileUtils {
}
}
public static void copyBodyFiles(String sourceId, String targetId) {
try {
String sourcePath = BODY_FILE_DIR + "/" + sourceId;
String targetPath = BODY_FILE_DIR + "/" + targetId;
copyFolder(sourcePath, targetPath);
} catch (Exception e) {
LoggerUtil.error(e);
}
}
/**
* 复制文件夹(使用缓冲字节流)
*
* @param sourcePath 源文件夹路径
* @param targetPath 目标文件夹路径
*/
public static void copyFolder(String sourcePath, String targetPath) {
//源文件夹路径
File sourceFile = new File(sourcePath);
//目标文件夹路径
File targetFile = new File(targetPath);
if (!sourceFile.exists() || !sourceFile.isDirectory()) {
return;
}
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File[] files = sourceFile.listFiles();
if (files == null || files.length == 0) {
return;
}
for (File file : files) {
//文件要移动的路径
String movePath = targetFile + File.separator + file.getName();
if (file.isDirectory()) {
//如果是目录则递归调用
copyFolder(file.getAbsolutePath(), movePath);
} else {
//如果是文件则复制文件
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(movePath))) {
byte[] b = new byte[1024];
int temp = 0;
while ((temp = in.read(b)) != -1) {
out.write(b, 0, temp);
}
} catch (Exception e) {
LoggerUtil.error(e);
}
}
}
}
public static File getFileByName(String name) {
String path = BODY_FILE_DIR + "/" + name;
return new File(path);

View File

@ -270,6 +270,7 @@ export default {
this.$success(this.$t('commons.save_success'));
this.reqUrl = "/api/definition/update";
this.currentApi.isCopy = false;
this.currentApi.sourceId = "";
// apiidref_id id
data.id = response.data.id;
data.remark = response.data.remark;
@ -302,7 +303,9 @@ export default {
data.request.protocol = this.currentProtocol;
}
if (data.isCopy) {
data.sourceId = data.id;
data.id = getUUID();
data.request.id = data.id;
} else {
if (data.id) {
data.request.id = data.id;