fix(接口测试): 场景等待时间步骤执行不生效

This commit is contained in:
AgAngle 2024-03-27 11:57:35 +08:00 committed by Craftsman
parent 6b51d28aed
commit 35d5c3f5ae
6 changed files with 31 additions and 96 deletions

View File

@ -7,10 +7,6 @@ import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
public class MsConstantTimerController extends AbstractMsTestElement {
/**
* id
*/
private String id;
/**
* 延迟时间
*/

View File

@ -4,19 +4,11 @@ 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;
return parseConfig2TestElement(step, MsConstantTimerController.class);
}
@Override

View File

@ -4,24 +4,11 @@ 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;
return parseConfig2TestElement(step, MsIfController.class);
}
@Override

View File

@ -1,79 +1,15 @@
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;
return parseConfig2TestElement(step, MsLoopController.class);
}
@Override

View File

@ -8,8 +8,11 @@ import io.metersphere.api.mapper.ApiScenarioStepBlobMapper;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.JSON;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
/**
* @Author: jianxing
* @CreateTime: 2024-01-20 15:43
@ -18,9 +21,10 @@ public abstract class StepParser {
/**
* 将步骤详情解析为 MsTestElement
* @param step 步骤
*
* @param step 步骤
* @param resourceBlob 关联的资源详情
* @param stepDetail 步骤详情
* @param stepDetail 步骤详情
* @return
*/
public abstract AbstractMsTestElement parseTestElement(ApiScenarioStepCommonDTO step, String resourceBlob, String stepDetail);
@ -29,6 +33,7 @@ public abstract class StepParser {
* 将步骤解析为步骤详情
* 场景步骤返回 ScenarioConfig
* 其余返回 MsTestElement
*
* @param step
* @return
*/
@ -54,4 +59,23 @@ public abstract class StepParser {
}
return new String(apiScenarioStepBlob.getContent());
}
public <T extends AbstractMsTestElement> T parseConfig2TestElement(Object config, Class<T> clazz) {
if (config != null && config instanceof Map confiMap) {
confiMap.put("polymorphicName", clazz.getSimpleName());
return JSON.parseObject(JSON.toJSONString(confiMap), clazz);
}
return null;
}
public <T extends AbstractMsTestElement> T parseConfig2TestElement(ApiScenarioStepCommonDTO step, Class<T> clazz) {
AbstractMsTestElement testElement = parseConfig2TestElement(step.getConfig(), clazz);
if (testElement == null) {
return null;
}
testElement.setName(step.getName());
testElement.setEnable(step.getEnable());
testElement.setStepId(step.getId());
testElement.setProjectId(step.getProjectId());
return (T) testElement;
}
}

View File

@ -981,7 +981,7 @@ public class ApiScenarioControllerTests extends BaseTest {
constantTimer.setId(IDGenerator.nextStr());
stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController)));
config = new LinkedHashMap<>();
config.put("waitTime", 1000);
config.put("delay", 1000L);
constantTimer.setConfig(config);
steps.add(constantTimer);
constantTimerController = constantTimerController("test-constant-timer", false);
@ -992,7 +992,7 @@ public class ApiScenarioControllerTests extends BaseTest {
constantTimer.setId(IDGenerator.nextStr());
stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController)));
config = new LinkedHashMap<>();
config.put("waitTime", 1000);
config.put("delay", 1000L);
constantTimer.setConfig(config);
steps.add(constantTimer);