This commit is contained in:
shiziyuan9527 2021-01-07 11:10:53 +08:00
commit 555d60fb07
6 changed files with 36 additions and 2 deletions

View File

@ -265,7 +265,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private Arguments httpArguments(List<KeyValue> 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());

View File

@ -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<MsTestElement> 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<DatabaseConfig> 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<KeyValue> variables) {
Arguments arguments = new Arguments();
if (!variables.isEmpty()) {

View File

@ -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() {

View File

@ -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");

View File

@ -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;

View File

@ -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 = [];