diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java index fcafb5c28f..d0b2c535e5 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -391,22 +391,22 @@ public class TestPlanApiCaseService { if (caseWithBLOBs != null) { apiResult.setName(caseWithBLOBs.getName()); } - if(StringUtils.equalsIgnoreCase(request.getTriggerMode(),ApiRunMode.SCHEDULE_API_PLAN.name())) { + if (StringUtils.equalsIgnoreCase(request.getTriggerMode(), ApiRunMode.SCHEDULE_API_PLAN.name())) { apiResult.setTriggerMode(TriggerMode.SCHEDULE.name()); - }else if(StringUtils.equalsIgnoreCase(request.getTriggerMode(),ApiRunMode.JENKINS_API_PLAN.name())){ + } else if (StringUtils.equalsIgnoreCase(request.getTriggerMode(), ApiRunMode.JENKINS_API_PLAN.name())) { apiResult.setTriggerMode(TriggerMode.MANUAL.name()); - }else { + } else { apiResult.setTriggerMode(TriggerMode.BATCH.name()); } apiResult.setActuator("LOCAL"); if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) { apiResult.setActuator(request.getConfig().getResourcePoolId()); } - if(StringUtils.isEmpty(request.getUserId())){ + if (StringUtils.isEmpty(request.getUserId())) { if (SessionUtils.getUser() != null) { apiResult.setUserId(SessionUtils.getUser().getId()); } - }else { + } else { apiResult.setUserId(request.getUserId()); } @@ -423,6 +423,7 @@ public class TestPlanApiCaseService { List ids = request.getPlanIds(); TestPlanApiCaseExample example = new TestPlanApiCaseExample(); example.createCriteria().andIdIn(ids); + example.setOrderByClause("`order` DESC"); List planApiCases = testPlanApiCaseMapper.selectByExample(example); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); ApiDefinitionExecResultMapper batchMapper = sqlSession.getMapper(ApiDefinitionExecResultMapper.class); @@ -449,13 +450,13 @@ public class TestPlanApiCaseService { planApiCases.forEach(testPlanApiCase -> { ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Waiting.name(), batchMapper); executeQueue.put(testPlanApiCase, report); - executeThreadIdMap.put(testPlanApiCase.getId(),report.getId()); + executeThreadIdMap.put(testPlanApiCase.getId(), report.getId()); }); //如果是测试计划生成报告的执行,则更新执行信息、执行线程信息。 - if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ + if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) { if (!executeThreadIdMap.isEmpty()) { - TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap,null, null); + TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap, null, null); } } @@ -503,7 +504,7 @@ public class TestPlanApiCaseService { mapper.updateByPrimaryKey(execResult); modeDataDTO.setApiCaseId(execResult.getId()); modeDataDTO.setDebugReportId(request.getPlanReportId()); - modeDataDTO.setTestId(modeDataDTO.getTestId()+":"+request.getPlanReportId()+ ":"+ execResult.getId()); + modeDataDTO.setTestId(modeDataDTO.getTestId() + ":" + request.getPlanReportId() + ":" + execResult.getId()); Future future = executorService.submit(new SerialApiExecTask(jMeterService, mapper, modeDataDTO, request.getConfig(), request.getTriggerMode())); ApiDefinitionExecResult report = future.get(); // 如果开启失败结束执行,则判断返回结果状态 @@ -533,11 +534,11 @@ public class TestPlanApiCaseService { } //如果是测试计划生成报告的执行,则更新执行信息、执行线程信息。 - if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ + if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) { if (!executeErrorList.isEmpty()) { - Map executeErrorMap = new HashMap<>(); - for(String id : executeErrorList){ - executeErrorMap.put(id,TestPlanApiExecuteStatus.FAILD.name()); + Map executeErrorMap = new HashMap<>(); + for (String id : executeErrorList) { + executeErrorMap.put(id, TestPlanApiExecuteStatus.FAILD.name()); } TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(request.getPlanReportId(), executeErrorMap, null, null); } @@ -558,14 +559,14 @@ public class TestPlanApiCaseService { planApiCases.forEach(testPlanApiCase -> { ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Running.name(), batchMapper); executeQueue.put(report.getId(), testPlanApiCase); - executeThreadIdMap.put(testPlanApiCase.getId(),report.getId()); + executeThreadIdMap.put(testPlanApiCase.getId(), report.getId()); MessageCache.caseExecResourceLock.put(report.getId(), report); }); sqlSession.commit(); //如果是测试计划生成报告的执行,则更新执行信息、执行线程信息。 - if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ + if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) { if (!executeThreadIdMap.isEmpty()) { - TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap,null, null); + TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap, null, null); } } @@ -577,28 +578,28 @@ public class TestPlanApiCaseService { try { String debugId = request.getPlanReportId(); if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) { - String testId = testPlanApiCase.getId()+":"+ request.getPlanReportId() + ":" +reportId; + String testId = testPlanApiCase.getId() + ":" + request.getPlanReportId() + ":" + reportId; jMeterService.runTest(testId, reportId, request.getTriggerMode(), request.getPlanReportId(), request.getConfig()); } else { HashTree hashTree = generateHashTree(testPlanApiCase.getId()); - if(StringUtils.isEmpty(debugId)){ + if (StringUtils.isEmpty(debugId)) { debugId = TriggerMode.BATCH.name(); } - String testId = reportId+":"+ request.getPlanReportId(); - jMeterService.runLocal(testId,request.getConfig(), hashTree, debugId, request.getTriggerMode()); + String testId = reportId + ":" + request.getPlanReportId(); + jMeterService.runLocal(testId, request.getConfig(), hashTree, debugId, request.getTriggerMode()); } - }catch (Exception e){ + } catch (Exception e) { executeErrorList.add(testPlanApiCase.getId()); } } //如果是测试计划生成报告的执行,则更新执行信息、执行线程信息。 - if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ + if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) { if (!executeErrorList.isEmpty()) { - Map executeErrorMap = new HashMap<>(); - for(String id : executeErrorList){ - executeErrorMap.put(id,TestPlanApiExecuteStatus.FAILD.name()); + Map executeErrorMap = new HashMap<>(); + for (String id : executeErrorList) { + executeErrorMap.put(id, TestPlanApiExecuteStatus.FAILD.name()); } TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(request.getPlanReportId(), executeErrorMap, null, null); } @@ -745,25 +746,25 @@ public class TestPlanApiCaseService { testPlanApiCaseMapper::updateByPrimaryKeySelective); } - public List getByApiExecReportIds(Map testPlanApiCaseReportMap,boolean isFinish) { + public List getByApiExecReportIds(Map testPlanApiCaseReportMap, boolean isFinish) { if (testPlanApiCaseReportMap.isEmpty()) { return new ArrayList<>(); } String defaultStatus = "Running"; - if(isFinish){ + if (isFinish) { defaultStatus = "error"; } - List apiTestCases = extTestPlanApiCaseMapper.getFailureListByIds(testPlanApiCaseReportMap.keySet(),null); - Map reportResult = apiDefinitionExecResultService.selectReportResultByReportIds(testPlanApiCaseReportMap.values()); + List apiTestCases = extTestPlanApiCaseMapper.getFailureListByIds(testPlanApiCaseReportMap.keySet(), null); + Map reportResult = apiDefinitionExecResultService.selectReportResultByReportIds(testPlanApiCaseReportMap.values()); for (TestPlanFailureApiDTO dto : apiTestCases) { String testPlanApiCaseId = dto.getId(); String reportId = testPlanApiCaseReportMap.get(testPlanApiCaseId); dto.setReportId(reportId); - if(StringUtils.isEmpty(reportId)){ + if (StringUtils.isEmpty(reportId)) { dto.setExecResult(defaultStatus); - }else { + } else { String status = reportResult.get(reportId); - if(status == null){ + if (status == null) { status = defaultStatus; } dto.setExecResult(status); diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java index f229abd377..27aeabc3a4 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -131,28 +131,17 @@ public class TestPlanReportService { } public TestPlanScheduleReportInfoDTO genTestPlanReportBySchedule(String projectID, String planId, String userId, String triggerMode) { - Map> apiTestCaseIdMap = new LinkedHashMap<>(); - Map planScenarioIdMap = new LinkedHashMap<>(); - Map apiTestCaseDataMap = new LinkedHashMap<>(); + Map planTestCaseIdMap = new LinkedHashMap<>(); Map performanceIdMap = new LinkedHashMap<>(); - List testPlanApiScenarioList = extTestPlanScenarioCaseMapper.selectLegalDataByTestPlanId(planId); for (TestPlanApiScenario model : testPlanApiScenarioList) { planScenarioIdMap.put(model.getId(), model.getApiScenarioId()); } List testPlanApiCaseList = extTestPlanApiCaseMapper.selectLegalDataByTestPlanId(planId); for (TestPlanApiCase model : testPlanApiCaseList) { - if (apiTestCaseIdMap.containsKey(model.getApiCaseId())) { - apiTestCaseIdMap.get(model.getApiCaseId()).add(model.getId()); - } else { - List list = new ArrayList<>(); - list.add(model.getId()); - apiTestCaseIdMap.put(model.getApiCaseId(), list); - } - + planTestCaseIdMap.put(model.getId(), model.getApiCaseId()); } - LoadCaseRequest loadCaseRequest = new LoadCaseRequest(); loadCaseRequest.setTestPlanId(planId); loadCaseRequest.setProjectId(projectID); @@ -160,29 +149,12 @@ public class TestPlanReportService { for (TestPlanLoadCaseDTO dto : testPlanLoadCaseDTOList) { performanceIdMap.put(dto.getId(), dto.getLoadCaseId()); } - String planReportId = UUID.randomUUID().toString(); Map apiCaseInfoMap = new HashMap<>(); - if (!apiTestCaseIdMap.isEmpty()) { - //筛选出未被删除/放入回收站的案例 - ApiTestCaseExample apiTestCaseExample = new ApiTestCaseExample(); - apiTestCaseExample.createCriteria().andIdIn(new ArrayList<>(apiTestCaseIdMap.keySet())); - List apiCaseList = apiTestCaseMapper.selectByExample(apiTestCaseExample); - if (!apiCaseList.isEmpty()) { - Map apiCaseDataMap = apiCaseList.stream().collect(Collectors.toMap(ApiTestCase::getId, k -> k)); - for (String id : apiCaseDataMap.keySet()) { - List testPlanCaseIdList = apiTestCaseIdMap.get(id); - if (CollectionUtils.isNotEmpty(testPlanCaseIdList)) { - for (String testPlanCaseId : testPlanCaseIdList) { - apiCaseInfoMap.put(testPlanCaseId, TestPlanApiExecuteStatus.PREPARE.name()); - apiTestCaseDataMap.put(testPlanCaseId, id); - } - } - } - } + for (String id : planTestCaseIdMap.keySet()) { + apiCaseInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); } - Map scenarioInfoMap = new HashMap<>(); for (String id : planScenarioIdMap.keySet()) { scenarioInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); @@ -192,14 +164,13 @@ public class TestPlanReportService { performanceInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); } TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(planReportId, planId, userId, triggerMode, - apiTestCaseDataMap.size() > 0, planScenarioIdMap.size() > 0, performanceIdMap.size() > 0, + planTestCaseIdMap.size() > 0, planScenarioIdMap.size() > 0, performanceIdMap.size() > 0, apiCaseInfoMap, scenarioInfoMap, performanceInfoMap); TestPlanReport report = this.genTestPlanReport(saveRequest); - TestPlanScheduleReportInfoDTO returnDTO = new TestPlanScheduleReportInfoDTO(); returnDTO.setTestPlanReport(report); returnDTO.setPlanScenarioIdMap(planScenarioIdMap); - returnDTO.setApiTestCaseDataMap(apiTestCaseDataMap); + returnDTO.setApiTestCaseDataMap(planTestCaseIdMap); returnDTO.setPerformanceIdMap(performanceIdMap); return returnDTO; } @@ -1171,14 +1142,14 @@ public class TestPlanReportService { if (StringUtils.isNotBlank(testPlanReportContent.getScenarioAllCases())) { List allCases = JSONObject.parseArray(testPlanReportContent.getScenarioAllCases(), TestPlanFailureScenarioDTO.class); for (TestPlanFailureScenarioDTO dto : allCases) { - if (StringUtils.equalsAnyIgnoreCase("Underway",dto.getStatus(), dto.getLastResult())) { + if (StringUtils.equalsAnyIgnoreCase("Underway", dto.getStatus(), dto.getLastResult())) { isUpdate = true; ApiScenarioReport apiReport = apiScenarioReportMapper.selectByPrimaryKey(dto.getReportId()); if (apiReport != null) { dto.setLastResult(apiReport.getStatus()); dto.setStatus(apiReport.getStatus()); } - }else if (StringUtils.equalsAnyIgnoreCase("Error",dto.getStatus(), dto.getLastResult())) { + } else if (StringUtils.equalsAnyIgnoreCase("Error", dto.getStatus(), dto.getLastResult())) { isUpdate = true; dto.setLastResult("Fail"); dto.setStatus("Fail");