fix(测试跟踪): 测试用例XMIND导入,重复内容更新

This commit is contained in:
fit2-zhao 2021-01-12 15:53:12 +08:00
parent 5b60ac942c
commit a46f3cfbba
2 changed files with 67 additions and 18 deletions

View File

@ -39,7 +39,7 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -119,7 +119,7 @@ public class TestCaseService {
return testCaseMapper.updateByPrimaryKeySelective(testCase); return testCaseMapper.updateByPrimaryKeySelective(testCase);
} }
private void checkTestCaseExist(TestCaseWithBLOBs testCase) { public TestCaseWithBLOBs checkTestCaseExist(TestCaseWithBLOBs testCase) {
// 全部字段值相同才判断为用例存在 // 全部字段值相同才判断为用例存在
if (testCase != null) { if (testCase != null) {
@ -152,17 +152,22 @@ public class TestCaseService {
List<TestCaseWithBLOBs> caseList = testCaseMapper.selectByExampleWithBLOBs(example); List<TestCaseWithBLOBs> caseList = testCaseMapper.selectByExampleWithBLOBs(example);
// 如果上边字段全部相同去检查 steps remark // 如果上边字段全部相同去检查 steps remark
boolean isExt = false;
if (!CollectionUtils.isEmpty(caseList)) { if (!CollectionUtils.isEmpty(caseList)) {
caseList.forEach(tc -> { for (TestCaseWithBLOBs tc : caseList) {
String steps = tc.getSteps(); String steps = tc.getSteps();
String remark = tc.getRemark(); String remark = tc.getRemark();
if (StringUtils.equals(steps, testCase.getSteps()) && StringUtils.equals(remark, testCase.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) { public int deleteTestCase(String testCaseId) {
@ -260,7 +265,9 @@ public class TestCaseService {
try { try {
XmindCaseParser xmindParser = new XmindCaseParser(this, userId, projectId, testCaseNames); XmindCaseParser xmindParser = new XmindCaseParser(this, userId, projectId, testCaseNames);
errList = xmindParser.parse(multipartFile); 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) { if (errList == null) {
errList = new ArrayList<>(); errList = new ArrayList<>();
} }
@ -269,15 +276,18 @@ public class TestCaseService {
excelResponse.setErrList(errList); excelResponse.setErrList(errList);
} }
if (errList.isEmpty()) { if (errList.isEmpty()) {
if (!xmindParser.getNodePaths().isEmpty()) { if (CollectionUtils.isNotEmpty(xmindParser.getNodePaths())) {
testCaseNodeService.createNodes(xmindParser.getNodePaths(), projectId); testCaseNodeService.createNodes(xmindParser.getNodePaths(), projectId);
} }
if (!xmindParser.getTestCase().isEmpty()) { if (CollectionUtils.isNotEmpty(xmindParser.getTestCase())) {
Collections.reverse(xmindParser.getTestCase()); Collections.reverse(xmindParser.getTestCase());
this.saveImportData(xmindParser.getTestCase(), projectId); this.saveImportData(xmindParser.getTestCase(), projectId);
xmindParser.clear(); }
if (CollectionUtils.isNotEmpty(xmindParser.getUpdateTestCase())) {
this.updateImportData(xmindParser.getUpdateTestCase(), projectId);
} }
} }
xmindParser.clear();
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
MSException.throwException(e.getMessage()); MSException.throwException(e.getMessage());
@ -313,7 +323,6 @@ public class TestCaseService {
} }
public void saveImportData(List<TestCaseWithBLOBs> testCases, String projectId) { public void saveImportData(List<TestCaseWithBLOBs> testCases, String projectId) {
Map<String, String> nodePathMap = testCaseNodeService.createNodeByTestCases(testCases, projectId); Map<String, String> nodePathMap = testCaseNodeService.createNodeByTestCases(testCases, projectId);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class); TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class);
@ -335,6 +344,27 @@ public class TestCaseService {
sqlSession.flushStatements(); sqlSession.flushStatements();
} }
public void updateImportData(List<TestCaseWithBLOBs> testCases, String projectId) {
Map<String, String> 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) { public void testCaseTemplateExport(HttpServletResponse response) {
try { try {
EasyExcelExporter easyExcelExporter = new EasyExcelExporter(new TestCaseExcelDataFactory().getExcelDataByLocal()); EasyExcelExporter easyExcelExporter = new EasyExcelExporter(new TestCaseExcelDataFactory().getExcelDataByLocal());
@ -572,7 +602,9 @@ public class TestCaseService {
public boolean exist(TestCaseWithBLOBs testCaseWithBLOBs) { public boolean exist(TestCaseWithBLOBs testCaseWithBLOBs) {
try { try {
checkTestCaseExist(testCaseWithBLOBs); TestCaseWithBLOBs caseWithBLOBs = checkTestCaseExist(testCaseWithBLOBs);
if (caseWithBLOBs != null)
return true;
} catch (MSException e) { } catch (MSException e) {
return true; return true;
} }

View File

@ -44,6 +44,11 @@ public class XmindCaseParser {
* 转换后的案例信息 * 转换后的案例信息
*/ */
private List<TestCaseWithBLOBs> testCases; private List<TestCaseWithBLOBs> testCases;
/**
* 需要更新的用例
*/
private List<TestCaseWithBLOBs> updateTestCases;
/** /**
* 案例详情重写了hashCode方法去重用 * 案例详情重写了hashCode方法去重用
*/ */
@ -59,6 +64,7 @@ public class XmindCaseParser {
this.projectId = projectId; this.projectId = projectId;
this.testCaseNames = testCaseNames; this.testCaseNames = testCaseNames;
testCases = new LinkedList<>(); testCases = new LinkedList<>();
updateTestCases = new LinkedList<>();
compartDatas = new ArrayList<>(); compartDatas = new ArrayList<>();
process = new DetailUtil(); process = new DetailUtil();
nodePaths = new ArrayList<>(); nodePaths = new ArrayList<>();
@ -71,6 +77,7 @@ public class XmindCaseParser {
public void clear() { public void clear() {
compartDatas.clear(); compartDatas.clear();
testCases.clear(); testCases.clear();
updateTestCases.clear();
testCaseNames.clear(); testCaseNames.clear();
nodePaths.clear(); nodePaths.clear();
} }
@ -79,6 +86,10 @@ public class XmindCaseParser {
return this.testCases; return this.testCases;
} }
public List<TestCaseWithBLOBs> getUpdateTestCase() {
return this.updateTestCases;
}
public List<String> getNodePaths() { public List<String> getNodePaths() {
return this.nodePaths; return this.nodePaths;
} }
@ -112,7 +123,7 @@ public class XmindCaseParser {
/** /**
* 验证用例的合规性 * 验证用例的合规性
*/ */
private void validate(TestCaseWithBLOBs data) { private boolean validate(TestCaseWithBLOBs data) {
String nodePath = data.getNodePath(); String nodePath = data.getNodePath();
if (!nodePath.startsWith("/")) { if (!nodePath.startsWith("/")) {
nodePath = "/" + nodePath; nodePath = "/" + nodePath;
@ -149,9 +160,13 @@ public class XmindCaseParser {
} }
if (testCaseNames.contains(data.getName())) { if (testCaseNames.contains(data.getName())) {
boolean dbExist = testCaseService.exist(data); TestCaseWithBLOBs bloBs = testCaseService.checkTestCaseExist(data);
if (dbExist) { if (bloBs != null) {
process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + data.getName()); // process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + data.getName());
// 记录需要变更的用例
BeanUtils.copyBean(bloBs, data, "id");
updateTestCases.add(bloBs);
return false;
} }
} else { } else {
testCaseNames.add(data.getName()); testCaseNames.add(data.getName());
@ -172,6 +187,7 @@ public class XmindCaseParser {
process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + compartData.getName()); process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + compartData.getName());
} }
compartDatas.add(compartData); compartDatas.add(compartData);
return true;
} }
/** /**
@ -299,9 +315,10 @@ public class XmindCaseParser {
} }
testCase.setRemark(rc.toString()); testCase.setRemark(rc.toString());
testCase.setSteps(this.getSteps(steps)); testCase.setSteps(this.getSteps(steps));
testCases.add(testCase);
// 校验合规性 // 校验合规性
validate(testCase); if (validate(testCase)) {
testCases.add(testCase);
}
} }
/** /**