fix(接口测试): 接口自动化上传的jmx中包含csv引用时,路径未自动修改问题修复

This commit is contained in:
fit2-zhao 2021-05-18 19:14:20 +08:00 committed by fit2-zhao
parent 340f69a467
commit 27ecaff4cd
1 changed files with 32 additions and 1 deletions

View File

@ -3,6 +3,8 @@ package io.metersphere.api.dto.definition.request.unknown;
import com.alibaba.fastjson.annotation.JSONType; import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.dto.scenario.request.BodyFile;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
@ -21,6 +23,7 @@ import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* 暂时存放所有未知的Jmeter Element对象 * 暂时存放所有未知的Jmeter Element对象
@ -61,9 +64,16 @@ public class MsJmeterElement extends MsTestElement {
if (!config.isOperating() && scriptWrapper instanceof CSVDataSet) { if (!config.isOperating() && scriptWrapper instanceof CSVDataSet) {
String path = ((CSVDataSet) scriptWrapper).getPropertyAsString("filename"); String path = ((CSVDataSet) scriptWrapper).getPropertyAsString("filename");
if (!new File(path).exists()) { if (!new File(path).exists()) {
// 检查场景变量中的csv文件是否存在
String pathArr[] = path.split("\\/");
String csvPath = this.getCSVPath(config, pathArr[pathArr.length - 1]);
if (StringUtils.isNotEmpty(csvPath)) {
((CSVDataSet) scriptWrapper).setProperty("filename", csvPath);
} 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文件不存在 ]");
} }
} }
}
if (CollectionUtils.isNotEmpty(hashTree)) { if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {
// 给所有孩子加一个父亲标志 // 给所有孩子加一个父亲标志
@ -79,6 +89,27 @@ public class MsJmeterElement extends MsTestElement {
} }
} }
private String getCSVPath(ParameterConfig config, String name) {
if (CollectionUtils.isNotEmpty(config.getVariables())) {
List<ScenarioVariable> list = config.getVariables().stream().filter(ScenarioVariable::isCSVValid).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(list)) {
for (ScenarioVariable item : list) {
if (CollectionUtils.isNotEmpty(item.getFiles())) {
List<String> names = item.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(names) && names.contains(name)) {
if (!config.isOperating() && !new File(FileUtils.BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName()).exists()) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ CSV文件不存在 ]");
} else {
return FileUtils.BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName();
}
}
}
}
}
}
return null;
}
public static InputStream getStrToStream(String sInputString) { public static InputStream getStrToStream(String sInputString) {
if (StringUtils.isNotEmpty(sInputString)) { if (StringUtils.isNotEmpty(sInputString)) {
try { try {