From 260cefe5f9a6353f198f58f7bc924667e3907903 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 7 Mar 2022 18:24:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=B9=E9=87=8F=E6=89=A7=E8=A1=8Ccase?= =?UTF-8?q?=E9=83=A8=E5=88=86case=E6=9C=AA=E9=80=89=E6=8B=A9=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E7=8E=AF=E5=A2=83=E6=89=B9=E9=87=8F=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=9C=AA=E6=8F=90=E9=86=92=E9=80=89=E6=8B=A9=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1010862 --user=王孝刚 【接口测试】-批量执行case-部分case未选择运行环境-批量执行也未统一选择运行环境-可批量执行 https://www.tapd.cn/55049933/s/1114734 --- .../api/exec/api/ApiCaseExecuteService.java | 57 +++++++++++++++++-- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java index 03b1da36d3..413cee6f5d 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java @@ -6,13 +6,12 @@ import io.metersphere.api.dto.automation.APIScenarioReportResult; import io.metersphere.api.dto.automation.ExecuteType; import io.metersphere.api.dto.definition.ApiTestCaseRequest; import io.metersphere.api.dto.definition.BatchRunDefinitionRequest; +import io.metersphere.api.dto.scenario.DatabaseConfig; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.exec.queue.DBTestQueue; import io.metersphere.api.exec.scenario.ApiScenarioSerialService; import io.metersphere.api.exec.utils.ApiDefinitionExecResultUtil; -import io.metersphere.api.service.ApiCaseResultService; -import io.metersphere.api.service.ApiExecutionQueueService; -import io.metersphere.api.service.ApiScenarioReportService; -import io.metersphere.api.service.ApiScenarioReportStructureService; +import io.metersphere.api.service.*; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiScenarioReportMapper; import io.metersphere.base.mapper.ApiTestCaseMapper; @@ -20,6 +19,7 @@ import io.metersphere.base.mapper.TestPlanApiCaseMapper; import io.metersphere.base.mapper.TestPlanMapper; import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; import io.metersphere.commons.constants.*; +import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.constants.RunModeConstants; @@ -31,6 +31,7 @@ import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections4.comparators.FixedOrderComparator; import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -133,6 +134,51 @@ public class ApiCaseExecuteService { return responseDTOS; } + public void checkEnv(List caseList) { + if (CollectionUtils.isNotEmpty(caseList)) { + StringBuilder builderHttp = new StringBuilder(); + StringBuilder builderTcp = new StringBuilder(); + for (ApiTestCaseWithBLOBs apiCase : caseList) { + JSONObject apiCaseNew = new JSONObject(apiCase.getRequest()); + if ("HTTPSamplerProxy".equals(apiCaseNew.getString("type"))) { + try { + String environmentId = apiCaseNew.getString("useEnvironment"); + if (!StringUtils.isNotEmpty(environmentId)) { + builderHttp.append(apiCase.getName()).append("; "); + } + } catch (Exception e) { + MSException.throwException("用例:" + builderHttp.append(apiCase.getName()).append("; ") + "运行环境为空!请检查"); + } + } + if ("JDBCSampler".equals(apiCaseNew.getString("type"))) { + try { + String environmentId = apiCaseNew.getString("useEnvironment"); + String dataSourceId = apiCaseNew.getString("dataSourceId"); + DatabaseConfig dataSource = null; + ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); + ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentId); + EnvironmentConfig envConfig = null; + if (environment != null && environment.getConfig() != null) { + envConfig = com.alibaba.fastjson.JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); + if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { + for (DatabaseConfig item : envConfig.getDatabaseConfigs()) { + if (item.getId().equals(dataSourceId)) { + dataSource = item; + } + } + } + } + if (dataSource == null) { + MSException.throwException("用例:" + builderTcp.append(apiCase.getName()).append("; ")); + } + } catch (Exception e) { + MSException.throwException("用例数据源为空,请检查!"); + } + } + } + } + } + /** * 接口定义case执行 * @@ -158,7 +204,8 @@ public class ApiCaseExecuteService { example.createCriteria().andIdIn(request.getIds()); List caseList = apiTestCaseMapper.selectByExampleWithBLOBs(example); LoggerUtil.debug("查询到执行数据:" + caseList.size()); - + // 环境检查 + this.checkEnv(caseList); // 集合报告设置 String serialReportId = null; if (StringUtils.equals(request.getConfig().getReportType(), RunModeConstants.SET_REPORT.toString())