diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParserHelper.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParserHelper.java index 0da2c1b58c..d161254ff7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParserHelper.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParserHelper.java @@ -1,61 +1,63 @@ package io.metersphere.api.parser.jmeter; +import io.metersphere.api.parser.jmeter.constants.JmeterAlias; import io.metersphere.jmeter.mock.Mock; +import io.metersphere.project.api.KeyValueParam; import io.metersphere.project.dto.environment.variables.CommonVariables; -import org.apache.commons.lang3.BooleanUtils; +import io.metersphere.sdk.util.BeanUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.jmeter.config.Argument; -import org.apache.jmeter.config.Arguments; -import org.apache.jmeter.save.SaveService; +import org.apache.jmeter.modifiers.UserParameters; import org.apache.jmeter.testelement.TestElement; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; -import static io.metersphere.api.parser.jmeter.constants.JmeterAlias.ARGUMENTS_PANEL; - /** * @Author: jianxing * @CreateTime: 2024-03-05 21:06 */ public class JmeterTestElementParserHelper { - public static Arguments getArguments(String name) { - Arguments arguments = new Arguments(); - arguments.setEnabled(true); - arguments.setName(name + "_Arguments"); - arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName()); - arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(ARGUMENTS_PANEL)); + public static UserParameters getUserParameters(String name) { + UserParameters userParameters = new UserParameters(); + userParameters.setEnabled(true); + userParameters.setName(name + "_User Parameters"); + userParameters.setPerIteration(true); + userParameters.setProperty(TestElement.TEST_CLASS, UserParameters.class.getName()); + userParameters.setProperty(TestElement.GUI_CLASS, JmeterAlias.USER_PARAMETERS_GUI); + return userParameters; + } + + public static UserParameters getUserParameters(String name, List argumentList) { + UserParameters arguments = getUserParameters(name); + List names = new LinkedList<>(); + List values = new LinkedList<>(); + List threadValues = new LinkedList<>(); + for (int i = 0; i < argumentList.size(); ++i) { + String value = argumentList.get(i).getValue(); + String key = argumentList.get(i).getKey(); + names.add(key); + values.add(Mock.buildFunctionCallString(value).replaceAll("[\r\n]", "")); + } + arguments.setNames(names); + threadValues.add(values); + arguments.setThreadLists(threadValues); return arguments; } - public static Arguments getArguments(String name, List argumentList) { - Arguments arguments = getArguments(name); - parse2ArgumentList(argumentList).forEach(arguments::addArgument); - return arguments; - } - - public static List parse2ArgumentList(List variables) { - List arguments = new ArrayList<>(variables.size()); - variables.forEach(variable -> { - if (BooleanUtils.isFalse(variable.getEnable())) { - return; - } - if (variable.isConstantValid()) { - // 处理常量 - String value = StringUtils.isBlank(variable.getValue()) ? variable.getValue() - : Mock.buildFunctionCallString(variable.getValue()).replaceAll("[\r\n]", ""); - arguments.add(new Argument(variable.getKey(), value, "=")); - } else if (variable.isListValid()) { - // 处理 List 变量 - String[] arrays = variable.getValue().replaceAll("[\r\n]", "").split(","); - for (int i = 0; i < arrays.length; i++) { - arguments.add(new Argument(variable.getKey() + "_" + (i + 1), arrays[i], "=")); - } + public static UserParameters getUserParameters(List constantVariables, List listVariables) { + List variableResults = new ArrayList<>(); + listVariables.forEach(listVariable -> { + String[] arrays = listVariable.getValue().replaceAll("[\r\n]", "").split(","); + for (int i = 0; i < arrays.length; i++) { + CommonVariables commonVariables = BeanUtils.copyBean(new CommonVariables(), listVariable); + commonVariables.setKey(listVariable.getKey() + "_" + (i + 1)); + commonVariables.setValue(arrays[i]); + variableResults.add(commonVariables); } }); - return arguments; + variableResults.addAll(constantVariables); + return getUserParameters(StringUtils.EMPTY, variableResults); } - - } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java index cc2b94f0a5..416a2016fb 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java @@ -4,9 +4,6 @@ package io.metersphere.api.parser.jmeter; import io.metersphere.api.constants.ApiConstants; import io.metersphere.api.dto.ApiParamConfig; import io.metersphere.api.dto.request.http.*; -import io.metersphere.project.dto.environment.auth.BasicAuth; -import io.metersphere.project.dto.environment.auth.DigestAuth; -import io.metersphere.project.dto.environment.auth.HTTPAuthConfig; import io.metersphere.api.dto.request.http.body.Body; import io.metersphere.api.parser.jmeter.body.MsBodyConverter; import io.metersphere.api.parser.jmeter.body.MsBodyConverterFactory; @@ -19,6 +16,9 @@ import io.metersphere.project.api.KeyValueEnableParam; import io.metersphere.project.api.KeyValueParam; import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.GlobalParams; +import io.metersphere.project.dto.environment.auth.BasicAuth; +import io.metersphere.project.dto.environment.auth.DigestAuth; +import io.metersphere.project.dto.environment.auth.HTTPAuthConfig; import io.metersphere.project.dto.environment.host.Host; import io.metersphere.project.dto.environment.http.HttpConfig; import io.metersphere.project.dto.environment.http.HttpConfigPathMatchRule; @@ -29,8 +29,11 @@ import io.metersphere.sdk.util.LogUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.jmeter.config.Arguments; -import org.apache.jmeter.protocol.http.control.*; +import org.apache.jmeter.modifiers.UserParameters; +import org.apache.jmeter.protocol.http.control.AuthManager; +import org.apache.jmeter.protocol.http.control.Authorization; +import org.apache.jmeter.protocol.http.control.DNSCacheManager; +import org.apache.jmeter.protocol.http.control.HeaderManager; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; @@ -89,8 +92,8 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter commonVariables = getCommonVariables(msScenario.getScenarioConfig()); @@ -132,9 +158,8 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter