diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index 036aed881d..b27df48e1f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -661,7 +661,7 @@ public class ApiModuleService extends NodeTreeService { } List optionData = new ArrayList<>(); - + List repeatApiDefinitionWithBLOBs; if (protocol.equals("HTTP")) { //去重 如果url可重复 则模块+名称+请求方式+路径 唯一,否则 请求方式+路径唯一, //覆盖模式留重复的最后一个,不覆盖留第一个 @@ -670,12 +670,25 @@ public class ApiModuleService extends NodeTreeService { //处理模块 setModule(moduleMap, pidChildrenMap, idPathMap, idModuleMap, optionData, chooseModule); //系统内重复的数据 - List repeatApiDefinitionWithBLOBs = getRepeatBLOBsList(projectId, versionSet, chooseModule, optionData, urlRepeat); + repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByBLOBs(optionData, projectId, versionSet); //处理数据 if (urlRepeat) { moduleMap = getRepeatApiModuleMap(fullCoverage, fullCoverageApi, moduleMap, toUpdateList, idPathMap, chooseModule, optionData, repeatApiDefinitionWithBLOBs); + //最后在整个体统内检查一遍 + if (!repeatApiDefinitionWithBLOBs.isEmpty()) { + Map> repeatMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.groupingBy(t -> t.getName() + t.getMethod() + t.getPath() + t.getModulePath())); + Map optionMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + t.getMethod() + t.getPath() + t.getModulePath(), api -> api)); + if (fullCoverage) { + startCover(toUpdateList, optionData, optionMap, repeatMap); + } else { + //不覆盖,同一接口不做更新 + if (!repeatApiDefinitionWithBLOBs.isEmpty()) { + removeSameData(repeatMap, optionMap, optionData); + } + } + } } else { - moduleMap = getOnlyApiModuleMap(fullCoverage, fullCoverageApi, moduleMap, toUpdateList, idPathMap, chooseModule, optionData, repeatApiDefinitionWithBLOBs); + moduleMap = getOnlyApiModuleMap(fullCoverage, fullCoverageApi, moduleMap, toUpdateList, optionData, repeatApiDefinitionWithBLOBs); } } else { //去重,TCP,SQL,DUBBO 模块下名称唯一 @@ -688,26 +701,55 @@ public class ApiModuleService extends NodeTreeService { List nameList = optionData.stream().map(ApiDefinitionWithBLOBs::getName).collect(Collectors.toList()); //获取系统内重复数据 - List repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByProtocol(nameList, protocol, versionSet); + repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByProtocol(nameList, protocol, versionSet); - Map repeatDataMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), api -> api)); - Map nameModuleMap = getNameApiMap(idPathMap, chooseModule, optionData, repeatApiDefinitionWithBLOBs); + moduleMap = getOtherApiModuleMap(fullCoverage, fullCoverageApi, chooseModuleId, moduleMap, toUpdateList, idPathMap, chooseModule, optionData, repeatApiDefinitionWithBLOBs); - //处理数据 - if (fullCoverage) { - if (fullCoverageApi) { - coverModule(toUpdateList, nameModuleMap, repeatDataMap); + if (!repeatApiDefinitionWithBLOBs.isEmpty()) { + Map repeatMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), api -> api)); + Map optionMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), api -> api)); + if (fullCoverage) { + cover(moduleMap, toUpdateList, optionMap, repeatMap); } else { - moduleMap = cover(moduleMap, toUpdateList, nameModuleMap, repeatDataMap); + //不覆盖,同一接口不做更新 + removeRepeat(optionData, optionMap, repeatMap); } - } else { - //不覆盖 - removeRepeat(optionData, nameModuleMap, repeatDataMap); } } + return getUpdateApiModuleDTO(moduleMap, toUpdateList, optionData); } + private Map getOtherApiModuleMap(Boolean fullCoverage, Boolean fullCoverageApi, String chooseModuleId, Map moduleMap, List toUpdateList, Map idPathMap, ApiModuleDTO chooseModule, List optionData, List repeatApiDefinitionWithBLOBs) { + Map repeatDataMap = null; + + Map nameModuleMap = null; + if (chooseModule != null) { + if (!repeatApiDefinitionWithBLOBs.isEmpty()) { + String chooseModuleParentId = getChooseModuleParentId(chooseModule); + String chooseModulePath = getChooseModulePath(idPathMap, chooseModule, chooseModuleParentId); + nameModuleMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + chooseModulePath, api -> api)); + repeatDataMap = repeatApiDefinitionWithBLOBs.stream().filter(t -> t.getModuleId().equals(chooseModuleId)).collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), api -> api)); + } + } else { + nameModuleMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + (t.getModulePath() == null ? "" : t.getModulePath()), api -> api)); + repeatDataMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), api -> api)); + } + + //处理数据 + if (fullCoverage) { + if (fullCoverageApi) { + coverModule(toUpdateList, nameModuleMap, repeatDataMap); + } else { + moduleMap = cover(moduleMap, toUpdateList, nameModuleMap, repeatDataMap); + } + } else { + //不覆盖 + removeRepeat(optionData, nameModuleMap, repeatDataMap); + } + return moduleMap; + } + private UpdateApiModuleDTO getUpdateApiModuleDTO(Map moduleMap, List toUpdateList, List optionData) { UpdateApiModuleDTO updateApiModuleDTO = new UpdateApiModuleDTO(); @@ -717,31 +759,6 @@ public class ApiModuleService extends NodeTreeService { return updateApiModuleDTO; } - private Map getNameApiMap(Map idPathMap, ApiModuleDTO chooseModule, List optionData, List repeatApiDefinitionWithBLOBs) { - Map nameModuleMap = null; - if (chooseModule != null) { - if (!repeatApiDefinitionWithBLOBs.isEmpty()) { - String chooseModuleParentId = getChooseModuleParentId(chooseModule); - String chooseModulePath = getChooseModulePath(idPathMap, chooseModule, chooseModuleParentId); - nameModuleMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + chooseModulePath, api -> api)); - } - } else { - nameModuleMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + (t.getModulePath() == null ? "" : t.getModulePath()), api -> api)); - } - return nameModuleMap; - } - - private List getRepeatBLOBsList(String projectId, Set versionSet, ApiModuleDTO chooseModule, List optionData, boolean urlRepeat) { - List repeatApiDefinitionWithBLOBs; - - if (chooseModule != null && urlRepeat) { - repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByBLOBsSameUrl(optionData, projectId, chooseModule.getId(), versionSet); - } else { - repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByBLOBs(optionData, projectId, versionSet); - } - return repeatApiDefinitionWithBLOBs; - } - private void removeRepeat(List optionData, Map nameModuleMap, Map repeatDataMap) { if (nameModuleMap != null) { Map finalNameModuleMap = nameModuleMap; @@ -803,7 +820,7 @@ public class ApiModuleService extends NodeTreeService { } } - private Map getOnlyApiModuleMap(Boolean fullCoverage, Boolean fullCoverageApi, Map moduleMap, List toUpdateList, Map idPathMap, ApiModuleDTO chooseModule, List optionData, List repeatApiDefinitionWithBLOBs) { + private Map getOnlyApiModuleMap(Boolean fullCoverage, Boolean fullCoverageApi, Map moduleMap, List toUpdateList, List optionData, List repeatApiDefinitionWithBLOBs) { Map methodPathMap; Map> repeatDataMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.groupingBy(t -> t.getMethod() + t.getPath())); @@ -836,20 +853,22 @@ public class ApiModuleService extends NodeTreeService { private Map getRepeatApiModuleMap(Boolean fullCoverage, Boolean fullCoverageApi, Map moduleMap, List toUpdateList, Map idPathMap, ApiModuleDTO chooseModule, List optionData, List repeatApiDefinitionWithBLOBs) { Map methodPathMap; + Map> repeatDataMap; //按照原来的顺序 if (chooseModule != null) { String chooseModuleParentId = getChooseModuleParentId(chooseModule); String chooseModulePath = getChooseModulePath(idPathMap, chooseModule, chooseModuleParentId); methodPathMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + t.getMethod() + t.getPath() + chooseModulePath, api -> api)); + repeatDataMap = repeatApiDefinitionWithBLOBs.stream().filter(t -> t.getModuleId().equals(chooseModule.getId())).collect(Collectors.groupingBy(t -> t.getName() + t.getMethod() + t.getPath() + t.getModulePath())); } else { methodPathMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + t.getMethod() + t.getPath() + (t.getModulePath() == null ? "" : t.getModulePath()), api -> api)); + repeatDataMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.groupingBy(t -> t.getName() + t.getMethod() + t.getPath() + t.getModulePath())); } - Map> repeatDataMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.groupingBy(t -> t.getName() + t.getMethod() + t.getPath() + t.getModulePath())); //覆盖接口 if (fullCoverage) { - //允许覆盖模块,用导入的重复数据的最后一条覆盖查询的所有重复数据 + //允许覆盖模块,用导入的重复数据的最后一条覆盖查询的所有重复数据 if (fullCoverageApi) { if (!repeatApiDefinitionWithBLOBs.isEmpty()) { startCoverModule(toUpdateList, optionData, methodPathMap, repeatDataMap); @@ -866,7 +885,6 @@ public class ApiModuleService extends NodeTreeService { if (!repeatApiDefinitionWithBLOBs.isEmpty()) { removeSameData(repeatDataMap, methodPathMap, optionData); } - } return moduleMap; } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java index 25c0dcdd86..3bc1408ab6 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java @@ -530,23 +530,41 @@ public class ApiScenarioModuleService extends NodeTreeService repeatApiScenarioWithBLOBs; - if (chooseModule != null) { - repeatApiScenarioWithBLOBs = extApiScenarioMapper.selectRepeatByBLOBsSameUrl(optionData, projectId, chooseModule.getId(), versionSet); - } else { - repeatApiScenarioWithBLOBs = extApiScenarioMapper.selectRepeatByBLOBs(optionData, projectId, versionSet); + List repeatApiScenarioWithBLOBs = extApiScenarioMapper.selectRepeatByBLOBs(optionData, projectId, versionSet); + + moduleMap = getApiScenarioModuleMap(fullCoverage, fullCoverageScenario, moduleMap, toUpdateList, chooseModuleId, idPathMap, chooseModule, optionData, repeatApiScenarioWithBLOBs); + + if (!repeatApiScenarioWithBLOBs.isEmpty()) { + Map repeatMap = repeatApiScenarioWithBLOBs.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), scenario -> scenario)); + Map optionMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), scenario -> scenario)); + if (fullCoverage) { + startCover(toUpdateList, optionMap, repeatMap); + } else { + //不覆盖,同一接口不做更新 + removeRepeat(optionData, optionMap, repeatMap); + } } + UpdateScenarioModuleDTO updateScenarioModuleDTO = new UpdateScenarioModuleDTO(); + updateScenarioModuleDTO.setModuleList(new ArrayList<>(moduleMap.values())); + updateScenarioModuleDTO.setNeedUpdateList(toUpdateList); + updateScenarioModuleDTO.setApiScenarioWithBLOBsList(optionData); + return updateScenarioModuleDTO; + } + + private Map getApiScenarioModuleMap(Boolean fullCoverage, Boolean fullCoverageScenario, Map moduleMap, List toUpdateList, String chooseModuleId, Map idPathMap, ApiScenarioModuleDTO chooseModule, List optionData, List repeatApiScenarioWithBLOBs) { Map nameModuleMap = null; - Map repeatDataMap = repeatApiScenarioWithBLOBs.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), scenario -> scenario)); + Map repeatDataMap = null; if (chooseModule != null) { if (!repeatApiScenarioWithBLOBs.isEmpty()) { String chooseModuleParentId = getChooseModuleParentId(chooseModule); String chooseModulePath = getChooseModulePath(idPathMap, chooseModule, chooseModuleParentId); nameModuleMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + chooseModulePath, scenario -> scenario)); + repeatDataMap = repeatApiScenarioWithBLOBs.stream().filter(t -> t.getApiScenarioModuleId().equals(chooseModuleId)).collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), scenario -> scenario)); } } else { nameModuleMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + (t.getModulePath() == null ? "" : t.getModulePath()), scenario -> scenario)); + repeatDataMap = repeatApiScenarioWithBLOBs.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), scenario -> scenario)); } //处理数据 if (fullCoverage) { @@ -564,12 +582,7 @@ public class ApiScenarioModuleService extends NodeTreeService(moduleMap.values())); - updateScenarioModuleDTO.setNeedUpdateList(toUpdateList); - updateScenarioModuleDTO.setApiScenarioWithBLOBsList(optionData); - return updateScenarioModuleDTO; + return moduleMap; } private void setFullVersionSet(ApiTestImportRequest request, Set versionSet) {