提交 6923eb10 编写于 作者: F fit2-zhao 提交者: fit2-zhao

fix(接口自动化): JDBC 请求数据源选择优化

上级 195a7158
......@@ -490,9 +490,11 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
// 环境中请求头
Arguments arguments = arguments(httpConfig.getHeaders());
if (arguments != null) {
tree.add(ParameterConfig.valueSupposeMock(arguments));
if (httpConfig != null) {
Arguments arguments = arguments(httpConfig.getHeaders());
if (arguments != null) {
tree.add(ParameterConfig.valueSupposeMock(arguments));
}
}
return httpConfig;
}
......
......@@ -32,7 +32,9 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
@Data
@EqualsAndHashCode(callSuper = true)
......@@ -71,9 +73,43 @@ public class MsJDBCSampler extends MsTestElement {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.setRefElement();
}
if (StringUtils.isNotEmpty(dataSourceId)) {
if (config.getConfig() == null) {
// 单独接口执行
this.setProjectId(config.getProjectId());
config.setConfig(getEnvironmentConfig(useEnvironment));
}
// 数据兼容处理
if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) {
// 1.8 之后 当前正常数据
} else if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) {
// 1.8 前后 混合数据
this.setProjectId(getParentProjectId());
} else {
// 1.8 之前 数据
if (config.getConfig() != null) {
if (config.getConfig().containsKey("historyProjectID")) {
this.setProjectId("historyProjectID");
} else {
// 测试计划执行
Iterator<String> it = config.getConfig().keySet().iterator();
if (it.hasNext()) {
this.setProjectId(it.next());
}
}
}
}
// 自选了数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())
&& isDataSource(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
this.dataSource = null;
this.initDataSource();
} else {
this.dataSource = null;
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
}
if (this.dataSource == null) {
MSException.throwException("数据源为空无法执行");
......@@ -91,6 +127,25 @@ public class MsJDBCSampler 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 String getParentProjectId() {
MsTestElement parent = this.getParent();
while (parent != null) {
if (StringUtils.isNotBlank(parent.getProjectId())) {
return parent.getProjectId();
}
parent = parent.getParent();
}
return "";
}
private void setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
......
......@@ -41,11 +41,11 @@
:request="request"
:showScript="false"
ref="esbDefinition"/>
<ms-tcp-basis-parameters v-if="(request.protocol==='TCP'|| request.type==='TCPSampler')&&request.esbDataStruct==null "
<ms-tcp-basis-parameters v-if="(request.protocol==='TCP'|| request.type==='TCPSampler')&& request.esbDataStruct==null "
:request="request"
:showScript="false"/>
<ms-sql-basis-parameters v-if="request.protocol==='SQL'|| request.type==='JDBCSampler'"
:request="request"
:request="request" :is-scenario="true" :environment="environment"
:showScript="false"/>
<ms-dubbo-basis-parameters v-if="request.protocol==='DUBBO' || request.protocol==='dubbo://'|| request.type==='DubboSampler'"
:request="request"
......@@ -64,7 +64,7 @@
</el-tabs>
</div>
<div v-else-if="showXpackCompnent&&request.backEsbDataStruct != null">
<esb-definition-response v-xpack v-if="showXpackCompnent" :currentProtocol="request.protocol" :request="request" :is-api-component="false"
<esb-definition-response v-xpack v-if="showXpackCompnent" :currentProtocol="request.protocol" :request="request" :is-api-component="false"
:show-options-button="false" :show-header="true" :result="request.requestResult"/>
</div>
<div v-else>
......@@ -134,6 +134,7 @@
runData: [],
isShowInput: false,
showXpackCompnent: false,
environment: {},
}
},
created() {
......@@ -162,6 +163,12 @@
if (requireComponent != null && JSON.stringify(esbDefinition) != '{}' && JSON.stringify(esbDefinitionResponse) != '{}') {
this.showXpackCompnent = true;
}
this.getEnvironments();
},
watch: {
envMap() {
this.getEnvironments();
}
},
computed: {
displayColor() {
......@@ -219,6 +226,13 @@
},
},
methods: {
getEnvironments() {
this.environment = {};
let id = this.envMap.get(this.request.projectId);
this.$get('/api/environment/get/' + id, response => {
this.environment = response.data;
});
},
remove() {
this.$emit('remove', this.request, this.node);
},
......
......@@ -6,12 +6,12 @@
<el-form :model="request" :rules="rules" ref="request" label-width="100px" :disabled="isReadOnly" style="margin: 10px">
<el-row>
<el-col :span="8">
<el-form-item prop="environmentId" :label="$t('api_test.definition.request.run_env')">
<el-form-item prop="environmentId" div v-if="!isScenario" :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')"
@change="environmentChange" clearable>
<el-option v-for="(environment, index) in environments" :key="index"
:label="environment.name + (environment.config.httpConfig.socket ? (': ' + environment.config.httpConfig.protocol + '://' + environment.config.httpConfig.socket) : '')"
:label="environment.name"
:value="environment.id"/>
<el-button class="environment-button" size="small" type="primary" @click="openEnvironmentConfig">
{{ $t('api_test.environment.environment_config') }}
......@@ -63,24 +63,6 @@
</el-tabs>
</el-form>
</div>
<!--<div v-if="showScript">-->
<!--<div v-for="row in request.hashTree" :key="row.id" v-loading="isReloadData" style="margin-left: 20px;width: 100%">-->
<!--&lt;!&ndash; 前置脚本 &ndash;&gt;-->
<!--<ms-jsr233-processor v-if="row.label ==='JSR223 PreProcessor'" @copyRow="copyRow" @remove="remove" :is-read-only="false" :title="$t('api_test.definition.request.pre_script')" style-type="color: #B8741A;background-color: #F9F1EA"-->
<!--:jsr223-processor="row"/>-->
<!--&lt;!&ndash;后置脚本&ndash;&gt;-->
<!--<ms-jsr233-processor v-if="row.label ==='JSR223 PostProcessor'" @copyRow="copyRow" @remove="remove" :is-read-only="false" :title="$t('api_test.definition.request.post_script')" style-type="color: #783887;background-color: #F2ECF3"-->
<!--:jsr223-processor="row"/>-->
<!--&lt;!&ndash;断言规则&ndash;&gt;-->
<!--<div style="margin-top: 10px">-->
<!--<ms-api-assertions v-if="row.type==='Assertions'" @copyRow="copyRow" @remove="remove" :is-read-only="isReadOnly" :assertions="row"/>-->
<!--</div>-->
<!--&lt;!&ndash;提取规则&ndash;&gt;-->
<!--<div style="margin-top: 10px">-->
<!--<ms-api-extract :is-read-only="isReadOnly" @copyRow="copyRow" @remove="remove" v-if="row.type==='Extract'" :extract="row"/>-->
<!--</div>-->
<!--</div>-->
<!--</div>-->
</el-col>
<el-col :span="3" class="ms-left-cell" v-if="showScript">
......@@ -126,6 +108,11 @@
request: {},
basisData: {},
moduleOptions: Array,
environment: {},//来自场景选择的环境
isScenario: {
type: Boolean,
default: false,
},
showScript: {
type: Boolean,
default: true,
......@@ -138,18 +125,24 @@
data() {
return {
environments: [],
currentEnvironment: {},
databaseConfigsOptions: [],
isReloadData: false,
activeName: "variables",
rules: {
environmentId: [{required: true, message: this.$t('api_test.definition.request.run_env'), trigger: 'change'}],
dataSourceId: [{required: true, message: this.$t('api_test.request.sql.dataSource'), trigger: 'blur'}],
},
}
},
watch: {
'request.dataSourceId'() {
this.setDataSource();
},
environment: {
handler: function () {
this.initDataSource();
},
deep: true
}
},
created() {
......@@ -212,7 +205,6 @@
runTest() {
},
getEnvironments() {
this.environments = [];
let id = this.request.projectId ? this.request.projectId : this.projectId;
......@@ -242,20 +234,35 @@
},
initDataSource() {
let flag = false;
for (let i in this.environments) {
if (this.environments[i].id === this.request.environmentId) {
this.databaseConfigsOptions = [];
this.environments[i].config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;
if (this.isScenario && this.environment) {
this.request.environmentId = this.environment.id;
this.databaseConfigsOptions = [];
if (this.environment.config) {
let config = JSON.parse(this.environment.config);
config.databaseConfigs.forEach(item => {
if (item.id !== this.request.dataSourceId) {
this.request.dataSourceId = item.id;
}
flag = true;
this.databaseConfigsOptions.push(item);
});
break;
}
} else {
for (let i in this.environments) {
if (this.environments[i].id === this.request.environmentId) {
this.databaseConfigsOptions = [];
this.environments[i].config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;
}
this.databaseConfigsOptions.push(item);
});
break;
}
}
}
if (!flag) {
this.request.dataSourceId = undefined;
this.request.dataSourceId = "";
}
},
setDataSource() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册