From a0de41847cb62df1229533b080c0ab532c3d18ae Mon Sep 17 00:00:00 2001 From: Jianguo-Genius Date: Thu, 7 Nov 2024 17:29:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E7=94=A8=E4=BE=8B=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=80=BB=E6=89=A7=E8=A1=8C=E9=80=9A=E8=BF=87=E7=8E=87=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001048522 --- .../definition/ApiTestCaseController.java | 9 +++++++-- .../api/dto/definition/ApiTestCaseDTO.java | 2 +- .../api/dto/scenario/ApiScenarioDTO.java | 2 +- .../api/service/definition/ApiTestCaseService.java | 14 ++++++++++++++ .../api/service/scenario/ApiScenarioService.java | 2 ++ .../api/controller/ApiReportControllerTests.java | 8 +++++++- 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java index db5e045746..f74d310e95 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java @@ -9,7 +9,6 @@ import io.metersphere.api.dto.ReferenceDTO; import io.metersphere.api.dto.ReferenceRequest; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiTransferRequest; -import io.metersphere.api.service.ApiBatchRunBaseService; import io.metersphere.api.service.ApiFileResourceService; import io.metersphere.api.service.definition.*; import io.metersphere.plugin.api.spi.AbstractMsTestElement; @@ -37,7 +36,6 @@ import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresPermissions; -import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -170,6 +168,13 @@ public class ApiTestCaseController { return PageUtils.setPageInfo(page, apiTestCaseService.page(request, false, true,null)); } + @PostMapping(value = "/statistics") + @Operation(summary = "接口测试-接口管理-接口用例-统计") + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ) + public List calculate(@Validated @RequestBody List ids) { + return apiTestCaseService.calculate(ids); + } + @PostMapping("/batch/delete") @Operation(summary = "接口测试-接口管理-接口用例-批量删除") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java index 013b39a873..421c3021e1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java @@ -87,7 +87,7 @@ public class ApiTestCaseDTO { private List tags; @Schema(description = "用例通过率") - private String passRate; + private String passRate = "NONE"; @Schema(description = "请求内容") private AbstractMsTestElement request; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java index c0e8ce4bd5..d0786ab142 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java @@ -29,5 +29,5 @@ public class ApiScenarioDTO extends ApiScenario { @Schema(description = "脚本错误标识") private String scriptIdentifier; @Schema(description = "执行通过率", requiredMode = Schema.RequiredMode.REQUIRED) - private String execPassRate = "0%"; + private String execPassRate = "NONE"; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java index d95f0d913f..3f64c6fbed 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java @@ -341,6 +341,20 @@ public class ApiTestCaseService extends MoveNodeService { return apiCaseLists; } + public List calculate(List ids) { + List returnList = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(ids)) { + List passRateList = extApiTestCaseMapper.findPassRateByIds(ids); + Map passRates = passRateList.stream().collect(Collectors.toMap(CasePassDTO::getId, CasePassDTO::getValue)); + for (String id : ids) { + ApiTestCaseDTO dto = new ApiTestCaseDTO(); + dto.setId(id); + dto.setPassRate(passRates.getOrDefault(id, "0%")); + returnList.add(dto); + } + } + return returnList; + } private void buildApiTestCaseDTO(List apiCaseLists) { if (CollectionUtils.isNotEmpty(apiCaseLists)) { List userIds = new ArrayList<>(); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index 2ffb595ddf..3ee8b36297 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -2641,6 +2641,8 @@ public class ApiScenarioService extends MoveNodeService { } } dto.setExecPassRate(CalculateUtils.reportPercentage(passCount, all)); + } else { + dto.setExecPassRate("0%"); } result.add(dto); } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportControllerTests.java index eac6b6d670..d0ba04c276 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportControllerTests.java @@ -45,6 +45,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -90,7 +91,7 @@ public class ApiReportControllerTests extends BaseTest { @Test @Order(1) - public void testInsert() { + public void testInsert() throws Exception { List reports = new ArrayList<>(); List records = new ArrayList<>(); for (int i = 0; i < 2515; i++) { @@ -130,6 +131,11 @@ public class ApiReportControllerTests extends BaseTest { steps.add(apiReportStep); } apiReportService.insertApiReportStep(steps); + + // 顺便查找一下通过率 + // 查统计数据 + List caseIds = Collections.singletonList("api-resource-id0"); + requestPostWithOk("/api/case/statistics", caseIds); } private MvcResult responsePost(String url, Object param) throws Exception {