refactor(接口测试): 枚举值验证处理

This commit is contained in:
fit2-zhao 2024-01-29 12:51:06 +08:00 committed by Craftsman
parent 0b07442daf
commit 1e0bbf88b7
12 changed files with 54 additions and 27 deletions

View File

@ -44,8 +44,7 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter<MsC
}
boolean isIgnoreStatus = false;
for (MsAssertion assertion : assertionConfig.getAssertions()) {
if (assertion instanceof MsResponseCodeAssertion) {
MsResponseCodeAssertion responseCodeAssertion = (MsResponseCodeAssertion) assertion;
if (assertion instanceof MsResponseCodeAssertion responseCodeAssertion) {
// 如果状态码断言添加了不校验状态码则所有断言忽略状态码
if (StringUtils.equals(responseCodeAssertion.getCondition(), MsAssertionCondition.UNCHECK.name())) {
isIgnoreStatus = true;

View File

@ -11,7 +11,7 @@ import java.util.Map;
*/
public class MsBodyConverterFactory {
private static Map<Class, MsBodyConverter> converterMap = new HashMap<>();
private static final Map<Class<?>, 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);
}
}

View File

@ -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<FormDataBody> {
/**
* 解析文件类型的参数
*
* @param fileFromValues
* @return
*/
@ -41,7 +43,7 @@ public class MsFormDataBodyConverter extends MsBodyConverter<FormDataBody> {
return new HTTPFileArg[0];
}
List<HTTPFileArg> list = new ArrayList<>();
if (fileFromValues != null) {
if (CollectionUtils.isNotEmpty(fileFromValues)) {
fileFromValues.forEach(formDataKV -> {
String paramName = formDataKV.getKey();
formDataKV.getFiles().forEach(file -> {

View File

@ -14,8 +14,8 @@ import java.util.Map;
*/
public class MsProcessorConverterFactory {
private static Map<Class, MsProcessorConverter> preConverterMap = new HashMap<>();
private static Map<Class, MsProcessorConverter> postConverterMap = new HashMap<>();
private static final Map<Class<?>, MsProcessorConverter> preConverterMap = new HashMap<>();
private static final Map<Class<?>, 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);
}
}

View File

@ -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<String> bks = IOUtils.readLines(in, Charset.defaultCharset());
if (CollectionUtils.isNotEmpty(bks)) {
scriptCache.put(path, bks);
@ -45,7 +44,7 @@ public class ScriptFilter {
List<String> 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;
}

View File

@ -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<T extends MsAssertion> {
/**
* 解析对应的提取器
*
* @param hashTree
* @param extract
* @param config
@ -56,7 +58,7 @@ public abstract class AssertionConverter<T extends MsAssertion> {
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);
}

View File

@ -10,7 +10,7 @@ import java.util.Map;
* @CreateTime: 2023-12-27 10:31
*/
public class AssertionConverterFactory {
private static Map<Class, AssertionConverter> converterMap = new HashMap<>();
private static final Map<Class<?>, 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);
}
}

View File

@ -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<MsRespo
String expectedValue = msAssertion.getExpectedValue();
String condition = msAssertion.getCondition();
assertion.setName(String.format("Response header %s %s", condition.toLowerCase().replace("_", ""), expectedValue));
MsAssertionCondition msAssertionCondition = MsAssertionCondition.valueOf(condition);
if (msAssertionCondition!= null) {
MsAssertionCondition msAssertionCondition = EnumValidator.validateEnum(MsAssertionCondition.class, condition);
if (msAssertionCondition != null) {
assertion.addTestString(generateRegexExpression(condition, expectedValue));
} else {
assertion.addTestString(expectedValue);

View File

@ -42,10 +42,7 @@ public class VariableAssertionConverter extends AssertionConverter<MsVariableAss
protected boolean needParse(MsVariableAssertion.VariableAssertionItem variableAssertionItem, ParameterConfig config) {
// 如果组件是启用的或者设置了解析禁用的组件则返回 true
if (BooleanUtils.isTrue(variableAssertionItem.getEnable()) || config.getParseDisabledElement()) {
return true;
}
return false;
return BooleanUtils.isTrue(variableAssertionItem.getEnable()) || config.getParseDisabledElement();
}
private static JSR223Assertion parse2JSR233Assertion(MsVariableAssertion.VariableAssertionItem variableAssertionItem) {

View File

@ -14,7 +14,7 @@ import java.util.Map;
*/
public class ResponseBodyTypeAssertionFactory {
private static Map<Class, ResponseBodyTypeAssertionConverter> converterMap = new HashMap<>();
private static final Map<Class<?>, 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);
}
}

View File

@ -12,7 +12,7 @@ import java.util.Map;
* @CreateTime: 2023-12-27 10:31
*/
public class ExtractConverterFactory {
private static Map<Class, ExtractConverter> converterMap = new HashMap<>();
private static final Map<Class<?>, 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);
}
}

View File

@ -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 <E> 枚举类型
* @return 枚举值
*/
public static <E extends Enum<E>> E validateEnum(Class<E> 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;
}
}
}