diff --git a/backend/src/main/java/io/metersphere/api/controller/APITestController.java b/backend/src/main/java/io/metersphere/api/controller/APITestController.java index a522ce36a9..3006f0af17 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -302,7 +302,7 @@ public class APITestController { if(allCount!=0){ float coverageRageNumber =(float)apiCountResult.getSuccessCount()*100/allCount; DecimalFormat df = new DecimalFormat("0.0"); - apiCountResult.setCoverageRage(df.format(coverageRageNumber)+"%"); + apiCountResult.setSuccessRage(df.format(coverageRageNumber)+"%"); } return apiCountResult; @@ -354,6 +354,6 @@ public class APITestController { public void updateScheduleEnableByPrimyKey(@RequestBody ScheduleInfoRequest request) { Schedule schedule = scheduleService.getSchedule(request.getTaskID()); schedule.setEnable(request.isEnable()); - apiTestService.updateSchedule(schedule); + apiAutomationService.updateSchedule(schedule); } } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index 53e57b6a1f..237de40e9f 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -1,6 +1,7 @@ package io.metersphere.api.jmeter; import io.metersphere.api.service.*; +import io.metersphere.base.domain.ApiScenarioReport; import io.metersphere.base.domain.ApiTestReport; import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.ApiRunMode; @@ -20,6 +21,7 @@ import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient; import org.apache.jmeter.visualizers.backend.BackendListenerContext; +import org.python.antlr.ast.Str; import org.springframework.http.HttpMethod; import java.io.ByteArrayOutputStream; @@ -50,6 +52,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl private ApiScenarioReportService apiScenarioReportService; + public String runMode = ApiRunMode.RUN.name(); // 测试ID @@ -155,6 +158,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl testResult.getScenarios().addAll(scenarios.values()); testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); ApiTestReport report = null; + String reportUrl = null; // 这部分后续优化只留 DELIMIT 和 SCENARIO 两部分 if (StringUtils.equals(this.runMode, ApiRunMode.DEBUG.name())) { report = apiReportService.get(debugReportId); @@ -173,7 +177,31 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name())) { // 执行报告不需要存储,由用户确认后在存储 testResult.setTestId(testId); - apiScenarioReportService.complete(testResult, this.runMode); + ApiScenarioReport scenarioReport = apiScenarioReportService.complete(testResult, this.runMode); + + + report = new ApiTestReport(); + report.setStatus(scenarioReport.getStatus()); + report.setId(scenarioReport.getId()); + report.setTriggerMode(scenarioReport.getTriggerMode()); + report.setName(scenarioReport.getName()); + + + SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); + assert systemParameterService != null; + BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); + reportUrl = baseSystemConfigDTO.getUrl() + "/#/api/automation/report"; + + + String scenaName = scenarioReport.getName(); + if(scenaName==null){ + scenaName = ""; + }else { + scenaName = scenaName.split("-")[0]; + } + + String scenarioID = apiScenarioReportService.getApiScenarioId(scenaName,scenarioReport.getProjectId()); + testResult.setTestId(scenarioID); } else { apiTestService.changeStatus(testId, APITestStatus.Completed); report = apiReportService.getRunningReport(testResult.getTestId()); @@ -196,22 +224,24 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl } } try { - sendTask(report, testResult); + sendTask(report,reportUrl, testResult); } catch (Exception e) { LogUtil.error(e.getMessage(), e); } } - private static void sendTask(ApiTestReport report, TestResult testResult) { + private static void sendTask(ApiTestReport report,String reportUrl, TestResult testResult) { SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); NoticeSendService noticeSendService = CommonBeanFactory.getBean(NoticeSendService.class); assert systemParameterService != null; assert noticeSendService != null; BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); - String url = baseSystemConfigDTO.getUrl() + "/#/api/report/view/" + report.getId(); - + String url = reportUrl; + if(StringUtils.isEmpty(url)){ + url = baseSystemConfigDTO.getUrl() + "/#/api/report/view/" + report.getId(); + } String successContext = ""; String failedContext = ""; String subject = ""; 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 bfb5494e3b..1a85a2dd6c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -40,6 +40,7 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; +import org.python.antlr.ast.Str; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -160,7 +161,7 @@ public class ApiAutomationService { apiScenarioMapper.deleteByPrimaryKey(id); } - public void preDelete(String scenarioID){ + public void preDelete(String scenarioID) { scheduleService.deleteByResourceId(scenarioID); TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); @@ -174,37 +175,40 @@ public class ApiAutomationService { } example = new TestPlanApiScenarioExample(); - if(!idList.isEmpty()){ + if (!idList.isEmpty()) { example.createCriteria().andIdIn(idList); testPlanApiScenarioMapper.deleteByExample(example); } } - public void preDelete(List scenarioIDList){ + + public void preDelete(List scenarioIDList) { List testPlanApiScenarioIdList = new ArrayList<>(); List scheduleIdList = new ArrayList<>(); - for (String id :scenarioIDList) { + for (String id : scenarioIDList) { TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); example.createCriteria().andApiScenarioIdEqualTo(id); List testPlanApiScenarioList = testPlanApiScenarioMapper.selectByExample(example); - for (TestPlanApiScenario api :testPlanApiScenarioList) { - if(!testPlanApiScenarioIdList.contains(api.getId())){ + for (TestPlanApiScenario api : testPlanApiScenarioList) { + if (!testPlanApiScenarioIdList.contains(api.getId())) { testPlanApiScenarioIdList.add(api.getId()); } } scheduleService.deleteByResourceId(id); } - if(!testPlanApiScenarioIdList.isEmpty()){ + if (!testPlanApiScenarioIdList.isEmpty()) { TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); example.createCriteria().andIdIn(testPlanApiScenarioIdList); testPlanApiScenarioMapper.deleteByExample(example); } } + public void deleteBatch(List ids) { //及连删除外键表 - preDelete(ids);; + preDelete(ids); + ; ApiScenarioExample example = new ApiScenarioExample(); example.createCriteria().andIdIn(ids); apiScenarioMapper.deleteByExample(example); @@ -242,16 +246,16 @@ public class ApiAutomationService { return new ArrayList<>(); } - private void createAPIScenarioReportResult(String id, String triggerMode, String execType, String projectId,String userID) { + private void createAPIScenarioReportResult(String id, String triggerMode, String execType, String projectId, String userID) { APIScenarioReportResult report = new APIScenarioReportResult(); report.setId(id); report.setName("测试执行结果"); report.setCreateTime(System.currentTimeMillis()); report.setUpdateTime(System.currentTimeMillis()); report.setStatus(APITestStatus.Running.name()); - if(StringUtils.isNotEmpty(userID)){ + if (StringUtils.isNotEmpty(userID)) { report.setUserId(userID); - }else { + } else { report.setUserId(SessionUtils.getUserId()); } @@ -316,7 +320,7 @@ public class ApiAutomationService { jMeterService.runDefinition(request.getId(), jmeterTestPlanHashTree, request.getReportId(), runMode); createAPIScenarioReportResult(request.getId(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), - request.getExecuteType(), projectID,request.getReportUserID()); + request.getExecuteType(), projectID, request.getReportUserID()); return request.getId(); } @@ -367,8 +371,8 @@ public class ApiAutomationService { ExtTestPlanScenarioCaseMapper scenarioBatchMapper = sqlSession.getMapper(ExtTestPlanScenarioCaseMapper.class); ExtTestPlanApiCaseMapper apiCaseBatchMapper = sqlSession.getMapper(ExtTestPlanApiCaseMapper.class); - for (TestPlanDTO testPlan:list) { - if(request.getScenarioIds()!=null){ + for (TestPlanDTO testPlan : list) { + if (request.getScenarioIds() != null) { for (String scenarioId : request.getScenarioIds()) { TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); testPlanApiScenario.setId(UUID.randomUUID().toString()); @@ -379,7 +383,7 @@ public class ApiAutomationService { scenarioBatchMapper.insertIfNotExists(testPlanApiScenario); } } - if(request.getApiIds()!=null){ + if (request.getApiIds() != null) { for (String caseId : request.getApiIds()) { TestPlanApiCase testPlanApiCase = new TestPlanApiCase(); testPlanApiCase.setId(UUID.randomUUID().toString()); 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 0834cdb62a..d2f372a2b2 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -50,7 +50,18 @@ public class ApiScenarioReportService { @Resource private TestPlanApiScenarioMapper testPlanApiScenarioMapper; - public void complete(TestResult result, String runMode) { + public String getApiScenarioId(String name, String projectID) { + ApiScenarioExample example = new ApiScenarioExample(); + example.createCriteria().andNameEqualTo(name).andProjectIdEqualTo(projectID).andStatusNotEqualTo("Trash"); + List list = apiScenarioMapper.selectByExample(example); + if (list.isEmpty()) { + return null; + } else { + return list.get(0).getId(); + } + } + + public ApiScenarioReport complete(TestResult result, String runMode) { Object obj = cache.get(result.getTestId()); if (obj == null) { MSException.throwException(Translator.get("api_report_is_null")); @@ -83,6 +94,7 @@ public class ApiScenarioReportService { if (!report.getTriggerMode().equals(ReportTriggerMode.SCHEDULE.name())) { cache.put(report.getId(), report); } + return report; } /** diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml index 0914b1e050..254b8576b7 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml @@ -53,7 +53,7 @@ SELECT scene.`name` AS caseName,testPlan.`name` AS testPlan,count(report.id) AS failureTimes,'scenario' AS caseType FROM api_scenario_report report INNER JOIN api_scenario_report_detail reportDetail ON report.id = reportDetail.report_id - INNER JOIN api_scenario scene ON reportDetail.content like concat('%"',scene.`name`,'"%') + INNER JOIN api_scenario scene ON reportDetail.content like concat('%"', scene.`name`,'"%') LEFT JOIN test_plan_api_scenario apiScene ON apiScene.api_scenario_id = scene.id LEFT JOIN test_plan testPlan ON testPlan.id = apiScene.test_plan_id WHERE report.project_id = #{projectId} diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml index 2b48a30ab8..a0b5b0c614 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml @@ -179,8 +179,8 @@ SELECT count(acr.report_id) AS countNumber FROM api_scenario_report_detail acr INNER JOIN api_scenario_report ar ON ar.id = acr.report_id INNER JOIN ( - SELECT acitem.id FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id - ) ac on acr.content like CONCAT('%', ac.id,'%') + SELECT acitem.`name` FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id + ) ac on acr.content like CONCAT('%"', ac.`name`,'"%') WHERE acr.project_id = #{projectId} AND ar.create_time BETWEEN #{firstDayTimestamp} AND #{lastDayTimestamp} @@ -189,8 +189,8 @@ FROM api_scenario_report_detail acr INNER JOIN api_scenario_report ar ON ar.id = acr.report_id INNER JOIN ( - SELECT acitem.id FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id - ) ac on acr.content like CONCAT('%', ac.id,'%') + SELECT acitem.`name` FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_id + ) ac on acr.content like CONCAT('%"', ac.`name`,'"%') WHERE acr.project_id = #{projectId} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml index 724925a6de..536eb80268 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml @@ -160,7 +160,6 @@ WHERE sch.resource_id IN ( SELECT id FROM api_test WHERE project_id = #{projectId,jdbcType=VARCHAR} ) - AND `group` = #{group}