diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiParamConfig.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiParamConfig.java index 1e1e99086e..9be3409efa 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiParamConfig.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiParamConfig.java @@ -60,4 +60,14 @@ public class ApiParamConfig extends ParameterConfig { String pluginId = testElementClassPluginIdMap.get(msTestElement.getClass()); return pluginConfigMap.get(pluginId); } + + + /** + * 获取当前环境配置 + * @param projectId + * @return + */ + public EnvironmentInfoDTO getEnvConfig(String projectId) { + return getEnvConfig(); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java index 53a24996ae..23c14f00e1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java @@ -40,6 +40,7 @@ public class ApiScenarioParamConfig extends ApiParamConfig { * @param projectId * @return */ + @Override public EnvironmentInfoDTO getEnvConfig(String projectId) { if (BooleanUtils.isTrue(grouped)) { return projectEnvMap.get(projectId); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/assertion/MsAssertionConfig.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/assertion/MsAssertionConfig.java index 64d67a5c3a..ba83f7d89c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/assertion/MsAssertionConfig.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/assertion/MsAssertionConfig.java @@ -15,9 +15,9 @@ import java.util.List; public class MsAssertionConfig { /** * 是否启用全局断言 - * 默认为 false + * 默认为 true */ - private Boolean enableGlobal = false; + private Boolean enableGlobal = true; /** * 断言列表 */ diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/assertion/MsScenarioAssertionConfig.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/assertion/MsScenarioAssertionConfig.java new file mode 100644 index 0000000000..84db3bb04a --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/assertion/MsScenarioAssertionConfig.java @@ -0,0 +1,20 @@ +package io.metersphere.api.dto.assertion; + +import io.metersphere.project.api.assertion.MsAssertion; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +/** + * 断言设置 + * @Author: jianxing + * @CreateTime: 2023-11-23 17:26 + */ +@Data +public class MsScenarioAssertionConfig { + /** + * 断言列表 + */ + private List assertions = new ArrayList<>(0); +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/MsCommonElement.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/MsCommonElement.java index 8bd0f75e52..9be7b73ede 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/MsCommonElement.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/MsCommonElement.java @@ -22,16 +22,16 @@ public class MsCommonElement extends AbstractMsTestElement { * 前置处理器配置 */ @Valid - private MsProcessorConfig preProcessorConfig; + private MsProcessorConfig preProcessorConfig = new MsProcessorConfig(); /** * 后置处理器配置 */ @Valid - private MsProcessorConfig postProcessorConfig; + private MsProcessorConfig postProcessorConfig = new MsProcessorConfig(); /** * 断言配置 */ @Valid - private MsAssertionConfig assertionConfig; + private MsAssertionConfig assertionConfig = new MsAssertionConfig(); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ScenarioConfig.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ScenarioConfig.java index 365bc915e4..0b16a3ddf4 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ScenarioConfig.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ScenarioConfig.java @@ -1,6 +1,6 @@ package io.metersphere.api.dto.scenario; -import io.metersphere.api.dto.assertion.MsAssertionConfig; +import io.metersphere.api.dto.assertion.MsScenarioAssertionConfig; import io.metersphere.api.dto.request.processors.MsProcessorConfig; import lombok.Data; @@ -25,7 +25,7 @@ public class ScenarioConfig { /** * 断言配置 */ - private MsAssertionConfig assertionConfig = new MsAssertionConfig(); + private MsScenarioAssertionConfig assertionConfig = new MsScenarioAssertionConfig(); /** * 其他配置 */ 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 699dd38d11..f470afd930 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 @@ -3,7 +3,6 @@ package io.metersphere.api.parser.jmeter; import io.metersphere.api.constants.ApiConstants; import io.metersphere.api.dto.ApiParamConfig; -import io.metersphere.api.dto.ApiScenarioParamConfig; import io.metersphere.api.dto.assertion.MsAssertionConfig; import io.metersphere.api.dto.request.MsCommonElement; import io.metersphere.api.dto.request.http.MsHTTPElement; @@ -48,27 +47,47 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter assertions = assertionConfig.getAssertions(); + + // 添加环境断言 + if (assertionConfig.getEnableGlobal() && config instanceof ApiParamConfig apiParamConfig) { + EnvironmentInfoDTO envConfig = apiParamConfig.getEnvConfig(element.getProjectId()); + if (envConfig != null) { + assertions.addAll(envConfig.getConfig().getAssertionConfig().getAssertions()); + } } + + assertionConfig.getAssertions() + .forEach(assertion -> AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, isIgnoreAssertStatus(assertions))); + } + + /** + * 是否忽略状态码 + * @param assertions + * @return + */ + public static boolean isIgnoreAssertStatus(List assertions) { boolean isIgnoreStatus = false; - for (MsAssertion assertion : assertionConfig.getAssertions()) { + for (MsAssertion assertion : assertions) { if (assertion instanceof MsResponseCodeAssertion responseCodeAssertion) { // 如果状态码断言添加了不校验状态码,则所有断言忽略状态码 if (StringUtils.equals(responseCodeAssertion.getCondition(), MsAssertionCondition.UNCHECK.name())) { @@ -76,9 +95,7 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, finalIsIgnoreStatus)); + return isIgnoreStatus; } private void addProcessors(HashTree tree, MsCommonElement msCommonElement, ParameterConfig config, diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java index a30f1b16b9..48ce52b00b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsScenarioConverter.java @@ -9,8 +9,10 @@ import io.metersphere.api.dto.scenario.ScenarioConfig; import io.metersphere.api.dto.scenario.ScenarioStepConfig; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory; +import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFactory; import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; +import io.metersphere.project.api.assertion.MsAssertion; import io.metersphere.project.api.processor.MsProcessor; import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.dto.environment.EnvironmentInfoDTO; @@ -50,6 +52,30 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter assertions = scenarioConfig + .getAssertionConfig() + .getAssertions(); + + boolean ignoreAssertStatus = MsCommonElementConverter.isIgnoreAssertStatus(assertions); + + assertions.forEach(assertion -> + AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, ignoreAssertStatus)); } /** diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java index f5bbb81c42..cdf680401e 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java @@ -2,10 +2,9 @@ package io.metersphere.api.controller; import io.metersphere.api.constants.*; import io.metersphere.api.domain.*; +import io.metersphere.api.dto.assertion.MsAssertionConfig; import io.metersphere.api.dto.definition.ApiDefinitionAddRequest; import io.metersphere.api.dto.definition.ApiTestCaseAddRequest; -import io.metersphere.api.dto.assertion.MsAssertionConfig; -import io.metersphere.project.api.assertion.MsScriptAssertion; import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.response.ApiScenarioBatchOperationResponse; import io.metersphere.api.dto.response.OperationDataInfo; @@ -22,6 +21,8 @@ import io.metersphere.api.utils.JmeterElementConverterRegister; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.JmeterElementConverter; import io.metersphere.plugin.api.spi.MsTestElement; +import io.metersphere.project.api.assertion.MsResponseCodeAssertion; +import io.metersphere.project.api.assertion.MsScriptAssertion; import io.metersphere.project.api.processor.MsProcessor; import io.metersphere.project.api.processor.SQLProcessor; import io.metersphere.project.dto.environment.EnvironmentConfig; @@ -395,7 +396,11 @@ public class ApiScenarioControllerTests extends BaseTest { scriptAssertion.setScript("{}"); scriptAssertion.setName("script"); msAssertionConfig.setAssertions(List.of(scriptAssertion)); - scenarioConfig.setAssertionConfig(msAssertionConfig); + MsResponseCodeAssertion responseCodeAssertion = new MsResponseCodeAssertion(); + responseCodeAssertion.setExpectedValue("200"); + responseCodeAssertion.setCondition(MsAssertionCondition.EMPTY.name()); + responseCodeAssertion.setName("test"); + scenarioConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion); ScenarioOtherConfig scenarioOtherConfig = new ScenarioOtherConfig(); scenarioOtherConfig.setStepWaitTime(1000); scenarioOtherConfig.setFailureStrategy(ScenarioOtherConfig.FailureStrategy.CONTINUE.name()); @@ -730,6 +735,13 @@ public class ApiScenarioControllerTests extends BaseTest { List postRequestProcessors = postProcessorConfig.getApiProcessorConfig().getRequestProcessorConfig().getProcessors(); postRequestProcessors.add(envRequestScriptProcessor); postRequestProcessors.add(sqlProcessor); + + MsResponseCodeAssertion responseCodeAssertion = new MsResponseCodeAssertion(); + responseCodeAssertion.setExpectedValue("200"); + responseCodeAssertion.setCondition(MsAssertionCondition.EMPTY.name()); + responseCodeAssertion.setName("test"); + environmentConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion); + environmentConfig.setPluginConfigMap(pluginConfigMap); envRequest.setConfig(environmentConfig); Environment environment = environmentService.add(envRequest, "admin", null);