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

View File

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

View File

@ -31,6 +31,7 @@ import org.apache.jorphan.collections.HashTree;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -47,7 +48,8 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
@Override @Override
public void toHashTree(HashTree tree, MsScenario msScenario, ParameterConfig msParameter) { 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()); EnvironmentInfoDTO envInfo = config.getEnvConfig(msScenario.getProjectId());
if (isRootScenario(msScenario.getRefType()) && msScenario.getScenarioConfig().getOtherConfig().getEnableGlobalCookie()) { 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); addEnvScenarioProcessor(tree, msScenario, config, envInfo, true);
@ -64,8 +66,7 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
addScenarioProcessor(tree, msScenario, config, true); addScenarioProcessor(tree, msScenario, config, true);
// 解析子步骤 // 解析子步骤
ApiScenarioParamConfig chileConfig = getChileConfig(msScenario, config); parseChild(tree, msScenario, config);
parseChild(tree, msScenario, chileConfig);
// 添加场景后置 // 添加场景后置
addScenarioProcessor(tree, msScenario, config, false); addScenarioProcessor(tree, msScenario, config, false);
@ -78,21 +79,46 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
/** /**
* 添加场景和环境变量 * 添加场景和环境变量
*
* @param tree * @param tree
* @param msScenario * @param msScenario
* @param envInfo * @param envInfo
*/ */
private void addArguments(HashTree tree, MsScenario msScenario, EnvironmentInfoDTO envInfo) { private void addArguments(HashTree tree, MsScenario msScenario, EnvironmentInfoDTO envInfo, ApiScenarioParamConfig config) {
if (envInfo == null) { // 如果是根场景获取场景变量
return; 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(); List<CommonVariables> envCommonVariables = List.of();
if (needParseEnv(msScenario) && envInfo.getConfig() != null) { if (envInfo != null && needParseEnv(msScenario) && envInfo.getConfig() != null) {
// 获取环境变量 // 获取环境变量
envCommonVariables = envInfo.getConfig().getCommonVariables(); envCommonVariables = envInfo.getConfig().getCommonVariables();
// 获取后将环境变量置空避免请求重复设置 // 获取后将环境变量置空避免请求重复设置
@ -112,8 +138,16 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
tree.add(arguments); 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 scenarioVariables
* @param envCommonVariables * @param envCommonVariables
* @param filter * @param filter
@ -216,6 +250,7 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
/** /**
* 是否需要解析环境 * 是否需要解析环境
*
* @param msScenario * @param msScenario
* @return * @return
*/ */
@ -273,45 +308,45 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
} }
/** /**
* 获取子步骤的配置信息 * 获取生效的环境
* 如果使用源场景环境则使用当前场景的环境信息 * 如果使用源场景环境则使用场景的环境信息
* *
* @param msScenario * @param msScenario
* @param config * @param config
* @return * @return
*/ */
private ApiScenarioParamConfig getChileConfig(MsScenario msScenario, ApiScenarioParamConfig config) { private ApiScenarioParamConfig getEnableConfig(MsScenario msScenario, ApiScenarioParamConfig config) {
ApiScenarioParamConfig childConfig = config; ApiScenarioParamConfig enableConfig = config;
if (!isRef(msScenario.getRefType())) { if (!isRef(msScenario.getRefType())) {
// 非引用的场景使用当前环境参数 // 非引用的场景使用当前环境参数
return childConfig; return enableConfig;
} }
ScenarioStepConfig scenarioStepConfig = msScenario.getScenarioStepConfig(); ScenarioStepConfig scenarioStepConfig = msScenario.getScenarioStepConfig();
if (scenarioStepConfig != null && BooleanUtils.isTrue(scenarioStepConfig.getEnableScenarioEnv())) { if (scenarioStepConfig != null && BooleanUtils.isTrue(scenarioStepConfig.getEnableScenarioEnv())) {
// 使用源场景环境 // 使用源场景环境
childConfig = BeanUtils.copyBean(new ApiScenarioParamConfig(), config); enableConfig = BeanUtils.copyBean(new ApiScenarioParamConfig(), config);
childConfig.setGrouped(msScenario.getGrouped()); enableConfig.setGrouped(msScenario.getGrouped());
// 清空环境信息 // 清空环境信息
childConfig.setEnvConfig(null); enableConfig.setEnvConfig(null);
childConfig.setProjectEnvMap(null); enableConfig.setProjectEnvMap(null);
if (BooleanUtils.isTrue(msScenario.getGrouped())) { if (BooleanUtils.isTrue(msScenario.getGrouped())) {
// 环境组设置环境Map // 环境组设置环境Map
Map<String, EnvironmentInfoDTO> projectEnvMap = msScenario.getProjectEnvMap(); Map<String, EnvironmentInfoDTO> projectEnvMap = msScenario.getProjectEnvMap();
childConfig.setProjectEnvMap(projectEnvMap); enableConfig.setProjectEnvMap(projectEnvMap);
} else { } else {
// 设置环境信息 // 设置环境信息
EnvironmentInfoDTO environmentInfo = msScenario.getEnvironmentInfo(); EnvironmentInfoDTO environmentInfo = msScenario.getEnvironmentInfo();
childConfig.setEnvConfig(environmentInfo); enableConfig.setEnvConfig(environmentInfo);
} }
} }
ScenarioConfig scenarioConfig = msScenario.getScenarioConfig(); ScenarioConfig scenarioConfig = msScenario.getScenarioConfig();
if (scenarioConfig != null) { if (scenarioConfig != null) {
// 设置是否使用全局cookie // 设置是否使用全局cookie
childConfig.setEnableGlobalCookie(scenarioConfig.getOtherConfig().getEnableCookieShare()); enableConfig.setEnableGlobalCookie(scenarioConfig.getOtherConfig().getEnableCookieShare());
} }
return childConfig; return enableConfig;
} }
private CookieManager getCookieManager() { private CookieManager getCookieManager() {

View File

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