From ba26a0c878a9b4794837497124d7868e9514f71d Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Thu, 13 Jun 2024 10:38: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=85=B3=E8=81=94=E5=9C=BA=E6=99=AF=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/TestPlanApiScenarioService.java | 52 ++++++++++++++++++- .../TestPlanApiScenarioControllerTests.java | 15 ++++++ 2 files changed, 65 insertions(+), 2 deletions(-) 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 247295a83f..e3f37965e4 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 @@ -1,10 +1,12 @@ package io.metersphere.plan.service; import io.metersphere.api.domain.ApiScenario; +import io.metersphere.api.domain.ApiScenarioExample; import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiScenarioReportExample; import io.metersphere.api.dto.scenario.ApiScenarioDTO; import io.metersphere.api.invoker.GetRunScriptServiceRegister; +import io.metersphere.api.mapper.ApiScenarioMapper; import io.metersphere.api.mapper.ApiScenarioReportMapper; import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.GetRunScriptService; @@ -92,6 +94,8 @@ public class TestPlanApiScenarioService extends TestPlanResourceService implemen private TestPlanCollectionMapper testPlanCollectionMapper; @Resource private ApiScenarioReportMapper apiScenarioReportMapper; + @Resource + private ApiScenarioMapper apiScenarioMapper; public TestPlanApiScenarioService() { GetRunScriptServiceRegister.register(ApiExecuteResourceType.TEST_PLAN_API_SCENARIO, this); @@ -163,8 +167,52 @@ public class TestPlanApiScenarioService extends TestPlanResourceService implemen @Override public void associateCollection(String planId, Map> collectionAssociates, String userId) { - List apiScenarios = collectionAssociates.get(AssociateCaseType.API_SCENARIO); - // TODO: 调用具体的关联场景用例入库方法 入参{计划ID, 测试集ID, 关联的用例ID集合} + List testPlanApiScenarioList = new ArrayList<>(); + handleApiScenarioData(collectionAssociates.get(AssociateCaseType.API_SCENARIO), userId, testPlanApiScenarioList, planId); + if (CollectionUtils.isNotEmpty(testPlanApiScenarioList)) { + testPlanApiScenarioMapper.batchInsert(testPlanApiScenarioList); + } + } + + private void handleApiScenarioData(List apiScenarioList, String userId, List testPlanApiScenarioList, String planId) { + if (CollectionUtils.isNotEmpty(apiScenarioList)) { + List ids = apiScenarioList.stream().flatMap(item -> item.getIds().stream()).toList(); + ApiScenarioExample scenarioExample = new ApiScenarioExample(); + scenarioExample.createCriteria().andIdIn(ids); + List apiScenarios = apiScenarioMapper.selectByExample(scenarioExample); + apiScenarioList.forEach(apiScenario -> { + List apiScenarioIds = apiScenario.getIds(); + if (CollectionUtils.isNotEmpty(apiScenarioIds)) { + List scenarios = apiScenarios.stream().filter(item -> apiScenarioIds.contains(item.getId())).collect(Collectors.toList()); + buildTestPlanApiScenario(planId, scenarios, apiScenario.getCollectionId(), userId, testPlanApiScenarioList); + } + }); + } + } + + /** + * 构建测试计划场景用例对象 + * + * @param planId + * @param scenarios + * @param collectionId + * @param userId + * @param testPlanApiScenarioList + */ + private void buildTestPlanApiScenario(String planId, List scenarios, String collectionId, String userId, List testPlanApiScenarioList) { + scenarios.forEach(scenario -> { + TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); + testPlanApiScenario.setId(IDGenerator.nextStr()); + testPlanApiScenario.setTestPlanId(planId); + testPlanApiScenario.setApiScenarioId(scenario.getId()); + testPlanApiScenario.setTestPlanCollectionId(collectionId); + testPlanApiScenario.setGrouped(scenario.getGrouped()); + testPlanApiScenario.setEnvironmentId(scenario.getEnvironmentId()); + testPlanApiScenario.setCreateTime(System.currentTimeMillis()); + testPlanApiScenario.setCreateUser(userId); + testPlanApiScenario.setPos(getNextOrder(collectionId)); + testPlanApiScenarioList.add(testPlanApiScenario); + }); } @Override 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 a5d79943ab..d5596f16a5 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 @@ -18,6 +18,7 @@ import io.metersphere.api.mapper.ApiScenarioReportMapper; import io.metersphere.api.service.scenario.ApiScenarioReportService; import io.metersphere.api.service.scenario.ApiScenarioService; import io.metersphere.api.utils.ApiDataUtils; +import io.metersphere.plan.constants.AssociateCaseType; import io.metersphere.plan.domain.TestPlanApiScenario; import io.metersphere.plan.domain.TestPlanApiScenarioExample; import io.metersphere.plan.dto.request.*; @@ -257,6 +258,20 @@ public class TestPlanApiScenarioControllerTests extends BaseTest { Assertions.assertNotNull(resultHolder); } + @Test + @Order(4) + public void testApiCaseAssociate() { + // api_scenario + Map> collectionAssociates = new HashMap<>(); + List baseCollectionAssociateRequests = new ArrayList<>(); + BaseCollectionAssociateRequest baseCollectionAssociateRequest = new BaseCollectionAssociateRequest(); + baseCollectionAssociateRequest.setCollectionId("wxxx_collection_1"); + baseCollectionAssociateRequest.setIds(List.of("wxxx_api_scenario_1")); + baseCollectionAssociateRequests.add(baseCollectionAssociateRequest); + collectionAssociates.put(AssociateCaseType.API_SCENARIO, baseCollectionAssociateRequests); + testPlanApiScenarioService.associateCollection("wxxx_plan_2", collectionAssociates, "wx"); + } + @Test @Order(4)