From 6f8ac5fb46175d34aa4c455b6e22b10d4c0edb3f Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Thu, 21 Jan 2021 13:36:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=BB=93=E6=9D=9F=E6=97=B6=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复定时任务结束时通知不正确的问题 --- .../constants/TestPlanReportStatus.java | 5 ++ .../track/service/TestPlanReportService.java | 72 ++++++++++++++++--- .../mail/TestPlanFailedNotification.html | 21 ++++++ .../mail/TestPlanSuccessfulNotification.html | 18 +++++ 4 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/commons/constants/TestPlanReportStatus.java create mode 100644 backend/src/main/resources/mail/TestPlanFailedNotification.html create mode 100644 backend/src/main/resources/mail/TestPlanSuccessfulNotification.html diff --git a/backend/src/main/java/io/metersphere/commons/constants/TestPlanReportStatus.java b/backend/src/main/java/io/metersphere/commons/constants/TestPlanReportStatus.java new file mode 100644 index 0000000000..ee327fbfdf --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/constants/TestPlanReportStatus.java @@ -0,0 +1,5 @@ +package io.metersphere.commons.constants; + +public enum TestPlanReportStatus { + RUNNING, COMPLETED, SUCCESS, FAILED +} 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 4ff490f8fe..7830c06e29 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -29,6 +29,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.lang.reflect.Array; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -78,6 +79,13 @@ public class TestPlanReportService { return returnList; } + /** + * 生成测试计划 + * @param planId + * @param userId + * @param triggerMode + * @return + */ public TestPlanReport genTestPlanReport(String planId, String userId,String triggerMode) { TestPlan testPlan = testPlanMapper.selectByPrimaryKey(planId); @@ -101,6 +109,7 @@ public class TestPlanReportService { TestPlanReport testPlanReport = new TestPlanReport(); testPlanReport.setTestPlanId(planId); testPlanReport.setId(testPlanReportID); + testPlanReport.setStatus(APITestStatus.Starting.name()); testPlanReport.setCreateTime(System.currentTimeMillis()); testPlanReport.setUpdateTime(System.currentTimeMillis()); try { @@ -244,13 +253,13 @@ public class TestPlanReportService { component.afterBuild(testCaseReportMetricDTO); }); - this.update(testPlanReport); - TestPlanReportDataExample example = new TestPlanReportDataExample(); example.createCriteria().andTestPlanReportIdEqualTo(planReportId); List testPlanReportDataList = testPlanReportDataMapper.selectByExampleWithBLOBs(example); + + TestPlanReportDataWithBLOBs testPlanReportData = null; if (!testPlanReportDataList.isEmpty()) { - TestPlanReportDataWithBLOBs testPlanReportData = testPlanReportDataList.get(0); + testPlanReportData = testPlanReportDataList.get(0); testPlanReportData.setExecuteResult(JSONObject.toJSONString(testCaseReportMetricDTO.getExecuteResult())); testPlanReportData.setFailurTestCases(JSONObject.toJSONString(testCaseReportMetricDTO.getFailureTestCases())); testPlanReportData.setModuleExecuteResult(JSONArray.toJSONString(testCaseReportMetricDTO.getModuleExecuteResult())); @@ -260,8 +269,49 @@ public class TestPlanReportService { testPlanReportDataMapper.updateByPrimaryKeyWithBLOBs(testPlanReportData); } + String testPlanStatus = this.getTestPlanReportStatus(testPlanReport,testPlanReportData); + testPlanReport.setStatus(testPlanStatus); + this.update(testPlanReport); } + /** + * 计算测试计划的状态 + * @param testPlanReport + * @return + */ + private String getTestPlanReportStatus(TestPlanReport testPlanReport, TestPlanReportDataWithBLOBs testPlanReportData) { + String status = TestPlanReportStatus.COMPLETED.name(); + if(testPlanReport!=null){ + if(testPlanReport.getIsApiCaseExecuting() || testPlanReport.getIsPerformanceExecuting() || testPlanReport.getIsScenarioExecuting()){ + status = TestPlanReportStatus.RUNNING.name(); + }else { + if(testPlanReportData == null){ + String failCaseString = testPlanReportData.getFailurTestCases(); + status = TestPlanReportStatus.SUCCESS.name(); + try { + JSONObject failurCaseObject = JSONObject.parseObject(failCaseString); + if(failurCaseObject.containsKey("apiTestCases")&&failurCaseObject.getJSONArray("apiTestCases").size()>=0){ + status = TestPlanReportStatus.FAILED.name(); + return status; + } + if(failurCaseObject.containsKey("loadTestCases")&&failurCaseObject.getJSONArray("loadTestCases").size()>=0){ + status = TestPlanReportStatus.FAILED.name(); + return status; + } + if(failurCaseObject.containsKey("scenarioTestCases")&&failurCaseObject.getJSONArray("scenarioTestCases").size()>=0){ + status = TestPlanReportStatus.FAILED.name(); + return status; + } + }catch (Exception e){ + status = TestPlanReportStatus.FAILED.name(); + } + }else { + status = TestPlanReportStatus.COMPLETED.name(); + } + } + } + return status; + } public void update(TestPlanReport report) { if (!report.getIsApiCaseExecuting() && !report.getIsPerformanceExecuting() && !report.getIsScenarioExecuting()) { @@ -295,26 +345,26 @@ public class TestPlanReportService { String subject = ""; String event = ""; - successContext = "接口测试定时任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; - failedContext = "接口测试定时任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; + successContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; + failedContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; subject = Translator.get("task_notification"); - if (StringUtils.equals("Success", testPlanReport.getStatus())) { - event = NoticeConstants.Event.EXECUTE_SUCCESSFUL; - } else { + if (StringUtils.equals(TestPlanReportStatus.FAILED.name(), testPlanReport.getStatus())) { event = NoticeConstants.Event.EXECUTE_FAILED; + } else { + event = NoticeConstants.Event.EXECUTE_SUCCESSFUL; } Map paramMap = new HashMap<>(); paramMap.put("testName", testPlan.getName()); paramMap.put("id", testPlanReport.getId()); - paramMap.put("type", "api"); + paramMap.put("type", "testPlan"); paramMap.put("url", url); paramMap.put("status", testPlanReport.getStatus()); NoticeModel noticeModel = NoticeModel.builder() .successContext(successContext) - .successMailTemplate("ApiSuccessfulNotification") + .successMailTemplate("TestPlanSuccessfulNotification") .failedContext(failedContext) - .failedMailTemplate("ApiFailedNotification") + .failedMailTemplate("TestPlanFailedNotification") .testId(testPlan.getId()) .status(testPlanReport.getStatus()) .event(event) diff --git a/backend/src/main/resources/mail/TestPlanFailedNotification.html b/backend/src/main/resources/mail/TestPlanFailedNotification.html new file mode 100644 index 0000000000..f6c3ddb8e7 --- /dev/null +++ b/backend/src/main/resources/mail/TestPlanFailedNotification.html @@ -0,0 +1,21 @@ + + + + + MeterSphere + + +
+
+

尊敬的用户:

+

您好: +

+
+

您所执行的 ${testName} 测试计划运行失败
+ 请点击下面链接进入测试报告页面

+ ${url}/#/${type}/report/view/${id} +
+ +
+ + \ No newline at end of file diff --git a/backend/src/main/resources/mail/TestPlanSuccessfulNotification.html b/backend/src/main/resources/mail/TestPlanSuccessfulNotification.html new file mode 100644 index 0000000000..738087eb37 --- /dev/null +++ b/backend/src/main/resources/mail/TestPlanSuccessfulNotification.html @@ -0,0 +1,18 @@ + + + + + MeterSphere + + +
+

尊敬的用户:

+

您好: +

+
+

您所执行的 ${testName} 测试计划运行成功
+ 请点击下面链接进入测试报告页面

+ ${url}/#/${type}/report/view/${id} +
+ + \ No newline at end of file