From 41fac4008c56610f653431070c854f279dc10c12 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Fri, 15 Nov 2024 19:04:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E8=BF=94=E5=9B=9E=E6=89=A7=E8=A1=8C=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=88=A0=E9=99=A4=E5=88=A4=E6=96=AD=E6=A0=87?= =?UTF-8?q?=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/definition/ExecuteReportDTO.java | 3 +++ .../definition/ApiTestCaseService.java | 27 ++++++++++++++++++- .../ApiScenarioControllerTests.java | 5 +++- .../resources/dml/init_exec_history_test.sql | 11 ++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 backend/services/api-test/src/test/resources/dml/init_exec_history_test.sql diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ExecuteReportDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ExecuteReportDTO.java index 9dfe757549..6bbe3a2a00 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ExecuteReportDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ExecuteReportDTO.java @@ -48,6 +48,9 @@ public class ExecuteReportDTO implements Serializable { @Schema(description = "测试计划Num") private String testPlanNum; + @Schema(description = "结果是否被删除") + private Boolean resultDeleted = true; + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java index 063e198105..ac65f74b7d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java @@ -46,6 +46,8 @@ import io.metersphere.system.utils.ServiceUtils; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -111,6 +113,8 @@ public class ApiTestCaseService extends MoveNodeService { private ExtApiScenarioMapper extApiScenarioMapper; private static final String CASE_TABLE = "api_test_case"; + @Resource + private ApiReportRelateTaskMapper apiReportRelateTaskMapper; private void checkProjectExist(String projectId) { Project project = projectMapper.selectByPrimaryKey(projectId); @@ -578,10 +582,11 @@ public class ApiTestCaseService extends MoveNodeService { private List handleList(List historyList) { List executeReportDTOList = new ArrayList<>(); - if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(historyList)) { + if (CollectionUtils.isNotEmpty(historyList)) { List userIds = historyList.stream().map(ExecHistoryDTO::getCreateUser).toList(); Map userNameMap = userLoginService.getUserNameMap(userIds); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); + Map reportMap = getReportMap(historyList); historyList.forEach(item -> { ExecuteReportDTO reportDTO = new ExecuteReportDTO(); BeanUtils.copyBean(reportDTO, item); @@ -591,12 +596,32 @@ public class ApiTestCaseService extends MoveNodeService { if (StringUtils.isNotBlank(item.getTestPlanNum())) { reportDTO.setTestPlanNum(StringUtils.join(Translator.get("test_plan"), ": ", item.getTestPlanNum())); } + if (BooleanUtils.isTrue(item.getIntegrated()) && reportMap.containsKey(item.getTaskId())) { + reportDTO.setResultDeleted(false); + } + if (BooleanUtils.isFalse(item.getIntegrated()) && reportMap.containsKey(item.getItemId())) { + reportDTO.setResultDeleted(false); + } executeReportDTOList.add(reportDTO); }); } return executeReportDTOList; } + private Map getReportMap(List list) { + Map reportMap = new HashMap<>(); + List integratedTaskIds = list.stream().filter(item -> BooleanUtils.isTrue(item.getIntegrated())).map(ExecHistoryDTO::getTaskId).toList(); + List noIntegratedTaskItemIds = list.stream().filter(item -> BooleanUtils.isFalse(item.getIntegrated())).map(ExecHistoryDTO::getItemId).toList(); + List resourceIds = ListUtils.union(integratedTaskIds, noIntegratedTaskItemIds); + if (CollectionUtils.isNotEmpty(resourceIds)) { + ApiReportRelateTaskExample example = new ApiReportRelateTaskExample(); + example.createCriteria().andTaskResourceIdIn(resourceIds); + List reportRelateTasks = apiReportRelateTaskMapper.selectByExample(example); + reportMap = reportRelateTasks.stream().collect(Collectors.toMap(ApiReportRelateTask::getTaskResourceId, ApiReportRelateTask::getReportId)); + } + return reportMap; + } + public List operationHistoryList(OperationHistoryRequest request) { return operationHistoryService.listWidthTable(request, CASE_TABLE); } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java index 66849375a9..94d828f940 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java @@ -77,6 +77,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -2704,6 +2706,7 @@ public class ApiScenarioControllerTests extends BaseTest { @Test @Order(10) + @Sql(scripts = {"/dml/init_exec_history_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) public void testExecuteList() throws Exception { ApiScenario first = apiScenarioMapper.selectByExample(new ApiScenarioExample()).getFirst(); List reports = new ArrayList<>(); @@ -2749,7 +2752,7 @@ public class ApiScenarioControllerTests extends BaseTest { } apiScenarioReportService.insertApiScenarioReport(reports, records); ExecutePageRequest request = new ExecutePageRequest(); - request.setId(first.getId()); + request.setId("1"); request.setPageSize(10); request.setCurrent(1); MvcResult mvcResult = responsePost(BASE_PATH + "execute/page", request); diff --git a/backend/services/api-test/src/test/resources/dml/init_exec_history_test.sql b/backend/services/api-test/src/test/resources/dml/init_exec_history_test.sql new file mode 100644 index 0000000000..0a1c647a6c --- /dev/null +++ b/backend/services/api-test/src/test/resources/dml/init_exec_history_test.sql @@ -0,0 +1,11 @@ +INSERT INTO `exec_task`(`id`, `num`, `task_name`, `status`, `case_count`, `result`, `task_type`, `trigger_mode`, `project_id`, `organization_id`, `create_time`, `create_user`, `start_time`, `end_time`, `integrated`) +VALUES + ('my_1', 1111, '测试1', 'SUCCESS', 10, 'SUCCESS', 'API_SCENARIO', 'API', '100001100001', '100001', 1727676089639, 'wx', 1727676089639, 1727676089639, 1), + ('my_2', 2222, '测试2', 'SUCCESS', 11, 'SUCCESS', 'API_SCENARIO', 'API', '12345567', '11234', 1727676089639, 'wx', 1727676089639, 1727676089639, 0); + + +INSERT INTO `exec_task_item`(`id`, `task_id`, `resource_id`, `resource_name`, `task_origin`, `status`, `result`, `resource_pool_id`, `resource_pool_node`, `resource_type`, `project_id`, `organization_id`, `thread_id`, `start_time`, `end_time`, `executor`, `case_id`) +VALUES + ('my_1', 'my_1', '1', '1', '1', 'SUCCESS', 'SUCCESS', '1', '1', 'API_SCENARIO', '100001100001', '100001', '1', NULL, NULL, 'admin', '1'), + ('my_2', 'my_2', '1', '2', '3', 'SUCCESS', 'SUCCESS', '2', '1', 'API_SCENARIO', '100001100001', '100001', '1', NULL, NULL, 'admin', '2'); +