From a46f3cfbba6c76f26c2410ce18515a537d942282 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 12 Jan 2021 15:53:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8BXMIND=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=EF=BC=8C=E9=87=8D=E5=A4=8D=E5=86=85=E5=AE=B9=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../track/service/TestCaseService.java | 56 +++++++++++++++---- .../io/metersphere/xmind/XmindCaseParser.java | 29 ++++++++-- 2 files changed, 67 insertions(+), 18 deletions(-) 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 9237735195..5666e3285b 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -39,7 +39,7 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.CollectionUtils; +import org.apache.commons.collections.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; @@ -119,7 +119,7 @@ public class TestCaseService { return testCaseMapper.updateByPrimaryKeySelective(testCase); } - private void checkTestCaseExist(TestCaseWithBLOBs testCase) { + public TestCaseWithBLOBs checkTestCaseExist(TestCaseWithBLOBs testCase) { // 全部字段值相同才判断为用例存在 if (testCase != null) { @@ -152,17 +152,22 @@ public class TestCaseService { List caseList = testCaseMapper.selectByExampleWithBLOBs(example); // 如果上边字段全部相同,去检查 steps 和 remark + boolean isExt = false; if (!CollectionUtils.isEmpty(caseList)) { - caseList.forEach(tc -> { + for (TestCaseWithBLOBs tc : caseList) { String steps = tc.getSteps(); String remark = tc.getRemark(); if (StringUtils.equals(steps, testCase.getSteps()) && StringUtils.equals(remark, testCase.getRemark())) { - MSException.throwException(Translator.get("test_case_already_exists")); + // MSException.throwException(Translator.get("test_case_already_exists")); + isExt = true; } - }); + } + } + if (isExt) { + return caseList.get(0); } - } + return null; } public int deleteTestCase(String testCaseId) { @@ -260,7 +265,9 @@ public class TestCaseService { try { XmindCaseParser xmindParser = new XmindCaseParser(this, userId, projectId, testCaseNames); errList = xmindParser.parse(multipartFile); - if (xmindParser.getNodePaths().isEmpty() && xmindParser.getTestCase().isEmpty()) { + if (CollectionUtils.isEmpty(xmindParser.getNodePaths()) + && CollectionUtils.isEmpty(xmindParser.getTestCase()) + && CollectionUtils.isEmpty(xmindParser.getUpdateTestCase())) { if (errList == null) { errList = new ArrayList<>(); } @@ -269,15 +276,18 @@ public class TestCaseService { excelResponse.setErrList(errList); } if (errList.isEmpty()) { - if (!xmindParser.getNodePaths().isEmpty()) { + if (CollectionUtils.isNotEmpty(xmindParser.getNodePaths())) { testCaseNodeService.createNodes(xmindParser.getNodePaths(), projectId); } - if (!xmindParser.getTestCase().isEmpty()) { + if (CollectionUtils.isNotEmpty(xmindParser.getTestCase())) { Collections.reverse(xmindParser.getTestCase()); this.saveImportData(xmindParser.getTestCase(), projectId); - xmindParser.clear(); + } + if (CollectionUtils.isNotEmpty(xmindParser.getUpdateTestCase())) { + this.updateImportData(xmindParser.getUpdateTestCase(), projectId); } } + xmindParser.clear(); } catch (Exception e) { LogUtil.error(e.getMessage(), e); MSException.throwException(e.getMessage()); @@ -313,7 +323,6 @@ public class TestCaseService { } public void saveImportData(List testCases, String projectId) { - Map nodePathMap = testCaseNodeService.createNodeByTestCases(testCases, projectId); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class); @@ -335,6 +344,27 @@ public class TestCaseService { sqlSession.flushStatements(); } + public void updateImportData(List testCases, String projectId) { + Map nodePathMap = testCaseNodeService.createNodeByTestCases(testCases, projectId); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class); + if (!testCases.isEmpty()) { + AtomicInteger sort = new AtomicInteger(); + AtomicInteger num = new AtomicInteger(); + num.set(getNextNum(projectId) + testCases.size()); + testCases.forEach(testcase -> { + testcase.setUpdateTime(System.currentTimeMillis()); + testcase.setNodeId(nodePathMap.get(testcase.getNodePath())); + testcase.setSort(sort.getAndIncrement()); + testcase.setNum(num.decrementAndGet()); + testcase.setReviewStatus(TestCaseReviewStatus.Prepare.name()); + mapper.updateByPrimaryKeySelective(testcase); + }); + } + sqlSession.flushStatements(); + } + + public void testCaseTemplateExport(HttpServletResponse response) { try { EasyExcelExporter easyExcelExporter = new EasyExcelExporter(new TestCaseExcelDataFactory().getExcelDataByLocal()); @@ -572,7 +602,9 @@ public class TestCaseService { public boolean exist(TestCaseWithBLOBs testCaseWithBLOBs) { try { - checkTestCaseExist(testCaseWithBLOBs); + TestCaseWithBLOBs caseWithBLOBs = checkTestCaseExist(testCaseWithBLOBs); + if (caseWithBLOBs != null) + return true; } catch (MSException e) { return true; } diff --git a/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java b/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java index 939b8f3730..1dab92a9f8 100644 --- a/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java +++ b/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java @@ -44,6 +44,11 @@ public class XmindCaseParser { * 转换后的案例信息 */ private List testCases; + /** + * 需要更新的用例 + */ + private List updateTestCases; + /** * 案例详情重写了hashCode方法去重用 */ @@ -59,6 +64,7 @@ public class XmindCaseParser { this.projectId = projectId; this.testCaseNames = testCaseNames; testCases = new LinkedList<>(); + updateTestCases = new LinkedList<>(); compartDatas = new ArrayList<>(); process = new DetailUtil(); nodePaths = new ArrayList<>(); @@ -71,6 +77,7 @@ public class XmindCaseParser { public void clear() { compartDatas.clear(); testCases.clear(); + updateTestCases.clear(); testCaseNames.clear(); nodePaths.clear(); } @@ -79,6 +86,10 @@ public class XmindCaseParser { return this.testCases; } + public List getUpdateTestCase() { + return this.updateTestCases; + } + public List getNodePaths() { return this.nodePaths; } @@ -112,7 +123,7 @@ public class XmindCaseParser { /** * 验证用例的合规性 */ - private void validate(TestCaseWithBLOBs data) { + private boolean validate(TestCaseWithBLOBs data) { String nodePath = data.getNodePath(); if (!nodePath.startsWith("/")) { nodePath = "/" + nodePath; @@ -149,9 +160,13 @@ public class XmindCaseParser { } if (testCaseNames.contains(data.getName())) { - boolean dbExist = testCaseService.exist(data); - if (dbExist) { - process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + data.getName()); + TestCaseWithBLOBs bloBs = testCaseService.checkTestCaseExist(data); + if (bloBs != null) { + // process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + data.getName()); + // 记录需要变更的用例 + BeanUtils.copyBean(bloBs, data, "id"); + updateTestCases.add(bloBs); + return false; } } else { testCaseNames.add(data.getName()); @@ -172,6 +187,7 @@ public class XmindCaseParser { process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + compartData.getName()); } compartDatas.add(compartData); + return true; } /** @@ -299,9 +315,10 @@ public class XmindCaseParser { } testCase.setRemark(rc.toString()); testCase.setSteps(this.getSteps(steps)); - testCases.add(testCase); // 校验合规性 - validate(testCase); + if (validate(testCase)) { + testCases.add(testCase); + } } /**