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)) { } else if (StringUtils.equals("2", componentId)) {
return new ReportResultComponent(testPlan); return new ReportResultComponent(testPlan);
} else if (StringUtils.equals("3", componentId)) { } else if (StringUtils.equals("3", componentId)) {
return new ReportResultChartComponent(testPlan); return new ReportResultAdvancedChartComponent(testPlan);
// return new ReportResultChartComponent(testPlan);
} else if (StringUtils.equals("4", componentId)) { } else if (StringUtils.equals("4", componentId)) {
return new ReportFailureResultComponent(testPlan); return new ReportFailureResultComponent(testPlan);
} }

View File

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

View File

@ -1,28 +1,48 @@
package io.metersphere.track.domain; 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.commons.constants.TestPlanTestCaseStatus;
import io.metersphere.track.dto.TestCaseReportMetricDTO; import io.metersphere.track.dto.*;
import io.metersphere.track.dto.TestCaseReportStatusResultDTO; import org.apache.commons.lang3.StringUtils;
import io.metersphere.track.dto.TestPlanCaseDTO;
import io.metersphere.track.dto.TestPlanDTO;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class ReportResultChartComponent extends ReportComponent { public class ReportResultAdvancedChartComponent extends ReportComponent {
Map<String, TestCaseReportStatusResultDTO> reportStatusResultMap = new HashMap<>(); 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); super(testPlan);
componentId = "3"; componentId = "3";
} }
@Override @Override
public void readRecord(TestPlanCaseDTO testCase) { 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 @Override
@ -30,31 +50,58 @@ public class ReportResultChartComponent extends ReportComponent {
testCaseReportMetric.setExecuteResult(getReportStatusResult()); testCaseReportMetric.setExecuteResult(getReportStatusResult());
} }
private List<TestCaseReportStatusResultDTO> getReportStatusResult() { private TestCaseReportAdvanceStatusResultDTO getReportStatusResult() {
List<TestCaseReportStatusResultDTO> reportStatusResult = new ArrayList<>(); TestCaseReportAdvanceStatusResultDTO reportStatusResult = new TestCaseReportAdvanceStatusResultDTO();
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Pass.name()); buildFunctionalStatusResult(reportStatusResult);
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Failure.name()); buildApiStatusResult(reportStatusResult);
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Blocking.name()); buildScenarioStatusResult(reportStatusResult);
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Skip.name());
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Underway.name());
addToReportStatusResultList(reportStatusResult, TestPlanTestCaseStatus.Prepare.name());
return reportStatusResult; return reportStatusResult;
} }
private void addToReportStatusResultList(List<TestCaseReportStatusResultDTO> reportStatusResultList, String status) { private void buildFunctionalStatusResult(TestCaseReportAdvanceStatusResultDTO reportStatusResult) {
if (reportStatusResultMap.get(status) != null) { List<TestCaseReportStatusResultDTO> functionalStatusResult = new ArrayList<>();
reportStatusResultList.add(reportStatusResultMap.get(status)); 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) { private void getStatusResultMap(Map<String, TestCaseReportStatusResultDTO> reportStatusResultMap, String result) {
TestCaseReportStatusResultDTO statusResult = reportStatusResultMap.get(testCase.getStatus()); if (StringUtils.isBlank(result)) {
result = TestPlanTestCaseStatus.Underway.name();
}
TestCaseReportStatusResultDTO statusResult = reportStatusResultMap.get(result);
if (statusResult == null) { if (statusResult == null) {
statusResult = new TestCaseReportStatusResultDTO(); statusResult = new TestCaseReportStatusResultDTO();
statusResult.setStatus(testCase.getStatus()); statusResult.setStatus(result);
statusResult.setCount(0); statusResult.setCount(0);
} }
statusResult.setCount(statusResult.getCount() + 1); 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 @Override
public void afterBuild(TestCaseReportMetricDTO testCaseReportMetric) { public void afterBuild(TestCaseReportMetricDTO testCaseReportMetric) {
testCaseReportMetric.setExecuteResult(getReportStatusResult()); // testCaseReportMetric.setExecuteResult(getReportStatusResult());
} }
private List<TestCaseReportStatusResultDTO> getReportStatusResult() { private List<TestCaseReportStatusResultDTO> getReportStatusResult() {

View File

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

View File

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

View File

@ -3,6 +3,10 @@ package io.metersphere.track.service;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; 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.domain.*;
import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtProjectMapper; 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.service.SystemParameterService;
import io.metersphere.track.Factory.ReportComponentFactory; import io.metersphere.track.Factory.ReportComponentFactory;
import io.metersphere.track.domain.ReportComponent; import io.metersphere.track.domain.ReportComponent;
import io.metersphere.track.domain.ReportResultAdvancedChartComponent;
import io.metersphere.track.dto.TestCaseReportMetricDTO; import io.metersphere.track.dto.TestCaseReportMetricDTO;
import io.metersphere.track.dto.TestPlanCaseDTO; import io.metersphere.track.dto.TestPlanCaseDTO;
import io.metersphere.track.dto.TestPlanDTO; import io.metersphere.track.dto.TestPlanDTO;
@ -80,6 +85,10 @@ public class TestPlanService {
private NoticeSendService noticeSendService; private NoticeSendService noticeSendService;
@Resource @Resource
private SystemParameterService systemParameterService; private SystemParameterService systemParameterService;
@Resource
private TestPlanApiCaseService testPlanApiCaseService;
@Resource
private TestPlanScenarioCaseService testPlanScenarioCaseService;
public synchronized void addTestPlan(AddTestPlanRequest testPlan) { public synchronized void addTestPlan(AddTestPlanRequest testPlan) {
if (getTestPlanByName(testPlan.getName()).size() > 0) { if (getTestPlanByName(testPlan.getName()).size() > 0) {
@ -459,7 +468,6 @@ public class TestPlanService {
} }
public TestCaseReportMetricDTO getMetric(String planId) { public TestCaseReportMetricDTO getMetric(String planId) {
IssuesService issuesService = (IssuesService) CommonBeanFactory.getBean("issuesService");
QueryTestPlanRequest queryTestPlanRequest = new QueryTestPlanRequest(); QueryTestPlanRequest queryTestPlanRequest = new QueryTestPlanRequest();
queryTestPlanRequest.setId(planId); queryTestPlanRequest.setId(planId);
@ -472,31 +480,8 @@ public class TestPlanService {
JSONArray componentIds = content.getJSONArray("components"); JSONArray componentIds = content.getJSONArray("components");
List<ReportComponent> components = ReportComponentFactory.createComponents(componentIds.toJavaList(String.class), testPlan); 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(); TestCaseReportMetricDTO testCaseReportMetricDTO = new TestCaseReportMetricDTO();
components.forEach(component -> { components.forEach(component -> {
component.afterBuild(testCaseReportMetricDTO); component.afterBuild(testCaseReportMetricDTO);
@ -609,4 +594,86 @@ public class TestPlanService {
return context; 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;
}
} }