fix(测试跟踪): 修复测试用例导入重复检验,不同模块下不能创建相同用例

--bug=1027307 --user=陈建星 【测试跟踪】github#25101,测试跟踪,excel用例导入新建,存在同名用例,导如800条 用例提示重复只导入了500条。 https://www.tapd.cn/55049933/s/1385238
This commit is contained in:
jianxing 2023-06-25 14:35:50 +08:00 committed by 刘瑞斌
parent cf885b8e76
commit 89e018d935
4 changed files with 51 additions and 3 deletions

View File

@ -274,7 +274,6 @@ public class NodeTreeService<T extends TreeNodeDTO> {
createNodeByPathIterator(pathIterator, path + "/" + child.getName(), createNodeByPathIterator(pathIterator, path + "/" + child.getName(),
child, pathMap, projectId, level + 1); child, pathMap, projectId, level + 1);
} }
;
} }
//若子节点中不包含该目标节点则在该节点下创建 //若子节点中不包含该目标节点则在该节点下创建

View File

@ -14,6 +14,7 @@ import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.CustomFieldDao; import io.metersphere.dto.CustomFieldDao;
import io.metersphere.dto.CustomFieldResourceDTO; import io.metersphere.dto.CustomFieldResourceDTO;
import io.metersphere.dto.TestCaseNodeDTO;
import io.metersphere.excel.annotation.NotRequired; import io.metersphere.excel.annotation.NotRequired;
import io.metersphere.excel.constants.TestCaseImportFiled; import io.metersphere.excel.constants.TestCaseImportFiled;
import io.metersphere.excel.domain.ExcelErrData; import io.metersphere.excel.domain.ExcelErrData;
@ -25,6 +26,7 @@ import io.metersphere.excel.utils.ExcelValidateHelper;
import io.metersphere.exception.CustomFieldValidateException; import io.metersphere.exception.CustomFieldValidateException;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.request.testcase.TestCaseImportRequest; import io.metersphere.request.testcase.TestCaseImportRequest;
import io.metersphere.service.TestCaseNodeService;
import io.metersphere.service.TestCaseService; import io.metersphere.service.TestCaseService;
import io.metersphere.validate.AbstractCustomFieldValidator; import io.metersphere.validate.AbstractCustomFieldValidator;
import io.metersphere.validate.CustomFieldValidatorFactory; import io.metersphere.validate.CustomFieldValidatorFactory;
@ -62,6 +64,8 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
private TestCaseService testCaseService; private TestCaseService testCaseService;
private TestCaseNodeService testCaseNodeService;
protected List<TestCaseExcelData> updateList = new ArrayList<>(); //存储待更新用例的集合 protected List<TestCaseExcelData> updateList = new ArrayList<>(); //存储待更新用例的集合
protected List<TestCaseExcelData> list = new ArrayList<>(); protected List<TestCaseExcelData> list = new ArrayList<>();
@ -104,6 +108,8 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
private HashMap<String, AbstractCustomFieldValidator> customFieldValidatorMap; private HashMap<String, AbstractCustomFieldValidator> customFieldValidatorMap;
private Map<String, List<CustomFieldResourceDTO>> testCaseCustomFieldMap = new HashMap<>(); private Map<String, List<CustomFieldResourceDTO>> testCaseCustomFieldMap = new HashMap<>();
private Map<String, String> pathMap = new HashMap<>();
private List<TestCaseNodeDTO> nodeTrees;
public boolean isUpdated() { public boolean isUpdated() {
return isUpdated; return isUpdated;
@ -113,6 +119,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
this.mergeInfoSet = mergeInfoSet; this.mergeInfoSet = mergeInfoSet;
this.excelDataClass = c; this.excelDataClass = c;
this.testCaseService = CommonBeanFactory.getBean(TestCaseService.class); this.testCaseService = CommonBeanFactory.getBean(TestCaseService.class);
this.testCaseNodeService = CommonBeanFactory.getBean(TestCaseNodeService.class);
customIds = new HashSet<>(); customIds = new HashSet<>();
this.request = request; this.request = request;
@ -123,6 +130,8 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
if (CollectionUtils.isNotEmpty(customFields)) { if (CollectionUtils.isNotEmpty(customFields)) {
customFieldsMap = customFields.stream().collect(Collectors.toMap(CustomFieldDao::getName, i -> i)); customFieldsMap = customFields.stream().collect(Collectors.toMap(CustomFieldDao::getName, i -> i));
} }
nodeTrees = testCaseNodeService.getNodeTreeByProjectId(request.getProjectId());
} }
@Override @Override
@ -304,6 +313,10 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
String steps = getSteps(data); String steps = getSteps(data);
testCase.setSteps(steps); testCase.setSteps(steps);
// 校验模块是否存在没有存在则新建一个模块
testCaseNodeService.createNodeByNodePath(testCase.getNodePath(), request.getProjectId(), nodeTrees, pathMap);
testCase.setNodeId(pathMap.get(testCase.getNodePath()));
boolean dbExist = testCaseService.exist(testCase); boolean dbExist = testCaseService.exist(testCase);
boolean excelExist = false; boolean excelExist = false;
@ -535,7 +548,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
List<TestCaseWithBLOBs> result = list.stream() List<TestCaseWithBLOBs> result = list.stream()
.map(item -> this.convert2TestCase(item)) .map(item -> this.convert2TestCase(item))
.collect(Collectors.toList()); .collect(Collectors.toList());
testCaseService.saveImportData(result, request, testCaseCustomFieldMap); testCaseService.saveImportData(result, request, testCaseCustomFieldMap, pathMap);
this.names = result.stream().map(TestCase::getName).collect(Collectors.toList()); this.names = result.stream().map(TestCase::getName).collect(Collectors.toList());
this.ids = result.stream().map(TestCase::getId).collect(Collectors.toList()); this.ids = result.stream().map(TestCase::getId).collect(Collectors.toList());
this.isUpdated = true; this.isUpdated = true;

View File

@ -438,6 +438,35 @@ public class TestCaseNodeService extends NodeTreeService<TestCaseNodeDTO> {
} }
public void createNodeByNodePath(String nodePath, String projectId, List<TestCaseNodeDTO> nodeTrees, Map<String, String> pathMap) {
if (nodePath == null) {
throw new ExcelException(Translator.get("test_case_module_not_null"));
}
List<String> nodeNameList = new ArrayList<>(Arrays.asList(nodePath.split("/")));
Iterator<String> itemIterator = nodeNameList.iterator();
Boolean hasNode = false;
String rootNodeName;
if (nodeNameList.size() <= 1) {
throw new ExcelException(Translator.get("test_case_create_module_fail") + ":" + nodePath);
} else {
itemIterator.next();
itemIterator.remove();
rootNodeName = itemIterator.next().trim();
//原来没有新建的树nodeTrees也不包含
for (TestCaseNodeDTO nodeTree : nodeTrees) {
if (StringUtils.equals(rootNodeName, nodeTree.getName())) {
hasNode = true;
createNodeByPathIterator(itemIterator, "/" + rootNodeName, nodeTree,
pathMap, projectId, 2);
}
}
}
if (!hasNode) {
createNodeByPath(itemIterator, rootNodeName, null, projectId, 1, StringUtils.EMPTY, pathMap);
}
}
@Override @Override
public String insertNode(String nodeName, String pId, String projectId, Integer level, String path) { public String insertNode(String nodeName, String pId, String projectId, Integer level, String path) {
TestCaseNode testCaseNode = new TestCaseNode(); TestCaseNode testCaseNode = new TestCaseNode();

View File

@ -1256,8 +1256,15 @@ public class TestCaseService {
public void saveImportData(List<TestCaseWithBLOBs> testCases, TestCaseImportRequest request, public void saveImportData(List<TestCaseWithBLOBs> testCases, TestCaseImportRequest request,
Map<String, List<CustomFieldResourceDTO>> testCaseCustomFieldMap) { Map<String, List<CustomFieldResourceDTO>> testCaseCustomFieldMap) {
saveImportData( testCases, request, testCaseCustomFieldMap, null);
}
public void saveImportData(List<TestCaseWithBLOBs> testCases, TestCaseImportRequest request,
Map<String, List<CustomFieldResourceDTO>> testCaseCustomFieldMap, Map<String, String> nodePathMap) {
String projectId = request.getProjectId(); String projectId = request.getProjectId();
Map<String, String> nodePathMap = testCaseNodeService.createNodeByTestCases(testCases, projectId); if (nodePathMap == null) {
nodePathMap = testCaseNodeService.createNodeByTestCases(testCases, projectId);
}
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
Project project = baseProjectService.getProjectById(projectId); Project project = baseProjectService.getProjectById(projectId);
TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class); TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class);