refactor(接口测试): 支持定时任务走默认环境

--bug=1028854 --user=赵勇 【接口测试】场景定时执行-默认环境-场景配置了环境-定时执行获取环境执行失败 https://www.tapd.cn/55049933/s/1404624

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-08-16 15:56:20 +08:00 committed by fit2-zhao
parent 75e2991219
commit eac007c3e4
3 changed files with 22 additions and 19 deletions

View File

@ -266,19 +266,4 @@ public class MsScenario extends MsTestElement {
} }
} }
} }
private void setEnv(Map<String, String> environmentMap, Map<String, EnvironmentConfig> envConfig) {
for (String projectId : environmentMap.keySet()) {
BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(environmentMap.get(projectId));
if (environment != null && StringUtils.isNotEmpty(environment.getConfig())) {
EnvironmentConfig env = JSONUtil.parseObject(environment.getConfig(), EnvironmentConfig.class);
env.setEnvironmentId(environment.getId());
envConfig.put(projectId, env);
if (StringUtils.equals(environment.getName(), MockConfigStaticData.MOCK_EVN_NAME)) {
this.setMockEnvironment(true);
}
}
}
}
} }

View File

@ -4,9 +4,11 @@ package io.metersphere.commons.utils;
import io.metersphere.api.dto.EnvironmentType; import io.metersphere.api.dto.EnvironmentType;
import io.metersphere.api.dto.definition.request.*; import io.metersphere.api.dto.definition.request.*;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.jmeter.NewDriverManager; import io.metersphere.api.jmeter.NewDriverManager;
import io.metersphere.api.jmeter.ResourcePoolCalculation; import io.metersphere.api.jmeter.ResourcePoolCalculation;
import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.TestResource; import io.metersphere.base.domain.TestResource;
import io.metersphere.base.domain.TestResourcePool; import io.metersphere.base.domain.TestResourcePool;
import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.base.mapper.TestResourcePoolMapper;
@ -19,6 +21,7 @@ import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.ProjectJarConfig; import io.metersphere.dto.ProjectJarConfig;
import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.dto.RunModeConfigDTO;
import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import io.metersphere.vo.BooleanPool; import io.metersphere.vo.BooleanPool;
@ -27,10 +30,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class GenerateHashTreeUtil { public class GenerateHashTreeUtil {
public static MsScenario parseScenarioDefinition(String scenarioDefinition) { public static MsScenario parseScenarioDefinition(String scenarioDefinition) {
@ -156,6 +156,7 @@ public class GenerateHashTreeUtil {
ParameterConfig config = new ParameterConfig(item.getProjectId(), false); ParameterConfig config = new ParameterConfig(item.getProjectId(), false);
config.setScenarioId(item.getId()); config.setScenarioId(item.getId());
config.setReportType(runRequest.getReportType()); config.setReportType(runRequest.getReportType());
config.setConfig(getEnv(scenario.getEnvironmentMap()));
if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) { if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
config.setRetryNum(runRequest.getRetryNum()); config.setRetryNum(runRequest.getRetryNum());
} }
@ -172,6 +173,22 @@ public class GenerateHashTreeUtil {
return jmeterHashTree; return jmeterHashTree;
} }
public static Map<String, EnvironmentConfig> getEnv(Map<String, String> environmentMap) {
Map<String, EnvironmentConfig> envConfig = new HashMap<>();
if(MapUtils.isEmpty(environmentMap)){
return envConfig;
}
for (String projectId : environmentMap.keySet()) {
BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(environmentMap.get(projectId));
if (environment != null && StringUtils.isNotEmpty(environment.getConfig())) {
EnvironmentConfig env = JSONUtil.parseObject(environment.getConfig(), EnvironmentConfig.class);
env.setEnvironmentId(environment.getId());
envConfig.put(projectId, env);
}
}
return envConfig;
}
public static boolean isSetReport(RunModeConfigDTO config) { public static boolean isSetReport(RunModeConfigDTO config) {
return config != null && isSetReport(config.getReportType()) && StringUtils.isNotEmpty(config.getReportName()); return config != null && isSetReport(config.getReportType()) && StringUtils.isNotEmpty(config.getReportName());
} }

View File

@ -990,6 +990,7 @@ public class ApiScenarioService {
if (scenario == null) { if (scenario == null) {
return null; return null;
} }
config.setConfig(GenerateHashTreeUtil.getEnv(scenario.getEnvironmentMap()));
scenario.setId(apiScenario.getId()); scenario.setId(apiScenario.getId());
GenerateHashTreeUtil.parse(apiScenario.getScenarioDefinition(), scenario); GenerateHashTreeUtil.parse(apiScenario.getScenarioDefinition(), scenario);
String environmentType = apiScenario.getEnvironmentType(); String environmentType = apiScenario.getEnvironmentType();