fix(接口测试): 接口定义执行,处理环境模块匹配

--bug=1049203 --user=陈建星 【项目管理】环境-勾选了包含新增子模块-在接口测试处新增子模块添加接口-使用该环境执行,未走该模块设置的路径 https://www.tapd.cn/55049933/s/1615936
This commit is contained in:
AgAngle 2024-11-25 10:29:03 +08:00 committed by jianxing
parent 871b6c738c
commit 4e7a033f68
3 changed files with 58 additions and 43 deletions

View File

@ -7,9 +7,13 @@ import io.metersphere.api.dto.ApiParamConfig;
import io.metersphere.api.dto.ApiScenarioParamConfig; import io.metersphere.api.dto.ApiScenarioParamConfig;
import io.metersphere.api.dto.debug.ApiDebugRunRequest; import io.metersphere.api.dto.debug.ApiDebugRunRequest;
import io.metersphere.api.dto.debug.ApiResourceRunRequest; import io.metersphere.api.dto.debug.ApiResourceRunRequest;
import io.metersphere.api.dto.definition.ApiModuleDTO;
import io.metersphere.api.dto.definition.EnvApiModuleRequest;
import io.metersphere.api.dto.definition.EnvApiTreeDTO;
import io.metersphere.api.dto.request.controller.MsScriptElement; import io.metersphere.api.dto.request.controller.MsScriptElement;
import io.metersphere.api.parser.TestElementParser; import io.metersphere.api.parser.TestElementParser;
import io.metersphere.api.parser.TestElementParserFactory; import io.metersphere.api.parser.TestElementParserFactory;
import io.metersphere.api.service.definition.ApiDefinitionModuleService;
import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.engine.EngineFactory; import io.metersphere.engine.EngineFactory;
import io.metersphere.engine.MsHttpClient; import io.metersphere.engine.MsHttpClient;
@ -22,6 +26,9 @@ import io.metersphere.project.dto.customfunction.request.CustomFunctionRunReques
import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import io.metersphere.project.dto.environment.GlobalParams; import io.metersphere.project.dto.environment.GlobalParams;
import io.metersphere.project.dto.environment.GlobalParamsDTO; import io.metersphere.project.dto.environment.GlobalParamsDTO;
import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.project.dto.environment.http.HttpConfigModuleMatchRule;
import io.metersphere.project.dto.environment.http.SelectModule;
import io.metersphere.project.service.*; import io.metersphere.project.service.*;
import io.metersphere.sdk.constants.*; import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.dto.api.task.*; import io.metersphere.sdk.dto.api.task.*;
@ -114,6 +121,8 @@ public class ApiExecuteService {
private ExecTaskItemMapper execTaskItemMapper; private ExecTaskItemMapper execTaskItemMapper;
@Resource @Resource
private SqlSessionFactory sqlSessionFactory; private SqlSessionFactory sqlSessionFactory;
@Resource
private ApiDefinitionModuleService apiDefinitionModuleService;
@PostConstruct @PostConstruct
private void init() { private void init() {
@ -844,9 +853,55 @@ public class ApiExecuteService {
public TaskRequestDTO apiExecute(ApiResourceRunRequest runRequest, TaskRequestDTO taskRequest, ApiParamConfig apiParamConfig) { public TaskRequestDTO apiExecute(ApiResourceRunRequest runRequest, TaskRequestDTO taskRequest, ApiParamConfig apiParamConfig) {
// 设置使用脚本前后置的公共脚本信息 // 设置使用脚本前后置的公共脚本信息
setTestElementParam(runRequest.getTestElement(), taskRequest.getTaskInfo().getProjectId(), taskRequest.getTaskItem()); setTestElementParam(runRequest.getTestElement(), taskRequest.getTaskInfo().getProjectId(), taskRequest.getTaskItem());
// 处理模块匹配
handleHttpModuleMatchRule(apiParamConfig.getEnvConfig());
return execute(runRequest, taskRequest, apiParamConfig); return execute(runRequest, taskRequest, apiParamConfig);
} }
/**
* 处理环境的 HTTP 配置模块匹配规则
* 查询新增子模块
*
* @param envInfoDTO
*/
public void handleHttpModuleMatchRule(EnvironmentInfoDTO envInfoDTO) {
if (envInfoDTO == null || envInfoDTO.getConfig() == null) {
return;
}
List<HttpConfig> httpConfigs = envInfoDTO.getConfig().getHttpConfig();
for (HttpConfig httpConfig : httpConfigs) {
if (!httpConfig.isModuleMatchRule()) {
continue;
}
// 获取勾选了包含子模块的模块ID
HttpConfigModuleMatchRule moduleMatchRule = httpConfig.getModuleMatchRule();
List<SelectModule> selectModules = moduleMatchRule.getModules();
EnvApiModuleRequest envApiModuleRequest = new EnvApiModuleRequest();
envApiModuleRequest.setProjectId(envInfoDTO.getProjectId());
List<ApiModuleDTO> apiModuleDTOS = selectModules.stream().map(selectModule -> {
ApiModuleDTO apiModuleDTO = new ApiModuleDTO();
apiModuleDTO.setModuleId(selectModule.getModuleId());
apiModuleDTO.setContainChildModule(selectModule.getContainChildModule());
return apiModuleDTO;
}).toList();
envApiModuleRequest.setSelectedModules(apiModuleDTOS);
EnvApiTreeDTO envApiTreeDTO = apiDefinitionModuleService.envTree(envApiModuleRequest);
List<ApiModuleDTO> selectedModules = envApiTreeDTO.getSelectedModules();
List<String> moduleIds = selectedModules.stream().map(ApiModuleDTO::getModuleId).toList();
// 重新设置选中的模块ID
moduleMatchRule.setModules(null);
List<SelectModule> allSelectModules = moduleIds.stream().map(moduleId -> {
SelectModule module = new SelectModule();
module.setModuleId(moduleId);
return module;
}).collect(Collectors.toList());
moduleMatchRule.setModules(allSelectModules);
}
}
public void setTestElementParam(AbstractMsTestElement testElement, String projectId, TaskItem taskItem) { public void setTestElementParam(AbstractMsTestElement testElement, String projectId, TaskItem taskItem) {
apiCommonService.setEnableCommonScriptProcessorInfo(testElement); apiCommonService.setEnableCommonScriptProcessorInfo(testElement);
testElement.setResourceId(taskItem.getResourceId()); testElement.setResourceId(taskItem.getResourceId());

View File

@ -218,6 +218,8 @@ public class ApiTestCaseRunService {
apiCommonService.setApiDefinitionExecuteInfo(msTestElement, BeanUtils.copyBean(new ApiDefinitionExecuteInfo(), apiDefinition)); apiCommonService.setApiDefinitionExecuteInfo(msTestElement, BeanUtils.copyBean(new ApiDefinitionExecuteInfo(), apiDefinition));
apiExecuteService.setTestElementParam(msTestElement, apiTestCase.getProjectId(), request.getTaskItem()); apiExecuteService.setTestElementParam(msTestElement, apiTestCase.getProjectId(), request.getTaskItem());
// 处理模块匹配
apiExecuteService.handleHttpModuleMatchRule(apiParamConfig.getEnvConfig());
// 设置环境信息 // 设置环境信息
apiParamConfig.setEnvConfig(environmentService.get(getEnvId(request.getRunModeConfig(), apiTestCase.getEnvironmentId()))); apiParamConfig.setEnvConfig(environmentService.get(getEnvId(request.getRunModeConfig(), apiTestCase.getEnvironmentId())));

View File

@ -919,53 +919,11 @@ public class ApiScenarioRunService {
envInfo.setEnvGroupMap(envGroupMap); envInfo.setEnvGroupMap(envGroupMap);
envInfo.setEnvMap(envMap); envInfo.setEnvMap(envMap);
envMap.forEach((envId, envInfoDTO) -> handleHttpModuleMatchRule(envInfoDTO)); envMap.forEach((envId, envInfoDTO) -> apiExecuteService.handleHttpModuleMatchRule(envInfoDTO));
return envInfo; return envInfo;
} }
/**
* 处理环境的 HTTP 配置模块匹配规则
* 查询新增子模块
*
* @param envInfoDTO
*/
private void handleHttpModuleMatchRule(EnvironmentInfoDTO envInfoDTO) {
List<HttpConfig> httpConfigs = envInfoDTO.getConfig().getHttpConfig();
for (HttpConfig httpConfig : httpConfigs) {
if (!httpConfig.isModuleMatchRule()) {
continue;
}
// 获取勾选了包含子模块的模块ID
HttpConfigModuleMatchRule moduleMatchRule = httpConfig.getModuleMatchRule();
List<SelectModule> selectModules = moduleMatchRule.getModules();
EnvApiModuleRequest envApiModuleRequest = new EnvApiModuleRequest();
envApiModuleRequest.setProjectId(envInfoDTO.getProjectId());
List<ApiModuleDTO> apiModuleDTOS = selectModules.stream().map(selectModule -> {
ApiModuleDTO apiModuleDTO = new ApiModuleDTO();
apiModuleDTO.setModuleId(selectModule.getModuleId());
apiModuleDTO.setContainChildModule(selectModule.getContainChildModule());
return apiModuleDTO;
}).toList();
envApiModuleRequest.setSelectedModules(apiModuleDTOS);
EnvApiTreeDTO envApiTreeDTO = apiDefinitionModuleService.envTree(envApiModuleRequest);
List<ApiModuleDTO> selectedModules = envApiTreeDTO.getSelectedModules();
List<String> moduleIds = selectedModules.stream().map(ApiModuleDTO::getModuleId).toList();
// 重新设置选中的模块ID
moduleMatchRule.setModules(null);
List<SelectModule> allSelectModules = moduleIds.stream().map(moduleId -> {
SelectModule module = new SelectModule();
module.setModuleId(moduleId);
return module;
}).collect(Collectors.toList());
moduleMatchRule.setModules(allSelectModules);
}
}
private List<ApiScenario> getApiScenarioByIds(List<String> apiScenarioIds) { private List<ApiScenario> getApiScenarioByIds(List<String> apiScenarioIds) {
ApiScenarioExample example = new ApiScenarioExample(); ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdIn(apiScenarioIds); example.createCriteria().andIdIn(apiScenarioIds);