From 3788084c7a42f1ebd7813391ffa54729b175955d Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Tue, 25 Oct 2022 15:15:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E4=BD=BF=E7=94=A8=E7=8E=AF=E5=A2=83=E4=B8=AD=E5=B8=A6?= =?UTF-8?q?=E7=9A=84mock=E5=87=BD=E6=95=B0=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1018823 --user=王孝刚 【接口测试】接口场景中可以使用变量「常量-函数」接口定义中测试无法使用 https://www.tapd.cn/55049933/s/1276193 --- .../dto/definition/request/ElementUtil.java | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index f520abcf4c..c50d8a7766 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -852,9 +852,12 @@ public class ElementUtil { // 场景变量 if (CollectionUtils.isNotEmpty(variables)) { - variables.stream().filter(ScenarioVariable::isConstantValid).forEach(keyValue -> arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")); - - List variableList = variables.stream().filter(ScenarioVariable::isListValid).collect(Collectors.toList()); + variables.stream() + .filter(ScenarioVariable::isConstantValid).forEach(keyValue -> + arguments.addArgument(keyValue.getName(), + keyValue.getValue(), "=")); + List variableList = variables.stream() + .filter(ScenarioVariable::isListValid).collect(Collectors.toList()); variableList.forEach(item -> { String[] arrays = item.getValue().split(","); for (int i = 0; i < arrays.length; i++) { @@ -863,12 +866,24 @@ public class ElementUtil { }); } // 环境通用变量 - if (config.isEffective(projectId) && config.getConfig().get(projectId).getCommonConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getCommonConfig().getVariables())) { + if (config.isEffective(projectId) + && config.getConfig().get(projectId).getCommonConfig() != null + && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getCommonConfig().getVariables())) { //常量 - List constants = config.getConfig().get(projectId).getCommonConfig().getVariables().stream().filter(ScenarioVariable::isConstantValid).filter(ScenarioVariable::isEnable).collect(Collectors.toList()); - constants.forEach(keyValue -> arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")); + List constants = config.getConfig().get(projectId).getCommonConfig().getVariables().stream() + .filter(ScenarioVariable::isConstantValid) + .filter(ScenarioVariable::isEnable) + .collect(Collectors.toList()); + constants.forEach(keyValue -> + arguments.addArgument(keyValue.getName(), + keyValue.getValue() != null && keyValue.getValue().startsWith("@") + ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) + : keyValue.getValue(), "=")); // List类型的变量 - List variableList = config.getConfig().get(projectId).getCommonConfig().getVariables().stream().filter(ScenarioVariable::isListValid).filter(ScenarioVariable::isEnable).collect(Collectors.toList()); + List variableList = config.getConfig().get(projectId).getCommonConfig().getVariables().stream() + .filter(ScenarioVariable::isListValid) + .filter(ScenarioVariable::isEnable) + .collect(Collectors.toList()); variableList.forEach(item -> { String[] arrays = item.getValue().split(","); for (int i = 0; i < arrays.length; i++) { @@ -888,9 +903,13 @@ public class ElementUtil { public static void addApiVariables(ParameterConfig config, HashTree httpSamplerTree, String projectId) { if (config.isEffective(projectId) && config.getConfig().get(projectId).getCommonConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getCommonConfig().getVariables())) { - ElementUtil.addApiCsvDataSet(httpSamplerTree, config.getConfig().get(projectId).getCommonConfig().getVariables(), config, "shareMode.group"); - ElementUtil.addCounter(httpSamplerTree, config.getConfig().get(projectId).getCommonConfig().getVariables(), false); - ElementUtil.addRandom(httpSamplerTree, config.getConfig().get(projectId).getCommonConfig().getVariables()); + ElementUtil.addApiCsvDataSet(httpSamplerTree, + config.getConfig().get(projectId).getCommonConfig().getVariables(), + config, "shareMode.group"); + ElementUtil.addCounter(httpSamplerTree, + config.getConfig().get(projectId).getCommonConfig().getVariables(), false); + ElementUtil.addRandom(httpSamplerTree, + config.getConfig().get(projectId).getCommonConfig().getVariables()); } } @@ -900,15 +919,24 @@ public class ElementUtil { List environment = environmentService.list(projectId); EnvironmentConfig dataConfig = null; List dataName = new ArrayList<>(); - List orgDataSource = environment.stream().filter(ApiTestEnvironmentWithBLOBs -> ApiTestEnvironmentWithBLOBs.getConfig().contains(dataSourceId)).collect(Collectors.toList()); + List orgDataSource = environment.stream() + .filter(ApiTestEnvironmentWithBLOBs -> + ApiTestEnvironmentWithBLOBs.getConfig().contains(dataSourceId)).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(orgDataSource)) { dataConfig = JSONUtil.parseObject(orgDataSource.get(0).getConfig(), EnvironmentConfig.class); if (CollectionUtils.isNotEmpty(dataConfig.getDatabaseConfigs())) { - dataName = dataConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getId().equals(dataSourceId)).map(DatabaseConfig::getName).collect(Collectors.toList()); + dataName = dataConfig.getDatabaseConfigs().stream() + .filter(DatabaseConfig -> + DatabaseConfig.getId().equals(dataSourceId)) + .map(DatabaseConfig::getName) + .collect(Collectors.toList()); } } List finalDataName = dataName; - List collect = envConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getName().equals(finalDataName.get(0))).collect(Collectors.toList()); + List collect = envConfig.getDatabaseConfigs().stream() + .filter(DatabaseConfig -> + DatabaseConfig.getName().equals(finalDataName.get(0))) + .collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(collect)) { return collect.get(0); }