From 7bccd9f50242907bc3c50e7ef20e533d2a2f40d4 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sun, 27 Sep 2020 18:00:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=AA=E8=83=BD=E5=AF=BC=E5=85=A5=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E7=94=A8=E4=BE=8B=E7=9A=84=E7=A9=BA=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../track/service/TestCaseNodeService.java | 39 +++++++--------- .../track/service/TestCaseService.java | 3 ++ .../io/metersphere/xmind/XmindCaseParser.java | 44 ++++++++++++++++--- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java index 664012c7ca..f768dd2567 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java @@ -230,7 +230,7 @@ public class TestCaseNodeService { return list; } - + private List getNodeDTO(String projectId, String planId) { TestPlanTestCaseExample testPlanTestCaseExample = new TestPlanTestCaseExample(); testPlanTestCaseExample.createCriteria().andPlanIdEqualTo(planId); @@ -355,50 +355,45 @@ public class TestCaseNodeService { } public Map createNodeByTestCases(List testCases, String projectId) { - - List nodeTrees = getNodeTreeByProjectId(projectId); - - Map pathMap = new HashMap<>(); - List nodePaths = testCases.stream() .map(TestCase::getNodePath) .collect(Collectors.toList()); - nodePaths.forEach(path -> { + return this.createNodes(nodePaths, projectId); + } - if (path == null) { + public Map createNodes(List nodePaths, String projectId) { + List nodeTrees = getNodeTreeByProjectId(projectId); + Map pathMap = new HashMap<>(); + for(String item : nodePaths){ + if (item == null) { throw new ExcelException(Translator.get("test_case_module_not_null")); } - List nodeNameList = new ArrayList<>(Arrays.asList(path.split("/"))); - Iterator pathIterator = nodeNameList.iterator(); - + List nodeNameList = new ArrayList<>(Arrays.asList(item.split("/"))); + Iterator itemIterator = nodeNameList.iterator(); Boolean hasNode = false; String rootNodeName = null; if (nodeNameList.size() <= 1) { - throw new ExcelException(Translator.get("test_case_create_module_fail") + ":" + path); + throw new ExcelException(Translator.get("test_case_create_module_fail") + ":" + item); } else { - pathIterator.next(); - pathIterator.remove(); - - rootNodeName = pathIterator.next().trim(); + itemIterator.next(); + itemIterator.remove(); + rootNodeName = itemIterator.next().trim(); //原来没有,新建的树nodeTrees也不包含 for (TestCaseNodeDTO nodeTree : nodeTrees) { if (StringUtils.equals(rootNodeName, nodeTree.getName())) { hasNode = true; - createNodeByPathIterator(pathIterator, "/" + rootNodeName, nodeTree, + createNodeByPathIterator(itemIterator, "/" + rootNodeName, nodeTree, pathMap, projectId, 2); } ; } } - - if (!hasNode) { - createNodeByPath(pathIterator, rootNodeName, null, projectId, 1, "", pathMap); + createNodeByPath(itemIterator, rootNodeName, null, projectId, 1, "", pathMap); } - }); - + } return pathMap; } diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index bdf3db6b67..be5b4fa4e2 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -284,6 +284,9 @@ public class TestCaseService { errList.add(excelErrData); excelResponse.setErrList(errList); } else { + if (!xmindParser.getNodePaths().isEmpty()) { + testCaseNodeService.createNodes(xmindParser.getNodePaths(), projectId); + } if (!xmindParser.getTestCase().isEmpty()) { this.saveImportData(xmindParser.getTestCase(), projectId); xmindParser.clear(); diff --git a/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java b/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java index f317424b19..e12c0f1c71 100644 --- a/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java +++ b/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java @@ -40,6 +40,9 @@ public class XmindCaseParser { // 案例详情重写了hashCode方法去重用 private List compartDatas; + // 记录没有用例的目录 + private List nodePaths; + public XmindCaseParser(TestCaseService testCaseService, String userId, String projectId, Set testCaseNames) { this.testCaseService = testCaseService; this.maintainer = userId; @@ -48,6 +51,7 @@ public class XmindCaseParser { testCases = new LinkedList<>(); compartDatas = new ArrayList<>(); process = new StringBuffer(); + nodePaths = new ArrayList<>(); } // 这里清理是为了 加快jvm 回收 @@ -55,26 +59,55 @@ public class XmindCaseParser { compartDatas.clear(); testCases.clear(); testCaseNames.clear(); + nodePaths.clear(); } public List getTestCase() { return this.testCases; } + public List getNodePaths() { + return this.nodePaths; + } + private final Map caseTypeMap = ImmutableMap.of("功能测试", "functional", "性能测试", "performance", "接口测试", "api"); + public void validate() { + nodePaths.forEach(nodePath -> { + String[] nodes = nodePath.split("/"); + if (nodes.length > TestCaseConstants.MAX_NODE_DEPTH + 1) { + process.append(Translator.get("test_case_node_level_tip") + + TestCaseConstants.MAX_NODE_DEPTH + Translator.get("test_case_node_level") + "; "); + } + for (int i = 0; i < nodes.length; i++) { + if (i != 0 && StringUtils.equals(nodes[i].trim(), "")) { + process.append(Translator.get("module_not_null") + "; "); + break; + } + } + }); + } + // 递归处理案例数据 - private void recursion(StringBuffer processBuffer, Attached parent, int level, String nodePath, List attacheds) { + private void recursion(StringBuffer processBuffer, Attached parent, int level, List attacheds) { for (Attached item : attacheds) { if (isAvailable(item.getTitle(), "(?:tc:|tc:|tc)")) { // 用例 item.setParent(parent); this.newTestCase(item.getTitle(), parent.getPath(), item.getChildren() != null ? item.getChildren().getAttached() : null); } else { - nodePath = parent.getPath() + "/" + item.getTitle(); + String nodePath = parent.getPath() + "/" + item.getTitle(); item.setPath(nodePath); + item.setParent(parent); if (item.getChildren() != null && !item.getChildren().getAttached().isEmpty()) { - item.setParent(parent); - recursion(processBuffer, item, level + 1, nodePath, item.getChildren().getAttached()); + recursion(processBuffer, item, level + 1, item.getChildren().getAttached()); + } else { + if (!nodePath.startsWith("/")) { + nodePath = "/" + nodePath; + } + if (nodePath.endsWith("/")) { + nodePath = nodePath.substring(0, nodePath.length() - 1); + } + nodePaths.add(nodePath); // 没有用例的路径 } } } @@ -243,11 +276,12 @@ public class XmindCaseParser { item.setPath(item.getTitle()); if (item.getChildren() != null && !item.getChildren().getAttached().isEmpty()) { item.setPath(item.getTitle()); - recursion(processBuffer, item, 1, item.getPath(), item.getChildren().getAttached()); + recursion(processBuffer, item, 1, item.getChildren().getAttached()); } } } } + this.validate(); } catch (Exception ex) { processBuffer.append(Translator.get("incorrect_format")); LogUtil.error(ex.getMessage());