From 1fa7e1c83370413bdd5efd4584f2d7aa8114bb43 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 25 Mar 2024 19:06:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E5=9C=BA=E6=99=AF=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TimeWaitingProcessorConverter.java | 7 +- .../parser/step/ConstantTimerStepParser.java | 26 ++++++ .../parser/step/IfControllerStepParser.java | 31 +++++++ .../parser/step/LoopControllerStepParser.java | 83 +++++++++++++++++++ .../step/OnceOnlyControllerStepParser.java | 21 +++++ .../api/parser/step/StepParserFactory.java | 4 + .../service/scenario/ApiScenarioService.java | 3 +- .../ApiScenarioControllerTests.java | 69 +++++++++++++++ 8 files changed, 238 insertions(+), 6 deletions(-) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/step/ConstantTimerStepParser.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/step/IfControllerStepParser.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/step/LoopControllerStepParser.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/step/OnceOnlyControllerStepParser.java diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/TimeWaitingProcessorConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/TimeWaitingProcessorConverter.java index 92a3e7553e..a1012fdb12 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/TimeWaitingProcessorConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/TimeWaitingProcessorConverter.java @@ -21,10 +21,10 @@ import static io.metersphere.api.parser.jmeter.constants.JmeterAlias.CONSTANT_TI public class TimeWaitingProcessorConverter extends MsProcessorConverter { @Override 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 = new DebugSampler(); debugSampler.setEnabled(true); @@ -37,9 +37,6 @@ public class TimeWaitingProcessorConverter extends MsProcessorConverter config = new LinkedHashMap<>(); + config.put("condition", "EQUALS"); + config.put("variable", "a"); + config.put("value", "b"); + msIf.setConfig(config); steps.add(msIf); stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController))); msIfController = ifController(null, true); @@ -835,6 +840,11 @@ public class ApiScenarioControllerTests extends BaseTest { msIf.setProjectId(DEFAULT_PROJECT_ID); msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name()); 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); stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController))); msIfController = ifController(null, false); @@ -843,6 +853,11 @@ public class ApiScenarioControllerTests extends BaseTest { msIf.setProjectId(DEFAULT_PROJECT_ID); msIf.setStepType(ApiScenarioStepType.IF_CONTROLLER.name()); 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); stepDetails.put(msIf.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msIfController))); MsOnceOnlyController msOnceOnlyController = onceController("test-once-only", true); @@ -876,6 +891,15 @@ public class ApiScenarioControllerTests extends BaseTest { loopController.setId(IDGenerator.nextStr()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(loopCount))); + config = new LinkedHashMap<>(); + config.put("loopType", "LOOP_COUNT"); + LinkedHashMap msCount = new LinkedHashMap<>(); + msCount.put("loops", "1"); + LinkedHashMap msForeach = new LinkedHashMap<>(); + msForeach.put("loopTime", "100"); + config.put("msCountController", msCount); + config.put("forEachController", msForeach); + loopController.setConfig(config); steps.add(loopController); loopCount = loopController("LOOP_COUNT", null, false); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), loopCount); @@ -884,6 +908,15 @@ public class ApiScenarioControllerTests extends BaseTest { loopController.setId(IDGenerator.nextStr()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); 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); MsLoopController aWhile = loopController("WHILE", "CONDITION", true); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), aWhile); @@ -892,6 +925,18 @@ public class ApiScenarioControllerTests extends BaseTest { loopController.setId(IDGenerator.nextStr()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); 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); aWhile = loopController("WHILE", "SCRIPT", true); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), aWhile); @@ -900,6 +945,16 @@ public class ApiScenarioControllerTests extends BaseTest { loopController.setId(IDGenerator.nextStr()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); 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); MsLoopController foreach = loopController("FOREACH", null, true); loopController = BeanUtils.copyBean(new ApiScenarioStepRequest(), foreach); @@ -908,6 +963,14 @@ public class ApiScenarioControllerTests extends BaseTest { loopController.setId(IDGenerator.nextStr()); loopController.setStepType(ApiScenarioStepType.LOOP_CONTROLLER.name()); 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); MsConstantTimerController constantTimerController = constantTimerController("test-constant-timer", true); @@ -917,6 +980,9 @@ public class ApiScenarioControllerTests extends BaseTest { constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name()); constantTimer.setId(IDGenerator.nextStr()); stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController))); + config = new LinkedHashMap<>(); + config.put("waitTime", 1000); + constantTimer.setConfig(config); steps.add(constantTimer); constantTimerController = constantTimerController("test-constant-timer", false); constantTimer = BeanUtils.copyBean(new ApiScenarioStepRequest(), constantTimerController); @@ -925,6 +991,9 @@ public class ApiScenarioControllerTests extends BaseTest { constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name()); constantTimer.setId(IDGenerator.nextStr()); stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController))); + config = new LinkedHashMap<>(); + config.put("waitTime", 1000); + constantTimer.setConfig(config); steps.add(constantTimer); request.setId(addApiScenario.getId());