refactor(接口自动化): 部分代码优化

This commit is contained in:
fit2-zhao 2021-01-28 14:50:34 +08:00
parent 8c8f2850c2
commit 98f3391c75
7 changed files with 28 additions and 43 deletions

View File

@ -30,6 +30,7 @@ import io.metersphere.api.service.ApiTestEnvironmentService;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.commons.constants.LoopConstants;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import lombok.Data;
@ -249,17 +250,17 @@ public abstract class MsTestElement {
if (element.getParent() == null) {
return element;
}
if ("LoopController".equals(element.getType())) {
if (MsTestElementConstants.LoopController.name().equals(element.getType())) {
return element;
}
return getRootParent(element.getParent());
}
protected String getParentName(MsTestElement element) {
protected String getParentName(MsTestElement element, ParameterConfig config) {
if (element != null) {
MsTestElement parent = this.getRootParent(element);
if (parent != null) {
if ("LoopController".equals(parent.getType())) {
if (MsTestElementConstants.LoopController.name().equals(parent.getType())) {
MsLoopController loopController = (MsLoopController) parent;
if (StringUtils.equals(loopController.getLoopType(), LoopConstants.WHILE.name()) && loopController.getWhileController() != null) {
return "While 循环-" + "${LoopCounterConfigXXX}";
@ -274,6 +275,12 @@ public abstract class MsTestElement {
return parent.getName();
}
return element.getName();
} else if (config != null && StringUtils.isNotEmpty(config.getStep())) {
if (MsTestElementConstants.SCENARIO.name().equals(config.getStepType())) {
return config.getStep();
} else {
return config.getStep() + "-" + "${LoopCounterConfigXXX}";
}
}
return "";
}

View File

@ -39,15 +39,9 @@ public class MsJSR223Processor extends MsTestElement {
} else {
processor.setName("JSR223Processor");
}
String name = this.getParentName(this.getParent());
String name = this.getParentName(this.getParent(), config);
if (StringUtils.isNotEmpty(name)) {
processor.setName(this.getName() + "<->" + name);
} else if (config != null && StringUtils.isNotEmpty(config.getStep())) {
if ("SCENARIO".equals(config.getStepType())) {
processor.setName(this.getName() + "<->" + config.getStep());
} else {
processor.setName(this.getName() + "<->" + config.getStep() + "-" + "${LoopCounterConfigXXX}");
}
}
processor.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));

View File

@ -12,6 +12,7 @@ import io.metersphere.api.dto.definition.request.sampler.dubbo.MsConfigCenter;
import io.metersphere.api.dto.definition.request.sampler.dubbo.MsConsumerAndService;
import io.metersphere.api.dto.definition.request.sampler.dubbo.MsRegistryCenter;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.MsTestElementConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
@ -62,7 +63,7 @@ public class MsDubboSampler extends MsTestElement {
if (this.getReferenced() != null && "Deleted".equals(this.getReferenced())) {
return;
}
if (this.getReferenced() != null && "REF".equals(this.getReferenced())) {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.getRefElement(this);
}
@ -77,15 +78,9 @@ public class MsDubboSampler extends MsTestElement {
private DubboSample dubboSample(ParameterConfig config) {
DubboSample sampler = new DubboSample();
sampler.setName(this.getName());
String name = this.getParentName(this.getParent());
String name = this.getParentName(this.getParent(), config);
if (StringUtils.isNotEmpty(name)) {
sampler.setName(this.getName() + "<->" + name);
} else if (config != null && StringUtils.isNotEmpty(config.getStep())) {
if ("SCENARIO".equals(config.getStepType())) {
sampler.setName(this.getName() + "<->" + config.getStep());
} else {
sampler.setName(this.getName() + "<->" + config.getStep() + "-" + "${LoopCounterConfigXXX}");
}
}
sampler.setProperty(TestElement.TEST_CLASS, DubboSample.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboSampleGui"));

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.ScriptEngineUtils;
import lombok.Data;
@ -91,23 +92,16 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (!this.isEnable()) {
return;
}
if (this.getReferenced() != null && "REF".equals(this.getReferenced())) {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.getRefElement(this);
}
HTTPSamplerProxy sampler = new HTTPSamplerProxy();
sampler.setEnabled(true);
sampler.setName(this.getName());
String name = this.getParentName(this.getParent());
String name = this.getParentName(this.getParent(), config);
if (StringUtils.isNotEmpty(name)) {
sampler.setName(this.getName() + "<->" + name);
} else if (config != null && StringUtils.isNotEmpty(config.getStep())) {
if ("SCENARIO".equals(config.getStepType())) {
sampler.setName(this.getName() + "<->" + config.getStep());
} else {
sampler.setName(this.getName() + "<->" + config.getStep() + "-" + "${LoopCounterConfigXXX}");
}
}
sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui"));
sampler.setMethod(this.getMethod());

View File

@ -10,6 +10,7 @@ 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.constants.MsTestElementConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import lombok.Data;
@ -57,7 +58,7 @@ public class MsJDBCSampler extends MsTestElement {
if (!this.isEnable()) {
return;
}
if (this.getReferenced() != null && "REF".equals(this.getReferenced())) {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.getRefElement(this);
}
if (StringUtils.isNotEmpty(dataSourceId)) {
@ -110,15 +111,9 @@ public class MsJDBCSampler extends MsTestElement {
private JDBCSampler jdbcSampler(ParameterConfig config) {
JDBCSampler sampler = new JDBCSampler();
sampler.setName(this.getName());
String name = this.getParentName(this.getParent());
String name = this.getParentName(this.getParent(), config);
if (StringUtils.isNotEmpty(name)) {
sampler.setName(this.getName() + "<->" + name);
} else if (config != null && StringUtils.isNotEmpty(config.getStep())) {
if ("SCENARIO".equals(config.getStepType())) {
sampler.setName(this.getName() + "<->" + config.getStep());
} else {
sampler.setName(this.getName() + "<->" + config.getStep() + "-" + "${LoopCounterConfigXXX}");
}
}
sampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.commons.constants.MsTestElementConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
@ -73,7 +74,7 @@ public class MsTCPSampler extends MsTestElement {
if (!this.isEnable()) {
return;
}
if (this.getReferenced() != null && this.getReferenced().equals("REF")) {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.getRefElement(this);
}
config.setConfig(getEnvironmentConfig(useEnvironment));
@ -102,15 +103,9 @@ public class MsTCPSampler extends MsTestElement {
private TCPSampler tcpSampler(ParameterConfig config) {
TCPSampler tcpSampler = new TCPSampler();
tcpSampler.setName(this.getName());
String name = this.getParentName(this.getParent());
String name = this.getParentName(this.getParent(), config);
if (StringUtils.isNotEmpty(name)) {
tcpSampler.setName(this.getName() + "<->" + name);
} else if (config != null && StringUtils.isNotEmpty(config.getStep())) {
if ("SCENARIO".equals(config.getStepType())) {
tcpSampler.setName(this.getName() + "<->" + config.getStep());
} else {
tcpSampler.setName(this.getName() + "<->" + config.getStep() + "-" + "${LoopCounterConfigXXX}");
}
}
tcpSampler.setProperty(TestElement.TEST_CLASS, TCPSampler.class.getName());

View File

@ -0,0 +1,5 @@
package io.metersphere.commons.constants;
public enum MsTestElementConstants {
LoopController,SCENARIO,REF
}