refactor(接口测试): 优化前后置SQL脚本选择数据源的逻辑

优化前后置SQL脚本选择数据源的逻辑
This commit is contained in:
song-tianyang 2023-03-31 10:43:37 +08:00 committed by fit2-zhao
parent c8cdd4dcd2
commit d150d41a75
5 changed files with 48 additions and 67 deletions

View File

@ -43,6 +43,7 @@ import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.request.BodyFile; import io.metersphere.request.BodyFile;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -1030,4 +1031,44 @@ public class ElementUtil {
loopController.setEnable(true); loopController.setEnable(true);
return loopController.controller(tree, name); return loopController.controller(tree, name);
} }
public static DatabaseConfig selectDataSourceFromJDBCProcessor(String processorName, String environmentId, String dataSourceId, String projectId, ParameterConfig config) {
if (config == null) {
return null;
}
DatabaseConfig dataSource = null;
// 自选了数据源
if (config.isEffective(projectId) && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getDatabaseConfigs())
&& isDataSource(dataSourceId, config.getConfig().get(projectId).getDatabaseConfigs())) {
EnvironmentConfig environmentConfig = config.getConfig().get(projectId);
if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) {
environmentId = environmentConfig.getEnvironmentId();
}
dataSource = ElementUtil.initDataSource(environmentId, dataSourceId);
if (dataSource == null && CollectionUtils.isNotEmpty(environmentConfig.getDatabaseConfigs())) {
dataSource = environmentConfig.getDatabaseConfigs().get(0);
}
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(projectId) && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getDatabaseConfigs())) {
LoggerUtil.info(processorName + ":开始获取当前环境下默认数据源");
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(projectId, dataSourceId, config.getConfig().get(projectId));
if (dataSourceOrg != null) {
dataSource = dataSourceOrg;
} else {
LoggerUtil.info(processorName + ":获取当前环境下默认数据源结束!未查找到默认数据源");
dataSource = config.getConfig().get(projectId).getDatabaseConfigs().get(0);
}
}
}
return dataSource;
}
private static boolean isDataSource(String dataSourceId, List<DatabaseConfig> databaseConfigs) {
List<String> ids = databaseConfigs.stream().map(DatabaseConfig::getId).collect(Collectors.toList());
if (StringUtils.isNotEmpty(dataSourceId) && ids.contains(dataSourceId)) {
return true;
}
return false;
}
} }

View File

@ -5,7 +5,6 @@ import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
@ -22,7 +21,6 @@ import org.apache.jmeter.protocol.jdbc.processor.JDBCPostProcessor;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* @author song.tianyang * @author song.tianyang
@ -61,28 +59,7 @@ public class MsJDBCPostProcessor extends MsTestElement {
config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId())); config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId()));
} }
// 自选了数据源 this.dataSource = ElementUtil.selectDataSourceFromJDBCProcessor(this.getName(), this.environmentId, this.dataSourceId, this.getProjectId(), config);
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);
LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null));
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
LoggerUtil.info(this.getName() + ":开始获取当前环境下默认数据源");
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
LoggerUtil.info(this.getName() + ":获取当前环境下默认数据源结束!未查找到默认数据源");
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
}
}
if (this.dataSource == null) { if (this.dataSource == null) {
// 用自身的数据 // 用自身的数据
if (StringUtils.isNotEmpty(dataSourceId)) { if (StringUtils.isNotEmpty(dataSourceId)) {
@ -106,14 +83,6 @@ public class MsJDBCPostProcessor extends MsTestElement {
} }
} }
private boolean isDataSource(List<DatabaseConfig> databaseConfigs) {
List<String> ids = databaseConfigs.stream().map(DatabaseConfig::getId).collect(Collectors.toList());
if (StringUtils.isNotEmpty(this.dataSourceId) && ids.contains(this.dataSourceId)) {
return true;
}
return false;
}
private JDBCPostProcessor jdbcPostProcessor(ParameterConfig config) { private JDBCPostProcessor jdbcPostProcessor(ParameterConfig config) {
JDBCPostProcessor jdbcPostProcessor = new JDBCPostProcessor(); JDBCPostProcessor jdbcPostProcessor = new JDBCPostProcessor();
JDBCProcessorVO vo = new JDBCProcessorVO(); JDBCProcessorVO vo = new JDBCProcessorVO();

View File

@ -5,7 +5,6 @@ import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.RequestTypeConstants; import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
@ -23,7 +22,6 @@ import org.apache.jmeter.protocol.jdbc.processor.JDBCPreProcessor;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* @author song.tianyang * @author song.tianyang
@ -60,29 +58,7 @@ public class MsJDBCPreProcessor extends MsTestElement {
this.setProjectId(config.getProjectId()); this.setProjectId(config.getProjectId());
config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId())); config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId()));
} }
this.dataSource = ElementUtil.selectDataSourceFromJDBCProcessor(this.getName(), this.environmentId, this.dataSourceId, this.getProjectId(), config);
// 自选了数据源
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);
LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null));
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
LoggerUtil.info(this.getName() + ":开始获取当前环境下默认数据源");
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
LoggerUtil.info(this.getName() + ":获取当前环境下默认数据源结束!未查找到默认数据源");
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
}
}
if (this.dataSource == null) { if (this.dataSource == null) {
// 用自身的数据 // 用自身的数据
if (StringUtils.isNotEmpty(dataSourceId)) { if (StringUtils.isNotEmpty(dataSourceId)) {
@ -108,14 +84,6 @@ public class MsJDBCPreProcessor extends MsTestElement {
} }
} }
private boolean isDataSource(List<DatabaseConfig> databaseConfigs) {
List<String> ids = databaseConfigs.stream().map(DatabaseConfig::getId).collect(Collectors.toList());
if (StringUtils.isNotEmpty(this.dataSourceId) && ids.contains(this.dataSourceId)) {
return true;
}
return false;
}
private JDBCPreProcessor jdbcPreProcessor(ParameterConfig config) { private JDBCPreProcessor jdbcPreProcessor(ParameterConfig config) {
JDBCPreProcessor jdbcPreProcessor = new JDBCPreProcessor(); JDBCPreProcessor jdbcPreProcessor = new JDBCPreProcessor();
JDBCProcessorVO vo = new JDBCProcessorVO(); JDBCProcessorVO vo = new JDBCProcessorVO();

View File

@ -92,8 +92,9 @@ public class MsJDBCSampler extends MsTestElement {
} }
this.dataSource = null; this.dataSource = null;
envConfig = this.initDataSource(); envConfig = this.initDataSource();
if (dataSource == null && CollectionUtils.isNotEmpty(environmentConfig.getDatabaseConfigs())) {
LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null)); dataSource = environmentConfig.getDatabaseConfigs().get(0);
}
} else { } else {
// 取当前环境下默认的一个数据源 // 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {

View File

@ -34,6 +34,7 @@ import org.apache.jorphan.collections.HashTree;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
@ -86,6 +87,7 @@ public class ApiCaseSerialService {
} }
} }
@Transactional(propagation = Propagation.NOT_SUPPORTED)
protected void updateDefinitionExecResultToRunning(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) { protected void updateDefinitionExecResultToRunning(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) {
ApiDefinitionExecResultWithBLOBs execResult = apiDefinitionExecResultMapper.selectByPrimaryKey(queue.getReportId()); ApiDefinitionExecResultWithBLOBs execResult = apiDefinitionExecResultMapper.selectByPrimaryKey(queue.getReportId());
if (execResult != null) { if (execResult != null) {