refactor(接口测试): 优化sql请求校验数据源为空的逻辑

This commit is contained in:
wxg0103 2023-03-29 14:37:04 +08:00 committed by fit2-zhao
parent c726bc2425
commit d63e74b224
12 changed files with 31 additions and 10 deletions

View File

@ -770,6 +770,9 @@ public class ElementUtil {
public static DatabaseConfig dataSource(String projectId, String dataSourceId, EnvironmentConfig envConfig) { public static DatabaseConfig dataSource(String projectId, String dataSourceId, EnvironmentConfig envConfig) {
try { try {
if (StringUtils.isBlank(dataSourceId)) {
return null;
}
BaseEnvironmentService environmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class); BaseEnvironmentService environmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
List<ApiTestEnvironmentWithBLOBs> environment = environmentService.list(projectId); List<ApiTestEnvironmentWithBLOBs> environment = environmentService.list(projectId);
EnvironmentConfig dataConfig = null; EnvironmentConfig dataConfig = null;

View File

@ -9,9 +9,11 @@ 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;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.commons.vo.JDBCProcessorVO; import io.metersphere.commons.vo.JDBCProcessorVO;
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.utils.LoggerUtil;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -67,13 +69,16 @@ public class MsJDBCPostProcessor extends MsTestElement {
this.environmentId = environmentConfig.getEnvironmentId(); this.environmentId = environmentConfig.getEnvironmentId();
} }
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId); this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null));
} else { } else {
// 取当前环境下默认的一个数据源 // 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { 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())); DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) { if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg; this.dataSource = dataSourceOrg;
} else { } else {
LoggerUtil.info(this.getName() + ":获取当前环境下默认数据源结束!未查找到默认数据源");
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
} }
} }
@ -84,6 +89,7 @@ public class MsJDBCPostProcessor extends MsTestElement {
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId); this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
} }
if (this.dataSource == null) { if (this.dataSource == null) {
LoggerUtil.info(this.getName() + " 当前项目id", this.getProjectId() + " 当前环境配置信息", JSONUtil.toJSONString(config));
String message = "数据源为空请选择数据源"; String message = "数据源为空请选择数据源";
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message); MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
} }

View File

@ -10,9 +10,11 @@ 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;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.commons.vo.JDBCProcessorVO; import io.metersphere.commons.vo.JDBCProcessorVO;
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.utils.LoggerUtil;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -67,13 +69,16 @@ public class MsJDBCPreProcessor extends MsTestElement {
this.environmentId = environmentConfig.getEnvironmentId(); this.environmentId = environmentConfig.getEnvironmentId();
} }
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId); this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null));
} else { } else {
// 取当前环境下默认的一个数据源 // 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { 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())); DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) { if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg; this.dataSource = dataSourceOrg;
} else { } else {
LoggerUtil.info(this.getName() + ":获取当前环境下默认数据源结束!未查找到默认数据源");
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
} }
} }
@ -84,6 +89,7 @@ public class MsJDBCPreProcessor extends MsTestElement {
this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId); this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId);
} }
if (this.dataSource == null) { if (this.dataSource == null) {
LoggerUtil.info(this.getName() + " 当前项目id", this.getProjectId() + " 当前环境配置信息", JSONUtil.toJSONString(config));
String message = "数据源为空请选择数据源"; String message = "数据源为空请选择数据源";
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message); MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
} }

View File

@ -92,16 +92,20 @@ public class MsJDBCSampler extends MsTestElement {
} }
this.dataSource = null; this.dataSource = null;
envConfig = this.initDataSource(); envConfig = this.initDataSource();
LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null));
} else { } else {
// 取当前环境下默认的一个数据源 // 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {
if (config.getConfig().get(this.getProjectId()) != null) { if (config.getConfig().get(this.getProjectId()) != null) {
envConfig = config.getConfig().get(this.getProjectId()); envConfig = config.getConfig().get(this.getProjectId());
if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
LoggerUtil.info(this.getName() + ":开始获取当前环境下默认数据源");
DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, envConfig); DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, envConfig);
if (dataSourceOrg != null) { if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg; this.dataSource = dataSourceOrg;
} else { } else {
LoggerUtil.info(this.getName() + ":获取当前环境下默认数据源结束!未查找到默认数据源");
this.dataSource = envConfig.getDatabaseConfigs().get(0); this.dataSource = envConfig.getDatabaseConfigs().get(0);
} }
} }
@ -110,6 +114,7 @@ public class MsJDBCSampler extends MsTestElement {
} }
} }
if (this.dataSource == null) { if (this.dataSource == null) {
LoggerUtil.info(this.getName() + " 当前项目id", this.getProjectId() + " 当前环境配置信息", JSONUtil.toJSONString(config));
String message = "数据源为空请选择数据源"; String message = "数据源为空请选择数据源";
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message); MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
} }

View File

@ -417,12 +417,13 @@ public class ApiDefinitionImportUtilService {
//这样的过滤规则下可能存在重复接口如果是覆盖模块需要按照去重规则再次去重否则就加上接口原有的模块 //这样的过滤规则下可能存在重复接口如果是覆盖模块需要按照去重规则再次去重否则就加上接口原有的模块
if (fullCoverage) { if (fullCoverage) {
removeHttpChooseModuleRepeat(optionData, chooseModulePath); removeHttpChooseModuleRepeat(optionData, chooseModulePath);
// optionData = singleOptionData;
// optionMap = optionData.stream().collect(Collectors.toMap(t -> t.getName().concat(t.getMethod()).concat(t.getPath()).concat(chooseModulePath), api -> api));
} }
} }
getUrlRepeatOptionMap(optionData, optionMap); getUrlRepeatOptionMap(optionData, optionMap);
repeatDataMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.groupingBy(t -> t.getName().concat(t.getMethod()).concat(t.getPath()).concat(t.getModulePath()))); repeatDataMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.groupingBy(t -> t.getName()
.concat(t.getMethod())
.concat(t.getPath())
.concat(StringUtils.isNotBlank(t.getModulePath()) ? t.getModulePath() : "/未规划用例")));
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap = new HashMap<>(); Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap = new HashMap<>();
//重复接口的case //重复接口的case
if (CollectionUtils.isNotEmpty(repeatApiDefinitionWithBLOBs)) { if (CollectionUtils.isNotEmpty(repeatApiDefinitionWithBLOBs)) {

View File

@ -70,7 +70,7 @@ export default {
removeGoBackListener(this.handleClose); removeGoBackListener(this.handleClose);
}, },
confirm() { confirm() {
let params = this.parameters.split('\n'); let params = this.parameters.split(/[(\r\n)\r\n]+/);
let index = 1; let index = 1;
let isNormal = true; let isNormal = true;
let msg = ''; let msg = '';

View File

@ -15,7 +15,7 @@ export default {
methods: { methods: {
batchSave(data) { batchSave(data) {
if (data) { if (data) {
let params = data.split('\n'); let params = data.split(/[(\r\n)\r\n]+/);
let keyValues = []; let keyValues = [];
params.forEach((item) => { params.forEach((item) => {
let line = item.split(/:|/); let line = item.split(/:|/);

View File

@ -599,7 +599,7 @@ export default {
}, },
batchSave(data) { batchSave(data) {
if (data) { if (data) {
let params = data.split('\n'); let params = data.split(/[(\r\n)\r\n]+/);
let keyValues = []; let keyValues = [];
params.forEach((item) => { params.forEach((item) => {
let line = item.split(/|,/); let line = item.split(/|,/);

View File

@ -60,7 +60,7 @@ export default {
removeGoBackListener(this.handleClose); removeGoBackListener(this.handleClose);
}, },
confirm() { confirm() {
let params = this.parameters.split("\n"); let params = this.parameters.split(/[(\r\n)\r\n]+/);
let index = 1; let index = 1;
let isNormal = true; let isNormal = true;
params.forEach(item => { params.forEach(item => {

View File

@ -265,7 +265,7 @@ export default {
}, },
batchSave(data) { batchSave(data) {
if (data) { if (data) {
let params = data.split("\n"); let params = data.split(/[(\r\n)\r\n]+/);
let keyValues = []; let keyValues = [];
params.forEach(item => { params.forEach(item => {
if (item) { if (item) {

View File

@ -60,7 +60,7 @@ export default {
removeGoBackListener(this.handleClose); removeGoBackListener(this.handleClose);
}, },
confirm() { confirm() {
let params = this.parameters.split("\n"); let params = this.parameters.split(/[(\r\n)\r\n]+/);
let index = 1; let index = 1;
let isNormal = true; let isNormal = true;
params.forEach(item => { params.forEach(item => {

View File

@ -448,7 +448,7 @@ export default {
this.$refs.batchAdd.open(); this.$refs.batchAdd.open();
}, },
_handleBatchVars(data) { _handleBatchVars(data) {
let params = data.split("\n"); let params = data.split(/[(\r\n)\r\n]+/);
let keyValues = []; let keyValues = [];
params.forEach(item => { params.forEach(item => {
if (item) { if (item) {