From 5500254a153b300f5feeea6f53dad76659719169 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Wed, 19 Jun 2024 18:46:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92):=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=A7=84=E5=88=92=E5=85=B3=E8=81=94=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/i18n/plan_en_US.properties | 1 + .../main/resources/i18n/plan_zh_CN.properties | 1 + .../main/resources/i18n/plan_zh_TW.properties | 1 + .../plan/service/TestPlanApiCaseService.java | 1 + .../service/TestPlanApiScenarioService.java | 1 + .../TestPlanFunctionalCaseService.java | 1 + .../plan/service/TestPlanResourceService.java | 36 +++++++++++++++---- .../TestPlanApiCaseControllerTests.java | 8 ++--- .../TestPlanApiScenarioControllerTests.java | 2 +- .../TestPlanCaseControllerTests.java | 8 +++-- .../dml/init_test_plan_case_relate_bug.sql | 4 ++- 11 files changed, 50 insertions(+), 14 deletions(-) diff --git a/backend/framework/sdk/src/main/resources/i18n/plan_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/plan_en_US.properties index 8b37daf1d3..d254aecdb2 100644 --- a/backend/framework/sdk/src/main/resources/i18n/plan_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/plan_en_US.properties @@ -131,3 +131,4 @@ test_plan.mind.case_count=Case count test_plan.mind.environment=Environment test_plan.mind.test_resource_pool=Resource pool test_plan.mind.collection_name_repeat=Duplicate test set name +test_plan.collection_not_exist=Test plan collection does not exist diff --git a/backend/framework/sdk/src/main/resources/i18n/plan_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/plan_zh_CN.properties index b6d3e69425..87e62bb96b 100644 --- a/backend/framework/sdk/src/main/resources/i18n/plan_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/plan_zh_CN.properties @@ -131,3 +131,4 @@ test_plan.mind.case_count=用例数 test_plan.mind.environment=环境 test_plan.mind.test_resource_pool=资源池 test_plan.mind.collection_name_repeat=测试集名称重复 +test_plan.collection_not_exist=测试集不存在 diff --git a/backend/framework/sdk/src/main/resources/i18n/plan_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/plan_zh_TW.properties index c7d836451c..756040b9dc 100644 --- a/backend/framework/sdk/src/main/resources/i18n/plan_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/plan_zh_TW.properties @@ -130,3 +130,4 @@ test_plan.mind.case_count=用例數 test_plan.mind.environment=環境 test_plan.mind.test_resource_pool=資源池 test_plan.mind.collection_name_repeat=測試集名稱重複 +test_plan.collection_not_exist=測試集不存在 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 97da1aea43..9436453a02 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 @@ -602,6 +602,7 @@ public class TestPlanApiCaseService extends TestPlanResourceService { * @param testPlanApiCaseList */ private void buildTestPlanApiCase(TestPlan testPlan, List apiTestCases, String collectionId, SessionUser user, List testPlanApiCaseList, List logDTOS) { + super.checkCollection(testPlan.getId(), collectionId, CaseType.API_CASE.getKey()); AtomicLong nextOrder = new AtomicLong(getNextOrder(collectionId)); apiTestCases.forEach(apiTestCase -> { TestPlanApiCase testPlanApiCase = new TestPlanApiCase(); 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 08fb1f79c9..81bb596f27 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 @@ -213,6 +213,7 @@ public class TestPlanApiScenarioService extends TestPlanResourceService { * @param testPlanApiScenarioList */ private void buildTestPlanApiScenario(TestPlan testPlan, List scenarios, String collectionId, SessionUser user, List testPlanApiScenarioList, List logDTOS) { + super.checkCollection(testPlan.getId(), collectionId, CaseType.SCENARIO_CASE.getKey()); AtomicLong nextOrder = new AtomicLong(getNextOrder(collectionId)); scenarios.forEach(scenario -> { TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); 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 0291a1eeca..45efa86e0f 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 @@ -769,6 +769,7 @@ public class TestPlanFunctionalCaseService extends TestPlanResourceService { * @param testPlanFunctionalCaseList */ private void buildTestPlanFunctionalCase(TestPlan testPlan, BaseCollectionAssociateRequest functional, SessionUser user, List testPlanFunctionalCaseList, List logDTOS) { + super.checkCollection(testPlan.getId(), functional.getCollectionId(), CaseType.FUNCTIONAL_CASE.getKey()); List functionalIds = functional.getIds(); if (CollectionUtils.isNotEmpty(functionalIds)) { FunctionalCaseExample example = new 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 67d5ea7ebe..5deecd40cd 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 @@ -1,13 +1,18 @@ package io.metersphere.plan.service; import io.metersphere.plan.domain.TestPlan; +import io.metersphere.plan.domain.TestPlanCollectionExample; 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; import io.metersphere.plan.dto.response.TestPlanAssociationResponse; +import io.metersphere.plan.mapper.TestPlanCollectionMapper; import io.metersphere.plan.mapper.TestPlanMapper; +import io.metersphere.sdk.constants.ModuleConstants; +import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.util.Translator; import io.metersphere.system.dto.LogInsertModule; import io.metersphere.system.dto.sdk.SessionUser; import jakarta.annotation.Resource; @@ -31,6 +36,8 @@ public abstract class TestPlanResourceService extends TestPlanSortService { private TestPlanMapper testPlanMapper; @Resource private TestPlanResourceLogService testPlanResourceLogService; + @Resource + private TestPlanCollectionMapper testPlanCollectionMapper; /** * 取消关联资源od @@ -65,10 +72,27 @@ public abstract class TestPlanResourceService extends TestPlanSortService { */ public abstract void associateCollection(String planId, Map> collectionAssociates, SessionUser user); - /** - * 初始化旧的关联资源到默认测试集 - * @param planId - * @param defaultCollections 默认的测试集集合 - */ - public abstract void initResourceDefaultCollection(String planId, List defaultCollections); + /** + * 初始化旧的关联资源到默认测试集 + * + * @param planId + * @param defaultCollections 默认的测试集集合 + */ + public abstract void initResourceDefaultCollection(String planId, List defaultCollections); + + + /** + * 校验测试集是否存在 + * + * @param testPlanId + * @param collectionId + * @param type + */ + public void checkCollection(String testPlanId, String collectionId, String type) { + TestPlanCollectionExample collectionExample = new TestPlanCollectionExample(); + collectionExample.createCriteria().andIdEqualTo(collectionId).andParentIdNotEqualTo(ModuleConstants.ROOT_NODE_PARENT_ID).andTestPlanIdEqualTo(testPlanId).andTypeEqualTo(type); + if (testPlanCollectionMapper.countByExample(collectionExample) == 0) { + throw new MSException(Translator.get("test_plan.collection_not_exist")); + } + } } diff --git a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiCaseControllerTests.java b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiCaseControllerTests.java index 6ad4dc12b3..3ef1640f2f 100644 --- a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiCaseControllerTests.java +++ b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiCaseControllerTests.java @@ -253,7 +253,7 @@ public class TestPlanApiCaseControllerTests extends BaseTest { Map> collectionAssociates = new HashMap<>(); List baseCollectionAssociateRequests = new ArrayList<>(); BaseCollectionAssociateRequest baseCollectionAssociateRequest = new BaseCollectionAssociateRequest(); - baseCollectionAssociateRequest.setCollectionId("wxxx_1"); + baseCollectionAssociateRequest.setCollectionId("wxxx_2"); baseCollectionAssociateRequest.setIds(List.of("wxxx_api_1")); baseCollectionAssociateRequests.add(baseCollectionAssociateRequest); collectionAssociates.put(AssociateCaseType.API, baseCollectionAssociateRequests); @@ -263,17 +263,17 @@ public class TestPlanApiCaseControllerTests extends BaseTest { userDTO.setName("admin"); userDTO.setLastOrganizationId("wxx_1234"); SessionUser user = SessionUser.fromUser(userDTO, sessionId); - testPlanApiCaseService.associateCollection("wxxx_2", collectionAssociates, user); + testPlanApiCaseService.associateCollection("wxxx_1", collectionAssociates, user); //api case Map> collectionAssociates1 = new HashMap<>(); List baseCollectionAssociateRequests1 = new ArrayList<>(); BaseCollectionAssociateRequest baseCollectionAssociateRequest1 = new BaseCollectionAssociateRequest(); - baseCollectionAssociateRequest1.setCollectionId("wxxx_1"); + baseCollectionAssociateRequest1.setCollectionId("wxxx_2"); baseCollectionAssociateRequest1.setIds(List.of("wxxx_api_case_1")); baseCollectionAssociateRequests1.add(baseCollectionAssociateRequest1); collectionAssociates1.put(AssociateCaseType.API_CASE, baseCollectionAssociateRequests1); - testPlanApiCaseService.associateCollection("wxxx_2", collectionAssociates1, user); + testPlanApiCaseService.associateCollection("wxxx_1", collectionAssociates1, user); apiTestCase = initApiData(); TestPlanApiCase testPlanApiCase = new TestPlanApiCase(); diff --git a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiScenarioControllerTests.java b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiScenarioControllerTests.java index 038f9c55ad..5c160d45ee 100644 --- a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiScenarioControllerTests.java +++ b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanApiScenarioControllerTests.java @@ -267,7 +267,7 @@ public class TestPlanApiScenarioControllerTests extends BaseTest { Map> collectionAssociates = new HashMap<>(); List baseCollectionAssociateRequests = new ArrayList<>(); BaseCollectionAssociateRequest baseCollectionAssociateRequest = new BaseCollectionAssociateRequest(); - baseCollectionAssociateRequest.setCollectionId("wxxx_collection_1"); + baseCollectionAssociateRequest.setCollectionId("wxxx_collection_3"); baseCollectionAssociateRequest.setIds(List.of("wxxx_api_scenario_1")); baseCollectionAssociateRequests.add(baseCollectionAssociateRequest); collectionAssociates.put(AssociateCaseType.API_SCENARIO, baseCollectionAssociateRequests); diff --git a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanCaseControllerTests.java b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanCaseControllerTests.java index baaea0f502..f19a5ea8ca 100644 --- a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanCaseControllerTests.java +++ b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/TestPlanCaseControllerTests.java @@ -25,7 +25,6 @@ import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.User; import io.metersphere.system.dto.sdk.SessionUser; import io.metersphere.system.dto.user.UserDTO; -import io.metersphere.system.utils.SessionUtils; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.junit.jupiter.api.*; @@ -362,7 +361,7 @@ public class TestPlanCaseControllerTests extends BaseTest { Map> collectionAssociates = new HashMap<>(); List baseCollectionAssociateRequests = new ArrayList<>(); BaseCollectionAssociateRequest baseCollectionAssociateRequest = new BaseCollectionAssociateRequest(); - baseCollectionAssociateRequest.setCollectionId("wxxx_1"); + baseCollectionAssociateRequest.setCollectionId("123"); baseCollectionAssociateRequest.setIds(List.of("fc_1")); baseCollectionAssociateRequests.add(baseCollectionAssociateRequest); collectionAssociates.put(AssociateCaseType.FUNCTIONAL, baseCollectionAssociateRequests); @@ -372,6 +371,11 @@ public class TestPlanCaseControllerTests extends BaseTest { userDTO.setLastOrganizationId("wxx_1234"); SessionUser user = SessionUser.fromUser(userDTO, sessionId); testPlanFunctionalCaseService.associateCollection("plan_1", collectionAssociates, user); + + baseCollectionAssociateRequest.setCollectionId("wxxx1231_1"); + baseCollectionAssociateRequests.add(baseCollectionAssociateRequest); + collectionAssociates.put(AssociateCaseType.FUNCTIONAL, baseCollectionAssociateRequests); + Assertions.assertThrows(Exception.class, () -> testPlanFunctionalCaseService.associateCollection("plan_1", collectionAssociates, user)); } @Test diff --git a/backend/services/test-plan/src/test/resources/dml/init_test_plan_case_relate_bug.sql b/backend/services/test-plan/src/test/resources/dml/init_test_plan_case_relate_bug.sql index 49ba0b85ef..a8ab507463 100644 --- a/backend/services/test-plan/src/test/resources/dml/init_test_plan_case_relate_bug.sql +++ b/backend/services/test-plan/src/test/resources/dml/init_test_plan_case_relate_bug.sql @@ -57,7 +57,9 @@ VALUES ('100001', 'functional_default', '', 1, UNIX_TIMESTAMP() * 1000, UNIX_TIM INSERT INTO template (id,name,remark,internal,update_time,create_time,create_user,scope_type,scope_id,enable_third_part, scene, ref_id) VALUES ('bug-template-id', 'bug_default', '', 1, UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'PROJECT', 'gyq_disassociate', 0, 'BUG', 'bug-template-id'); -- 初始化项目默认模板内置字段, 项目默认模板内置字段 - +INSERT INTO `test_plan_collection`(`id`, `test_plan_id`, `name`, `type`, `environment_id`, `test_resource_pool_id`, `pos`, `create_user`, `create_time`, `parent_id`) +VALUES + ('123', 'plan_1', 'coll_1', 'FUNCTIONAL', 'NONE', 'NONE', 1, 'admin', 1716370415311, '123456'); INSERT INTO `test_plan_case_execute_history`(`id`, `test_plan_case_id`, `test_plan_id`, `case_id`, `status`, `content`, `steps`, `deleted`, `notifier`, `create_user`, `create_time`) VALUES