refactor(接口测试): 重构基本数据格式

This commit is contained in:
fit2-zhao 2021-02-26 11:32:49 +08:00
parent 8ae4743d42
commit a3d3fc8366
9 changed files with 38 additions and 26 deletions

View File

@ -189,7 +189,7 @@ public class MsScenario extends MsTestElement {
} }
}); });
} }
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) { && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) {
config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=") arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")

View File

@ -47,8 +47,10 @@ import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type") @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@ -166,7 +168,7 @@ public abstract class MsTestElement {
} }
public Arguments addArguments(ParameterConfig config) { public Arguments addArguments(ParameterConfig config) {
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) { && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) {
Arguments arguments = new Arguments(); Arguments arguments = new Arguments();
arguments.setEnabled(true); arguments.setEnabled(true);
@ -181,11 +183,14 @@ public abstract class MsTestElement {
return null; return null;
} }
protected EnvironmentConfig getEnvironmentConfig(String environmentId) { protected Map<String, EnvironmentConfig> getEnvironmentConfig(String environmentId) {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentId); ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentId);
if (environment != null && environment.getConfig() != null) { if (environment != null && environment.getConfig() != null) {
return JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); // 单独接口执行
Map<String, EnvironmentConfig> map = new HashMap<>();
map.put(this.getProjectId(), JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class));
return map;
} }
return null; return null;
} }
@ -269,7 +274,7 @@ public abstract class MsTestElement {
getFullPath(element.getParent(), path); getFullPath(element.getParent(), path);
} }
protected String getParentName(MsTestElement parent, ParameterConfig config) { protected String getParentName(MsTestElement parent) {
if (parent != null) { if (parent != null) {
if (MsTestElementConstants.LoopController.name().equals(parent.getType())) { if (MsTestElementConstants.LoopController.name().equals(parent.getType())) {
MsLoopController loopController = (MsLoopController) parent; MsLoopController loopController = (MsLoopController) parent;

View File

@ -30,4 +30,11 @@ public class ParameterConfig {
* 项目ID支持单接口执行 * 项目ID支持单接口执行
*/ */
private String projectId; private String projectId;
}
public boolean isEffective(String projectId) {
if (this.config != null && this.config.get(projectId) != null) {
return true;
}
return false;
}
}

View File

@ -36,7 +36,7 @@ public class MsJSR223Processor extends MsTestElement {
} else { } else {
processor.setName("JSR223Processor"); processor.setName("JSR223Processor");
} }
String name = this.getParentName(this.getParent(), config); String name = this.getParentName(this.getParent());
if (StringUtils.isNotEmpty(name) && !config.isOperating()) { if (StringUtils.isNotEmpty(name) && !config.isOperating()) {
processor.setName(this.getName() + "<->" + name); processor.setName(this.getName() + "<->" + name);
} }

View File

@ -76,7 +76,7 @@ public class MsDubboSampler extends MsTestElement {
DubboSample sampler = new DubboSample(); DubboSample sampler = new DubboSample();
sampler.setEnabled(this.isEnable()); sampler.setEnabled(this.isEnable());
sampler.setName(this.getName()); sampler.setName(this.getName());
String name = this.getParentName(this.getParent(), config); String name = this.getParentName(this.getParent());
if (StringUtils.isNotEmpty(name) && !config.isOperating()) { if (StringUtils.isNotEmpty(name) && !config.isOperating()) {
sampler.setName(this.getName() + "<->" + name); sampler.setName(this.getName() + "<->" + name);
} }

View File

@ -8,7 +8,6 @@ import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager; import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager;
import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.ScriptEngineUtils; import io.metersphere.commons.utils.ScriptEngineUtils;
@ -101,7 +100,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
HTTPSamplerProxy sampler = new HTTPSamplerProxy(); HTTPSamplerProxy sampler = new HTTPSamplerProxy();
sampler.setEnabled(this.isEnable()); sampler.setEnabled(this.isEnable());
sampler.setName(this.getName()); sampler.setName(this.getName());
String name = this.getParentName(this.getParent(), config); String name = this.getParentName(this.getParent());
if (StringUtils.isNotEmpty(name) && !config.isOperating()) { if (StringUtils.isNotEmpty(name) && !config.isOperating()) {
sampler.setName(this.getName() + "<->" + name); sampler.setName(this.getName() + "<->" + name);
} }
@ -114,14 +113,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setFollowRedirects(this.isFollowRedirects()); sampler.setFollowRedirects(this.isFollowRedirects());
sampler.setUseKeepAlive(true); sampler.setUseKeepAlive(true);
sampler.setDoMultipart(this.isDoMultipartPost()); sampler.setDoMultipart(this.isDoMultipartPost());
if (config != null && config.getConfig() != null) { if (config.getConfig() == null) {
config.setConfig(config.getConfig());
} else {
// 单独接口执行 // 单独接口执行
this.setProjectId(config.getProjectId()); this.setProjectId(config.getProjectId());
Map<String, EnvironmentConfig> map = new HashMap<>(); config.setConfig(getEnvironmentConfig(useEnvironment));
map.put(this.getProjectId(), getEnvironmentConfig(useEnvironment));
config.setConfig(map);
} }
// 添加环境中的公共变量 // 添加环境中的公共变量
@ -130,7 +125,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
tree.add(arguments); tree.add(arguments);
} }
try { try {
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null) { if (config.isEffective(this.getProjectId())) {
String url = config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol() + "://" + config.getConfig().get(this.getProjectId()).getHttpConfig().getSocket(); String url = config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol() + "://" + config.getConfig().get(this.getProjectId()).getHttpConfig().getSocket();
// 补充如果是完整URL 则用自身URL // 补充如果是完整URL 则用自身URL
boolean isUrl = false; boolean isUrl = false;
@ -219,13 +214,13 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
// 通用请求Headers // 通用请求Headers
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null && config.getConfig().get(this.getProjectId()).getHttpConfig() != null if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getHttpConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders())) { && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders())) {
setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders()); setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders());
} }
//判断是否要开启DNS //判断是否要开启DNS
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) { && config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) {
MsDNSCacheManager.addEnvironmentVariables(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId())); MsDNSCacheManager.addEnvironmentVariables(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId()));
MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId())); MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId()));

View File

@ -113,7 +113,7 @@ public class MsJDBCSampler extends MsTestElement {
JDBCSampler sampler = new JDBCSampler(); JDBCSampler sampler = new JDBCSampler();
sampler.setEnabled(this.isEnable()); sampler.setEnabled(this.isEnable());
sampler.setName(this.getName()); sampler.setName(this.getName());
String name = this.getParentName(this.getParent(), config); String name = this.getParentName(this.getParent());
if (StringUtils.isNotEmpty(name) && !config.isOperating()) { if (StringUtils.isNotEmpty(name) && !config.isOperating()) {
sampler.setName(this.getName() + "<->" + name); sampler.setName(this.getName() + "<->" + name);
} }

View File

@ -23,9 +23,7 @@ import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Random;
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ -59,7 +57,7 @@ public class MsTCPSampler extends MsTestElement {
private String password = ""; private String password = "";
@JSONField(ordinal = 33) @JSONField(ordinal = 33)
private String request; private String request;
// @JSONField(ordinal = 34) // @JSONField(ordinal = 34)
// private Object requestResult; // private Object requestResult;
@JSONField(ordinal = 35) @JSONField(ordinal = 35)
private List<KeyValue> parameters; private List<KeyValue> parameters;
@ -77,7 +75,11 @@ public class MsTCPSampler extends MsTestElement {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.getRefElement(this); this.getRefElement(this);
} }
// config.setConfig(getEnvironmentConfig(useEnvironment)); if (config.getConfig() == null) {
// 单独接口执行
this.setProjectId(config.getProjectId());
config.setConfig(getEnvironmentConfig(useEnvironment));
}
parseEnvironment(config.getConfig().get(this.projectId)); parseEnvironment(config.getConfig().get(this.projectId));
// 添加环境中的公共变量 // 添加环境中的公共变量
@ -111,7 +113,7 @@ public class MsTCPSampler extends MsTestElement {
TCPSampler tcpSampler = new TCPSampler(); TCPSampler tcpSampler = new TCPSampler();
tcpSampler.setEnabled(this.isEnable()); tcpSampler.setEnabled(this.isEnable());
tcpSampler.setName(this.getName()); tcpSampler.setName(this.getName());
String name = this.getParentName(this.getParent(), config); String name = this.getParentName(this.getParent());
if (StringUtils.isNotEmpty(name) && !config.isOperating()) { if (StringUtils.isNotEmpty(name) && !config.isOperating()) {
tcpSampler.setName(this.getName() + "<->" + name); tcpSampler.setName(this.getName() + "<->" + name);
} }

View File

@ -146,6 +146,9 @@
if (!this.response.body) { if (!this.response.body) {
this.response.body = ""; this.response.body = "";
} }
if(!this.response.responseResult.vars){
this.response.responseResult.vars="";
}
this.reqMessages = this.$t('api_test.request.address') + ":\n" + this.response.url + "\n" + this.reqMessages = this.$t('api_test.request.address') + ":\n" + this.response.url + "\n" +
this.$t('api_test.scenario.headers') + ":\n" + this.response.headers + "\n" + "Cookies :\n" + this.$t('api_test.scenario.headers') + ":\n" + this.response.headers + "\n" + "Cookies :\n" +
this.response.cookies + "\n" + "Bpdy:" + "\n" + this.response.body; this.response.cookies + "\n" + "Bpdy:" + "\n" + this.response.body;