fix(接口测试): 修复场景列表批量执行选择环境问题

This commit is contained in:
fit2-zhao 2022-01-04 15:09:18 +08:00 committed by fit2-zhao
parent 0af786a962
commit 9f7e35cbd5
4 changed files with 29 additions and 5 deletions

View File

@ -216,7 +216,7 @@ public class ApiScenarioEnvService {
* *
* @param apiScenarioWithBLOBs * @param apiScenarioWithBLOBs
*/ */
public void setScenarioEnv(ApiScenarioWithBLOBs apiScenarioWithBLOBs) { public void setScenarioEnv(ApiScenarioWithBLOBs apiScenarioWithBLOBs, RunScenarioRequest request) {
String environmentType = apiScenarioWithBLOBs.getEnvironmentType(); String environmentType = apiScenarioWithBLOBs.getEnvironmentType();
String environmentJson = apiScenarioWithBLOBs.getEnvironmentJson(); String environmentJson = apiScenarioWithBLOBs.getEnvironmentJson();
String environmentGroupId = apiScenarioWithBLOBs.getEnvironmentGroupId(); String environmentGroupId = apiScenarioWithBLOBs.getEnvironmentGroupId();
@ -232,6 +232,9 @@ public class ApiScenarioEnvService {
Map<String, String> map = environmentGroupProjectService.getEnvMap(environmentGroupId); Map<String, String> map = environmentGroupProjectService.getEnvMap(environmentGroupId);
scenario.setEnvironmentMap(map); scenario.setEnvironmentMap(map);
} }
if (request != null && request.getConfig() != null && request.getConfig().getEnvMap() != null && !request.getConfig().getEnvMap().isEmpty()) {
scenario.setEnvironmentMap(request.getConfig().getEnvMap());
}
apiScenarioWithBLOBs.setScenarioDefinition(JSON.toJSONString(scenario)); apiScenarioWithBLOBs.setScenarioDefinition(JSON.toJSONString(scenario));
} }
@ -336,7 +339,7 @@ public class ApiScenarioEnvService {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (ApiScenarioWithBLOBs apiScenarioWithBLOBs : apiScenarios) { for (ApiScenarioWithBLOBs apiScenarioWithBLOBs : apiScenarios) {
try { try {
this.setScenarioEnv(apiScenarioWithBLOBs); this.setScenarioEnv(apiScenarioWithBLOBs, request);
boolean haveEnv = this.checkScenarioEnv(apiScenarioWithBLOBs, null); boolean haveEnv = this.checkScenarioEnv(apiScenarioWithBLOBs, null);
if (!haveEnv) { if (!haveEnv) {
builder.append(apiScenarioWithBLOBs.getName()).append("; "); builder.append(apiScenarioWithBLOBs.getName()).append("; ");
@ -351,7 +354,7 @@ public class ApiScenarioEnvService {
} else if (StringUtils.equals(request.getRunMode(), ApiRunMode.SCHEDULE_SCENARIO.name())) { } else if (StringUtils.equals(request.getRunMode(), ApiRunMode.SCHEDULE_SCENARIO.name())) {
for (ApiScenarioWithBLOBs apiScenarioWithBLOBs : apiScenarios) { for (ApiScenarioWithBLOBs apiScenarioWithBLOBs : apiScenarios) {
try { try {
this.setScenarioEnv(apiScenarioWithBLOBs); this.setScenarioEnv(apiScenarioWithBLOBs, request);
} catch (Exception e) { } catch (Exception e) {
MSException.throwException("定时任务设置场景环境失败场景ID " + apiScenarioWithBLOBs.getId()); MSException.throwException("定时任务设置场景环境失败场景ID " + apiScenarioWithBLOBs.getId());
} }

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
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.jmeter.ResourcePoolCalculation; import io.metersphere.api.jmeter.ResourcePoolCalculation;
@ -20,6 +21,7 @@ import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.JvmInfoDTO; import io.metersphere.dto.JvmInfoDTO;
import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.dto.RunModeConfigDTO;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.EnvironmentGroupProjectService;
import io.metersphere.vo.BooleanPool; import io.metersphere.vo.BooleanPool;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
@ -31,7 +33,7 @@ import java.util.Map;
public class GenerateHashTreeUtil { public class GenerateHashTreeUtil {
public static MsScenario parseScenarioDefinition(String scenarioDefinition) { public static MsScenario parseScenarioDefinition(String scenarioDefinition) {
if(StringUtils.isNotEmpty(scenarioDefinition)) { if (StringUtils.isNotEmpty(scenarioDefinition)) {
MsScenario scenario = JSONObject.parseObject(scenarioDefinition, MsScenario.class); MsScenario scenario = JSONObject.parseObject(scenarioDefinition, MsScenario.class);
parse(scenarioDefinition, scenario, scenario.getId(), null); parse(scenarioDefinition, scenario, scenario.getId(), null);
return scenario; return scenario;
@ -102,6 +104,21 @@ public class GenerateHashTreeUtil {
return null; return null;
} }
public static void setScenarioEnv(MsScenario scenario, ApiScenarioWithBLOBs apiScenarioWithBLOBs) {
String environmentType = apiScenarioWithBLOBs.getEnvironmentType();
String environmentJson = apiScenarioWithBLOBs.getEnvironmentJson();
String environmentGroupId = apiScenarioWithBLOBs.getEnvironmentGroupId();
if (StringUtils.isBlank(environmentType)) {
environmentType = EnvironmentType.JSON.toString();
}
if (StringUtils.equals(environmentType, EnvironmentType.JSON.toString())) {
scenario.setEnvironmentMap(JSON.parseObject(environmentJson, Map.class));
} else if (StringUtils.equals(environmentType, EnvironmentType.GROUP.toString())) {
Map<String, String> map = CommonBeanFactory.getBean(EnvironmentGroupProjectService.class).getEnvMap(environmentGroupId);
scenario.setEnvironmentMap(map);
}
}
public static HashTree generateHashTree(ApiScenarioWithBLOBs item, String reportId, Map<String, String> planEnvMap, String reportType) { public static HashTree generateHashTree(ApiScenarioWithBLOBs item, String reportId, Map<String, String> planEnvMap, String reportType) {
HashTree jmeterHashTree = new HashTree(); HashTree jmeterHashTree = new HashTree();
MsTestPlan testPlan = new MsTestPlan(); MsTestPlan testPlan = new MsTestPlan();
@ -114,6 +131,8 @@ public class GenerateHashTreeUtil {
group.setOnSampleError(scenario.getOnSampleError()); group.setOnSampleError(scenario.getOnSampleError());
if (planEnvMap != null && planEnvMap.size() > 0) { if (planEnvMap != null && planEnvMap.size() > 0) {
scenario.setEnvironmentMap(planEnvMap); scenario.setEnvironmentMap(planEnvMap);
} else {
setScenarioEnv(scenario, item);
} }
GenerateHashTreeUtil.parse(item.getScenarioDefinition(), scenario, item.getId(), reportType); GenerateHashTreeUtil.parse(item.getScenarioDefinition(), scenario, item.getId(), reportType);

View File

@ -1895,7 +1895,7 @@ public class ApiAutomationService {
public boolean checkScenarioEnv(String scenarioId) { public boolean checkScenarioEnv(String scenarioId) {
ApiScenarioWithBLOBs apiScenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(scenarioId); ApiScenarioWithBLOBs apiScenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(scenarioId);
apiScenarioEnvService.setScenarioEnv(apiScenarioWithBLOBs); apiScenarioEnvService.setScenarioEnv(apiScenarioWithBLOBs, null);
return apiScenarioEnvService.checkScenarioEnv(apiScenarioWithBLOBs, null); return apiScenarioEnvService.checkScenarioEnv(apiScenarioWithBLOBs, null);
} }

View File

@ -119,6 +119,7 @@ export default {
this.runModeVisible = true; this.runModeVisible = true;
this.getResourcePools(); this.getResourcePools();
this.getWsProjects(); this.getWsProjects();
this.runConfig.environmentType = ENV_TYPE.JSON;
}, },
changeMode() { changeMode() {
this.runConfig.runWithinResourcePool = false; this.runConfig.runWithinResourcePool = false;
@ -131,6 +132,7 @@ export default {
mode: "serial", mode: "serial",
reportType: "iddReport", reportType: "iddReport",
reportName: "", reportName: "",
environmentType: ENV_TYPE.JSON,
runWithinResourcePool: false, runWithinResourcePool: false,
resourcePoolId: null, resourcePoolId: null,
}; };