From 1e0bbf88b7e3e80ebe9bd89b89a9ec84d628cd28 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 29 Jan 2024 12:51:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=9E=9A=E4=B8=BE=E5=80=BC=E9=AA=8C=E8=AF=81=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jmeter/MsCommonElementConverter.java | 3 +-- .../jmeter/body/MsBodyConverterFactory.java | 4 +-- .../jmeter/body/MsFormDataBodyConverter.java | 4 ++- .../MsProcessorConverterFactory.java | 8 +++--- .../parser/jmeter/processor/ScriptFilter.java | 9 +++---- .../assertion/AssertionConverter.java | 4 ++- .../assertion/AssertionConverterFactory.java | 4 +-- .../ResponseHeaderAssertionConverter.java | 5 ++-- .../assertion/VariableAssertionConverter.java | 5 +--- .../ResponseBodyTypeAssertionFactory.java | 4 +-- .../extract/ExtractConverterFactory.java | 4 +-- .../jmeter/validator/EnumValidator.java | 27 +++++++++++++++++++ 12 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/validator/EnumValidator.java diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java index 423bcd4792..b614ad4849 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java @@ -44,8 +44,7 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter converterMap = new HashMap<>(); + private static final Map, MsBodyConverter> converterMap = new HashMap<>(); static { converterMap.put(RawBody.class, new MsRawBodyConverter()); @@ -23,7 +23,7 @@ public class MsBodyConverterFactory { converterMap.put(BinaryBody.class, new MsBinaryBodyConverter()); } - public static MsBodyConverter getConverter(Class bodyClassByType) { + public static MsBodyConverter getConverter(Class bodyClassByType) { return converterMap.get(bodyClassByType); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsFormDataBodyConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsFormDataBodyConverter.java index 076ba4c848..2cd2a20400 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsFormDataBodyConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsFormDataBodyConverter.java @@ -14,6 +14,7 @@ import java.util.stream.Collectors; /** * 处理 form-data 类型的请求体 + * * @Author: jianxing * @CreateTime: 2023-12-14 15:18 */ @@ -33,6 +34,7 @@ public class MsFormDataBodyConverter extends MsBodyConverter { /** * 解析文件类型的参数 + * * @param fileFromValues * @return */ @@ -41,7 +43,7 @@ public class MsFormDataBodyConverter extends MsBodyConverter { return new HTTPFileArg[0]; } List list = new ArrayList<>(); - if (fileFromValues != null) { + if (CollectionUtils.isNotEmpty(fileFromValues)) { fileFromValues.forEach(formDataKV -> { String paramName = formDataKV.getKey(); formDataKV.getFiles().forEach(file -> { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/MsProcessorConverterFactory.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/MsProcessorConverterFactory.java index 1b5842095c..09662c3597 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/MsProcessorConverterFactory.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/MsProcessorConverterFactory.java @@ -14,8 +14,8 @@ import java.util.Map; */ public class MsProcessorConverterFactory { - private static Map preConverterMap = new HashMap<>(); - private static Map postConverterMap = new HashMap<>(); + private static final Map, MsProcessorConverter> preConverterMap = new HashMap<>(); + private static final Map, MsProcessorConverter> postConverterMap = new HashMap<>(); static { preConverterMap.put(ScriptProcessor.class, new ScriptPreProcessorConverter()); @@ -28,11 +28,11 @@ public class MsProcessorConverterFactory { postConverterMap.put(ExtractPostProcessor.class, new ExtractPostProcessorConverter()); } - public static MsProcessorConverter getPreConverter(Class processorClass) { + public static MsProcessorConverter getPreConverter(Class processorClass) { return preConverterMap.get(processorClass); } - public static MsProcessorConverter getPostConverter(Class processorClass) { + public static MsProcessorConverter getPostConverter(Class processorClass) { return postConverterMap.get(processorClass); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ScriptFilter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ScriptFilter.java index a340a1e27c..57effe77dc 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ScriptFilter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ScriptFilter.java @@ -21,7 +21,7 @@ public class ScriptFilter { // 关键字内容较小,全局缓存下来避免重复读取 public static final Map> scriptCache = new HashMap<>(); - static { + static { // 初始化安全过滤脚本 ScriptFilter.initScript(ScriptFilter.beanshell); ScriptFilter.initScript(ScriptFilter.python); @@ -29,8 +29,7 @@ public class ScriptFilter { } public static void initScript(String path) { - try { - InputStream in = ScriptFilter.class.getResourceAsStream(path); + try (InputStream in = ScriptFilter.class.getResourceAsStream(path)) { List bks = IOUtils.readLines(in, Charset.defaultCharset()); if (CollectionUtils.isNotEmpty(bks)) { scriptCache.put(path, bks); @@ -45,7 +44,7 @@ public class ScriptFilter { List bks = scriptCache.get(path); if (CollectionUtils.isNotEmpty(bks)) { bks.forEach(item -> { - if (script.contains(item) && script.indexOf(item) != -1) { + if (script.contains(item)) { buffer.append(item).append(","); } }); @@ -76,7 +75,7 @@ public class ScriptFilter { break; } if (StringUtils.isNotEmpty(buffer.toString())) { - String message = "脚本内包含敏感函数:【" + buffer.toString().substring(0, buffer.toString().length() - 1) + "】"; + String message = "脚本内包含敏感函数:【" + buffer.substring(0, buffer.toString().length() - 1) + "】"; if (StringUtils.isNotEmpty(label)) { message = label + "," + message; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java index 26d6aef025..f521ae8527 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverter.java @@ -1,6 +1,7 @@ package io.metersphere.api.parser.jmeter.processor.assertion; import io.metersphere.api.dto.request.assertion.MsAssertion; +import io.metersphere.api.parser.jmeter.validator.EnumValidator; import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.sdk.constants.MsAssertionCondition; import org.apache.commons.lang3.BooleanUtils; @@ -23,6 +24,7 @@ import static io.metersphere.api.parser.jmeter.constants.JmeterAlias.ASSERTION_G public abstract class AssertionConverter { /** * 解析对应的提取器 + * * @param hashTree * @param extract * @param config @@ -56,7 +58,7 @@ public abstract class AssertionConverter { regexgenerateMap.put(MsAssertionCondition.EMPTY, value -> StringUtils.join("^$", value)); regexgenerateMap.put(MsAssertionCondition.NOT_EMPTY, value -> StringUtils.join("^(?!^$).*$", value)); regexgenerateMap.put(MsAssertionCondition.REGEX, value -> value); - MsAssertionCondition msAssertionCondition = MsAssertionCondition.valueOf(condition); + MsAssertionCondition msAssertionCondition = EnumValidator.validateEnum(MsAssertionCondition.class, condition); if (msAssertionCondition != null && regexgenerateMap.get(msAssertionCondition) != null) { return regexgenerateMap.get(msAssertionCondition).apply(text); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverterFactory.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverterFactory.java index 2ffaff98fe..8a1dda17d7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverterFactory.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/AssertionConverterFactory.java @@ -10,7 +10,7 @@ import java.util.Map; * @CreateTime: 2023-12-27 10:31 */ public class AssertionConverterFactory { - private static Map converterMap = new HashMap<>(); + private static final Map, AssertionConverter> converterMap = new HashMap<>(); static { converterMap.put(MsResponseCodeAssertion.class, new ResponseCodeAssertionConverter()); @@ -21,7 +21,7 @@ public class AssertionConverterFactory { converterMap.put(MsVariableAssertion.class, new VariableAssertionConverter()); } - public static AssertionConverter getConverter(Class processorClass) { + public static AssertionConverter getConverter(Class processorClass) { return converterMap.get(processorClass); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java index 27c5ef60aa..8c0d54a812 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/assertion/ResponseHeaderAssertionConverter.java @@ -1,6 +1,7 @@ package io.metersphere.api.parser.jmeter.processor.assertion; import io.metersphere.api.dto.request.assertion.MsResponseHeaderAssertion; +import io.metersphere.api.parser.jmeter.validator.EnumValidator; import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.sdk.constants.MsAssertionCondition; import org.apache.commons.lang3.BooleanUtils; @@ -47,8 +48,8 @@ public class ResponseHeaderAssertionConverter extends AssertionConverter converterMap = new HashMap<>(); + private static final Map, ResponseBodyTypeAssertionConverter> converterMap = new HashMap<>(); static { converterMap.put(MsJSONPathAssertion.class, new JSONPathAssertionConverter()); @@ -23,7 +23,7 @@ public class ResponseBodyTypeAssertionFactory { converterMap.put(MsRegexAssertion.class, new RegexAssertionConverter()); } - public static ResponseBodyTypeAssertionConverter getConverter(Class processorClass) { + public static ResponseBodyTypeAssertionConverter getConverter(Class processorClass) { return converterMap.get(processorClass); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverterFactory.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverterFactory.java index 83f2625a14..fde6c2b806 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverterFactory.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/extract/ExtractConverterFactory.java @@ -12,7 +12,7 @@ import java.util.Map; * @CreateTime: 2023-12-27 10:31 */ public class ExtractConverterFactory { - private static Map converterMap = new HashMap<>(); + private static final Map, ExtractConverter> converterMap = new HashMap<>(); static { converterMap.put(RegexExtract.class, new RegexExtractConverter()); @@ -20,7 +20,7 @@ public class ExtractConverterFactory { converterMap.put(XPathExtract.class, new XPathExtractConverter()); } - public static ExtractConverter getConverter(Class processorClass) { + public static ExtractConverter getConverter(Class processorClass) { return converterMap.get(processorClass); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/validator/EnumValidator.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/validator/EnumValidator.java new file mode 100644 index 0000000000..4cdb5160f2 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/validator/EnumValidator.java @@ -0,0 +1,27 @@ +package io.metersphere.api.parser.jmeter.validator; + +import io.metersphere.sdk.util.LogUtils; +import org.apache.commons.lang3.StringUtils; + +public class EnumValidator { + /** + * 校验枚举值 + * + * @param enumClass 枚举类 + * @param value 枚举值 + * @param 枚举类型 + * @return 枚举值 + */ + public static > E validateEnum(Class enumClass, String value) { + if (StringUtils.isBlank(value)) { + LogUtils.error("Invalid value for enum " + enumClass.getSimpleName() + ": " + value); + return null; + } + try { + return Enum.valueOf(enumClass, value); + } catch (IllegalArgumentException e) { + LogUtils.error("Invalid value for enum " + enumClass.getSimpleName() + ": " + value, e); + return null; + } + } +}