From d5e7d577608758ab965a051610b0000809c47e64 Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Thu, 28 Jan 2021 16:38:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E5=BC=80=E5=90=AF=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=EF=BC=8C?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=94=A8=E4=BE=8B=E7=9A=84=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=B2=A1=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E3=80=81=E6=8A=A5=E5=91=8A=E7=82=B9=E4=B8=8D=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复测试计划开启定时任务,接口用例的执行时间没自动更新、报告点不开的问题 --- .../ApiDefinitionExecResultService.java | 9 +- .../job/sechedule/TestPlanTestJob.java | 116 +++--------------- .../track/service/TestPlanReportService.java | 21 +++- .../track/service/TestPlanService.java | 108 +++++++++++++++- 4 files changed, 143 insertions(+), 111 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java index fa7fbebfde..adb1757e92 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java @@ -88,6 +88,12 @@ public class ApiDefinitionExecResultService { * @param type */ public void saveApiResultByScheduleTask(TestResult result, String type) { + String saveResultType = type; + if(StringUtils.equalsAny(ApiRunMode.SCHEDULE_API_PLAN.name(),saveResultType)){ + saveResultType = ApiRunMode.API_PLAN.name(); + } + + String finalSaveResultType = saveResultType; result.getScenarios().get(0).getRequestResults().forEach(item -> { ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult(); saveResult.setId(UUID.randomUUID().toString()); @@ -98,7 +104,7 @@ public class ApiDefinitionExecResultService { saveResult.setStartTime(item.getStartTime()); String status = item.isSuccess() ? "success" : "error"; saveResult.setEndTime(item.getResponseResult().getResponseTime()); - saveResult.setType(type); + saveResult.setType(finalSaveResultType); saveResult.setStatus(status); String userID = null; @@ -107,6 +113,7 @@ public class ApiDefinitionExecResultService { String scheduleCreateUser = testPlanService.findScheduleCreateUserById(apiCase.getTestPlanId()); userID = scheduleCreateUser; apiCase.setStatus(status); + apiCase.setUpdateTime(System.currentTimeMillis()); testPlanApiCaseService.updateByPrimaryKeySelective(apiCase); } else { userID = Objects.requireNonNull(SessionUtils.getUser()).getId(); diff --git a/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java b/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java index aad6505a13..facd021288 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java @@ -27,25 +27,23 @@ import java.util.*; */ public class TestPlanTestJob extends MsScheduleJob { private String projectID; - private Map planScenarioIdMap; - private Map apiTestCaseIdMap; - private Map performanceIdMap; - private PerformanceTestService performanceTestService; - private TestPlanScenarioCaseService testPlanScenarioCaseService; - private TestPlanApiCaseService testPlanApiCaseService; - private ApiTestCaseService apiTestCaseService; - private TestPlanReportService testPlanReportService; - private TestPlanLoadCaseService testPlanLoadCaseService; + +// private PerformanceTestService performanceTestService; +// private TestPlanScenarioCaseService testPlanScenarioCaseService; +// private TestPlanApiCaseService testPlanApiCaseService; +// private ApiTestCaseService apiTestCaseService; +// private TestPlanReportService testPlanReportService; +// private TestPlanLoadCaseService testPlanLoadCaseService; private TestPlanService testPlanService; public TestPlanTestJob() { - this.performanceTestService = CommonBeanFactory.getBean(PerformanceTestService.class); - this.testPlanScenarioCaseService = CommonBeanFactory.getBean(TestPlanScenarioCaseService.class); - this.testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class); - this.apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); - this.testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class); - this.testPlanLoadCaseService = CommonBeanFactory.getBean(TestPlanLoadCaseService.class); +// this.performanceTestService = CommonBeanFactory.getBean(PerformanceTestService.class); +// this.testPlanScenarioCaseService = CommonBeanFactory.getBean(TestPlanScenarioCaseService.class); +// this.testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class); +// this.apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); +// this.testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class); +// this.testPlanLoadCaseService = CommonBeanFactory.getBean(TestPlanLoadCaseService.class); this.testPlanService = CommonBeanFactory.getBean(TestPlanService.class); @@ -67,100 +65,14 @@ public class TestPlanTestJob extends MsScheduleJob { this.expression = jobDataMap.getString("expression"); this.projectID = jobDataMap.getString("projectId"); - planScenarioIdMap = new LinkedHashMap<>(); - apiTestCaseIdMap = new LinkedHashMap<>(); - performanceIdMap = new LinkedHashMap<>(); - List testPlanApiScenarioList = testPlanScenarioCaseService.getCasesByPlanId(this.resourceId); - for (TestPlanApiScenario model :testPlanApiScenarioList) { - planScenarioIdMap.put(model.getApiScenarioId(),model.getId()); - } - List testPlanApiCaseList = testPlanApiCaseService.getCasesByPlanId(this.resourceId); - for (TestPlanApiCase model : - testPlanApiCaseList) { - apiTestCaseIdMap.put(model.getApiCaseId(),model.getId()); - } - - LoadCaseRequest loadCaseRequest = new LoadCaseRequest(); - loadCaseRequest.setTestPlanId(this.resourceId); - loadCaseRequest.setProjectId(this.projectID); - List testPlanLoadCaseDTOList = testPlanLoadCaseService.list(loadCaseRequest); - for (TestPlanLoadCaseDTO dto : testPlanLoadCaseDTOList) { - performanceIdMap.put(dto.getId(),dto.getLoadCaseId()); - } businessExecute(context); } @Override void businessExecute(JobExecutionContext context) { - LogUtil.info("-------------- start testplan schedule ----------"); - //首先创建testPlanReport,然后返回的ID重新赋值为resourceID,作为后续的参数 - TestPlanReport testPlanReport = testPlanReportService.genTestPlanReport(this.resourceId,this.userId,ReportTriggerMode.SCHEDULE.name()); - //执行接口案例任务 - for (Map.Entry entry: this.apiTestCaseIdMap.entrySet()) { - String apiCaseID = entry.getKey(); - String planCaseID = entry.getValue(); - ApiTestCaseWithBLOBs blobs = apiTestCaseService.get(apiCaseID); - //需要更新这里来保证PlanCase的状态能正常更改 - apiTestCaseService.run(blobs,UUID.randomUUID().toString(),testPlanReport.getId(),this.resourceId,ApiRunMode.SCHEDULE_API_PLAN.name()); - } - - //执行场景执行任务 - if(!planScenarioIdMap.isEmpty()){ - LogUtil.info("-------------- testplan schedule ---------- api case over -----------------"); - SchedulePlanScenarioExecuteRequest scenarioRequest = new SchedulePlanScenarioExecuteRequest(); - String senarionReportID = UUID.randomUUID().toString(); - scenarioRequest.setId(senarionReportID); - scenarioRequest.setReportId(senarionReportID); - scenarioRequest.setProjectId(projectID); - scenarioRequest.setTriggerMode(ReportTriggerMode.SCHEDULE.name()); - scenarioRequest.setExecuteType(ExecuteType.Saved.name()); - Map> testPlanScenarioIdMap = new HashMap<>(); - testPlanScenarioIdMap.put(resourceId, this.planScenarioIdMap); - scenarioRequest.setTestPlanScenarioIDMap(testPlanScenarioIdMap); - scenarioRequest.setReportUserID(this.userId); - scenarioRequest.setTestPlanID(this.resourceId); - scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name()); - scenarioRequest.setTestPlanReportId(testPlanReport.getId()); - testPlanService.runScenarioCase(scenarioRequest); - LogUtil.info("-------------- testplan schedule ---------- scenario case over -----------------"); - } - //执行性能测试任务 - List performaneReportIDList = new ArrayList<>(); - for (Map.Entry entry: this.performanceIdMap.entrySet()) { - String id = entry.getKey(); - String caseID = entry.getValue(); - RunTestPlanRequest performanceRequest = new RunTestPlanRequest(); - performanceRequest.setId(caseID); - performanceRequest.setTestPlanLoadId(caseID); - performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_SCHEDULE.name()); - String reportId = null; - try { - reportId = performanceTestService.run(performanceRequest); - if(reportId!=null){ - performaneReportIDList.add(reportId); - - TestPlanLoadCase testPlanLoadCase = new TestPlanLoadCase(); - testPlanLoadCase.setId(performanceRequest.getTestPlanLoadId()); - testPlanLoadCase.setLoadReportId(reportId); - testPlanLoadCaseService.update(testPlanLoadCase); - } - }catch (Exception e){ - e.printStackTrace(); - } - //更新关联处的报告 - TestPlanLoadCase loadCase = new TestPlanLoadCaseDTO(); - loadCase.setId(id); - loadCase.setLoadReportId(reportId); - testPlanLoadCaseService.update(loadCase); - } - - if(!performaneReportIDList.isEmpty()){ - //性能测试时保存性能测试报告ID,在结果返回时用于捕捉并进行 - testPlanReportService.updatePerformanceInfo(testPlanReport,performaneReportIDList,ReportTriggerMode.SCHEDULE.name()); - } - + testPlanService.run(this.resourceId,this.projectID,this.userId,ReportTriggerMode.SCHEDULE.name()); } public static JobKey getJobKey(String testId) { 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 1e1e9ccbcc..c688c2a4b3 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -362,8 +362,12 @@ public class TestPlanReportService { testPlan.setStatus(TestPlanStatus.Completed.name()); testPlanMapper.updateByPrimaryKeySelective(testPlan); } - //发送通知 - sendMessage(report); + + if(StringUtils.equalsAny(report.getTriggerMode(),ReportTriggerMode.SCHEDULE.name())){ + //发送通知 + sendMessage(report); + } + } catch (Exception e) { } @@ -401,11 +405,20 @@ public class TestPlanReportService { paramMap.put("type", "testPlan"); paramMap.put("url", url); paramMap.put("status", testPlanReport.getStatus()); + + String successfulMailTemplate = ""; + String errfoMailTemplate = ""; + + if(StringUtils.equals(testPlanReport.getTriggerMode(),ReportTriggerMode.SCHEDULE.name())){ + successfulMailTemplate = "TestPlanSuccessfulNotification"; + errfoMailTemplate = "TestPlanFailedNotification"; + } + NoticeModel noticeModel = NoticeModel.builder() .successContext(successContext) - .successMailTemplate("TestPlanSuccessfulNotification") + .successMailTemplate(successfulMailTemplate) .failedContext(failedContext) - .failedMailTemplate("TestPlanFailedNotification") + .failedMailTemplate(errfoMailTemplate) .testId(testPlan.getId()) .status(testPlanReport.getStatus()) .event(event) 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 f245d46f98..c6651c55f2 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -7,16 +7,14 @@ import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import io.metersphere.api.dto.automation.ApiScenarioDTO; -import io.metersphere.api.dto.automation.ScenarioStatus; -import io.metersphere.api.dto.automation.SchedulePlanScenarioExecuteRequest; -import io.metersphere.api.dto.automation.TestPlanScenarioRequest; +import io.metersphere.api.dto.automation.*; import io.metersphere.api.dto.definition.ApiTestCaseRequest; import io.metersphere.api.dto.definition.TestPlanApiCaseDTO; import io.metersphere.api.dto.definition.request.*; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.service.ApiAutomationService; +import io.metersphere.api.service.ApiTestCaseService; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.ext.*; @@ -28,6 +26,7 @@ import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.i18n.Translator; import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.service.NoticeSendService; +import io.metersphere.performance.service.PerformanceTestService; import io.metersphere.service.ScheduleService; import io.metersphere.service.SystemParameterService; import io.metersphere.track.Factory.ReportComponentFactory; @@ -37,6 +36,7 @@ import io.metersphere.track.request.testcase.PlanCaseRelevanceRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; import io.metersphere.track.request.testplan.AddTestPlanRequest; import io.metersphere.track.request.testplan.LoadCaseRequest; +import io.metersphere.track.request.testplan.RunTestPlanRequest; import io.metersphere.track.request.testplancase.QueryTestPlanCaseRequest; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.annotations.Param; @@ -109,6 +109,10 @@ public class TestPlanService { private ExtTestPlanLoadCaseMapper extTestPlanLoadCaseMapper; @Resource private ExtTestPlanScenarioCaseMapper extTestPlanScenarioCaseMapper; + @Resource + private ApiTestCaseService apiTestCaseService; + @Resource + private PerformanceTestService performanceTestService; public synchronized void addTestPlan(AddTestPlanRequest testPlan) { if (getTestPlanByName(testPlan.getName()).size() > 0) { @@ -858,4 +862,100 @@ public class TestPlanService { jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode); return request.getId(); } + + public void run(String testPlanID,String projectID,String userId,String triggerMode){ + Map planScenarioIdMap; + Map apiTestCaseIdMap; + Map performanceIdMap; + + planScenarioIdMap = new LinkedHashMap<>(); + apiTestCaseIdMap = new LinkedHashMap<>(); + performanceIdMap = new LinkedHashMap<>(); + + List testPlanApiScenarioList = testPlanScenarioCaseService.getCasesByPlanId(testPlanID); + for (TestPlanApiScenario model :testPlanApiScenarioList) { + planScenarioIdMap.put(model.getApiScenarioId(),model.getId()); + } + List testPlanApiCaseList = testPlanApiCaseService.getCasesByPlanId(testPlanID); + for (TestPlanApiCase model : + testPlanApiCaseList) { + apiTestCaseIdMap.put(model.getApiCaseId(),model.getId()); + } + + LoadCaseRequest loadCaseRequest = new LoadCaseRequest(); + loadCaseRequest.setTestPlanId(testPlanID); + loadCaseRequest.setProjectId(projectID); + List testPlanLoadCaseDTOList = testPlanLoadCaseService.list(loadCaseRequest); + for (TestPlanLoadCaseDTO dto : testPlanLoadCaseDTOList) { + performanceIdMap.put(dto.getId(),dto.getLoadCaseId()); + } + + LogUtil.info("-------------- start testplan schedule ----------"); + TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class); + //首先创建testPlanReport,然后返回的ID重新赋值为resourceID,作为后续的参数 + TestPlanReport testPlanReport = testPlanReportService.genTestPlanReport(testPlanID,userId,triggerMode); + //执行接口案例任务 + for (Map.Entry entry: apiTestCaseIdMap.entrySet()) { + String apiCaseID = entry.getKey(); + String planCaseID = entry.getValue(); + ApiTestCaseWithBLOBs blobs = apiTestCaseService.get(apiCaseID); + //需要更新这里来保证PlanCase的状态能正常更改 + apiTestCaseService.run(blobs,UUID.randomUUID().toString(),testPlanReport.getId(),testPlanID,ApiRunMode.SCHEDULE_API_PLAN.name()); + } + + //执行场景执行任务 + if(!planScenarioIdMap.isEmpty()){ + LogUtil.info("-------------- testplan schedule ---------- api case over -----------------"); + SchedulePlanScenarioExecuteRequest scenarioRequest = new SchedulePlanScenarioExecuteRequest(); + String senarionReportID = UUID.randomUUID().toString(); + scenarioRequest.setId(senarionReportID); + scenarioRequest.setReportId(senarionReportID); + scenarioRequest.setProjectId(projectID); + scenarioRequest.setTriggerMode(ReportTriggerMode.SCHEDULE.name()); + scenarioRequest.setExecuteType(ExecuteType.Saved.name()); + Map> testPlanScenarioIdMap = new HashMap<>(); + testPlanScenarioIdMap.put(testPlanID, planScenarioIdMap); + scenarioRequest.setTestPlanScenarioIDMap(testPlanScenarioIdMap); + scenarioRequest.setReportUserID(userId); + scenarioRequest.setTestPlanID(testPlanID); + scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name()); + scenarioRequest.setTestPlanReportId(testPlanReport.getId()); + this.runScenarioCase(scenarioRequest); + LogUtil.info("-------------- testplan schedule ---------- scenario case over -----------------"); + } + //执行性能测试任务 + List performaneReportIDList = new ArrayList<>(); + for (Map.Entry entry: performanceIdMap.entrySet()) { + String id = entry.getKey(); + String caseID = entry.getValue(); + RunTestPlanRequest performanceRequest = new RunTestPlanRequest(); + performanceRequest.setId(caseID); + performanceRequest.setTestPlanLoadId(caseID); + performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_SCHEDULE.name()); + String reportId = null; + try { + reportId = performanceTestService.run(performanceRequest); + if(reportId!=null){ + performaneReportIDList.add(reportId); + + TestPlanLoadCase testPlanLoadCase = new TestPlanLoadCase(); + testPlanLoadCase.setId(performanceRequest.getTestPlanLoadId()); + testPlanLoadCase.setLoadReportId(reportId); + testPlanLoadCaseService.update(testPlanLoadCase); + } + }catch (Exception e){ + e.printStackTrace(); + } + //更新关联处的报告 + TestPlanLoadCase loadCase = new TestPlanLoadCaseDTO(); + loadCase.setId(id); + loadCase.setLoadReportId(reportId); + testPlanLoadCaseService.update(loadCase); + } + + if(!performaneReportIDList.isEmpty()){ + //性能测试时保存性能测试报告ID,在结果返回时用于捕捉并进行 + testPlanReportService.updatePerformanceInfo(testPlanReport,performaneReportIDList,ReportTriggerMode.SCHEDULE.name()); + } + } }