From 9a27ac79d527afbcd7d8b8e5520907be42239529 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 23 Dec 2022 14:52:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E7=94=A8=E4=BE=8B=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=EF=BC=8C=E6=8F=90=E9=AB=98=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/exec/api/ApiCaseExecuteService.java | 159 +++++++----------- .../api/parse/api/MsDefinitionParser.java | 13 +- .../parse/scenario/ApiScenarioImportUtil.java | 12 +- .../commons/constants/PropertyConstant.java | 5 + .../service/ApiTestDelService.java | 63 ------- .../definition/ApiTestCaseService.java | 2 +- .../service/ext/ExtApiScheduleService.java | 116 +------------ .../ext/ExtProjectApplicationService.java | 8 - .../service/scenario/ApiScenarioService.java | 2 +- 9 files changed, 81 insertions(+), 299 deletions(-) delete mode 100644 api-test/backend/src/main/java/io/metersphere/service/ApiTestDelService.java diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java index 900dbd0dd6..b2242c5879 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java @@ -65,6 +65,8 @@ public class ApiCaseExecuteService { private TestPlanApiCaseMapper testPlanApiCaseMapper; @Resource private JMeterService jMeterService; + @Resource + private BaseEnvironmentService baseEnvironmentService; /** * 测试计划case执行 @@ -140,15 +142,12 @@ public class ApiCaseExecuteService { // 开始选择执行模式 if (deQueue != null && deQueue.getDetail() != null) { - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - Thread.currentThread().setName("PLAN-CASE:" + request.getPlanReportId()); - if (request.getConfig() != null && request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { - apiCaseSerialService.serial(deQueue); - } else { - apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), deQueue, runMode); - } + Thread thread = new Thread(() -> { + Thread.currentThread().setName("PLAN-CASE:" + request.getPlanReportId()); + if (request.getConfig() != null && request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { + apiCaseSerialService.serial(deQueue); + } else { + apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), deQueue, runMode); } }); thread.start(); @@ -166,100 +165,64 @@ public class ApiCaseExecuteService { return testPlanApiCaseMapper.selectByExample(example); } - public Map> checkEnv(List caseList) { + public Map> getRequestEnv(List caseList) { Map> projectEnvMap = new HashMap<>(); - if (CollectionUtils.isNotEmpty(caseList)) { - StringBuilder builderHttp = new StringBuilder(); - StringBuilder builderTcp = new StringBuilder(); - for (int i = caseList.size() - 1; i >= 0; i--) { - ApiTestCaseWithBLOBs apiCase = caseList.get(i); - JSONObject apiCaseNew = new JSONObject(apiCase.getRequest()); - if (apiCaseNew.has(PropertyConstant.TYPE) && ElementConstants.HTTP_SAMPLER.equals(apiCaseNew.optString(PropertyConstant.TYPE))) { - if (!apiCaseNew.has("useEnvironment") || StringUtils.isEmpty(apiCaseNew.optString("useEnvironment"))) { - builderHttp.append(apiCase.getName()).append("; "); - } else { - //记录运行环境ID - String envId = apiCaseNew.optString("useEnvironment"); - if (projectEnvMap.containsKey(apiCase.getProjectId())) { - if (!projectEnvMap.get(apiCase.getProjectId()).contains(envId)) { - projectEnvMap.get(apiCase.getProjectId()).add(envId); - } - } else { - projectEnvMap.put(apiCase.getProjectId(), new ArrayList<>() {{ - this.add(envId); - }}); - } - } - } - if (apiCaseNew.has(PropertyConstant.TYPE) && ElementConstants.JDBC_SAMPLER.equals(apiCaseNew.optString(PropertyConstant.TYPE))) { - DatabaseConfig dataSource = null; - if (apiCaseNew.has("useEnvironment") && apiCaseNew.has("dataSourceId")) { - String environmentId = apiCaseNew.optString("useEnvironment"); - String dataSourceId = apiCaseNew.optString("dataSourceId"); - BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class); - ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(environmentId); - EnvironmentConfig envConfig = null; - if (environment != null && environment.getConfig() != null) { - envConfig = JSON.parseObject(environment.getConfig(), EnvironmentConfig.class); - if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { - for (DatabaseConfig item : envConfig.getDatabaseConfigs()) { - if (item.getId().equals(dataSourceId)) { - dataSource = item; - //记录运行环境ID - if (projectEnvMap.containsKey(apiCase.getProjectId())) { - if (!projectEnvMap.get(apiCase.getProjectId()).contains(environmentId)) { - projectEnvMap.get(apiCase.getProjectId()).add(environmentId); - } - } else { - projectEnvMap.put(apiCase.getProjectId(), new ArrayList<>() {{ - this.add(environmentId); - }}); - } - } - } - } - } - } - if (dataSource == null) { - builderTcp.append(apiCase.getName()).append("; "); - } + if (CollectionUtils.isEmpty(caseList)) { + return projectEnvMap; + } + StringBuilder message = new StringBuilder(); + for (ApiTestCaseWithBLOBs apiCase : caseList) { + JSONObject requestObj = new JSONObject(apiCase.getRequest()); + if (StringUtils.equalsAny(requestObj.optString(PropertyConstant.TYPE), ElementConstants.HTTP_SAMPLER, ElementConstants.JDBC_SAMPLER)) { + Map envMap = this.getEnvMap(requestObj, apiCase.getProjectId()); + if (MapUtils.isEmpty(envMap)) { + message.append(apiCase.getName()).append(";"); + } else { + //记录运行环境ID + this.setEnvId(projectEnvMap, apiCase.getProjectId(), envMap.get(apiCase.getProjectId())); } } - if (StringUtils.isNotEmpty(builderHttp)) { - MSException.throwException("用例:" + builderHttp + "运行环境为空!请检查"); - } - if (StringUtils.isNotEmpty(builderTcp)) { - MSException.throwException("用例:" + builderTcp + "数据源为空!请检查"); - } + } + if (StringUtils.isNotEmpty(message)) { + MSException.throwException("用例:" + message + "运行环境为空!"); } return projectEnvMap; } - public Map getEnvMap(ApiTestCaseWithBLOBs apiCase) { - Map projectEnvMap = new HashMap<>(); + private void setEnvId(Map> projectEnvMap, String projectId, String envId) { + //记录运行环境ID + if (projectEnvMap.containsKey(projectId)) { + if (!projectEnvMap.get(projectId).contains(envId)) { + projectEnvMap.get(projectId).add(envId); + } + } else { + projectEnvMap.put(projectId, new ArrayList<>() {{ + this.add(envId); + }}); + } + } - JSONObject apiCaseNew = new JSONObject(apiCase.getRequest()); - if (apiCaseNew.has(PropertyConstant.TYPE) && ElementConstants.HTTP_SAMPLER.equals(apiCaseNew.optString(PropertyConstant.TYPE))) { - if (apiCaseNew.has("useEnvironment") && StringUtils.isNotEmpty(apiCaseNew.optString("useEnvironment"))) { + public Map getEnvMap(JSONObject request, String projectId) { + Map 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 - String envId = apiCaseNew.optString("useEnvironment"); - projectEnvMap.put(apiCase.getProjectId(), envId); + projectEnvMap.put(projectId, request.optString(PropertyConstant.ENVIRONMENT)); } } - if (apiCaseNew.has(PropertyConstant.TYPE) && ElementConstants.JDBC_SAMPLER.equals(apiCaseNew.optString(PropertyConstant.TYPE))) { - if (apiCaseNew.has("useEnvironment") && apiCaseNew.has("dataSourceId")) { - String environmentId = apiCaseNew.optString("useEnvironment"); - String dataSourceId = apiCaseNew.optString("dataSourceId"); - BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class); - ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(environmentId); - EnvironmentConfig envConfig = null; + 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) { - envConfig = JSON.parseObject(environment.getConfig(), EnvironmentConfig.class); + EnvironmentConfig envConfig = JSON.parseObject(environment.getConfig(), EnvironmentConfig.class); if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { for (DatabaseConfig item : envConfig.getDatabaseConfigs()) { - if (item.getId().equals(dataSourceId)) { + if (StringUtils.equals(item.getId(), request.optString(PropertyConstant.DATASOURCE_ID))) { //记录运行环境ID - projectEnvMap.put(apiCase.getProjectId(), environmentId); + projectEnvMap.put(projectId, request.optString(PropertyConstant.ENVIRONMENT)); } } } @@ -300,7 +263,7 @@ public class ApiCaseExecuteService { Map> testCaseEnvMap = new HashMap<>(); // 环境检查 if (MapUtils.isEmpty(request.getConfig().getEnvMap())) { - testCaseEnvMap = this.checkEnv(caseList); + testCaseEnvMap = this.getRequestEnv(caseList); } // 集合报告设置 String serialReportId = request.isRerun() ? request.getReportId() : null; @@ -363,7 +326,8 @@ public class ApiCaseExecuteService { BeanUtils.copyBean(config, request.getConfig()); if (StringUtils.equals(config.getEnvironmentType(), EnvironmentType.JSON.name()) && MapUtils.isEmpty(config.getEnvMap())) { - config.setEnvMap(this.getEnvMap(caseWithBLOBs)); + JSONObject jsonObject = new JSONObject(caseWithBLOBs.getRequest()); + config.setEnvMap(this.getEnvMap(jsonObject, caseWithBLOBs.getProjectId())); } ApiDefinitionExecResultWithBLOBs report = ApiDefinitionExecResultUtil.initBase(caseWithBLOBs.getId(), ApiReportStatus.RUNNING.name(), null, config); report.setStatus(status); @@ -387,15 +351,12 @@ public class ApiCaseExecuteService { DBTestQueue queue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.DEFINITION.name(), serialReportId, reportType, ApiRunMode.DEFINITION.name(), request.getConfig()); // 开始选择执行模式 if (queue != null && queue.getDetail() != null) { - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - Thread.currentThread().setName("API-CASE-RUN"); - if (request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { - apiCaseSerialService.serial(queue); - } else { - apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), queue, ApiRunMode.DEFINITION.name()); - } + Thread thread = new Thread(() -> { + Thread.currentThread().setName("API-CASE-RUN"); + if (request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { + apiCaseSerialService.serial(queue); + } else { + apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), queue, ApiRunMode.DEFINITION.name()); } }); thread.start(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java index 94917a0d2f..17d92cf01a 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java @@ -3,18 +3,15 @@ package io.metersphere.api.parse.api; import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; -import io.metersphere.commons.constants.RequestTypeConstants; import io.metersphere.api.parse.ApiImportAbstractParser; import io.metersphere.api.parse.MsAbstractParser; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.commons.constants.ApiImportPlatform; +import io.metersphere.commons.constants.PropertyConstant; +import io.metersphere.commons.constants.RequestTypeConstants; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.CommonBeanFactory; -import io.metersphere.commons.utils.JSON; -import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.SessionUtils; -import io.metersphere.commons.utils.JSONUtil; +import io.metersphere.commons.utils.*; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -152,8 +149,8 @@ public class MsDefinitionParser extends MsAbstractParser { return null; } JSONObject requestObj = JSONUtil.parseObject(request); - requestObj.put("useEnvironment", StringUtils.EMPTY); - requestObj.put("environmentId", StringUtils.EMPTY); + requestObj.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY); + requestObj.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY); requestObj.put("projectId", projectId); return requestObj; } catch (Exception e) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java index bb54920dad..cc0e8a3f16 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java @@ -185,8 +185,8 @@ public class ApiScenarioImportUtil { object.put("id", sameCase.getId()); object.put("resourceId", sameCase.getId()); object.put("projectId", projectId); - object.put("useEnvironment", StringUtils.EMPTY); - object.put("environmentId", StringUtils.EMPTY); + object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY); + object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY); } } } else { @@ -244,8 +244,8 @@ public class ApiScenarioImportUtil { object.put("id", test.getId()); object.put("resourceId", test.getId()); object.put("projectId", projectId); - object.put("useEnvironment", StringUtils.EMPTY); - object.put("environmentId", StringUtils.EMPTY); + object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY); + object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY); object.put("url", StringUtils.EMPTY); JSONObject objectNew = JSONUtil.parseObject(object.toString()); objectNew.remove("refType"); @@ -300,8 +300,8 @@ public class ApiScenarioImportUtil { object.put("id", apiTestCase.getId()); object.put("resourceId", apiTestCase.getId()); object.put("projectId", projectId); - object.put("useEnvironment", StringUtils.EMPTY); - object.put("environmentId", StringUtils.EMPTY); + object.put(PropertyConstant.ENVIRONMENT, StringUtils.EMPTY); + object.put(PropertyConstant.ENVIRONMENT_ID, StringUtils.EMPTY); JSONObject objectNew = JSONUtil.parseObject(object.toString()); objectNew.remove("refType"); objectNew.remove("referenced"); diff --git a/api-test/backend/src/main/java/io/metersphere/commons/constants/PropertyConstant.java b/api-test/backend/src/main/java/io/metersphere/commons/constants/PropertyConstant.java index 5931a9c886..443fc9d94a 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/constants/PropertyConstant.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/constants/PropertyConstant.java @@ -24,4 +24,9 @@ public class PropertyConstant { public final static String ASS_OPTION = "ASS_OPTION"; public final static String EXPECTED_VALUE = "EXPECTED_VALUE"; public final static String ELEMENT_CONDITION = "ElementCondition"; + public final static String ENVIRONMENT = "useEnvironment"; + public final static String ENVIRONMENT_ID = "environmentId"; + public final static String DATASOURCE_ID = "dataSourceId"; + + } diff --git a/api-test/backend/src/main/java/io/metersphere/service/ApiTestDelService.java b/api-test/backend/src/main/java/io/metersphere/service/ApiTestDelService.java deleted file mode 100644 index aa5a9ed4b2..0000000000 --- a/api-test/backend/src/main/java/io/metersphere/service/ApiTestDelService.java +++ /dev/null @@ -1,63 +0,0 @@ -package io.metersphere.service; - -import io.metersphere.service.definition.ApiDefinitionService; -import io.metersphere.service.scenario.ApiScenarioService; -import io.metersphere.base.domain.*; -import io.metersphere.base.mapper.ApiDefinitionMapper; -import io.metersphere.base.mapper.ApiModuleMapper; -import io.metersphere.base.mapper.ApiScenarioMapper; -import io.metersphere.base.mapper.ApiScenarioModuleMapper; -import org.apache.commons.collections4.CollectionUtils; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import javax.annotation.Resource; -import java.util.List; -import java.util.stream.Collectors; - -@Service -@Transactional -/** - * 接口测试所有级联删除资源 - */ -public class ApiTestDelService { - @Resource - private ApiDefinitionService apiDefinitionService; - @Resource - private ApiScenarioService apiAutomationService; - @Resource - private ApiScenarioMapper apiScenarioMapper; - @Resource - private ApiDefinitionMapper apiDefinitionMapper; - @Resource - private ApiModuleMapper apiModuleMapper; - @Resource - private ApiScenarioModuleMapper apiScenarioModuleMapper; - - public void delete(String projectId) { - ApiDefinitionExample example = new ApiDefinitionExample(); - example.createCriteria().andProjectIdEqualTo(projectId); - // api and case - List apiIds = apiDefinitionMapper.selectByExample(example).stream().map(ApiDefinition::getId).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(apiIds)) { - apiDefinitionService.deleteBatch(apiIds); - } - // scenario - ApiScenarioExample scenarioExample = new ApiScenarioExample(); - scenarioExample.createCriteria().andProjectIdEqualTo(projectId); - List scenarios = apiScenarioMapper.selectByExample(scenarioExample).stream().map(ApiScenario::getId).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(scenarios)) { - apiAutomationService.deleteBatch(scenarios); - } - // api module - ApiModuleExample moduleExample = new ApiModuleExample(); - moduleExample.createCriteria().andProjectIdEqualTo(projectId); - apiModuleMapper.deleteByExample(moduleExample); - - // scenario module - ApiScenarioModuleExample scenarioModuleExample = new ApiScenarioModuleExample(); - scenarioModuleExample.createCriteria().andProjectIdEqualTo(projectId); - apiScenarioModuleMapper.deleteByExample(scenarioModuleExample); - - } -} diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java index 9057e887e3..57d6476763 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java @@ -678,7 +678,7 @@ public class ApiTestCaseService { bloBs.forEach(apiTestCase -> { JSONObject req = JSONUtil.parseObject(apiTestCase.getRequest()); - req.put("useEnvironment", request.getEnvId()); + req.put(PropertyConstant.ENVIRONMENT, request.getEnvId()); String requestStr = JSON.toJSONString(req); apiTestCase.setRequest(requestStr); batchMapper.updateByPrimaryKeySelective(apiTestCase); diff --git a/api-test/backend/src/main/java/io/metersphere/service/ext/ExtApiScheduleService.java b/api-test/backend/src/main/java/io/metersphere/service/ext/ExtApiScheduleService.java index e95d73cbd8..367c730db1 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ext/ExtApiScheduleService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ext/ExtApiScheduleService.java @@ -2,32 +2,26 @@ package io.metersphere.service.ext; import io.metersphere.api.dto.ScheduleRequest; import io.metersphere.base.domain.*; -import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ScheduleMapper; import io.metersphere.base.mapper.SwaggerUrlProjectMapper; import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.ext.ExtScheduleMapper; import io.metersphere.commons.constants.ScheduleGroup; -import io.metersphere.commons.constants.ScheduleType; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.DateUtils; import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.dto.ScheduleDao; -import io.metersphere.dto.TaskInfoResult; import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.schedule.ScheduleReference; -import io.metersphere.request.BaseQueryRequest; import io.metersphere.request.OrderRequest; import io.metersphere.request.QueryScheduleRequest; import io.metersphere.sechedule.ApiScenarioTestJob; import io.metersphere.sechedule.ScheduleManager; import io.metersphere.sechedule.SwaggerUrlImportJob; import io.metersphere.service.ServiceUtils; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.quartz.JobKey; import org.quartz.SchedulerException; @@ -36,14 +30,14 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.*; +import java.util.List; +import java.util.Map; +import java.util.UUID; import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) public class ExtApiScheduleService { - @Resource - private ApiScenarioMapper apiScenarioMapper; @Resource private ScheduleMapper scheduleMapper; @Resource @@ -70,10 +64,6 @@ public class ExtApiScheduleService { swaggerUrlProjectMapper.updateByPrimaryKeyWithBLOBs(swaggerUrlProject); } - public Schedule getSchedule(String ScheduleId) { - return scheduleMapper.selectByPrimaryKey(ScheduleId); - } - public int editSchedule(Schedule schedule) { schedule.setUpdateTime(System.currentTimeMillis()); return scheduleMapper.updateByPrimaryKeySelective(schedule); @@ -89,19 +79,6 @@ public class ExtApiScheduleService { return null; } - public List getScheduleByResourceIds(List resourceIds, String group) { - if (CollectionUtils.isEmpty(resourceIds)) { - return new ArrayList<>(); - } - ScheduleExample example = new ScheduleExample(); - example.createCriteria().andResourceIdIn(resourceIds).andGroupEqualTo(group); - List schedules = scheduleMapper.selectByExample(example); - if (schedules.size() > 0) { - return schedules; - } - return new ArrayList<>(); - } - public int deleteByResourceId(String resourceId, String group) { ScheduleExample scheduleExample = new ScheduleExample(); scheduleExample.createCriteria().andResourceIdEqualTo(resourceId); @@ -120,16 +97,6 @@ public class ExtApiScheduleService { return scheduleMapper.deleteByExample(scheduleExample); } - public int deleteByWorkspaceId(String workspaceId) { - ScheduleExample scheduleExample = new ScheduleExample(); - scheduleExample.createCriteria().andWorkspaceIdEqualTo(workspaceId); - List schedules = scheduleMapper.selectByExample(scheduleExample); - schedules.forEach(item -> { - removeJob(item.getResourceId(), item.getGroup()); - }); - return scheduleMapper.deleteByExample(scheduleExample); - } - private void removeJob(String resourceId, String group) { if (StringUtils.equals(ScheduleGroup.API_SCENARIO_TEST.name(), group)) { scheduleManager.removeJob(ApiScenarioTestJob.getJobKey(resourceId), ApiScenarioTestJob.getTriggerKey(resourceId)); @@ -138,32 +105,6 @@ public class ExtApiScheduleService { } } - public List listSchedule() { - ScheduleExample example = new ScheduleExample(); - return scheduleMapper.selectByExample(example); - } - - public List getEnableSchedule() { - ScheduleExample example = new ScheduleExample(); - example.createCriteria().andEnableEqualTo(true); - return scheduleMapper.selectByExample(example); - } - - public void startEnableSchedules() { - List Schedules = getEnableSchedule(); - - Schedules.forEach(schedule -> { - try { - if (schedule.getEnable()) { - LogUtil.info("初始化任务:" + JSON.toJSONString(schedule)); - scheduleManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getGroup()), new TriggerKey(schedule.getKey(), schedule.getGroup()), Class.forName(schedule.getJob()), schedule.getValue(), scheduleManager.getDefaultJobDataMap(schedule, schedule.getValue(), schedule.getUserId())); - } - } catch (Exception e) { - LogUtil.error("初始化任务失败", e); - } - }); - } - public Schedule buildApiTestSchedule(ScheduleRequest request) { Schedule schedule = new Schedule(); schedule.setResourceId(request.getResourceId()); @@ -231,52 +172,6 @@ public class ExtApiScheduleService { }); } - public long countTaskByProjectId(String projectId) { - return extScheduleMapper.countTaskByProjectId(projectId); - } - - public long countTaskByProjectIdInThisWeek(String projectId) { - Map startAndEndDateInWeek = DateUtils.getWeedFirstTimeAndLastTime(new Date()); - - Date firstTime = startAndEndDateInWeek.get("firstTime"); - Date lastTime = startAndEndDateInWeek.get("lastTime"); - - if (firstTime == null || lastTime == null) { - return 0; - } else { - return extScheduleMapper.countTaskByProjectIdAndCreateTimeRange(projectId, firstTime.getTime(), lastTime.getTime()); - } - } - - public List findRunningTaskInfoByProjectID(String projectID, BaseQueryRequest request) { - List runningTaskInfoList = extScheduleMapper.findRunningTaskInfoByProjectID(projectID, request); - return runningTaskInfoList; - } - - public void createSchedule(ScheduleRequest request) { - Schedule schedule = this.buildApiTestSchedule(request); - JobKey jobKey = null; - TriggerKey triggerKey = null; - Class clazz = null; - if ("testPlan".equals(request.getScheduleFrom())) { - LogUtil.info("testPlan"); - } else { - //默认为情景 - ApiScenarioWithBLOBs apiScene = apiScenarioMapper.selectByPrimaryKey(request.getResourceId()); - schedule.setName(apiScene.getName()); - schedule.setProjectId(apiScene.getProjectId()); - schedule.setGroup(ScheduleGroup.API_SCENARIO_TEST.name()); - schedule.setType(ScheduleType.CRON.name()); - jobKey = ApiScenarioTestJob.getJobKey(request.getResourceId()); - triggerKey = ApiScenarioTestJob.getTriggerKey(request.getResourceId()); - clazz = ApiScenarioTestJob.class; - schedule.setJob(ApiScenarioTestJob.class.getName()); - } - this.addSchedule(schedule); - - this.addOrUpdateCronJob(request, jobKey, triggerKey, clazz); - } - public void updateSchedule(Schedule request) { JobKey jobKey = null; TriggerKey triggerKey = null; @@ -307,11 +202,6 @@ public class ExtApiScheduleService { } } - - public Object getCurrentlyExecutingJobs() { - return scheduleManager.getCurrentlyExecutingJobs(); - } - public String getLogDetails(String id) { Schedule bloB = scheduleMapper.selectByPrimaryKey(id); if (bloB != null) { diff --git a/api-test/backend/src/main/java/io/metersphere/service/ext/ExtProjectApplicationService.java b/api-test/backend/src/main/java/io/metersphere/service/ext/ExtProjectApplicationService.java index 7aa408411a..694fb09889 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ext/ExtProjectApplicationService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ext/ExtProjectApplicationService.java @@ -6,7 +6,6 @@ import io.metersphere.base.domain.Project; import io.metersphere.base.domain.ProjectApplication; import io.metersphere.base.domain.ProjectApplicationExample; import io.metersphere.base.mapper.ProjectApplicationMapper; -import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.ext.BaseProjectMapper; import io.metersphere.base.mapper.ext.ExtApiProjectMapper; @@ -41,8 +40,6 @@ public class ExtProjectApplicationService { @Resource private BaseProjectService baseProjectService; @Resource - private ProjectMapper projectMapper; - @Resource private BaseProjectMapper baseProjectMapper; @Resource private UserMapper userMapper; @@ -144,11 +141,6 @@ public class ExtProjectApplicationService { } } - public void closeMockTcp(String projectId) { - Project project = projectMapper.selectByPrimaryKey(projectId); - this.closeMockTcp(project); - } - public void closeMockTcp(Project project) { if (project == null) { MSException.throwException("Project not found!"); diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java index b9f92c34d1..959d8e91c4 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java @@ -1188,7 +1188,7 @@ public class ApiScenarioService { List apiScenarios = selectByIdsWithBLOBs(request.getIds()); apiScenarios.forEach(item -> { JSONObject object = JSONUtil.parseObject(item.getScenarioDefinition()); - object.put("environmentId", request.getEnvironmentId()); + object.put(PropertyConstant.ENVIRONMENT_ID, request.getEnvironmentId()); if (object != null) { item.setScenarioDefinition(object.toString()); }