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.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<TestCaseWithBLOBs> 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);
}
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<TestCaseWithBLOBs> testCases, String projectId) {
Map<String, String> 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<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) {
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;
}

View File

@ -44,6 +44,11 @@ public class XmindCaseParser {
* 转换后的案例信息
*/
private List<TestCaseWithBLOBs> testCases;
/**
* 需要更新的用例
*/
private List<TestCaseWithBLOBs> 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<TestCaseWithBLOBs> getUpdateTestCase() {
return this.updateTestCases;
}
public List<String> 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);
}
}
/**