feat(测试计划): 测试计划批量修改定时任务功能开发

This commit is contained in:
Jianguo-Genius 2024-11-04 17:13:51 +08:00 committed by Craftsman
parent 0f8e535204
commit 3316487592
6 changed files with 126 additions and 2 deletions

View File

@ -232,6 +232,15 @@ public class TestPlanController {
return testPlanScheduleService.scheduleConfig(request, SessionUtils.getUserId()); return testPlanScheduleService.scheduleConfig(request, SessionUtils.getUserId());
} }
@PostMapping(value = "/batch-schedule-config")
@Operation(summary = "接口测试-接口场景管理-定时任务配置")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_EXECUTE)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public void batchScheduleConfig(@Validated @RequestBody TestPlanScheduleBatchConfigRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getProjectId(), TestPlanResourceConfig.CHECK_TYPE_PROJECT, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN));
testPlanScheduleService.batchScheduleConfig(request, SessionUtils.getUserId());
}
@GetMapping(value = "/schedule-config-delete/{testPlanId}") @GetMapping(value = "/schedule-config-delete/{testPlanId}")
@Operation(summary = "接口测试-接口场景管理-删除定时任务配置") @Operation(summary = "接口测试-接口场景管理-删除定时任务配置")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_EXECUTE) @RequiresPermissions(PermissionConstants.TEST_PLAN_READ_EXECUTE)

View File

@ -0,0 +1,25 @@
package io.metersphere.plan.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class TestPlanScheduleBatchConfigRequest extends TestPlanBatchProcessRequest {
@Schema(description = "启用/禁用")
private boolean enable;
@Schema(description = "Cron表达式")
@NotBlank
@Size(max = 255, message = "{length.too.large}")
private String cron;
@Schema(description = "运行配置")
private Map<String, String> runConfig = new HashMap<>();
}

View File

@ -265,4 +265,25 @@ public class TestPlanLogService {
return OperationLogModule.TEST_PLAN_TEST_PLAN_GROUP; return OperationLogModule.TEST_PLAN_TEST_PLAN_GROUP;
} }
} }
public void batchScheduleLog(String projectId, List<TestPlan> testPlanList, String operator) {
Project project = projectMapper.selectByPrimaryKey(projectId);
List<LogDTO> list = new ArrayList<>();
for (TestPlan testPlan : testPlanList) {
LogDTO dto = LogDTOBuilder.builder()
.projectId(project.getId())
.organizationId(project.getOrganizationId())
.type(OperationLogType.UPDATE.name())
.module(getLogModule(testPlan))
.sourceId(testPlan.getId())
.method("POST")
.path("/test-plan/batch-schedule-config")
.sourceId(testPlan.getId())
.content(Translator.get("test_plan_schedule") + ":" + testPlan.getName())
.createUser(operator)
.build().getLogDTO();
list.add(dto);
}
operationLogService.batchAdd(list);
}
} }

View File

@ -2,6 +2,7 @@ package io.metersphere.plan.service;
import io.metersphere.plan.domain.TestPlan; import io.metersphere.plan.domain.TestPlan;
import io.metersphere.plan.domain.TestPlanExample; import io.metersphere.plan.domain.TestPlanExample;
import io.metersphere.plan.dto.request.TestPlanScheduleBatchConfigRequest;
import io.metersphere.plan.job.TestPlanScheduleJob; import io.metersphere.plan.job.TestPlanScheduleJob;
import io.metersphere.plan.mapper.TestPlanMapper; import io.metersphere.plan.mapper.TestPlanMapper;
import io.metersphere.sdk.constants.ScheduleResourceType; import io.metersphere.sdk.constants.ScheduleResourceType;
@ -13,6 +14,7 @@ import io.metersphere.system.dto.request.ScheduleConfig;
import io.metersphere.system.dto.request.schedule.BaseScheduleConfigRequest; import io.metersphere.system.dto.request.schedule.BaseScheduleConfigRequest;
import io.metersphere.system.schedule.ScheduleService; import io.metersphere.system.schedule.ScheduleService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -27,6 +29,51 @@ public class TestPlanScheduleService {
private TestPlanMapper testPlanMapper; private TestPlanMapper testPlanMapper;
@Resource @Resource
private ScheduleService scheduleService; private ScheduleService scheduleService;
@Resource
private TestPlanLogService testPlanLogService;
public void batchScheduleConfig(TestPlanScheduleBatchConfigRequest request, String operator) {
// 目前计划的批量操作不支持全选所有页
List<String> ids = request.getSelectIds();
if (CollectionUtils.isNotEmpty(ids)) {
TestPlanExample example = new TestPlanExample();
example.createCriteria().andIdIn(ids).andGroupIdIsNotNull();
List<TestPlan> testPlanList = testPlanMapper.selectByExample(example);
for (TestPlan testPlan : testPlanList) {
ScheduleConfig scheduleConfig = ScheduleConfig.builder()
.resourceId(testPlan.getId())
.key(testPlan.getId())
.projectId(testPlan.getProjectId())
.name(testPlan.getName())
.enable(request.isEnable())
.cron(request.getCron())
.resourceType(StringUtils.equalsIgnoreCase(testPlan.getType(), TestPlanConstants.TEST_PLAN_TYPE_PLAN) ? ScheduleResourceType.TEST_PLAN.name() : ScheduleResourceType.TEST_PLAN_GROUP.name())
.config(JSON.toJSONString(request.getRunConfig()))
.build();
if (request.isEnable() && StringUtils.equalsIgnoreCase(testPlan.getType(), TestPlanConstants.TEST_PLAN_TYPE_GROUP)) {
//配置开启的测试计划组定时任务要将组下的所有测试计划定时任务都关闭掉
TestPlanExample childExample = new TestPlanExample();
childExample.createCriteria().andGroupIdEqualTo(testPlan.getId()).andStatusNotEqualTo(TestPlanConstants.TEST_PLAN_STATUS_ARCHIVED);
childExample.setOrderByClause("pos asc");
List<TestPlan> children = testPlanMapper.selectByExample(childExample);
for (TestPlan child : children) {
scheduleService.updateIfExist(child.getId(), false, TestPlanScheduleJob.getJobKey(testPlan.getId()),
TestPlanScheduleJob.getTriggerKey(testPlan.getId()),
TestPlanScheduleJob.class, operator);
}
}
scheduleService.scheduleConfig(
scheduleConfig,
TestPlanScheduleJob.getJobKey(testPlan.getId()),
TestPlanScheduleJob.getTriggerKey(testPlan.getId()),
TestPlanScheduleJob.class,
operator);
}
testPlanLogService.batchScheduleLog(request.getProjectId(), testPlanList, operator);
}
}
public String scheduleConfig(BaseScheduleConfigRequest request, String operator) { public String scheduleConfig(BaseScheduleConfigRequest request, String operator) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(request.getResourceId()); TestPlan testPlan = testPlanMapper.selectByPrimaryKey(request.getResourceId());

View File

@ -123,6 +123,7 @@ public class TestPlanTests extends BaseTest {
private static final String URL_POST_TEST_PLAN_UPDATE = "/test-plan/update"; private static final String URL_POST_TEST_PLAN_UPDATE = "/test-plan/update";
private static final String URL_POST_TEST_PLAN_BATCH_DELETE = "/test-plan/batch-delete"; private static final String URL_POST_TEST_PLAN_BATCH_DELETE = "/test-plan/batch-delete";
private static final String URL_POST_TEST_PLAN_SCHEDULE = "/test-plan/schedule-config"; private static final String URL_POST_TEST_PLAN_SCHEDULE = "/test-plan/schedule-config";
private static final String URL_POST_TEST_PLAN_BATCH_SCHEDULE = "/test-plan/batch-schedule-config";
private static final String URL_POST_TEST_PLAN_SCHEDULE_DELETE = "/test-plan/schedule-config-delete/%s"; private static final String URL_POST_TEST_PLAN_SCHEDULE_DELETE = "/test-plan/schedule-config-delete/%s";
//测试计划资源-功能用例 //测试计划资源-功能用例
@ -1487,9 +1488,16 @@ public class TestPlanTests extends BaseTest {
request.setEnable(true); request.setEnable(true);
request.setCron("0 0 0 * * ?"); request.setCron("0 0 0 * * ?");
TestPlanScheduleBatchConfigRequest batchRequest = new TestPlanScheduleBatchConfigRequest();
batchRequest.setProjectId(project.getId());
batchRequest.setEnable(false);
batchRequest.setCron("0 0 0 * * ?");
batchRequest.setSelectIds(new ArrayList<>(List.of(groupTestPlanId7, groupTestPlanId15)));
//先测试一下没有开启模块时接口能否使用 //先测试一下没有开启模块时接口能否使用
testPlanTestService.removeProjectModule(project, PROJECT_MODULE, "testPlan"); testPlanTestService.removeProjectModule(project, PROJECT_MODULE, "testPlan");
this.requestPost(URL_POST_TEST_PLAN_SCHEDULE, request).andExpect(status().is5xxServerError()); this.requestPost(URL_POST_TEST_PLAN_SCHEDULE, request).andExpect(status().is5xxServerError());
this.requestPost(URL_POST_TEST_PLAN_BATCH_SCHEDULE, batchRequest).andExpect(status().is5xxServerError());
this.requestGet(String.format(URL_POST_TEST_PLAN_SCHEDULE_DELETE, groupTestPlanId7)).andExpect(status().is5xxServerError()); this.requestGet(String.format(URL_POST_TEST_PLAN_SCHEDULE_DELETE, groupTestPlanId7)).andExpect(status().is5xxServerError());
//恢复 //恢复
testPlanTestService.resetProjectModule(project, PROJECT_MODULE); testPlanTestService.resetProjectModule(project, PROJECT_MODULE);
@ -1508,12 +1516,21 @@ public class TestPlanTests extends BaseTest {
TestPlanStatisticsResponse.class); TestPlanStatisticsResponse.class);
Assertions.assertTrue(statisticsResponses.size() > 1); Assertions.assertTrue(statisticsResponses.size() > 1);
//增加日志检查 //增加日志检查
LOG_CHECK_LIST.add( LOG_CHECK_LIST.add(
new CheckLogModel(groupTestPlanId7, OperationLogType.UPDATE, null) new CheckLogModel(groupTestPlanId7, OperationLogType.UPDATE, null)
); );
this.requestPostWithOk(URL_POST_TEST_PLAN_BATCH_SCHEDULE, batchRequest);
testPlanTestService.checkSchedule(groupTestPlanId7, batchRequest.isEnable());
testPlanTestService.checkSchedule(groupTestPlanId15, batchRequest.isEnable());
batchRequest.setEnable(true);
this.requestPostWithOk(URL_POST_TEST_PLAN_BATCH_SCHEDULE, batchRequest);
testPlanTestService.checkSchedule(groupTestPlanId7, batchRequest.isEnable());
testPlanTestService.checkSchedule(groupTestPlanId15, batchRequest.isEnable());
//增加日志检查
LOG_CHECK_LIST.add(
new CheckLogModel(groupTestPlanId15, OperationLogType.UPDATE, null)
);
//关闭 //关闭
request.setEnable(false); request.setEnable(false);
result = this.requestPostAndReturn(URL_POST_TEST_PLAN_SCHEDULE, request); result = this.requestPostAndReturn(URL_POST_TEST_PLAN_SCHEDULE, request);

View File

@ -471,6 +471,11 @@ public class TestPlanTestService {
Assertions.assertEquals(scheduler.checkExists(TestPlanScheduleJob.getJobKey(resourceId)), isEnable); Assertions.assertEquals(scheduler.checkExists(TestPlanScheduleJob.getJobKey(resourceId)), isEnable);
} }
public void checkSchedule(String resourceId, boolean isEnable) throws Exception {
Assertions.assertEquals(extScheduleMapper.countByResourceId(resourceId), 1L);
Assertions.assertEquals(scheduler.checkExists(TestPlanScheduleJob.getJobKey(resourceId)), isEnable);
}
public void checkScheduleIsRemove(String resourceId) throws SchedulerException { public void checkScheduleIsRemove(String resourceId) throws SchedulerException {
Assertions.assertEquals(extScheduleMapper.countByResourceId(resourceId), 0L); Assertions.assertEquals(extScheduleMapper.countByResourceId(resourceId), 0L);
Assertions.assertEquals(scheduler.checkExists(TestPlanScheduleJob.getJobKey(resourceId)), false); Assertions.assertEquals(scheduler.checkExists(TestPlanScheduleJob.getJobKey(resourceId)), false);