feat: 测试计划-报告统计

This commit is contained in:
chenjianxing 2020-12-23 14:40:31 +08:00
parent 9c199766b0
commit 847585e427
7 changed files with 179 additions and 55 deletions

View File

@ -15,7 +15,8 @@ public class ReportComponentFactory {
} else if (StringUtils.equals("2", componentId)) {
return new ReportResultComponent(testPlan);
} else if (StringUtils.equals("3", componentId)) {
return new ReportResultChartComponent(testPlan);
return new ReportResultAdvancedChartComponent(testPlan);
// return new ReportResultChartComponent(testPlan);
} else if (StringUtils.equals("4", componentId)) {
return new ReportFailureResultComponent(testPlan);
}

View File

@ -113,6 +113,11 @@ public class TestPlanController {
return testPlanService.getMetric(planId);
}
@GetMapping("/get/statistics/metric/{planId}")
public TestCaseReportMetricDTO getStatisticsMetric(@PathVariable String planId) {
return testPlanService.getStatisticsMetric(planId);
}
@GetMapping("/project/name/{planId}")
public String getProjectNameByPlanId(@PathVariable String planId) {
return testPlanService.getProjectNameByPlanId(planId);

View File

@ -1,28 +1,48 @@
package io.metersphere.track.domain;
import io.metersphere.api.dto.automation.ApiScenarioDTO;
import io.metersphere.api.dto.definition.TestPlanApiCaseDTO;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.TestPlanTestCaseStatus;
import io.metersphere.track.dto.TestCaseReportMetricDTO;
import io.metersphere.track.dto.TestCaseReportStatusResultDTO;
import io.metersphere.track.dto.TestPlanCaseDTO;
import io.metersphere.track.dto.TestPlanDTO;
import io.metersphere.track.dto.*;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ReportResultChartComponent extends ReportComponent {
Map<String, TestCaseReportStatusResultDTO> reportStatusResultMap = new HashMap<>();
public class ReportResultAdvancedChartComponent extends ReportComponent {
Map<String, TestCaseReportStatusResultDTO> functionalStatusResultMap = new HashMap<>();
Map<String, TestCaseReportStatusResultDTO> apiStatusResultMap = new HashMap<>();
Map<String, TestCaseReportStatusResultDTO> scenarioStatusResultMap = new HashMap<>();
public ReportResultChartComponent(TestPlanDTO testPlan) {
private static Map<String, String> apiResultMap = new HashMap<>();
private static Map<String, String> scenarioResultMap = new HashMap<>();
static {
apiResultMap.put("success", TestPlanTestCaseStatus.Pass.name());
apiResultMap.put("error", TestPlanTestCaseStatus.Failure.name());
scenarioResultMap.put(APITestStatus.Success.name(), TestPlanTestCaseStatus.Pass.name());
scenarioResultMap.put(APITestStatus.Error.name(), TestPlanTestCaseStatus.Failure.name());
}
public ReportResultAdvancedChartComponent(TestPlanDTO testPlan) {
super(testPlan);
componentId = "3";
}
@Override
public void readRecord(TestPlanCaseDTO testCase) {
getStatusResultMap(reportStatusResultMap, testCase);
getStatusResultMap(functionalStatusResultMap, testCase.getStatus());
}
public void readRecord(TestPlanApiCaseDTO testCase) {
getStatusResultMap(apiStatusResultMap, apiResultMap.get(testCase.getExecResult()));
}
public void readRecord(ApiScenarioDTO testCase) {
getStatusResultMap(scenarioStatusResultMap, scenarioResultMap.get(testCase.getLastResult()));
}
@Override
@ -30,31 +50,58 @@ public class ReportResultChartComponent extends ReportComponent {
testCaseReportMetric.setExecuteResult(getReportStatusResult());
}
private List<TestCaseReportStatusResultDTO> getReportStatusResult() {
List<TestCaseReportStatusResultDTO> reportStatusResult = new ArrayList<>();
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Pass.name());
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Failure.name());
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Blocking.name());
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Skip.name());
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Underway.name());
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Prepare.name());
private TestCaseReportAdvanceStatusResultDTO getReportStatusResult() {
TestCaseReportAdvanceStatusResultDTO reportStatusResult = new TestCaseReportAdvanceStatusResultDTO();
buildFunctionalStatusResult(reportStatusResult);
buildApiStatusResult(reportStatusResult);
buildScenarioStatusResult(reportStatusResult);
return reportStatusResult;
}
private void addToReportStatusResultList(List<TestCaseReportStatusResultDTO> reportStatusResultList, String status) {
if (reportStatusResultMap.get(status) != null) {
reportStatusResultList.add(reportStatusResultMap.get(status));
private void buildFunctionalStatusResult(TestCaseReportAdvanceStatusResultDTO reportStatusResult) {
List<TestCaseReportStatusResultDTO> functionalStatusResult = new ArrayList<>();
addToReportStatusResultList(functionalStatusResultMap, functionalStatusResult, TestPlanTestCaseStatus.Pass.name());
addToReportStatusResultList(functionalStatusResultMap, functionalStatusResult, TestPlanTestCaseStatus.Failure.name());
addToReportStatusResultList(functionalStatusResultMap, functionalStatusResult, TestPlanTestCaseStatus.Blocking.name());
addToReportStatusResultList(functionalStatusResultMap, functionalStatusResult, TestPlanTestCaseStatus.Skip.name());
addToReportStatusResultList(functionalStatusResultMap, functionalStatusResult, TestPlanTestCaseStatus.Underway.name());
addToReportStatusResultList(functionalStatusResultMap, functionalStatusResult, TestPlanTestCaseStatus.Prepare.name());
reportStatusResult.setFunctionalResult(functionalStatusResult);
}
private void buildApiStatusResult(TestCaseReportAdvanceStatusResultDTO reportStatusResult) {
List<TestCaseReportStatusResultDTO> apiStatusResult = new ArrayList<>();
addToReportStatusResultList(apiStatusResultMap, apiStatusResult, TestPlanTestCaseStatus.Pass.name());
addToReportStatusResultList(apiStatusResultMap, apiStatusResult, TestPlanTestCaseStatus.Failure.name());
addToReportStatusResultList(apiStatusResultMap, apiStatusResult, TestPlanTestCaseStatus.Underway.name());
reportStatusResult.setApiResult(apiStatusResult);
}
private void buildScenarioStatusResult(TestCaseReportAdvanceStatusResultDTO reportStatusResult) {
List<TestCaseReportStatusResultDTO> scenarioStatusResult = new ArrayList<>();
addToReportStatusResultList(scenarioStatusResultMap, scenarioStatusResult, TestPlanTestCaseStatus.Pass.name());
addToReportStatusResultList(scenarioStatusResultMap, scenarioStatusResult, TestPlanTestCaseStatus.Failure.name());
addToReportStatusResultList(scenarioStatusResultMap, scenarioStatusResult, TestPlanTestCaseStatus.Underway.name());
reportStatusResult.setScenarioResult(scenarioStatusResult);
}
private void addToReportStatusResultList(Map<String, TestCaseReportStatusResultDTO> resultMap, List<TestCaseReportStatusResultDTO> reportStatusResultList, String status) {
if (resultMap.get(status) != null) {
reportStatusResultList.add(resultMap.get(status));
}
}
private void getStatusResultMap(Map<String, TestCaseReportStatusResultDTO> reportStatusResultMap, TestPlanCaseDTO testCase) {
TestCaseReportStatusResultDTO statusResult = reportStatusResultMap.get(testCase.getStatus());
private void getStatusResultMap(Map<String, TestCaseReportStatusResultDTO> reportStatusResultMap, String result) {
if (StringUtils.isBlank(result)) {
result = TestPlanTestCaseStatus.Underway.name();
}
TestCaseReportStatusResultDTO statusResult = reportStatusResultMap.get(result);
if (statusResult == null) {
statusResult = new TestCaseReportStatusResultDTO();
statusResult.setStatus(testCase.getStatus());
statusResult.setStatus(result);
statusResult.setCount(0);
}
statusResult.setCount(statusResult.getCount() + 1);
reportStatusResultMap.put(testCase.getStatus(), statusResult);
reportStatusResultMap.put(result, statusResult);
}
}

View File

@ -27,7 +27,7 @@ public class ReportResultChartComponent extends ReportComponent {
@Override
public void afterBuild(TestCaseReportMetricDTO testCaseReportMetric) {
testCaseReportMetric.setExecuteResult(getReportStatusResult());
// testCaseReportMetric.setExecuteResult(getReportStatusResult());
}
private List<TestCaseReportStatusResultDTO> getReportStatusResult() {

View File

@ -3,10 +3,13 @@ package io.metersphere.track.dto;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class TestCaseReportStatusResultDTO {
private String status;
private Integer count;
public class TestCaseReportAdvanceStatusResultDTO {
private List<TestCaseReportStatusResultDTO> functionalResult;
private List<TestCaseReportStatusResultDTO> apiResult;
private List<TestCaseReportStatusResultDTO> scenarioResult;
}

View File

@ -10,7 +10,8 @@ import java.util.List;
@Setter
public class TestCaseReportMetricDTO {
private List<TestCaseReportStatusResultDTO> executeResult;
// private List<TestCaseReportStatusResultDTO> executeResult;
private TestCaseReportAdvanceStatusResultDTO executeResult;
private List<TestCaseReportModuleResultDTO> moduleExecuteResult;
private List<TestPlanCaseDTO> failureTestCases;
private List<Issues> Issues;

View File

@ -3,6 +3,10 @@ package io.metersphere.track.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.automation.ApiScenarioDTO;
import io.metersphere.api.dto.automation.TestPlanScenarioRequest;
import io.metersphere.api.dto.definition.ApiTestCaseRequest;
import io.metersphere.api.dto.definition.TestPlanApiCaseDTO;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtProjectMapper;
@ -22,6 +26,7 @@ import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.SystemParameterService;
import io.metersphere.track.Factory.ReportComponentFactory;
import io.metersphere.track.domain.ReportComponent;
import io.metersphere.track.domain.ReportResultAdvancedChartComponent;
import io.metersphere.track.dto.TestCaseReportMetricDTO;
import io.metersphere.track.dto.TestPlanCaseDTO;
import io.metersphere.track.dto.TestPlanDTO;
@ -80,6 +85,10 @@ public class TestPlanService {
private NoticeSendService noticeSendService;
@Resource
private SystemParameterService systemParameterService;
@Resource
private TestPlanApiCaseService testPlanApiCaseService;
@Resource
private TestPlanScenarioCaseService testPlanScenarioCaseService;
public synchronized void addTestPlan(AddTestPlanRequest testPlan) {
if (getTestPlanByName(testPlan.getName()).size() > 0) {
@ -459,7 +468,6 @@ public class TestPlanService {
}
public TestCaseReportMetricDTO getMetric(String planId) {
IssuesService issuesService = (IssuesService) CommonBeanFactory.getBean("issuesService");
QueryTestPlanRequest queryTestPlanRequest = new QueryTestPlanRequest();
queryTestPlanRequest.setId(planId);
@ -472,31 +480,8 @@ public class TestPlanService {
JSONArray componentIds = content.getJSONArray("components");
List<ReportComponent> components = ReportComponentFactory.createComponents(componentIds.toJavaList(String.class), testPlan);
List<Issues> issues = buildFunctionalCaseReport(planId, components);
List<TestPlanCaseDTO> testPlanTestCases = listTestCaseByPlanId(planId);
List<Issues> issues = new ArrayList<>();
for (TestPlanCaseDTO testCase : testPlanTestCases) {
List<Issues> issue = issuesService.getIssues(testCase.getCaseId());
if (issue.size() > 0) {
for (Issues i : issue) {
i.setModel(testCase.getNodePath());
i.setProjectName(testCase.getProjectName());
String des = i.getDescription().replaceAll("<p>", "").replaceAll("</p>", "");
i.setDescription(des);
if (i.getLastmodify() == null || i.getLastmodify() == "") {
if (i.getReporter() != null || i.getReporter() != "") {
i.setLastmodify(i.getReporter());
}
}
}
issues.addAll(issue);
Collections.sort(issues, Comparator.comparing(Issues::getCreateTime, (t1, t2) -> t2.compareTo(t1)));
}
components.forEach(component -> {
component.readRecord(testCase);
});
}
TestCaseReportMetricDTO testCaseReportMetricDTO = new TestCaseReportMetricDTO();
components.forEach(component -> {
component.afterBuild(testCaseReportMetricDTO);
@ -609,4 +594,86 @@ public class TestPlanService {
return context;
}
public TestCaseReportMetricDTO getStatisticsMetric(String planId) {
QueryTestPlanRequest queryTestPlanRequest = new QueryTestPlanRequest();
queryTestPlanRequest.setId(planId);
TestPlanDTO testPlan = extTestPlanMapper.list(queryTestPlanRequest).get(0);
String projectName = getProjectNameByPlanId(planId);
testPlan.setProjectName(projectName);
TestCaseReport testCaseReport = testCaseReportMapper.selectByPrimaryKey(testPlan.getReportId());
JSONObject content = JSONObject.parseObject(testCaseReport.getContent());
JSONArray componentIds = content.getJSONArray("components");
List<ReportComponent> components = ReportComponentFactory.createComponents(componentIds.toJavaList(String.class), testPlan);
List<Issues> issues = buildFunctionalCaseReport(planId, components);
buildApiCaseReport(planId, components);
buildScenarioCaseReport(planId, components);
TestCaseReportMetricDTO testCaseReportMetricDTO = new TestCaseReportMetricDTO();
components.forEach(component -> {
component.afterBuild(testCaseReportMetricDTO);
});
testCaseReportMetricDTO.setIssues(issues);
return testCaseReportMetricDTO;
}
public void buildApiCaseReport(String planId, List<ReportComponent> components) {
ApiTestCaseRequest request = new ApiTestCaseRequest();
request.setPlanId(planId);
List<TestPlanApiCaseDTO> apiCaseDTOS = testPlanApiCaseService.list(request);
ReportResultAdvancedChartComponent chartComponent = null;
for (TestPlanApiCaseDTO item : apiCaseDTOS) {
for (ReportComponent component : components) {
if (component instanceof ReportResultAdvancedChartComponent) {
chartComponent = (ReportResultAdvancedChartComponent) component;
chartComponent.readRecord(item);
}
}
}
}
public void buildScenarioCaseReport(String planId, List<ReportComponent> components) {
TestPlanScenarioRequest request = new TestPlanScenarioRequest();
request.setPlanId(planId);
List<ApiScenarioDTO> scenarioDTOS = testPlanScenarioCaseService.list(request);
ReportResultAdvancedChartComponent chartComponent = null;
for (ApiScenarioDTO item : scenarioDTOS) {
for (ReportComponent component : components) {
if (component instanceof ReportResultAdvancedChartComponent) {
chartComponent = (ReportResultAdvancedChartComponent) component;
chartComponent.readRecord(item);
}
}
}
}
public List<Issues> buildFunctionalCaseReport(String planId, List<ReportComponent> components) {
IssuesService issuesService = (IssuesService) CommonBeanFactory.getBean("issuesService");
List<TestPlanCaseDTO> testPlanTestCases = listTestCaseByPlanId(planId);
List<Issues> issues = new ArrayList<>();
for (TestPlanCaseDTO testCase : testPlanTestCases) {
List<Issues> issue = issuesService.getIssues(testCase.getCaseId());
if (issue.size() > 0) {
for (Issues i : issue) {
i.setModel(testCase.getNodePath());
i.setProjectName(testCase.getProjectName());
String des = i.getDescription().replaceAll("<p>", "").replaceAll("</p>", "");
i.setDescription(des);
if (i.getLastmodify() == null || i.getLastmodify() == "") {
if (i.getReporter() != null || i.getReporter() != "") {
i.setLastmodify(i.getReporter());
}
}
}
issues.addAll(issue);
Collections.sort(issues, Comparator.comparing(Issues::getCreateTime, (t1, t2) -> t2.compareTo(t1)));
}
components.forEach(component -> {
component.readRecord(testCase);
});
}
return issues;
}
}