fix(接口测试): 使用源场景参数配置失效

This commit is contained in:
AgAngle 2024-04-12 14:36:08 +08:00 committed by Craftsman
parent a5f51e6866
commit 10d92940aa
4 changed files with 68 additions and 27 deletions

View File

@ -1,5 +1,6 @@
package io.metersphere.api.dto;
import io.metersphere.api.dto.scenario.ScenarioConfig;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import lombok.Data;
@ -19,6 +20,10 @@ public class ApiScenarioParamConfig extends ApiParamConfig {
* value 为环境信息
*/
private Map<String, EnvironmentInfoDTO> projectEnvMap;
/**
* 当前场景的场景配置
*/
private ScenarioConfig rootScenarioConfig;
/**
* 是否为环境组
* 是则使用 projectEnvMap

View File

@ -107,8 +107,8 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter<MsC
private void addProcessors(HashTree tree, MsCommonElement msCommonElement, ParameterConfig config,
EnvironmentInfoDTO envInfo, boolean isPre) {
MsProcessorConfig processorConfig = isPre ? msCommonElement.getPreProcessorConfig() : msCommonElement.getPostProcessorConfig();
if (processorConfig == null || processorConfig.getProcessors() == null) {
return;
if (processorConfig == null) {
processorConfig = new MsProcessorConfig();
}
AbstractMsTestElement parent = msCommonElement.getParent();
String protocol = null;

View File

@ -31,6 +31,7 @@ import org.apache.jorphan.collections.HashTree;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@ -47,7 +48,8 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
@Override
public void toHashTree(HashTree tree, MsScenario msScenario, ParameterConfig msParameter) {
ApiScenarioParamConfig config = (ApiScenarioParamConfig) msParameter;
// 获取生效的环境配置
ApiScenarioParamConfig config = getEnableConfig(msScenario, (ApiScenarioParamConfig) msParameter);
EnvironmentInfoDTO envInfo = config.getEnvConfig(msScenario.getProjectId());
if (isRootScenario(msScenario.getRefType()) && msScenario.getScenarioConfig().getOtherConfig().getEnableGlobalCookie()) {
@ -56,7 +58,7 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
}
// 添加场景和环境变量
addArguments(tree, msScenario, envInfo);
addArguments(tree, msScenario, envInfo, config);
// 添加环境的前置
addEnvScenarioProcessor(tree, msScenario, config, envInfo, true);
@ -64,8 +66,7 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
addScenarioProcessor(tree, msScenario, config, true);
// 解析子步骤
ApiScenarioParamConfig chileConfig = getChileConfig(msScenario, config);
parseChild(tree, msScenario, chileConfig);
parseChild(tree, msScenario, config);
// 添加场景后置
addScenarioProcessor(tree, msScenario, config, false);
@ -78,21 +79,46 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
/**
* 添加场景和环境变量
*
* @param tree
* @param msScenario
* @param envInfo
*/
private void addArguments(HashTree tree, MsScenario msScenario, EnvironmentInfoDTO envInfo) {
if (envInfo == null) {
return;
private void addArguments(HashTree tree, MsScenario msScenario, EnvironmentInfoDTO envInfo, ApiScenarioParamConfig config) {
// 如果是根场景获取场景变量
List<CommonVariables> commonVariables = getCommonVariables(msScenario.getScenarioConfig());
if (!isRootScenario(msScenario.getRefType())) {
// 不是根场景从步骤配置中获取是否使用场景变量
ScenarioStepConfig scenarioStepConfig = msScenario.getScenarioStepConfig();
if (scenarioStepConfig != null) {
if (BooleanUtils.isTrue(scenarioStepConfig.getUseOriginScenarioParam())) {
if (BooleanUtils.isNotTrue(scenarioStepConfig.getUseOriginScenarioParamPreferential())) {
// 如果不是源场景变量优先则跟根场景比较去掉重名的变量
Set<String> rootVariableKeys = getCommonVariables(config.getRootScenarioConfig())
.stream()
.filter(CommonVariables::getEnable)
.filter(variable -> variable.getEnable() && (variable.isListValid() ? variable.isListValid() : variable.isConstantValid()))
.map(variable -> variable.getParamType() + variable.getKey())
.collect(Collectors.toSet());
commonVariables = commonVariables.stream().filter(variable -> {
if (rootVariableKeys.contains(variable.getParamType() + variable.getKey())) {
// 当前场景变量优先过滤掉当前场景变量中存在的变量
return false;
}
return true;
}).collect(Collectors.toList());
}
} else {
// 如果没有启用源场景环境则置空
commonVariables = List.of();
}
}
}
ScenarioConfig scenarioConfig = msScenario.getScenarioConfig();
ScenarioVariable scenarioVariable = scenarioConfig == null ? new ScenarioVariable() : scenarioConfig.getVariable();
scenarioVariable = scenarioVariable == null ? new ScenarioVariable() : scenarioVariable;
List<CommonVariables> commonVariables = scenarioVariable.getCommonVariables();
List<CommonVariables> envCommonVariables = List.of();
if (needParseEnv(msScenario) && envInfo.getConfig() != null) {
if (envInfo != null && needParseEnv(msScenario) && envInfo.getConfig() != null) {
// 获取环境变量
envCommonVariables = envInfo.getConfig().getCommonVariables();
// 获取后将环境变量置空避免请求重复设置
@ -112,8 +138,16 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
tree.add(arguments);
}
private List<CommonVariables> getCommonVariables(ScenarioConfig scenarioConfig) {
ScenarioVariable scenarioVariable = scenarioConfig == null ? new ScenarioVariable() : scenarioConfig.getVariable();
scenarioVariable = scenarioVariable == null ? new ScenarioVariable() : scenarioVariable;
List<CommonVariables> commonVariables = scenarioVariable.getCommonVariables();
return commonVariables;
}
/**
* 合并环境变量和场景变量
*
* @param scenarioVariables
* @param envCommonVariables
* @param filter
@ -216,6 +250,7 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
/**
* 是否需要解析环境
*
* @param msScenario
* @return
*/
@ -273,45 +308,45 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
}
/**
* 获取子步骤的配置信息
* 如果使用源场景环境则使用当前场景的环境信息
* 获取生效的环境
* 如果使用源场景环境则使用场景的环境信息
*
* @param msScenario
* @param config
* @return
*/
private ApiScenarioParamConfig getChileConfig(MsScenario msScenario, ApiScenarioParamConfig config) {
ApiScenarioParamConfig childConfig = config;
private ApiScenarioParamConfig getEnableConfig(MsScenario msScenario, ApiScenarioParamConfig config) {
ApiScenarioParamConfig enableConfig = config;
if (!isRef(msScenario.getRefType())) {
// 非引用的场景使用当前环境参数
return childConfig;
return enableConfig;
}
ScenarioStepConfig scenarioStepConfig = msScenario.getScenarioStepConfig();
if (scenarioStepConfig != null && BooleanUtils.isTrue(scenarioStepConfig.getEnableScenarioEnv())) {
// 使用源场景环境
childConfig = BeanUtils.copyBean(new ApiScenarioParamConfig(), config);
childConfig.setGrouped(msScenario.getGrouped());
enableConfig = BeanUtils.copyBean(new ApiScenarioParamConfig(), config);
enableConfig.setGrouped(msScenario.getGrouped());
// 清空环境信息
childConfig.setEnvConfig(null);
childConfig.setProjectEnvMap(null);
enableConfig.setEnvConfig(null);
enableConfig.setProjectEnvMap(null);
if (BooleanUtils.isTrue(msScenario.getGrouped())) {
// 环境组设置环境Map
Map<String, EnvironmentInfoDTO> projectEnvMap = msScenario.getProjectEnvMap();
childConfig.setProjectEnvMap(projectEnvMap);
enableConfig.setProjectEnvMap(projectEnvMap);
} else {
// 设置环境信息
EnvironmentInfoDTO environmentInfo = msScenario.getEnvironmentInfo();
childConfig.setEnvConfig(environmentInfo);
enableConfig.setEnvConfig(environmentInfo);
}
}
ScenarioConfig scenarioConfig = msScenario.getScenarioConfig();
if (scenarioConfig != null) {
// 设置是否使用全局cookie
childConfig.setEnableGlobalCookie(scenarioConfig.getOtherConfig().getEnableCookieShare());
enableConfig.setEnableGlobalCookie(scenarioConfig.getOtherConfig().getEnableCookieShare());
}
return childConfig;
return enableConfig;
}
private CookieManager getCookieManager() {

View File

@ -1542,6 +1542,7 @@ public class ApiScenarioService extends MoveNodeService {
parseConfig.setTestElementClassPluginIdMap(apiPluginService.getTestElementPluginMap());
parseConfig.setTestElementClassProtocalMap(apiPluginService.getTestElementProtocolMap());
parseConfig.setGrouped(request.getGrouped());
parseConfig.setRootScenarioConfig(request.getScenarioConfig());
if (BooleanUtils.isTrue(request.getGrouped())) {
// 设置环境组 map
parseConfig.setProjectEnvMap(getProjectEnvMap(scenarioParseEnvInfo, request.getEnvironmentId()));