From e7555e436ca02064f4d6cb8024ad8294d7dd0a93 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Sun, 7 Apr 2024 16:42:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=9D=A1=E4=BB=B6=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=99=A8=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/controller/ConditionUtils.java | 31 +++++++++ .../request/controller/MsIfController.java | 68 +------------------ .../controller/loop/MsWhileVariable.java | 26 +------ .../api/service/ApiTaskCenterService.java | 5 +- .../scenario/ApiScenarioReportService.java | 8 +-- .../ms-common-script/ms-script-menu.vue | 16 +++++ .../business/ms-common-script/utils.ts | 3 +- 7 files changed, 58 insertions(+), 99 deletions(-) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/ConditionUtils.java diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/ConditionUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/ConditionUtils.java new file mode 100644 index 0000000000..cf45ad29d3 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/ConditionUtils.java @@ -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 + "\""; + }; + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsIfController.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsIfController.java index c07bfee7c5..c216182f1f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsIfController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/MsIfController.java @@ -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 + ")}"; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/loop/MsWhileVariable.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/loop/MsWhileVariable.java index 950ff0efef..95711d7fa8 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/loop/MsWhileVariable.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/controller/loop/MsWhileVariable.java @@ -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); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java index bf34c44b06..000a99c8eb 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java @@ -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); } }); }); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioReportService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioReportService.java index 623bfe86bf..dd42a100ee 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioReportService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioReportService.java @@ -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 controllerIds = children.stream().filter(child -> stepTypes.contains(child.getStepType())).map(ApiScenarioReportStepDTO::getStepId).toList(); - //看map中有没有这些id 如果没有 需要返回几个没有 - List noControllerIds = controllerIds.stream().filter(controllerId -> !scenarioReportStepMap.containsKey(controllerId)).toList(); - //获取所有的子请求的状态 + //获取所有的子请求的状态 List requestStatus = children.stream().map(ApiScenarioReportStepDTO::getStatus).toList(); //过滤出来SUCCESS的状态 List 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())) { diff --git a/frontend/src/components/business/ms-common-script/ms-script-menu.vue b/frontend/src/components/business/ms-common-script/ms-script-menu.vue index c7ae1ba7d9..800c001afb 100644 --- a/frontend/src/components/business/ms-common-script/ms-script-menu.vue +++ b/frontend/src/components/business/ms-common-script/ms-script-menu.vue @@ -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 ''; } diff --git a/frontend/src/components/business/ms-common-script/utils.ts b/frontend/src/components/business/ms-common-script/utils.ts index a0d093d95c..992bf593b2 100644 --- a/frontend/src/components/business/ms-common-script/utils.ts +++ b/frontend/src/components/business/ms-common-script/utils.ts @@ -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', }, ];