refactor(接口测试): 增加场景的等待控制器

This commit is contained in:
wxg0103 2024-03-22 16:02:32 +08:00 committed by 刘瑞斌
parent 0c365ad88f
commit 5e50258f68
6 changed files with 101 additions and 2 deletions

View File

@ -1,5 +1,5 @@
package io.metersphere.plugin.api.constants;
public enum ElementProperty {
MS_RESOURCE_ID, MS_STEP_ID, MS_FAKE_ERROR, MS_REPORT_ID, PROJECT_ID
MS_RESOURCE_ID, MS_STEP_ID, MS_FAKE_ERROR, MS_REPORT_ID, PROJECT_ID,SCENARIO_CONSTANT_TIMER
}

View File

@ -0,0 +1,20 @@
package io.metersphere.api.dto.request.controller;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
public class MsConstantTimerController extends AbstractMsTestElement {
/**
* id
*/
private String id;
/**
* 延迟时间
*/
private String delay;
}

View File

@ -0,0 +1,51 @@
package io.metersphere.api.parser.jmeter;
import io.metersphere.api.dto.request.controller.MsConstantTimerController;
import io.metersphere.plugin.api.constants.ElementProperty;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter;
import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.jmeter.sampler.DebugSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.timers.ConstantTimer;
import org.apache.jorphan.collections.HashTree;
public class MsConstantTimerControllerConverter extends AbstractJmeterElementConverter<MsConstantTimerController> {
@Override
public void toHashTree(HashTree tree, MsConstantTimerController element, ParameterConfig config) {
if (BooleanUtils.isFalse(element.getEnable())) {
LogUtils.info("MsConstantTimerController is disabled");
return;
}
HashTree groupTree = tree.add(constantTimer(element));
parseChild(groupTree, element, config);
}
private DebugSampler constantTimer(MsConstantTimerController element) {
// 添加 debugSampler 步骤间生效
DebugSampler debugSampler = new DebugSampler();
debugSampler.setEnabled(true);
debugSampler.setName(ElementProperty.SCENARIO_CONSTANT_TIMER.name());
debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName());
debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
// 上面三行直接Set属性会导致DebugSampler构建时取不到值可能是JMeter的Bug,需要SetProperty
debugSampler.setProperty("displayJMeterProperties", false);
debugSampler.setProperty("displayJMeterVariables", false);
debugSampler.setProperty("displaySystemProperties", false);
ConstantTimer constantTimer = new ConstantTimer();
constantTimer.setEnabled(element.getEnable());
constantTimer.setName(element.getName() + " ms");
constantTimer.setProperty(TestElement.TEST_CLASS, ConstantTimer.class.getName());
constantTimer.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ConstantTimerGui"));
constantTimer.setDelay(element.getDelay());
debugSampler.addTestElement(constantTimer);
return debugSampler;
}
}

View File

@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.type.CollectionType;
import io.metersphere.api.dto.request.MsCommonElement;
import io.metersphere.api.dto.request.controller.MsConstantTimerController;
import io.metersphere.api.dto.request.controller.MsIfController;
import io.metersphere.api.dto.request.controller.MsLoopController;
import io.metersphere.api.dto.request.controller.MsOnceOnlyController;
@ -40,7 +41,7 @@ public class ApiDataUtils {
namedTypes.add(new NamedType(MsIfController.class, MsIfController.class.getSimpleName()));
namedTypes.add(new NamedType(MsLoopController.class, MsLoopController.class.getSimpleName()));
namedTypes.add(new NamedType(MsOnceOnlyController.class, MsOnceOnlyController.class.getSimpleName()));
namedTypes.add(new NamedType(MsConstantTimerController.class, MsConstantTimerController.class.getSimpleName()));
setObjectMapper(objectMapper);
namedTypes.forEach(objectMapper::registerSubtypes);
}

View File

@ -32,6 +32,7 @@ public class JmeterElementConverterRegister {
register(MsIfControllerConverter.class);
register(MsLoopControllerConverter.class);
register(MsOnceOnlyControllerConverter.class);
register(MsConstantTimerControllerConverter.class);
}
/**

View File

@ -910,6 +910,23 @@ public class ApiScenarioControllerTests extends BaseTest {
stepDetails.put(loopController.getId(), JSON.parseObject(ApiDataUtils.toJSONString(foreach)));
steps.add(loopController);
MsConstantTimerController constantTimerController = constantTimerController("test-constant-timer", true);
ApiScenarioStepRequest constantTimer = BeanUtils.copyBean(new ApiScenarioStepRequest(), constantTimerController);
constantTimer.setRefType(ApiScenarioStepRefType.DIRECT.name());
constantTimer.setProjectId(DEFAULT_PROJECT_ID);
constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name());
constantTimer.setId(IDGenerator.nextStr());
stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController)));
steps.add(constantTimer);
constantTimerController = constantTimerController("test-constant-timer", false);
constantTimer = BeanUtils.copyBean(new ApiScenarioStepRequest(), constantTimerController);
constantTimer.setRefType(ApiScenarioStepRefType.DIRECT.name());
constantTimer.setProjectId(DEFAULT_PROJECT_ID);
constantTimer.setStepType(ApiScenarioStepType.CONSTANT_TIMER.name());
constantTimer.setId(IDGenerator.nextStr());
stepDetails.put(constantTimer.getId(), JSON.parseObject(ApiDataUtils.toJSONString(constantTimerController)));
steps.add(constantTimer);
request.setId(addApiScenario.getId());
request.setSteps(steps);
request.setStepDetails(new HashMap<>());
@ -936,6 +953,15 @@ public class ApiScenarioControllerTests extends BaseTest {
return msIfController;
}
public MsConstantTimerController constantTimerController(String name, boolean enable) {
//等待控制器
MsConstantTimerController constantTimerController = new MsConstantTimerController();
constantTimerController.setName(StringUtils.isNotBlank(name) ? name : "等待控制器");
constantTimerController.setEnable(enable);
constantTimerController.setDelay("1000");
return constantTimerController;
}
//一次控制器
public MsOnceOnlyController onceController(String name, boolean enable) {
//一次控制器