From c0fb16571ad5f7e6282dcc1048b369a8b48bdfb2 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 7 Jan 2021 10:41:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E8=A7=A3=E5=86=B3sql=E7=B1=BB=E5=9E=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90=20=E6=97=A0=E6=B3=95=E5=9B=9E=E6=98=BE?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B9=B6=E5=AF=B9=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BF=9B=E8=A1=8C=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/sampler/MsJDBCSampler.java | 30 +++++++++++++++++++ .../service/HistoricalDataUpgradeService.java | 1 + .../components/sampler/jdbc-sampler/index.js | 2 ++ .../request/database/BasisParameters.vue | 1 + 4 files changed, 34 insertions(+) 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 a943d13ccb..d9b3ecd4ae 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 @@ -1,14 +1,21 @@ package io.metersphere.api.dto.definition.request.sampler; +import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONType; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; +import io.metersphere.api.service.ApiTestEnvironmentService; +import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; +import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.CommonBeanFactory; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.jdbc.config.DataSourceElement; import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler; @@ -17,6 +24,7 @@ import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.collections.HashTree; import java.util.List; +import java.util.stream.Collectors; @Data @EqualsAndHashCode(callSuper = true) @@ -40,6 +48,8 @@ public class MsJDBCSampler extends MsTestElement { private String environmentId; @JSONField(ordinal = 27) private Object requestResult; + @JSONField(ordinal = 28) + private String dataSourceId; public void toHashTree(HashTree tree, List hashTree, ParameterConfig config) { if (!this.isEnable()) { @@ -48,6 +58,12 @@ public class MsJDBCSampler extends MsTestElement { if (this.getReferenced() != null && this.getReferenced().equals("REF")) { this.getRefElement(this); } + if (StringUtils.isNotEmpty(dataSourceId)) { + initDataSource(); + } + if (this.dataSource == null) { + MSException.throwException("数据源为空无法执行"); + } final HashTree samplerHashTree = tree.add(jdbcSampler()); tree.add(jdbcDataSource()); tree.add(arguments(this.getName() + " Variables", this.getVariables())); @@ -58,6 +74,20 @@ public class MsJDBCSampler extends MsTestElement { } } + private void initDataSource() { + ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); + ApiTestEnvironmentWithBLOBs environment = environmentService.get(this.dataSourceId); + if (environment != null && environment.getConfig() != null) { + EnvironmentConfig config = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); + if (CollectionUtils.isNotEmpty(config.getDatabaseConfigs())) { + List databaseConfigs = config.getDatabaseConfigs().stream().filter((DatabaseConfig d) -> this.dataSourceId.equals(d.getId())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(databaseConfigs)) { + this.dataSource = databaseConfigs.get(0); + } + } + } + } + private Arguments arguments(String name, List variables) { Arguments arguments = new Arguments(); if (!variables.isEmpty()) { diff --git a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java index cb8aeb52fa..f9b028c29a 100644 --- a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java +++ b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java @@ -134,6 +134,7 @@ public class HistoricalDataUpgradeService { EnvironmentDTO dto = environmentDTOMap.get(request1.getDataSource()); if (dto != null) { ((MsJDBCSampler) element).setEnvironmentId(dto.getEnvironmentId()); + ((MsJDBCSampler) element).setDataSourceId(dto.getDatabaseConfig().getId()); ((MsJDBCSampler) element).setDataSource(dto.getDatabaseConfig()); } element.setType("JDBCSampler"); diff --git a/frontend/src/business/components/api/definition/components/jmeter/components/sampler/jdbc-sampler/index.js b/frontend/src/business/components/api/definition/components/jmeter/components/sampler/jdbc-sampler/index.js index 571925c643..1ae222608b 100644 --- a/frontend/src/business/components/api/definition/components/jmeter/components/sampler/jdbc-sampler/index.js +++ b/frontend/src/business/components/api/definition/components/jmeter/components/sampler/jdbc-sampler/index.js @@ -17,7 +17,9 @@ export default class JDBCSampler extends Sampler { this.type = "JDBCSampler"; this.hashTree = []; this.variables = []; + this.environmentId = undefined; this.dataSource = undefined; + this.dataSourceId = undefined; this.query = undefined; this.queryType = undefined; this.queryArguments = undefined; 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 9402456eee..eead171abd 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 @@ -247,6 +247,7 @@ }, environmentChange(value) { this.request.dataSource = undefined; + this.request.dataSourceId = ""; for (let i in this.environments) { if (this.environments[i].id === value) { this.databaseConfigsOptions = []; From cfa70bb6223379fc95c507d206efaa1fd577b878 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 7 Jan 2021 10:47:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E4=BF=AE=E5=A4=8Dhttp=20=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=8F=82=E6=95=B0=E6=97=A0=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=20=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/definition/request/sampler/MsHTTPSamplerProxy.java | 2 +- .../src/main/java/io/metersphere/api/dto/scenario/KeyValue.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 dbfa8b8173..967bedb446 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 @@ -265,7 +265,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { private Arguments httpArguments(List list) { Arguments arguments = new Arguments(); list.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> { - HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), keyValue.getValue() != null && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue()); + HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue()); httpArgument.setAlwaysEncoded(keyValue.isEncode()); if (StringUtils.isNotBlank(keyValue.getContentType())) { httpArgument.setContentType(keyValue.getContentType()); diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java b/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java index 2d5191ac4e..5a7e978ac6 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java @@ -49,7 +49,7 @@ public class KeyValue { } public boolean isValid() { - return (StringUtils.isNotBlank(name) || StringUtils.isNotBlank(value)) && !StringUtils.equalsIgnoreCase(type, "file"); + return (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) && !StringUtils.equalsIgnoreCase(type, "file"); } public boolean isFile() {