From f43f23ae7b2f46db789e7258029479e0fb16fdfd Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Thu, 6 Jun 2024 17:35:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92):=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=CE=B2=E7=89=88=E6=9C=AC=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E9=9B=86=E6=95=B0=E6=8D=AE=E5=8F=8A?= =?UTF-8?q?=E5=85=B6=E5=85=B3=E8=81=94=E7=94=A8=E4=BE=8B=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plan/service/TestPlanApiCaseService.java | 14 +++++++++ .../service/TestPlanApiScenarioService.java | 16 ++++++++++ .../plan/service/TestPlanBugService.java | 6 ++++ .../TestPlanFunctionalCaseService.java | 18 +++++++++++ .../plan/service/TestPlanResourceService.java | 8 +++++ .../plan/service/TestPlanService.java | 30 +++++++++++++++++-- 6 files changed, 89 insertions(+), 3 deletions(-) diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java index 65e0f7cc5a..00d7524906 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseService.java @@ -18,6 +18,7 @@ import io.metersphere.plan.domain.TestPlanCollection; import io.metersphere.plan.domain.TestPlanCollectionExample; import io.metersphere.plan.dto.ApiCaseModuleDTO; import io.metersphere.plan.dto.TestPlanCaseRunResultCount; +import io.metersphere.plan.dto.TestPlanCollectionDTO; import io.metersphere.plan.dto.TestPlanResourceAssociationParam; import io.metersphere.plan.dto.request.*; import io.metersphere.plan.dto.response.TestPlanApiCasePageResponse; @@ -516,4 +517,17 @@ public class TestPlanApiCaseService extends TestPlanResourceService { testPlanApiCaseList.add(testPlanApiCase); }); } + + @Override + public void initResourceDefaultCollection(String planId, List defaultCollections) { + TestPlanCollectionDTO defaultCollection = defaultCollections.stream().filter(collection -> StringUtils.equals(collection.getType(), CaseType.API_CASE.getKey()) + && !StringUtils.equals(collection.getParentId(), "NONE")).toList().get(0); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + TestPlanApiCaseMapper apiBatchMapper = sqlSession.getMapper(TestPlanApiCaseMapper.class); + TestPlanApiCase record = new TestPlanApiCase(); + record.setTestPlanCollectionId(defaultCollection.getId()); + TestPlanApiCaseExample apiCaseExample = new TestPlanApiCaseExample(); + apiCaseExample.createCriteria().andTestPlanIdEqualTo(planId); + apiBatchMapper.updateByExampleSelective(record, apiCaseExample); + } } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java index 4305aca3ce..47342a4d48 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioService.java @@ -4,9 +4,11 @@ import io.metersphere.plan.constants.AssociateCaseType; import io.metersphere.plan.domain.TestPlanApiScenario; import io.metersphere.plan.domain.TestPlanApiScenarioExample; import io.metersphere.plan.dto.TestPlanCaseRunResultCount; +import io.metersphere.plan.dto.TestPlanCollectionDTO; import io.metersphere.plan.dto.request.BaseCollectionAssociateRequest; import io.metersphere.plan.mapper.ExtTestPlanApiScenarioMapper; import io.metersphere.plan.mapper.TestPlanApiScenarioMapper; +import io.metersphere.plan.mapper.TestPlanCollectionMapper; import io.metersphere.sdk.constants.CaseType; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.system.uid.IDGenerator; @@ -34,6 +36,8 @@ public class TestPlanApiScenarioService extends TestPlanResourceService { private TestPlanApiScenarioMapper testPlanApiScenarioMapper; @Resource private ExtTestPlanApiScenarioMapper extTestPlanApiScenarioMapper; + @Resource + private TestPlanCollectionMapper testPlanCollectionMapper; @Override public void deleteBatchByTestPlanId(List testPlanIdList) { @@ -102,4 +106,16 @@ public class TestPlanApiScenarioService extends TestPlanResourceService { // TODO: 调用具体的关联场景用例入库方法 入参{计划ID, 测试集ID, 关联的用例ID集合} } + @Override + public void initResourceDefaultCollection(String planId, List defaultCollections) { + TestPlanCollectionDTO defaultCollection = defaultCollections.stream().filter(collection -> StringUtils.equals(collection.getType(), CaseType.SCENARIO_CASE.getKey()) + && !StringUtils.equals(collection.getParentId(), "NONE")).toList().get(0); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + TestPlanApiScenarioMapper scenarioBatchMapper = sqlSession.getMapper(TestPlanApiScenarioMapper.class); + TestPlanApiScenario record = new TestPlanApiScenario(); + record.setTestPlanCollectionId(defaultCollection.getId()); + TestPlanApiScenarioExample scenarioCaseExample = new TestPlanApiScenarioExample(); + scenarioCaseExample.createCriteria().andTestPlanIdEqualTo(planId); + scenarioBatchMapper.updateByExampleSelective(record, scenarioCaseExample); + } } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanBugService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanBugService.java index 4f86c2da9a..1cb58ebf58 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanBugService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanBugService.java @@ -4,6 +4,7 @@ import io.metersphere.bug.domain.BugRelationCaseExample; import io.metersphere.bug.mapper.BugRelationCaseMapper; import io.metersphere.bug.service.BugCommonService; import io.metersphere.plan.dto.TestPlanBugCaseDTO; +import io.metersphere.plan.dto.TestPlanCollectionDTO; import io.metersphere.plan.dto.request.BaseCollectionAssociateRequest; import io.metersphere.plan.dto.request.TestPlanBugPageRequest; import io.metersphere.plan.dto.response.TestPlanBugPageResponse; @@ -121,4 +122,9 @@ public class TestPlanBugService extends TestPlanResourceService { public void associateCollection(String planId, Map> collectionAssociates,String userId) { // TODO: 暂不支持缺陷关联测试集 } + + @Override + public void initResourceDefaultCollection(String planId, List defaultCollections) { + // 暂不支持缺陷关联测试集 + } } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanFunctionalCaseService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanFunctionalCaseService.java index 6d1c41b96d..8132fd9138 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanFunctionalCaseService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanFunctionalCaseService.java @@ -21,6 +21,7 @@ import io.metersphere.plan.constants.AssociateCaseType; import io.metersphere.plan.domain.*; import io.metersphere.plan.dto.ResourceLogInsertModule; import io.metersphere.plan.dto.TestPlanCaseRunResultCount; +import io.metersphere.plan.dto.TestPlanCollectionDTO; import io.metersphere.plan.dto.TestPlanResourceAssociationParam; import io.metersphere.plan.dto.request.*; import io.metersphere.plan.dto.response.*; @@ -108,11 +109,14 @@ public class TestPlanFunctionalCaseService extends TestPlanResourceService { private TestPlanSendNoticeService testPlanSendNoticeService; @Resource private BugStatusService bugStatusService; + @Resource + private TestPlanCollectionMapper testPlanCollectionMapper; @Resource private ExtUserMapper extUserMapper; private static final String CASE_MODULE_COUNT_ALL = "all"; + @Override public long copyResource(String originalTestPlanId, String newTestPlanId, String operator, long operatorTime) { List copyList = new ArrayList<>(); extTestPlanFunctionalCaseMapper.selectByTestPlanIdAndNotDeleted(originalTestPlanId).forEach(originalCase -> { @@ -688,4 +692,18 @@ public class TestPlanFunctionalCaseService extends TestPlanResourceService { }); } } + + @Override + public void initResourceDefaultCollection(String planId, List defaultCollections) { + TestPlanCollectionDTO defaultCollection = defaultCollections.stream().filter(collection -> StringUtils.equals(collection.getType(), CaseType.FUNCTIONAL_CASE.getKey()) + && !StringUtils.equals(collection.getParentId(), "NONE")).toList().get(0); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + TestPlanFunctionalCaseMapper functionalBatchMapper = sqlSession.getMapper(TestPlanFunctionalCaseMapper.class); + TestPlanFunctionalCase record = new TestPlanFunctionalCase(); + record.setTestPlanCollectionId(defaultCollection.getId()); + TestPlanFunctionalCaseExample functionalCaseExample = new TestPlanFunctionalCaseExample(); + functionalCaseExample.createCriteria().andTestPlanIdEqualTo(planId); + functionalBatchMapper.updateByExampleSelective(record, functionalCaseExample); + } + } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanResourceService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanResourceService.java index 834df161e5..4b1f98ff7e 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanResourceService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanResourceService.java @@ -2,6 +2,7 @@ package io.metersphere.plan.service; import io.metersphere.plan.domain.TestPlan; import io.metersphere.plan.dto.ResourceLogInsertModule; +import io.metersphere.plan.dto.TestPlanCollectionDTO; import io.metersphere.plan.dto.TestPlanResourceAssociationParam; import io.metersphere.plan.dto.request.BaseCollectionAssociateRequest; import io.metersphere.plan.dto.request.BasePlanCaseBatchRequest; @@ -62,4 +63,11 @@ public abstract class TestPlanResourceService extends TestPlanSortService { * @param collectionAssociates 测试集关联用例参数 */ public abstract void associateCollection(String planId, Map> collectionAssociates, String userId); + + /** + * 初始化旧的关联资源到默认测试集 + * @param planId + * @param defaultCollections 默认的测试集集合 + */ + public abstract void initResourceDefaultCollection(String planId, List defaultCollections); } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanService.java index 3d1e205b99..f405775aae 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanService.java @@ -42,6 +42,7 @@ import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -488,10 +489,35 @@ public class TestPlanService extends TestPlanBaseUtilsService { Boolean isFollow = checkIsFollowCase(id, userId); response.setFollowFlag(isFollow); + // 是否计划已初始化默认测试集 + TestPlanCollectionExample collectionExample = new TestPlanCollectionExample(); + collectionExample.createCriteria().andTestPlanIdEqualTo(id); + if (testPlanCollectionMapper.countByExample(collectionExample) == 0) { + List collections = initDefaultPlanCollection(id, userId); + TestPlanService testPlanService = CommonBeanFactory.getBean(TestPlanService.class); + testPlanService.initResourceDefaultCollection(id, collections); + } return response; } + /** + * 关联的资源初始化默认测试集 + * @param planId 计划ID + * @param allCollections 测试集 + */ + @Async + public void initResourceDefaultCollection(String planId, List allCollections) { + // 批处理旧数据 + List defaultCollections = new ArrayList<>(); + allCollections.forEach(allCollection -> { + defaultCollections.addAll(allCollection.getChildren()); + }); + Map beansOfType = applicationContext.getBeansOfType(TestPlanResourceService.class); + beansOfType.forEach((k, v) -> v.initResourceDefaultCollection(planId, defaultCollections)); + } + + private Boolean checkIsFollowCase(String testPlanId, String userId) { TestPlanFollowerExample example = new TestPlanFollowerExample(); example.createCriteria().andTestPlanIdEqualTo(testPlanId).andUserIdEqualTo(userId); @@ -853,9 +879,7 @@ public class TestPlanService extends TestPlanBaseUtilsService { collectionDTOS.add(parentCollectionDTO); } - testPlanCollectionMapper.batchInsertSelective(collections, TestPlanCollection.Column.id, TestPlanCollection.Column.testPlanId, - TestPlanCollection.Column.parentId, TestPlanCollection.Column.name, TestPlanCollection.Column.type, TestPlanCollection.Column.testResourcePoolId, - TestPlanCollection.Column.createUser, TestPlanCollection.Column.createTime, TestPlanCollection.Column.pos); + testPlanCollectionMapper.batchInsert(collections); return collectionDTOS; }