From f3e2d99fc365ed2994390ea06caecc38ff6a79e0 Mon Sep 17 00:00:00 2001 From: MeterSphere Bot <78466014+metersphere-bot@users.noreply.github.com> Date: Thu, 30 Jun 2022 11:43:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E6=8E=A5=E5=8F=A3=E6=A8=A1=E5=9D=97=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E9=80=BB=E8=BE=91=E9=87=8D=E6=9E=84=20(#15368)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user=郭雨琦 Co-authored-by: guoyuqi --- .../api/service/ApiAutomationService.java | 6 +- .../api/service/ApiModuleService.java | 55 ++++++++++----- .../api/service/ApiScenarioModuleService.java | 69 ++++++++++++------- 3 files changed, 85 insertions(+), 45 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 725c2756f0..a92dd80184 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -1341,6 +1341,9 @@ public class ApiAutomationService { List initData = apiImport.getData(); currentScenarioOrder.remove(); + String defaultVersion = extProjectVersionMapper.getDefaultVersion(request.getProjectId()); + request.setDefaultVersion(defaultVersion); + UpdateScenarioModuleDTO updateScenarioModuleDTO = apiScenarioModuleService.checkScenarioModule(request, initData, StringUtils.equals("fullCoverage", request.getModeId()), request.getCoverModule()); List moduleList = updateScenarioModuleDTO.getModuleList(); List data = updateScenarioModuleDTO.getApiScenarioWithBLOBsList(); @@ -1359,8 +1362,7 @@ public class ApiAutomationService { num = getNextNum(data.get(0).getProjectId()); request.setOpenCustomNum(config.getScenarioCustomNum()); } - String defaultVersion = extProjectVersionMapper.getDefaultVersion(request.getProjectId()); - request.setDefaultVersion(defaultVersion); + for (int i = 0; i < data.size(); i++) { ApiScenarioWithBLOBs item = data.get(i); 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 75ad5e01ec..d1e412f566 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -917,23 +917,23 @@ public class ApiModuleService extends NodeTreeService { } } - private ApiModule getMinModule(String[] tagTree, List moduleList, ApiModule parentModule, Map> pidChildrenMap, Map moduleMap + private ApiModule getMinModule(String[] tagTree, List parentModuleList, ApiModule parentModule, Map> pidChildrenMap, Map moduleMap , Map idPathMap, Map idModuleMap) { //如果parentModule==null 则证明需要创建根目录同级的模块 ApiModule returnModule = null; for (int i = 0; i < tagTree.length; i++) { int finalI = i; - List collect = moduleList.stream().filter(t -> t.getName().equals(tagTree[finalI])).collect(Collectors.toList()); + List collect = parentModuleList.stream().filter(t -> t.getName().equals(tagTree[finalI])).collect(Collectors.toList()); if (collect.isEmpty()) { - if (parentModule == null) { - List moduleList1 = pidChildrenMap.get("root"); - ApiModule apiModule = moduleList1.get(0); - apiModule.setId("root"); - apiModule.setLevel(0); - parentModule = apiModule; - } else if (i > 0) { - if (!moduleList.isEmpty()) { - String parentId = moduleList.get(0).getParentId(); + if (i == 0) { + //证明需要在根目录创建, + parentModule = new ApiModule(); + parentModule.setProjectId(pidChildrenMap.get("root").get(0).getProjectId()); + parentModule.setId("root"); + parentModule.setLevel(0); + } else { + if (!parentModuleList.isEmpty() && parentModule == null) { + String parentId = parentModuleList.get(0).getParentId(); ApiModuleDTO apiModuleDTO = idModuleMap.get(parentId); parentModule = JSON.parseObject(JSON.toJSONString(apiModuleDTO), ApiModule.class); } @@ -941,7 +941,8 @@ public class ApiModuleService extends NodeTreeService { return createModule(tagTree, i, parentModule, moduleMap, pidChildrenMap, idPathMap); } else { returnModule = collect.get(0); - moduleList = pidChildrenMap.get(collect.get(0).getId()); + parentModule = collect.get(0); + parentModuleList = pidChildrenMap.get(collect.get(0).getId()); } } return returnModule; @@ -988,8 +989,9 @@ public class ApiModuleService extends NodeTreeService { private void buildProcessData(List nodeTreeByProjectId, Map> pidChildrenMap, Map idPathMap, Map parentModulePathMap) { //当前层级的模块的所有子模块的集合 - List childrenList = new ArrayList<>(); + Map> idChildrenMap = new HashMap<>(); int i = 0; + Map> idModuleMap = new HashMap<>(); List moduleList = new ArrayList<>(); for (ApiModuleDTO apiModuleDTO : nodeTreeByProjectId) { if (StringUtils.isBlank(apiModuleDTO.getParentId())) { @@ -997,19 +999,24 @@ public class ApiModuleService extends NodeTreeService { } String parentModulePath = parentModulePathMap.get(apiModuleDTO.getParentId()); if (parentModulePath != null) { - apiModuleDTO.setPath(parentModulePath + "/" + apiModuleDTO.getName()); + if (parentModulePath.equals("/root")) { + apiModuleDTO.setPath("/" + apiModuleDTO.getName()); + } else { + apiModuleDTO.setPath(parentModulePath + "/" + apiModuleDTO.getName()); + } } else { apiModuleDTO.setPath("/" + apiModuleDTO.getName()); } idPathMap.put(apiModuleDTO.getId(), apiModuleDTO.getPath()); - ApiModule apiModule = buildModule(moduleList, apiModuleDTO); + ApiModule apiModule = buildModule(idModuleMap, apiModuleDTO); if (pidChildrenMap.get(apiModuleDTO.getParentId()) != null) { pidChildrenMap.get(apiModuleDTO.getParentId()).add(apiModule); } else { - pidChildrenMap.put(apiModuleDTO.getParentId(), moduleList); + pidChildrenMap.put(apiModuleDTO.getParentId(), idModuleMap.get(apiModuleDTO.getId())); } i = i + 1; + List childrenList = idChildrenMap.get(apiModuleDTO.getId()); if (apiModuleDTO.getChildren() != null) { childrenList.addAll(apiModuleDTO.getChildren()); } else { @@ -1021,11 +1028,16 @@ public class ApiModuleService extends NodeTreeService { parentModulePathMap.put(apiModuleDTO.getId(), apiModuleDTO.getPath()); } if (i == nodeTreeByProjectId.size() && nodeTreeByProjectId.size() > 0) { + Collection> values = idChildrenMap.values(); + List childrenList = new ArrayList<>(); + for (List value : values) { + childrenList.addAll(value); + } buildProcessData(childrenList, pidChildrenMap, idPathMap, parentModulePathMap); } } - private ApiModule buildModule(List moduleList, ApiModuleDTO apiModuleDTO) { + private ApiModule buildModule(Map> idModuleMap, ApiModuleDTO apiModuleDTO) { ApiModule apiModule = new ApiModule(); apiModule.setId(apiModuleDTO.getId()); apiModule.setName(apiModuleDTO.getName()); @@ -1033,7 +1045,14 @@ public class ApiModuleService extends NodeTreeService { apiModule.setProjectId(apiModuleDTO.getProjectId()); apiModule.setProtocol(apiModuleDTO.getProtocol()); apiModule.setLevel(apiModuleDTO.getLevel()); - moduleList.add(apiModule); + List moduleList = idModuleMap.get(apiModuleDTO.getId()); + if (moduleList != null) { + moduleList.add(apiModule); + } else { + moduleList = new ArrayList<>(); + moduleList.add(apiModule); + idModuleMap.put(apiModuleDTO.getId(), moduleList); + } return apiModule; } 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 2c60850d4f..7a158c3813 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java @@ -669,9 +669,8 @@ public class ApiScenarioModuleService extends NodeTreeService parentModuleList = pidChildrenMap.get("root"); - ApiScenarioModule minModule = getMinModule(tagTree, parentModuleList, null, pidChildrenMap, map, idPathMap, idModuleMap); + //父级同级的模块list + ApiScenarioModule minModule = getMinModule(tagTree, pidChildrenMap.get("root"), null, pidChildrenMap, map, idPathMap, idModuleMap); String id = minModule.getId(); datum.setApiScenarioModuleId(id); datum.setModulePath(idPathMap.get(id)); @@ -680,7 +679,7 @@ public class ApiScenarioModuleService extends NodeTreeService moduleList = pidChildrenMap.get("root"); for (ApiScenarioModule module : moduleList) { - if (module.getName().equals("未规划接口")) { + if (module.getName().equals("未规划场景")) { datum.setApiScenarioModuleId(module.getId()); datum.setModulePath("/" + module.getName()); } @@ -706,30 +705,30 @@ public class ApiScenarioModuleService extends NodeTreeService parentModuleList, ApiScenarioModule parentModule, Map> pidChildrenMap, Map map, Map idPathMap, Map idModuleMap) { //如果parentModule==null 则证明需要创建根目录同级的模块 ApiScenarioModule returnModule = null; + for (int i = 0; i < tagTree.length; i++) { int finalI = i; //查找上一级里面是否有当前全路径的第一级,没有则需要创建 List collect = parentModuleList.stream().filter(t -> t.getName().equals(tagTree[finalI])).collect(Collectors.toList()); + if (collect.isEmpty()) { - //如果找不到,而且父级模块也为空,证明当前的父级模块应为root - if (parentModule == null) { - List moduleList1 = pidChildrenMap.get("root"); - ApiScenarioModule apiModule = moduleList1.get(0); - apiModule.setId("root"); - apiModule.setLevel(0); - parentModule = apiModule; - } else if (i > 0) { - //如果已经循环第二次级以上,如果父模块不为空, parentModuleList不为空,tagTree[finalI] 找不到,那就从上一级开始创建 - if (!parentModuleList.isEmpty()) { + if (i == 0) { + //证明需要在根目录创建, + parentModule = new ApiScenarioModule(); + parentModule.setProjectId(pidChildrenMap.get("root").get(0).getProjectId()); + parentModule.setId("root"); + parentModule.setLevel(0); + } else { + if (!parentModuleList.isEmpty() && parentModule == null) { String parentId = parentModuleList.get(0).getParentId(); ApiScenarioModuleDTO apiScenarioModuleDTO = idModuleMap.get(parentId); parentModule = JSON.parseObject(JSON.toJSONString(apiScenarioModuleDTO), ApiScenarioModule.class); } } - //开始创建tagTree[finalI]这个模块 return createModule(tagTree, i, parentModule, map, pidChildrenMap, idPathMap); } else { returnModule = collect.get(0); + parentModule = collect.get(0); parentModuleList = pidChildrenMap.get(collect.get(0).getId()); } } @@ -779,51 +778,71 @@ public class ApiScenarioModuleService extends NodeTreeService nodeTreeByProjectId, Map> pidChildrenMap, Map idPathMap, Map parentModulePathMap) { - List childrenList = new ArrayList<>(); + Map> idChildrenMap = new HashMap<>(); int i = 0; - List moduleList = new ArrayList<>(); + Map> idModuleMap = new HashMap<>(); for (ApiScenarioModuleDTO scenarioModuleDTO : nodeTreeByProjectId) { if (StringUtils.isBlank(scenarioModuleDTO.getParentId())) { scenarioModuleDTO.setParentId("root"); } String parentModulePath = parentModulePathMap.get(scenarioModuleDTO.getParentId()); if (parentModulePath != null) { - scenarioModuleDTO.setPath(parentModulePath + "/" + scenarioModuleDTO.getName()); + if (parentModulePath.equals("/root")) { + scenarioModuleDTO.setPath("/" + scenarioModuleDTO.getName()); + } else { + scenarioModuleDTO.setPath(parentModulePath + "/" + scenarioModuleDTO.getName()); + } } else { scenarioModuleDTO.setPath("/" + scenarioModuleDTO.getName()); } idPathMap.put(scenarioModuleDTO.getId(), scenarioModuleDTO.getPath()); - ApiScenarioModule scenarioModule = buildModule(moduleList, scenarioModuleDTO); + ApiScenarioModule scenarioModule = buildModule(idModuleMap, scenarioModuleDTO); if (pidChildrenMap.get(scenarioModuleDTO.getParentId()) != null) { pidChildrenMap.get(scenarioModuleDTO.getParentId()).add(scenarioModule); } else { - pidChildrenMap.put(scenarioModuleDTO.getParentId(), moduleList); + pidChildrenMap.put(scenarioModuleDTO.getParentId(), idModuleMap.get(scenarioModuleDTO.getId())); } i = i + 1; + List childrenList = idChildrenMap.get(scenarioModuleDTO.getId()); if (scenarioModuleDTO.getChildren() != null) { - childrenList.addAll(scenarioModuleDTO.getChildren()); + if (childrenList != null) { + childrenList.addAll(scenarioModuleDTO.getChildren()); + } else { + idChildrenMap.put(scenarioModuleDTO.getId(), scenarioModuleDTO.getChildren()); + } } else { - childrenList.addAll(new ArrayList<>()); - if (i == nodeTreeByProjectId.size() && childrenList.size() == 0) { + if (i == nodeTreeByProjectId.size() && idChildrenMap.size() == 0) { pidChildrenMap.put(scenarioModuleDTO.getId(), new ArrayList<>()); } } parentModulePathMap.put(scenarioModuleDTO.getId(), scenarioModuleDTO.getPath()); } if (i == nodeTreeByProjectId.size() && nodeTreeByProjectId.size() > 0) { + Collection> values = idChildrenMap.values(); + List childrenList = new ArrayList<>(); + for (List value : values) { + childrenList.addAll(value); + } buildProcessData(childrenList, pidChildrenMap, idPathMap, parentModulePathMap); } } - private ApiScenarioModule buildModule(List moduleList, ApiScenarioModuleDTO scenarioModuleDTO) { + private ApiScenarioModule buildModule(Map> IdModuleMap, ApiScenarioModuleDTO scenarioModuleDTO) { ApiScenarioModule scenarioModule = new ApiScenarioModule(); scenarioModule.setId(scenarioModuleDTO.getId()); scenarioModule.setName(scenarioModuleDTO.getName()); scenarioModule.setParentId(scenarioModuleDTO.getParentId()); scenarioModule.setProjectId(scenarioModuleDTO.getProjectId()); scenarioModule.setLevel(scenarioModuleDTO.getLevel()); - moduleList.add(scenarioModule); + List moduleList = IdModuleMap.get(scenarioModuleDTO.getId()); + if (moduleList != null) { + moduleList.add(scenarioModule); + } else { + moduleList = new ArrayList<>(); + moduleList.add(scenarioModule); + IdModuleMap.put(scenarioModuleDTO.getId(), moduleList); + } return scenarioModule; } }