refactor(接口测试): 优化场景执行

This commit is contained in:
wxg0103 2024-03-25 19:06:35 +08:00 committed by Craftsman
parent bee35b522b
commit 1fa7e1c833
8 changed files with 238 additions and 6 deletions

View File

@ -21,10 +21,10 @@ import static io.metersphere.api.parser.jmeter.constants.JmeterAlias.CONSTANT_TI
public class TimeWaitingProcessorConverter extends MsProcessorConverter<TimeWaitingProcessor> { public class TimeWaitingProcessorConverter extends MsProcessorConverter<TimeWaitingProcessor> {
@Override @Override
public void parse(HashTree hashTree, TimeWaitingProcessor processor, ParameterConfig config) { public void parse(HashTree hashTree, TimeWaitingProcessor processor, ParameterConfig config) {
hashTree.add(getConstantTimerElement(processor)); hashTree.add(getConstantTimerElement(),getConstantTimer(processor));
} }
private DebugSampler getConstantTimerElement(TimeWaitingProcessor processor) { private DebugSampler getConstantTimerElement() {
// 添加 debugSampler 步骤间生效 // 添加 debugSampler 步骤间生效
DebugSampler debugSampler = new DebugSampler(); DebugSampler debugSampler = new DebugSampler();
debugSampler.setEnabled(true); debugSampler.setEnabled(true);
@ -37,9 +37,6 @@ public class TimeWaitingProcessorConverter extends MsProcessorConverter<TimeWait
debugSampler.setProperty("displayJMeterVariables", false); debugSampler.setProperty("displayJMeterVariables", false);
debugSampler.setProperty("displaySystemProperties", false); debugSampler.setProperty("displaySystemProperties", false);
ConstantTimer constantTimer = getConstantTimer(processor);
debugSampler.addTestElement(constantTimer);
return debugSampler; return debugSampler;
} }

View File

@ -0,0 +1,26 @@
package io.metersphere.api.parser.step;
import io.metersphere.api.domain.ApiScenarioStep;
import io.metersphere.api.dto.request.controller.MsConstantTimerController;
import io.metersphere.api.dto.scenario.ApiScenarioStepCommonDTO;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.sdk.util.BeanUtils;
import java.util.LinkedHashMap;
public class ConstantTimerStepParser extends StepParser {
private static final String WAIT_TIME = "waitTime";
@Override
public AbstractMsTestElement parseTestElement(ApiScenarioStepCommonDTO step, String resourceBlob, String stepDetail) {
MsConstantTimerController msConstantTimerController = new MsConstantTimerController();
BeanUtils.copyBean(msConstantTimerController, step);
LinkedHashMap msTime = (LinkedHashMap) step.getConfig();
msConstantTimerController.setDelay(Long.valueOf((Integer) msTime.getOrDefault(WAIT_TIME, 0)));
return msConstantTimerController;
}
@Override
public Object parseDetail(ApiScenarioStep step) {
return null;
}
}

View File

@ -0,0 +1,31 @@
package io.metersphere.api.parser.step;
import io.metersphere.api.domain.ApiScenarioStep;
import io.metersphere.api.dto.request.controller.MsIfController;
import io.metersphere.api.dto.scenario.ApiScenarioStepCommonDTO;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.sdk.util.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashMap;
public class IfControllerStepParser extends StepParser {
private static final String CONDITION = "condition";
private static final String VARIABLE = "variable";
private static final String VALUE = "value";
@Override
public AbstractMsTestElement parseTestElement(ApiScenarioStepCommonDTO step, String resourceBlob, String stepDetail) {
MsIfController msIfController = new MsIfController();
BeanUtils.copyBean(msIfController, step);
LinkedHashMap msIf = (LinkedHashMap) step.getConfig();
msIfController.setCondition(String.valueOf(msIf.getOrDefault(CONDITION,StringUtils.EMPTY)));
msIfController.setVariable(String.valueOf(msIf.getOrDefault(VARIABLE,StringUtils.EMPTY)));
msIfController.setValue(String.valueOf(msIf.getOrDefault(VALUE, StringUtils.EMPTY)));
return msIfController;
}
@Override
public Object parseDetail(ApiScenarioStep step) {
return null;
}
}

View File

@ -0,0 +1,83 @@
package io.metersphere.api.parser.step;
import io.metersphere.api.domain.ApiScenarioStep;
import io.metersphere.api.dto.request.controller.LoopType;
import io.metersphere.api.dto.request.controller.MsLoopController;
import io.metersphere.api.dto.request.controller.loop.*;
import io.metersphere.api.dto.scenario.ApiScenarioStepCommonDTO;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.sdk.util.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashMap;
public class LoopControllerStepParser extends StepParser {
private static final String LOOP_TYPE = "loopType";
private static final String LOOP_COUNT = "msCountController";
private static final String FOREACH = "forEachController";
private static final String WHILE = "whileController";
private static final String LOOPS = "loops";
private static final String LOOP_TIME = "loopTime";
private static final String VALUE = "value";
private static final String VARIABLE = "variable";
private static final String CONDITION_TYPE = "conditionType";
private static final String CONDITION_ = "CONDITION";
private static final String WHILE_VARIABLE = "msWhileVariable";
private static final String WHILE_SCRIPT = "msWhileScript";
private static final String TIMEOUT = "timeout";
private static final String SCRIPT = "scriptValue";
private static final String CONDITION = "condition";
@Override
public AbstractMsTestElement parseTestElement(ApiScenarioStepCommonDTO step, String resourceBlob, String stepDetail) {
MsLoopController msLoopController = new MsLoopController();
BeanUtils.copyBean(msLoopController, step);
LinkedHashMap msLoop = (LinkedHashMap) step.getConfig();
String loopType = String.valueOf(msLoop.get(LOOP_TYPE));
msLoopController.setLoopType(loopType);
LinkedHashMap msCount = (LinkedHashMap) msLoop.get(LOOP_COUNT);
LinkedHashMap msForeach = (LinkedHashMap) msLoop.get(FOREACH);
LinkedHashMap msWhile = (LinkedHashMap) msLoop.get(WHILE);
switch (LoopType.valueOf(loopType)) {
case LoopType.FOREACH:
MsForEachController msForEachController = new MsForEachController();
msForEachController.setValue(String.valueOf(msForeach.get(VALUE)));
msForEachController.setVariable(String.valueOf(msForeach.get(VARIABLE)));
msForEachController.setLoopTime(Long.parseLong(String.valueOf(msForeach.get(LOOP_TIME))));
msLoopController.setForEachController(msForEachController);
break;
case LoopType.WHILE:
MsWhileController whileController = new MsWhileController();
LinkedHashMap whileVariable = (LinkedHashMap)msWhile.get(WHILE_VARIABLE);
LinkedHashMap whileScript = (LinkedHashMap) msWhile.get(WHILE_SCRIPT);
String conditionType = String.valueOf(msWhile.get(CONDITION_TYPE));
whileController.setConditionType(conditionType);
whileController.setTimeout(Long.parseLong(String.valueOf(msWhile.get(TIMEOUT))));
if (StringUtils.equals(conditionType, CONDITION_)) {
MsWhileVariable msWhileVariable = new MsWhileVariable();
msWhileVariable.setVariable(String.valueOf(whileVariable.get(VARIABLE)));
msWhileVariable.setValue(String.valueOf(whileVariable.get(VALUE)));
msWhileVariable.setCondition(String.valueOf(whileVariable.get(CONDITION)));
whileController.setMsWhileVariable(msWhileVariable);
} else {
MsWhileScript msWhileScript = new MsWhileScript();
msWhileScript.setScriptValue(whileScript.get(SCRIPT).toString());
whileController.setMsWhileScript(msWhileScript);
}
break;
default:
MsCountController msCountController = new MsCountController();
msCountController.setLoops(String.valueOf(msCount.get(LOOPS)));
msCountController.setLoopTime(Long.parseLong(String.valueOf(msForeach.get(LOOP_TIME))));
msLoopController.setMsCountController(msCountController);
break;
}
return msLoopController;
}
@Override
public Object parseDetail(ApiScenarioStep step) {
return null;
}
}

View File

@ -0,0 +1,21 @@
package io.metersphere.api.parser.step;
import io.metersphere.api.domain.ApiScenarioStep;
import io.metersphere.api.dto.request.controller.MsOnceOnlyController;
import io.metersphere.api.dto.scenario.ApiScenarioStepCommonDTO;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.sdk.util.BeanUtils;
public class OnceOnlyControllerStepParser extends StepParser {
@Override
public AbstractMsTestElement parseTestElement(ApiScenarioStepCommonDTO step, String resourceBlob, String stepDetail) {
MsOnceOnlyController msOnceOnlyController = new MsOnceOnlyController();
BeanUtils.copyBean(msOnceOnlyController, step);
return msOnceOnlyController;
}
@Override
public Object parseDetail(ApiScenarioStep step) {
return null;
}
}

View File

@ -18,6 +18,10 @@ public abstract class StepParserFactory {
stepParserMap.put(ApiScenarioStepType.API.name(), new ApiStepParser()); stepParserMap.put(ApiScenarioStepType.API.name(), new ApiStepParser());
stepParserMap.put(ApiScenarioStepType.API_CASE.name(), new ApiCaseStepParser()); stepParserMap.put(ApiScenarioStepType.API_CASE.name(), new ApiCaseStepParser());
stepParserMap.put(ApiScenarioStepType.API_SCENARIO.name(), new ApiScenarioStepParser()); stepParserMap.put(ApiScenarioStepType.API_SCENARIO.name(), new ApiScenarioStepParser());
stepParserMap.put(ApiScenarioStepType.CONSTANT_TIMER.name(), new ConstantTimerStepParser());
stepParserMap.put(ApiScenarioStepType.LOOP_CONTROLLER.name(), new LoopControllerStepParser());
stepParserMap.put(ApiScenarioStepType.ONCE_ONLY_CONTROLLER.name(), new OnceOnlyControllerStepParser());
stepParserMap.put(ApiScenarioStepType.IF_CONTROLLER.name(), new IfControllerStepParser());
} }
public static StepParser getStepParser(String stepType) { public static StepParser getStepParser(String stepType) {

View File

@ -1565,7 +1565,8 @@ public class ApiScenarioService extends MoveNodeService {
setPartialRefStepEnable(step, stepDetailMap); setPartialRefStepEnable(step, stepDetailMap);
// 将步骤详情解析生成对应的MsTestElement // 将步骤详情解析生成对应的MsTestElement
AbstractMsTestElement msTestElement = stepParser.parseTestElement(step, resourceDetailMap.get(step.getResourceId()), stepDetailMap.get(step.getId())); AbstractMsTestElement msTestElement = stepParser.parseTestElement(step,
MapUtils.isNotEmpty(resourceDetailMap) ? resourceDetailMap.getOrDefault(step.getResourceId(),StringUtils.EMPTY) : StringUtils.EMPTY, stepDetailMap.get(step.getId()));
if (msTestElement != null) { if (msTestElement != null) {
if (msTestElement instanceof MsHTTPElement msHTTPElement) { if (msTestElement instanceof MsHTTPElement msHTTPElement) {
// 暂存http类型的步骤 // 暂存http类型的步骤

View File

@ -827,6 +827,11 @@ public class ApiScenarioControllerTests extends BaseTest {
msIf.setProjectId(DEFAULT_PROJECT_ID); msIf.setProjectId(DEFAULT_PROJECT_ID);
msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name()); msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name());
msIf.setId(IDGenerator.nextStr()); msIf.setId(IDGenerator.nextStr());
LinkedHashMap<String, Object> config = new LinkedHashMap<>();
config.put("condition", "EQUALS");
config.put("variable", "a");
config.put("value", "b");
msIf.setConfig(config);
steps.add(msIf); steps.add(msIf);
stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController))); stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController)));
msIfController = ifController(null, true); msIfController = ifController(null, true);
@ -835,6 +840,11 @@ public class ApiScenarioControllerTests extends BaseTest {
msIf.setProjectId(DEFAULT_PROJECT_ID); msIf.setProjectId(DEFAULT_PROJECT_ID);
msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name()); msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name());
msIf.setId(IDGenerator.nextStr()); msIf.setId(IDGenerator.nextStr());
config = new LinkedHashMap<>();
config.put("condition", "EQUALS");
config.put("variable", "a");
config.put("value", "b");
msIf.setConfig(config);
steps.add(msIf); steps.add(msIf);
stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController))); stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController)));
msIfController = ifController(null, false); msIfController = ifController(null, false);
@ -843,6 +853,11 @@ public class ApiScenarioControllerTests extends BaseTest {
msIf.setProjectId(DEFAULT_PROJECT_ID); msIf.setProjectId(DEFAULT_PROJECT_ID);
msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name()); msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name());
msIf.setId(IDGenerator.nextStr()); msIf.setId(IDGenerator.nextStr());
config = new LinkedHashMap<>();
config.put("condition", "EQUALS");
config.put("variable", "a");
config.put("value", "b");
msIf.setConfig(config);
steps.add(msIf); steps.add(msIf);
stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController))); stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController)));
MsOnceOnlyController msOnceOnlyController = onceController("test-once-only", true); MsOnceOnlyController msOnceOnlyController = onceController("test-once-only", true);
@ -876,6 +891,15 @@ public class ApiScenarioControllerTests extends BaseTest {
loopController.setId(IDGenerator.nextStr()); loopController.setId(IDGenerator.nextStr());
loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name());
stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(loopCount))); stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(loopCount)));
config = new LinkedHashMap<>();
config.put("loopType", "LOOP_COUNT");
LinkedHashMap<String, Object> msCount = new LinkedHashMap<>();
msCount.put("loops", "1");
LinkedHashMap<String, Object> msForeach = new LinkedHashMap<>();
msForeach.put("loopTime", "100");
config.put("msCountController", msCount);
config.put("forEachController", msForeach);
loopController.setConfig(config);
steps.add(loopController); steps.add(loopController);
loopCount = loopController("LOOP_COUNT", null, false); loopCount = loopController("LOOP_COUNT", null, false);
loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), loopCount); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), loopCount);
@ -884,6 +908,15 @@ public class ApiScenarioControllerTests extends BaseTest {
loopController.setId(IDGenerator.nextStr()); loopController.setId(IDGenerator.nextStr());
loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name());
stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(loopCount))); stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(loopCount)));
config = new LinkedHashMap<>();
config.put("loopType", "LOOP_COUNT");
msCount = new LinkedHashMap<>();
msCount.put("loops", "1");
msForeach = new LinkedHashMap<>();
msForeach.put("loopTime", "100");
config.put("msCountController", msCount);
config.put("forEachController", msForeach);
loopController.setConfig(config);
steps.add(loopController); steps.add(loopController);
MsLoopController aWhile = loopController("WHILE", "CONDITION", true); MsLoopController aWhile = loopController("WHILE", "CONDITION", true);
loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), aWhile); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), aWhile);
@ -892,6 +925,18 @@ public class ApiScenarioControllerTests extends BaseTest {
loopController.setId(IDGenerator.nextStr()); loopController.setId(IDGenerator.nextStr());
loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name());
stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(aWhile))); stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(aWhile)));
config = new LinkedHashMap<>();
config.put("loopType", "WHILE");
LinkedHashMap msWhile = new LinkedHashMap<>();
msWhile.put("conditionType", "CONDITION");
LinkedHashMap msCondition = new LinkedHashMap<>();
msCondition.put("value", "100");
msCondition.put("variable", "a");
msCondition.put("condition", "EQUALS");
msWhile.put("msWhileVariable", msCondition);
msWhile.put("timeout", "100");
config.put("whileController", msWhile);
loopController.setConfig(config);
steps.add(loopController); steps.add(loopController);
aWhile = loopController("WHILE", "SCRIPT", true); aWhile = loopController("WHILE", "SCRIPT", true);
loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), aWhile); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), aWhile);
@ -900,6 +945,16 @@ public class ApiScenarioControllerTests extends BaseTest {
loopController.setId(IDGenerator.nextStr()); loopController.setId(IDGenerator.nextStr());
loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name());
stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(aWhile))); stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(aWhile)));
config = new LinkedHashMap<>();
config.put("loopType", "WHILE");
msWhile = new LinkedHashMap<>();
msWhile.put("conditionType", "SCRIPT");
msCondition = new LinkedHashMap<>();
msCondition.put("scriptValue", "100");
msWhile.put("msWhileScript", msCondition);
msWhile.put("timeout", "100");
config.put("whileController", msWhile);
loopController.setConfig(config);
steps.add(loopController); steps.add(loopController);
MsLoopController foreach = loopController("FOREACH", null, true); MsLoopController foreach = loopController("FOREACH", null, true);
loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), foreach); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), foreach);
@ -908,6 +963,14 @@ public class ApiScenarioControllerTests extends BaseTest {
loopController.setId(IDGenerator.nextStr()); loopController.setId(IDGenerator.nextStr());
loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name());
stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(foreach))); stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(foreach)));
config = new LinkedHashMap<>();
config.put("loopType", "FOREACH");
LinkedHashMap msForeachController = new LinkedHashMap<>();
msForeachController.put("variable", "a");
msForeachController.put("value", "b");
msForeachController.put("loopTime", "100");
config.put("forEachController", msForeachController);
loopController.setConfig(config);
steps.add(loopController); steps.add(loopController);
MsConstantTimerController constantTimerController = constantTimerController("test-constant-timer", true); MsConstantTimerController constantTimerController = constantTimerController("test-constant-timer", true);
@ -917,6 +980,9 @@ public class ApiScenarioControllerTests extends BaseTest {
constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name()); constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name());
constantTimer.setId(IDGenerator.nextStr()); constantTimer.setId(IDGenerator.nextStr());
stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController))); stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController)));
config = new LinkedHashMap<>();
config.put("waitTime", 1000);
constantTimer.setConfig(config);
steps.add(constantTimer); steps.add(constantTimer);
constantTimerController = constantTimerController("test-constant-timer", false); constantTimerController = constantTimerController("test-constant-timer", false);
constantTimer = BeanUtils.copyBean(new ApiScenarioStepRequest(), constantTimerController); constantTimer = BeanUtils.copyBean(new ApiScenarioStepRequest(), constantTimerController);
@ -925,6 +991,9 @@ public class ApiScenarioControllerTests extends BaseTest {
constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name()); constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name());
constantTimer.setId(IDGenerator.nextStr()); constantTimer.setId(IDGenerator.nextStr());
stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController))); stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController)));
config = new LinkedHashMap<>();
config.put("waitTime", 1000);
constantTimer.setConfig(config);
steps.add(constantTimer); steps.add(constantTimer);
request.setId(addApiScenario.getId()); request.setId(addApiScenario.getId());