refactor(测试计划): 保存测试规划统一类型测试集名称不能同名

This commit is contained in:
guoyuqi 2024-06-17 18:47:03 +08:00 committed by Craftsman
parent c96d6126ca
commit 9dc20b5ca6
6 changed files with 95 additions and 10 deletions

View File

@ -126,4 +126,5 @@ test_plan.mind.strip=条
test_plan.mind.case_count=用例数 test_plan.mind.case_count=用例数
test_plan.mind.environment=环境 test_plan.mind.environment=环境
test_plan.mind.test_resource_pool=资源池 test_plan.mind.test_resource_pool=资源池
test_plan.mind.collection_name_repeat=测试集名称重复

View File

@ -128,4 +128,5 @@ test_plan.mind.parallel=Parallel
test_plan.mind.strip=Strip test_plan.mind.strip=Strip
test_plan.mind.case_count=Case count test_plan.mind.case_count=Case count
test_plan.mind.environment=Environment test_plan.mind.environment=Environment
test_plan.mind.test_resource_pool=Resource pool test_plan.mind.test_resource_pool=Resource pool
test_plan.mind.collection_name_repeat=Duplicate test set name

View File

@ -128,4 +128,5 @@ test_plan.mind.parallel=并
test_plan.mind.strip= test_plan.mind.strip=
test_plan.mind.case_count=用例数 test_plan.mind.case_count=用例数
test_plan.mind.environment=环境 test_plan.mind.environment=环境
test_plan.mind.test_resource_pool=资源池 test_plan.mind.test_resource_pool=资源池
test_plan.mind.collection_name_repeat=测试集名称重复

View File

@ -127,4 +127,5 @@ test_plan.mind.parallel=並
test_plan.mind.strip= test_plan.mind.strip=
test_plan.mind.case_count=用例數 test_plan.mind.case_count=用例數
test_plan.mind.environment=環境 test_plan.mind.environment=環境
test_plan.mind.test_resource_pool=資源池 test_plan.mind.test_resource_pool=資源池
test_plan.mind.collection_name_repeat=測試集名稱重複

View File

@ -9,6 +9,7 @@ import io.metersphere.sdk.constants.ApiBatchRunMode;
import io.metersphere.sdk.constants.CaseType; import io.metersphere.sdk.constants.CaseType;
import io.metersphere.sdk.constants.CommonConstants; import io.metersphere.sdk.constants.CommonConstants;
import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.ModuleConstants;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.sdk.SessionUser; import io.metersphere.system.dto.sdk.SessionUser;
@ -257,30 +258,60 @@ public class TestPlanCollectionMinderService {
private void dealEditList(TestPlanCollectionMinderEditRequest request, String userId, Map<String, List<BaseCollectionAssociateRequest>> associateMap) { private void dealEditList(TestPlanCollectionMinderEditRequest request, String userId, Map<String, List<BaseCollectionAssociateRequest>> associateMap) {
if (CollectionUtils.isNotEmpty(request.getEditList())) { if (CollectionUtils.isNotEmpty(request.getEditList())) {
Map<String, List<TestPlanCollection>> parentMap = getParentMap(request); Map<String, List<TestPlanCollection>> parentMap = getParentMap(request);
Map<String, List<TestPlanCollection>> childrenMap = getChildrenMap(request);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
TestPlanCollectionMapper collectionMapper = sqlSession.getMapper(TestPlanCollectionMapper.class); TestPlanCollectionMapper collectionMapper = sqlSession.getMapper(TestPlanCollectionMapper.class);
//新增 //新增
dealAddList(request, userId, associateMap, parentMap, collectionMapper); Map<String, List<String>> addTypeNameMap = dealAddList(request, userId, associateMap, parentMap, collectionMapper);
//更新 //更新
dealUpdateList(request, userId, associateMap, parentMap, collectionMapper); Map<String, List<String>> updateTypeNameMap = dealUpdateList(request, userId, associateMap, parentMap, collectionMapper);
//检查同一类型名称重复
checkNameRepeat(updateTypeNameMap, addTypeNameMap, childrenMap);
sqlSession.flushStatements(); sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
} }
} }
private void dealUpdateList(TestPlanCollectionMinderEditRequest request, String userId, Map<String, List<BaseCollectionAssociateRequest>> associateMap, Map<String, List<TestPlanCollection>> parentMap, TestPlanCollectionMapper collectionMapper) { private static void checkNameRepeat(Map<String, List<String>> updateTypeNameMap, Map<String, List<String>> addTypeNameMap, Map<String, List<TestPlanCollection>> childrenMap) {
updateTypeNameMap.forEach((k, v)->{
List<String> nameList = addTypeNameMap.get(k);
if (CollectionUtils.isNotEmpty(nameList)) {
List<String> repeatList = v.stream().filter(nameList::contains).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(repeatList)) {
throw new MSException(Translator.get("test_plan.mind.collection_name_repeat"));
} else {
nameList.addAll(v);
addTypeNameMap.put(k, nameList);
}
} else {
addTypeNameMap.put(k,v);
}
});
childrenMap.forEach((k, v)->{
List<String> nameList = addTypeNameMap.get(k);
if (CollectionUtils.isNotEmpty(nameList)) {
List<TestPlanCollection> list = v.stream().filter(t -> nameList.contains(t.getName())).toList();
if (CollectionUtils.isNotEmpty(list)) {
throw new MSException(Translator.get("test_plan.mind.collection_name_repeat"));
}
}
});
}
private Map<String, List<String>> dealUpdateList(TestPlanCollectionMinderEditRequest request, String userId, Map<String, List<BaseCollectionAssociateRequest>> associateMap, Map<String, List<TestPlanCollection>> parentMap, TestPlanCollectionMapper collectionMapper) {
List<TestPlanCollectionMinderEditDTO> updateList = request.getEditList().stream().filter(t -> StringUtils.isNotBlank(t.getId())).toList(); List<TestPlanCollectionMinderEditDTO> updateList = request.getEditList().stream().filter(t -> StringUtils.isNotBlank(t.getId())).toList();
Map<String, List<String>>typeNamesMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(updateList)) { if (CollectionUtils.isNotEmpty(updateList)) {
//处理删除 //处理删除
deleteCollection(updateList, request.getPlanId()); deleteCollection(updateList, request.getPlanId());
//处理更新 //处理更新
for (TestPlanCollectionMinderEditDTO testPlanCollectionMinderEditDTO : updateList) { for (TestPlanCollectionMinderEditDTO testPlanCollectionMinderEditDTO : updateList) {
TestPlanCollection testPlanCollection = updateCollection(request, userId, testPlanCollectionMinderEditDTO, parentMap, collectionMapper); TestPlanCollection testPlanCollection = updateCollection(request, userId, testPlanCollectionMinderEditDTO, parentMap, collectionMapper);
checkChangeDataReapet(typeNamesMap, testPlanCollection);
setAssociateMap(testPlanCollectionMinderEditDTO, associateMap, testPlanCollection); setAssociateMap(testPlanCollectionMinderEditDTO, associateMap, testPlanCollection);
} }
} }
return typeNamesMap;
} }
private void deleteCollection(List<TestPlanCollectionMinderEditDTO> updateList, String planId) { private void deleteCollection(List<TestPlanCollectionMinderEditDTO> updateList, String planId) {
@ -294,14 +325,32 @@ public class TestPlanCollectionMinderService {
} }
} }
private static void dealAddList(TestPlanCollectionMinderEditRequest request, String userId, Map<String, List<BaseCollectionAssociateRequest>> associateMap, Map<String, List<TestPlanCollection>> parentMap, TestPlanCollectionMapper collectionMapper) { private Map<String, List<String>> dealAddList(TestPlanCollectionMinderEditRequest request, String userId, Map<String, List<BaseCollectionAssociateRequest>> associateMap, Map<String, List<TestPlanCollection>> parentMap, TestPlanCollectionMapper collectionMapper) {
Map<String, List<String>>typeNamesMap = new HashMap<>();
List<TestPlanCollectionMinderEditDTO> addList = request.getEditList().stream().filter(t -> StringUtils.isBlank(t.getId())).toList(); List<TestPlanCollectionMinderEditDTO> addList = request.getEditList().stream().filter(t -> StringUtils.isBlank(t.getId())).toList();
if (CollectionUtils.isNotEmpty(addList)) { if (CollectionUtils.isNotEmpty(addList)) {
for (TestPlanCollectionMinderEditDTO testPlanCollectionMinderEditDTO : addList) { for (TestPlanCollectionMinderEditDTO testPlanCollectionMinderEditDTO : addList) {
TestPlanCollection testPlanCollection = addCollection(request, userId, testPlanCollectionMinderEditDTO, parentMap, collectionMapper); TestPlanCollection testPlanCollection = addCollection(request, userId, testPlanCollectionMinderEditDTO, parentMap, collectionMapper);
checkChangeDataReapet(typeNamesMap, testPlanCollection);
setAssociateMap(testPlanCollectionMinderEditDTO, associateMap, testPlanCollection); setAssociateMap(testPlanCollectionMinderEditDTO, associateMap, testPlanCollection);
} }
} }
return typeNamesMap;
}
private static void checkChangeDataReapet(Map<String, List<String>> typeNamesMap, TestPlanCollection testPlanCollection) {
List<String> nameList = typeNamesMap.get(testPlanCollection.getType());
if (CollectionUtils.isNotEmpty(nameList)) {
if (nameList.contains(testPlanCollection.getName())) {
throw new MSException(Translator.get("test_plan.mind.collection_name_repeat"));
} else {
nameList.add(testPlanCollection.getName());
typeNamesMap.put(testPlanCollection.getType(), nameList);
}
} else {
nameList = new ArrayList<>(List.of(testPlanCollection.getName()));
typeNamesMap.put(testPlanCollection.getType(), nameList);
}
} }
@NotNull @NotNull
@ -312,6 +361,14 @@ public class TestPlanCollectionMinderService {
return testPlanCollections.stream().collect(Collectors.groupingBy(TestPlanCollection::getType)); return testPlanCollections.stream().collect(Collectors.groupingBy(TestPlanCollection::getType));
} }
@NotNull
private Map<String, List<TestPlanCollection>> getChildrenMap(TestPlanCollectionMinderEditRequest request) {
TestPlanCollectionExample testPlanCollectionExample = new TestPlanCollectionExample();
testPlanCollectionExample.createCriteria().andTestPlanIdEqualTo(request.getPlanId()).andParentIdNotEqualTo(ModuleConstants.ROOT_NODE_PARENT_ID);
List<TestPlanCollection> testPlanCollections = testPlanCollectionMapper.selectByExample(testPlanCollectionExample);
return testPlanCollections.stream().collect(Collectors.groupingBy(TestPlanCollection::getType));
}
@NotNull @NotNull
private static TestPlanCollection updateCollection(TestPlanCollectionMinderEditRequest request, String userId, TestPlanCollectionMinderEditDTO testPlanCollectionMinderEditDTO, Map<String, List<TestPlanCollection>> parentMap, TestPlanCollectionMapper collectionMapper) { private static TestPlanCollection updateCollection(TestPlanCollectionMinderEditRequest request, String userId, TestPlanCollectionMinderEditDTO testPlanCollectionMinderEditDTO, Map<String, List<TestPlanCollection>> parentMap, TestPlanCollectionMapper collectionMapper) {
TestPlanCollection testPlanCollection = new TestPlanCollection(); TestPlanCollection testPlanCollection = new TestPlanCollection();

View File

@ -24,6 +24,8 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc @AutoConfigureMockMvc
@ -119,10 +121,32 @@ public class TestPlanCollectionMinderControllerTests extends BaseTest {
editList = new ArrayList<>(); editList = new ArrayList<>();
editList.add(deleteDTO); editList.add(deleteDTO);
request.setEditList(editList); request.setEditList(editList);
this.requestPostWithOkAndReturn(EDIT_MIND, request); this.requestPost(EDIT_MIND, request).andExpect(status().is5xxServerError());;
testPlanCollectionExample = new TestPlanCollectionExample(); testPlanCollectionExample = new TestPlanCollectionExample();
testPlanCollectionExample.createCriteria().andNameEqualTo("新建名称"); testPlanCollectionExample.createCriteria().andNameEqualTo("新建名称");
testPlanCollections = testPlanCollectionMapper.selectByExample(testPlanCollectionExample); testPlanCollections = testPlanCollectionMapper.selectByExample(testPlanCollectionExample);
Assertions.assertFalse(CollectionUtils.isEmpty(testPlanCollections));
testPlanCollectionMinderEditDTO = new TestPlanCollectionMinderEditDTO();
testPlanCollectionMinderEditDTO.setId(testPlanCollections.get(0).getId());
testPlanCollectionMinderEditDTO.setText("hahaha");
testPlanCollectionMinderEditDTO.setNum(500L);
testPlanCollectionMinderEditDTO.setExecuteMethod("PARALLEL");
testPlanCollectionMinderEditDTO.setType("API");
testPlanCollectionMinderEditDTO.setExtended(false);
testPlanCollectionMinderEditDTO.setGrouped(false);
testPlanCollectionMinderEditDTO.setEnvironmentId("gyq_123");
testPlanCollectionMinderEditDTO.setTestResourcePoolId("gyq_123_pool");
testPlanCollectionMinderEditDTO.setRetryOnFail(true);
testPlanCollectionMinderEditDTO.setRetryType("SCENARIO");
testPlanCollectionMinderEditDTO.setRetryTimes(5);
testPlanCollectionMinderEditDTO.setRetryInterval(1000);
testPlanCollectionMinderEditDTO.setStopOnFail(true);
editList = new ArrayList<>();
editList.add(testPlanCollectionMinderEditDTO);
request.setEditList(editList);
this.requestPostWithOkAndReturn(EDIT_MIND, request);
testPlanCollectionExample.createCriteria().andNameEqualTo("新建名称");
testPlanCollections = testPlanCollectionMapper.selectByExample(testPlanCollectionExample);
Assertions.assertTrue(CollectionUtils.isEmpty(testPlanCollections)); Assertions.assertTrue(CollectionUtils.isEmpty(testPlanCollections));
} }