From 4e7a033f6811cac55632e34da610bb09b34032f5 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 25 Nov 2024 10:29:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=E6=89=A7=E8=A1=8C=EF=BC=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E7=8E=AF=E5=A2=83=E6=A8=A1=E5=9D=97=E5=8C=B9?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1049203 --user=陈建星 【项目管理】环境-勾选了包含新增子模块-在接口测试处新增子模块添加接口-使用该环境执行,未走该模块设置的路径 https://www.tapd.cn/55049933/s/1615936 --- .../api/service/ApiExecuteService.java | 55 +++++++++++++++++++ .../definition/ApiTestCaseRunService.java | 2 + .../scenario/ApiScenarioRunService.java | 44 +-------------- 3 files changed, 58 insertions(+), 43 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index 87b396786b..390d5f584e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -7,9 +7,13 @@ import io.metersphere.api.dto.ApiParamConfig; import io.metersphere.api.dto.ApiScenarioParamConfig; import io.metersphere.api.dto.debug.ApiDebugRunRequest; 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.parser.TestElementParser; import io.metersphere.api.parser.TestElementParserFactory; +import io.metersphere.api.service.definition.ApiDefinitionModuleService; import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.engine.EngineFactory; 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.GlobalParams; 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.sdk.constants.*; import io.metersphere.sdk.dto.api.task.*; @@ -114,6 +121,8 @@ public class ApiExecuteService { private ExecTaskItemMapper execTaskItemMapper; @Resource private SqlSessionFactory sqlSessionFactory; + @Resource + private ApiDefinitionModuleService apiDefinitionModuleService; @PostConstruct private void init() { @@ -844,9 +853,55 @@ public class ApiExecuteService { public TaskRequestDTO apiExecute(ApiResourceRunRequest runRequest, TaskRequestDTO taskRequest, ApiParamConfig apiParamConfig) { // 设置使用脚本前后置的公共脚本信息 setTestElementParam(runRequest.getTestElement(), taskRequest.getTaskInfo().getProjectId(), taskRequest.getTaskItem()); + // 处理模块匹配 + handleHttpModuleMatchRule(apiParamConfig.getEnvConfig()); return execute(runRequest, taskRequest, apiParamConfig); } + /** + * 处理环境的 HTTP 配置模块匹配规则 + * 查询新增子模块 + * + * @param envInfoDTO + */ + public void handleHttpModuleMatchRule(EnvironmentInfoDTO envInfoDTO) { + if (envInfoDTO == null || envInfoDTO.getConfig() == null) { + return; + } + List httpConfigs = envInfoDTO.getConfig().getHttpConfig(); + for (HttpConfig httpConfig : httpConfigs) { + if (!httpConfig.isModuleMatchRule()) { + continue; + } + // 获取勾选了包含子模块的模块ID + HttpConfigModuleMatchRule moduleMatchRule = httpConfig.getModuleMatchRule(); + List selectModules = moduleMatchRule.getModules(); + + EnvApiModuleRequest envApiModuleRequest = new EnvApiModuleRequest(); + envApiModuleRequest.setProjectId(envInfoDTO.getProjectId()); + List 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 selectedModules = envApiTreeDTO.getSelectedModules(); + List moduleIds = selectedModules.stream().map(ApiModuleDTO::getModuleId).toList(); + + + // 重新设置选中的模块ID + moduleMatchRule.setModules(null); + List 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) { apiCommonService.setEnableCommonScriptProcessorInfo(testElement); testElement.setResourceId(taskItem.getResourceId()); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java index 0552138fcc..0f1ff8eabe 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java @@ -218,6 +218,8 @@ public class ApiTestCaseRunService { apiCommonService.setApiDefinitionExecuteInfo(msTestElement, BeanUtils.copyBean(new ApiDefinitionExecuteInfo(), apiDefinition)); apiExecuteService.setTestElementParam(msTestElement, apiTestCase.getProjectId(), request.getTaskItem()); + // 处理模块匹配 + apiExecuteService.handleHttpModuleMatchRule(apiParamConfig.getEnvConfig()); // 设置环境信息 apiParamConfig.setEnvConfig(environmentService.get(getEnvId(request.getRunModeConfig(), apiTestCase.getEnvironmentId()))); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java index 2ddc963612..3de5df2ba2 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java @@ -919,53 +919,11 @@ public class ApiScenarioRunService { envInfo.setEnvGroupMap(envGroupMap); envInfo.setEnvMap(envMap); - envMap.forEach((envId, envInfoDTO) -> handleHttpModuleMatchRule(envInfoDTO)); + envMap.forEach((envId, envInfoDTO) -> apiExecuteService.handleHttpModuleMatchRule(envInfoDTO)); return envInfo; } - - /** - * 处理环境的 HTTP 配置模块匹配规则 - * 查询新增子模块 - * - * @param envInfoDTO - */ - private void handleHttpModuleMatchRule(EnvironmentInfoDTO envInfoDTO) { - List httpConfigs = envInfoDTO.getConfig().getHttpConfig(); - for (HttpConfig httpConfig : httpConfigs) { - if (!httpConfig.isModuleMatchRule()) { - continue; - } - // 获取勾选了包含子模块的模块ID - HttpConfigModuleMatchRule moduleMatchRule = httpConfig.getModuleMatchRule(); - List selectModules = moduleMatchRule.getModules(); - - EnvApiModuleRequest envApiModuleRequest = new EnvApiModuleRequest(); - envApiModuleRequest.setProjectId(envInfoDTO.getProjectId()); - List 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 selectedModules = envApiTreeDTO.getSelectedModules(); - List moduleIds = selectedModules.stream().map(ApiModuleDTO::getModuleId).toList(); - - - // 重新设置选中的模块ID - moduleMatchRule.setModules(null); - List allSelectModules = moduleIds.stream().map(moduleId -> { - SelectModule module = new SelectModule(); - module.setModuleId(moduleId); - return module; - }).collect(Collectors.toList()); - moduleMatchRule.setModules(allSelectModules); - } - } - private List getApiScenarioByIds(List apiScenarioIds) { ApiScenarioExample example = new ApiScenarioExample(); example.createCriteria().andIdIn(apiScenarioIds);