diff --git a/api-test/backend/src/main/java/io/metersphere/listener/ProjectCreatedListener.java b/api-test/backend/src/main/java/io/metersphere/listener/ProjectCreatedListener.java index af84756ec2..9380cda2fa 100644 --- a/api-test/backend/src/main/java/io/metersphere/listener/ProjectCreatedListener.java +++ b/api-test/backend/src/main/java/io/metersphere/listener/ProjectCreatedListener.java @@ -1,21 +1,21 @@ package io.metersphere.listener; -import io.metersphere.base.domain.ApiModule; -import io.metersphere.base.domain.ModuleNode; -import io.metersphere.base.domain.Project; +import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiModuleMapper; +import io.metersphere.base.mapper.ApiScenarioModuleMapper; import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.ext.BaseModuleNodeMapper; import io.metersphere.commons.constants.KafkaTopicConstants; import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.SessionUtils; +import org.apache.commons.collections.CollectionUtils; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.List; import java.util.UUID; @Component @@ -28,6 +28,8 @@ public class ProjectCreatedListener { @Resource private ApiModuleMapper apiModuleMapper; @Resource + private ApiScenarioModuleMapper apiScenarioModuleMapper; + @Resource private ProjectMapper projectMapper; @@ -52,7 +54,12 @@ public class ProjectCreatedListener { record.setUpdateTime(System.currentTimeMillis()); record.setProjectId(projectId); record.setName(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()); - baseModuleNodeMapper.insert(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getTableName(), record); + ApiScenarioModuleExample scenarioModuleExample = new ApiScenarioModuleExample(); + scenarioModuleExample.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()).andParentIdIsNull(); + List scenarioModules = apiScenarioModuleMapper.selectByExample(scenarioModuleExample); + if (CollectionUtils.isEmpty(scenarioModules)) { + baseModuleNodeMapper.insert(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getTableName(), record); + } ApiModule apiRecord = new ApiModule(); BeanUtils.copyBean(apiRecord, record); apiRecord.setName(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()); @@ -60,7 +67,12 @@ public class ProjectCreatedListener { for (String protocol : protocols) { apiRecord.setProtocol(protocol); apiRecord.setId(UUID.randomUUID().toString()); - apiModuleMapper.insert(apiRecord); + ApiModuleExample apiExample = new ApiModuleExample(); + apiExample.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()).andParentIdIsNull(); + List apiList = apiModuleMapper.selectByExample(apiExample); + if (CollectionUtils.isEmpty(apiList)) { + apiModuleMapper.insert(apiRecord); + } } } } diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtil.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtil.java index 7e41354d09..dcd8a49aac 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtil.java @@ -9,6 +9,7 @@ import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiModuleMapper; import io.metersphere.base.mapper.ApiTestCaseMapper; import io.metersphere.commons.constants.NoticeConstants; +import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.enums.ApiTestDataStatus; import io.metersphere.commons.utils.BeanUtils; @@ -355,7 +356,7 @@ public class ApiDefinitionImportUtil { public static void setModule(ApiDefinitionWithBLOBs item, ApiModuleMapper apiModuleMapper) { if (item != null && StringUtils.isEmpty(item.getModuleId()) || "default-module".equals(item.getModuleId())) { ApiModuleExample example = new ApiModuleExample(); - example.createCriteria().andProjectIdEqualTo(item.getProjectId()).andProtocolEqualTo(item.getProtocol()).andNameEqualTo("未规划接口"); + example.createCriteria().andProjectIdEqualTo(item.getProjectId()).andProtocolEqualTo(item.getProtocol()).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()); List modules = apiModuleMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(modules)) { item.setModuleId(modules.get(0).getId()); diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java index 1e44428a86..a50b3f1831 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java @@ -1003,7 +1003,7 @@ public class ApiDefinitionService { private void setModule(ApiDefinitionWithBLOBs item) { if (item != null && StringUtils.isEmpty(item.getModuleId()) || "default-module".equals(item.getModuleId())) { ApiModuleExample example = new ApiModuleExample(); - example.createCriteria().andProjectIdEqualTo(item.getProjectId()).andProtocolEqualTo(item.getProtocol()).andNameEqualTo("未规划接口"); + example.createCriteria().andProjectIdEqualTo(item.getProjectId()).andProtocolEqualTo(item.getProtocol()).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()); List modules = apiModuleMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(modules)) { item.setModuleId(modules.get(0).getId()); @@ -1840,7 +1840,7 @@ public class ApiDefinitionService { public void initModulePathAndId(String projectId, ApiDefinitionWithBLOBs test) { ApiModuleExample example = new ApiModuleExample(); - example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(test.getProtocol()).andNameEqualTo("未规划接口").andLevelEqualTo(1); + example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(test.getProtocol()).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()).andLevelEqualTo(1); List modules = apiModuleMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(modules)) { test.setModuleId(modules.get(0).getId()); diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java index 1e8d1d577f..b8d75361ce 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java @@ -1,24 +1,15 @@ package io.metersphere.service.definition; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiDefinitionRequest; -import io.metersphere.api.dto.definition.ApiDefinitionResult; -import io.metersphere.api.dto.definition.ApiModuleDTO; -import io.metersphere.api.dto.definition.ApiTestCaseRequest; -import io.metersphere.api.dto.definition.DragModuleRequest; -import io.metersphere.api.dto.definition.UpdateApiModuleDTO; +import io.metersphere.api.dto.definition.*; import io.metersphere.api.parse.api.ApiDefinitionImport; -import io.metersphere.base.domain.ApiDefinition; -import io.metersphere.base.domain.ApiDefinitionExample; -import io.metersphere.base.domain.ApiDefinitionWithBLOBs; -import io.metersphere.base.domain.ApiModule; -import io.metersphere.base.domain.ApiModuleExample; -import io.metersphere.base.domain.ApiTestCaseWithBLOBs; +import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiDefinitionMapper; import io.metersphere.base.mapper.ApiModuleMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper; import io.metersphere.base.mapper.ext.ExtApiModuleMapper; import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; +import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.TestCaseConstants; import io.metersphere.commons.enums.ApiTestDataStatus; @@ -44,15 +35,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -204,7 +187,7 @@ public class ApiModuleService extends NodeTreeService { return getNodeTrees(apiModules); } - public List getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request ) { + public List getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request) { // 判断当前项目下是否有默认模块,没有添加默认模块 this.getDefaultNode(projectId, protocol); List apiModules = getApiModulesByProjectAndPro(projectId, protocol); @@ -588,12 +571,12 @@ public class ApiModuleService extends NodeTreeService { public ApiModule getDefaultNode(String projectId, String protocol) { ApiModuleExample example = new ApiModuleExample(); - example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("未规划接口").andParentIdIsNull(); + example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()).andParentIdIsNull(); List list = apiModuleMapper.selectByExample(example); if (CollectionUtils.isEmpty(list)) { ApiModule record = new ApiModule(); record.setId(UUID.randomUUID().toString()); - record.setName("未规划接口"); + record.setName(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()); record.setProtocol(protocol); record.setPos(1.0); record.setLevel(1); @@ -608,17 +591,6 @@ public class ApiModuleService extends NodeTreeService { } } - public ApiModule getDefaultNodeUnCreateNew(String projectId, String protocol) { - ApiModuleExample example = new ApiModuleExample(); - example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("未规划接口").andParentIdIsNull(); - List list = apiModuleMapper.selectByExample(example); - if (CollectionUtils.isEmpty(list)) { - return null; - } else { - return list.get(0); - } - } - public long countTrashApiData(String projectId, String protocol) { ApiDefinitionExample example = new ApiDefinitionExample(); example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andStatusEqualTo("Trash"); @@ -666,7 +638,7 @@ public class ApiModuleService extends NodeTreeService { if (protocol.equals("HTTP")) { return dealHttp(data, pidChildrenMap, idPathMap, idModuleMap, request, fullCoverage, urlRepeat, importCases); } else { - return delOtherProtocol(data, pidChildrenMap, idPathMap, idModuleMap, request, fullCoverage, importCases); + return delOtherProtocol(data, pidChildrenMap, idPathMap, idModuleMap, request, fullCoverage, importCases); } } @@ -693,7 +665,7 @@ public class ApiModuleService extends NodeTreeService { //处理模块 setModule(moduleMap, pidChildrenMap, idPathMap, idModuleMap, optionData, chooseModule); - return getUpdateApiModuleDTO(chooseModule,idPathMap,optionData,fullCoverage, moduleMap,optionDataCases); + return getUpdateApiModuleDTO(chooseModule, idPathMap, optionData, fullCoverage, moduleMap, optionDataCases); } private UpdateApiModuleDTO dealHttp(List data, @@ -722,8 +694,7 @@ public class ApiModuleService extends NodeTreeService { setModule(moduleMap, pidChildrenMap, idPathMap, idModuleMap, optionData, chooseModule); - - return getUpdateApiModuleDTO(chooseModule,idPathMap,optionData,fullCoverage, moduleMap,optionDataCases); + return getUpdateApiModuleDTO(chooseModule, idPathMap, optionData, fullCoverage, moduleMap, optionDataCases); } @@ -752,7 +723,7 @@ public class ApiModuleService extends NodeTreeService { } } - private UpdateApiModuleDTO getUpdateApiModuleDTO(ApiModuleDTO chooseModule,Map idPathMap,List optionData,Boolean fullCoverage,Map moduleMap, List optionDataCases) { + private UpdateApiModuleDTO getUpdateApiModuleDTO(ApiModuleDTO chooseModule, Map idPathMap, List optionData, Boolean fullCoverage, Map moduleMap, List optionDataCases) { UpdateApiModuleDTO updateApiModuleDTO = new UpdateApiModuleDTO(); updateApiModuleDTO.setChooseModule(chooseModule); updateApiModuleDTO.setIdPathMap(idPathMap); @@ -822,7 +793,7 @@ public class ApiModuleService extends NodeTreeService { //导入时即没选中模块,接口自身也没模块的,直接返会当前项目,当前协议下的默认模块 List moduleList = pidChildrenMap.get(PropertyConstant.ROOT); for (ApiModule module : moduleList) { - if (module.getName().equals("未规划接口")) { + if (module.getName().equals(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName())) { datum.setModuleId(module.getId()); datum.setModulePath("/" + module.getName()); } @@ -846,6 +817,7 @@ public class ApiModuleService extends NodeTreeService { datum.setModulePath(idPathMap.get(chooseModule.getId())); } } + public String[] getPathTree(String modulePath) { String substring = modulePath.substring(0, 1); if (substring.equals("/")) { diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java index cac52aed90..c83af33aa2 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java @@ -11,6 +11,7 @@ import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ApiScenarioModuleMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioModuleMapper; +import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.TestCaseConstants; import io.metersphere.commons.enums.ApiTestDataStatus; @@ -157,7 +158,7 @@ public class ApiScenarioModuleService extends NodeTreeService statusList = new ArrayList<>(); statusList.add(ApiTestDataStatus.TRASH.getValue()); request.getFilters().put("status", statusList); @@ -507,12 +508,12 @@ public class ApiScenarioModuleService extends NodeTreeService list = apiScenarioModuleMapper.selectByExample(example); if (CollectionUtils.isEmpty(list)) { ApiScenarioModule record = new ApiScenarioModule(); record.setId(UUID.randomUUID().toString()); - record.setName("未规划场景"); + record.setName(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()); record.setPos(1.0); record.setLevel(1); record.setCreateTime(System.currentTimeMillis()); @@ -825,7 +826,7 @@ public class ApiScenarioModuleService extends NodeTreeService moduleList = pidChildrenMap.get(PropertyConstant.ROOT); for (ApiScenarioModule module : moduleList) { - if (module.getName().equals("未规划场景")) { + if (module.getName().equals(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName())) { datum.setApiScenarioModuleId(module.getId()); datum.setModulePath("/" + module.getName()); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java index 71a15a0ef8..442a4bfe32 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java @@ -1478,7 +1478,7 @@ public class ApiScenarioService { private void replenishScenarioModuleIdPath(String request, ApiScenarioModuleMapper apiScenarioModuleMapper, ApiScenarioWithBLOBs item) { ApiScenarioModuleExample example = new ApiScenarioModuleExample(); - example.createCriteria().andProjectIdEqualTo(request).andNameEqualTo("未规划场景"); + example.createCriteria().andProjectIdEqualTo(request).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()); List modules = apiScenarioModuleMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(modules)) { item.setApiScenarioModuleId(modules.get(0).getId());