diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index a7ce7091d03e3a6a70eb113f353d46dfa72de8c6..93bd87b009b040be7ad3644f4ce5cf81efd206da 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -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; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index 9aea5e50dc572fb6499fa63f9f14e56c481b9d2c..1969f1d5ccff10c90e0ed14f51a26806c6149c6b 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -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 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 databaseConfigs) { + List 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); diff --git a/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue b/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue index 10ce245cecd11d01dd4f4c41a1dddaee6df4b6cf..b6421bb68f47d37b5c0afff58545dfe377450df5 100644 --- a/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue +++ b/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue @@ -41,11 +41,11 @@ :request="request" :showScript="false" ref="esbDefinition"/> -
-
@@ -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); }, diff --git a/frontend/src/business/components/api/definition/components/request/database/BasisParameters.vue b/frontend/src/business/components/api/definition/components/request/database/BasisParameters.vue index 391c3c911971ca8874dbf061d7558cb300bd15f7..2ec4ec1f371be6d6a45e02ddae5931c5dd930b46 100644 --- a/frontend/src/business/components/api/definition/components/request/database/BasisParameters.vue +++ b/frontend/src/business/components/api/definition/components/request/database/BasisParameters.vue @@ -6,12 +6,12 @@ - + {{ $t('api_test.environment.environment_config') }} @@ -63,24 +63,6 @@
- - - - - - - - - - - - - - - - - - @@ -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() {