From 8696347f4ab1b2c77e590f2dcf500133d5107c27 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Mon, 28 Dec 2020 12:49:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E6=B2=A1=E6=9C=89=E7=BB=9F=E8=AE=A1=E5=88=B0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiDefinitionExecResultService.java | 13 ++++- .../api/service/ApiScenarioReportService.java | 5 +- .../mapper/ext/ExtTestPlanApiCaseMapper.java | 2 + .../mapper/ext/ExtTestPlanApiCaseMapper.xml | 6 ++ .../ext/ExtTestPlanScenarioCaseMapper.java | 1 + .../ext/ExtTestPlanScenarioCaseMapper.xml | 6 ++ .../mapper/ext/ExtTestPlanTestCaseMapper.java | 2 + .../mapper/ext/ExtTestPlanTestCaseMapper.xml | 7 +++ .../track/service/TestPlanApiCaseService.java | 13 +++++ .../service/TestPlanScenarioCaseService.java | 4 ++ .../track/service/TestPlanService.java | 58 +++++++++++-------- 11 files changed, 89 insertions(+), 28 deletions(-) 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 3412cd9767..362781a4cf 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java @@ -7,8 +7,11 @@ import io.metersphere.base.domain.ApiDefinitionExecResult; import io.metersphere.base.domain.ApiDefinitionExecResultExample; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; +import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.utils.DateUtils; import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.track.service.TestPlanApiCaseService; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -25,6 +28,8 @@ public class ApiDefinitionExecResultService { private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; @Resource private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper; + @Resource + private TestPlanApiCaseService testPlanApiCaseService; public void saveApiResult(TestResult result, String type) { @@ -37,9 +42,13 @@ public class ApiDefinitionExecResultService { saveResult.setResourceId(item.getName()); saveResult.setContent(JSON.toJSONString(item)); saveResult.setStartTime(item.getStartTime()); - saveResult.setType(type); + String status = item.isSuccess() ? "success" : "error"; saveResult.setEndTime(item.getResponseResult().getResponseTime()); - saveResult.setStatus(item.isSuccess() ? "success" : "error"); + saveResult.setType(type); + saveResult.setStatus(status); + if (StringUtils.equals(type, ApiRunMode.API_PLAN.name())) { + testPlanApiCaseService.setExecResult(item.getName(), status); + } apiDefinitionExecResultMapper.insert(saveResult); }); } 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 82a4896c1f..3905a95bc9 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -5,6 +5,7 @@ import io.metersphere.api.dto.DeleteAPIReportRequest; import io.metersphere.api.dto.QueryAPIReportRequest; import io.metersphere.api.dto.automation.APIScenarioReportResult; import io.metersphere.api.dto.automation.ExecuteType; +import io.metersphere.api.dto.automation.ScenarioStatus; import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.jmeter.ScenarioResult; import io.metersphere.api.jmeter.TestResult; @@ -124,9 +125,9 @@ public class ApiScenarioReportService { TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(result.getTestId()); ScenarioResult scenarioResult = result.getScenarios().get(0); if (scenarioResult.getError() > 0) { - testPlanApiScenario.setLastResult("Fail"); + testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name()); } else { - testPlanApiScenario.setLastResult("Success"); + testPlanApiScenario.setLastResult(ScenarioStatus.Success.name()); } String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError())); testPlanApiScenario.setPassRate(passRate); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java index fdd6bec917..5e3e69e9e3 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java @@ -11,4 +11,6 @@ public interface ExtTestPlanApiCaseMapper { void insertIfNotExists(@Param("request") TestPlanApiCase request); List list(@Param("request") ApiTestCaseRequest request); + + List getExecResultByPlanId(String planId); } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml index e4742e7784..55291b8a4d 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml @@ -108,5 +108,11 @@ + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java index 670ae1b37d..edfa92898d 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java @@ -18,4 +18,5 @@ public interface ExtTestPlanScenarioCaseMapper { List list(@Param("request") TestPlanScenarioRequest request); + List getExecResultByPlanId(String planId); } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml index c8230df279..05a4cacd42 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml @@ -80,5 +80,11 @@ + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.java index bd23bcfbf3..2a3adb0c9d 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.java @@ -41,4 +41,6 @@ public interface ExtTestPlanTestCaseMapper { TestPlanCaseDTO get(String testPlanTestCaseId); void deleteByTestCaseID(String id); + + List getExecResultByPlanId(String planId); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.xml index 1a0a98a4bd..13eda218c7 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanTestCaseMapper.xml @@ -330,6 +330,13 @@ where test_plan_test_case.id = #{testPlanTestCaseId} + + update test_plan_test_case 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 4c8743da66..9e566df6e7 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -11,6 +11,7 @@ import io.metersphere.base.mapper.TestPlanApiCaseMapper; import io.metersphere.base.mapper.ext.ExtTestPlanApiCaseMapper; import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.track.request.testcase.TestPlanApiCaseBatchRequest; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -29,6 +30,7 @@ public class TestPlanApiCaseService { ApiTestCaseService apiTestCaseService; @Resource ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper; + @Lazy @Resource ApiDefinitionExecResultService apiDefinitionExecResultService; @@ -43,6 +45,10 @@ public class TestPlanApiCaseService { return apiTestCases; } + public List getExecResultByPlanId(String plan) { + return extTestPlanApiCaseMapper.getExecResultByPlanId(plan); + } + public List relevanceList(ApiTestCaseRequest request) { List ids = apiTestCaseService.selectIdsNotExistsInPlan(request.getProjectId(), request.getPlanId()); if (CollectionUtils.isEmpty(ids)) { @@ -78,4 +84,11 @@ public class TestPlanApiCaseService { example.createCriteria().andTestPlanIdEqualTo(planId); return testPlanApiCaseMapper.selectByExample(example); } + + public void setExecResult(String id, String status) { + TestPlanApiCase apiCase = new TestPlanApiCase(); + apiCase.setId(id); + apiCase.setStatus(status); + testPlanApiCaseMapper.updateByPrimaryKeySelective(apiCase); + } } 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 164252bf11..f2d5c74d91 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java @@ -89,4 +89,8 @@ public class TestPlanScenarioCaseService { example.createCriteria().andTestPlanIdEqualTo(planId); return testPlanApiScenarioMapper.selectByExample(example); } + + public List getExecResultByPlanId(String planId) { + return extTestPlanScenarioCaseMapper.getExecResultByPlanId(planId); + } } 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 cd0e35302b..fc6d68a2b7 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -4,6 +4,7 @@ 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.ScenarioStatus; import io.metersphere.api.dto.automation.TestPlanScenarioRequest; import io.metersphere.api.dto.definition.ApiTestCaseRequest; import io.metersphere.api.dto.definition.TestPlanApiCaseDTO; @@ -300,35 +301,44 @@ public class TestPlanService { } private void calcTestPlanRate(List testPlans) { - List projectIds = extProjectMapper.getProjectIdByWorkspaceId(SessionUtils.getCurrentWorkspaceId()); - Map> testCaseMap = new HashMap<>(); - listTestCaseByProjectIds(projectIds).forEach(testCase -> { - List list = testCaseMap.get(testCase.getPlanId()); - if (list == null) { - list = new ArrayList<>(); - list.add(testCase); - testCaseMap.put(testCase.getPlanId(), list); - } else { - list.add(testCase); - } - }); testPlans.forEach(testPlan -> { - List testCases = testCaseMap.get(testPlan.getId()); testPlan.setTested(0); testPlan.setPassed(0); testPlan.setTotal(0); - if (testCases != null) { - testPlan.setTotal(testCases.size()); - testCases.forEach(testCase -> { - if (!StringUtils.equals(testCase.getStatus(), TestPlanTestCaseStatus.Prepare.name()) - && !StringUtils.equals(testCase.getStatus(), TestPlanTestCaseStatus.Underway.name())) { - testPlan.setTested(testPlan.getTested() + 1); - if (StringUtils.equals(testCase.getStatus(), TestPlanTestCaseStatus.Pass.name())) { - testPlan.setPassed(testPlan.getPassed() + 1); - } + + List functionalExecResults = extTestPlanTestCaseMapper.getExecResultByPlanId(testPlan.getId()); + functionalExecResults.forEach(item -> { + if (!StringUtils.equals(item, TestPlanTestCaseStatus.Prepare.name()) + && !StringUtils.equals(item, TestPlanTestCaseStatus.Underway.name())) { + testPlan.setTested(testPlan.getTested() + 1); + if (StringUtils.equals(item, TestPlanTestCaseStatus.Pass.name())) { + testPlan.setPassed(testPlan.getPassed() + 1); } - }); - } + } + }); + + List apiExecResults = testPlanApiCaseService.getExecResultByPlanId(testPlan.getId()); + apiExecResults.forEach(item -> { + if (StringUtils.isNotBlank(item)) { + testPlan.setTested(testPlan.getTested() + 1); + if (StringUtils.equals(item, "success")) { + testPlan.setPassed(testPlan.getPassed() + 1); + } + } + }); + + List scenarioExecResults = testPlanScenarioCaseService.getExecResultByPlanId(testPlan.getId()); + scenarioExecResults.forEach(item -> { + if (StringUtils.isNotBlank(item)) { + testPlan.setTested(testPlan.getTested() + 1); + if (StringUtils.equals(item, ScenarioStatus.Success.name())) { + testPlan.setPassed(testPlan.getPassed() + 1); + } + } + }); + + testPlan.setTotal(apiExecResults.size() + scenarioExecResults.size() + functionalExecResults.size()); + testPlan.setPassRate(MathUtils.getPercentWithDecimal(testPlan.getTested() == 0 ? 0 : testPlan.getPassed() * 1.0 / testPlan.getTested())); testPlan.setTestRate(MathUtils.getPercentWithDecimal(testPlan.getTotal() == 0 ? 0 : testPlan.getTested() * 1.0 / testPlan.getTotal())); });