Merge remote-tracking branch 'origin/master'

This commit is contained in:
Captain.B 2020-12-24 10:24:57 +08:00
commit 74abe9ece5
7 changed files with 74 additions and 29 deletions

View File

@ -302,7 +302,7 @@ public class APITestController {
if(allCount!=0){ if(allCount!=0){
float coverageRageNumber =(float)apiCountResult.getSuccessCount()*100/allCount; float coverageRageNumber =(float)apiCountResult.getSuccessCount()*100/allCount;
DecimalFormat df = new DecimalFormat("0.0"); DecimalFormat df = new DecimalFormat("0.0");
apiCountResult.setCoverageRage(df.format(coverageRageNumber)+"%"); apiCountResult.setSuccessRage(df.format(coverageRageNumber)+"%");
} }
return apiCountResult; return apiCountResult;
@ -354,6 +354,6 @@ public class APITestController {
public void updateScheduleEnableByPrimyKey(@RequestBody ScheduleInfoRequest request) { public void updateScheduleEnableByPrimyKey(@RequestBody ScheduleInfoRequest request) {
Schedule schedule = scheduleService.getSchedule(request.getTaskID()); Schedule schedule = scheduleService.getSchedule(request.getTaskID());
schedule.setEnable(request.isEnable()); schedule.setEnable(request.isEnable());
apiTestService.updateSchedule(schedule); apiAutomationService.updateSchedule(schedule);
} }
} }

View File

@ -1,6 +1,7 @@
package io.metersphere.api.jmeter; package io.metersphere.api.jmeter;
import io.metersphere.api.service.*; import io.metersphere.api.service.*;
import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.base.domain.ApiTestReport; import io.metersphere.base.domain.ApiTestReport;
import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ApiRunMode; 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.samplers.SampleResult;
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient; import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
import org.apache.jmeter.visualizers.backend.BackendListenerContext; import org.apache.jmeter.visualizers.backend.BackendListenerContext;
import org.python.antlr.ast.Str;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -50,6 +52,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
private ApiScenarioReportService apiScenarioReportService; private ApiScenarioReportService apiScenarioReportService;
public String runMode = ApiRunMode.RUN.name(); public String runMode = ApiRunMode.RUN.name();
// 测试ID // 测试ID
@ -155,6 +158,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
testResult.getScenarios().addAll(scenarios.values()); testResult.getScenarios().addAll(scenarios.values());
testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId));
ApiTestReport report = null; ApiTestReport report = null;
String reportUrl = null;
// 这部分后续优化只留 DELIMIT SCENARIO 两部分 // 这部分后续优化只留 DELIMIT SCENARIO 两部分
if (StringUtils.equals(this.runMode, ApiRunMode.DEBUG.name())) { if (StringUtils.equals(this.runMode, ApiRunMode.DEBUG.name())) {
report = apiReportService.get(debugReportId); 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())) { } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name())) {
// 执行报告不需要存储由用户确认后在存储 // 执行报告不需要存储由用户确认后在存储
testResult.setTestId(testId); 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 { } else {
apiTestService.changeStatus(testId, APITestStatus.Completed); apiTestService.changeStatus(testId, APITestStatus.Completed);
report = apiReportService.getRunningReport(testResult.getTestId()); report = apiReportService.getRunningReport(testResult.getTestId());
@ -196,22 +224,24 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
} }
} }
try { try {
sendTask(report, testResult); sendTask(report,reportUrl, testResult);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage(), 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); SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
NoticeSendService noticeSendService = CommonBeanFactory.getBean(NoticeSendService.class); NoticeSendService noticeSendService = CommonBeanFactory.getBean(NoticeSendService.class);
assert systemParameterService != null; assert systemParameterService != null;
assert noticeSendService != null; assert noticeSendService != null;
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); 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 successContext = "";
String failedContext = ""; String failedContext = "";
String subject = ""; String subject = "";

View File

@ -40,6 +40,7 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import org.python.antlr.ast.Str;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -160,7 +161,7 @@ public class ApiAutomationService {
apiScenarioMapper.deleteByPrimaryKey(id); apiScenarioMapper.deleteByPrimaryKey(id);
} }
public void preDelete(String scenarioID){ public void preDelete(String scenarioID) {
scheduleService.deleteByResourceId(scenarioID); scheduleService.deleteByResourceId(scenarioID);
TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); TestPlanApiScenarioExample example = new TestPlanApiScenarioExample();
@ -174,37 +175,40 @@ public class ApiAutomationService {
} }
example = new TestPlanApiScenarioExample(); example = new TestPlanApiScenarioExample();
if(!idList.isEmpty()){ if (!idList.isEmpty()) {
example.createCriteria().andIdIn(idList); example.createCriteria().andIdIn(idList);
testPlanApiScenarioMapper.deleteByExample(example); testPlanApiScenarioMapper.deleteByExample(example);
} }
} }
public void preDelete(List<String> scenarioIDList){
public void preDelete(List<String> scenarioIDList) {
List<String> testPlanApiScenarioIdList = new ArrayList<>(); List<String> testPlanApiScenarioIdList = new ArrayList<>();
List<String> scheduleIdList = new ArrayList<>(); List<String> scheduleIdList = new ArrayList<>();
for (String id :scenarioIDList) { for (String id : scenarioIDList) {
TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); TestPlanApiScenarioExample example = new TestPlanApiScenarioExample();
example.createCriteria().andApiScenarioIdEqualTo(id); example.createCriteria().andApiScenarioIdEqualTo(id);
List<TestPlanApiScenario> testPlanApiScenarioList = testPlanApiScenarioMapper.selectByExample(example); List<TestPlanApiScenario> testPlanApiScenarioList = testPlanApiScenarioMapper.selectByExample(example);
for (TestPlanApiScenario api :testPlanApiScenarioList) { for (TestPlanApiScenario api : testPlanApiScenarioList) {
if(!testPlanApiScenarioIdList.contains(api.getId())){ if (!testPlanApiScenarioIdList.contains(api.getId())) {
testPlanApiScenarioIdList.add(api.getId()); testPlanApiScenarioIdList.add(api.getId());
} }
} }
scheduleService.deleteByResourceId(id); scheduleService.deleteByResourceId(id);
} }
if(!testPlanApiScenarioIdList.isEmpty()){ if (!testPlanApiScenarioIdList.isEmpty()) {
TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); TestPlanApiScenarioExample example = new TestPlanApiScenarioExample();
example.createCriteria().andIdIn(testPlanApiScenarioIdList); example.createCriteria().andIdIn(testPlanApiScenarioIdList);
testPlanApiScenarioMapper.deleteByExample(example); testPlanApiScenarioMapper.deleteByExample(example);
} }
} }
public void deleteBatch(List<String> ids) { public void deleteBatch(List<String> ids) {
//及连删除外键表 //及连删除外键表
preDelete(ids);; preDelete(ids);
;
ApiScenarioExample example = new ApiScenarioExample(); ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdIn(ids); example.createCriteria().andIdIn(ids);
apiScenarioMapper.deleteByExample(example); apiScenarioMapper.deleteByExample(example);
@ -242,16 +246,16 @@ public class ApiAutomationService {
return new ArrayList<>(); 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(); APIScenarioReportResult report = new APIScenarioReportResult();
report.setId(id); report.setId(id);
report.setName("测试执行结果"); report.setName("测试执行结果");
report.setCreateTime(System.currentTimeMillis()); report.setCreateTime(System.currentTimeMillis());
report.setUpdateTime(System.currentTimeMillis()); report.setUpdateTime(System.currentTimeMillis());
report.setStatus(APITestStatus.Running.name()); report.setStatus(APITestStatus.Running.name());
if(StringUtils.isNotEmpty(userID)){ if (StringUtils.isNotEmpty(userID)) {
report.setUserId(userID); report.setUserId(userID);
}else { } else {
report.setUserId(SessionUtils.getUserId()); report.setUserId(SessionUtils.getUserId());
} }
@ -316,7 +320,7 @@ public class ApiAutomationService {
jMeterService.runDefinition(request.getId(), jmeterTestPlanHashTree, request.getReportId(), runMode); jMeterService.runDefinition(request.getId(), jmeterTestPlanHashTree, request.getReportId(), runMode);
createAPIScenarioReportResult(request.getId(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), createAPIScenarioReportResult(request.getId(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
request.getExecuteType(), projectID,request.getReportUserID()); request.getExecuteType(), projectID, request.getReportUserID());
return request.getId(); return request.getId();
} }
@ -367,8 +371,8 @@ public class ApiAutomationService {
ExtTestPlanScenarioCaseMapper scenarioBatchMapper = sqlSession.getMapper(ExtTestPlanScenarioCaseMapper.class); ExtTestPlanScenarioCaseMapper scenarioBatchMapper = sqlSession.getMapper(ExtTestPlanScenarioCaseMapper.class);
ExtTestPlanApiCaseMapper apiCaseBatchMapper = sqlSession.getMapper(ExtTestPlanApiCaseMapper.class); ExtTestPlanApiCaseMapper apiCaseBatchMapper = sqlSession.getMapper(ExtTestPlanApiCaseMapper.class);
for (TestPlanDTO testPlan:list) { for (TestPlanDTO testPlan : list) {
if(request.getScenarioIds()!=null){ if (request.getScenarioIds() != null) {
for (String scenarioId : request.getScenarioIds()) { for (String scenarioId : request.getScenarioIds()) {
TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario();
testPlanApiScenario.setId(UUID.randomUUID().toString()); testPlanApiScenario.setId(UUID.randomUUID().toString());
@ -379,7 +383,7 @@ public class ApiAutomationService {
scenarioBatchMapper.insertIfNotExists(testPlanApiScenario); scenarioBatchMapper.insertIfNotExists(testPlanApiScenario);
} }
} }
if(request.getApiIds()!=null){ if (request.getApiIds() != null) {
for (String caseId : request.getApiIds()) { for (String caseId : request.getApiIds()) {
TestPlanApiCase testPlanApiCase = new TestPlanApiCase(); TestPlanApiCase testPlanApiCase = new TestPlanApiCase();
testPlanApiCase.setId(UUID.randomUUID().toString()); testPlanApiCase.setId(UUID.randomUUID().toString());

View File

@ -50,7 +50,18 @@ public class ApiScenarioReportService {
@Resource @Resource
private TestPlanApiScenarioMapper testPlanApiScenarioMapper; 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<ApiScenario> 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()); Object obj = cache.get(result.getTestId());
if (obj == null) { if (obj == null) {
MSException.throwException(Translator.get("api_report_is_null")); MSException.throwException(Translator.get("api_report_is_null"));
@ -83,6 +94,7 @@ public class ApiScenarioReportService {
if (!report.getTriggerMode().equals(ReportTriggerMode.SCHEDULE.name())) { if (!report.getTriggerMode().equals(ReportTriggerMode.SCHEDULE.name())) {
cache.put(report.getId(), report); cache.put(report.getId(), report);
} }
return report;
} }
/** /**

View File

@ -53,7 +53,7 @@
SELECT scene.`name` AS caseName,testPlan.`name` AS testPlan,count(report.id) AS failureTimes,'scenario' AS caseType SELECT scene.`name` AS caseName,testPlan.`name` AS testPlan,count(report.id) AS failureTimes,'scenario' AS caseType
FROM api_scenario_report report FROM api_scenario_report report
INNER JOIN api_scenario_report_detail reportDetail ON report.id = reportDetail.report_id 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_api_scenario apiScene ON apiScene.api_scenario_id = scene.id
LEFT JOIN test_plan testPlan ON testPlan.id = apiScene.test_plan_id LEFT JOIN test_plan testPlan ON testPlan.id = apiScene.test_plan_id
WHERE report.project_id = #{projectId} WHERE report.project_id = #{projectId}

View File

@ -179,8 +179,8 @@
SELECT count(acr.report_id) AS countNumber FROM api_scenario_report_detail acr 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 api_scenario_report ar ON ar.id = acr.report_id
INNER JOIN ( INNER JOIN (
SELECT acitem.id FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_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.id,'%') ) ac on acr.content like CONCAT('%"', ac.`name`,'"%')
WHERE acr.project_id = #{projectId} AND ar.create_time BETWEEN #{firstDayTimestamp} AND #{lastDayTimestamp} WHERE acr.project_id = #{projectId} AND ar.create_time BETWEEN #{firstDayTimestamp} AND #{lastDayTimestamp}
</select> </select>
@ -189,8 +189,8 @@
FROM api_scenario_report_detail acr FROM api_scenario_report_detail acr
INNER JOIN api_scenario_report ar ON ar.id = acr.report_id INNER JOIN api_scenario_report ar ON ar.id = acr.report_id
INNER JOIN ( INNER JOIN (
SELECT acitem.id FROM api_scenario acitem INNER JOIN `schedule` sc ON acitem.id = sc.resource_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.id,'%') ) ac on acr.content like CONCAT('%"', ac.`name`,'"%')
WHERE acr.project_id = #{projectId} WHERE acr.project_id = #{projectId}
</select> </select>
</mapper> </mapper>

View File

@ -160,7 +160,6 @@
WHERE sch.resource_id IN ( WHERE sch.resource_id IN (
SELECT id FROM api_test WHERE project_id = #{projectId,jdbcType=VARCHAR} SELECT id FROM api_test WHERE project_id = #{projectId,jdbcType=VARCHAR}
) )
AND `group` = #{group}
</select> </select>
<select id="countByProjectIDAndCreateInThisWeek" resultType="java.lang.Long"> <select id="countByProjectIDAndCreateInThisWeek" resultType="java.lang.Long">