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
*/
public void setScenarioEnv(ApiScenarioWithBLOBs apiScenarioWithBLOBs) {
public void setScenarioEnv(ApiScenarioWithBLOBs apiScenarioWithBLOBs, RunScenarioRequest request) {
String environmentType = apiScenarioWithBLOBs.getEnvironmentType();
String environmentJson = apiScenarioWithBLOBs.getEnvironmentJson();
String environmentGroupId = apiScenarioWithBLOBs.getEnvironmentGroupId();
@ -232,6 +232,9 @@ public class ApiScenarioEnvService {
Map<String, String> map = environmentGroupProjectService.getEnvMap(environmentGroupId);
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));
}
@ -336,7 +339,7 @@ public class ApiScenarioEnvService {
StringBuilder builder = new StringBuilder();
for (ApiScenarioWithBLOBs apiScenarioWithBLOBs : apiScenarios) {
try {
this.setScenarioEnv(apiScenarioWithBLOBs);
this.setScenarioEnv(apiScenarioWithBLOBs, request);
boolean haveEnv = this.checkScenarioEnv(apiScenarioWithBLOBs, null);
if (!haveEnv) {
builder.append(apiScenarioWithBLOBs.getName()).append("; ");
@ -351,7 +354,7 @@ public class ApiScenarioEnvService {
} else if (StringUtils.equals(request.getRunMode(), ApiRunMode.SCHEDULE_SCENARIO.name())) {
for (ApiScenarioWithBLOBs apiScenarioWithBLOBs : apiScenarios) {
try {
this.setScenarioEnv(apiScenarioWithBLOBs);
this.setScenarioEnv(apiScenarioWithBLOBs, request);
} catch (Exception e) {
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.databind.DeserializationFeature;
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.variable.ScenarioVariable;
import io.metersphere.api.jmeter.ResourcePoolCalculation;
@ -20,6 +21,7 @@ import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.JvmInfoDTO;
import io.metersphere.dto.RunModeConfigDTO;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.EnvironmentGroupProjectService;
import io.metersphere.vo.BooleanPool;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
@ -31,7 +33,7 @@ import java.util.Map;
public class GenerateHashTreeUtil {
public static MsScenario parseScenarioDefinition(String scenarioDefinition) {
if(StringUtils.isNotEmpty(scenarioDefinition)) {
if (StringUtils.isNotEmpty(scenarioDefinition)) {
MsScenario scenario = JSONObject.parseObject(scenarioDefinition, MsScenario.class);
parse(scenarioDefinition, scenario, scenario.getId(), null);
return scenario;
@ -102,6 +104,21 @@ public class GenerateHashTreeUtil {
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) {
HashTree jmeterHashTree = new HashTree();
MsTestPlan testPlan = new MsTestPlan();
@ -114,6 +131,8 @@ public class GenerateHashTreeUtil {
group.setOnSampleError(scenario.getOnSampleError());
if (planEnvMap != null && planEnvMap.size() > 0) {
scenario.setEnvironmentMap(planEnvMap);
} else {
setScenarioEnv(scenario, item);
}
GenerateHashTreeUtil.parse(item.getScenarioDefinition(), scenario, item.getId(), reportType);

View File

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

View File

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