feat(接口测试): 脚本支持beanshell和jsr223

--story=1010675 --user=赵勇 【接口测试】接口测试前后置脚本支持beanshell处理器 https://www.tapd.cn/55049933/s/1310073
This commit is contained in:
fit2-zhao 2022-12-01 18:43:51 +08:00 committed by fit2-zhao
parent 4df9e8ad47
commit f5172e3748
21 changed files with 482 additions and 738 deletions

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.definition.request;
import io.metersphere.api.dto.EnvironmentType;
import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.controller.MsIfController;
@ -18,16 +19,21 @@ import io.metersphere.api.dto.definition.request.timer.MsConstantTimer;
import io.metersphere.api.dto.definition.request.unknown.MsJmeterElement;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.scenario.environment.item.EnvAssertions;
import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.commons.vo.JDBCProcessorVO;
import io.metersphere.commons.vo.ScriptProcessorVO;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.environment.service.BaseEnvironmentService;
@ -46,8 +52,10 @@ import org.apache.jmeter.config.RandomVariableConfig;
import org.apache.jmeter.modifiers.CounterConfig;
import org.apache.jmeter.modifiers.UserParameters;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.java.sampler.BeanShellSampler;
import org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
@ -67,6 +75,7 @@ public class ElementUtil {
private static final String POST = "POST";
private static final String ASSERTIONS = ElementConstants.ASSERTIONS;
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;
private static final String TEST_BEAN_GUI = "TestBeanGUI";
public static Map<String, EnvironmentConfig> getEnvironmentConfig(String environmentId, String projectId) {
@ -101,7 +110,7 @@ public class ElementUtil {
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setEnabled(true);
csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName());
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName());
csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? StandardCharsets.UTF_8.name() : item.getEncoding());
if (CollectionUtils.isEmpty(item.getFiles())) {
@ -150,7 +159,7 @@ public class ElementUtil {
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setEnabled(true);
csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName());
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName());
csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? StandardCharsets.UTF_8.name() : item.getEncoding());
if (CollectionUtils.isEmpty(item.getFiles())) {
@ -219,7 +228,7 @@ public class ElementUtil {
RandomVariableConfig randomVariableConfig = new RandomVariableConfig();
randomVariableConfig.setEnabled(true);
randomVariableConfig.setProperty(TestElement.TEST_CLASS, RandomVariableConfig.class.getName());
randomVariableConfig.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
randomVariableConfig.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
randomVariableConfig.setName(item.getName());
randomVariableConfig.setProperty("variableName", item.getName());
randomVariableConfig.setProperty("outputFormat", item.getValue());
@ -590,7 +599,7 @@ public class ElementUtil {
return processor;
}
public static void setBaseParams(AbstractTestElement sampler, MsTestElement parent, ParameterConfig config, String id, String indexPath) {
public static void setBaseParams(TestElement sampler, MsTestElement parent, ParameterConfig config, String id, String indexPath) {
sampler.setProperty("MS-ID", id);
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(id, config, parent, indexPath));
}
@ -866,4 +875,127 @@ public class ElementUtil {
public static String getDataSourceName(String name) {
return StringUtils.join(name, "-", UUID.randomUUID().toString());
}
public static void initScript(TestElement testElement, ScriptProcessorVO vo) {
testElement.setEnabled(vo.isEnabled());
if (StringUtils.isNotEmpty(vo.getName())) {
testElement.setName(vo.getName());
} else {
testElement.setName(testElement.getClass().getSimpleName());
}
//替换环境变量
if (StringUtils.isNotEmpty(vo.getScript())) {
vo.setScript(StringUtils.replace(vo.getScript(), RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + vo.getEnvironmentId() + ".\""));
}
testElement.setProperty(TestElement.TEST_CLASS, testElement.getClass().getSimpleName());
testElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
String scriptLanguage = vo.getScriptLanguage();
if (StringUtils.equals(scriptLanguage, "nashornScript")) {
scriptLanguage = "nashorn";
}
if (StringUtils.equalsAny(scriptLanguage, "rhinoScript", "javascript")) {
scriptLanguage = "rhino";
}
testElement.setProperty("scriptLanguage", scriptLanguage);
if (testElement instanceof BeanShellSampler) {
testElement.setProperty("BeanShellSampler.query", vo.getScript());
testElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("BeanShellSamplerGui"));
} else {
testElement.setProperty("scriptLanguage", vo.getScriptLanguage());
testElement.setProperty("script", vo.getScript());
}
}
public static String getScriptEnv(String environmentId, ParameterConfig config) {
if (StringUtils.isEmpty(environmentId)) {
if (config.getConfig() != null) {
if (config.getProjectId() != null && config.getConfig().containsKey(config.getProjectId())) {
return config.getConfig().get(config.getProjectId()).getEnvironmentId();
} else {
if (CollectionUtils.isNotEmpty(config.getConfig().values())) {
Optional<EnvironmentConfig> values = config.getConfig().entrySet().stream().findFirst().map(Map.Entry::getValue);
return values.get().getEnvironmentId();
}
}
}
}
return environmentId;
}
public static void jdbcArguments(String name, List<KeyValue> variables, HashTree tree) {
if (CollectionUtils.isNotEmpty(variables)) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);
name = StringUtils.isNotEmpty(name) ? name : "Arguments";
arguments.setName(name + "JDBC_Argument");
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
variables.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), ElementUtil.getEvlValue(keyValue.getValue()), "=")
);
if (!arguments.getArguments().isEmpty()) {
tree.add(arguments);
}
}
}
public static void jdbcProcessor(AbstractJDBCTestElement jdbcProcessor, ParameterConfig config, JDBCProcessorVO vo) {
jdbcProcessor.setEnabled(vo.isEnable());
jdbcProcessor.setName(vo.getName() == null ? jdbcProcessor.getClass().getSimpleName() : vo.getName());
jdbcProcessor.setProperty(TestElement.TEST_CLASS, jdbcProcessor.getClass().getSimpleName());
jdbcProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
ElementUtil.setBaseParams(jdbcProcessor, vo.getParent(), config, vo.getId(), vo.getIndex());
jdbcProcessor.setDataSource(ElementUtil.getDataSourceName(vo.getDataSource().getName()));
jdbcProcessor.setProperty("dataSource", jdbcProcessor.getDataSource());
jdbcProcessor.setProperty("query", vo.getQuery());
jdbcProcessor.setProperty("queryTimeout", String.valueOf(vo.getQueryTimeout()));
jdbcProcessor.setProperty("resultVariable", vo.getResultVariable());
jdbcProcessor.setProperty("variableNames", vo.getVariableNames());
jdbcProcessor.setProperty("resultSetHandler", "Store as String");
jdbcProcessor.setProperty("queryType", "Callable Statement");
}
public static DataSourceElement jdbcDataSource(String sourceName, DatabaseConfig dataSource) {
DataSourceElement dataSourceElement = new DataSourceElement();
dataSourceElement.setEnabled(true);
dataSourceElement.setName(sourceName + " JDBCDataSource");
dataSourceElement.setProperty(TestElement.TEST_CLASS, DataSourceElement.class.getName());
dataSourceElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
dataSourceElement.setProperty("autocommit", true);
dataSourceElement.setProperty("keepAlive", true);
dataSourceElement.setProperty("preinit", false);
dataSourceElement.setProperty("dataSource", sourceName);
dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl());
dataSourceElement.setProperty("driver", dataSource.getDriver());
dataSourceElement.setProperty("username", dataSource.getUsername());
dataSourceElement.setProperty("password", dataSource.getPassword());
dataSourceElement.setProperty("poolMax", dataSource.getPoolMax());
dataSourceElement.setProperty("timeout", String.valueOf(dataSource.getTimeout()));
dataSourceElement.setProperty("connectionAge", 5000);
dataSourceElement.setProperty("trimInterval", 6000);
dataSourceElement.setProperty("transactionIsolation", "DEFAULT");
return dataSourceElement;
}
public static DatabaseConfig initDataSource(String environmentId, String dataSourceId) {
if (StringUtils.isNotBlank(environmentId) && StringUtils.isNotBlank(dataSourceId)) {
BaseEnvironmentService service = CommonBeanFactory.getBean(BaseEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = service.get(environmentId);
if (environment != null && environment.getConfig() != null) {
EnvironmentConfig envConfig = JSONUtil.parseObject(environment.getConfig(), EnvironmentConfig.class);
if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
List<DatabaseConfig> configs = envConfig.getDatabaseConfigs().stream().filter(item ->
StringUtils.equals(item.getId(), dataSourceId)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(configs)) {
return configs.get(0);
}
}
}
}
return null;
}
}

View File

@ -14,6 +14,7 @@ public class MsAssertionJSR223 extends MsAssertionType {
private String name;
private String script;
private String scriptLanguage;
private Boolean jsrEnable;
public MsAssertionJSR223() {
setType(MsAssertionType.JSR223);

View File

@ -2,16 +2,17 @@ package io.metersphere.api.dto.definition.request.assertions;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.assertions.document.MsAssertionDocument;
import io.metersphere.service.definition.ApiDefinitionService;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.ErrorReportLibraryUtil;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.commons.utils.ErrorReportLibraryUtil;
import io.metersphere.service.definition.ApiDefinitionService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.*;
import org.apache.jmeter.save.SaveService;
@ -196,8 +197,11 @@ public class MsAssertions extends MsTestElement {
return assertion;
}
private JSR223Assertion jsr223Assertion(MsAssertionJSR223 assertionJSR223) {
JSR223Assertion assertion = new JSR223Assertion();
private TestElement jsr223Assertion(MsAssertionJSR223 assertionJSR223) {
TestElement assertion = new BeanShellAssertion();
if (assertionJSR223.getJsrEnable() == null || BooleanUtils.isTrue(assertionJSR223.getJsrEnable())) {
assertion = new JSR223Assertion();
}
assertion.setEnabled(this.isEnable());
if (StringUtils.isNotEmpty(assertionJSR223.getDesc())) {
assertion.setName("JSR223" + delimiter + this.getName() + delimiter + assertionJSR223.getDesc() + delimiterScript + assertionJSR223.getScript());

View File

@ -1,23 +1,24 @@
package io.metersphere.api.dto.definition.request.processors;
import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.shell.filter.ScriptFilter;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.vo.ScriptProcessorVO;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.utils.JMeterVars;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.protocol.java.sampler.BeanShellSampler;
import org.apache.jmeter.protocol.java.sampler.JSR223Sampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Collection;
import java.util.List;
@Data
@ -27,32 +28,15 @@ public class MsJSR223Processor extends MsTestElement {
private String clazzName = MsJSR223Processor.class.getCanonicalName();
private String script;
private String scriptLanguage;
private Boolean jsrEnable;
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script);
ParameterConfig config = (ParameterConfig) msParameter;
//替换Metersphere环境变量
if (StringUtils.isEmpty(this.getEnvironmentId())) {
if (config.getConfig() != null) {
if (config.getProjectId() != null) {
String evnId = config.getConfig().get(config.getProjectId()).getEnvironmentId();
this.setEnvironmentId(evnId);
} else {
Collection<EnvironmentConfig> evnConfigList = config.getConfig().values();
if (evnConfigList != null && !evnConfigList.isEmpty()) {
for (EnvironmentConfig configItem : evnConfigList) {
String evnId = configItem.getEnvironmentId();
this.setEnvironmentId(evnId);
break;
}
}
}
}
}
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\"");
if (config.isOperating()) {
if (StringUtils.isNotEmpty(script) && script.startsWith("io.metersphere.utils.JMeterVars.addVars")) {
if (StringUtils.isNotEmpty(script) && script.startsWith(JMeterVars.class.getCanonicalName())) {
return;
}
}
@ -60,28 +44,20 @@ public class MsJSR223Processor extends MsTestElement {
if (!config.isOperating() && !this.isEnable()) {
return;
}
JSR223Sampler processor = new JSR223Sampler();
processor.setEnabled(this.isEnable());
if (StringUtils.isNotEmpty(this.getName())) {
processor.setName(this.getName());
} else {
processor.setName(ElementConstants.JSR223);
this.setEnvironmentId(ElementUtil.getScriptEnv(this.getEnvironmentId(), config));
TestElement processor = new BeanShellSampler();
if (jsrEnable == null || BooleanUtils.isTrue(jsrEnable)) {
processor = new JSR223Sampler();
}
ScriptProcessorVO vo = new ScriptProcessorVO();
BeanUtils.copyBean(vo, this);
vo.setEnabled(this.isEnable());
ElementUtil.initScript(processor, vo);
String resourceId = StringUtils.isNotEmpty(this.getId()) ? this.getId() : this.getResourceId();
ElementUtil.setBaseParams(processor, this.getParent(), config, resourceId, this.getIndex());
processor.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
processor.setProperty("scriptLanguage", this.getScriptLanguage());
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("nashornScript")) {
processor.setProperty("scriptLanguage", "nashorn");
}
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("rhinoScript")) {
processor.setProperty("scriptLanguage", "rhino");
}
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("javascript")) {
processor.setProperty("scriptLanguage", "rhino");
}
processor.setProperty("script", this.getScript());
final HashTree jsr223PreTree = tree.add(processor);
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {

View File

@ -1,40 +1,23 @@
package io.metersphere.api.dto.definition.request.processors.post;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.commons.constants.CommonConstants;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.vo.JDBCProcessorVO;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.definition.ApiDefinitionService;
import io.metersphere.service.definition.ApiTestCaseService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.protocol.jdbc.processor.JDBCPostProcessor;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import org.json.JSONObject;
import java.util.List;
import java.util.stream.Collectors;
@ -70,42 +53,35 @@ public class MsJDBCPostProcessor extends MsTestElement {
if (!config.isOperating() && !this.isEnable()) {
return;
}
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.setRefElement();
}
if (config.getConfig() == null) {
// 单独接口执行
this.setProjectId(config.getProjectId());
config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId()));
}
//如果当前数据源为null则获取已选环境的数据源
if (this.dataSource == null) {
// 自选了数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())
&& isDataSource(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId());
if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) {
this.environmentId = environmentConfig.getEnvironmentId();
}
this.initDataSource();
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
// 自选了数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())
&& isDataSource(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId());
if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) {
this.environmentId = environmentConfig.getEnvironmentId();
}
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
}
}
if (this.dataSource == null) {
// 用自身的数据
if (StringUtils.isNotEmpty(dataSourceId)) {
this.dataSource = null;
this.initDataSource();
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
}
if (this.dataSource == null) {
String message = "数据源为空请选择数据源";
@ -114,11 +90,9 @@ public class MsJDBCPostProcessor extends MsTestElement {
}
JDBCPostProcessor jdbcPostProcessor = jdbcPostProcessor(config);
final HashTree samplerHashTree = tree.add(jdbcPostProcessor);
tree.add(jdbcDataSource(jdbcPostProcessor.getDataSource()));
Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables());
if (arguments != null) {
tree.add(arguments);
}
tree.add(ElementUtil.jdbcDataSource(jdbcPostProcessor.getDataSource(), this.dataSource));
ElementUtil.jdbcArguments(this.getName(), this.getVariables(), tree);
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {
el.toHashTree(samplerHashTree, el.getHashTree(), config);
@ -134,117 +108,11 @@ public class MsJDBCPostProcessor extends MsTestElement {
return false;
}
private void setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsJDBCPostProcessor proxy = null;
if (StringUtils.equals(this.getRefType(), CommonConstants.CASE)) {
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId());
if (bloBs != null) {
this.setName(bloBs.getName());
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSONUtil.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toString(), new TypeReference<MsJDBCPostProcessor>() {
});
}
} else {
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId());
if (apiDefinition != null) {
this.setProjectId(apiDefinition.getProjectId());
proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference<MsJDBCPostProcessor>() {
});
this.setName(apiDefinition.getName());
}
}
if (proxy != null) {
this.setHashTree(proxy.getHashTree());
this.setDataSource(proxy.getDataSource());
this.setDataSourceId(proxy.getDataSourceId());
this.setQuery(proxy.getQuery());
this.setVariables(proxy.getVariables());
this.setVariableNames(proxy.getVariableNames());
this.setResultVariable(proxy.getResultVariable());
this.setQueryTimeout(proxy.getQueryTimeout());
}
} catch (Exception ex) {
ex.printStackTrace();
LogUtil.error(ex);
}
}
private void initDataSource() {
BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(environmentId);
if (environment != null && environment.getConfig() != null) {
EnvironmentConfig envConfig = JSONUtil.parseObject(environment.getConfig(), EnvironmentConfig.class);
if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
envConfig.getDatabaseConfigs().forEach(item -> {
if (item.getId().equals(this.dataSourceId)) {
this.dataSource = item;
}
});
}
}
}
private Arguments arguments(String name, List<KeyValue> variables) {
if (CollectionUtils.isNotEmpty(variables)) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);
arguments.setName(name + "JDBC_Argument");
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
variables.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), ElementUtil.getEvlValue(keyValue.getValue()), "=")
);
return arguments;
}
return null;
}
private JDBCPostProcessor jdbcPostProcessor(ParameterConfig config) {
JDBCPostProcessor jdbcPostProcessor = new JDBCPostProcessor();
jdbcPostProcessor.setEnabled(this.isEnable());
jdbcPostProcessor.setName(this.getName() == null ? ElementConstants.JDBC_POST : this.getName());
jdbcPostProcessor.setProperty(TestElement.TEST_CLASS, JDBCPostProcessor.class.getName());
jdbcPostProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
ElementUtil.setBaseParams(jdbcPostProcessor, this.getParent(), config, this.getId(), this.getIndex());
// request.getDataSource() 是ID需要转换为Name
jdbcPostProcessor.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName()));
jdbcPostProcessor.setProperty("dataSource", jdbcPostProcessor.getDataSource());
jdbcPostProcessor.setProperty("query", this.getQuery());
jdbcPostProcessor.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));
jdbcPostProcessor.setProperty("resultVariable", this.getResultVariable());
jdbcPostProcessor.setProperty("variableNames", this.getVariableNames());
jdbcPostProcessor.setProperty("resultSetHandler", "Store as String");
jdbcPostProcessor.setProperty("queryType", "Callable Statement");
JDBCProcessorVO vo = new JDBCProcessorVO();
BeanUtils.copyBean(vo, this);
ElementUtil.jdbcProcessor(jdbcPostProcessor, config, vo);
return jdbcPostProcessor;
}
private DataSourceElement jdbcDataSource(String sourceName) {
DataSourceElement dataSourceElement = new DataSourceElement();
dataSourceElement.setEnabled(true);
dataSourceElement.setName(this.getName() + " JDBCDataSource");
dataSourceElement.setProperty(TestElement.TEST_CLASS, DataSourceElement.class.getName());
dataSourceElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
dataSourceElement.setProperty("autocommit", true);
dataSourceElement.setProperty("keepAlive", true);
dataSourceElement.setProperty("preinit", false);
dataSourceElement.setProperty("dataSource", sourceName);
dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl());
dataSourceElement.setProperty("driver", dataSource.getDriver());
dataSourceElement.setProperty("username", dataSource.getUsername());
dataSourceElement.setProperty("password", dataSource.getPassword());
dataSourceElement.setProperty("poolMax", dataSource.getPoolMax());
dataSourceElement.setProperty("timeout", String.valueOf(dataSource.getTimeout()));
dataSourceElement.setProperty("connectionAge", 5000);
dataSourceElement.setProperty("trimInterval", 6000);
dataSourceElement.setProperty("transactionIsolation", "DEFAULT");
return dataSourceElement;
}
}

View File

@ -1,22 +1,24 @@
package io.metersphere.api.dto.definition.request.processors.post;
import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.shell.filter.ScriptFilter;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.vo.ScriptProcessorVO;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.utils.JMeterVars;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.extractor.BeanShellPostProcessor;
import org.apache.jmeter.extractor.JSR223PostProcessor;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Collection;
import java.util.List;
@Data
@ -26,31 +28,14 @@ public class MsJSR223PostProcessor extends MsTestElement {
private String clazzName = MsJSR223PostProcessor.class.getCanonicalName();
private String script;
private String scriptLanguage;
private Boolean jsrEnable;
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script);
ParameterConfig config = (ParameterConfig) msParameter;
if (StringUtils.isEmpty(this.getEnvironmentId())) {
if (config.getConfig() != null) {
if (config.getProjectId() != null && config.getConfig().containsKey(config.getProjectId())) {
String evnId = config.getConfig().get(config.getProjectId()).getEnvironmentId();
this.setEnvironmentId(evnId);
} else {
Collection<EnvironmentConfig> evnConfigList = config.getConfig().values();
if (evnConfigList != null && !evnConfigList.isEmpty()) {
for (EnvironmentConfig configItem : evnConfigList) {
String evnId = configItem.getEnvironmentId();
this.setEnvironmentId(evnId);
break;
}
}
}
}
}
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\"");
ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script);
if (config.isOperating()) {
if (StringUtils.isNotEmpty(script) && script.startsWith("io.metersphere.utils.JMeterVars.addVars")) {
if (StringUtils.isNotEmpty(script) && script.startsWith(JMeterVars.class.getCanonicalName())) {
return;
}
}
@ -58,26 +43,17 @@ public class MsJSR223PostProcessor extends MsTestElement {
if (!config.isOperating() && !this.isEnable()) {
return;
}
JSR223PostProcessor processor = new JSR223PostProcessor();
processor.setEnabled(this.isEnable());
if (StringUtils.isNotEmpty(this.getName())) {
processor.setName(this.getName());
} else {
processor.setName(ElementConstants.JSR223_POST);
this.setEnvironmentId(ElementUtil.getScriptEnv(this.getEnvironmentId(), config));
TestElement processor = new BeanShellPostProcessor();
if (jsrEnable == null || BooleanUtils.isTrue(jsrEnable)) {
processor = new JSR223PostProcessor();
}
processor.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
processor.setProperty("scriptLanguage", this.getScriptLanguage());
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("nashornScript")) {
processor.setProperty("scriptLanguage", "nashorn");
}
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("rhinoScript")) {
processor.setProperty("scriptLanguage", "rhino");
}
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("javascript")) {
processor.setProperty("scriptLanguage", "rhino");
}
processor.setProperty("script", this.getScript());
ScriptProcessorVO vo = new ScriptProcessorVO();
BeanUtils.copyBean(vo, this);
vo.setEnabled(this.isEnable());
ElementUtil.initScript(processor, vo);
final HashTree jsr223PostTree = tree.add(processor);
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {

View File

@ -1,41 +1,24 @@
package io.metersphere.api.dto.definition.request.processors.pre;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.commons.constants.CommonConstants;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.vo.JDBCProcessorVO;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.definition.ApiDefinitionService;
import io.metersphere.service.definition.ApiTestCaseService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.protocol.jdbc.processor.JDBCPreProcessor;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import org.json.JSONObject;
import java.util.List;
import java.util.stream.Collectors;
@ -70,55 +53,48 @@ public class MsJDBCPreProcessor extends MsTestElement {
if (!config.isOperating() && !this.isEnable()) {
return;
}
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.setRefElement();
}
if (config.getConfig() == null) {
// 单独接口执行
this.setProjectId(config.getProjectId());
config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId()));
}
//如果当前数据源为null则获取已选环境的数据源
if (this.dataSource == null) {
// 自选了数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())
&& isDataSource(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId());
if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) {
this.environmentId = environmentConfig.getEnvironmentId();
}
this.initDataSource();
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
// 自选了数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())
&& isDataSource(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId());
if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) {
this.environmentId = environmentConfig.getEnvironmentId();
}
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
}
}
if (this.dataSource == null) {
// 用自身的数据
if (StringUtils.isNotEmpty(dataSourceId)) {
this.dataSource = null;
this.initDataSource();
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
}
if (this.dataSource == null) {
String message = "数据源为空请选择数据源";
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
}
}
JDBCPreProcessor jdbcPreProcessor = jdbcPreProcessor(config);
final HashTree samplerHashTree = tree.add(jdbcPreProcessor);
tree.add(jdbcDataSource(jdbcPreProcessor.getDataSource()));
Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables());
if (arguments != null) {
tree.add(arguments);
}
// 数据源
tree.add(ElementUtil.jdbcDataSource(jdbcPreProcessor.getDataSource(), this.dataSource));
// 参数
ElementUtil.jdbcArguments(this.getName(), this.getVariables(), tree);
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {
el.toHashTree(samplerHashTree, el.getHashTree(), config);
@ -134,117 +110,11 @@ public class MsJDBCPreProcessor extends MsTestElement {
return false;
}
private void setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsJDBCPreProcessor proxy = null;
if (StringUtils.equals(this.getRefType(), CommonConstants.CASE)) {
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId());
if (bloBs != null) {
this.setName(bloBs.getName());
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSONUtil.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toString(), new TypeReference<MsJDBCPreProcessor>() {
});
}
} else {
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId());
if (apiDefinition != null) {
this.setProjectId(apiDefinition.getProjectId());
proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference<MsJDBCPreProcessor>() {
});
this.setName(apiDefinition.getName());
}
}
if (proxy != null) {
this.setHashTree(proxy.getHashTree());
this.setDataSource(proxy.getDataSource());
this.setDataSourceId(proxy.getDataSourceId());
this.setQuery(proxy.getQuery());
this.setVariables(proxy.getVariables());
this.setVariableNames(proxy.getVariableNames());
this.setResultVariable(proxy.getResultVariable());
this.setQueryTimeout(proxy.getQueryTimeout());
}
} catch (Exception ex) {
ex.printStackTrace();
LogUtil.error(ex);
}
}
private void initDataSource() {
BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(environmentId);
if (environment != null && environment.getConfig() != null) {
EnvironmentConfig envConfig = JSONUtil.parseObject(environment.getConfig(), EnvironmentConfig.class);
if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
envConfig.getDatabaseConfigs().forEach(item -> {
if (item.getId().equals(this.dataSourceId)) {
this.dataSource = item;
return;
}
});
}
}
}
private Arguments arguments(String name, List<KeyValue> variables) {
if (CollectionUtils.isNotEmpty(variables)) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);
arguments.setName(name + "JDBC_Argument");
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
variables.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), ElementUtil.getEvlValue(keyValue.getValue()), "=")
);
return arguments;
}
return null;
}
private JDBCPreProcessor jdbcPreProcessor(ParameterConfig config) {
JDBCPreProcessor jdbcPreProcessor = new JDBCPreProcessor();
jdbcPreProcessor.setEnabled(this.isEnable());
jdbcPreProcessor.setName(this.getName() == null ? ElementConstants.JDBC_PRE : this.getName());
jdbcPreProcessor.setProperty(TestElement.TEST_CLASS, JDBCPreProcessor.class.getName());
jdbcPreProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
ElementUtil.setBaseParams(jdbcPreProcessor, this.getParent(), config, this.getId(), this.getIndex());
jdbcPreProcessor.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName()));
jdbcPreProcessor.setProperty("dataSource", jdbcPreProcessor.getDataSource());
jdbcPreProcessor.setProperty("query", this.getQuery());
jdbcPreProcessor.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));
jdbcPreProcessor.setProperty("resultVariable", this.getResultVariable());
jdbcPreProcessor.setProperty("variableNames", this.getVariableNames());
jdbcPreProcessor.setProperty("resultSetHandler", "Store as String");
jdbcPreProcessor.setProperty("queryType", "Callable Statement");
JDBCProcessorVO vo = new JDBCProcessorVO();
BeanUtils.copyBean(vo, this);
ElementUtil.jdbcProcessor(jdbcPreProcessor, config, vo);
return jdbcPreProcessor;
}
private DataSourceElement jdbcDataSource(String sourceName) {
DataSourceElement dataSourceElement = new DataSourceElement();
dataSourceElement.setEnabled(true);
dataSourceElement.setName(this.getName() + " JDBCDataSource");
dataSourceElement.setProperty(TestElement.TEST_CLASS, DataSourceElement.class.getName());
dataSourceElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
dataSourceElement.setProperty("autocommit", true);
dataSourceElement.setProperty("keepAlive", true);
dataSourceElement.setProperty("preinit", false);
dataSourceElement.setProperty("dataSource", sourceName);
dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl());
dataSourceElement.setProperty("driver", dataSource.getDriver());
dataSourceElement.setProperty("username", dataSource.getUsername());
dataSourceElement.setProperty("password", dataSource.getPassword());
dataSourceElement.setProperty("poolMax", dataSource.getPoolMax());
dataSourceElement.setProperty("timeout", String.valueOf(dataSource.getTimeout()));
dataSourceElement.setProperty("connectionAge", 5000);
dataSourceElement.setProperty("trimInterval", 6000);
dataSourceElement.setProperty("transactionIsolation", "DEFAULT");
return dataSourceElement;
}
}

View File

@ -1,23 +1,24 @@
package io.metersphere.api.dto.definition.request.processors.pre;
import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.shell.filter.ScriptFilter;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.vo.ScriptProcessorVO;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.utils.JMeterVars;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.modifiers.BeanShellPreProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Collection;
import java.util.List;
@Data
@ -27,6 +28,7 @@ public class MsJSR223PreProcessor extends MsTestElement {
private String clazzName = MsJSR223PreProcessor.class.getCanonicalName();
private String script;
private String scriptLanguage;
private Boolean jsrEnable;
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
@ -35,34 +37,15 @@ public class MsJSR223PreProcessor extends MsTestElement {
if (!config.isOperating() && !this.isEnable()) {
return;
}
ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script);
if (StringUtils.isEmpty(this.getEnvironmentId())) {
if (config.getConfig() != null) {
if (config.getProjectId() != null) {
String evnId = config.getConfig().get(config.getProjectId()).getEnvironmentId();
this.setEnvironmentId(evnId);
} else {
Collection<EnvironmentConfig> evnConfigList = config.getConfig().values();
if (evnConfigList != null && !evnConfigList.isEmpty()) {
for (EnvironmentConfig configItem : evnConfigList) {
String evnId = configItem.getEnvironmentId();
this.setEnvironmentId(evnId);
break;
}
}
}
}
}
//替换环境变量
if (StringUtils.isNotEmpty(script)) {
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\"");
}
if (config.isOperating()) {
if (StringUtils.isNotEmpty(script) && script.startsWith(JMeterVars.class.getCanonicalName())) {
return;
}
}
final HashTree jsr223PreTree = tree.add(getJSR223PreProcessor());
ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script);
this.setEnvironmentId(ElementUtil.getScriptEnv(this.getEnvironmentId(), config));
final HashTree jsr223PreTree = tree.add(getShellProcessor());
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {
el.toHashTree(jsr223PreTree, el.getHashTree(), config);
@ -70,28 +53,15 @@ public class MsJSR223PreProcessor extends MsTestElement {
}
}
public JSR223PreProcessor getJSR223PreProcessor() {
JSR223PreProcessor processor = new JSR223PreProcessor();
processor.setEnabled(this.isEnable());
if (StringUtils.isNotEmpty(this.getName())) {
processor.setName(this.getName());
} else {
processor.setName(ElementConstants.JSR223_PRE);
public TestElement getShellProcessor() {
TestElement processor = new BeanShellPreProcessor();
if (jsrEnable == null || BooleanUtils.isTrue(jsrEnable)) {
processor = new JSR223PreProcessor();
}
processor.setProperty(TestElement.TEST_CLASS, JSR223PreProcessor.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
/*processor.setProperty("cacheKey", "true");*/
processor.setProperty("scriptLanguage", this.getScriptLanguage());
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("nashornScript")) {
processor.setProperty("scriptLanguage", "nashorn");
}
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("rhinoScript")) {
processor.setProperty("scriptLanguage", "rhino");
}
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("javascript")) {
processor.setProperty("scriptLanguage", "rhino");
}
processor.setProperty("script", this.getScript());
ScriptProcessorVO vo = new ScriptProcessorVO();
BeanUtils.copyBean(vo, this);
vo.setEnabled(this.isEnable());
ElementUtil.initScript(processor, vo);
return processor;
}
}

View File

@ -2,9 +2,6 @@ package io.metersphere.api.dto.definition.request.sampler;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample;
import io.github.ningyu.jmeter.plugin.dubbo.sample.MethodArgument;
import io.github.ningyu.jmeter.plugin.util.Constants;
@ -113,9 +110,6 @@ public class MsDubboSampler extends MsTestElement {
private boolean setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsDubboSampler proxy = null;
if (StringUtils.equals(this.getRefType(), CommonConstants.CASE)) {
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
@ -124,16 +118,15 @@ public class MsDubboSampler extends MsTestElement {
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSONUtil.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toString(), new TypeReference<MsDubboSampler>() {
});
proxy = JSONUtil.parseObject(element.toString(), MsDubboSampler.class);
this.setName(bloBs.getName());
}
} else {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId());
if (apiDefinition != null) {
this.setProjectId(apiDefinition.getProjectId());
proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference<MsDubboSampler>() {
});
proxy = JSONUtil.parseObject(apiDefinition.getRequest(), MsDubboSampler.class);
this.setName(apiDefinition.getName());
}
}

View File

@ -1,8 +1,5 @@
package io.metersphere.api.dto.definition.request.sampler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
@ -250,8 +247,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private boolean setRefElement() {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsHTTPSamplerProxy proxy = null;
if (StringUtils.equals(this.getRefType(), CommonConstants.CASE)) {
ApiTestCaseWithBLOBs bloBs = CommonBeanFactory.getBean(ApiTestCaseService.class).get(this.getId());
@ -259,8 +254,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSONUtil.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toString(), new TypeReference<MsHTTPSamplerProxy>() {
});
proxy = JSONUtil.parseObject(element.toString(), MsHTTPSamplerProxy.class);
this.setName(bloBs.getName());
}
}

View File

@ -1,8 +1,5 @@
package io.metersphere.api.dto.definition.request.sampler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.DatabaseConfig;
@ -17,10 +14,8 @@ import io.metersphere.commons.constants.CommonConstants;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.HashTreeUtil;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.*;
import io.metersphere.commons.vo.JDBCProcessorVO;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
@ -32,10 +27,7 @@ import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import org.json.JSONObject;
@ -123,11 +115,9 @@ public class MsJDBCSampler extends MsTestElement {
}
JDBCSampler jdbcSampler = jdbcSampler(config);
final HashTree samplerHashTree = tree.add(jdbcSampler);
tree.add(jdbcDataSource(jdbcSampler.getDataSource()));
Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables());
if (arguments != null) {
tree.add(arguments);
}
tree.add(ElementUtil.jdbcDataSource(jdbcSampler.getDataSource(), this.dataSource));
ElementUtil.jdbcArguments(this.getName(), this.getVariables(), tree);
// 环境通用请求头
Arguments envArguments = ElementUtil.getConfigArguments(config, this.getName(), this.getProjectId(), null);
if (envArguments != null) {
@ -162,22 +152,8 @@ public class MsJDBCSampler extends MsTestElement {
return false;
}
private String getParentProjectId() {
MsTestElement parent = this.getParent();
while (parent != null) {
if (StringUtils.isNotBlank(parent.getProjectId())) {
return parent.getProjectId();
}
parent = parent.getParent();
}
return "";
}
private boolean setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsJDBCSampler proxy = null;
if (StringUtils.equals(this.getRefType(), CommonConstants.CASE)) {
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
@ -187,15 +163,14 @@ public class MsJDBCSampler extends MsTestElement {
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSONUtil.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toString(), new TypeReference<MsJDBCSampler>() {
});
proxy = JSONUtil.parseObject(element.toString(), MsJDBCSampler.class);
}
} else {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId());
if (apiDefinition != null) {
this.setProjectId(apiDefinition.getProjectId());
proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference<MsJDBCSampler>() {
});
proxy = JSONUtil.parseObject(apiDefinition.getRequest(), MsJDBCSampler.class);
this.setName(apiDefinition.getName());
}
}
@ -230,7 +205,6 @@ public class MsJDBCSampler extends MsTestElement {
envConfig.getDatabaseConfigs().forEach(item -> {
if (item.getId().equals(this.dataSourceId)) {
this.dataSource = item;
return;
}
});
}
@ -238,23 +212,12 @@ public class MsJDBCSampler extends MsTestElement {
return envConfig;
}
private Arguments arguments(String name, List<KeyValue> variables) {
if (CollectionUtils.isNotEmpty(variables)) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);
arguments.setName(name + "JDBC_Argument");
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
variables.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
);
return arguments;
}
return null;
}
private JDBCSampler jdbcSampler(ParameterConfig config) {
JDBCSampler sampler = new JDBCSampler();
JDBCProcessorVO vo = new JDBCProcessorVO();
BeanUtils.copyBean(vo, this);
ElementUtil.jdbcProcessor(sampler, config, vo);
sampler.setEnabled(this.isEnable());
sampler.setName(this.getName());
if (config.isOperating()) {
@ -264,39 +227,6 @@ public class MsJDBCSampler extends MsTestElement {
sampler.setName(testName);
}
}
sampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
ElementUtil.setBaseParams(sampler, this.getParent(), config, this.getId(), this.getIndex());
sampler.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName()));
sampler.setProperty("dataSource", sampler.getDataSource());
sampler.setProperty("query", this.getQuery());
sampler.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));
sampler.setProperty("resultVariable", this.getResultVariable());
sampler.setProperty("variableNames", this.getVariableNames());
sampler.setProperty("resultSetHandler", "Store as String");
sampler.setProperty("queryType", "Callable Statement");
return sampler;
}
private DataSourceElement jdbcDataSource(String dataSourceName) {
DataSourceElement dataSourceElement = new DataSourceElement();
dataSourceElement.setEnabled(true);
dataSourceElement.setName(this.getName() + " JDBCDataSource");
dataSourceElement.setProperty(TestElement.TEST_CLASS, DataSourceElement.class.getName());
dataSourceElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
dataSourceElement.setProperty("autocommit", true);
dataSourceElement.setProperty("keepAlive", true);
dataSourceElement.setProperty("preinit", false);
dataSourceElement.setProperty("dataSource", dataSourceName);
dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl());
dataSourceElement.setProperty("driver", dataSource.getDriver());
dataSourceElement.setProperty("username", dataSource.getUsername());
dataSourceElement.setProperty("password", dataSource.getPassword());
dataSourceElement.setProperty("poolMax", dataSource.getPoolMax());
dataSourceElement.setProperty("timeout", String.valueOf(dataSource.getTimeout()));
dataSourceElement.setProperty("connectionAge", 5000);
dataSourceElement.setProperty("trimInterval", 6000);
dataSourceElement.setProperty("transactionIsolation", "DEFAULT");
return dataSourceElement;
}
}

View File

@ -1,8 +1,5 @@
package io.metersphere.api.dto.definition.request.sampler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.automation.EsbDataStruct;
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
import io.metersphere.api.dto.definition.request.ElementUtil;
@ -151,7 +148,7 @@ public class MsTCPSampler extends MsTestElement {
tree.set(tcpSampler(config), samplerHashTree);
setUserParameters(samplerHashTree);
if (tcpPreProcessor != null && StringUtils.isNotBlank(tcpPreProcessor.getScript())) {
samplerHashTree.add(tcpPreProcessor.getJSR223PreProcessor());
samplerHashTree.add(tcpPreProcessor.getShellProcessor());
}
//增加误报全局断言
HashTreeUtil.addPositive(envConfig, samplerHashTree, config, this.getProjectId());
@ -180,9 +177,6 @@ public class MsTCPSampler extends MsTestElement {
private boolean setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsTCPSampler proxy = null;
if (StringUtils.equals(this.getRefType(), CommonConstants.CASE)) {
ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
@ -192,16 +186,15 @@ public class MsTCPSampler extends MsTestElement {
this.setProjectId(bloBs.getProjectId());
JSONObject element = JSONUtil.parseObject(bloBs.getRequest());
ElementUtil.dataFormatting(element);
proxy = mapper.readValue(element.toString(), new TypeReference<MsTCPSampler>() {
});
proxy = JSONUtil.parseObject(element.toString(), MsTCPSampler.class);
}
} else {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId());
if (apiDefinition != null) {
this.setName(apiDefinition.getName());
this.setProjectId(apiDefinition.getProjectId());
proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference<MsTCPSampler>() {
});
proxy = JSONUtil.parseObject(apiDefinition.getRequest(), MsTCPSampler.class);
}
}
if (proxy != null) {

View File

@ -55,4 +55,6 @@ public class BaseEnvElement {
// 步骤别名
private String stepName;
private Boolean jsrEnable;
}

View File

@ -2,7 +2,6 @@ package io.metersphere.api.exec.api;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.definition.RunCaseRequest;
import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.api.dto.definition.request.ElementUtil;
@ -68,8 +67,6 @@ public class ApiExecuteService {
@Resource
private ExtApiTestCaseMapper extApiTestCaseMapper;
@Resource
private ObjectMapper mapper;
@Resource
private TestPlanApiCaseMapper testPlanApiCaseMapper;
@Resource
private SystemParameterService systemParameterService;
@ -276,8 +273,7 @@ public class ApiExecuteService {
JSONObject elementObj = JSONUtil.parseObject(testCaseWithBLOBs.getRequest());
ElementUtil.dataFormatting(elementObj);
MsTestElement element = mapper.readValue(elementObj.toString(), new TypeReference<MsTestElement>() {
});
MsTestElement element = JSON.parseObject(elementObj.toString(), new TypeReference<MsTestElement>() {});
element.setProjectId(testCaseWithBLOBs.getProjectId());
if (StringUtils.isBlank(request.getEnvironmentId())) {
TestPlanApiCaseExample example = new TestPlanApiCaseExample();

View File

@ -1,10 +1,10 @@
package io.metersphere.api.jmeter;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ExtendedParameter;
import io.metersphere.commons.utils.JSON;
import io.metersphere.dto.ResultDTO;
import io.metersphere.service.ApiExecutionQueueService;
import io.metersphere.service.TestResultService;
@ -22,8 +22,6 @@ public class KafkaListenerTask implements Runnable {
private ConsumerRecord<?, String> record;
private ApiExecutionQueueService apiExecutionQueueService;
private TestResultService testResultService;
private ObjectMapper mapper;
private static final Map<String, String> RUN_MODE_MAP = new HashMap<String, String>() {{
this.put(ApiRunMode.SCHEDULE_API_PLAN.name(), "schedule-task");
this.put(ApiRunMode.JENKINS_API_PLAN.name(), "schedule-task");
@ -99,9 +97,8 @@ public class KafkaListenerTask implements Runnable {
private ResultDTO formatResult() {
try {
// 多态JSON普通转换会丢失内容需要通过 ObjectMapper 获取
if (StringUtils.isNotEmpty(record.value())) {
return mapper.readValue(record.value(), new TypeReference<ResultDTO>() {
return JSON.parseObject(record.value(), new TypeReference<ResultDTO>() {
});
}
} catch (Exception e) {

View File

@ -1,6 +1,5 @@
package io.metersphere.api.jmeter;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.MsgDTO;
import io.metersphere.commons.constants.KafkaTopicConstants;
import io.metersphere.commons.utils.JSONUtil;
@ -30,8 +29,6 @@ public class MsKafkaListener {
private ApiExecutionQueueService apiExecutionQueueService;
@Resource
private TestResultService testResultService;
@Resource
private ObjectMapper mapper;
// 线程池维护线程的最少数量
private final static int CORE_POOL_SIZE = 20;
// 线程池维护线程的最大数量
@ -57,7 +54,6 @@ public class MsKafkaListener {
KafkaListenerTask task = new KafkaListenerTask();
task.setApiExecutionQueueService(apiExecutionQueueService);
task.setTestResultService(testResultService);
task.setMapper(mapper);
task.setRecord(item);
threadPool.execute(task);
this.outKafkaPoolLogger();

View File

@ -0,0 +1,30 @@
package io.metersphere.commons.vo;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.plugin.core.MsTestElement;
import lombok.Data;
import java.util.List;
@Data
public class JDBCProcessorVO {
private boolean enable;
private String name;
private String id;
private String index;
private DatabaseConfig dataSource;
private String query;
private long queryTimeout;
private String resultVariable;
private String variableNames;
private List<KeyValue> variables;
private String environmentId;
private String dataSourceId;
private String protocol = RequestTypeConstants.SQL;
private String useEnvironment;
private MsTestElement parent;
}

View File

@ -0,0 +1,13 @@
package io.metersphere.commons.vo;
import lombok.Data;
@Data
public class ScriptProcessorVO {
private String name;
private String scriptLanguage;
private String script;
private Boolean jsrEnable;
private boolean enabled;
private String environmentId;
}

View File

@ -31,7 +31,6 @@ import io.metersphere.api.parse.scenario.TcpTreeTableDataParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.*;
import io.metersphere.base.mapper.plan.ext.ExtTestPlanApiCaseMapper;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.enums.*;
import io.metersphere.commons.exception.MSException;
@ -158,8 +157,6 @@ public class ApiDefinitionService {
@Resource
private ExtApiScenarioMapper extApiScenarioMapper;
@Resource
private ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper;
@Resource
private ApiCustomFieldService customFieldApiService;
@Resource
private ApiScenarioMapper apiScenarioMapper;
@ -298,51 +295,6 @@ public class ApiDefinitionService {
}
}
public void initDefaultModuleId() {
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria().andModuleIdIsNull();
List<ApiDefinition> updateApiList = apiDefinitionMapper.selectByExample(example);
Map<String, Map<String, List<ApiDefinition>>> projectIdMap = new HashMap<>();
for (ApiDefinition api : updateApiList) {
String projectId = api.getProjectId();
String protocol = api.getProtocol();
if (projectIdMap.containsKey(projectId)) {
if (projectIdMap.get(projectId).containsKey(protocol)) {
projectIdMap.get(projectId).get(protocol).add(api);
} else {
List<ApiDefinition> list = new ArrayList<>();
list.add(api);
projectIdMap.get(projectId).put(protocol, list);
}
} else {
List<ApiDefinition> list = new ArrayList<>();
list.add(api);
Map<String, List<ApiDefinition>> map = new HashMap<>();
map.put(protocol, list);
projectIdMap.put(projectId, map);
}
}
for (Map.Entry<String, Map<String, List<ApiDefinition>>> entry : projectIdMap.entrySet()) {
String projectId = entry.getKey();
Map<String, List<ApiDefinition>> map = entry.getValue();
for (Map.Entry<String, List<ApiDefinition>> itemEntry : map.entrySet()) {
String protocol = itemEntry.getKey();
ApiModule node = apiModuleService.getDefaultNodeUnCreateNew(projectId, protocol);
if (node != null) {
List<ApiDefinition> testCaseList = itemEntry.getValue();
for (ApiDefinition apiDefinition : testCaseList) {
ApiDefinitionWithBLOBs updateCase = new ApiDefinitionWithBLOBs();
updateCase.setId(apiDefinition.getId());
updateCase.setModuleId(node.getId());
updateCase.setModulePath("/" + node.getName());
apiDefinitionMapper.updateByPrimaryKeySelective(updateCase);
}
}
}
}
}
public List<ApiDefinitionResult> listBatch(ApiBatchRequest request) {
ServiceUtils.getSelectAllIds(request, request.getCondition(), (query) -> extApiDefinitionMapper.selectIds(query));
@ -2926,27 +2878,6 @@ public class ApiDefinitionService {
return envIdList;
}
public Map<String, List<String>> getProjectEnvNameByEnvConfig(String projectId, String envConfig) {
Map<String, List<String>> returnMap = new HashMap<>();
RunModeConfigDTO runModeConfigDTO = null;
try {
runModeConfigDTO = JSON.parseObject(envConfig, RunModeConfigDTO.class);
} catch (Exception e) {
LogUtil.error("解析" + envConfig + "为RunModeConfigDTO时失败", e);
}
if (StringUtils.isNotEmpty(projectId) && runModeConfigDTO != null && MapUtils.isNotEmpty(runModeConfigDTO.getEnvMap())) {
String envId = runModeConfigDTO.getEnvMap().get(projectId);
String envName = apiTestEnvironmentService.selectNameById(envId);
Project project = baseProjectService.getProjectById(projectId);
String projectName = project == null ? null : project.getName();
if (StringUtils.isNoneEmpty(envName, projectName)) {
returnMap.put(projectName, new ArrayList<>() {{
this.add(envName);
}});
}
}
return returnMap;
}
public Map<String, List<String>> getProjectEnvNameByEnvConfig(Map<String, List<String>> projectEnvConfigMap) {
Map<String, List<String>> returnMap = new HashMap<>();

View File

@ -25,6 +25,12 @@
:commands="languages"
style="margin-bottom: 5px; margin-left: 15px"
@command="languageChange" />
<el-checkbox
v-model="jsr223ProcessorData.jsrEnable"
style="padding-left: 10px"
:disabled="jsr223ProcessorData.scriptLanguage !== 'beanshell'">
JSR223
</el-checkbox>
<script-nav-menu
ref="scriptNavMenu"
:language="jsr223ProcessorData.scriptLanguage"
@ -164,6 +170,9 @@ export default {
};
},
created() {
if (this.jsr223Processor.jsrEnable === null || this.jsr223Processor.jsrEnable === undefined) {
this.$set(this.jsr223Processor, 'jsrEnable', true);
}
this.jsr223ProcessorData = this.jsr223Processor;
},
props: {
@ -206,6 +215,9 @@ export default {
},
languageChange(language) {
this.jsr223ProcessorData.scriptLanguage = language;
if (language !== 'beanshell') {
this.jsr223ProcessorData.jsrEnable = true;
}
this.$emit('languageChange');
},
addCustomFuncScript(script) {

View File

@ -2,24 +2,52 @@
<div>
<el-row type="flex" :gutter="10">
<el-col :span="codeSpan" class="script-content">
<ms-code-edit v-if="isCodeEditAlive" :mode="codeEditModeMap[jsr223ProcessorData.scriptLanguage]"
:read-only="isReadOnly"
:data.sync="jsr223ProcessorData.script" theme="eclipse" :modes="['java','python']"
ref="codeEdit"/>
<ms-code-edit
v-if="isCodeEditAlive"
:mode="codeEditModeMap[jsr223ProcessorData.scriptLanguage]"
:read-only="isReadOnly"
:data.sync="jsr223ProcessorData.script"
theme="eclipse"
:modes="['java', 'python']"
ref="codeEdit"
/>
</el-col>
<div style="width: 14px;margin-right: 5px;">
<div style="height: 12px;width: 12px; line-height:12px;">
<i :class="showMenu ? 'el-icon-remove-outline' : 'el-icon-circle-plus-outline'"
class="show-menu"
@click="switchMenu"></i>
<div style="width: 14px; margin-right: 5px">
<div style="height: 12px; width: 12px; line-height: 12px">
<i
:class="
showMenu
? 'el-icon-remove-outline'
: 'el-icon-circle-plus-outline'
"
class="show-menu"
@click="switchMenu"
></i>
</div>
</div>
<el-col :span="menuSpan" class="script-index" v-if="isReadOnly !== true">
<ms-dropdown :default-command="jsr223ProcessorData.scriptLanguage" :commands="languages"
style="margin-bottom: 5px;margin-left: 15px;"
@command="languageChange"/>
<script-nav-menu ref="scriptNavMenu" :language="jsr223ProcessorData.scriptLanguage" :menus="codeTemplates"
@handleCode="handleCodeTemplate"/>
<ms-dropdown
:default-command="jsr223ProcessorData.scriptLanguage"
:commands="languages"
style="margin-bottom: 5px; margin-left: 15px"
@command="languageChange"
/>
<el-checkbox
v-model="jsr223ProcessorData.jsrEnable"
style="padding-left: 10px"
:disabled="
jsr223ProcessorData.scriptLanguage &&
jsr223ProcessorData.scriptLanguage !== 'beanshell'
"
>
JSR223
</el-checkbox>
<script-nav-menu
ref="scriptNavMenu"
:language="jsr223ProcessorData.scriptLanguage"
:menus="codeTemplates"
@handleCode="handleCodeTemplate"
/>
</el-col>
</el-row>
</div>
@ -32,135 +60,165 @@ import ScriptNavMenu from "../snippet/ScriptNavMenu";
export default {
name: "Jsr233ProcessorContent",
components: {MsDropdown, MsCodeEdit, ScriptNavMenu},
components: { MsDropdown, MsCodeEdit, ScriptNavMenu },
data() {
return {
jsr223ProcessorData: {},
codeTemplates: [
{
title: this.$t('project.code_segment.api_test'),
title: this.$t("project.code_segment.api_test"),
children: [
{
title: this.$t('project.code_segment.import_api_test'),
title: this.$t("project.code_segment.import_api_test"),
command: "api_definition",
},
{
title: this.$t('project.code_segment.new_api_test'),
title: this.$t("project.code_segment.new_api_test"),
command: "new_api_request",
}
]
},
],
},
{
title: this.$t('project.code_segment.custom_value'),
title: this.$t("project.code_segment.custom_value"),
children: [
{
title: this.$t('api_test.request.processor.code_template_get_variable'),
title: this.$t(
"api_test.request.processor.code_template_get_variable"
),
value: 'vars.get("variable_name")',
},
{
title: this.$t('api_test.request.processor.code_template_set_variable'),
title: this.$t(
"api_test.request.processor.code_template_set_variable"
),
value: 'vars.put("variable_name", "variable_value")',
},
{
title: this.$t('api_test.request.processor.code_template_get_response_header'),
value: 'prev.getResponseHeaders()',
disabled: this.isPreProcessor
title: this.$t(
"api_test.request.processor.code_template_get_response_header"
),
value: "prev.getResponseHeaders()",
disabled: this.isPreProcessor,
},
{
title: this.$t('api_test.request.processor.code_template_get_response_code'),
value: 'prev.getResponseCode()',
disabled: this.isPreProcessor
title: this.$t(
"api_test.request.processor.code_template_get_response_code"
),
value: "prev.getResponseCode()",
disabled: this.isPreProcessor,
},
{
title: this.$t('api_test.request.processor.code_template_get_response_result'),
value: 'prev.getResponseDataAsString()',
disabled: this.isPreProcessor
title: this.$t(
"api_test.request.processor.code_template_get_response_result"
),
value: "prev.getResponseDataAsString()",
disabled: this.isPreProcessor,
},
]
],
},
{
title: this.$t('project.code_segment.project_env'),
title: this.$t("project.code_segment.project_env"),
children: [
{
title: this.$t('api_test.request.processor.param_environment_set_global_variable'),
value: 'vars.put(${__metersphere_env_id}+"key","value");\n' + 'vars.put("key","value")',
title: this.$t(
"api_test.request.processor.param_environment_set_global_variable"
),
value:
'vars.put(${__metersphere_env_id}+"key","value");\n' +
'vars.put("key","value")',
},
]
],
},
{
title: this.$t('project.code_segment.code_segment'),
title: this.$t("project.code_segment.code_segment"),
children: [
{
title: this.$t('project.code_segment.insert_segment'),
title: this.$t("project.code_segment.insert_segment"),
command: "custom_function",
}
]
},
],
},
{
title: this.$t('project.code_segment.exception_handle'),
title: this.$t("project.code_segment.exception_handle"),
children: [
{
title: this.$t('project.code_segment.stop_test'),
value: 'ctx.getEngine().stopThreadNow(ctx.getThread().getThreadName());'
title: this.$t("project.code_segment.stop_test"),
value:
"ctx.getEngine().stopThreadNow(ctx.getThread().getThreadName());",
},
]
],
},
{
title: this.$t('project.code_segment.report_handle'),
title: this.$t("project.code_segment.report_handle"),
hideScript: this.isHideScript(),
children: [
{
title: this.$t('api_test.request.processor.code_add_report_length'),
value: 'String report = ctx.getCurrentSampler().getRequestData();\n' +
'if(report!=null){\n' +
' //补足8位长度前置补0\n' +
title: this.$t(
"api_test.request.processor.code_add_report_length"
),
value:
"String report = ctx.getCurrentSampler().getRequestData();\n" +
"if(report!=null){\n" +
" //补足8位长度前置补0\n" +
' String reportlengthStr = String.format("%08d",report.length());\n' +
' report = reportlengthStr+report;\n' +
' ctx.getCurrentSampler().setRequestData(report);\n' +
'}',
" report = reportlengthStr+report;\n" +
" ctx.getCurrentSampler().setRequestData(report);\n" +
"}",
},
{
title: this.$t('api_test.request.processor.code_hide_report_length'),
value: '//Get response data\n' +
'String returnData = prev.getResponseDataAsString();\n' +
'if(returnData!=null&&returnData.length()>8){\n' +
'//remove 8 report length \n' +
' String subStringData = returnData.substring(8,returnData.length());\n' +
title: this.$t(
"api_test.request.processor.code_hide_report_length"
),
value:
"//Get response data\n" +
"String returnData = prev.getResponseDataAsString();\n" +
"if(returnData!=null&&returnData.length()>8){\n" +
"//remove 8 report length \n" +
" String subStringData = returnData.substring(8,returnData.length());\n" +
' if(subStringData.startsWith("<")){\n' +
' returnData = subStringData;\n' +
' prev.setResponseData(returnData);\n' +
' }\n' +
'}',
" returnData = subStringData;\n" +
" prev.setResponseData(returnData);\n" +
" }\n" +
"}",
disabled: this.isPreProcessor,
},
]
}
],
},
],
isCodeEditAlive: true,
languages: [
'beanshell', "python", "groovy", "javascript"
],
languages: ["beanshell", "python", "groovy", "javascript"],
codeEditModeMap: {
beanshell: 'java',
python: 'python',
groovy: 'java',
nashornScript: 'javascript',
rhinoScript: 'javascript',
javascript: 'javascript',
beanshell: "java",
python: "python",
groovy: "java",
nashornScript: "javascript",
rhinoScript: "javascript",
javascript: "javascript",
},
codeSpan: 20,
menuSpan: 4,
showMenu: true,
}
};
},
created() {
if (
this.jsr223Processor.jsrEnable === null ||
this.jsr223Processor.jsrEnable === undefined
) {
this.$set(this.jsr223Processor, "jsrEnable", true);
}
if (
this.jsr223Processor.scriptLanguage === null ||
this.jsr223Processor.scriptLanguage === undefined
) {
this.$set(this.jsr223Processor, "scriptLanguage", "beanshell");
}
this.jsr223ProcessorData = this.jsr223Processor;
},
props: {
isReadOnly: {
type: Boolean,
default:
false
default: false,
},
jsr223Processor: {
type: Object,
@ -168,15 +226,14 @@ export default {
protocol: String,
isPreProcessor: {
type: Boolean,
default:
false
default: false,
},
node: {},
},
watch: {
jsr223Processor() {
this.reload();
}
},
},
methods: {
addTemplate(template) {
@ -184,25 +241,29 @@ export default {
this.jsr223ProcessorData.script = "";
}
this.jsr223ProcessorData.script += template.value;
if (this.jsr223ProcessorData.scriptLanguage === 'beanshell') {
this.jsr223ProcessorData.script += ';';
if (this.jsr223ProcessorData.scriptLanguage === "beanshell") {
this.jsr223ProcessorData.script += ";";
}
this.reload();
},
isHideScript() {
return this.protocol !== 'TCP';
return this.protocol !== "TCP";
},
reload() {
this.isCodeEditAlive = false;
this.$nextTick(() => (this.isCodeEditAlive = true));
},
languageChange(language) {
if (language !== "beanshell") {
this.jsr223ProcessorData.jsrEnable = true;
}
this.jsr223ProcessorData.scriptLanguage = language;
this.$emit("languageChange");
},
addCustomFuncScript(script) {
this.jsr223ProcessorData.script = this.jsr223ProcessorData.script ?
this.jsr223ProcessorData.script + '\n\n' + script : script;
this.jsr223ProcessorData.script = this.jsr223ProcessorData.script
? this.jsr223ProcessorData.script + "\n\n" + script
: script;
this.reload();
},
switchMenu() {
@ -220,8 +281,8 @@ export default {
this.$refs.codeEdit.insert(code);
}
},
}
}
},
};
</script>
<style scoped>
@ -268,5 +329,4 @@ export default {
.show-menu:hover {
color: #935aa1;
}
</style>