From b90f5c05519f3dc06e4f0476d7abd1e7c4d6a1c0 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 3 Aug 2021 15:57:08 +0800 Subject: [PATCH] =?UTF-8?q?fix=20(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E4=BF=AE=E5=A4=8D=E9=9B=86=E5=90=88=E6=8A=A5?= =?UTF-8?q?=E5=91=8A=20=E5=9C=BA=E6=99=AF=E5=90=8E=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98#1005605?= =?UTF-8?q?=20--bug=3D1005605=20--user=3D=E8=B5=B5=E5=8B=87=20=E3=80=90?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8=E5=8C=96=E3=80=91=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E5=88=97...=20https://www.tapd.cn/55049933/s/1031760?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiAutomationService.java | 20 +++---- .../api/service/ApiScenarioReportService.java | 56 +++++++++---------- .../api/service/TestResultService.java | 10 ++-- .../track/service/TestPlanService.java | 38 +++++-------- 4 files changed, 56 insertions(+), 68 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 670ebc1103..7833af4334 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -811,7 +811,7 @@ public class ApiAutomationService { return null; } - public APIScenarioReportResult createScenarioReport(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userID, RunModeConfig config) { + public APIScenarioReportResult createScenarioReport(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userID, RunModeConfig config,String desc) { APIScenarioReportResult report = new APIScenarioReportResult(); if (triggerMode.equals(ApiRunMode.SCENARIO.name()) || triggerMode.equals(ApiRunMode.DEFINITION.name())) { triggerMode = ReportTriggerMode.MANUAL.name(); @@ -844,7 +844,7 @@ public class ApiAutomationService { report.setProjectId(projectId); report.setScenarioName(scenarioName); report.setScenarioId(scenarioId); - + report.setDescription(desc); return report; } @@ -1027,14 +1027,14 @@ public class ApiAutomationService { if (request.isTestPlanScheduleJob()) { String savedScenarioId = testPlanScenarioId + ":" + request.getTestPlanReportId(); report = createScenarioReport(reportId, savedScenarioId, item.getName(), request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId()); } else { report = createScenarioReport(reportId, testPlanScenarioId, item.getName(), request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId()); } } else { report = createScenarioReport(reportId, ExecuteType.Marge.name().equals(request.getExecuteType()) ? serialReportId : item.getId(), item.getName(), request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId()); } try { if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) { @@ -1058,7 +1058,7 @@ public class ApiAutomationService { APIScenarioReportResult report = createScenarioReport(request.getConfig().getReportId(), JSON.toJSONString(CollectionUtils.isNotEmpty(scenarioIds) && scenarioIds.size() > 50 ? scenarioIds.subList(0, 50) : scenarioIds), scenarioNames.length() >= 3000 ? scenarioNames.substring(0, 2000) : scenarioNames.deleteCharAt(scenarioNames.toString().length() - 1).toString(), - ReportTriggerMode.MANUAL.name(), ExecuteType.Saved.name(), request.getProjectId(), request.getReportUserID(), request.getConfig()); + ReportTriggerMode.MANUAL.name(), ExecuteType.Saved.name(), request.getProjectId(), request.getReportUserID(), request.getConfig(),JSON.toJSONString(scenarioIds)); report.setName(request.getConfig().getReportName()); report.setId(serialReportId); @@ -1252,14 +1252,14 @@ public class ApiAutomationService { if (request.isTestPlanScheduleJob()) { String savedScenarioId = testPlanScenarioId + ":" + request.getTestPlanReportId(); report = createScenarioReport(group.getName(), savedScenarioId, item.getName(), request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId()); } else { report = createScenarioReport(group.getName(), testPlanScenarioId, item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId()); } } else { report = createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId()); } batchMapper.insert(report); reportIds.add(group.getName()); @@ -1476,7 +1476,7 @@ public class ApiAutomationService { } APIScenarioReportResult report = createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(), - SessionUtils.getUserId(), request.getConfig()); + SessionUtils.getUserId(), request.getConfig(),request.getId()); apiScenarioReportMapper.insert(report); uploadBodyFiles(request.getBodyFileRequestIds(), bodyFiles); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index 530496db84..6a5714d1ba 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -254,7 +254,7 @@ public class ApiScenarioReportService { return returnReport; } - public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMode) { + public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMode) { ApiScenarioReport lastReport = null; List scenarioResultList = result.getScenarios(); @@ -264,7 +264,7 @@ public class ApiScenarioReportService { List reportIds = new ArrayList<>(); List scenarioIdList = new ArrayList<>(); - Map scenarioAndErrorMap = new HashMap<>(); + Map scenarioAndErrorMap = new HashMap<>(); for (ScenarioResult scenarioResult : scenarioResultList) { // 存储场景报告 @@ -332,10 +332,10 @@ public class ApiScenarioReportService { } TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class); - testPlanLog.info("TestPlanReportId"+ JSONArray.toJSONString(testPlanReportIdList) +" EXECUTE OVER. SCENARIO STATUS : "+JSONObject.toJSONString(scenarioAndErrorMap)); + testPlanLog.info("TestPlanReportId" + JSONArray.toJSONString(testPlanReportIdList) + " EXECUTE OVER. SCENARIO STATUS : " + JSONObject.toJSONString(scenarioAndErrorMap)); - for (String planId :testPlanReportIdList) { - testPlanReportService.updateExecuteApis(planId,null,scenarioAndErrorMap,null); + for (String planId : testPlanReportIdList) { + testPlanReportService.updateExecuteApis(planId, null, scenarioAndErrorMap, null); } return lastReport; @@ -390,7 +390,8 @@ public class ApiScenarioReportService { List details = apiScenarioReportDetailMapper.selectByExampleWithBLOBs(example); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - + // 记录单场景通过率 + Map passRateMap = new HashMap<>(); for (ApiScenarioReportDetail detail : details) { try { String content = new String(detail.getContent(), StandardCharsets.UTF_8); @@ -409,6 +410,8 @@ public class ApiScenarioReportService { testResult.setScenarioStepError(scenarioResult.getScenarioStepError() + testResult.getScenarioStepError()); testResult.setScenarioStepSuccess(scenarioResult.getScenarioStepSuccess() + testResult.getScenarioStepSuccess()); testResult.setScenarioStepTotal(scenarioResult.getScenarioStepTotal() + testResult.getScenarioStepTotal()); + String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError())); + passRateMap.put(detail.getReportId(), passRate); } catch (Exception e) { LogUtil.error(e.getMessage()); } @@ -430,32 +433,25 @@ public class ApiScenarioReportService { apiScenarioReportDetailMapper.insert(detail); } // 更新场景状态 - if (StringUtils.isNotEmpty(report.getScenarioId())) { - List strings = JSON.parseObject(report.getScenarioId(), List.class); - ApiScenarioExample scenarioExample = new ApiScenarioExample(); - scenarioExample.createCriteria().andIdIn(strings); - - List scenarios = apiScenarioMapper.selectByExample(scenarioExample); - if (CollectionUtils.isNotEmpty(scenarios)) { - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - ApiScenarioMapper scenarioReportMapper = sqlSession.getMapper(ApiScenarioMapper.class); - scenarios.forEach(scenario -> { - if (testResult.getError() > 0) { - scenario.setLastResult("Fail"); - } else { - scenario.setLastResult("Success"); - } - String passRate = new DecimalFormat("0%").format((float) testResult.getSuccess() / (testResult.getSuccess() + testResult.getError())); - scenario.setPassRate(passRate); - scenario.setReportId(report.getId()); - scenarioReportMapper.updateByPrimaryKey(scenario); - }); - sqlSession.flushStatements(); - } + if (CollectionUtils.isNotEmpty(reportIds)) { + ApiScenarioReportExample scenarioReportExample = new ApiScenarioReportExample(); + scenarioReportExample.createCriteria().andIdIn(reportIds); + List reports = apiScenarioReportMapper.selectByExampleWithBLOBs(scenarioReportExample); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + ApiScenarioMapper scenarioReportMapper = sqlSession.getMapper(ApiScenarioMapper.class); + reports.forEach(apiScenarioReport -> { + ApiScenarioWithBLOBs scenario = new ApiScenarioWithBLOBs(); + scenario.setId(apiScenarioReport.getDescription()); + scenario.setLastResult(StringUtils.equals("Error", apiScenarioReport.getStatus()) ? "Fail" : apiScenarioReport.getStatus()); + scenario.setPassRate(passRateMap.get(apiScenarioReport.getId())); + scenario.setReportId(report.getId()); + scenarioReportMapper.updateByPrimaryKeySelective(scenario); + }); + sqlSession.flushStatements(); } // 清理其他报告保留一份合并后的报告 - this.deleteByIds(reportIds); - + passRateMap.clear(); + deleteByIds(reportIds); } } diff --git a/backend/src/main/java/io/metersphere/api/service/TestResultService.java b/backend/src/main/java/io/metersphere/api/service/TestResultService.java index f7d950d134..ce22238a13 100644 --- a/backend/src/main/java/io/metersphere/api/service/TestResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/TestResultService.java @@ -102,6 +102,10 @@ public class TestResultService { //环境 ApiScenarioWithBLOBs apiScenario = apiAutomationService.getDto(scenarioReport.getScenarioId()); String name = ""; + //执行人 + String userName = ""; + //负责人 + String principal = ""; if (apiScenario != null) { String executionEnvironment = apiScenario.getScenarioDefinition(); JSONObject json = JSONObject.parseObject(executionEnvironment); @@ -110,11 +114,9 @@ public class TestResultService { String environmentId = environment.get(apiScenario.getProjectId()).toString(); name = apiAutomationService.get(environmentId).getName(); } + userName = apiAutomationService.getUser(apiScenario.getUserId()); + principal = apiAutomationService.getUser(apiScenario.getPrincipal()); } - //执行人 - String userName = apiAutomationService.getUser(apiScenario.getUserId()); - //负责人 - String principal = apiAutomationService.getUser(apiScenario.getPrincipal()); //报告内容 reportTask = new ApiTestReportVariable(); reportTask.setStatus(scenarioReport.getStatus()); diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index a70331263a..4f8b675bcc 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -37,7 +37,6 @@ import io.metersphere.service.SystemParameterService; import io.metersphere.track.Factory.ReportComponentFactory; import io.metersphere.track.domain.ReportComponent; import io.metersphere.track.dto.*; -import io.metersphere.track.request.report.TestPlanReportSaveRequest; import io.metersphere.track.request.testcase.PlanCaseRelevanceRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; import io.metersphere.track.request.testplan.AddTestPlanRequest; @@ -53,7 +52,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; @@ -462,7 +460,7 @@ public class TestPlanService { testCaseExample.createCriteria().andIdIn(testCaseIds); List testCaseList = testCaseMapper.selectByExample(testCaseExample); Map userMap = testCaseList.stream() - .collect(HashMap::new, (m,v)-> m.put(v.getId(), v.getMaintainer()), HashMap::putAll); + .collect(HashMap::new, (m, v) -> m.put(v.getId(), v.getMaintainer()), HashMap::putAll); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); TestPlanTestCaseMapper batchMapper = sqlSession.getMapper(TestPlanTestCaseMapper.class); @@ -849,7 +847,7 @@ public class TestPlanService { Comparator.comparing(Issues::getCreateTime, (t1, t2) -> { if (t1 == null) { return 1; - } else if (t2 == null) { + } else if (t2 == null) { return -1; } return t2.compareTo(t1); @@ -1001,7 +999,7 @@ public class TestPlanService { APIScenarioReportResult report = apiAutomationService.createScenarioReport(group.getName(), planScenarioID + ":" + request.getTestPlanReportId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig()); + request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(), null); apiScenarioReportMapper.insert(report); group.setHashTree(scenarios); testPlan.getHashTree().add(group); @@ -1011,16 +1009,8 @@ public class TestPlanService { } public String run(String testPlanID, String projectID, String userId, String triggerMode, String apiRunConfig) { - if (StringUtils.isEmpty(apiRunConfig)) { - apiRunConfig = - "{\"mode\":\"parallel\"," + - "\"reportType\":\"iddReport\"," + - "\"onSampleError\":true," + - "\"runWithinResourcePool\":true," + - "\"resourcePoolId\":\"29773f4f-55e4-4bce-ad3d-b531b4eb59c2\"}"; - } //创建测试报告,然后返回的ID重新赋值为resourceID,作为后续的参数 - TestPlanScheduleReportInfoDTO reportInfoDTO = testPlanReportService.genTestPlanReportBySchedule(projectID,testPlanID,userId,triggerMode); + TestPlanScheduleReportInfoDTO reportInfoDTO = testPlanReportService.genTestPlanReportBySchedule(projectID, testPlanID, userId, triggerMode); TestPlanReport testPlanReport = reportInfoDTO.getTestPlanReport(); Map planScenarioIdMap = reportInfoDTO.getPlanScenarioIdMap(); @@ -1029,12 +1019,12 @@ public class TestPlanService { String planReportId = testPlanReport.getId(); - testPlanLog.info("ReportId["+planReportId+"] created. TestPlanID:["+testPlanID+"]. "); + testPlanLog.info("ReportId[" + planReportId + "] created. TestPlanID:[" + testPlanID + "]. "); //不同任务的执行ID - Map executePerformanceIdMap = new HashMap<>(); - Map executeApiCaseIdMap = new HashMap<>(); - Map executeScenarioCaseIdMap = new HashMap<>(); + Map executePerformanceIdMap = new HashMap<>(); + Map executeApiCaseIdMap = new HashMap<>(); + Map executeScenarioCaseIdMap = new HashMap<>(); //执行性能测试任务 List performaneReportIDList = new ArrayList<>(); @@ -1070,7 +1060,7 @@ public class TestPlanService { e.printStackTrace(); } if (StringUtils.isNotEmpty(reportId)) { - executePerformanceIdMap.put(caseID,TestPlanApiExecuteStatus.RUNNING.name()); + executePerformanceIdMap.put(caseID, TestPlanApiExecuteStatus.RUNNING.name()); } } if (!performaneReportIDList.isEmpty()) { @@ -1081,13 +1071,13 @@ public class TestPlanService { for (Map.Entry entry : apiTestCaseIdMap.entrySet()) { String apiCaseID = entry.getKey(); - executeApiCaseIdMap.put(apiCaseID,TestPlanApiExecuteStatus.RUNNING.name()); + executeApiCaseIdMap.put(apiCaseID, TestPlanApiExecuteStatus.RUNNING.name()); } for (String id : planScenarioIdMap.keySet()) { - executeScenarioCaseIdMap.put(id,TestPlanApiExecuteStatus.RUNNING.name()); + executeScenarioCaseIdMap.put(id, TestPlanApiExecuteStatus.RUNNING.name()); } - testPlanLog.info("ReportId["+planReportId+"] start run. TestPlanID:["+testPlanID+"]. Execute api :"+JSONObject.toJSONString(executeApiCaseIdMap)+"; Execute scenario:"+JSONObject.toJSONString(executeScenarioCaseIdMap)+"; Execute performance:"+JSONObject.toJSONString(executePerformanceIdMap)); - testPlanReportService.updateExecuteApis(planReportId,executeApiCaseIdMap,executeScenarioCaseIdMap,executePerformanceIdMap); + testPlanLog.info("ReportId[" + planReportId + "] start run. TestPlanID:[" + testPlanID + "]. Execute api :" + JSONObject.toJSONString(executeApiCaseIdMap) + "; Execute scenario:" + JSONObject.toJSONString(executeScenarioCaseIdMap) + "; Execute performance:" + JSONObject.toJSONString(executePerformanceIdMap)); + testPlanReportService.updateExecuteApis(planReportId, executeApiCaseIdMap, executeScenarioCaseIdMap, executePerformanceIdMap); //执行接口案例任务 @@ -1101,7 +1091,7 @@ public class TestPlanService { } else { apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name()); } - executeApiCaseIdMap.put(apiCaseID,TestPlanApiExecuteStatus.RUNNING.name()); + executeApiCaseIdMap.put(apiCaseID, TestPlanApiExecuteStatus.RUNNING.name()); } //执行场景执行任务