fix(接口定义): 修复循环控制器下场景变量csv取值不对的缺陷

--bug=1017388 --user=王孝刚 【接口测试】github#18232,循环控制器+条件控制器组合使用,场景变量csv取值不对
https://www.tapd.cn/55049933/s/1253310
This commit is contained in:
wxg0103 2022-09-27 19:34:26 +08:00 committed by wxg0103
parent 5f3faf63a7
commit 7a2ca8d656
4 changed files with 55 additions and 6 deletions

View File

@ -155,6 +155,56 @@ public class ElementUtil {
}
}
public static void addApiCsvDataSet(HashTree tree, List<ScenarioVariable> variables, ParameterConfig config, String shareMode) {
if (CollectionUtils.isNotEmpty(variables)) {
List<ScenarioVariable> list = variables.stream().filter(ScenarioVariable::isCSVValid).filter(ScenarioVariable::isEnable).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(list)) {
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
list.forEach(item -> {
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setEnabled(true);
csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName());
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName());
csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? "UTF-8" : item.getEncoding());
if (CollectionUtils.isEmpty(item.getFiles())) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
} else {
boolean isRef = false;
String fileId = null;
boolean isRepository = false;
BodyFile file = item.getFiles().get(0);
String path = BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName();
if (StringUtils.equalsIgnoreCase(file.getStorage(), StorageConstants.FILE_REF.name())) {
isRef = true;
fileId = file.getFileId();
if (fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(fileId);
if (fileMetadata != null && StringUtils.equals(fileMetadata.getStorage(), StorageConstants.GIT.name())) {
isRepository = true;
}
}
path = FileUtils.getFilePath(file);
}
if (!config.isOperating() && !isRepository && !new File(path).exists()) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
}
csvDataSet.setProperty("filename", path);
csvDataSet.setProperty("isRef", isRef);
csvDataSet.setProperty("fileId", fileId);
}
csvDataSet.setIgnoreFirstLine(false);
csvDataSet.setProperty("shareMode", shareMode);
csvDataSet.setProperty("recycle", true);
csvDataSet.setProperty("delimiter", item.getDelimiter());
csvDataSet.setProperty("quotedData", item.isQuotedData());
csvDataSet.setComment(StringUtils.isEmpty(item.getDescription()) ? "" : item.getDescription());
tree.add(csvDataSet);
});
}
}
}
public static void addCounter(HashTree tree, List<ScenarioVariable> variables, boolean isInternal) {
if (CollectionUtils.isNotEmpty(variables)) {
List<ScenarioVariable> list = variables.stream().filter(ScenarioVariable::isCounterValid).filter(ScenarioVariable::isEnable).collect(Collectors.toList());
@ -860,10 +910,10 @@ public class ElementUtil {
return null;
}
public static void addOtherVariables(ParameterConfig config, HashTree httpSamplerTree, String projectId) {
public static void addApiVariables(ParameterConfig config, HashTree httpSamplerTree, String projectId) {
if (config.isEffective(projectId) && config.getConfig().get(projectId).getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getCommonConfig().getVariables())) {
ElementUtil.addCsvDataSet(httpSamplerTree, config.getConfig().get(projectId).getCommonConfig().getVariables(), config, "shareMode.group");
ElementUtil.addApiCsvDataSet(httpSamplerTree, config.getConfig().get(projectId).getCommonConfig().getVariables(), config, "shareMode.group");
ElementUtil.addCounter(httpSamplerTree, config.getConfig().get(projectId).getCommonConfig().getVariables(), false);
ElementUtil.addRandom(httpSamplerTree, config.getConfig().get(projectId).getCommonConfig().getVariables());
}

View File

@ -49,7 +49,6 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
@ -232,7 +231,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
httpSamplerTree.add(arguments);
}
//添加csv
ElementUtil.addOtherVariables(config, httpSamplerTree, this.getProjectId());
ElementUtil.addApiVariables(config, httpSamplerTree, this.getProjectId());
//判断是否要开启DNS
if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) {

View File

@ -175,7 +175,7 @@ public class MsJDBCSampler extends MsTestElement {
tree.add(envArguments);
}
//添加csv
ElementUtil.addOtherVariables(config, tree, this.getProjectId());
ElementUtil.addApiVariables(config, tree, this.getProjectId());
//增加误报全局断言
HashTreeUtil.addPositive(envConfig, samplerHashTree, config, this.getProjectId());

View File

@ -176,7 +176,7 @@ public class MsTCPSampler extends MsTestElement {
tree.add(arguments);
}
//添加csv
ElementUtil.addOtherVariables(config, tree, this.getProjectId());
ElementUtil.addApiVariables(config, tree, this.getProjectId());
final HashTree samplerHashTree = new ListedHashTree();
samplerHashTree.add(tcpConfig());
tree.set(tcpSampler(config), samplerHashTree);