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

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

上级 195a7158
...@@ -490,9 +490,11 @@ public class MsHTTPSamplerProxy extends MsTestElement { ...@@ -490,9 +490,11 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
// 环境中请求头 // 环境中请求头
Arguments arguments = arguments(httpConfig.getHeaders()); if (httpConfig != null) {
if (arguments != null) { Arguments arguments = arguments(httpConfig.getHeaders());
tree.add(ParameterConfig.valueSupposeMock(arguments)); if (arguments != null) {
tree.add(ParameterConfig.valueSupposeMock(arguments));
}
} }
return httpConfig; return httpConfig;
} }
......
...@@ -32,7 +32,9 @@ import org.apache.jmeter.save.SaveService; ...@@ -32,7 +32,9 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
...@@ -71,9 +73,43 @@ public class MsJDBCSampler extends MsTestElement { ...@@ -71,9 +73,43 @@ public class MsJDBCSampler extends MsTestElement {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.setRefElement(); 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.dataSource = null;
this.initDataSource(); 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) { if (this.dataSource == null) {
MSException.throwException("数据源为空无法执行"); MSException.throwException("数据源为空无法执行");
...@@ -91,6 +127,25 @@ public class MsJDBCSampler extends MsTestElement { ...@@ -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() { private void setRefElement() {
try { try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
......
...@@ -41,11 +41,11 @@ ...@@ -41,11 +41,11 @@
:request="request" :request="request"
:showScript="false" :showScript="false"
ref="esbDefinition"/> 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" :request="request"
:showScript="false"/> :showScript="false"/>
<ms-sql-basis-parameters v-if="request.protocol==='SQL'|| request.type==='JDBCSampler'" <ms-sql-basis-parameters v-if="request.protocol==='SQL'|| request.type==='JDBCSampler'"
:request="request" :request="request" :is-scenario="true" :environment="environment"
:showScript="false"/> :showScript="false"/>
<ms-dubbo-basis-parameters v-if="request.protocol==='DUBBO' || request.protocol==='dubbo://'|| request.type==='DubboSampler'" <ms-dubbo-basis-parameters v-if="request.protocol==='DUBBO' || request.protocol==='dubbo://'|| request.type==='DubboSampler'"
:request="request" :request="request"
...@@ -64,7 +64,7 @@ ...@@ -64,7 +64,7 @@
</el-tabs> </el-tabs>
</div> </div>
<div v-else-if="showXpackCompnent&&request.backEsbDataStruct != null"> <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"/> :show-options-button="false" :show-header="true" :result="request.requestResult"/>
</div> </div>
<div v-else> <div v-else>
...@@ -134,6 +134,7 @@ ...@@ -134,6 +134,7 @@
runData: [], runData: [],
isShowInput: false, isShowInput: false,
showXpackCompnent: false, showXpackCompnent: false,
environment: {},
} }
}, },
created() { created() {
...@@ -162,6 +163,12 @@ ...@@ -162,6 +163,12 @@
if (requireComponent != null && JSON.stringify(esbDefinition) != '{}' && JSON.stringify(esbDefinitionResponse) != '{}') { if (requireComponent != null && JSON.stringify(esbDefinition) != '{}' && JSON.stringify(esbDefinitionResponse) != '{}') {
this.showXpackCompnent = true; this.showXpackCompnent = true;
} }
this.getEnvironments();
},
watch: {
envMap() {
this.getEnvironments();
}
}, },
computed: { computed: {
displayColor() { displayColor() {
...@@ -219,6 +226,13 @@ ...@@ -219,6 +226,13 @@
}, },
}, },
methods: { methods: {
getEnvironments() {
this.environment = {};
let id = this.envMap.get(this.request.projectId);
this.$get('/api/environment/get/' + id, response => {
this.environment = response.data;
});
},
remove() { remove() {
this.$emit('remove', this.request, this.node); this.$emit('remove', this.request, this.node);
}, },
......
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
<el-form :model="request" :rules="rules" ref="request" label-width="100px" :disabled="isReadOnly" style="margin: 10px"> <el-form :model="request" :rules="rules" ref="request" label-width="100px" :disabled="isReadOnly" style="margin: 10px">
<el-row> <el-row>
<el-col :span="8"> <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" <el-select v-model="request.environmentId" size="small" class="ms-htt-width"
:placeholder="$t('api_test.definition.request.run_env')" :placeholder="$t('api_test.definition.request.run_env')"
@change="environmentChange" clearable> @change="environmentChange" clearable>
<el-option v-for="(environment, index) in environments" :key="index" <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"/> :value="environment.id"/>
<el-button class="environment-button" size="small" type="primary" @click="openEnvironmentConfig"> <el-button class="environment-button" size="small" type="primary" @click="openEnvironmentConfig">
{{ $t('api_test.environment.environment_config') }} {{ $t('api_test.environment.environment_config') }}
...@@ -63,24 +63,6 @@ ...@@ -63,24 +63,6 @@
</el-tabs> </el-tabs>
</el-form> </el-form>
</div> </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>
<el-col :span="3" class="ms-left-cell" v-if="showScript"> <el-col :span="3" class="ms-left-cell" v-if="showScript">
...@@ -126,6 +108,11 @@ ...@@ -126,6 +108,11 @@
request: {}, request: {},
basisData: {}, basisData: {},
moduleOptions: Array, moduleOptions: Array,
environment: {},//来自场景选择的环境
isScenario: {
type: Boolean,
default: false,
},
showScript: { showScript: {
type: Boolean, type: Boolean,
default: true, default: true,
...@@ -138,18 +125,24 @@ ...@@ -138,18 +125,24 @@
data() { data() {
return { return {
environments: [], environments: [],
currentEnvironment: {},
databaseConfigsOptions: [], databaseConfigsOptions: [],
isReloadData: false, isReloadData: false,
activeName: "variables", activeName: "variables",
rules: { 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: { watch: {
'request.dataSourceId'() { 'request.dataSourceId'() {
this.setDataSource(); this.setDataSource();
},
environment: {
handler: function () {
this.initDataSource();
},
deep: true
} }
}, },
created() { created() {
...@@ -212,7 +205,6 @@ ...@@ -212,7 +205,6 @@
runTest() { runTest() {
}, },
getEnvironments() { getEnvironments() {
this.environments = []; this.environments = [];
let id = this.request.projectId ? this.request.projectId : this.projectId; let id = this.request.projectId ? this.request.projectId : this.projectId;
...@@ -242,20 +234,35 @@ ...@@ -242,20 +234,35 @@
}, },
initDataSource() { initDataSource() {
let flag = false; let flag = false;
for (let i in this.environments) { if (this.isScenario && this.environment) {
if (this.environments[i].id === this.request.environmentId) { this.request.environmentId = this.environment.id;
this.databaseConfigsOptions = []; this.databaseConfigsOptions = [];
this.environments[i].config.databaseConfigs.forEach(item => { if (this.environment.config) {
if (item.id === this.request.dataSourceId) { let config = JSON.parse(this.environment.config);
flag = true; config.databaseConfigs.forEach(item => {
if (item.id !== this.request.dataSourceId) {
this.request.dataSourceId = item.id;
} }
flag = true;
this.databaseConfigsOptions.push(item); 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) { if (!flag) {
this.request.dataSourceId = undefined; this.request.dataSourceId = "";
} }
}, },
setDataSource() { setDataSource() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册