diff --git a/backend/src/main/java/io/metersphere/api/cache/TestPlanExecuteInfo.java b/backend/src/main/java/io/metersphere/api/cache/TestPlanExecuteInfo.java deleted file mode 100644 index 6946e887de..0000000000 --- a/backend/src/main/java/io/metersphere/api/cache/TestPlanExecuteInfo.java +++ /dev/null @@ -1,286 +0,0 @@ -package io.metersphere.api.cache; - - -import io.metersphere.api.exec.queue.ExecThreadPoolExecutor; -import io.metersphere.api.jmeter.JmeterThreadUtils; -import io.metersphere.base.domain.ApiScenarioReport; -import io.metersphere.base.domain.ApiScenarioReportExample; -import io.metersphere.base.mapper.ApiScenarioReportMapper; -import io.metersphere.commons.constants.TestPlanApiExecuteStatus; -import io.metersphere.commons.constants.TestPlanResourceType; -import io.metersphere.commons.utils.CommonBeanFactory; -import io.metersphere.track.dto.TestPlanReportExecuteCheckResultDTO; -import io.metersphere.utils.LoggerUtil; -import lombok.Getter; -import lombok.Setter; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * @author song.tianyang - * @Date 2021/8/21 5:15 下午 - */ -@Getter -@Setter -public class TestPlanExecuteInfo { - private String reportId; - private String creator; - private Map apiCaseExecInfo = new ConcurrentHashMap<>(); - private Map apiScenarioCaseExecInfo = new ConcurrentHashMap<>(); - private Map loadCaseExecInfo = new ConcurrentHashMap<>(); - - private Map apiCaseExecuteThreadMap = new ConcurrentHashMap<>(); - private Map apiScenarioThreadMap = new ConcurrentHashMap<>(); - private Map loadCaseReportIdMap = new ConcurrentHashMap<>(); - - private Map apiCaseReportMap = new ConcurrentHashMap<>(); - private Map apiScenarioReportMap = new ConcurrentHashMap<>(); - private boolean reportDataInDataBase; - - int lastUnFinishedNumCount = 0; - long lastFinishedNumCountTime = 0; - - private boolean isApiCaseAllExecuted; - private boolean isScenarioAllExecuted; - private boolean isLoadCaseAllExecuted; - - public TestPlanExecuteInfo(String reportId, String creator) { - this.reportId = reportId; - this.creator = creator; - } - - public synchronized void updateExecuteInfo(Map apiCaseExecInfo, Map apiScenarioCaseExecInfo, Map loadCaseExecInfo) { - if (MapUtils.isNotEmpty(apiCaseExecInfo)) { - this.apiCaseExecInfo.putAll(apiCaseExecInfo); - } - - if (MapUtils.isNotEmpty(apiScenarioCaseExecInfo)) { - this.apiScenarioCaseExecInfo.putAll(apiScenarioCaseExecInfo); - } - - if (MapUtils.isNotEmpty(loadCaseExecInfo)) { - this.loadCaseExecInfo.putAll(loadCaseExecInfo); - } - } - - public synchronized void updateThreadResult(Map apiCaseExecResultInfo, Map apiScenarioCaseExecResultInfo, Map loadCaseExecResultInfo) { - if (MapUtils.isNotEmpty(apiCaseExecResultInfo)) { - this.apiCaseExecuteThreadMap.putAll(apiCaseExecResultInfo); - } - - if (MapUtils.isNotEmpty(apiScenarioCaseExecResultInfo)) { - this.apiScenarioThreadMap.putAll(apiScenarioCaseExecResultInfo); - } - - if (MapUtils.isNotEmpty(loadCaseExecResultInfo)) { - this.loadCaseReportIdMap.putAll(loadCaseExecResultInfo); - } - } - - public synchronized TestPlanReportExecuteCheckResultDTO countUnFinishedNum() { - TestPlanReportExecuteCheckResultDTO executeCheck = new TestPlanReportExecuteCheckResultDTO(); - int unFinishedCount = 0; - - this.isApiCaseAllExecuted = true; - this.isScenarioAllExecuted = true; - this.isLoadCaseAllExecuted = true; - boolean isQueue = false; - for (String result : apiCaseExecInfo.values()) { - if (StringUtils.equalsIgnoreCase(result, TestPlanApiExecuteStatus.RUNNING.name())) { - unFinishedCount++; - if (this.isApiCaseAllExecuted) { - this.isApiCaseAllExecuted = false; - } - if (!isQueue) { - isQueue = CommonBeanFactory.getBean(ExecThreadPoolExecutor.class).check(apiCaseExecuteThreadMap.get(result)); - } - } - } - for (String result : apiScenarioCaseExecInfo.values()) { - if (StringUtils.equalsIgnoreCase(result, TestPlanApiExecuteStatus.RUNNING.name())) { - unFinishedCount++; - if (this.isScenarioAllExecuted) { - isScenarioAllExecuted = false; - } - if (!isQueue) { - isQueue = CommonBeanFactory.getBean(ExecThreadPoolExecutor.class).check(apiScenarioThreadMap.get(result)); - } - } - } - for (String result : loadCaseExecInfo.values()) { - if (StringUtils.equalsIgnoreCase(result, TestPlanApiExecuteStatus.RUNNING.name())) { - unFinishedCount++; - if (this.isLoadCaseAllExecuted) { - isLoadCaseAllExecuted = false; - } - } - } - if (lastUnFinishedNumCount != unFinishedCount) { - lastUnFinishedNumCount = unFinishedCount; - lastFinishedNumCountTime = System.currentTimeMillis(); - } - - if (lastUnFinishedNumCount == unFinishedCount && isQueue) { - LoggerUtil.info("执行的报告还在队列中,重置超时时间"); - lastUnFinishedNumCount = unFinishedCount; - lastFinishedNumCountTime = System.currentTimeMillis(); - executeCheck.setFinishedCaseChanged(true); - } else if (unFinishedCount == 0) { - executeCheck.setFinishedCaseChanged(true); - } else { - executeCheck.setFinishedCaseChanged(false); - } - - executeCheck.setTimeOut(false); - if (unFinishedCount > 0) { - //20分钟没有案例执行结果更新,则定位超时 - long nowTime = System.currentTimeMillis(); - if (nowTime - lastFinishedNumCountTime > 1200000) { - executeCheck.setTimeOut(true); - } - } - return executeCheck; - } - - public Map> getExecutedResult() { - Map> resourceTypeMap = new HashMap<>(); - - for (Map.Entry entry : apiCaseExecInfo.entrySet()) { - String resourceId = entry.getKey(); - String executeResult = entry.getValue(); - String resourceType = TestPlanResourceType.API_CASE.name(); - - if (resourceTypeMap.containsKey(resourceType)) { - resourceTypeMap.get(resourceType).put(resourceId, executeResult); - } else { - Map map = new HashMap<>(); - map.put(resourceId, executeResult); - resourceTypeMap.put(resourceType, map); - } - } - - for (Map.Entry entry : apiScenarioCaseExecInfo.entrySet()) { - String resourceId = entry.getKey(); - String executeResult = entry.getValue(); - String resourceType = TestPlanResourceType.SCENARIO_CASE.name(); - - if (resourceTypeMap.containsKey(resourceType)) { - resourceTypeMap.get(resourceType).put(resourceId, executeResult); - } else { - Map map = new HashMap<>(); - map.put(resourceId, executeResult); - resourceTypeMap.put(resourceType, map); - } - } - - for (Map.Entry entry : loadCaseExecInfo.entrySet()) { - String resourceId = entry.getKey(); - String executeResult = entry.getValue(); - String resourceType = TestPlanResourceType.PERFORMANCE_CASE.name(); - - if (resourceTypeMap.containsKey(resourceType)) { - resourceTypeMap.get(resourceType).put(resourceId, executeResult); - } else { - Map map = new HashMap<>(); - map.put(resourceId, executeResult); - resourceTypeMap.put(resourceType, map); - } - } - - return resourceTypeMap; - } - - public void finishAllTask() { - for (Map.Entry entry : apiCaseExecInfo.entrySet()) { - String resourceId = entry.getKey(); - String executeResult = entry.getValue(); - if (StringUtils.equalsIgnoreCase(executeResult, TestPlanApiExecuteStatus.RUNNING.name())) { - apiCaseExecInfo.put(resourceId, TestPlanApiExecuteStatus.FAILD.name()); - if (StringUtils.isNotEmpty(apiCaseExecuteThreadMap.get(resourceId))) { - JmeterThreadUtils.stop(apiCaseExecuteThreadMap.get(resourceId)); - } - } - } - - List updateScenarioReportList = new ArrayList<>(); - for (Map.Entry entry : apiScenarioCaseExecInfo.entrySet()) { - String resourceId = entry.getKey(); - String executeResult = entry.getValue(); - if (StringUtils.equalsIgnoreCase(executeResult, TestPlanApiExecuteStatus.RUNNING.name())) { - apiScenarioCaseExecInfo.put(resourceId, TestPlanApiExecuteStatus.FAILD.name()); - updateScenarioReportList.add(apiScenarioThreadMap.get(resourceId)); - if (StringUtils.isNotEmpty(apiScenarioThreadMap.get(resourceId))) { - JmeterThreadUtils.stop(apiScenarioThreadMap.get(resourceId)); - } - } - } - if (CollectionUtils.isNotEmpty(updateScenarioReportList)) { - ApiScenarioReportMapper apiScenarioReportMapper = CommonBeanFactory.getBean(ApiScenarioReportMapper.class); - ApiScenarioReportExample example = new ApiScenarioReportExample(); - example.createCriteria().andIdIn(updateScenarioReportList).andStatusEqualTo("Running"); - ApiScenarioReport report = new ApiScenarioReport(); - report.setStatus("Error"); - apiScenarioReportMapper.updateByExampleSelective(report, example); - } - - for (Map.Entry entry : loadCaseExecInfo.entrySet()) { - String resourceId = entry.getKey(); - String executeResult = entry.getValue(); - if (StringUtils.equalsIgnoreCase(executeResult, TestPlanApiExecuteStatus.RUNNING.name())) { - if (StringUtils.isNotEmpty(loadCaseReportIdMap.get(resourceId))) { - JmeterThreadUtils.stop(loadCaseReportIdMap.get(resourceId)); - } - loadCaseExecInfo.put(resourceId, TestPlanApiExecuteStatus.FAILD.name()); - } - } - - this.countUnFinishedNum(); - } - - public synchronized void updateReport(Map apiCaseExecResultInfo, Map apiScenarioCaseExecResultInfo) { - if (MapUtils.isNotEmpty(apiCaseExecResultInfo)) { - this.apiCaseReportMap.putAll(apiCaseExecResultInfo); - } - - if (MapUtils.isNotEmpty(apiScenarioCaseExecResultInfo)) { - this.apiScenarioReportMap.putAll(apiScenarioCaseExecResultInfo); - } - } - - public Map getRunningApiCaseReportMap() { - //key: reportId, value: testPlanApiCaseId - Map returnMap = new HashMap<>(); - for (Map.Entry entry : apiCaseExecInfo.entrySet()) { - String planCaseId = entry.getKey(); - String status = entry.getValue(); - if (StringUtils.equalsIgnoreCase(status, TestPlanApiExecuteStatus.RUNNING.name())) { - if (apiCaseExecuteThreadMap.containsKey(planCaseId)) { - returnMap.put(apiCaseExecuteThreadMap.get(planCaseId), planCaseId); - } - } - } - return returnMap; - } - - public Map getRunningScenarioReportMap() { - //key: reportId, value: testPlanApiScenarioId - Map returnMap = new HashMap<>(); - for (Map.Entry entry : apiScenarioCaseExecInfo.entrySet()) { - String planScenarioId = entry.getKey(); - String status = entry.getValue(); - if (StringUtils.equalsIgnoreCase(status, TestPlanApiExecuteStatus.RUNNING.name())) { - if (apiScenarioThreadMap.containsKey(planScenarioId)) { - returnMap.put(apiScenarioThreadMap.get(planScenarioId), planScenarioId); - } - } - } - return returnMap; - } -} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/api/cache/TestPlanReportExecuteCatch.java b/backend/src/main/java/io/metersphere/api/cache/TestPlanReportExecuteCatch.java deleted file mode 100644 index 3f4c680264..0000000000 --- a/backend/src/main/java/io/metersphere/api/cache/TestPlanReportExecuteCatch.java +++ /dev/null @@ -1,118 +0,0 @@ -package io.metersphere.api.cache; - -import io.metersphere.commons.constants.TestPlanApiExecuteStatus; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * @author song.tianyang - * @Date 2021/8/20 3:29 下午 - */ -public class TestPlanReportExecuteCatch { - private static Logger testPlanLog = LoggerFactory.getLogger("testPlanExecuteLog"); - private static Map testPlanReportMap = new HashMap<>(); - - private TestPlanReportExecuteCatch() { - } - - public synchronized static void addApiTestPlanExecuteInfo(String reportId, String creator, - Map apiCaseExecInfo, Map apiScenarioCaseExecInfo, Map loadCaseExecInfo) { - if (testPlanReportMap == null) { - testPlanReportMap = new HashMap<>(); - } - if (apiCaseExecInfo == null) { - apiCaseExecInfo = new HashMap<>(); - } - if (apiScenarioCaseExecInfo == null) { - apiScenarioCaseExecInfo = new HashMap<>(); - } - if (loadCaseExecInfo == null) { - loadCaseExecInfo = new HashMap<>(); - } - - TestPlanExecuteInfo executeInfo = new TestPlanExecuteInfo(reportId, creator); - executeInfo.setApiCaseExecInfo(apiCaseExecInfo); - executeInfo.setApiScenarioCaseExecInfo(apiScenarioCaseExecInfo); - executeInfo.setLoadCaseExecInfo(loadCaseExecInfo); - testPlanReportMap.put(reportId, executeInfo); - } - - public synchronized static String getCreator(String reportId) { - if (testPlanReportMap != null && testPlanReportMap.containsKey(reportId)) { - return testPlanReportMap.get(reportId).getCreator(); - } else { - return null; - } - } - - public synchronized static boolean containsReport(String reportId) { - if(StringUtils.isEmpty(reportId)){ - return false; - }else { - return testPlanReportMap != null && testPlanReportMap.containsKey(reportId); - } - } - - public synchronized static void updateApiTestPlanExecuteInfo(String reportId, - Map apiCaseExecInfo, Map apiScenarioCaseExecInfo, Map loadCaseExecInfo) { - if (testPlanReportMap != null && testPlanReportMap.containsKey(reportId)) { - testPlanReportMap.get(reportId).updateExecuteInfo(apiCaseExecInfo, apiScenarioCaseExecInfo, loadCaseExecInfo); - } - } - - public synchronized static void updateTestPlanThreadInfo(String reportId, - Map apiCaseExecResultInfo, Map apiScenarioCaseExecResultInfo, Map loadCaseExecResultInfo) { - if (testPlanReportMap != null && testPlanReportMap.containsKey(reportId)) { - testPlanReportMap.get(reportId).updateThreadResult(apiCaseExecResultInfo, apiScenarioCaseExecResultInfo, loadCaseExecResultInfo); - } - } - - public synchronized static void updateTestPlanReport(String reportId, - Map apiCaseExecResultInfo, Map apiScenarioCaseExecResultInfo) { - if (testPlanReportMap != null && testPlanReportMap.containsKey(reportId)) { - testPlanReportMap.get(reportId).updateReport(apiCaseExecResultInfo, apiScenarioCaseExecResultInfo); - } - } - - - public static TestPlanExecuteInfo getTestPlanExecuteInfo(String reportId) { - return testPlanReportMap.get(reportId); - } - - public static synchronized void setReportDataCheckResult(String reportId, boolean result) { - if (testPlanReportMap.containsKey(reportId)) { - testPlanReportMap.get(reportId).setReportDataInDataBase(result); - } - } - - public static synchronized void remove(String reportId) { - if (testPlanReportMap.containsKey(reportId)) { - testPlanReportMap.get(reportId).finishAllTask(); - testPlanReportMap.remove(reportId); - } - } - - public static void finishAllTask(String planReportId) { - testPlanLog.info("ReportId[" + planReportId + "] finish task!"); - if (testPlanReportMap.containsKey(planReportId)) { - testPlanReportMap.get(planReportId).finishAllTask(); - } - } - - public static void set(String planReportId, List executeErrorList) { - if (TestPlanReportExecuteCatch.containsReport(planReportId)) { - if (!executeErrorList.isEmpty()) { - Map executeErrorMap = new HashMap<>(); - for (String id : executeErrorList) { - executeErrorMap.put(id, TestPlanApiExecuteStatus.FAILD.name()); - } - TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(planReportId, executeErrorMap, null, null); - } - } - } -} diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java b/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java index 62f555d156..17ec6c5dff 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java @@ -46,5 +46,7 @@ public class RunScenarioRequest { //生成测试报告:当isTestPlanScheduleJob为ture时使用 private String testPlanReportId; + private String testPlanReportContentId; + private String requestOriginator; } diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/SchedulePlanScenarioExecuteRequest.java b/backend/src/main/java/io/metersphere/api/dto/automation/SchedulePlanScenarioExecuteRequest.java index 09cbddedbd..44c8d72ee7 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/SchedulePlanScenarioExecuteRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/SchedulePlanScenarioExecuteRequest.java @@ -33,9 +33,11 @@ public class SchedulePlanScenarioExecuteRequest { private String reportUserID; //key: test_plan.id, value: test_plan_api_scenario <->scenarioValue - private Map> testPlanScenarioIDMap; + private Map> testPlanScenarioIDMap; private String testPlanReportId; + private String planReportContentId; + private RunModeConfigDTO config; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/BatchRunDefinitionRequest.java b/backend/src/main/java/io/metersphere/api/dto/definition/BatchRunDefinitionRequest.java index bdeb5ca2b2..63ebccbf93 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/BatchRunDefinitionRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/BatchRunDefinitionRequest.java @@ -17,8 +17,11 @@ public class BatchRunDefinitionRequest { private RunModeConfigDTO config; + private String userId; + //测试计划报告ID。 测试计划执行时使用 private String planReportId; - private String userId; + private String planReportContentId; + } diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java index 49d9415504..496ee3987d 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java @@ -1,7 +1,6 @@ package io.metersphere.api.exec.api; import com.alibaba.fastjson.JSON; -import io.metersphere.api.cache.TestPlanReportExecuteCatch; import io.metersphere.api.dto.ApiCaseRunRequest; import io.metersphere.api.dto.definition.ApiTestCaseRequest; import io.metersphere.api.dto.definition.BatchRunDefinitionRequest; @@ -17,11 +16,13 @@ import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.TriggerMode; +import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.constants.RunModeConstants; import io.metersphere.dto.MsExecResponseDTO; import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.service.EnvironmentGroupProjectService; +import io.metersphere.track.service.TestPlanReportService; import io.metersphere.utils.LoggerUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -88,13 +89,12 @@ public class ApiCaseExecuteService { List responseDTOS = new LinkedList<>(); Map executeQueue = new HashMap<>(); - //记录案例线程结果以及执行失败的案例ID - Map executeThreadIdMap = new HashMap<>(); + Map testPlanCaseIdAndReportIdMap = new HashMap<>(); String status = request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString()) ? APITestStatus.Waiting.name() : APITestStatus.Running.name(); planApiCases.forEach(testPlanApiCase -> { ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.addResult(request, testPlanApiCase, status, batchMapper); executeQueue.put(testPlanApiCase.getId(), report); - executeThreadIdMap.put(testPlanApiCase.getId(), report.getId()); + testPlanCaseIdAndReportIdMap.put(testPlanApiCase.getId(), report.getId()); responseDTOS.add(new MsExecResponseDTO(testPlanApiCase.getId(), report.getId(), request.getTriggerMode())); }); sqlSession.flushStatements(); @@ -109,10 +109,9 @@ public class ApiCaseExecuteService { DBTestQueue deQueue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.API_PLAN.name(), request.getPlanReportId(), reportType, runMode, request.getConfig().getEnvMap()); //如果是测试计划生成报告的执行,则更新执行信息、执行线程信息。 - if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) { - if (!executeThreadIdMap.isEmpty()) { - TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap, null, null); - } + if (StringUtils.isNotEmpty(request.getPlanReportContentId())) { + TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class); + testPlanReportService.updateTestPlanReportContentReportIds(request.getPlanReportContentId(),testPlanCaseIdAndReportIdMap,null,null); } // 开始选择执行模式 if (request.getConfig() != null && request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java index ece60cdc39..2641464c3a 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java @@ -1,7 +1,6 @@ package io.metersphere.api.exec.scenario; import com.alibaba.fastjson.JSON; -import io.metersphere.api.cache.TestPlanReportExecuteCatch; import io.metersphere.api.dto.EnvironmentType; import io.metersphere.api.dto.RunModeDataDTO; import io.metersphere.api.dto.automation.APIScenarioReportResult; @@ -25,10 +24,7 @@ import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ReportTriggerMode; import io.metersphere.commons.constants.TriggerMode; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.FileUtils; -import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.ServiceUtils; -import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.commons.utils.*; import io.metersphere.constants.RunModeConstants; import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.MsExecResponseDTO; @@ -36,6 +32,7 @@ import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.i18n.Translator; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.EnvironmentGroupProjectService; +import io.metersphere.track.service.TestPlanReportService; import io.metersphere.track.service.TestPlanScenarioCaseService; import io.metersphere.utils.LoggerUtil; import org.apache.commons.beanutils.BeanComparator; @@ -227,6 +224,7 @@ public class ApiScenarioExecuteService { } String projectId = request.getProjectId(); Map scenarioMap = apiScenarios.stream().collect(Collectors.toMap(ApiScenarioWithBLOBs::getId, Function.identity(), (t1, t2) -> t1)); + Map scenarioReportIdMap = new HashMap<>(); for (Map.Entry entry : planScenarioIdMap.entrySet()) { String testPlanScenarioId = entry.getKey(); String scenarioId = entry.getValue(); @@ -258,10 +256,8 @@ public class ApiScenarioExecuteService { report = apiScenarioReportService.init(reportId, testPlanScenarioId, scenario.getName(), request.getTriggerMode(), request.getExecuteType(), projectId, request.getReportUserID(), request.getConfig(), scenario.getId()); - if (report != null && StringUtils.isNotEmpty(request.getTestPlanReportId())) { - Map scenarioReportIdMap = new HashMap<>(); + if (report != null && StringUtils.isNotEmpty(request.getTestPlanReportContentId())) { scenarioReportIdMap.put(testPlanScenarioId, report.getId()); - TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getTestPlanReportId(), null, scenarioReportIdMap, null); } scenarioIds.add(scenario.getId()); if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) { @@ -298,6 +294,10 @@ public class ApiScenarioExecuteService { // 重置报告ID reportId = UUID.randomUUID().toString(); } + if(StringUtils.isNotEmpty(request.getTestPlanReportContentId())){ + TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class); + testPlanReportService.updateTestPlanReportContentReportIds(request.getTestPlanReportContentId(),null,scenarioReportIdMap,null); + } } /** diff --git a/backend/src/main/java/io/metersphere/api/exec/schedule/TestPlanReportListenerScheduled.java b/backend/src/main/java/io/metersphere/api/exec/schedule/TestPlanReportListenerScheduled.java deleted file mode 100644 index 610505dc4e..0000000000 --- a/backend/src/main/java/io/metersphere/api/exec/schedule/TestPlanReportListenerScheduled.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.metersphere.api.exec.schedule; - -import io.metersphere.api.cache.TestPlanReportExecuteCatch; -import io.metersphere.api.exec.queue.ExecThreadPoolExecutor; -import io.metersphere.api.jmeter.MessageCache; -import io.metersphere.commons.utils.CommonBeanFactory; -import io.metersphere.track.service.TestPlanReportService; -import io.metersphere.utils.LoggerUtil; -import org.apache.commons.collections.CollectionUtils; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -@Component -public class TestPlanReportListenerScheduled { - /** - * 定时调用监听检查报告状态 - */ - @Scheduled(cron = "*/9 * * * * ?") - public void testPlanScheduled() { - //判断缓冲队列是否存在记录 - if (CollectionUtils.isNotEmpty(MessageCache.jobReportCache)) { - for (int i = 0; i < MessageCache.jobReportCache.size(); i++) { - this.listener(MessageCache.jobReportCache.get(i)); - } - } - } - - private void listener(String planReportId) { - if (TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId) != null) { - if (!CommonBeanFactory.getBean(ExecThreadPoolExecutor.class).checkPlanReport(planReportId)) { - LoggerUtil.info("检查测试计划执行报告:【" + planReportId + "】"); - CommonBeanFactory.getBean(TestPlanReportService.class).countReport(planReportId); - } - } else { - MessageCache.jobReportCache.remove(planReportId); - LoggerUtil.info("测试计划执行报告:【" + planReportId + "】执行完成,剩余队列:" + MessageCache.jobReportCache.size()); - } - } -} 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 2fce16be16..419b938346 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java @@ -2,7 +2,6 @@ package io.metersphere.api.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import io.metersphere.api.cache.TestPlanReportExecuteCatch; import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; @@ -229,7 +228,6 @@ public class ApiDefinitionExecResultService { apiIdResultMap.put(dto.getReportId(), status); } testPlanLog.info("TestPlanReportId[" + dto.getTestPlanReportId() + "] APICASE OVER. API CASE STATUS:" + JSONObject.toJSONString(apiIdResultMap)); - TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(dto.getTestPlanReportId(), apiIdResultMap, null, null); } /** 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 338339d918..c3ea6dfe80 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -3,7 +3,6 @@ package io.metersphere.api.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import io.metersphere.api.cache.TestPlanReportExecuteCatch; import io.metersphere.api.dto.APIReportBatchRequest; import io.metersphere.api.dto.DeleteAPIReportRequest; import io.metersphere.api.dto.EnvironmentType; @@ -103,7 +102,6 @@ public class ApiScenarioReportService { this.put(dto.getReportId(), status); }}; testPlanLog.info("TestPlanReportId" + JSONArray.toJSONString(dto.getReportId()) + " EXECUTE OVER. SCENARIO STATUS : " + JSONObject.toJSONString(reportMap)); - TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(dto.getTestPlanReportId(), null, reportMap, null); } ApiScenarioReport scenarioReport; diff --git a/backend/src/main/java/io/metersphere/base/domain/TestPlanReportContentWithBLOBs.java b/backend/src/main/java/io/metersphere/base/domain/TestPlanReportContentWithBLOBs.java index 1851227673..4bebc555f6 100644 --- a/backend/src/main/java/io/metersphere/base/domain/TestPlanReportContentWithBLOBs.java +++ b/backend/src/main/java/io/metersphere/base/domain/TestPlanReportContentWithBLOBs.java @@ -37,5 +37,11 @@ public class TestPlanReportContentWithBLOBs extends TestPlanReportContent implem private String loadFailureCases; + private String scenarioReportId; + + private String apiCaseReportId; + + private String loadCaseReportId; + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/TestPlanReportContentMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/TestPlanReportContentMapper.xml index 1509bedbdd..1a7a409165 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/TestPlanReportContentMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/TestPlanReportContentMapper.xml @@ -26,6 +26,9 @@ + + + @@ -92,7 +95,7 @@ config, summary, function_result, api_result, load_result, function_all_cases, function_failure_cases, issue_list, api_all_cases, api_failure_cases, scenario_all_cases, scenario_failure_cases, - load_all_Cases, load_failure_cases + load_all_Cases, load_failure_cases, scenario_report_id, api_case_report_id, load_case_report_id @@ -374,6 +399,15 @@ load_failure_cases = #{record.loadFailureCases,jdbcType=LONGVARCHAR}, + + scenario_report_id = #{record.scenarioReportId,jdbcType=LONGVARCHAR}, + + + api_case_report_id = #{record.apiCaseReportId,jdbcType=LONGVARCHAR}, + + + load_case_report_id = #{record.loadCaseReportId,jdbcType=LONGVARCHAR}, + @@ -402,7 +436,10 @@ scenario_all_cases = #{record.scenarioAllCases,jdbcType=LONGVARCHAR}, scenario_failure_cases = #{record.scenarioFailureCases,jdbcType=LONGVARCHAR}, load_all_Cases = #{record.loadAllCases,jdbcType=LONGVARCHAR}, - load_failure_cases = #{record.loadFailureCases,jdbcType=LONGVARCHAR} + load_failure_cases = #{record.loadFailureCases,jdbcType=LONGVARCHAR}, + scenario_report_id = #{record.scenarioReportId,jdbcType=LONGVARCHAR}, + api_case_report_id = #{record.apiCaseReportId,jdbcType=LONGVARCHAR}, + load_case_report_id = #{record.loadCaseReportId,jdbcType=LONGVARCHAR} @@ -487,6 +524,15 @@ load_failure_cases = #{loadFailureCases,jdbcType=LONGVARCHAR}, + + scenario_report_id = #{scenarioReportId,jdbcType=LONGVARCHAR}, + + + api_case_report_id = #{apiCaseReportId,jdbcType=LONGVARCHAR}, + + + load_case_report_id = #{loadCaseReportId,jdbcType=LONGVARCHAR}, + where id = #{id,jdbcType=VARCHAR} @@ -512,7 +558,10 @@ scenario_all_cases = #{scenarioAllCases,jdbcType=LONGVARCHAR}, scenario_failure_cases = #{scenarioFailureCases,jdbcType=LONGVARCHAR}, load_all_Cases = #{loadAllCases,jdbcType=LONGVARCHAR}, - load_failure_cases = #{loadFailureCases,jdbcType=LONGVARCHAR} + load_failure_cases = #{loadFailureCases,jdbcType=LONGVARCHAR}, + scenario_report_id = #{scenarioReportId,jdbcType=LONGVARCHAR}, + api_case_report_id = #{apiCaseReportId,jdbcType=LONGVARCHAR}, + load_case_report_id = #{loadCaseReportId,jdbcType=LONGVARCHAR} where id = #{id,jdbcType=VARCHAR} diff --git a/backend/src/main/java/io/metersphere/controller/ShareController.java b/backend/src/main/java/io/metersphere/controller/ShareController.java index 14b7ee527f..e9dee178b1 100644 --- a/backend/src/main/java/io/metersphere/controller/ShareController.java +++ b/backend/src/main/java/io/metersphere/controller/ShareController.java @@ -74,7 +74,7 @@ public class ShareController { @GetMapping("/test/plan/report/{shareId}/{planId}") public TestPlanSimpleReportDTO getReport(@PathVariable String shareId, @PathVariable String planId) { shareInfoService.validate(shareId, planId); - return testPlanService.getReport(planId); + return testPlanService.getReport(planId, null); } @GetMapping("/report/export/{shareId}/{planId}") diff --git a/backend/src/main/java/io/metersphere/dto/TestPlanExecuteReportDTO.java b/backend/src/main/java/io/metersphere/dto/TestPlanExecuteReportDTO.java new file mode 100644 index 0000000000..00cf95fe07 --- /dev/null +++ b/backend/src/main/java/io/metersphere/dto/TestPlanExecuteReportDTO.java @@ -0,0 +1,16 @@ +package io.metersphere.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +import java.util.Map; + +@Getter +@Setter +@AllArgsConstructor +public class TestPlanExecuteReportDTO { + private Map testPlanApiCaseIdAndReportIdMap; + private Map testPlanScenarioIdAndReportIdMap; + private Map testPlanLoadCaseIdAndReportIdMap; +} diff --git a/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java b/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java index 2cf5e48f5c..9ec6666184 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java @@ -128,8 +128,8 @@ public class TestPlanController { @PostMapping("/edit/follows/{planId}") @RequiresPermissions(PermissionConstants.PROJECT_TRACK_PLAN_READ_EDIT) - public void editTestFollows(@PathVariable String planId,@RequestBody List follows) { - testPlanService.editTestFollows(planId,follows); + public void editTestFollows(@PathVariable String planId, @RequestBody List follows) { + testPlanService.editTestFollows(planId, follows); } @PostMapping("/delete/{testPlanId}") @@ -232,7 +232,7 @@ public class TestPlanController { @GetMapping("/report/{planId}") public TestPlanSimpleReportDTO getReport(@PathVariable String planId) { - return testPlanService.getReport(planId); + return testPlanService.getReport(planId, null); } @GetMapping("/report/functional/result") diff --git a/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java b/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java index c0cd341792..ae59a0e3c2 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java @@ -12,6 +12,7 @@ import io.metersphere.commons.utils.SessionUtils; import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.notice.annotation.SendNotice; import io.metersphere.track.dto.TestPlanReportDTO; +import io.metersphere.track.dto.TestPlanScheduleReportInfoDTO; import io.metersphere.track.dto.TestPlanSimpleReportDTO; import io.metersphere.track.request.report.QueryTestPlanReportRequest; import io.metersphere.track.request.report.TestPlanReportSaveRequest; @@ -82,8 +83,8 @@ public class TestPlanReportController { public void apiExecuteFinish(@PathVariable String planId, @PathVariable String userId) { String reportId = UUID.randomUUID().toString(); TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(reportId, planId, userId, ReportTriggerMode.API.name()); - TestPlanReport report = testPlanReportService.genTestPlanReport(saveRequest); - testPlanReportService.countReportByTestPlanReportId(report.getId(), null, ReportTriggerMode.API.name(), null); + TestPlanScheduleReportInfoDTO report = testPlanReportService.genTestPlanReport(saveRequest); + testPlanReportService.countReportByTestPlanReportId(report.getTestPlanReport().getId(), null, ReportTriggerMode.API.name()); } @GetMapping("/saveTestPlanReport/{planId}/{triggerMode}") @@ -91,8 +92,8 @@ public class TestPlanReportController { String userId = SessionUtils.getUser().getId(); String reportId = UUID.randomUUID().toString(); TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(reportId, planId, userId, triggerMode); - TestPlanReport report = testPlanReportService.genTestPlanReport(saveRequest); - testPlanReportService.countReportByTestPlanReportId(report.getId(), null, triggerMode, null); + TestPlanScheduleReportInfoDTO report = testPlanReportService.genTestPlanReport(saveRequest); + testPlanReportService.countReportByTestPlanReportId(report.getTestPlanReport().getId(), null, triggerMode); return "success"; } } diff --git a/backend/src/main/java/io/metersphere/track/dto/TestPlanScheduleReportInfoDTO.java b/backend/src/main/java/io/metersphere/track/dto/TestPlanScheduleReportInfoDTO.java index 6f268b4fcd..30dae2367d 100644 --- a/backend/src/main/java/io/metersphere/track/dto/TestPlanScheduleReportInfoDTO.java +++ b/backend/src/main/java/io/metersphere/track/dto/TestPlanScheduleReportInfoDTO.java @@ -1,6 +1,7 @@ package io.metersphere.track.dto; import io.metersphere.base.domain.TestPlanReport; +import io.metersphere.base.domain.TestPlanReportContent; import lombok.Getter; import lombok.Setter; @@ -15,6 +16,7 @@ import java.util.Map; @Setter public class TestPlanScheduleReportInfoDTO { private TestPlanReport testPlanReport; + private TestPlanReportContent testPlanReportContent; private Map planScenarioIdMap = new LinkedHashMap<>(); private Map apiTestCaseDataMap = new LinkedHashMap<>(); private Map performanceIdMap = new LinkedHashMap<>(); diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java index 3bf3e3e25e..d837a984f3 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -412,14 +412,11 @@ public class TestPlanApiCaseService { testPlanApiCaseMapper::updateByPrimaryKeySelective); } - public List getByApiExecReportIds(Map testPlanApiCaseReportMap, boolean isFinish) { + public List getByApiExecReportIds(Map testPlanApiCaseReportMap) { if (testPlanApiCaseReportMap.isEmpty()) { return new ArrayList<>(); } - String defaultStatus = "Running"; - if (isFinish) { - defaultStatus = "error"; - } + String defaultStatus = "error"; List apiTestCases = extTestPlanApiCaseMapper.getFailureListByIds(testPlanApiCaseReportMap.keySet(), null); Map reportResult = apiDefinitionExecResultService.selectReportResultByReportIds(testPlanApiCaseReportMap.values()); for (TestPlanFailureApiDTO dto : apiTestCases) { 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 8572a94479..1d07bd2d01 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -3,22 +3,16 @@ package io.metersphere.track.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import io.metersphere.api.cache.TestPlanExecuteInfo; -import io.metersphere.api.cache.TestPlanReportExecuteCatch; -import io.metersphere.api.dto.automation.ApiScenarioDTO; import io.metersphere.api.dto.automation.TestPlanFailureApiDTO; import io.metersphere.api.dto.automation.TestPlanFailureScenarioDTO; -import io.metersphere.api.dto.automation.TestPlanScenarioRequest; -import io.metersphere.api.dto.definition.ApiTestCaseRequest; -import io.metersphere.api.dto.definition.TestPlanApiCaseDTO; import io.metersphere.api.service.ShareInfoService; -import io.metersphere.api.exec.utils.NamedThreadFactory; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.ext.*; import io.metersphere.commons.constants.*; import io.metersphere.commons.utils.*; import io.metersphere.dto.BaseSystemConfigDTO; +import io.metersphere.dto.TestPlanExecuteReportDTO; import io.metersphere.dto.UserDTO; import io.metersphere.i18n.Translator; import io.metersphere.log.vo.OperatingLogDetails; @@ -27,7 +21,6 @@ import io.metersphere.notice.service.NoticeSendService; import io.metersphere.service.ProjectService; import io.metersphere.service.SystemParameterService; import io.metersphere.service.UserService; -import io.metersphere.track.domain.ReportResultComponent; import io.metersphere.track.dto.*; import io.metersphere.track.request.report.QueryTestPlanReportRequest; import io.metersphere.track.request.report.TestPlanReportSaveRequest; @@ -45,8 +38,6 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.stream.Collectors; /** @@ -63,10 +54,6 @@ public class TestPlanReportService { @Resource TestPlanReportMapper testPlanReportMapper; @Resource - ApiScenarioReportMapper apiScenarioReportMapper; - @Resource - ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; - @Resource TestPlanReportDataMapper testPlanReportDataMapper; @Resource ExtTestPlanScenarioCaseMapper extTestPlanScenarioCaseMapper; @@ -80,14 +67,6 @@ public class TestPlanReportService { ExtTestPlanMapper extTestPlanMapper; @Resource ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper; - @Resource - ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper; - @Resource - ExtApiScenarioReportMapper extApiScenarioReportMapper; - @Resource - LoadTestReportMapper loadTestReportMapper; - @Resource - TestPlanLoadCaseMapper testPlanLoadCaseMapper; @Lazy @Resource TestPlanService testPlanService; @@ -103,8 +82,6 @@ public class TestPlanReportService { @Resource private ProjectService projectService; - private final ExecutorService executorService = Executors.newFixedThreadPool(20, new NamedThreadFactory("TestPlanReportService")); - public List list(QueryTestPlanReportRequest request) { List list = new ArrayList<>(); request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders())); @@ -171,9 +148,7 @@ public class TestPlanReportService { TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(planReportId, planId, userId, triggerMode, planTestCaseIdMap.size() > 0, planScenarioIdMap.size() > 0, performanceIdMap.size() > 0, apiCaseInfoMap, scenarioInfoMap, performanceInfoMap); - TestPlanReport report = this.genTestPlanReport(saveRequest); - TestPlanScheduleReportInfoDTO returnDTO = new TestPlanScheduleReportInfoDTO(); - returnDTO.setTestPlanReport(report); + TestPlanScheduleReportInfoDTO returnDTO = this.genTestPlanReport(saveRequest); returnDTO.setPlanScenarioIdMap(planScenarioIdMap); returnDTO.setApiTestCaseDataMap(planTestCaseIdMap); returnDTO.setPerformanceIdMap(performanceIdMap); @@ -190,7 +165,7 @@ public class TestPlanReportService { * saveRequest.scenarioIsExecuting 场景案例是否执行中 * saveRequest.performanceIsExecuting 性能案例是否执行中 */ - public TestPlanReport genTestPlanReport(TestPlanReportSaveRequest saveRequest) { + public TestPlanScheduleReportInfoDTO genTestPlanReport(TestPlanReportSaveRequest saveRequest) { TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(saveRequest.getPlanId()); String testPlanReportID = saveRequest.getReportID(); TestPlanReport testPlanReport = new TestPlanReport(); @@ -219,10 +194,6 @@ public class TestPlanReportService { testPlanReportContent.setEndTime(System.currentTimeMillis()); } - Map apiCaseInfoMap = new HashMap<>(); - Map scenarioInfoMap = new HashMap<>(); - Map performanceInfoMap = new HashMap<>(); - if (saveRequest.isCountResources()) { List testPlanApiCaseList = extTestPlanApiCaseMapper.selectLegalDataByTestPlanId(saveRequest.getPlanId()); List apiCaseIdList = testPlanApiCaseList.stream().map(TestPlanApiCase::getApiCaseId).collect(Collectors.toList()); @@ -238,28 +209,12 @@ public class TestPlanReportService { List performanceIdList = extTestPlanLoadCaseMapper.selectTestPlanLoadCaseList(loadCaseRequest) .stream().map(TestPlanLoadCaseDTO::getLoadCaseId).collect(Collectors.toList()); testPlanReport.setIsPerformanceExecuting(!performanceIdList.isEmpty()); - - for (String id : apiCaseIdList) { - apiCaseInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); - } - for (String id : scenarioIdList) { - scenarioInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); - } - for (String id : performanceIdList) { - performanceInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); - } } else { testPlanReport.setIsApiCaseExecuting(saveRequest.isApiCaseIsExecuting()); testPlanReport.setIsScenarioExecuting(saveRequest.isScenarioIsExecuting()); testPlanReport.setIsPerformanceExecuting(saveRequest.isPerformanceIsExecuting()); - - apiCaseInfoMap = saveRequest.getApiCaseIdMap(); - scenarioInfoMap = saveRequest.getScenarioIdMap(); - performanceInfoMap = saveRequest.getPerformanceIdMap(); } - TestPlanReportExecuteCatch.addApiTestPlanExecuteInfo(testPlanReportID, saveRequest.getUserId(), apiCaseInfoMap, scenarioInfoMap, performanceInfoMap); - if (testPlanReport.getIsScenarioExecuting() || testPlanReport.getIsApiCaseExecuting() || testPlanReport.getIsPerformanceExecuting()) { testPlanReport.setStatus(TestPlanReportStatus.RUNNING.name()); } else { @@ -272,7 +227,11 @@ public class TestPlanReportService { //更新TestPlan状态,改为进行中 testPlan.setStatus(TestPlanStatus.Underway.name()); testPlanMapper.updateByPrimaryKeySelective(testPlan); - return testPlanReport; + + TestPlanScheduleReportInfoDTO returnDTO = new TestPlanScheduleReportInfoDTO(); + returnDTO.setTestPlanReport(testPlanReport); + returnDTO.setTestPlanReportContent(testPlanReportContent); + return returnDTO; } public TestPlanReportDTO getMetric(String reportId) { @@ -343,258 +302,48 @@ public class TestPlanReportService { public synchronized void updateReport(List testPlanReportIdList, String runMode, String triggerMode) { for (String planReportId : testPlanReportIdList) { - this.countReportByTestPlanReportId(planReportId, runMode, triggerMode, null); + this.countReportByTestPlanReportId(planReportId, runMode, triggerMode); } } - public synchronized void updateReportByScenarioIdList(List testPlanReportIdList, String runMode, String triggerMode, List scenarioIdList) { - for (String planReportId : testPlanReportIdList) { - this.countReportByTestPlanReportId(planReportId, runMode, triggerMode, scenarioIdList); - } - } - - public TestCaseReportMetricDTO countReportData(TestPlanDTO testPlan, Map> executeResult) { - TestCaseReportMetricDTO returnDTO = new TestCaseReportMetricDTO(); - ReportResultComponent reportResultComponent = new ReportResultComponent(testPlan); - reportResultComponent.afterBuild(returnDTO); - - TestCaseReportAdvanceStatusResultDTO statusDTO = new TestCaseReportAdvanceStatusResultDTO(); - Map apiCaseExecuteMap = executeResult.get(TestPlanResourceType.API_CASE.name()); - Map scenarioExecuteMap = executeResult.get(TestPlanResourceType.SCENARIO_CASE.name()); - Map performanceCaseExecuteMap = executeResult.get(TestPlanResourceType.PERFORMANCE_CASE.name()); - - List apiResult = new ArrayList<>(); - List scenarioResult = new ArrayList<>(); - List loadResult = new ArrayList<>(); - - List faliureApiCaseIdList = new ArrayList<>(); - List faliureScenarioCaseIdList = new ArrayList<>(); - List faliureLoadCaseIdList = new ArrayList<>(); - - if (MapUtils.isNotEmpty(apiCaseExecuteMap)) { - Map countMap = new HashMap<>(); - for (Map.Entry executeEntry : apiCaseExecuteMap.entrySet()) { - String caseResult = executeEntry.getValue(); - String id = executeEntry.getKey(); - if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.SUCCESS.name())) { - if (countMap.containsKey("Pass")) { - countMap.put("Pass", countMap.get("Pass") + 1); - } else { - countMap.put("Pass", 1); - } - } else if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.FAILD.name())) { - faliureApiCaseIdList.add(id); - if (countMap.containsKey("Failure")) { - countMap.put("Failure", countMap.get("Failure") + 1); - } else { - countMap.put("Failure", 1); - } - } else if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.PREPARE.name())) { - faliureApiCaseIdList.add(id); - if (countMap.containsKey("Skip")) { - countMap.put("Skip", countMap.get("Skip") + 1); - } else { - countMap.put("Skip", 1); - } - } else { - if (countMap.containsKey("Underway")) { - countMap.put("Underway", countMap.get("Underway") + 1); - } else { - countMap.put("Underway", 1); - } - } - } - for (Map.Entry entry : countMap.entrySet()) { - String status = entry.getKey(); - Integer value = entry.getValue(); - TestCaseReportStatusResultDTO dto = new TestCaseReportStatusResultDTO(); - dto.setStatus(status); - dto.setCount(value); - apiResult.add(dto); - } - } - - if (MapUtils.isNotEmpty(scenarioExecuteMap)) { - Map countMap = new HashMap<>(); - for (Map.Entry executeEntry : scenarioExecuteMap.entrySet()) { - String caseResult = executeEntry.getValue(); - String id = executeEntry.getKey(); - if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.SUCCESS.name())) { - if (countMap.containsKey("Pass")) { - countMap.put("Pass", countMap.get("Pass") + 1); - } else { - countMap.put("Pass", 1); - } - } else if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.FAILD.name())) { - faliureApiCaseIdList.add(id); - if (countMap.containsKey("Failure")) { - countMap.put("Failure", countMap.get("Failure") + 1); - } else { - countMap.put("Failure", 1); - } - } else if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.PREPARE.name())) { - faliureApiCaseIdList.add(id); - if (countMap.containsKey("Skip")) { - countMap.put("Skip", countMap.get("Skip") + 1); - } else { - countMap.put("Skip", 1); - } - } else { - if (countMap.containsKey("Underway")) { - countMap.put("Underway", countMap.get("Underway") + 1); - } else { - countMap.put("Underway", 1); - } - } - } - for (Map.Entry entry : countMap.entrySet()) { - String status = entry.getKey(); - Integer value = entry.getValue(); - TestCaseReportStatusResultDTO dto = new TestCaseReportStatusResultDTO(); - dto.setStatus(status); - dto.setCount(value); - scenarioResult.add(dto); - } - } - - if (MapUtils.isNotEmpty(performanceCaseExecuteMap)) { - Map countMap = new HashMap<>(); - for (Map.Entry executeEntry : performanceCaseExecuteMap.entrySet()) { - String caseResult = executeEntry.getValue(); - String id = executeEntry.getKey(); - if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.SUCCESS.name())) { - if (countMap.containsKey("Pass")) { - countMap.put("Pass", countMap.get("Pass") + 1); - } else { - countMap.put("Pass", 1); - } - } else if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.FAILD.name())) { - faliureApiCaseIdList.add(id); - if (countMap.containsKey("Failure")) { - countMap.put("Failure", countMap.get("Failure") + 1); - } else { - countMap.put("Failure", 1); - } - } else if (StringUtils.equalsAnyIgnoreCase(caseResult, TestPlanApiExecuteStatus.PREPARE.name())) { - faliureApiCaseIdList.add(id); - if (countMap.containsKey("Skip")) { - countMap.put("Skip", countMap.get("Skip") + 1); - } else { - countMap.put("Skip", 1); - } - } else { - if (countMap.containsKey("Underway")) { - countMap.put("Underway", countMap.get("Underway") + 1); - } else { - countMap.put("Underway", 1); - } - } - } - for (Map.Entry entry : countMap.entrySet()) { - String status = entry.getKey(); - Integer value = entry.getValue(); - TestCaseReportStatusResultDTO dto = new TestCaseReportStatusResultDTO(); - dto.setStatus(status); - dto.setCount(value); - loadResult.add(dto); - } - } - statusDTO.setApiResult(apiResult); - statusDTO.setScenarioResult(scenarioResult); - statusDTO.setLoadResult(loadResult); - - returnDTO.setExecuteResult(statusDTO); - - //统计失败用例 - FailureTestCasesAdvanceDTO failureDto = new FailureTestCasesAdvanceDTO(); - failureDto.setFunctionalTestCases(new ArrayList<>()); - if (!faliureApiCaseIdList.isEmpty()) { - TestPlanApiCaseService testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class); - ApiTestCaseRequest request = new ApiTestCaseRequest(); - request.setPlanId(testPlan.getId()); - request.setIds(faliureApiCaseIdList); - List testPlanApiCaseDTOList = testPlanApiCaseService.list(request); - failureDto.setApiTestCases(testPlanApiCaseDTOList); - } else { - failureDto.setApiTestCases(new ArrayList<>()); - } - if (!faliureScenarioCaseIdList.isEmpty()) { - TestPlanScenarioCaseService testPlanScenarioCaseService = CommonBeanFactory.getBean(TestPlanScenarioCaseService.class); - - TestPlanScenarioRequest request = new TestPlanScenarioRequest(); - request.setPlanId(testPlan.getId()); - request.setScenarioIds(faliureScenarioCaseIdList); - List scenarioDTOS = testPlanScenarioCaseService.list(request); - failureDto.setScenarioTestCases(scenarioDTOS); - } else { - failureDto.setScenarioTestCases(new ArrayList<>()); - } - if (!faliureLoadCaseIdList.isEmpty()) { - TestPlanLoadCaseService testPlanLoadCaseService = CommonBeanFactory.getBean(TestPlanLoadCaseService.class); - LoadCaseRequest request = new LoadCaseRequest(); - request.setTestPlanId(testPlan.getId()); - request.setIds(faliureLoadCaseIdList); - List loadDTOs = testPlanLoadCaseService.list(request); - failureDto.setLoadTestCases(loadDTOs); - } else { - failureDto.setLoadTestCases(new ArrayList<>()); - } - returnDTO.setFailureTestCases(failureDto); - - return returnDTO; - } - - public TestPlanReport updateReport(TestPlanReport testPlanReport, TestPlanReportContentWithBLOBs reportContent, TestPlanExecuteInfo executeInfo) { - if (testPlanReport == null || executeInfo == null) { + public TestPlanReportContentWithBLOBs updateReport(TestPlanReport testPlanReport, TestPlanReportContentWithBLOBs reportContent) { + if (testPlanReport == null) { return null; } + TestPlanService testPlanService = CommonBeanFactory.getBean(TestPlanService.class); + TestPlanSimpleReportDTO reportDTO = testPlanService.buildPlanReport(testPlanReport, reportContent); + reportDTO.setStartTime(testPlanReport.getStartTime()); + reportContent = parseReportDaoToReportContent(reportDTO, reportContent); + testPlanReportContentMapper.updateByPrimaryKeySelective(reportContent); + testPlanReportMapper.updateByPrimaryKey(testPlanReport); - boolean apiCaseIsOk = executeInfo.isApiCaseAllExecuted(); - boolean scenarioIsOk = executeInfo.isScenarioAllExecuted(); - boolean performanceIsOk = executeInfo.isLoadCaseAllExecuted(); - - if (apiCaseIsOk) { - testPlanReport.setIsApiCaseExecuting(false); - } - if (scenarioIsOk) { - testPlanReport.setIsScenarioExecuting(false); - } - if (performanceIsOk) { - testPlanReport.setIsPerformanceExecuting(false); - } + return reportContent; + } + public TestPlanReport finishedTestPlanReport(String testPlanReportId) { + TestPlanReport testPlanReport = this.getTestPlanReport(testPlanReportId); + //初始化测试计划包含组件信息 int[] componentIndexArr = new int[]{1, 3, 4}; testPlanReport.setComponents(JSONArray.toJSONString(componentIndexArr)); - - TestPlanService testPlanService = CommonBeanFactory.getBean(TestPlanService.class); - - Map> testPlanExecuteResult = executeInfo.getExecutedResult(); - testPlanLog.info("ReportId[" + testPlanReport.getId() + "] COUNT OVER. COUNT RESULT :" + JSONObject.toJSONString(testPlanExecuteResult)); - - TestPlanSimpleReportDTO reportDTO = testPlanService.buildPlanReport(executeInfo, testPlanReport.getTestPlanId(), apiCaseIsOk && scenarioIsOk && performanceIsOk); - reportDTO.setStartTime(testPlanReport.getStartTime()); - long endTime = System.currentTimeMillis(); - //全部结束时,更新时间 - if (apiCaseIsOk && scenarioIsOk && performanceIsOk) { - reportDTO.setEndTime(endTime); - //如果测试案例没有未结束的功能用例,则更新最后结束日期。 - TestPlanTestCaseMapper testPlanTestCaseMapper = CommonBeanFactory.getBean(TestPlanTestCaseMapper.class); - TestPlanTestCaseExample testPlanTestCaseExample = new TestPlanTestCaseExample(); - testPlanTestCaseExample.createCriteria().andPlanIdEqualTo(testPlanReport.getTestPlanId()).andStatusNotEqualTo("Prepare"); - long testCaseCount = testPlanTestCaseMapper.countByExample(testPlanTestCaseExample); - boolean updateTestPlanTime = testCaseCount > 0; - if (updateTestPlanTime) { - testPlanReport.setEndTime(endTime); - testPlanReport.setUpdateTime(endTime); - } - TestPlanReportExecuteCatch.remove(testPlanReport.getId()); - testPlanLog.info("Task is finish. Remove listener:" + testPlanReport.getId()); - } - testPlanReportContentMapper.updateByPrimaryKeySelective(parseReportDaoToReportContent(reportDTO, reportContent)); - - String testPlanStatus = this.getTestPlanReportStatus(testPlanReport, reportDTO); + //计算测试计划状态 + String testPlanStatus = this.getTestPlanReportStatus(testPlanReport, null); testPlanReport.setStatus(testPlanStatus); + //如果测试案例没有未结束的功能用例,则更新最后结束日期。 + TestPlanTestCaseMapper testPlanTestCaseMapper = CommonBeanFactory.getBean(TestPlanTestCaseMapper.class); + TestPlanTestCaseExample testPlanTestCaseExample = new TestPlanTestCaseExample(); + testPlanTestCaseExample.createCriteria().andPlanIdEqualTo(testPlanReport.getTestPlanId()).andStatusNotEqualTo("Prepare"); + long endTime = System.currentTimeMillis(); + long testCaseCount = testPlanTestCaseMapper.countByExample(testPlanTestCaseExample); + boolean updateTestPlanTime = testCaseCount > 0; + if (updateTestPlanTime) { + testPlanReport.setEndTime(endTime); + testPlanReport.setUpdateTime(endTime); + } + //更新测试计划并发送通知 + testPlanReport.setIsApiCaseExecuting(false); + testPlanReport.setIsScenarioExecuting(false); + testPlanReport.setIsPerformanceExecuting(false); testPlanReport = this.update(testPlanReport); return testPlanReport; } @@ -604,7 +353,7 @@ public class TestPlanReportService { * @param resourceRunMode 资源的运行模式,triggerMode非Scedule可以为null * @param triggerMode 触发方式 ReportTriggerMode.enum */ - public void countReportByTestPlanReportId(String planReportId, String resourceRunMode, String triggerMode, List scenarioIdList) { + public void countReportByTestPlanReportId(String planReportId, String resourceRunMode, String triggerMode) { TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(planReportId); QueryTestPlanRequest queryTestPlanRequest = new QueryTestPlanRequest(); @@ -656,8 +405,10 @@ public class TestPlanReportService { public TestPlanReportContentWithBLOBs parseReportDaoToReportContent(TestPlanSimpleReportDTO reportDTO, TestPlanReportContentWithBLOBs testPlanReportContentWithBLOBs) { String id = testPlanReportContentWithBLOBs.getId(); + String testPlanReportId = testPlanReportContentWithBLOBs.getTestPlanReportId(); BeanUtils.copyBean(testPlanReportContentWithBLOBs, reportDTO); testPlanReportContentWithBLOBs.setId(id); + testPlanReportContentWithBLOBs.setTestPlanReportId(testPlanReportId); if (reportDTO.getFunctionResult() != null) { testPlanReportContentWithBLOBs.setFunctionResult(JSONObject.toJSONString(reportDTO.getFunctionResult())); } @@ -730,49 +481,6 @@ public class TestPlanReportService { return status; } - 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) { - JSONArray array = failurCaseObject.getJSONArray("apiTestCases"); - if (array.size() > 0) { - status = TestPlanReportStatus.FAILED.name(); - return status; - } - } - if (failurCaseObject.containsKey("loadTestCases") && failurCaseObject.getJSONArray("loadTestCases").size() >= 0) { - JSONArray array = failurCaseObject.getJSONArray("loadTestCases"); - if (array.size() > 0) { - status = TestPlanReportStatus.FAILED.name(); - return status; - } - } - if (failurCaseObject.containsKey("scenarioTestCases") && failurCaseObject.getJSONArray("scenarioTestCases").size() >= 0) { - JSONArray array = failurCaseObject.getJSONArray("scenarioTestCases"); - if (array.size() > 0) { - status = TestPlanReportStatus.FAILED.name(); - return status; - } - } - } catch (Exception e) { - status = TestPlanReportStatus.FAILED.name(); - } - } else { - status = TestPlanReportStatus.COMPLETED.name(); - } - } - } - return status; - } - public TestPlanReport update(TestPlanReport report) { if (!report.getIsApiCaseExecuting() && !report.getIsPerformanceExecuting() && !report.getIsScenarioExecuting()) try { @@ -868,88 +576,6 @@ public class TestPlanReportService { return testPlanReportMapper.selectByPrimaryKey(planId); } - /** - * 更新TestPlanReportData的PerformanceInfo - * - * @param testPlanReport - */ - public void updatePerformanceInfo(TestPlanReport testPlanReport, Map performaneReportIDMap, String triggerMode) { - - /** - * 虽然kafka已经设置了topic推送,但是当执行机器性能不够时会影响到报告状态当修改 - * 同时如果执行过程中报告删除,那么此时也应当记为失败。 - */ - Map finishLoadTestId = new HashMap<>(); - Map caseReportMap = new HashMap<>(); - executorService.submit(() -> { - //错误数据检查集合。 如果错误数据出现超过20次,则取消该条数据的检查 - Map errorDataCheckMap = new HashMap<>(); - List performaneReportIDList = new ArrayList<>(performaneReportIDMap.keySet()); - while (performaneReportIDList.size() > 0) { - List selectList = new ArrayList<>(performaneReportIDList); - testPlanLog.info("TestPlanReportId[" + testPlanReport.getId() + "] SELECT performance BATCH START:" + JSONArray.toJSONString(selectList)); - for (String loadTestReportId : selectList) { - try { - LoadTestReportWithBLOBs loadTestReportFromDatabase = loadTestReportMapper.selectByPrimaryKey(loadTestReportId); - if (loadTestReportFromDatabase == null) { - testPlanLog.info("TestPlanReportId[" + testPlanReport.getId() + "] SELECT performance ID:" + loadTestReportId + ",RESULT IS NULL"); - //检查错误数据 - if (errorDataCheckMap.containsKey(loadTestReportId)) { - if (errorDataCheckMap.get(loadTestReportId) > 10) { - performaneReportIDList.remove(loadTestReportId); - if (performaneReportIDMap.containsKey(loadTestReportId)) { - finishLoadTestId.put(performaneReportIDMap.get(loadTestReportId), TestPlanLoadCaseStatus.error.name()); - caseReportMap.put(performaneReportIDMap.get(loadTestReportId), loadTestReportId); - } - } else { - errorDataCheckMap.put(loadTestReportId, errorDataCheckMap.get(loadTestReportId) + 1); - } - } else { - errorDataCheckMap.put(loadTestReportId, 1); - } - } else { - testPlanLog.info("TestPlanReportId[" + testPlanReport.getId() + "] SELECT performance ID:" + loadTestReportId + ",RESULT :" + loadTestReportFromDatabase.getStatus()); - if (StringUtils.equalsAny(loadTestReportFromDatabase.getStatus(), - PerformanceTestStatus.Completed.name(), PerformanceTestStatus.Error.name())) { - finishLoadTestId.put(performaneReportIDMap.get(loadTestReportId), TestPlanLoadCaseStatus.success.name()); - caseReportMap.put(performaneReportIDMap.get(loadTestReportId), loadTestReportId); - performaneReportIDList.remove(loadTestReportId); - } - } - } catch (Exception e) { - performaneReportIDList.remove(loadTestReportId); - finishLoadTestId.put(performaneReportIDMap.get(loadTestReportId), TestPlanLoadCaseStatus.error.name()); - caseReportMap.put(performaneReportIDMap.get(loadTestReportId), loadTestReportId); - testPlanLog.error(e.getMessage()); - } - } - testPlanLog.info("TestPlanReportId[" + testPlanReport.getId() + "] SELECT performance BATCH OVER:" + JSONArray.toJSONString(selectList)); - if (performaneReportIDList.isEmpty()) { - testPlanLog.info("TestPlanReportId[" + testPlanReport.getId() + "] performance EXECUTE OVER. TRIGGER_MODE:" + triggerMode + ",REsult:" + JSONObject.toJSONString(finishLoadTestId)); - if (StringUtils.equalsAnyIgnoreCase(triggerMode, ReportTriggerMode.API.name() ,ReportTriggerMode.MANUAL.name())) { - for (String string : finishLoadTestId.keySet()) { - String reportId = caseReportMap.get(string); - TestPlanLoadCaseWithBLOBs updateDTO = new TestPlanLoadCaseWithBLOBs(); - updateDTO.setId(string); - updateDTO.setStatus(finishLoadTestId.get(string)); - updateDTO.setLoadReportId(reportId); - testPlanLoadCaseMapper.updateByPrimaryKeySelective(updateDTO); - } - } - TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(testPlanReport.getId(), null, null, finishLoadTestId); - TestPlanReportExecuteCatch.updateTestPlanThreadInfo(testPlanReport.getId(), null, null, caseReportMap); - } else { - try { - //查询定时任务是否关闭 - Thread.sleep(1000 * 10);// 检查 loadtest 的状态 - } catch (InterruptedException e) { - } - } - } - return true; - }); - } - public void updatePerformanceTestStatus(TestPlanLoadCaseEventDTO eventDTO) { List testPlanReportId = extTestPlanMapper.findIdByPerformanceReportId(eventDTO.getReportId()); if (StringUtils.equals(eventDTO.getTriggerMode(), ReportTriggerMode.API.name())) { @@ -1028,44 +654,6 @@ public class TestPlanReportService { return null; } - public synchronized TestPlanReport updateExecuteApis(String planReportId) { - - TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId); - if (executeInfo == null) { - return null; - } - Map executeApiCaseIdMap = executeInfo.getApiCaseExecInfo(); - Map executeScenarioCaseIdMap = executeInfo.getApiScenarioCaseExecInfo(); - Map executePerformanceIdMap = executeInfo.getLoadCaseExecInfo(); - if (executeApiCaseIdMap == null) { - executeApiCaseIdMap = new HashMap<>(); - } - if (executeScenarioCaseIdMap == null) { - executeScenarioCaseIdMap = new HashMap<>(); - } - if (executePerformanceIdMap == null) { - executePerformanceIdMap = new HashMap<>(); - } - - testPlanLog.info("ReportId[" + planReportId + "] Executed. api :" + JSONObject.toJSONString(executeApiCaseIdMap) + "; scenario:" + JSONObject.toJSONString(executeScenarioCaseIdMap) + "; performance:" + JSONObject.toJSONString(executePerformanceIdMap)); - - TestPlanReportContentExample example = new TestPlanReportContentExample(); - example.createCriteria().andTestPlanReportIdEqualTo(planReportId); - List reportDataList = testPlanReportContentMapper.selectByExampleWithBLOBs(example); - - TestPlanReport report = null; - if (!reportDataList.isEmpty()) { - TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId, true); - TestPlanReportContentWithBLOBs reportData = reportDataList.get(0); - report = testPlanReportMapper.selectByPrimaryKey(planReportId); - report = this.updateReport(report, reportData, executeInfo); - } else { - TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId, false); - } - - return report; - } - public void deleteByPlanId(String planId) { TestPlanReportExample example = new TestPlanReportExample(); example.createCriteria().andTestPlanIdEqualTo(planId); @@ -1077,19 +665,6 @@ public class TestPlanReportService { this.delete(testPlanReportIdList); } - public void countReport(String planReportId) { - TestPlanReportExecuteCheckResultDTO checkResult = this.checkTestPlanReportIsTimeOut(planReportId); - testPlanLog.info("Check PlanReport:" + planReportId + "; result: "+ JSON.toJSONString(checkResult)); - if (checkResult.isTimeOut()) { - //判断是否超时。超时时强行停止任务 - TestPlanReportExecuteCatch.finishAllTask(planReportId); - checkResult.setFinishedCaseChanged(true); - } - if(checkResult.isFinishedCaseChanged()){ - this.updateExecuteApis(planReportId); - } - } - public TestPlanSimpleReportDTO getReport(String reportId) { TestPlanReportContentExample example = new TestPlanReportContentExample(); example.createCriteria().andTestPlanReportIdEqualTo(reportId); @@ -1098,8 +673,12 @@ public class TestPlanReportService { return null; } TestPlanReportContentWithBLOBs testPlanReportContent = testPlanReportContents.get(0); - //更新测试报告对应的最后执行结果 - this.updateReportExecResult(testPlanReportContent); + if (testPlanReportContent == null) { + return null; + } + if (this.isDynamicallyGenerateReports(testPlanReportContent)) { + testPlanReportContent = this.dynamicallyGenerateReports(testPlanReportContent); + } TestPlanSimpleReportDTO testPlanReportDTO = new TestPlanSimpleReportDTO(); BeanUtils.copyBean(testPlanReportDTO, testPlanReportContent); if (StringUtils.isNotBlank(testPlanReportContent.getFunctionResult())) { @@ -1144,149 +723,65 @@ public class TestPlanReportService { return testPlanReportDTO; } - private void updateReportExecResult(TestPlanReportContentWithBLOBs testPlanReportContent) { - boolean isUpdate = false; - boolean isTaskRunning = false; - boolean reportHasData = false; - if (StringUtils.isNotBlank(testPlanReportContent.getApiAllCases())) { - reportHasData = true; - List allCases = JSONObject.parseArray(testPlanReportContent.getApiAllCases(), TestPlanFailureApiDTO.class); - for (TestPlanFailureApiDTO dto : allCases) { - String status = dto.getExecResult(); - if (StringUtils.equalsAnyIgnoreCase(status, "Running", "Waiting")) { - isUpdate = true; - ApiDefinitionExecResult definitionExecResult = apiDefinitionExecResultMapper.selectByPrimaryKey(dto.getReportId()); - if (definitionExecResult != null) { - dto.setExecResult(definitionExecResult.getStatus()); - } - } - - if (StringUtils.equalsAnyIgnoreCase(dto.getExecResult(), "Running", "Waiting")) { - isTaskRunning = true; - } - } - testPlanReportContent.setApiAllCases(JSONArray.toJSONString(allCases)); - } - - if (StringUtils.isNotBlank(testPlanReportContent.getScenarioAllCases())) { - reportHasData = true; - List allCases = JSONObject.parseArray(testPlanReportContent.getScenarioAllCases(), TestPlanFailureScenarioDTO.class); - for (TestPlanFailureScenarioDTO dto : allCases) { - String lastResult = dto.getLastResult(); - if (StringUtils.equalsAnyIgnoreCase(lastResult, "Running", "Waiting", "Underway")) { - isUpdate = true; - ApiScenarioReport apiReport = apiScenarioReportMapper.selectByPrimaryKey(dto.getReportId()); - if (apiReport != null) { - dto.setLastResult(apiReport.getStatus()); - dto.setStatus(apiReport.getStatus()); - } - } else if (StringUtils.equalsAnyIgnoreCase("Error", lastResult)) { - isUpdate = true; - dto.setLastResult("Fail"); - dto.setStatus("Fail"); - } - - if (StringUtils.equalsAnyIgnoreCase(dto.getLastResult(), "Running", "Waiting", "Underway")) { - isTaskRunning = true; - } - } - testPlanReportContent.setScenarioAllCases(JSONArray.toJSONString(allCases)); - } - - if (StringUtils.isNotBlank(testPlanReportContent.getLoadAllCases())) { - List allCases = JSONObject.parseArray(testPlanReportContent.getLoadAllCases(), TestPlanLoadCaseDTO.class); - if(!allCases.isEmpty()){ - isTaskRunning = true; - } - } - if (isUpdate) { - testPlanReportContentMapper.updateByPrimaryKeyWithBLOBs(testPlanReportContent); - } - - if (!isTaskRunning && reportHasData) { - this.finishTestPlanReport(testPlanReportContent.getTestPlanReportId()); - } + private boolean isDynamicallyGenerateReports(TestPlanReportContentWithBLOBs testPlanReportContent) { + return testPlanReportContent != null && + (StringUtils.isNotEmpty(testPlanReportContent.getApiCaseReportId()) || StringUtils.isNotEmpty(testPlanReportContent.getApiCaseReportId()) || StringUtils.isNotEmpty(testPlanReportContent.getApiCaseReportId())); } - public void finishReport(TestPlanReport testPlanReport) { - long endTime = System.currentTimeMillis(); - testPlanReport.setEndTime(endTime); - testPlanReport.setUpdateTime(endTime); - - testPlanReport.setStatus(TestPlanReportStatus.FAILED.name()); - testPlanReportMapper.updateByPrimaryKeySelective(testPlanReport); - - TestPlanReportContentWithBLOBs bloBs = new TestPlanReportContentWithBLOBs(); - bloBs.setEndTime(endTime); - TestPlanReportContentExample example = new TestPlanReportContentExample(); - example.createCriteria().andTestPlanReportIdEqualTo(testPlanReport.getId()); - testPlanReportContentMapper.updateByExampleSelective(bloBs,example); + private TestPlanReportContentWithBLOBs dynamicallyGenerateReports(TestPlanReportContentWithBLOBs testPlanReportContent) { + TestPlanReport report = testPlanReportMapper.selectByPrimaryKey(testPlanReportContent.getTestPlanReportId()); + testPlanReportContent = this.updateReport(report, testPlanReportContent); + return testPlanReportContent; } - private TestPlanReportExecuteCheckResultDTO checkTestPlanReportIsTimeOut(String planReportId) { - //同步数据库更新状态信息 - try { - this.syncReportStatus(planReportId); - } catch (Exception e) { - LogUtil.info("联动数据库同步执行状态失败! " + e.getMessage()); - LogUtil.error(e); - } - TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId); - TestPlanReportExecuteCheckResultDTO checkResult = executeInfo.countUnFinishedNum(); - return checkResult; - } + public synchronized void updateTestPlanReportContentReportIds(String testPlanReportContentId, Map apiCaseReportMap, Map scenarioReportIdMap, Map loadCaseReportIdMap) { + if (StringUtils.isNotEmpty(testPlanReportContentId)) { + TestPlanReportContentWithBLOBs content = new TestPlanReportContentWithBLOBs(); + content.setId(testPlanReportContentId); - private void syncReportStatus(String planReportId) { - if (TestPlanReportExecuteCatch.containsReport(planReportId)) { - TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId); - if (executeInfo != null) { - //同步接口案例结果 - Map updateCaseStatusMap = new HashMap<>(); - Map apiCaseReportMap = executeInfo.getRunningApiCaseReportMap(); - if (MapUtils.isNotEmpty(apiCaseReportMap)) { - List execList = extApiDefinitionExecResultMapper.selectStatusByIdList(apiCaseReportMap.keySet()); - for (ApiDefinitionExecResult report : execList) { - String reportId = report.getId(); - String status = report.getStatus(); - if (!StringUtils.equalsAnyIgnoreCase(status, "Running", "Waiting")) { - String planCaseId = apiCaseReportMap.get(reportId); - if (StringUtils.isNotEmpty(planCaseId)) { - updateCaseStatusMap.put(planCaseId, status); - } - } - } - } - //同步场景结果 - Map updateScenarioStatusMap = new HashMap<>(); - Map scenarioReportMap = executeInfo.getRunningScenarioReportMap(); - if (MapUtils.isNotEmpty(scenarioReportMap)) { - List reportList = extApiScenarioReportMapper.selectStatusByIds(scenarioReportMap.keySet()); - for (ApiScenarioReport report : reportList) { - String reportId = report.getId(); - String status = report.getStatus(); - if (!StringUtils.equalsAnyIgnoreCase(status, "Running", "Waiting")) { - String planScenarioId = scenarioReportMap.get(reportId); - if (StringUtils.isNotEmpty(planScenarioId)) { - updateScenarioStatusMap.put(planScenarioId, status); - } - } - } - } - testPlanLog.info("ReportID:"+planReportId+" 本次数据库同步,案例ID:"+JSON.toJSONString(apiCaseReportMap.keySet())+";场景ID:"+JSON.toJSONString(scenarioReportMap.keySet())+"; 同步结果,案例:"+JSON.toJSONString(updateCaseStatusMap)+";场景:"+JSON.toJSONString(updateScenarioStatusMap)); - TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(planReportId, updateCaseStatusMap, updateScenarioStatusMap, null); - }else { - testPlanLog.info("同步数据库查询执行信息失败! 报告ID在缓存中未找到!"+planReportId); + if (MapUtils.isNotEmpty(apiCaseReportMap)) { + content.setApiCaseReportId(JSONObject.toJSONString(apiCaseReportMap)); + } + if (MapUtils.isNotEmpty(scenarioReportIdMap)) { + content.setScenarioReportId(JSONObject.toJSONString(scenarioReportIdMap)); + } + if (MapUtils.isNotEmpty(loadCaseReportIdMap)) { + content.setLoadCaseReportId(JSONObject.toJSONString(loadCaseReportIdMap)); + } + + if (StringUtils.isNotEmpty(content.getApiCaseReportId()) || StringUtils.isNotEmpty(content.getScenarioReportId()) || StringUtils.isNotEmpty(content.getLoadCaseReportId())) { + testPlanReportContentMapper.updateByPrimaryKeySelective(content); } } } - private void finishTestPlanReport(String planReportId) { - TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(planReportId); - if (testPlanReport != null && StringUtils.equalsIgnoreCase("Running", testPlanReport.getStatus())) { - this.finishReport(testPlanReport); - testPlanLog.info("结束测试计划报告:[" + planReportId + "]"); + public TestPlanExecuteReportDTO genTestPlanExecuteReportDTOByTestPlanReportContent(TestPlanReportContentWithBLOBs testPlanReportContentWithBLOBs) { + Map testPlanApiCaseIdAndReportIdMap = new HashMap<>(); + Map testPlanScenarioIdAndReportIdMap = new HashMap<>(); + Map testPlanLoadCaseIdAndReportIdMap = new HashMap<>(); + + if (testPlanReportContentWithBLOBs != null) { + if (StringUtils.isNotEmpty(testPlanReportContentWithBLOBs.getApiCaseReportId())) { + try { + testPlanApiCaseIdAndReportIdMap = JSONObject.parseObject(testPlanReportContentWithBLOBs.getApiCaseReportId(), Map.class); + } catch (Exception ignore) { + } + } + if (StringUtils.isNotEmpty(testPlanReportContentWithBLOBs.getScenarioReportId())) { + try { + testPlanScenarioIdAndReportIdMap = JSONObject.parseObject(testPlanReportContentWithBLOBs.getScenarioReportId(), Map.class); + } catch (Exception ignore) { + } + } + if (StringUtils.isNotEmpty(testPlanReportContentWithBLOBs.getLoadCaseReportId())) { + try { + testPlanLoadCaseIdAndReportIdMap = JSONObject.parseObject(testPlanReportContentWithBLOBs.getLoadCaseReportId(), Map.class); + } catch (Exception ignore) { + } + } } - TestPlanReportExecuteCatch.remove(planReportId); + TestPlanExecuteReportDTO returnDTO = new TestPlanExecuteReportDTO(testPlanApiCaseIdAndReportIdMap, testPlanScenarioIdAndReportIdMap, testPlanLoadCaseIdAndReportIdMap); + return returnDTO; } public void cleanUpReport(long time, String projectId) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java index b37e7329a6..b9e0056181 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java @@ -509,14 +509,11 @@ public class TestPlanScenarioCaseService { return buildCases(apiTestCases); } - public List getAllCases(Map idMap, boolean isFinish) { + public List getAllCases(Map idMap) { List apiTestCases = extTestPlanScenarioCaseMapper.getFailureListByIds(idMap.keySet(), null); - String defaultStatus = "Running"; - if(isFinish){ - defaultStatus = "Fail"; - } + String defaultStatus = "Fail"; Map reportStatus = apiScenarioReportService.getReportStatusByReportIds(idMap.values()); for (TestPlanFailureScenarioDTO dto: apiTestCases) { String reportId = idMap.get(dto.getId()); 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 7499baea2a..2d5b9c0fbf 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -8,8 +8,6 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; -import io.metersphere.api.cache.TestPlanExecuteInfo; -import io.metersphere.api.cache.TestPlanReportExecuteCatch; import io.metersphere.api.dto.APIReportResult; import io.metersphere.api.dto.EnvironmentType; import io.metersphere.api.dto.automation.*; @@ -21,8 +19,6 @@ import io.metersphere.api.dto.definition.request.MsScenario; import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.api.dto.definition.request.MsThreadGroup; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; -import io.metersphere.api.jmeter.JMeterService; -import io.metersphere.api.jmeter.MessageCache; import io.metersphere.api.service.ApiAutomationService; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiScenarioReportService; @@ -134,8 +130,6 @@ public class TestPlanService { @Resource private TestPlanLoadCaseService testPlanLoadCaseService; @Resource - private JMeterService jMeterService; - @Resource private ApiAutomationService apiAutomationService; @Resource private ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper; @@ -151,8 +145,6 @@ public class TestPlanService { @Resource private ApiTestCaseMapper apiTestCaseMapper; @Resource - private ApiDefinitionMapper apiDefinitionMapper; - @Resource private TestPlanApiCaseMapper testPlanApiCaseMapper; @Resource private TestPlanApiScenarioMapper testPlanApiScenarioMapper; @@ -953,7 +945,6 @@ public class TestPlanService { public List scenarioRunModeConfig(SchedulePlanScenarioExecuteRequest planScenarioExecuteRequest) { Map> testPlanScenarioIdMap = planScenarioExecuteRequest.getTestPlanScenarioIDMap(); List list = new LinkedList<>(); - String returnStr = null; for (Map.Entry> entry : testPlanScenarioIdMap.entrySet()) { Map scenarioMap = entry.getValue(); @@ -969,6 +960,7 @@ public class TestPlanService { request.setConfig(planScenarioExecuteRequest.getConfig()); request.setTestPlanScheduleJob(true); request.setTestPlanReportId(planScenarioExecuteRequest.getTestPlanReportId()); + request.setTestPlanReportContentId(planScenarioExecuteRequest.getPlanReportContentId()); request.setId(UUID.randomUUID().toString()); request.setProjectId(planScenarioExecuteRequest.getProjectId()); request.setRequestOriginator("TEST_PLAN"); @@ -1066,101 +1058,38 @@ public class TestPlanService { runModeConfig.setEnvMap(new HashMap<>()); } } + //创建测试报告,然后返回的ID重新赋值为resourceID,作为后续的参数 TestPlanScheduleReportInfoDTO reportInfoDTO = this.genTestPlanReport(projectID, testPlanID, userId, triggerMode); - TestPlanReport testPlanReport = reportInfoDTO.getTestPlanReport(); - Map planScenarioIdsMap = reportInfoDTO.getPlanScenarioIdMap(); - Map planApiCaseMap = reportInfoDTO.getApiTestCaseDataMap(); - Map performanceIdMap = reportInfoDTO.getPerformanceIdMap(); + + //测试计划准备执行,取消测试计划的实际结束时间 extTestPlanMapper.updateActualEndTimeIsNullById(testPlanID); - String planReportId = testPlanReport.getId(); + + String planReportId = reportInfoDTO.getTestPlanReport().getId(); testPlanLog.info("ReportId[" + planReportId + "] created. TestPlanID:[" + testPlanID + "]. " + "API Run Config:【" + apiRunConfig + "】"); - //开启测试计划执行状态的监听 - MessageCache.jobReportCache.add(planReportId); - //不同任务的执行ID - Map executePerformanceIdMap = new HashMap<>(); - Map executeApiCaseIdMap = new HashMap<>(); - Map executeScenarioCaseIdMap = new HashMap<>(); - //执行性能测试任务 - Map performaneReportIDMap = new LinkedHashMap<>(); - Map performaneThreadIDMap = new LinkedHashMap<>(); - for (Map.Entry entry : performanceIdMap.entrySet()) { - String id = entry.getKey(); - String caseID = entry.getValue(); - RunTestPlanRequest performanceRequest = new RunTestPlanRequest(); - performanceRequest.setId(caseID); - performanceRequest.setTestPlanLoadId(id); - if (StringUtils.isNotBlank(runModeConfig.getResourcePoolId())) { - performanceRequest.setTestResourcePoolId(runModeConfig.getResourcePoolId()); - } - if (StringUtils.equals(ReportTriggerMode.API.name(), triggerMode)) { - performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_API.name()); - } else if (StringUtils.equals(ReportTriggerMode.MANUAL.name(), triggerMode)) { - performanceRequest.setTriggerMode(ReportTriggerMode.MANUAL.name()); - } else { - performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_SCHEDULE.name()); - } - String reportId = null; - try { - reportId = performanceTestService.run(performanceRequest); - if (reportId != null) { - performaneReportIDMap.put(reportId, id); - //更新关联处的报告 - TestPlanLoadCaseWithBLOBs loadCase = new TestPlanLoadCaseDTO(); - loadCase.setId(id); - loadCase.setLoadReportId(reportId); - loadCase.setStatus(TestPlanLoadCaseStatus.run.name()); - testPlanLoadCaseService.update(loadCase); - } - } catch (Exception e) { - TestPlanLoadCaseWithBLOBs testPlanLoadCase = new TestPlanLoadCaseWithBLOBs(); - testPlanLoadCase.setId(id); - testPlanLoadCase.setLoadReportId(reportId); - testPlanLoadCase.setStatus(TestPlanLoadCaseStatus.error.name()); - testPlanLoadCaseService.update(testPlanLoadCase); - LogUtil.error(e); - } - if (StringUtils.isNotEmpty(reportId)) { - performaneThreadIDMap.put(performanceRequest.getTestPlanLoadId(), reportId); - executePerformanceIdMap.put(performanceRequest.getTestPlanLoadId(), TestPlanApiExecuteStatus.RUNNING.name()); - } else { - executePerformanceIdMap.put(performanceRequest.getTestPlanLoadId(), TestPlanApiExecuteStatus.PREPARE.name()); - } - } - TestPlanReportExecuteCatch.updateTestPlanThreadInfo(planReportId, null, null, performaneThreadIDMap); - if (!performaneReportIDMap.isEmpty()) { - //性能测试时保存性能测试报告ID,在结果返回时用于捕捉并进行 - testPlanReportService.updatePerformanceInfo(testPlanReport, performaneReportIDMap, triggerMode); - } - for (Map.Entry entry : planApiCaseMap.entrySet()) { - String id = entry.getKey(); - executeApiCaseIdMap.put(id, TestPlanApiExecuteStatus.RUNNING.name()); - } - for (String id : planScenarioIdsMap.keySet()) { - 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)); - TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(planReportId, executeApiCaseIdMap, executeScenarioCaseIdMap, executePerformanceIdMap); //执行接口案例任务 - this.executeApiTestCase(triggerMode, planReportId, userId, new ArrayList<>(planApiCaseMap.keySet()), runModeConfig); + this.executeApiTestCase(triggerMode, planReportId, reportInfoDTO.getTestPlanReportContent().getId(), userId, new ArrayList<>(reportInfoDTO.getApiTestCaseDataMap().keySet()), runModeConfig); //执行场景执行任务 - this.executeScenarioCase(planReportId, testPlanID, projectID, runModeConfig, triggerMode, userId, planScenarioIdsMap); - return testPlanReport.getId(); + this.executeScenarioCase(planReportId, reportInfoDTO.getTestPlanReportContent().getId(), testPlanID, projectID, runModeConfig, triggerMode, userId, reportInfoDTO.getPlanScenarioIdMap()); + //执行性能测试任务 + this.executeLoadCaseTask(runModeConfig, triggerMode, reportInfoDTO.getPerformanceIdMap(), reportInfoDTO.getTestPlanReportContent().getId()); + return planReportId; } - private void executeApiTestCase(String triggerMode, String planReportId, String userId, List planCaseIds, RunModeConfigDTO runModeConfig) { + private void executeApiTestCase(String triggerMode, String planReportId, String reportContentId, String userId, List planCaseIds, RunModeConfigDTO runModeConfig) { BatchRunDefinitionRequest request = new BatchRunDefinitionRequest(); request.setTriggerMode(triggerMode); request.setPlanIds(planCaseIds); request.setPlanReportId(planReportId); request.setConfig(runModeConfig); request.setUserId(userId); + request.setPlanReportContentId(reportContentId); testPlanApiCaseService.run(request); } - private void executeScenarioCase(String planReportId, String testPlanID, String projectID, RunModeConfigDTO runModeConfig, String triggerMode, String userId, Map planScenarioIdMap) { + private void executeScenarioCase(String planReportId, String reportContentId, String testPlanID, String projectID, RunModeConfigDTO runModeConfig, String triggerMode, String userId, Map planScenarioIdMap) { if (!planScenarioIdMap.isEmpty()) { SchedulePlanScenarioExecuteRequest scenarioRequest = new SchedulePlanScenarioExecuteRequest(); String senarionReportID = UUID.randomUUID().toString(); @@ -1183,14 +1112,57 @@ public class TestPlanService { scenarioRequest.setTestPlanScenarioIDMap(testPlanScenarioIdMap); scenarioRequest.setReportUserID(userId); scenarioRequest.setTestPlanID(testPlanID); - scenarioRequest.setTestPlanReportId(planReportId); - + scenarioRequest.setPlanReportContentId(reportContentId); scenarioRequest.setConfig(runModeConfig); this.scenarioRunModeConfig(scenarioRequest); } } + private void executeLoadCaseTask(RunModeConfigDTO runModeConfig, String triggerMode, Map performanceIdMap, String reportContentId) { + Map loadCaseReportMap = new HashMap<>(); + + for (Map.Entry entry : performanceIdMap.entrySet()) { + String id = entry.getKey(); + String caseID = entry.getValue(); + RunTestPlanRequest performanceRequest = new RunTestPlanRequest(); + performanceRequest.setId(caseID); + performanceRequest.setTestPlanLoadId(id); + if (StringUtils.isNotBlank(runModeConfig.getResourcePoolId())) { + performanceRequest.setTestResourcePoolId(runModeConfig.getResourcePoolId()); + } + if (StringUtils.equals(ReportTriggerMode.API.name(), triggerMode)) { + performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_API.name()); + } else if (StringUtils.equals(ReportTriggerMode.MANUAL.name(), triggerMode)) { + performanceRequest.setTriggerMode(ReportTriggerMode.MANUAL.name()); + } else { + performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_SCHEDULE.name()); + } + String reportId = null; + try { + reportId = performanceTestService.run(performanceRequest); + if (reportId != null) { + loadCaseReportMap.put(id, reportId); + //更新关联处的报告 + TestPlanLoadCaseWithBLOBs loadCase = new TestPlanLoadCaseDTO(); + loadCase.setId(id); + loadCase.setLoadReportId(reportId); + loadCase.setStatus(TestPlanLoadCaseStatus.run.name()); + testPlanLoadCaseService.update(loadCase); + } + } catch (Exception e) { + TestPlanLoadCaseWithBLOBs testPlanLoadCase = new TestPlanLoadCaseWithBLOBs(); + testPlanLoadCase.setId(id); + testPlanLoadCase.setLoadReportId(reportId); + testPlanLoadCase.setStatus(TestPlanLoadCaseStatus.error.name()); + testPlanLoadCaseService.update(testPlanLoadCase); + LogUtil.error(e); + } + + } + testPlanReportService.updateTestPlanReportContentReportIds(reportContentId, null, null, loadCaseReportMap); + } + public String getLogDetails(String id) { TestPlan plan = testPlanMapper.selectByPrimaryKey(id); if (plan != null) { @@ -1646,22 +1618,22 @@ public class TestPlanService { } } - public void buildApiReport(TestPlanSimpleReportDTO report, JSONObject config, TestPlanExecuteInfo executeInfo, boolean isFinish) { - if (MapUtils.isEmpty(executeInfo.getApiCaseExecInfo()) && MapUtils.isEmpty(executeInfo.getApiScenarioCaseExecInfo())) { + public void buildApiReport(TestPlanSimpleReportDTO report, JSONObject config, TestPlanExecuteReportDTO testPlanExecuteReportDTO) { + if (MapUtils.isEmpty(testPlanExecuteReportDTO.getTestPlanApiCaseIdAndReportIdMap()) && MapUtils.isEmpty(testPlanExecuteReportDTO.getTestPlanScenarioIdAndReportIdMap())) { return; } if (checkReportConfig(config, "api")) { List apiAllCases = null; List scenarioAllCases = null; if (checkReportConfig(config, "api", "all")) { - if (MapUtils.isNotEmpty(executeInfo.getApiCaseExecInfo())) { + if (MapUtils.isNotEmpty(testPlanExecuteReportDTO.getTestPlanApiCaseIdAndReportIdMap())) { // 接口 - apiAllCases = testPlanApiCaseService.getByApiExecReportIds(executeInfo.getApiCaseExecuteThreadMap(), isFinish); + apiAllCases = testPlanApiCaseService.getByApiExecReportIds(testPlanExecuteReportDTO.getTestPlanApiCaseIdAndReportIdMap()); report.setApiAllCases(apiAllCases); } - if (MapUtils.isNotEmpty(executeInfo.getApiScenarioCaseExecInfo())) { + if (MapUtils.isNotEmpty(testPlanExecuteReportDTO.getTestPlanScenarioIdAndReportIdMap())) { //场景 - scenarioAllCases = testPlanScenarioCaseService.getAllCases(executeInfo.getApiScenarioThreadMap(), isFinish); + scenarioAllCases = testPlanScenarioCaseService.getAllCases(testPlanExecuteReportDTO.getTestPlanScenarioIdAndReportIdMap()); report.setScenarioAllCases(scenarioAllCases); } } @@ -1689,17 +1661,16 @@ public class TestPlanService { } } - public void buildLoadReport(TestPlanSimpleReportDTO report, JSONObject config, TestPlanExecuteInfo executeInfo, String planId, boolean saveResponse) { - if (MapUtils.isEmpty(executeInfo.getLoadCaseExecInfo())) { + public void buildLoadReport(TestPlanSimpleReportDTO report, JSONObject config, Map loadCaseReportMap, String planId, boolean saveResponse) { + if (MapUtils.isEmpty(loadCaseReportMap)) { return; } if (checkReportConfig(config, "load")) { List allCases = null; if (checkReportConfig(config, "load", "all")) { - allCases = testPlanLoadCaseService.getAllCases(executeInfo.getLoadCaseExecInfo().keySet(), planId, null); - for (TestPlanLoadCaseDTO dto : - allCases) { - String reportId = executeInfo.getLoadCaseReportIdMap().get(dto.getId()); + allCases = testPlanLoadCaseService.getAllCases(loadCaseReportMap.keySet(), planId, null); + for (TestPlanLoadCaseDTO dto : allCases) { + String reportId = loadCaseReportMap.get(dto.getId()); dto.setReportId(reportId); } if (saveResponse) { @@ -1720,18 +1691,19 @@ public class TestPlanService { } } - public TestPlanSimpleReportDTO buildPlanReport(TestPlanExecuteInfo executeInfo, String planId, boolean isFinish) { - TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(planId); + public TestPlanSimpleReportDTO buildPlanReport(TestPlanReport testPlanReport, TestPlanReportContentWithBLOBs testPlanReportContentWithBLOBs) { + TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(testPlanReport.getTestPlanId()); if (testPlan != null) { String reportConfig = testPlan.getReportConfig(); JSONObject config = null; if (StringUtils.isNotBlank(reportConfig)) { config = JSONObject.parseObject(reportConfig); } - TestPlanSimpleReportDTO report = getReport(planId); - buildFunctionalReport(report, config, planId); - buildApiReport(report, config, executeInfo, isFinish); - buildLoadReport(report, config, executeInfo, planId, false); + TestPlanExecuteReportDTO testPlanExecuteReportDTO = testPlanReportService.genTestPlanExecuteReportDTOByTestPlanReportContent(testPlanReportContentWithBLOBs); + TestPlanSimpleReportDTO report = getReport(testPlanReport.getTestPlanId(), testPlanExecuteReportDTO); + buildFunctionalReport(report, config, testPlanReport.getTestPlanId()); + buildApiReport(report, config, testPlanExecuteReportDTO); + buildLoadReport(report, config, testPlanExecuteReportDTO.getTestPlanLoadCaseIdAndReportIdMap(), testPlanReport.getTestPlanId(), false); return report; } else { return null; @@ -1747,7 +1719,7 @@ public class TestPlanService { if (StringUtils.isNotBlank(reportConfig)) { config = JSONObject.parseObject(reportConfig); } - TestPlanSimpleReportDTO report = getReport(planId); + TestPlanSimpleReportDTO report = getReport(planId, null); buildFunctionalReport(report, config, planId); buildApiReport(report, config, planId, saveResponse); buildLoadReport(report, config, planId, saveResponse); @@ -1824,7 +1796,14 @@ public class TestPlanService { } } - public TestPlanSimpleReportDTO getReport(String planId) { + /** + * 生成测试计划报告并进行统计 + * + * @param planId + * @param testPlanExecuteReportDTO 测试计划各个资源的报告。 (如果为空,则取当前测试计划资源的最新报告) + * @return + */ + public TestPlanSimpleReportDTO getReport(String planId, TestPlanExecuteReportDTO testPlanExecuteReportDTO) { TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(planId); TestPlanSimpleReportDTO report = new TestPlanSimpleReportDTO(); TestPlanFunctionResultReportDTO functionResult = new TestPlanFunctionResultReportDTO(); @@ -1838,9 +1817,22 @@ public class TestPlanService { IssueTemplateDao template = issueTemplateService.getTemplate(testPlan.getProjectId()); testPlanTestCaseService.calculatePlanReport(planId, report); issuesService.calculatePlanReport(planId, report); - testPlanApiCaseService.calculatePlanReport(planId, report); - testPlanScenarioCaseService.calculatePlanReport(planId, report); - testPlanLoadCaseService.calculatePlanReport(planId, report); + if (testPlanExecuteReportDTO == null) { + testPlanApiCaseService.calculatePlanReport(planId, report); + testPlanScenarioCaseService.calculatePlanReport(planId, report); + testPlanLoadCaseService.calculatePlanReport(planId, report); + } else { + if (MapUtils.isNotEmpty(testPlanExecuteReportDTO.getTestPlanApiCaseIdAndReportIdMap())) { + testPlanApiCaseService.calculatePlanReport(new ArrayList<>(testPlanExecuteReportDTO.getTestPlanApiCaseIdAndReportIdMap().values()), report); + } + if (MapUtils.isNotEmpty(testPlanExecuteReportDTO.getTestPlanScenarioIdAndReportIdMap())) { + testPlanScenarioCaseService.calculatePlanReport(new ArrayList<>(testPlanExecuteReportDTO.getTestPlanScenarioIdAndReportIdMap().values()), report); + } + if (MapUtils.isNotEmpty(testPlanExecuteReportDTO.getTestPlanLoadCaseIdAndReportIdMap())) { + testPlanLoadCaseService.calculatePlanReport(new ArrayList<>(testPlanExecuteReportDTO.getTestPlanLoadCaseIdAndReportIdMap().values()), report); + } + } + if (report.getExecuteCount() != 0 && report.getCaseCount() != null) { report.setExecuteRate(report.getExecuteCount() * 0.1 * 10 / report.getCaseCount()); } else { diff --git a/backend/src/main/resources/db/migration/V105__v1.16.5_release.sql b/backend/src/main/resources/db/migration/V105__v1.16.5_release.sql new file mode 100644 index 0000000000..90347a0bb3 --- /dev/null +++ b/backend/src/main/resources/db/migration/V105__v1.16.5_release.sql @@ -0,0 +1,3 @@ +ALTER TABLE test_plan_report_content ADD scenario_report_id longtext NULL; +ALTER TABLE test_plan_report_content ADD api_case_report_id longtext NULL; +ALTER TABLE test_plan_report_content ADD load_case_report_id longtext NULL; \ No newline at end of file diff --git a/backend/src/main/resources/generatorConfig.xml b/backend/src/main/resources/generatorConfig.xml index 7ab1c66b16..5237cbeb8b 100644 --- a/backend/src/main/resources/generatorConfig.xml +++ b/backend/src/main/resources/generatorConfig.xml @@ -71,7 +71,7 @@ - +