feat(接口测试): SQL请求步骤多数据源切换

--story=1007191 --user=赵勇 多数据源切换环境逻辑优化 https://www.tapd.cn/55049933/s/1139426
This commit is contained in:
fit2-zhao 2022-04-18 16:22:22 +08:00 committed by 刘瑞斌
parent 04086f6b9e
commit e20a055605
6 changed files with 152 additions and 88 deletions

View File

@ -78,13 +78,16 @@ public class MsJDBCSampler extends MsTestElement {
@JSONField(ordinal = 31)
private boolean customizeReq;
@JSONField(ordinal = 32)
private Boolean isRefEnvironment;
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
ParameterConfig config = (ParameterConfig) msParameter;
// 非导出操作且不是启用状态则跳过执行
if (config != null && !config.isOperating() && !this.isEnable()) {
return;
}else if(config.isOperating() && StringUtils.isNotEmpty(config.getOperatingSampleTestName())){
} else if (config.isOperating() && StringUtils.isNotEmpty(config.getOperatingSampleTestName())) {
this.setName(config.getOperatingSampleTestName());
}
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
@ -121,38 +124,37 @@ public class MsJDBCSampler extends MsTestElement {
}
}
}
EnvironmentConfig envConfig = 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.getApiEnvironmentid())){
this.environmentId = environmentConfig.getApiEnvironmentid();
}
// 自定义请求非引用环境取自身环境
if (StringUtils.equalsIgnoreCase(this.getReferenced(), "Created") && (isRefEnvironment == null || !isRefEnvironment)) {
this.dataSource = null;
envConfig = this.initDataSource();
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId())) {
if (config.getConfig().get(this.getProjectId()) != null) {
envConfig = config.getConfig().get(this.getProjectId());
if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
this.dataSource = envConfig.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.getApiEnvironmentid())) {
this.environmentId = environmentConfig.getApiEnvironmentid();
}
this.dataSource = null;
envConfig = this.initDataSource();
} else {
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId())) {
if (config.getConfig().get(this.getProjectId()) != null) {
envConfig = config.getConfig().get(this.getProjectId());
if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
this.dataSource = envConfig.getDatabaseConfigs().get(0);
}
}
}
}
}
if (this.dataSource == null) {
// 用自身的数据
if (StringUtils.isNotEmpty(dataSourceId)) {
this.dataSource = null;
envConfig = this.initDataSource();
}
if (this.dataSource == null) {
String message = "数据源为空请选择数据源";
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
}
String message = "数据源为空请选择数据源";
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
}
final HashTree samplerHashTree = tree.add(jdbcSampler(config));
tree.add(jdbcDataSource());

View File

@ -170,52 +170,65 @@ export default {
getEnvironments() {
this.environments = [];
let id = this.request.projectId ? this.request.projectId : this.projectId;
this.$get('/api/environment/list/' + id, response => {
let envId = "";
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
}
let targetDataSourceName = "";
let currentEnvironment = {};
this.result = this.$get('/api/environment/list/' + id, response => {
this.environments = response.data;
this.environments.forEach(environment => {
parseEnvironment(environment);
//
if (environment.id === this.request.environmentId && environment.id !== envId) {
if (environment.config && environment.config.databaseConfigs) {
environment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
targetDataSourceName = item.name;
}
});
}
}
if (envId && environment.id === envId) {
currentEnvironment = environment;
this.environments = [currentEnvironment];
}
});
let envId = "";
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
}
this.initDataSource(envId);
this.initDataSource(envId, currentEnvironment, targetDataSourceName);
});
},
openEnvironmentConfig() {
this.$refs.environmentConfig.open(getCurrentProjectID());
},
initDataSource(envId) {
let flag = false;
let environment = {};
initDataSource(envId, currentEnvironment, targetDataSourceName) {
this.databaseConfigsOptions = [];
if (envId) {
this.request.environmentId = envId;
} else {
for (let i in this.environments) {
if (this.environments[i].id === envId && this.environments[i].config && this.environments[i].config.databaseConfigs
&& this.environments[i].config.databaseConfigs.length > 0) {
this.request.environmentId = envId;
this.request.dataSourceId = this.environments[i].config.databaseConfigs[0].id;
if (this.environments[i].id === this.request.environmentId) {
currentEnvironment = this.environments[i];
break;
}
}
}
for (let i in this.environments) {
if (this.environments[i].id === this.request.environmentId) {
environment = this.environments[i];
break;
}
}
this.databaseConfigsOptions = [];
if (environment.config && environment.config.databaseConfigs) {
environment.config.databaseConfigs.forEach(item => {
let flag = false;
if (currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
currentEnvironment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;
}
//
else if (targetDataSourceName && item.name === targetDataSourceName) {
this.request.dataSourceId = item.id;
flag = true;
}
this.databaseConfigsOptions.push(item);
});
if (!flag && environment.config.databaseConfigs.length > 0) {
this.request.dataSourceId = environment.config.databaseConfigs[0].id;
if (!flag && currentEnvironment.config.databaseConfigs.length > 0) {
this.request.dataSourceId = currentEnvironment.config.databaseConfigs[0].id;
flag = true;
}
}

View File

@ -104,6 +104,7 @@
v-if="request.protocol==='SQL'|| request.type==='JDBCSampler'"
:request="request"
:response="response"
:isScenario="true"
:is-read-only="isCompReadOnly"
:showScript="true"/>
@ -627,12 +628,12 @@ export default {
});
}
},
checkPermission(resource, workspaceId, isTurnSpace){
checkPermission(resource, workspaceId, isTurnSpace) {
this.$get('/project/getOwnerProjectIds', res => {
const project = res.data.find(p => p === resource.projectId);
if(!project){
if (!project) {
this.$warning(this.$t('commons.no_permission'));
}else{
} else {
this.gotoTurn(resource, workspaceId, isTurnSpace)
}

View File

@ -144,6 +144,7 @@
v-if="isXpack&&api.method==='ESB'" ref="esbDefinition"/>
<ms-sql-basis-parameters
:showScript="true"
:is-scenario="true"
:request="apiCase.request"
:response="apiCase.responseData"
v-if="api.protocol==='SQL'"/>

View File

@ -22,7 +22,7 @@
</el-row>
<div class="extract-add">
<el-row :gutter="10">
<el-col :span="2">
<el-col :span="4">
<el-select :disabled="isReadOnly" class="extract-item" v-model="type"
:placeholder="$t('api_test.request.extract.select_type')"
size="small">
@ -31,7 +31,7 @@
<el-option label="XPath" :value="options.XPATH"/>
</el-select>
</el-col>
<el-col :span="22">
<el-col :span="20">
<ms-api-extract-common :if-from-variable-advance="ifFromVariableAdvance" :is-read-only="isReadOnly" :extract-type="type" :list="list" v-if="type" :callback="after"/>
</el-col>

View File

@ -1,11 +1,11 @@
<template>
<div v-loading="isReloadData">
<div v-loading="isReloadData || result.loading">
<el-row>
<el-col :span="spanNum" style="padding-bottom: 20px">
<div style="border:1px #DCDFE6 solid; height: 100%;border-radius: 4px ;width: 100% ;">
<el-form :model="request" :rules="rules" ref="request" label-width="100px" :disabled="isReadOnly" style="margin: 10px">
<el-row>
<el-col :span="8">
<el-col :span="7">
<el-form-item prop="environmentId" :label="$t('api_test.definition.request.run_env')">
<el-select v-model="request.environmentId" size="small" class="ms-htt-width"
:placeholder="$t('api_test.definition.request.run_env')"
@ -34,11 +34,18 @@
</el-form-item>
</el-col>
<el-col :span="8">
<el-col :span="7">
<el-form-item :label="$t('api_test.request.sql.timeout')" prop="queryTimeout" style="margin-left: 10px">
<el-input-number :disabled="isReadOnly" size="small" v-model="request.queryTimeout" :placeholder="$t('commons.millisecond')" :max="1000*10000000" :min="0"/>
</el-form-item>
</el-col>
<el-col :span="2">
<el-checkbox
v-if="request.referenced==='Created' && isScenario"
v-model="request.isRefEnvironment" :disabled="request.disabled" class="ref_environment" @change="getEnvironments">
{{ $t('api_test.request.refer_to_environment') }}
</el-checkbox>
</el-col>
</el-row>
@ -141,6 +148,10 @@ export default {
type: Boolean,
default: true,
},
isScenario: {
type: Boolean,
default: false,
},
isReadOnly: {
type: Boolean,
default: false
@ -156,6 +167,7 @@ export default {
isReloadData: false,
activeName: "variables",
rules: {},
result: {}
}
},
watch: {
@ -241,55 +253,86 @@ export default {
runTest() {
},
getEnvironments() {
this.environments = [];
itselfEnvironment() {
let id = this.request.projectId ? this.request.projectId : this.projectId;
this.$get('/api/environment/list/' + id, response => {
this.result = this.$get('/api/environment/list/' + id, response => {
this.environments = response.data;
this.environments.forEach(environment => {
parseEnvironment(environment);
})
this.initDataSource();
});
},
getEnvironments() {
let envId = "";
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
}
if (this.request.referenced === 'Created' && this.isScenario && !this.request.isRefEnvironment) {
this.itselfEnvironment();
return;
} else if (!this.isScenario && !this.request.customizeReq) {
this.itselfEnvironment();
return;
}
this.environments = [];
let id = this.request.projectId ? this.request.projectId : this.projectId;
let targetDataSourceName = "";
let currentEnvironment = {};
this.result = this.$get('/api/environment/list/' + id, response => {
this.environments = response.data;
this.environments.forEach(environment => {
parseEnvironment(environment);
//
if (environment.id === this.request.environmentId && environment.id !== envId) {
if (environment.config && environment.config.databaseConfigs) {
environment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
targetDataSourceName = item.name;
}
});
}
}
if (envId && environment.id === envId) {
currentEnvironment = environment;
this.environments = [currentEnvironment];
}
});
let envId = "";
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
}
this.initDataSource(envId);
this.initDataSource(envId, currentEnvironment, targetDataSourceName);
});
},
openEnvironmentConfig() {
this.$refs.environmentConfig.open(getCurrentProjectID());
},
initDataSource(envId) {
let flag = false;
let environment = {};
initDataSource(envId, currentEnvironment, targetDataSourceName) {
this.databaseConfigsOptions = [];
if (envId) {
this.request.environmentId = envId;
} else {
for (let i in this.environments) {
if (this.environments[i].id === envId && this.environments[i].config && this.environments[i].config.databaseConfigs
&& this.environments[i].config.databaseConfigs.length > 0) {
this.request.environmentId = envId;
this.request.dataSourceId = this.environments[i].config.databaseConfigs[0].id;
if (this.environments[i].id === this.request.environmentId) {
currentEnvironment = this.environments[i];
break;
}
}
}
for (let i in this.environments) {
if (this.environments[i].id === this.request.environmentId) {
environment = this.environments[i];
break;
}
}
this.databaseConfigsOptions = [];
if (environment.config && environment.config.databaseConfigs) {
environment.config.databaseConfigs.forEach(item => {
let flag = false;
if (currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
currentEnvironment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;
}
//
else if (targetDataSourceName && item.name === targetDataSourceName) {
this.request.dataSourceId = item.id;
flag = true;
}
this.databaseConfigsOptions.push(item);
});
if (!flag && environment.config.databaseConfigs.length > 0) {
this.request.dataSourceId = environment.config.databaseConfigs[0].id;
if (!flag && currentEnvironment.config.databaseConfigs.length > 0) {
this.request.dataSourceId = currentEnvironment.config.databaseConfigs[0].id;
flag = true;
}
}
@ -299,7 +342,6 @@ export default {
},
setDataSource() {
this.initDataSource();
for (let item of this.databaseConfigsOptions) {
if (this.request.dataSourceId === item.id) {
this.request.dataSource = item;
@ -357,4 +399,9 @@ export default {
/deep/ .el-form-item {
margin-bottom: 15px;
}
.ref_environment {
margin-top: 13px;
float: right;
}
</style>