From c002a6b4310d37f6dcf38d3638cdd780c41d3352 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 28 Nov 2023 19:10:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=8A=9F=E8=83=BD=E7=94=A8=E4=BE=8B):?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E5=8A=9F=E8=83=BD=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E9=9C=80=E6=B1=82=E5=88=97=E8=A1=A8=E8=BF=94=E5=9B=9E=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FunctionalCaseDemandController.java | 4 +- .../functional/dto/FunctionalDemandDTO.java | 13 ++++ .../mapper/ExtFunctionalCaseDemandMapper.java | 3 +- .../mapper/ExtFunctionalCaseDemandMapper.xml | 28 ++++++++- .../service/FunctionalCaseDemandService.java | 26 +++++++- .../FunctionalCaseDemandControllerTests.java | 62 +++++++++++++++++-- .../test/resources/dml/init_case_demand.sql | 3 + 7 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalDemandDTO.java diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseDemandController.java b/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseDemandController.java index 67a9a2880d..0fda258ad3 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseDemandController.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseDemandController.java @@ -2,7 +2,7 @@ package io.metersphere.functional.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; -import io.metersphere.functional.domain.FunctionalCaseDemand; +import io.metersphere.functional.dto.FunctionalDemandDTO; import io.metersphere.functional.request.FunctionalCaseDemandRequest; import io.metersphere.functional.request.QueryDemandListRequest; import io.metersphere.functional.service.FunctionalCaseDemandService; @@ -36,7 +36,7 @@ public class FunctionalCaseDemandController { @PostMapping("/page") @Operation(summary = "用例管理-功能用例-关联需求-获取已关联的需求列表") @RequiresPermissions(value = {PermissionConstants.FUNCTIONAL_CASE_READ,PermissionConstants.FUNCTIONAL_CASE_READ_ADD, PermissionConstants.FUNCTIONAL_CASE_READ_UPDATE, PermissionConstants.FUNCTIONAL_CASE_READ_DELETE}, logical = Logical.OR) - public Pager> listFunctionalCaseDemands(@Validated @RequestBody QueryDemandListRequest request) { + public Pager> listFunctionalCaseDemands(@Validated @RequestBody QueryDemandListRequest request) { Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), true); return PageUtils.setPageInfo(page, functionalCaseDemandService.listFunctionalCaseDemands(request)); } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalDemandDTO.java b/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalDemandDTO.java new file mode 100644 index 0000000000..095f978974 --- /dev/null +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalDemandDTO.java @@ -0,0 +1,13 @@ +package io.metersphere.functional.dto; + +import io.metersphere.functional.domain.FunctionalCaseDemand; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +@Data +public class FunctionalDemandDTO extends FunctionalCaseDemand { + @Schema(description = "同平台需求展开项") + private List children; +} diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.java b/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.java index 12b6ca5f91..cad1df56cd 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.java @@ -10,6 +10,7 @@ import java.util.List; */ public interface ExtFunctionalCaseDemandMapper { - List selectByKeyword(@Param("keyword") String keyword, @Param("caseId") String caseId); + List selectGroupByKeyword(@Param("keyword") String keyword, @Param("caseId") String caseId); + List selectByKeyword(@Param("keyword") String keyword, @Param("caseId") String caseId, @Param("platforms") List platforms, @Param("ids") List ids); } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.xml b/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.xml index 7ec9753234..ec599e6d32 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.xml +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/mapper/ExtFunctionalCaseDemandMapper.xml @@ -2,7 +2,7 @@ - SELECT * FROM functional_case_demand @@ -14,6 +14,32 @@ ) + GROUP BY functional_case_demand.demand_platform + + + diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseDemandService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseDemandService.java index 9afb55e78c..d0350540f6 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseDemandService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseDemandService.java @@ -2,11 +2,13 @@ package io.metersphere.functional.service; import io.metersphere.functional.domain.FunctionalCaseDemand; import io.metersphere.functional.dto.DemandDTO; +import io.metersphere.functional.dto.FunctionalDemandDTO; import io.metersphere.functional.mapper.ExtFunctionalCaseDemandMapper; import io.metersphere.functional.mapper.FunctionalCaseDemandMapper; import io.metersphere.functional.request.FunctionalCaseDemandRequest; import io.metersphere.functional.request.QueryDemandListRequest; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.Translator; import io.metersphere.system.uid.IDGenerator; import jakarta.annotation.Resource; @@ -19,7 +21,10 @@ import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * @author guoyuqi @@ -40,8 +45,25 @@ public class FunctionalCaseDemandService { * @param request QueryDemandListRequest * @return List */ - public List listFunctionalCaseDemands(QueryDemandListRequest request) { - return extFunctionalCaseDemandMapper.selectByKeyword(request.getKeyword(), request.getCaseId()); + public List listFunctionalCaseDemands(QueryDemandListRequest request) { + List functionalCaseDemands = extFunctionalCaseDemandMapper.selectGroupByKeyword(request.getKeyword(), request.getCaseId()); + List platforms = functionalCaseDemands.stream().map(FunctionalCaseDemand::getDemandPlatform).distinct().toList(); + List ids = functionalCaseDemands.stream().map(FunctionalCaseDemand::getId).distinct().toList(); + List functionalCaseDemandChildList = extFunctionalCaseDemandMapper.selectByKeyword(request.getKeyword(), request.getCaseId(), platforms, ids); + Map> platformDemandMap = functionalCaseDemandChildList.stream().collect(Collectors.groupingBy(FunctionalCaseDemand::getDemandPlatform)); + List list = new ArrayList<>(); + for (FunctionalCaseDemand functionalCaseDemand : functionalCaseDemands) { + FunctionalDemandDTO functionalDemandDTO = new FunctionalDemandDTO(); + BeanUtils.copyBean(functionalDemandDTO,functionalCaseDemand); + List childrenDemands= platformDemandMap.get(functionalCaseDemand.getDemandPlatform()); + if (CollectionUtils.isNotEmpty(childrenDemands)) { + functionalDemandDTO.setChildren(childrenDemands); + } else { + functionalDemandDTO.setChildren(new ArrayList<>()); + } + list.add(functionalDemandDTO); + } + return list; } /** diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseDemandControllerTests.java b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseDemandControllerTests.java index b98f8c6116..54d0415f3a 100644 --- a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseDemandControllerTests.java +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseDemandControllerTests.java @@ -3,6 +3,7 @@ package io.metersphere.functional.controller; import io.metersphere.functional.domain.FunctionalCaseDemand; import io.metersphere.functional.domain.FunctionalCaseDemandExample; import io.metersphere.functional.dto.DemandDTO; +import io.metersphere.functional.dto.FunctionalDemandDTO; import io.metersphere.functional.mapper.FunctionalCaseDemandMapper; import io.metersphere.functional.request.FunctionalCaseDemandRequest; import io.metersphere.functional.request.QueryDemandListRequest; @@ -67,6 +68,20 @@ public class FunctionalCaseDemandControllerTests extends BaseTest { functionalCaseDemandExample.createCriteria().andCaseIdEqualTo("DEMAND_TEST_FUNCTIONAL_CASE_ID"); List functionalCaseDemands = functionalCaseDemandMapper.selectByExample(functionalCaseDemandExample); Assertions.assertFalse(functionalCaseDemands.isEmpty()); + + functionalCaseDemandRequest = new FunctionalCaseDemandRequest(); + functionalCaseDemandRequest.setCaseId("DEMAND_TEST_FUNCTIONAL_CASE_ID"); + functionalCaseDemandRequest.setDemandPlatform("LOCAL"); + demandList = new ArrayList<>(); + demandDTO = new DemandDTO(); + demandDTO.setDemandName("手动加入孩子"); + demandList.add(demandDTO); + functionalCaseDemandRequest.setDemandList(demandList); + this.requestPostWithOkAndReturn(URL_DEMAND_ADD, functionalCaseDemandRequest); + functionalCaseDemandExample = new FunctionalCaseDemandExample(); + functionalCaseDemandExample.createCriteria().andCaseIdEqualTo("DEMAND_TEST_FUNCTIONAL_CASE_ID"); + functionalCaseDemands = functionalCaseDemandMapper.selectByExample(functionalCaseDemandExample); + Assertions.assertFalse(functionalCaseDemands.isEmpty()); } @Test @@ -82,6 +97,20 @@ public class FunctionalCaseDemandControllerTests extends BaseTest { functionalCaseDemandExample.createCriteria().andCaseIdEqualTo("DEMAND_TEST_FUNCTIONAL_CASE_ID2"); List functionalCaseDemands = functionalCaseDemandMapper.selectByExample(functionalCaseDemandExample); Assertions.assertTrue(functionalCaseDemands.isEmpty()); + + functionalCaseDemandRequest = new FunctionalCaseDemandRequest(); + functionalCaseDemandRequest.setCaseId("DEMAND_TEST_FUNCTIONAL_CASE_ID3"); + functionalCaseDemandRequest.setDemandPlatform("LOCAL"); + demandList = new ArrayList<>(); + DemandDTO demandDTO = new DemandDTO(); + demandDTO.setDemandName("手动加入3"); + demandList.add(demandDTO); + functionalCaseDemandRequest.setDemandList(demandList); + this.requestPostWithOkAndReturn(URL_DEMAND_ADD, functionalCaseDemandRequest); + functionalCaseDemandExample = new FunctionalCaseDemandExample(); + functionalCaseDemandExample.createCriteria().andCaseIdEqualTo("DEMAND_TEST_FUNCTIONAL_CASE_ID3"); + functionalCaseDemands = functionalCaseDemandMapper.selectByExample(functionalCaseDemandExample); + Assertions.assertFalse(functionalCaseDemands.isEmpty()); } @Test @@ -212,14 +241,17 @@ public class FunctionalCaseDemandControllerTests extends BaseTest { queryDemandListRequest.setPageSize(5); queryDemandListRequest.setCaseId("DEMAND_TEST_FUNCTIONAL_CASE_ID"); MvcResult mvcResult = this.requestPostWithOkAndReturn(URL_DEMAND_PAGE, queryDemandListRequest); - Pager> tableData = JSON.parseObject(JSON.toJSONString( + Pager> tableData = JSON.parseObject(JSON.toJSONString( JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData()), Pager.class); //返回值的页码和当前页码相同 Assertions.assertEquals(tableData.getCurrent(), queryDemandListRequest.getCurrent()); + List list = JSON.parseArray(JSON.toJSONString(tableData.getList()), FunctionalDemandDTO.class); + for (FunctionalDemandDTO functionalDemandDTO : list) { + Assertions.assertTrue(CollectionUtils.isNotEmpty(functionalDemandDTO.getChildren())); + } //返回的数据量不超过规定要返回的数据量相同 Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(tableData.getList())).size() <= queryDemandListRequest.getPageSize()); - queryDemandListRequest = new QueryDemandListRequest(); queryDemandListRequest.setCaseId("DEMAND_TEST_FUNCTIONAL_CASE_ID2"); queryDemandListRequest.setCurrent(1); @@ -232,20 +264,40 @@ public class FunctionalCaseDemandControllerTests extends BaseTest { Assertions.assertEquals(tableData.getCurrent(), queryDemandListRequest.getCurrent()); //返回的数据量为空 Assertions.assertTrue(CollectionUtils.isEmpty(tableData.getList())); + + queryDemandListRequest = new QueryDemandListRequest(); + queryDemandListRequest.setCurrent(1); + queryDemandListRequest.setPageSize(5); + queryDemandListRequest.setCaseId("DEMAND_TEST_FUNCTIONAL_CASE_ID3"); + mvcResult = this.requestPostWithOkAndReturn(URL_DEMAND_PAGE, queryDemandListRequest); + tableData = JSON.parseObject(JSON.toJSONString( + JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData()), + Pager.class); + //返回值的页码和当前页码相同 + Assertions.assertEquals(tableData.getCurrent(), queryDemandListRequest.getCurrent()); + //返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(tableData.getList())).size() <= queryDemandListRequest.getPageSize()); + List list1 = JSON.parseArray(JSON.toJSONString(tableData.getList()), FunctionalDemandDTO.class); + for (FunctionalDemandDTO functionalDemandDTO : list1) { + Assertions.assertTrue(CollectionUtils.isEmpty(functionalDemandDTO.getChildren())); + } } @Test @Order(8) public void cancelDemand() throws Exception { + FunctionalCaseDemandExample functionalCaseDemandExample = new FunctionalCaseDemandExample(); + functionalCaseDemandExample.createCriteria().andCaseIdEqualTo("DEMAND_TEST_FUNCTIONAL_CASE_ID"); + List beforeList = functionalCaseDemandMapper.selectByExample(functionalCaseDemandExample); String id = getId("DEMAND_TEST_FUNCTIONAL_CASE_ID"); mockMvc.perform(MockMvcRequestBuilders.get(URL_DEMAND_CANCEL+id).header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); - FunctionalCaseDemandExample functionalCaseDemandExample = new FunctionalCaseDemandExample(); + functionalCaseDemandExample = new FunctionalCaseDemandExample(); functionalCaseDemandExample.createCriteria().andCaseIdEqualTo("DEMAND_TEST_FUNCTIONAL_CASE_ID"); - List functionalCaseDemands = functionalCaseDemandMapper.selectByExample(functionalCaseDemandExample); - Assertions.assertTrue(CollectionUtils.isEmpty(functionalCaseDemands)); + List after = functionalCaseDemandMapper.selectByExample(functionalCaseDemandExample); + Assertions.assertTrue(beforeList.size()>after.size()); checkLog("DEMAND_TEST_FUNCTIONAL_CASE_ID", OperationLogType.DISASSOCIATE); } diff --git a/backend/services/case-management/src/test/resources/dml/init_case_demand.sql b/backend/services/case-management/src/test/resources/dml/init_case_demand.sql index 3d9b5a4f69..ce1553b381 100644 --- a/backend/services/case-management/src/test/resources/dml/init_case_demand.sql +++ b/backend/services/case-management/src/test/resources/dml/init_case_demand.sql @@ -14,6 +14,9 @@ VALUES ('DEMAND_TEST_FUNCTIONAL_CASE_ID', 1, 'DEMAND_TEST_MODULE_ID', 'project-c INSERT INTO functional_case(id, num, module_id, project_id, template_id, name, review_status, tags, case_edit_type, pos, version_id, ref_id, last_execute_result, deleted, public_case, latest, create_user, update_user, delete_user, create_time, update_time, delete_time) VALUES ('DEMAND_TEST_FUNCTIONAL_CASE_ID2', 1, 'DEMAND_TEST_MODULE_ID', 'project-case-demand-test', '100001', '关联需求测试', 'UN_REVIEWED', NULL, 'STEP', 0, 'v1.0.0', 'DEMAND_TEST_FUNCTIONAL_CASE_ID2', 'UN_EXECUTED', true, b'0', b'0', 'gyq', 'gyq', '', 1698058347559, 1698058347559, NULL); +INSERT INTO functional_case(id, num, module_id, project_id, template_id, name, review_status, tags, case_edit_type, pos, version_id, ref_id, last_execute_result, deleted, public_case, latest, create_user, update_user, delete_user, create_time, update_time, delete_time) +VALUES ('DEMAND_TEST_FUNCTIONAL_CASE_ID3', 1, 'DEMAND_TEST_MODULE_ID', 'project-case-demand-test', '100001', '关联需求测试', 'UN_REVIEWED', NULL, 'STEP', 0, 'v1.0.0', 'DEMAND_TEST_FUNCTIONAL_CASE_ID3', 'UN_EXECUTED', true, b'0', b'0', 'gyq', 'gyq', '', 1698058347559, 1698058347559, NULL); + INSERT INTO functional_case_custom_field(case_id, field_id, value) VALUES ('DEMAND_TEST_FUNCTIONAL_CASE_ID', 'gyq_custom_id_demand1', '22'); INSERT INTO functional_case_custom_field(case_id, field_id, value) VALUES ('DEMAND_TEST_FUNCTIONAL_CASE_ID', 'gyq_custom_id_demand2', '33');