From 9269aba07bc09a6a95af4bafdbeaf12797825b8d Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sat, 3 Feb 2024 15:45:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=AF=AF=E6=8A=A5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/dto/api/result/FakeErrorDTO.java | 11 +++ .../sdk/dto/api/result/MsRegexDTO.java | 34 ++++++-- .../sdk/dto/api/result/RequestResult.java | 10 +-- .../sdk/dto/api/task/TaskRequestDTO.java | 4 +- .../sdk/util/fake/error/FakeErrorUtils.java | 87 +++++++++++++++++++ .../parser/jmeter/MsHTTPElementConverter.java | 3 +- .../api/service/ApiExecuteService.java | 17 +++- .../project/service/FakeErrorService.java | 2 +- .../service/ProjectApplicationService.java | 19 ++++ 9 files changed, 167 insertions(+), 20 deletions(-) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/FakeErrorDTO.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/util/fake/error/FakeErrorUtils.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/FakeErrorDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/FakeErrorDTO.java new file mode 100644 index 0000000000..0f27923e79 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/FakeErrorDTO.java @@ -0,0 +1,11 @@ +package io.metersphere.sdk.dto.api.result; + +import lombok.Data; + +@Data +public class FakeErrorDTO { + private String projectId; + private boolean higherThanSuccess; + private boolean higherThanError; +} + diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/MsRegexDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/MsRegexDTO.java index 10c5c5911b..9fab54fdee 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/MsRegexDTO.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/MsRegexDTO.java @@ -1,12 +1,32 @@ package io.metersphere.sdk.dto.api.result; - import lombok.Data; +import java.io.Serial; +import java.io.Serializable; + @Data -public class MsRegexDTO { - private String subject; - private String condition; - private String value; - private String errorCode; - private boolean pass; +public class MsRegexDTO implements Serializable { + // 项目ID + private String projectId; + + // 误报名称 + private String name; + + // 匹配类型/文本内容 + private String type; + + // 响应内容类型/header/data/body + private String respType; + + // 操作类型/大于/等于/小于 + private String relation; + + // 表达式 + private String expression; + + private Boolean pass; + + @Serial + private static final long serialVersionUID = 1L; + } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/RequestResult.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/RequestResult.java index 020050d6af..92cefce4c5 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/RequestResult.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/RequestResult.java @@ -15,6 +15,11 @@ public class RequestResult { */ private String resourceId; + /** + * 项目ID + */ + private String projectId; + /** * 步骤请求唯一ID */ @@ -109,11 +114,6 @@ public class RequestResult { this.passAssertionsTotal++; } - /** - * 误报信息 - */ - private String fakeErrorMessage; - /** * 误报编码名称 */ diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java index d7e6aa91db..79b91b96dd 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequestDTO.java @@ -6,7 +6,6 @@ import lombok.Data; import java.io.Serial; import java.io.Serializable; import java.util.List; -import java.util.Map; /** * 任务请求参数数据 @@ -42,6 +41,7 @@ public class TaskRequestDTO implements Serializable { /** * 资源类型 + * * @see io.metersphere.sdk.constants.ApiExecuteResourceType */ private String resourceType; @@ -63,7 +63,7 @@ public class TaskRequestDTO implements Serializable { /** * 误报规则 */ - Map> fakeErrorMap; + List msRegexList; /** * 项目id diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/fake/error/FakeErrorUtils.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/fake/error/FakeErrorUtils.java new file mode 100644 index 0000000000..38442c5644 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/fake/error/FakeErrorUtils.java @@ -0,0 +1,87 @@ +package io.metersphere.sdk.util.fake.error; + +import io.metersphere.sdk.constants.ApiReportStatus; +import io.metersphere.sdk.dto.api.result.MsRegexDTO; +import io.metersphere.sdk.dto.api.result.RequestResult; +import io.metersphere.sdk.util.LogUtils; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * 误报解析类 + */ +public class FakeErrorUtils { + public static void compute(RequestResult result, List regexDTOS) { + try { + if (result != null && StringUtils.isNotBlank(result.getProjectId()) && + CollectionUtils.isNotEmpty(regexDTOS)) { + + Map> fakeErrorMap = regexDTOS.stream() + .collect(Collectors.groupingBy(MsRegexDTO::getProjectId)); + + List regexList = fakeErrorMap.get(result.getProjectId()); + //根据配置来筛选断言、获取误报编码、获取接口状态是否是误报 + List errorCodeList = new ArrayList<>(); + regexList.forEach(item -> { + if (StringUtils.isNotEmpty(item.getType())) { + switch (item.getRespType()) { + case "Response Code" -> + item.setPass(parseResponseCode(result.getResponseResult().getResponseCode(), item.getExpression(), item.getRelation())); + + case "Response Headers" -> + item.setPass(parseResponseCode(result.getResponseResult().getHeaders(), item.getExpression(), item.getRelation())); + + case "Response Data" -> + item.setPass(parseResponseCode(result.getResponseResult().getBody(), item.getExpression(), item.getRelation())); + default -> item.setPass(false); + } + } + if (BooleanUtils.isTrue(item.getPass())) { + errorCodeList.add(item.getName()); + } + }); + if (CollectionUtils.isNotEmpty(errorCodeList)) { + result.setStatus(ApiReportStatus.FAKE_ERROR.name()); + result.setFakeErrorCode(getErrorCodeStr(errorCodeList)); + } + } + } catch (Exception e) { + LogUtils.error("误报处理错误:", e); + } + } + + private static boolean parseResponseCode(String result, String regexDTO, String condition) { + return switch (condition.toUpperCase()) { + case "CONTAINS" -> result.contains(regexDTO); + + case "NOT_CONTAINS" -> notContains(result, regexDTO); + + case "EQUALS" -> StringUtils.equals(result, regexDTO); + + case "START_WITH" -> result.startsWith(regexDTO); + + case "END_WITH" -> result.endsWith(regexDTO); + + default -> false; + }; + } + + private static String getErrorCodeStr(List errorCodeList) { + if (CollectionUtils.isNotEmpty(errorCodeList)) { + String errorCodeStr = StringUtils.join(errorCodeList, ";"); + return errorCodeStr; + } else { + return StringUtils.EMPTY; + } + } + + private static boolean notContains(String result, String regexDTO) { + return !result.contains(regexDTO); + } +} \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java index bd3ab12795..634ddfe042 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java @@ -7,9 +7,9 @@ import io.metersphere.api.parser.jmeter.body.MsBodyConverter; import io.metersphere.api.parser.jmeter.body.MsBodyConverterFactory; import io.metersphere.api.parser.jmeter.body.MsFormDataBodyConverter; import io.metersphere.api.parser.jmeter.body.MsWWWFormBodyConverter; +import io.metersphere.plugin.api.constants.ElementProperty; import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; -import io.metersphere.plugin.api.constants.ElementProperty; import io.metersphere.sdk.util.LogUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; @@ -47,7 +47,6 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter 0) { diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java index 1d7587eeb0..75cd887dcc 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java @@ -3,6 +3,7 @@ package io.metersphere.project.service; import io.metersphere.plugin.platform.spi.AbstractPlatformPlugin; import io.metersphere.plugin.platform.spi.Platform; import io.metersphere.plugin.sdk.spi.MsPlugin; +import io.metersphere.project.domain.FakeError; import io.metersphere.project.domain.FakeErrorExample; import io.metersphere.project.domain.Project; import io.metersphere.project.domain.ProjectApplication; @@ -13,7 +14,9 @@ import io.metersphere.project.request.ProjectApplicationRequest; import io.metersphere.project.utils.ModuleSortUtils; import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.constants.ProjectApplicationType; +import io.metersphere.sdk.dto.api.result.MsRegexDTO; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; @@ -473,6 +476,22 @@ public class ProjectApplicationService { return (int) l; } + public List get(List projectIds) { + List regexList = new ArrayList<>(); + if (CollectionUtils.isEmpty(projectIds)) { + return regexList; + } + FakeErrorExample example = new FakeErrorExample(); + example.createCriteria().andProjectIdIn(projectIds).andEnableEqualTo(true); + List fakeErrors = fakeErrorMapper.selectByExample(example); + fakeErrors.forEach(item -> { + MsRegexDTO regexConfig = new MsRegexDTO(); + BeanUtils.copyBean(regexConfig, item); + regexList.add(regexConfig); + }); + return regexList; + } + /** * 获取缺陷项目配置信息