refactor(接口测试): 测试计划关联接口用例执行时生成脚本优化

This commit is contained in:
fit2-zhao 2022-12-16 11:15:01 +08:00 committed by fit2-zhao
parent fc292d686b
commit dd3b34f0d5
8 changed files with 56 additions and 40 deletions

View File

@ -83,23 +83,21 @@ public class ApiCaseExecuteService {
if (StringUtils.equals(EnvironmentType.GROUP.toString(), request.getConfig().getEnvironmentType()) && StringUtils.isNotEmpty(request.getConfig().getEnvironmentGroupId())) { if (StringUtils.equals(EnvironmentType.GROUP.toString(), request.getConfig().getEnvironmentType()) && StringUtils.isNotEmpty(request.getConfig().getEnvironmentGroupId())) {
request.getConfig().setEnvMap(environmentGroupProjectService.getEnvMap(request.getConfig().getEnvironmentGroupId())); request.getConfig().setEnvMap(environmentGroupProjectService.getEnvMap(request.getConfig().getEnvironmentGroupId()));
} }
LoggerUtil.debug("开始查询测试计划用例"); LoggerUtil.info("开始查询测试计划用例", request.getPlanIds().size());
TestPlanApiCaseExample example = new TestPlanApiCaseExample(); List<TestPlanApiCase> planApiCases = this.selectByPlanApiCaseIds(request.getPlanIds());
example.createCriteria().andIdIn(request.getPlanIds());
example.setOrderByClause("`order` DESC");
List<TestPlanApiCase> planApiCases = testPlanApiCaseMapper.selectByExample(example);
if (CollectionUtils.isEmpty(planApiCases)) { if (CollectionUtils.isEmpty(planApiCases)) {
return responseDTOS; return responseDTOS;
} }
if (StringUtils.isEmpty(request.getTriggerMode())) { if (StringUtils.isEmpty(request.getTriggerMode())) {
request.setTriggerMode(ApiRunMode.API_PLAN.name()); request.setTriggerMode(ApiRunMode.API_PLAN.name());
} }
LoggerUtil.debug("查询到测试计划用例 " + planApiCases.size()); LoggerUtil.info("查询到测试计划用例 " + planApiCases.size());
Map<String, ApiDefinitionExecResultWithBLOBs> executeQueue = request.isRerun() ? request.getExecuteQueue() : new LinkedHashMap<>(); Map<String, ApiDefinitionExecResultWithBLOBs> executeQueue = request.isRerun() ? request.getExecuteQueue() : new LinkedHashMap<>();
String status = request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString()) ? ApiReportStatus.PENDING.name()
: ApiReportStatus.RUNNING.name(); String status = StringUtils.equals(request.getConfig().getMode(), RunModeConstants.SERIAL.toString())
? ApiReportStatus.PENDING.name() : ApiReportStatus.RUNNING.name();
// 查出用例 // 查出用例
List<String> apiCaseIds = planApiCases.stream().map(TestPlanApiCase::getApiCaseId).collect(Collectors.toList()); List<String> apiCaseIds = planApiCases.stream().map(TestPlanApiCase::getApiCaseId).collect(Collectors.toList());
@ -117,23 +115,24 @@ public class ApiCaseExecuteService {
//处理环境配置为空时的情况 //处理环境配置为空时的情况
RunModeConfigDTO runModeConfigDTO = new RunModeConfigDTO(); RunModeConfigDTO runModeConfigDTO = new RunModeConfigDTO();
BeanUtils.copyBean(runModeConfigDTO, request.getConfig()); BeanUtils.copyBean(runModeConfigDTO, request.getConfig());
if (MapUtils.isEmpty(runModeConfigDTO.getEnvMap())) { ApiTestCase testCase = caseMap.get(testPlanApiCase.getApiCaseId());
ApiTestCase testCase = caseMap.get(testPlanApiCase.getApiCaseId()); if (testCase == null) {
if (testCase != null) { continue;
runModeConfigDTO.setEnvMap(new HashMap<>() {{
this.put(testCase.getProjectId(), testPlanApiCase.getEnvironmentId());
}});
}
} }
ApiDefinitionExecResultWithBLOBs report = ApiDefinitionExecResultUtil.addResult(request, runModeConfigDTO, testPlanApiCase, status, caseMap, resourcePoolId); if (MapUtils.isEmpty(runModeConfigDTO.getEnvMap())) {
runModeConfigDTO.setEnvMap(new HashMap<>() {{
this.put(testCase.getProjectId(), testPlanApiCase.getEnvironmentId());
}});
}
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()));
LoggerUtil.debug("预生成测试用例结果报告:" + report.getName() + ", ID " + report.getId()); LoggerUtil.info("预生成测试用例结果报告:" + report.getName(), report.getId());
} }
apiCaseResultService.batchSave(executeQueue); apiCaseResultService.batchSave(executeQueue);
} }
LoggerUtil.debug("开始生成测试计划队列"); LoggerUtil.info("开始生成测试计划队列");
String reportType = request.getConfig().getReportType(); String reportType = request.getConfig().getReportType();
String poolId = request.getConfig().getResourcePoolId(); String poolId = request.getConfig().getResourcePoolId();
String runMode = StringUtils.equals(request.getTriggerMode(), TriggerMode.MANUAL.name()) ? ApiRunMode.API_PLAN.name() : ApiRunMode.SCHEDULE_API_PLAN.name(); String runMode = StringUtils.equals(request.getTriggerMode(), TriggerMode.MANUAL.name()) ? ApiRunMode.API_PLAN.name() : ApiRunMode.SCHEDULE_API_PLAN.name();
@ -157,6 +156,16 @@ public class ApiCaseExecuteService {
return responseDTOS; return responseDTOS;
} }
public List<TestPlanApiCase> selectByPlanApiCaseIds(List<String> planApiCaseIds) {
if (CollectionUtils.isEmpty(planApiCaseIds)) {
return new ArrayList<>();
}
TestPlanApiCaseExample example = new TestPlanApiCaseExample();
example.createCriteria().andIdIn(planApiCaseIds);
example.setOrderByClause("`order` DESC");
return testPlanApiCaseMapper.selectByExample(example);
}
public Map<String, List<String>> checkEnv(List<ApiTestCaseWithBLOBs> caseList) { public Map<String, List<String>> checkEnv(List<ApiTestCaseWithBLOBs> caseList) {
Map<String, List<String>> projectEnvMap = new HashMap<>(); Map<String, List<String>> projectEnvMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(caseList)) { if (CollectionUtils.isNotEmpty(caseList)) {

View File

@ -8,10 +8,10 @@ import io.metersphere.base.domain.TestPlanApiCase;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ReportTypeConstants; import io.metersphere.commons.constants.ReportTypeConstants;
import io.metersphere.commons.constants.TriggerMode; import io.metersphere.commons.constants.TriggerMode;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.dto.RunModeConfigDTO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@ -27,7 +27,7 @@ public class ApiDefinitionExecResultUtil {
apiResult.setStartTime(System.currentTimeMillis()); apiResult.setStartTime(System.currentTimeMillis());
apiResult.setEndTime(System.currentTimeMillis()); apiResult.setEndTime(System.currentTimeMillis());
apiResult.setTriggerMode(TriggerMode.BATCH.name()); apiResult.setTriggerMode(TriggerMode.BATCH.name());
apiResult.setActuator("LOCAL"); apiResult.setActuator(StorageEnums.LOCAL.name());
if (config != null && GenerateHashTreeUtil.isResourcePool(config.getResourcePoolId()).isPool()) { if (config != null && GenerateHashTreeUtil.isResourcePool(config.getResourcePoolId()).isPool()) {
apiResult.setActuator(config.getResourcePoolId()); apiResult.setActuator(config.getResourcePoolId());
} }
@ -42,31 +42,33 @@ public class ApiDefinitionExecResultUtil {
return apiResult; return apiResult;
} }
public static ApiDefinitionExecResultWithBLOBs addResult(BatchRunDefinitionRequest request, RunModeConfigDTO runModeConfigDTO, TestPlanApiCase key, String status, public static ApiDefinitionExecResultWithBLOBs addResult(
Map<String, ApiTestCase> caseMap, String poolId) { BatchRunDefinitionRequest request,
RunModeConfigDTO runModeConfigDTO,
TestPlanApiCase key,
String status,
ApiTestCase testCase,
String poolId) {
ApiDefinitionExecResultWithBLOBs apiResult = new ApiDefinitionExecResultWithBLOBs(); ApiDefinitionExecResultWithBLOBs apiResult = new ApiDefinitionExecResultWithBLOBs();
apiResult.setId(UUID.randomUUID().toString()); apiResult.setId(UUID.randomUUID().toString());
apiResult.setCreateTime(System.currentTimeMillis()); apiResult.setCreateTime(System.currentTimeMillis());
apiResult.setStartTime(System.currentTimeMillis()); apiResult.setStartTime(System.currentTimeMillis());
apiResult.setEndTime(System.currentTimeMillis()); apiResult.setEndTime(System.currentTimeMillis());
apiResult.setReportType(ReportTypeConstants.API_INDEPENDENT.name()); apiResult.setReportType(ReportTypeConstants.API_INDEPENDENT.name());
ApiTestCase testCase = caseMap.get(key.getApiCaseId());
if (testCase != null) { if (testCase != null) {
apiResult.setName(testCase.getName()); apiResult.setName(testCase.getName());
apiResult.setProjectId(testCase.getProjectId()); apiResult.setProjectId(testCase.getProjectId());
apiResult.setVersionId(testCase.getVersionId()); apiResult.setVersionId(testCase.getVersionId());
} }
apiResult.setTriggerMode(request.getTriggerMode()); apiResult.setTriggerMode(request.getTriggerMode());
apiResult.setActuator("LOCAL"); apiResult.setActuator(StorageEnums.LOCAL.name());
if (StringUtils.isNotEmpty(poolId)) { if (StringUtils.isNotEmpty(poolId)) {
apiResult.setActuator(poolId); apiResult.setActuator(poolId);
} }
if (StringUtils.isEmpty(request.getUserId())) { apiResult.setUserId(request.getUserId());
if (SessionUtils.getUser() != null) { if (StringUtils.isEmpty(apiResult.getUserId())) {
apiResult.setUserId(SessionUtils.getUser().getId()); apiResult.setUserId(SessionUtils.getUserId());
}
} else {
apiResult.setUserId(request.getUserId());
} }
apiResult.setResourceId(key.getId()); apiResult.setResourceId(key.getId());
@ -92,7 +94,7 @@ public class ApiDefinitionExecResultUtil {
apiResult.setStartTime(System.currentTimeMillis()); apiResult.setStartTime(System.currentTimeMillis());
apiResult.setEndTime(System.currentTimeMillis()); apiResult.setEndTime(System.currentTimeMillis());
apiResult.setTriggerMode(TriggerMode.BATCH.name()); apiResult.setTriggerMode(TriggerMode.BATCH.name());
apiResult.setActuator("LOCAL"); apiResult.setActuator(StorageEnums.LOCAL.name());
apiResult.setUserId(userId); apiResult.setUserId(userId);
apiResult.setResourceId(resourceId); apiResult.setResourceId(resourceId);
apiResult.setStartTime(System.currentTimeMillis()); apiResult.setStartTime(System.currentTimeMillis());

View File

@ -26,6 +26,7 @@ import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.*; import io.metersphere.base.mapper.ext.*;
import io.metersphere.commons.constants.*; import io.metersphere.commons.constants.*;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.commons.enums.*; import io.metersphere.commons.enums.*;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
@ -997,7 +998,7 @@ public class ApiDefinitionService {
apiReportEnvConfig.setResourcePoolName(resourcePool.getName()); apiReportEnvConfig.setResourcePoolName(resourcePool.getName());
} }
} else { } else {
apiReportEnvConfig.setResourcePoolName("LOCAL"); apiReportEnvConfig.setResourcePoolName(StorageEnums.LOCAL.name());
} }
return apiReportEnvConfig; return apiReportEnvConfig;
} }

View File

@ -14,6 +14,7 @@ import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.NodeDTO; import io.metersphere.dto.NodeDTO;
@ -203,7 +204,7 @@ public class ExtApiTaskService extends TaskService {
if (StringUtils.isEmpty(reportId)) { if (StringUtils.isEmpty(reportId)) {
return; return;
} }
if (StringUtils.isNotEmpty(actuator) && !StringUtils.equals(actuator, "LOCAL")) { if (StringUtils.isNotEmpty(actuator) && !StringUtils.equals(actuator, StorageEnums.LOCAL.name())) {
if (poolMap.containsKey(actuator)) { if (poolMap.containsKey(actuator)) {
poolMap.get(actuator).add(reportId); poolMap.get(actuator).add(reportId);
} else { } else {

View File

@ -790,7 +790,7 @@ public class ApiScenarioReportService {
if (initModel.getConfig() != null && StringUtils.isNotBlank(initModel.getConfig().getResourcePoolId())) { if (initModel.getConfig() != null && StringUtils.isNotBlank(initModel.getConfig().getResourcePoolId())) {
report.setActuator(initModel.getConfig().getResourcePoolId()); report.setActuator(initModel.getConfig().getResourcePoolId());
} else { } else {
report.setActuator("LOCAL"); report.setActuator(StorageEnums.LOCAL.name());
} }
report.setTriggerMode(initModel.getTriggerMode()); report.setTriggerMode(initModel.getTriggerMode());
report.setReportVersion(2); report.setReportVersion(2);

View File

@ -10,6 +10,7 @@ import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.ReportTypeConstants; import io.metersphere.commons.constants.ReportTypeConstants;
import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.RequestResult; import io.metersphere.dto.RequestResult;
@ -714,7 +715,7 @@ public class ApiScenarioReportStructureService {
dto.setPoolName(resourcePool.getName()); dto.setPoolName(resourcePool.getName());
} }
} else { } else {
dto.setPoolName("LOCAL"); dto.setPoolName(StorageEnums.LOCAL.name());
} }
if (runModeConfigDTO != null && StringUtils.isNotBlank(runModeConfigDTO.getMode())) { if (runModeConfigDTO != null && StringUtils.isNotBlank(runModeConfigDTO.getMode())) {
dto.setMode(runModeConfigDTO.getMode()); dto.setMode(runModeConfigDTO.getMode());

View File

@ -19,6 +19,7 @@ import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.ReportTypeConstants; import io.metersphere.commons.constants.ReportTypeConstants;
import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
@ -111,7 +112,7 @@ public class ApiScenarioRerunService {
config.setMode(RunModeConstants.PARALLEL.toString()); config.setMode(RunModeConstants.PARALLEL.toString());
} }
config.setReportType(RunModeConstants.SET_REPORT.toString()); config.setReportType(RunModeConstants.SET_REPORT.toString());
if (!StringUtils.equalsAnyIgnoreCase(reportResults.get(0).getActuator(), "LOCAL")) { if (!StringUtils.equalsAnyIgnoreCase(reportResults.get(0).getActuator(), StorageEnums.LOCAL.name())) {
config.setResourcePoolId(reportResults.get(0).getActuator()); config.setResourcePoolId(reportResults.get(0).getActuator());
} }
request.setConfig(config); request.setConfig(config);
@ -165,7 +166,7 @@ public class ApiScenarioRerunService {
config.setMode(RunModeConstants.PARALLEL.toString()); config.setMode(RunModeConstants.PARALLEL.toString());
} }
config.setReportName(reportDTO.getName()); config.setReportName(reportDTO.getName());
if (!StringUtils.equalsAnyIgnoreCase(reportDTO.getActuator(), "LOCAL")) { if (!StringUtils.equalsAnyIgnoreCase(reportDTO.getActuator(), StorageEnums.LOCAL.name())) {
config.setResourcePoolId(reportDTO.getActuator()); config.setResourcePoolId(reportDTO.getActuator());
} }
config.setReportType(RunModeConstants.SET_REPORT.toString()); config.setReportType(RunModeConstants.SET_REPORT.toString());
@ -241,7 +242,7 @@ public class ApiScenarioRerunService {
continue; continue;
} }
runModeConfig.setReportType(report.getReportType()); runModeConfig.setReportType(report.getReportType());
if (!StringUtils.equalsAnyIgnoreCase(report.getActuator(), "LOCAL")) { if (!StringUtils.equalsAnyIgnoreCase(report.getActuator(), StorageEnums.LOCAL.name())) {
runModeConfig.setResourcePoolId(report.getActuator()); runModeConfig.setResourcePoolId(report.getActuator());
} }
executeQueue.put(testPlanApiCase.getId(), report); executeQueue.put(testPlanApiCase.getId(), report);
@ -293,7 +294,7 @@ public class ApiScenarioRerunService {
// 执行配置 // 执行配置
RunModeConfigDTO config = new RunModeConfigDTO(); RunModeConfigDTO config = new RunModeConfigDTO();
config.setMode(RunModeConstants.PARALLEL.toString()); config.setMode(RunModeConstants.PARALLEL.toString());
if (!StringUtils.equalsAnyIgnoreCase(reports.get(0).getActuator(), "LOCAL")) { if (!StringUtils.equalsAnyIgnoreCase(reports.get(0).getActuator(), StorageEnums.LOCAL.name())) {
config.setResourcePoolId(reports.get(0).getActuator()); config.setResourcePoolId(reports.get(0).getActuator());
} }

View File

@ -10,7 +10,7 @@
<font-awesome-icon class="icon global focusing" :icon="['fas', 'tasks']"/> <font-awesome-icon class="icon global focusing" :icon="['fas', 'tasks']"/>
</el-badge> </el-badge>
</div> </div>
<font-awesome-icon @click="showTaskCenter" class="icon global focusing" :icon="['fas', 'tasks']" v-else/> <font-awesome-icon @click="open('API')" class="icon global focusing" :icon="['fas', 'tasks']" v-else/>
</el-tooltip> </el-tooltip>
</div> </div>
<el-drawer <el-drawer
@ -300,6 +300,7 @@ export default {
this.activeName = activeName; this.activeName = activeName;
} }
this.init(true); this.init(true);
this.taskVisible = true;
setTimeout(this.showTaskCenter, 2000); setTimeout(this.showTaskCenter, 2000);
}, },
getPercentage(status) { getPercentage(status) {