From 47a70f2eebb7c74dba4689277b298d21adcea1fd Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Mon, 28 Aug 2023 16:18:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E8=AE=BE=E7=BD=AE-=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=AE=A1=E5=88=92=E9=85=8D=E7=BD=AE-=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/config/ScheduleConfig.java | 23 ++++ .../sdk/constants/PermissionConstants.java | 3 + .../sdk/constants/ProjectApplicationType.java | 48 ++++++++ .../sdk/constants/ScheduleType.java | 5 + .../sdk/dto/BaseSystemConfigDTO.java | 1 - .../sdk/invoker/ProjectServiceInvoker.java | 6 + .../sdk/sechedule/BaseScheduleService.java | 24 ++++ .../CleanupProjectResourceService.java | 7 ++ .../resources/i18n/project_en_US.properties | 3 + .../resources/i18n/project_zh_CN.properties | 3 + .../resources/i18n/project_zh_TW.properties | 5 +- .../service/CleanupAPIResourceService.java | 5 + .../api/controller/CleanupResourceTests.java | 5 + .../service/CleanupBugResourceService.java | 5 + .../bug/controller/CleanupResourceTests.java | 5 + .../service/CleanupCaseResourceService.java | 5 + .../controller/CleanupResourceTests.java | 5 + .../ProjectApplicationController.java | 28 ++--- .../project/job/CleanUpReportJob.java | 25 +++++ .../service/ProjectApplicationService.java | 105 ++++++++++++++++-- .../src/main/resources/permission.json | 15 +++ .../ProjectApplicationControllerTests.java | 94 ++++++++-------- .../param/ProjectApplicationDefinition.java | 22 ++++ .../src/test/resources/application.properties | 2 +- .../mock/CleanupTestResourceService.java | 5 + .../service/CleanupPlanResourceService.java | 5 + .../plan/controller/CleanupResourceTests.java | 5 + 27 files changed, 390 insertions(+), 74 deletions(-) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/config/ScheduleConfig.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ProjectApplicationType.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ScheduleType.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/job/CleanUpReportJob.java create mode 100644 backend/services/project-management/src/test/java/io/metersphere/project/controller/param/ProjectApplicationDefinition.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/config/ScheduleConfig.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/config/ScheduleConfig.java new file mode 100644 index 0000000000..183d8b000b --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/config/ScheduleConfig.java @@ -0,0 +1,23 @@ +package io.metersphere.sdk.config; + +import io.metersphere.sdk.sechedule.BaseScheduleService; +import io.metersphere.sdk.sechedule.ScheduleManager; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ScheduleConfig { + + @Bean + @ConditionalOnProperty(prefix = "quartz", value = "enabled", havingValue = "true") + public ScheduleManager scheduleManager() { + return new ScheduleManager(); + } + + @Bean + @ConditionalOnProperty(prefix = "quartz", value = "enabled", havingValue = "true") + public BaseScheduleService baseScheduleService() { + return new BaseScheduleService(); + } +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PermissionConstants.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PermissionConstants.java index 58ba1b4e5c..517ab057b7 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PermissionConstants.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PermissionConstants.java @@ -112,4 +112,7 @@ public class PermissionConstants { public static final String PROJECT_MESSAGE_READ_UPDATE = "PROJECT_MESSAGE:READ+UPDATE"; public static final String PROJECT_MESSAGE_READ_ADD = "PROJECT_MESSAGE:READ+ADD"; public static final String PROJECT_MESSAGE_READ_DELETE = "PROJECT_MESSAGE:READ+DELETE"; + + public static final String PROJECT_APPLICATION_TEST_PLAN_READ = "PROJECT_APPLICATION_TEST_PLAN:READ"; + public static final String PROJECT_APPLICATION_TEST_PLAN_UPDATE = "PROJECT_APPLICATION_TEST_PLAN:UPDATE"; } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ProjectApplicationType.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ProjectApplicationType.java new file mode 100644 index 0000000000..5f620fc4ac --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ProjectApplicationType.java @@ -0,0 +1,48 @@ +package io.metersphere.sdk.constants; + +/** + * 应用设置 -相关配置 + */ +public enum ProjectApplicationType { + /** + * 工作台 我的待办 + */ + APPLICATION_WORKSTATION, + + + //测试计划 + /** + * 自定义测试计划报告保留范围 是否开启标识 type + */ + APPLICATION_CLEAN_TEST_PLAN_REPORT, + + /** + * 自定义测试计划报告保留范围 value + */ + APPLICATION_CLEAN_TEST_PLAN_REPORT_VALUE, + /** + * 自定义测试报告有效期 是否开启标识 type + */ + APPLICATION_TEST_PLAN_SHARE_REPORT, + + /** + * 自定义测试报告有效期 value + */ + APPLICATION_TEST_PLAN_SHARE_REPORT_VALUE, + + + //缺陷管理 + /** + * 同步缺陷 标识 + */ + APPLICATION_ISSUE, + + /** + * 缺陷模板 + */ + APPLICATION_ISSUE_TEMPLATE, + + + + +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ScheduleType.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ScheduleType.java new file mode 100644 index 0000000000..1deb6dcf95 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ScheduleType.java @@ -0,0 +1,5 @@ +package io.metersphere.sdk.constants; + +public enum ScheduleType { + CRON, SIMPLE +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseSystemConfigDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseSystemConfigDTO.java index bab1b5edc4..4fec58ec82 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseSystemConfigDTO.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseSystemConfigDTO.java @@ -9,7 +9,6 @@ public class BaseSystemConfigDTO { private String url; private String concurrency; private String prometheusHost; - private String seleniumDockerUrl; private String runMode; private String docUrl; } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java index b284cad0a4..662afc5233 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java @@ -20,4 +20,10 @@ public class ProjectServiceInvoker { service.deleteResources(projectId); } } + + public void invokeReportServices(String projectId) { + for (CleanupProjectResourceService service : cleanupProjectResourceServices) { + service.cleanReportResources(projectId); + } + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/sechedule/BaseScheduleService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/sechedule/BaseScheduleService.java index e318ccd43d..e6268d60d0 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/sechedule/BaseScheduleService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/sechedule/BaseScheduleService.java @@ -1,15 +1,20 @@ package io.metersphere.sdk.sechedule; +import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.util.SessionUtils; import io.metersphere.system.domain.Schedule; import io.metersphere.system.domain.ScheduleExample; import io.metersphere.system.mapper.ScheduleMapper; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.quartz.JobKey; +import org.quartz.SchedulerException; import org.quartz.TriggerKey; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; import java.util.UUID; @Transactional(rollbackFor = Exception.class) @@ -77,4 +82,23 @@ public class BaseScheduleService { private void removeJob(String key, String job) { scheduleManager.removeJob(new JobKey(key, job), new TriggerKey(key, job)); } + + public void addOrUpdateCronJob(Schedule request, JobKey jobKey, TriggerKey triggerKey, Class clazz) { + Boolean enable = request.getEnable(); + String cronExpression = request.getValue(); + if (Optional.ofNullable(enable).isPresent() && StringUtils.isNotBlank(cronExpression)) { + try { + scheduleManager.addOrUpdateCronJob(jobKey, triggerKey, clazz, cronExpression, + scheduleManager.getDefaultJobDataMap(request, cronExpression, SessionUtils.getUser().getId())); + } catch (SchedulerException e) { + throw new MSException("定时任务开启异常: " + e.getMessage()); + } + } else { + try { + scheduleManager.removeJob(jobKey, triggerKey); + } catch (Exception e) { + throw new MSException("定时任务关闭异常: " + e.getMessage()); + } + } + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CleanupProjectResourceService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CleanupProjectResourceService.java index ae8a1b3f9e..e606b395b7 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CleanupProjectResourceService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CleanupProjectResourceService.java @@ -5,4 +5,11 @@ package io.metersphere.sdk.service; */ public interface CleanupProjectResourceService { void deleteResources(String projectId); + + + /** + * 清理报告资源 + * @param projectId + */ + void cleanReportResources(String projectId); } diff --git a/backend/framework/sdk/src/main/resources/i18n/project_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/project_en_US.properties index 0846d412b3..b981bbe4ef 100644 --- a/backend/framework/sdk/src/main/resources/i18n/project_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/project_en_US.properties @@ -104,4 +104,7 @@ permission.project_file.name=File management permission.project_template.name=Template management permission.project_message.name=Message management permission.project_fake_error.name=Fake error +permission.project_application.name=Project application +permission.project_application_test_plan.read=Test plan read +permission.project_application_test_plan.update=Test plan update diff --git a/backend/framework/sdk/src/main/resources/i18n/project_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/project_zh_CN.properties index 0c09c92b5f..fe9cb748a5 100644 --- a/backend/framework/sdk/src/main/resources/i18n/project_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/project_zh_CN.properties @@ -104,3 +104,6 @@ permission.project_file.name=文件管理 permission.project_template.name=模版管理 permission.project_message.name=消息管理 permission.project_fake_error.name=误报库 +permission.project_application.name=应用管理 +permission.project_application_test_plan.read=测试计划-查询 +permission.project_application_test_plan.update=测试计划-编辑 diff --git a/backend/framework/sdk/src/main/resources/i18n/project_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/project_zh_TW.properties index 683806f662..8b9cdcde56 100644 --- a/backend/framework/sdk/src/main/resources/i18n/project_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/project_zh_TW.properties @@ -103,4 +103,7 @@ permission.project_environment.name=環境配置 permission.project_file.name=文件管理 permission.project_template.name=模版管理 permission.project_message.name=消息管理 -permission.project_fake_error.name=誤報庫 \ No newline at end of file +permission.project_fake_error.name=誤報庫 +permission.project_application.name=應用管理 +permission.project_application_test_plan.read=測試計劃-查詢 +permission.project_application_test_plan.update=測試計劃-編輯 \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupAPIResourceService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupAPIResourceService.java index d21fb33652..a877eaf4fe 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupAPIResourceService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupAPIResourceService.java @@ -11,4 +11,9 @@ public class CleanupAPIResourceService implements CleanupProjectResourceService public void deleteResources(String projectId) { LogUtils.info("删除当前项目[" + projectId + "]相关接口测试资源"); } + + @Override + public void cleanReportResources(String projectId) { + LogUtils.info("清理当前项目[" + projectId + "]相关接口测试报告资源"); + } } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/CleanupResourceTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/CleanupResourceTests.java index e0e63ecf50..81c541b8b0 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/CleanupResourceTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/CleanupResourceTests.java @@ -23,4 +23,9 @@ public class CleanupResourceTests { resourceService.deleteResources("test"); } + @Test + @Order(1) + public void testCleanupReportResource() throws Exception { + resourceService.cleanReportResources("test"); + } } diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/CleanupBugResourceService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/CleanupBugResourceService.java index 8b831a9d0e..fe171c6c4f 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/CleanupBugResourceService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/CleanupBugResourceService.java @@ -11,4 +11,9 @@ public class CleanupBugResourceService implements CleanupProjectResourceService public void deleteResources(String projectId) { LogUtils.info("删除当前项目[" + projectId + "]相关缺陷资源"); } + + @Override + public void cleanReportResources(String projectId) { + LogUtils.info("清理当前项目[" + projectId + "]相关缺陷报告资源"); + } } diff --git a/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/CleanupResourceTests.java b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/CleanupResourceTests.java index e5e166a662..7b16d9606d 100644 --- a/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/CleanupResourceTests.java +++ b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/CleanupResourceTests.java @@ -23,4 +23,9 @@ public class CleanupResourceTests { resourceService.deleteResources("test"); } + @Test + @Order(2) + public void testCleanupReportResource() throws Exception { + resourceService.cleanReportResources("test"); + } } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/CleanupCaseResourceService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/CleanupCaseResourceService.java index 79d3bff4a1..25d2145ca4 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/CleanupCaseResourceService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/CleanupCaseResourceService.java @@ -11,4 +11,9 @@ public class CleanupCaseResourceService implements CleanupProjectResourceService public void deleteResources(String projectId) { LogUtils.info("删除当前项目[" + projectId + "]相关功能用例资源"); } + + @Override + public void cleanReportResources(String projectId) { + LogUtils.info("清理当前项目[" + projectId + "]相关功能用例报告资源"); + } } diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/CleanupResourceTests.java b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/CleanupResourceTests.java index d09eb90254..8bf968d441 100644 --- a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/CleanupResourceTests.java +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/CleanupResourceTests.java @@ -23,4 +23,9 @@ public class CleanupResourceTests { resourceService.deleteResources("test"); } + @Test + @Order(2) + public void testCleanupReportResource() throws Exception { + resourceService.cleanReportResources("test"); + } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java index 16357a0da9..59cce52c32 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java @@ -2,35 +2,31 @@ package io.metersphere.project.controller; import io.metersphere.project.domain.ProjectApplication; import io.metersphere.project.service.ProjectApplicationService; -import io.metersphere.validation.groups.Created; +import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.log.annotation.Log; +import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.validation.groups.Updated; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.List; +@Tag(name = "项目管理-应用设置") @RestController -@RequestMapping("project/application") +@RequestMapping("/project/application") public class ProjectApplicationController { @Resource private ProjectApplicationService projectApplicationService; - @PostMapping("save") - public ProjectApplication save(@Validated({Created.class}) @RequestBody ProjectApplication application) { - return projectApplicationService.save(application); - } - /** - * 更新 - */ - @PostMapping("update") + @PostMapping("/update") + @Operation(summary = "应用设置-测试计划-配置") + @RequiresPermissions(PermissionConstants.PROJECT_APPLICATION_TEST_PLAN_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#application)", msClass = ProjectApplicationService.class) public ProjectApplication update(@Validated({Updated.class}) @RequestBody ProjectApplication application) { return projectApplicationService.update(application); } - - @GetMapping("list/{projectId}") - public List update(@PathVariable String projectId) { - return projectApplicationService.list(projectId); - } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/job/CleanUpReportJob.java b/backend/services/project-management/src/main/java/io/metersphere/project/job/CleanUpReportJob.java new file mode 100644 index 0000000000..0f56783ea3 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/job/CleanUpReportJob.java @@ -0,0 +1,25 @@ +package io.metersphere.project.job; + +import io.metersphere.sdk.sechedule.BaseScheduleJob; +import org.quartz.JobExecutionContext; +import org.quartz.JobKey; +import org.quartz.TriggerKey; + +public class CleanUpReportJob extends BaseScheduleJob { + + + + @Override + protected void businessExecute(JobExecutionContext context) { + //TODO 定时任务执行 清除报告 invokerReportServices + //serviceInvoker.invokeReportServices(projectId); + } + + public static JobKey getJobKey(String projectId) { + return new JobKey(projectId, CleanUpReportJob.class.getName()); + } + + public static TriggerKey getTriggerKey(String projectId) { + return new TriggerKey(projectId, CleanUpReportJob.class.getName()); + } +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java index 11b30f437b..2031b9bdbc 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java @@ -2,12 +2,26 @@ package io.metersphere.project.service; import io.metersphere.project.domain.ProjectApplication; import io.metersphere.project.domain.ProjectApplicationExample; +import io.metersphere.project.job.CleanUpReportJob; import io.metersphere.project.mapper.ProjectApplicationMapper; +import io.metersphere.sdk.constants.OperationLogConstants; +import io.metersphere.sdk.constants.ProjectApplicationType; +import io.metersphere.sdk.constants.ScheduleType; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.sechedule.BaseScheduleService; +import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.SessionUtils; +import io.metersphere.system.domain.Schedule; import jakarta.annotation.Resource; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; @Service @Transactional @@ -15,19 +29,94 @@ public class ProjectApplicationService { @Resource private ProjectApplicationMapper projectApplicationMapper; - public ProjectApplication save(ProjectApplication application) { - projectApplicationMapper.insert(application); - return application; - } + @Resource + private BaseScheduleService baseScheduleService; public ProjectApplication update(ProjectApplication application) { - projectApplicationMapper.updateByPrimaryKey(application); + //定时任务配置,检查是否存在定时任务配置,存在则更新,不存在则新增 + this.doBeforeUpdate(application); + //配置信息入库 + this.createOrUpdateConfig(application); return application; } - public List list(String projectId) { + private void createOrUpdateConfig(ProjectApplication application) { + String type = application.getType(); + String projectId = application.getProjectId(); ProjectApplicationExample example = new ProjectApplicationExample(); - example.createCriteria().andProjectIdEqualTo(projectId); - return projectApplicationMapper.selectByExample(example); + example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(type); + if (projectApplicationMapper.countByExample(example) > 0) { + example.clear(); + example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(type); + projectApplicationMapper.updateByExample(application, example); + } else { + projectApplicationMapper.insertSelective(application); + } + } + + private void doBeforeUpdate(ProjectApplication application) { + String type = application.getType(); + //TODO 自定义id配置 &其他配置 + if (StringUtils.equals(type, ProjectApplicationType.APPLICATION_CLEAN_TEST_PLAN_REPORT.name())) { + //处理测试计划报告 定时任务 + this.doHandleSchedule(application); + } + } + + private void doHandleSchedule(ProjectApplication application) { + String typeValue = application.getTypeValue(); + String projectId = application.getProjectId(); + Boolean enable = BooleanUtils.isTrue(Boolean.valueOf(typeValue)); + Schedule schedule = baseScheduleService.getScheduleByResource(application.getProjectId(), CleanUpReportJob.class.getName()); + Optional optional = Optional.ofNullable(schedule); + optional.ifPresentOrElse(s -> { + s.setEnable(enable); + baseScheduleService.editSchedule(s); + baseScheduleService.addOrUpdateCronJob(s, + CleanUpReportJob.getJobKey(projectId), + CleanUpReportJob.getTriggerKey(projectId), + CleanUpReportJob.class); + },() -> { + Schedule request = new Schedule(); + request.setName("Clean Report Job"); + request.setResourceId(projectId); + request.setKey(projectId); + request.setProjectId(projectId); + request.setEnable(enable); + request.setCreateUser(SessionUtils.getUserId()); + request.setType(ScheduleType.CRON.name()); + // 每天凌晨2点执行清理任务 + request.setValue("0 0 2 * * ?"); + request.setJob(CleanUpReportJob.class.getName()); + baseScheduleService.addSchedule(request); + baseScheduleService.addOrUpdateCronJob(request, + CleanUpReportJob.getJobKey(projectId), + CleanUpReportJob.getTriggerKey(projectId), + CleanUpReportJob.class); + }); + + } + + + /** + * 更新接口日志 + * + * @param application + * @return + */ + public LogDTO updateLog(ProjectApplication application) { + ProjectApplicationExample example = new ProjectApplicationExample(); + example.createCriteria().andTypeEqualTo(application.getType()).andProjectIdEqualTo(application.getProjectId()); + List list = projectApplicationMapper.selectByExample(example); + LogDTO dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + null, + OperationLogType.ADD.name(), + OperationLogModule.PROJECT_PROJECT_MANAGER, + "测试计划配置"); + dto.setOriginalValue(JSON.toJSONBytes(list)); + return dto; } } diff --git a/backend/services/project-management/src/main/resources/permission.json b/backend/services/project-management/src/main/resources/permission.json index 1c94a6c813..37c6c3ac92 100644 --- a/backend/services/project-management/src/main/resources/permission.json +++ b/backend/services/project-management/src/main/resources/permission.json @@ -113,6 +113,21 @@ "id": "PROJECT_FAKE_ERROR:READ+DELETE" } ] + }, + { + "id": "PROJECT_APPLICATION", + "name": "permission.project_application.name", + "license": true, + "permissions": [ + { + "id": "PROJECT_APPLICATION_TEST_PLAN:READ", + "name": "permission.project_application_test_plan.read" + }, + { + "id": "PROJECT_APPLICATION_TEST_PLAN:UPDATE", + "name": "permission.project_application_test_plan.update" + } + ] } ] } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java index 363152dc78..2fa5ff3df0 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java @@ -1,74 +1,74 @@ package io.metersphere.project.controller; +import io.metersphere.project.controller.param.ProjectApplicationDefinition; import io.metersphere.project.domain.ProjectApplication; import io.metersphere.sdk.base.BaseTest; -import io.metersphere.sdk.constants.SessionConstants; -import io.metersphere.sdk.util.JSON; -import jakarta.annotation.Resource; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @SpringBootTest @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @AutoConfigureMockMvc public class ProjectApplicationControllerTests extends BaseTest { - @Resource - private MockMvc mockMvc; - @Test - @Order(0) - public void testAddApp() throws Exception { - ProjectApplication projectApplication = new ProjectApplication(); - projectApplication.setProjectId("1"); - projectApplication.setType("1"); - projectApplication.setTypeValue("1"); - mockMvc.perform(MockMvcRequestBuilders.post("/project/application/save") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken) - .content(JSON.toJSONString(projectApplication)) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.data.typeValue").value("1")); - } + // 测试计划 + public static final String TEST_PLAN_CLEAN_REPORT_URL = "/project/application/update"; + //清理报告配置 + public static final String TEST_PLAN_CLEAN_REPORT = "APPLICATION_CLEAN_TEST_PLAN_REPORT"; + //分享报告配置 + public static final String TEST_PLAN_SHARE_REPORT = "APPLICATION_TEST_PLAN_SHARE_REPORT"; + + + /** + * 应用配置 - 测试计划 清理报告配置 + * @throws Exception + */ @Test @Order(1) - public void testUpdateApp() throws Exception { - ProjectApplication projectApplication = new ProjectApplication(); - projectApplication.setProjectId("1"); - projectApplication.setType("1"); - projectApplication.setTypeValue("2"); - mockMvc.perform(MockMvcRequestBuilders.post("/project/application/update") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken) - .content(JSON.toJSONString(projectApplication)) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.data.typeValue").value("2")); + public void testTestPlanClean() throws Exception { + //新增 + ProjectApplication request = creatRequest(TEST_PLAN_CLEAN_REPORT); + this.requestPost(TEST_PLAN_CLEAN_REPORT_URL, request); + + //更新 + request.setTypeValue("false"); + this.requestPost(TEST_PLAN_CLEAN_REPORT_URL, request); + + // @@异常参数校验 + updatedGroupParamValidateTest(ProjectApplicationDefinition.class, TEST_PLAN_CLEAN_REPORT_URL); + } + + + /** + * 应用管理 测试计划 - 分享报告配置 + * @throws Exception + */ @Test @Order(2) - public void testListApp() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/project/application/list/1") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.data[0].typeValue").value("2")); + public void testUpdateApp() throws Exception { + //新增 + ProjectApplication request = creatRequest(TEST_PLAN_SHARE_REPORT); + this.requestPost(TEST_PLAN_CLEAN_REPORT_URL, request); + //更新 + request.setTypeValue("false"); + this.requestPost(TEST_PLAN_CLEAN_REPORT_URL, request); + } + + + private ProjectApplication creatRequest(String type) { + ProjectApplication projectApplication = new ProjectApplication(); + projectApplication.setProjectId("project_application_test_id"); + projectApplication.setType(type); + projectApplication.setTypeValue("true"); + return projectApplication; } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/param/ProjectApplicationDefinition.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/param/ProjectApplicationDefinition.java new file mode 100644 index 0000000000..bc13e99ab6 --- /dev/null +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/param/ProjectApplicationDefinition.java @@ -0,0 +1,22 @@ +package io.metersphere.project.controller.param; + +import jakarta.validation.constraints.NotBlank; +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; + +@Getter +@Setter +public class ProjectApplicationDefinition implements Serializable { + + private static final long serialVersionUID = 1L; + + @NotBlank(message = "{project_application.project_id.not_blank}") + private String projectId; + + @NotBlank(message = "{project_application.type.not_blank}") + private String type; + + private String typeValue; +} diff --git a/backend/services/project-management/src/test/resources/application.properties b/backend/services/project-management/src/test/resources/application.properties index 637eab5e89..92a5f7baef 100644 --- a/backend/services/project-management/src/test/resources/application.properties +++ b/backend/services/project-management/src/test/resources/application.properties @@ -6,7 +6,7 @@ server.compression.enabled=true server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css,text/javascript,image/jpeg server.compression.min-response-size=2048 # -quartz.enabled=false +quartz.enabled=true quartz.scheduler-name=msScheduler quartz.thread-count=10 quartz.properties.org.quartz.jobStore.acquireTriggersWithinLock=true diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CleanupTestResourceService.java b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CleanupTestResourceService.java index 99649a1084..13ac4e0493 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CleanupTestResourceService.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CleanupTestResourceService.java @@ -11,4 +11,9 @@ public class CleanupTestResourceService implements CleanupProjectResourceService public void deleteResources(String projectId) { LogUtils.info("删除当前项目[" + projectId + "]TEST资源"); } + + @Override + public void cleanReportResources(String projectId) { + LogUtils.info("清理当前项目[" + projectId + "]TEST报告资源"); + } } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupPlanResourceService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupPlanResourceService.java index 0281788d46..8dbc688414 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupPlanResourceService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupPlanResourceService.java @@ -11,4 +11,9 @@ public class CleanupPlanResourceService implements CleanupProjectResourceService public void deleteResources(String projectId) { LogUtils.info("删除当前项目[" + projectId + "]相关测试计划资源"); } + + @Override + public void cleanReportResources(String projectId) { + LogUtils.info("清理当前项目[" + projectId + "]相关测试计划报告资源"); + } } diff --git a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/CleanupResourceTests.java b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/CleanupResourceTests.java index 890fa62c51..42862a3bbb 100644 --- a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/CleanupResourceTests.java +++ b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/CleanupResourceTests.java @@ -23,4 +23,9 @@ public class CleanupResourceTests { resourceService.deleteResources("test"); } + @Test + @Order(2) + public void testCleanupReportResource() throws Exception { + resourceService.cleanReportResources("test"); + } }