refactor(接口测试): 返回执行历史是否删除判断标识

This commit is contained in:
WangXu10 2024-11-15 19:04:15 +08:00 committed by Craftsman
parent b3ef97eb67
commit 41fac4008c
4 changed files with 44 additions and 2 deletions

View File

@ -48,6 +48,9 @@ public class ExecuteReportDTO implements Serializable {
@Schema(description = "测试计划Num") @Schema(description = "测试计划Num")
private String testPlanNum; private String testPlanNum;
@Schema(description = "结果是否被删除")
private Boolean resultDeleted = true;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
} }

View File

@ -46,6 +46,8 @@ import io.metersphere.system.utils.ServiceUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; 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.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
@ -111,6 +113,8 @@ public class ApiTestCaseService extends MoveNodeService {
private ExtApiScenarioMapper extApiScenarioMapper; private ExtApiScenarioMapper extApiScenarioMapper;
private static final String CASE_TABLE = "api_test_case"; private static final String CASE_TABLE = "api_test_case";
@Resource
private ApiReportRelateTaskMapper apiReportRelateTaskMapper;
private void checkProjectExist(String projectId) { private void checkProjectExist(String projectId) {
Project project = projectMapper.selectByPrimaryKey(projectId); Project project = projectMapper.selectByPrimaryKey(projectId);
@ -578,10 +582,11 @@ public class ApiTestCaseService extends MoveNodeService {
private List<ExecuteReportDTO> handleList(List<ExecHistoryDTO> historyList) { private List<ExecuteReportDTO> handleList(List<ExecHistoryDTO> historyList) {
List<ExecuteReportDTO> executeReportDTOList = new ArrayList<>(); List<ExecuteReportDTO> executeReportDTOList = new ArrayList<>();
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(historyList)) { if (CollectionUtils.isNotEmpty(historyList)) {
List<String> userIds = historyList.stream().map(ExecHistoryDTO::getCreateUser).toList(); List<String> userIds = historyList.stream().map(ExecHistoryDTO::getCreateUser).toList();
Map<String, String> userNameMap = userLoginService.getUserNameMap(userIds); Map<String, String> userNameMap = userLoginService.getUserNameMap(userIds);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Map<String, String> reportMap = getReportMap(historyList);
historyList.forEach(item -> { historyList.forEach(item -> {
ExecuteReportDTO reportDTO = new ExecuteReportDTO(); ExecuteReportDTO reportDTO = new ExecuteReportDTO();
BeanUtils.copyBean(reportDTO, item); BeanUtils.copyBean(reportDTO, item);
@ -591,12 +596,32 @@ public class ApiTestCaseService extends MoveNodeService {
if (StringUtils.isNotBlank(item.getTestPlanNum())) { if (StringUtils.isNotBlank(item.getTestPlanNum())) {
reportDTO.setTestPlanNum(StringUtils.join(Translator.get("test_plan"), ": ", 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); executeReportDTOList.add(reportDTO);
}); });
} }
return executeReportDTOList; return executeReportDTOList;
} }
private Map<String, String> getReportMap(List<ExecHistoryDTO> list) {
Map<String, String> reportMap = new HashMap<>();
List<String> integratedTaskIds = list.stream().filter(item -> BooleanUtils.isTrue(item.getIntegrated())).map(ExecHistoryDTO::getTaskId).toList();
List<String> noIntegratedTaskItemIds = list.stream().filter(item -> BooleanUtils.isFalse(item.getIntegrated())).map(ExecHistoryDTO::getItemId).toList();
List<String> resourceIds = ListUtils.union(integratedTaskIds, noIntegratedTaskItemIds);
if (CollectionUtils.isNotEmpty(resourceIds)) {
ApiReportRelateTaskExample example = new ApiReportRelateTaskExample();
example.createCriteria().andTaskResourceIdIn(resourceIds);
List<ApiReportRelateTask> reportRelateTasks = apiReportRelateTaskMapper.selectByExample(example);
reportMap = reportRelateTasks.stream().collect(Collectors.toMap(ApiReportRelateTask::getTaskResourceId, ApiReportRelateTask::getReportId));
}
return reportMap;
}
public List<OperationHistoryDTO> operationHistoryList(OperationHistoryRequest request) { public List<OperationHistoryDTO> operationHistoryList(OperationHistoryRequest request) {
return operationHistoryService.listWidthTable(request, CASE_TABLE); return operationHistoryService.listWidthTable(request, CASE_TABLE);
} }

View File

@ -77,6 +77,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile; 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.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@ -2704,6 +2706,7 @@ public class ApiScenarioControllerTests extends BaseTest {
@Test @Test
@Order(10) @Order(10)
@Sql(scripts = {"/dml/init_exec_history_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void testExecuteList() throws Exception { public void testExecuteList() throws Exception {
ApiScenario first = apiScenarioMapper.selectByExample(new ApiScenarioExample()).getFirst(); ApiScenario first = apiScenarioMapper.selectByExample(new ApiScenarioExample()).getFirst();
List<ApiScenarioReport> reports = new ArrayList<>(); List<ApiScenarioReport> reports = new ArrayList<>();
@ -2749,7 +2752,7 @@ public class ApiScenarioControllerTests extends BaseTest {
} }
apiScenarioReportService.insertApiScenarioReport(reports, records); apiScenarioReportService.insertApiScenarioReport(reports, records);
ExecutePageRequest request = new ExecutePageRequest(); ExecutePageRequest request = new ExecutePageRequest();
request.setId(first.getId()); request.setId("1");
request.setPageSize(10); request.setPageSize(10);
request.setCurrent(1); request.setCurrent(1);
MvcResult mvcResult = responsePost(BASE_PATH + "execute/page", request); MvcResult mvcResult = responsePost(BASE_PATH + "execute/page", request);

View File

@ -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');