From 68f5fb66248476107c563a0b854fc318cb2f68d9 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 6 Nov 2024 16:18:02 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=A7=84=E8=8C=83=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mockserver/ParamConditionEnums.java | 13 +++++++++++-- .../api/dto/mockserver/ApiMockConfigDTO.java | 9 --------- .../dto/mockserver/BodyParamMatchRule.java | 2 +- .../api/dto/mockserver/KeyValueInfo.java | 1 - ...eMatchRule.java => KeyValueMatchRule.java} | 2 +- .../api/dto/mockserver/MockMatchRule.java | 19 +++++++++++++------ .../api/service/MockServerTestService.java | 8 ++++---- 7 files changed, 30 insertions(+), 24 deletions(-) delete mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/ApiMockConfigDTO.java rename backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/{keyValueMatchRule.java => KeyValueMatchRule.java} (97%) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/constants/mockserver/ParamConditionEnums.java b/backend/services/api-test/src/main/java/io/metersphere/api/constants/mockserver/ParamConditionEnums.java index 0eafca24e4..14936b2919 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/constants/mockserver/ParamConditionEnums.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/constants/mockserver/ParamConditionEnums.java @@ -2,6 +2,15 @@ package io.metersphere.api.constants.mockserver; // mock匹配规则 public enum ParamConditionEnums { - EQUALS, NOT_EQUALS, CONTAINS, LENGTH_EQUALS, LENGTH_NOT_EQUALS, LENGTH_LARGE, LENGTH_SHOT, REGULAR_MATCH, - IS_EMPTY, IS_NOT_EMPTY, NOT_CONTAINS + EQUALS, + NOT_EQUALS, + CONTAINS, + LENGTH_EQUALS, + LENGTH_NOT_EQUALS, + LENGTH_LARGE, + LENGTH_SHOT, + REGULAR_MATCH, + IS_EMPTY, + IS_NOT_EMPTY, + NOT_CONTAINS } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/ApiMockConfigDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/ApiMockConfigDTO.java deleted file mode 100644 index 392c6bb566..0000000000 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/ApiMockConfigDTO.java +++ /dev/null @@ -1,9 +0,0 @@ -package io.metersphere.api.dto.mockserver; - -import io.metersphere.api.domain.ApiDefinitionMockConfig; -import lombok.Data; - -@Data -public class ApiMockConfigDTO extends ApiDefinitionMockConfig { - private boolean enable; -} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/BodyParamMatchRule.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/BodyParamMatchRule.java index 508d889288..45bb618d3b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/BodyParamMatchRule.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/BodyParamMatchRule.java @@ -33,7 +33,7 @@ public class BodyParamMatchRule { * x-www-form-urlencoded 请求体 * 当 bodyType 为 WWW_FORM 时,使用该字段 */ - private keyValueMatchRule wwwFormBody = new keyValueMatchRule(); + private KeyValueMatchRule wwwFormBody = new KeyValueMatchRule(); /** * json 请求体 * 当 bodyType 为 JSON 时,使用该字段 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/KeyValueInfo.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/KeyValueInfo.java index cdeb4e4b8c..0ee1319a5d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/KeyValueInfo.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/KeyValueInfo.java @@ -34,7 +34,6 @@ public class KeyValueInfo { case REGULAR_MATCH -> value.matches(this.value); case IS_EMPTY -> StringUtils.isBlank(value); case IS_NOT_EMPTY -> StringUtils.isNotBlank(value); - default -> false; }; } catch (Exception e) { return false; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/keyValueMatchRule.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/KeyValueMatchRule.java similarity index 97% rename from backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/keyValueMatchRule.java rename to backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/KeyValueMatchRule.java index d99b8320b5..080705a6e4 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/keyValueMatchRule.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/KeyValueMatchRule.java @@ -9,7 +9,7 @@ import java.util.List; import java.util.Map; @Data -public class keyValueMatchRule { +public class KeyValueMatchRule { @Schema(description = "是否是全部匹配 (false为任意匹配)") private boolean isMatchAll; @Schema(description = "匹配规则") diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java index e241b75d31..44b269e1f9 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java @@ -7,28 +7,35 @@ import lombok.Data; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import java.io.Serial; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Map; -//mock匹配规则 +/** + * Mock匹配规则 + */ @Data public class MockMatchRule implements Serializable { + @Serial private static final long serialVersionUID = 1L; @Schema(description = "请求头匹配规则") - private keyValueMatchRule header = new keyValueMatchRule(); + private KeyValueMatchRule header = new KeyValueMatchRule(); + @Schema(description = "query参数匹配规则") - private keyValueMatchRule query = new keyValueMatchRule(); + private KeyValueMatchRule query = new KeyValueMatchRule(); + @Schema(description = "REST参数匹配规则") - private keyValueMatchRule rest = new keyValueMatchRule(); + private KeyValueMatchRule rest = new KeyValueMatchRule(); + @Schema(description = "body参数匹配规则") private BodyParamMatchRule body = new BodyParamMatchRule(); public boolean keyValueMatch(String matchType, Map matchParam) { - keyValueMatchRule matchRule = switch (matchType) { + KeyValueMatchRule matchRule = switch (matchType) { case "header" -> header; case "query" -> query; case "rest" -> rest; @@ -55,7 +62,7 @@ public class MockMatchRule implements Serializable { return body.matchJson(httpRequestParam.getJsonString()); case FORM_DATA: MockFormDataBody formDataBody = body.getFormDataBody(); - keyValueMatchRule formDataBodyRule = new keyValueMatchRule(); + KeyValueMatchRule formDataBodyRule = new KeyValueMatchRule(); if (formDataBody != null) { formDataBodyRule.setMatchAll(formDataBody.isMatchAll()); List matchRules = new ArrayList<>(); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java b/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java index f70ff2f3b4..b3d05cf9fa 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java @@ -138,7 +138,7 @@ public class MockServerTestService { public MockMatchRule genMockMatchRule(String valuePrefix, boolean hasQuery, boolean hasHeader, String bodyParamType, boolean matchAll) { MockMatchRule mockMatchRule = new MockMatchRule(); - keyValueMatchRule restMatchRule = new keyValueMatchRule(); + KeyValueMatchRule restMatchRule = new KeyValueMatchRule(); restMatchRule.setMatchAll(matchAll); restMatchRule.setMatchRules(new ArrayList<>() {{ this.add(new KeyValueInfo() {{ @@ -153,7 +153,7 @@ public class MockServerTestService { mockMatchRule.setRest(restMatchRule); if (hasQuery) { - keyValueMatchRule queryMatchRule = new keyValueMatchRule(); + KeyValueMatchRule queryMatchRule = new KeyValueMatchRule(); queryMatchRule.setMatchAll(matchAll); queryMatchRule.setMatchRules(new ArrayList<>() {{ this.add(new KeyValueInfo() {{ @@ -173,7 +173,7 @@ public class MockServerTestService { } if (hasHeader) { - keyValueMatchRule headerMatchRule = new keyValueMatchRule(); + KeyValueMatchRule headerMatchRule = new KeyValueMatchRule(); headerMatchRule.setMatchAll(matchAll); headerMatchRule.setMatchRules(new ArrayList<>() {{ this.add(new KeyValueInfo() {{ @@ -195,7 +195,7 @@ public class MockServerTestService { if (StringUtils.equalsIgnoreCase(bodyParamType, "www")) { mockMatchRule.setBody(new BodyParamMatchRule() {{ this.setBodyType(Body.BodyType.WWW_FORM.name()); - this.setWwwFormBody(new keyValueMatchRule() {{ + this.setWwwFormBody(new KeyValueMatchRule() {{ this.setMatchAll(matchAll); this.setMatchRules(new ArrayList<>() {{ this.add(new KeyValueInfo() {{