fix(测试跟踪): 修复测试计划不选择环境关联场景和接口执行时报告没有环境的问题

--bug=1028771 --user=宋天阳
【测试跟踪】测试计划-场景用例列表-操作-执行-任务中心-报告未显示运行环境字段
https://www.tapd.cn/55049933/s/1404646;--bug=1028789
--user=宋天阳 【测试跟踪】接口用例-操作-执行/批量执行-执行结果未显示运行环境字段
https://www.tapd.cn/55049933/s/1404648
This commit is contained in:
song-tianyang 2023-08-16 16:17:19 +08:00 committed by 建国
parent 231d099655
commit 557b98e8f8
4 changed files with 68 additions and 21 deletions

View File

@ -108,8 +108,8 @@ public class ApiCaseExecuteService {
List<String> apiCaseIds = planApiCases.stream().map(TestPlanApiCaseInfoDTO::getApiCaseId).collect(Collectors.toList()); List<String> apiCaseIds = planApiCases.stream().map(TestPlanApiCaseInfoDTO::getApiCaseId).collect(Collectors.toList());
ApiTestCaseExample caseExample = new ApiTestCaseExample(); ApiTestCaseExample caseExample = new ApiTestCaseExample();
caseExample.createCriteria().andIdIn(apiCaseIds); caseExample.createCriteria().andIdIn(apiCaseIds);
List<ApiTestCase> apiTestCases = apiTestCaseMapper.selectByExample(caseExample); List<ApiTestCaseWithBLOBs> apiTestCases = apiTestCaseMapper.selectByExampleWithBLOBs(caseExample);
Map<String, ApiTestCase> caseMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getId, a -> a, (k1, k2) -> k1)); Map<String, ApiTestCaseWithBLOBs> caseMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getId, a -> a, (k1, k2) -> k1));
// 资源池 // 资源池
String resourcePoolId = null; String resourcePoolId = null;
if (request.getConfig() != null && GenerateHashTreeUtil.isResourcePool(request.getConfig().getResourcePoolId()).isPool()) { if (request.getConfig() != null && GenerateHashTreeUtil.isResourcePool(request.getConfig().getResourcePoolId()).isPool()) {
@ -120,15 +120,20 @@ public class ApiCaseExecuteService {
//处理环境配置为空时的情况 //处理环境配置为空时的情况
RunModeConfigDTO runModeConfigDTO = new RunModeConfigDTO(); RunModeConfigDTO runModeConfigDTO = new RunModeConfigDTO();
BeanUtils.copyBean(runModeConfigDTO, request.getConfig()); BeanUtils.copyBean(runModeConfigDTO, request.getConfig());
ApiTestCase testCase = caseMap.get(testPlanApiCase.getApiCaseId()); ApiTestCaseWithBLOBs testCase = caseMap.get(testPlanApiCase.getApiCaseId());
if (testCase == null) { if (testCase == null) {
continue; continue;
} }
if (MapUtils.isEmpty(runModeConfigDTO.getEnvMap())) { if (MapUtils.isEmpty(runModeConfigDTO.getEnvMap())) {
if (StringUtils.isEmpty(testPlanApiCase.getEnvironmentId())) {
JSONObject jsonObject = new JSONObject(testCase.getRequest());
runModeConfigDTO.setEnvMap(this.getEnvMap(jsonObject, testCase.getProjectId()));
} else {
runModeConfigDTO.setEnvMap(new HashMap<>() {{ runModeConfigDTO.setEnvMap(new HashMap<>() {{
this.put(testCase.getProjectId(), testPlanApiCase.getEnvironmentId()); this.put(testCase.getProjectId(), testPlanApiCase.getEnvironmentId());
}}); }});
} }
}
ApiDefinitionExecResultWithBLOBs report = ApiDefinitionExecResultUtil.addResult(request, runModeConfigDTO, testPlanApiCase, status, testCase, resourcePoolId); ApiDefinitionExecResultWithBLOBs report = ApiDefinitionExecResultUtil.addResult(request, runModeConfigDTO, testPlanApiCase, status, testCase, resourcePoolId);
executeQueue.put(testPlanApiCase.getId(), report); executeQueue.put(testPlanApiCase.getId(), report);
responseDTOS.add(new MsExecResponseDTO(testPlanApiCase.getId(), report.getId(), request.getTriggerMode())); responseDTOS.add(new MsExecResponseDTO(testPlanApiCase.getId(), report.getId(), request.getTriggerMode()));

View File

@ -298,6 +298,16 @@ public class ApiScenarioExecuteService {
} }
// 获取场景用例单独的执行环境 // 获取场景用例单独的执行环境
Map<String, String> planEnvMap = apiScenarioEnvService.getPlanScenarioEnv(planApiScenario, configEnvMap); Map<String, String> planEnvMap = apiScenarioEnvService.getPlanScenarioEnv(planApiScenario, configEnvMap);
if (MapUtils.isEmpty(planEnvMap)) {
Map<String, List<String>> projectEnvMap = apiScenarioEnvService.selectApiScenarioEnv(new ArrayList<>() {{
this.add(scenario);
}});
projectEnvMap.forEach((projectId, envList) -> {
if (CollectionUtils.isNotEmpty(envList)) {
planEnvMap.put(projectId, envList.get(0));
}
});
}
if (StringUtils.isEmpty(request.getProjectId())) { if (StringUtils.isEmpty(request.getProjectId())) {
request.setProjectId(extTestPlanScenarioCaseMapper.getProjectIdById(testPlanScenarioId)); request.setProjectId(extTestPlanScenarioCaseMapper.getProjectIdById(testPlanScenarioId));
} }

View File

@ -13,6 +13,8 @@ import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.plan.AutomationsRunInfoDTO; import io.metersphere.api.dto.plan.AutomationsRunInfoDTO;
import io.metersphere.api.dto.plan.TestPlanApiCaseBatchRequest; import io.metersphere.api.dto.plan.TestPlanApiCaseBatchRequest;
import io.metersphere.api.dto.plan.TestPlanApiCaseInfoDTO; import io.metersphere.api.dto.plan.TestPlanApiCaseInfoDTO;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.exec.api.ApiCaseExecuteService; import io.metersphere.api.exec.api.ApiCaseExecuteService;
import io.metersphere.api.exec.api.ApiExecuteService; import io.metersphere.api.exec.api.ApiExecuteService;
import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.JMeterService;
@ -30,6 +32,7 @@ import io.metersphere.commons.utils.*;
import io.metersphere.dto.MsExecResponseDTO; import io.metersphere.dto.MsExecResponseDTO;
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.log.vo.OperatingLogDetails; import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.request.OrderRequest; import io.metersphere.request.OrderRequest;
import io.metersphere.request.ResetOrderRequest; import io.metersphere.request.ResetOrderRequest;
@ -44,6 +47,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.json.JSONObject;
import org.mybatis.spring.SqlSessionUtils; import org.mybatis.spring.SqlSessionUtils;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -98,6 +102,8 @@ public class TestPlanApiCaseService {
private ApiCaseResultService apiCaseResultService; private ApiCaseResultService apiCaseResultService;
@Resource @Resource
private RedisTemplateService redisTemplateService; private RedisTemplateService redisTemplateService;
@Resource
private BaseEnvironmentService baseEnvironmentService;
public List<TestPlanApiCaseDTO> list(ApiTestCaseRequest request) { public List<TestPlanApiCaseDTO> list(ApiTestCaseRequest request) {
request.setProjectId(null); request.setProjectId(null);
@ -776,6 +782,36 @@ public class TestPlanApiCaseService {
return nodeTrees; return nodeTrees;
} }
public Map<String, String> getEnvMap(JSONObject request, String projectId) {
Map<String, String> projectEnvMap = new HashMap<>();
if (!request.has(PropertyConstant.TYPE)) {
return projectEnvMap;
}
if (StringUtils.equals(ElementConstants.HTTP_SAMPLER, request.optString(PropertyConstant.TYPE))) {
if (StringUtils.isNotEmpty(request.optString(PropertyConstant.ENVIRONMENT))) {
//记录运行环境ID
projectEnvMap.put(projectId, request.optString(PropertyConstant.ENVIRONMENT));
}
}
if (StringUtils.equals(ElementConstants.JDBC_SAMPLER, request.optString(PropertyConstant.TYPE))) {
if (request.has(PropertyConstant.ENVIRONMENT) && request.has(PropertyConstant.DATASOURCE_ID)) {
ApiTestEnvironmentWithBLOBs environment = baseEnvironmentService.get(request.optString(PropertyConstant.ENVIRONMENT));
if (environment != null && environment.getConfig() != null) {
EnvironmentConfig envConfig = JSON.parseObject(environment.getConfig(), EnvironmentConfig.class);
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
for (DatabaseConfig item : envConfig.getDatabaseConfigs()) {
if (StringUtils.equals(item.getId(), request.optString(PropertyConstant.DATASOURCE_ID))) {
//记录运行环境ID
projectEnvMap.put(projectId, request.optString(PropertyConstant.ENVIRONMENT));
}
}
}
}
}
}
return projectEnvMap;
}
public void run(String testId, String reportId) { public void run(String testId, String reportId) {
TestPlanApiCase testPlanApiCase = testPlanApiCaseMapper.selectByPrimaryKey(testId); TestPlanApiCase testPlanApiCase = testPlanApiCaseMapper.selectByPrimaryKey(testId);
if (testPlanApiCase == null) { if (testPlanApiCase == null) {
@ -796,9 +832,12 @@ public class TestPlanApiCaseService {
runModeConfigDTO.setEnvMap(new HashMap<>() {{ runModeConfigDTO.setEnvMap(new HashMap<>() {{
this.put(result.getProjectId(), testPlanApiCase.getEnvironmentId()); this.put(result.getProjectId(), testPlanApiCase.getEnvironmentId());
}}); }});
} else {
JSONObject jsonObject = new JSONObject(apiCase.getRequest());
runModeConfigDTO.setEnvMap(this.getEnvMap(jsonObject, apiCase.getProjectId()));
}
runModeConfigDTO.setResourcePoolId(runModeConfigDTO.getResourcePoolId()); runModeConfigDTO.setResourcePoolId(runModeConfigDTO.getResourcePoolId());
result.setEnvConfig(JSON.toJSONString(runModeConfigDTO)); result.setEnvConfig(JSON.toJSONString(runModeConfigDTO));
}
result.setActuator(runModeConfigDTO.getResourcePoolId()); result.setActuator(runModeConfigDTO.getResourcePoolId());
apiCaseResultService.batchSave(result); apiCaseResultService.batchSave(result);
apiCase.setId(testId); apiCase.setId(testId);

View File

@ -3,9 +3,9 @@ package io.metersphere.service.plan;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import io.metersphere.api.dto.ApiCaseRelevanceRequest; import io.metersphere.api.dto.ApiCaseRelevanceRequest;
import io.metersphere.api.dto.EnvironmentCheckDTO;
import io.metersphere.api.dto.EnvironmentType; import io.metersphere.api.dto.EnvironmentType;
import io.metersphere.api.dto.RelevanceScenarioRequest; import io.metersphere.api.dto.RelevanceScenarioRequest;
import io.metersphere.api.dto.EnvironmentCheckDTO;
import io.metersphere.api.dto.automation.*; import io.metersphere.api.dto.automation.*;
import io.metersphere.api.dto.plan.*; import io.metersphere.api.dto.plan.*;
import io.metersphere.api.exec.scenario.ApiScenarioEnvService; import io.metersphere.api.exec.scenario.ApiScenarioEnvService;
@ -240,7 +240,11 @@ public class TestPlanScenarioCaseService {
EnvironmentCheckDTO scenarioEnv = apiAutomationService.getApiScenarioProjectId(id); EnvironmentCheckDTO scenarioEnv = apiAutomationService.getApiScenarioProjectId(id);
list = new ArrayList<>(scenarioEnv.getProjectIds()); list = new ArrayList<>(scenarioEnv.getProjectIds());
} }
list.forEach(l -> newEnvMap.put(l, envMap == null ? StringUtils.EMPTY : envMap.getOrDefault(l, StringUtils.EMPTY))); for (String s : list) {
if (envMap != null && StringUtils.isNotEmpty(envMap.get(s))) {
newEnvMap.put(s, envMap.get(s));
}
}
TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario();
testPlanApiScenario.setId(UUID.randomUUID().toString()); testPlanApiScenario.setId(UUID.randomUUID().toString());
testPlanApiScenario.setCreateUser(SessionUtils.getUserId()); testPlanApiScenario.setCreateUser(SessionUtils.getUserId());
@ -305,17 +309,6 @@ public class TestPlanScenarioCaseService {
if (CollectionUtils.isEmpty(planCaseIdList)) { if (CollectionUtils.isEmpty(planCaseIdList)) {
MSException.throwException("未找到执行场景!"); MSException.throwException("未找到执行场景!");
} }
RunModeConfigDTO config = testPlanScenarioRequest.getConfig();
if (config != null) {
String envType = config.getEnvironmentType();
String envGroupId = config.getEnvironmentGroupId();
Map<String, String> envMap = config.getEnvMap();
if ((StringUtils.equals(envType, EnvironmentType.JSON.toString()) && envMap != null && !envMap.isEmpty())
|| (StringUtils.equals(envType, EnvironmentType.GROUP.toString()) && StringUtils.isNotBlank(envGroupId))) {
// 更新场景用例环境信息运行时从数据库读取最新环境
this.setScenarioEnv(new ArrayList<>(), planCaseIdList, testPlanScenarioRequest.getConfig());
}
}
jMeterService.verifyPool(testPlanScenarioRequest.getProjectId(), testPlanScenarioRequest.getConfig()); jMeterService.verifyPool(testPlanScenarioRequest.getProjectId(), testPlanScenarioRequest.getConfig());
planCaseIdList.forEach(item -> { planCaseIdList.forEach(item -> {