mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-04 21:19:52 +08:00
refactor(接口测试): 优化条件控制器脚本
This commit is contained in:
parent
bfe9fc9902
commit
e7555e436c
@ -0,0 +1,31 @@
|
||||
package io.metersphere.api.dto.request.controller;
|
||||
|
||||
import io.metersphere.sdk.constants.MsAssertionCondition;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class ConditionUtils {
|
||||
|
||||
|
||||
public String getConditionValue(String variable, String condition, String value) {
|
||||
variable = "\"" + variable + "\"";
|
||||
MsAssertionCondition msAssertionCondition = MsAssertionCondition.valueOf(condition);
|
||||
return switch (msAssertionCondition) {
|
||||
case EMPTY -> String.format("(%s==\"\"|| empty(%s))", variable, variable);
|
||||
case NOT_EMPTY ->
|
||||
String.format("(%s!=\"\"&& !empty(%s))", variable, variable);
|
||||
case GT -> StringUtils.isNumeric(value) ? variable + ">" + value : variable + ">\"" + value + "\"";
|
||||
case LT -> StringUtils.isNumeric(value) ? variable + "<" + value : variable + "<\"" + value + "\"";
|
||||
case LT_OR_EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + "<=" + value : variable + "<=\"" + value + "\"";
|
||||
case GT_OR_EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + ">=" + value : variable + ">=\"" + value + "\"";
|
||||
case CONTAINS -> String.format("%s.contains(%s)", variable, "\"" + value + "\"");
|
||||
case NOT_CONTAINS -> String.format("!%s.contains(%s)", variable, "\"" + value + "\"");
|
||||
case EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + "==" + value : variable + "==" + "\"" + value + "\"";
|
||||
case NOT_EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + "!=" + value : variable + "!=" + "\"" + value + "\"";
|
||||
default -> "\"" + value + "\"";
|
||||
};
|
||||
}
|
||||
}
|
@ -1,14 +1,9 @@
|
||||
package io.metersphere.api.dto.request.controller;
|
||||
|
||||
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
|
||||
import io.metersphere.sdk.constants.MsAssertionCondition;
|
||||
import io.metersphere.system.valid.EnumValue;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ -25,67 +20,10 @@ public class MsIfController extends AbstractMsTestElement {
|
||||
*/
|
||||
private String value;
|
||||
|
||||
public boolean isValid() {
|
||||
if (StringUtils.contains(condition, MsAssertionCondition.EMPTY.name())) {
|
||||
return StringUtils.isNotBlank(variable);
|
||||
}
|
||||
return StringUtils.isNotBlank(variable) && StringUtils.isNotBlank(condition) && StringUtils.isNotBlank(value);
|
||||
}
|
||||
|
||||
public String getContentValue() {
|
||||
try {
|
||||
String content = this.variable;
|
||||
String pattern = "\\$\\{([^}]*)\\}";
|
||||
Pattern regex = Pattern.compile(pattern);
|
||||
Matcher matcher = regex.matcher(content);
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
while (matcher.find()) {
|
||||
stringBuilder.append(matcher.group(1)).append(",");
|
||||
}
|
||||
if (!stringBuilder.isEmpty()) {
|
||||
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
|
||||
}
|
||||
if (StringUtils.isEmpty(stringBuilder.toString())) {
|
||||
return this.variable;
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getConditionValue() {
|
||||
String key = getContentValue();
|
||||
|
||||
String variable = (StringUtils.isEmpty(key) || key.equals(this.variable)) || key.startsWith("__")
|
||||
? StringUtils.join("\"", this.variable, "\"")
|
||||
: StringUtils.join("vars.get('", key, "')");
|
||||
|
||||
String operator = this.condition;
|
||||
|
||||
MsAssertionCondition msAssertionCondition = MsAssertionCondition.valueOf(operator);
|
||||
return switch (msAssertionCondition) {
|
||||
case EMPTY ->
|
||||
buildExpression(variable + "==" + "\"\\" + this.variable + "\"" + "|| empty(" + variable + ")");
|
||||
case NOT_EMPTY ->
|
||||
buildExpression(variable + "!=" + "\"\\" + this.variable + "\"" + "&& !empty(" + variable + ")");
|
||||
case GT ->
|
||||
buildExpression(StringUtils.isNumeric(value) ? variable + ">" + value : variable + ">" + "\"" + value + "\"");
|
||||
case LT ->
|
||||
buildExpression(StringUtils.isNumeric(value) ? variable + "<" + value : variable + "<" + "\"" + value + "\"");
|
||||
case GT_OR_EQUALS ->
|
||||
buildExpression(StringUtils.isNumeric(value) ? variable + ">=" + value : variable + ">=" + "\"" + value + "\"");
|
||||
case LT_OR_EQUALS ->
|
||||
buildExpression(StringUtils.isNumeric(value) ? variable + "<=" + value : variable + "<=" + "\"" + value + "\"");
|
||||
case CONTAINS -> buildExpression("\"(\\n|.)*" + value + "(\\n|.)*\"=~" + variable);
|
||||
case NOT_CONTAINS -> buildExpression("\"(\\n|.)*" + value + "(\\n|.)*\"!~" + variable);
|
||||
case EQUALS ->
|
||||
buildExpression(StringUtils.isNumeric(value) ? variable + "==" + value : variable + "==" + "\"" + value + "\"");
|
||||
case NOT_EQUALS ->
|
||||
buildExpression(StringUtils.isNumeric(value) ? variable + "!=" + value : variable + "!=" + "\"" + value + "\"");
|
||||
default -> buildExpression("\"" + condition + value + "\"");
|
||||
};
|
||||
}
|
||||
ConditionUtils conditionUtils = new ConditionUtils();
|
||||
return buildExpression(conditionUtils.getConditionValue(this.variable, this.condition, value));
|
||||
}
|
||||
|
||||
private String buildExpression(String expression) {
|
||||
return "${__jexl3(" + expression + ")}";
|
||||
|
@ -1,9 +1,8 @@
|
||||
package io.metersphere.api.dto.request.controller.loop;
|
||||
|
||||
import io.metersphere.sdk.constants.MsAssertionCondition;
|
||||
import io.metersphere.api.dto.request.controller.ConditionUtils;
|
||||
import io.metersphere.system.valid.EnumValue;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@Data
|
||||
public class MsWhileVariable {
|
||||
@ -22,27 +21,8 @@ public class MsWhileVariable {
|
||||
private String value;
|
||||
|
||||
public String getConditionValue() {
|
||||
String variable = "\"" + this.getVariable() + "\"";
|
||||
String value = this.getValue();
|
||||
MsAssertionCondition msAssertionCondition = MsAssertionCondition.valueOf(this.getCondition());
|
||||
return switch (msAssertionCondition) {
|
||||
case EMPTY -> String.format("(%s==\"\\\"%s\\\"\"|| empty(%s))", variable, this.getVariable(), variable);
|
||||
case NOT_EMPTY ->
|
||||
String.format("(%s!=\"\\\"%s\\\"\"&& !empty(%s))", variable, this.getVariable(), variable);
|
||||
case GT -> StringUtils.isNumeric(value) ? variable + ">" + value : variable + ">\"" + value + "\"";
|
||||
case LT -> StringUtils.isNumeric(value) ? variable + "<" + value : variable + "<\"" + value + "\"";
|
||||
case LT_OR_EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + "<=" + value : variable + "<=\"" + value + "\"";
|
||||
case GT_OR_EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + ">=" + value : variable + ">=\"" + value + "\"";
|
||||
case CONTAINS -> String.format("\"(\\n|.)*%s(\\n|.)*\"=~", variable);
|
||||
case NOT_CONTAINS -> String.format("\"(\\n|.)*%s(\\n|.)*\"!~", variable);
|
||||
case EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + "==" + value : variable + "==" + "\"" + value + "\"";
|
||||
case NOT_EQUALS ->
|
||||
StringUtils.isNumeric(value) ? variable + "!=" + value : variable + "!=" + "\"" + value + "\"";
|
||||
default -> "\"" + value + "\"";
|
||||
};
|
||||
ConditionUtils conditionUtils = new ConditionUtils();
|
||||
return conditionUtils.getConditionValue(variable, condition, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,8 +47,6 @@ import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.metersphere.api.controller.result.ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR;
|
||||
|
||||
/**
|
||||
* @author: LAN
|
||||
* @date: 2024/1/17 11:24
|
||||
@ -264,7 +262,6 @@ public class ApiTaskCenterService {
|
||||
LogUtils.info(String.format("开始发送停止请求到 %s 节点执行", endpoint), subList.toString());
|
||||
TaskRunnerClient.stopApi(endpoint, subList);
|
||||
} catch (Exception e) {
|
||||
LogUtils.error(e);
|
||||
if (request.getModuleType().equals(TaskCenterResourceType.API_CASE.toString())) {
|
||||
extApiReportMapper.updateReportStatus(subList, System.currentTimeMillis(), userId);
|
||||
//记录日志
|
||||
@ -273,7 +270,7 @@ public class ApiTaskCenterService {
|
||||
extApiScenarioReportMapper.updateReportStatus(subList, System.currentTimeMillis(), userId);
|
||||
saveLog(subList, userId, path, method, module, TaskCenterResourceType.API_SCENARIO.toString());
|
||||
}
|
||||
throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage());
|
||||
LogUtils.error(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -305,11 +305,7 @@ public class ApiScenarioReportService {
|
||||
step.setResponseSize(step.getChildren().stream().mapToLong(child -> child.getResponseSize() != null ? child.getResponseSize() : 0).sum());
|
||||
//请求的状态, 如果是 LOOP_CONTROLLER IF_CONTROLLER ONCE_ONLY_CONTROLLER 则需要判断子级的状态 但是如果下面没有子集不需要判断状态
|
||||
//需要把这些数据拿出来 如果没有子请求说明是最后一级的请求 不需要计算入状态
|
||||
//children 先过滤满足控制器的数据,然后再获取id
|
||||
List<String> controllerIds = children.stream().filter(child -> stepTypes.contains(child.getStepType())).map(ApiScenarioReportStepDTO::getStepId).toList();
|
||||
//看map中有没有这些id 如果没有 需要返回几个没有
|
||||
List<String> noControllerIds = controllerIds.stream().filter(controllerId -> !scenarioReportStepMap.containsKey(controllerId)).toList();
|
||||
//获取所有的子请求的状态
|
||||
//获取所有的子请求的状态
|
||||
List<String> requestStatus = children.stream().map(ApiScenarioReportStepDTO::getStatus).toList();
|
||||
//过滤出来SUCCESS的状态
|
||||
List<String> successStatus = requestStatus.stream().filter(status -> StringUtils.equals(ApiReportStatus.SUCCESS.name(), status)).toList();
|
||||
@ -318,7 +314,7 @@ public class ApiScenarioReportService {
|
||||
step.setStatus(ApiReportStatus.ERROR.name());
|
||||
} else if (requestStatus.contains(ApiReportStatus.FAKE_ERROR.name())) {
|
||||
step.setStatus(ApiReportStatus.FAKE_ERROR.name());
|
||||
} else if (successStatus.size() == children.size() - noControllerIds.size()) {
|
||||
} else if (successStatus.size() == children.size()) {
|
||||
step.setStatus(ApiReportStatus.SUCCESS.name());
|
||||
}
|
||||
} else if (stepTypes.contains(step.getStepType())) {
|
||||
|
@ -111,6 +111,22 @@
|
||||
headers.set('Content-type', 'application/json');
|
||||
return getCodeTemplate(innerLanguageType.value, { requestHeaders: headers });
|
||||
}
|
||||
case 'api_stop': {
|
||||
if (innerLanguageType.value === LanguageEnum.PYTHON) {
|
||||
return `
|
||||
import java
|
||||
StandardJMeterEngine = java.type('org.apache.jmeter.engine.StandardJMeterEngine')
|
||||
StandardJMeterEngine.stopThreadNow(ctx.getThread().getThreadName());
|
||||
`;
|
||||
}
|
||||
if (innerLanguageType.value === LanguageEnum.JAVASCRIPT) {
|
||||
return `
|
||||
StandardJMeterEngine = Java.type('org.apache.jmeter.engine.StandardJMeterEngine')
|
||||
StandardJMeterEngine.stopThreadNow(ctx.getThread().getThreadName());
|
||||
`;
|
||||
}
|
||||
return 'ctx.getEngine().stopThreadNow(ctx.getThread().getThreadName());';
|
||||
}
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
|
@ -59,7 +59,8 @@ export const SCRIPT_MENU: CommonScriptMenu[] = [
|
||||
...getInsertCommonScript(),
|
||||
{
|
||||
title: t('project.processor.terminationTest'),
|
||||
value: 'ctx.getEngine().stopTest();',
|
||||
value: 'api_stop',
|
||||
command: 'api_stop',
|
||||
},
|
||||
];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user