From e2d3ab9cfc72b0ec843e95f1f14c06233fb51489 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Wed, 24 Jan 2024 12:10:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E8=A1=A5=E5=85=85=E5=9C=BA=E6=99=AF=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definition/ApiTestCaseController.java | 9 ++++++ .../scenario/ApiScenarioController.java | 22 ++++++++++++++ .../api/dto/scenario/ApiScenarioDTO.java | 6 ++++ .../definition/ApiTestCaseService.java | 13 +++++++- .../scenario/ApiScenarioLogService.java | 16 ++++++++++ .../service/scenario/ApiScenarioService.java | 26 +++++++++++++++- .../ApiScenarioControllerTests.java | 30 +++++++++++++++++++ .../ApiTestCaseControllerTests.java | 15 ++++++++++ 8 files changed, 135 insertions(+), 2 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java index 3120449680..2e7869de7d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java @@ -115,6 +115,15 @@ public class ApiTestCaseController { return apiTestCaseService.update(request, SessionUtils.getUserId()); } + @GetMapping(value = "/update-priority/{id}/{priority}") + @Operation(summary = "接口测试-接口管理-接口用例-更新等级") + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#id)", msClass = ApiTestCaseLogService.class) + @CheckOwner(resourceId = "#id", resourceType = "api_test_case") + public void updatePriority(@PathVariable String id, @PathVariable String priority) { + apiTestCaseService.updatePriority(id, priority, SessionUtils.getUserId()); + } + @GetMapping(value = "/update-status/{id}/{status}") @Operation(summary = "接口测试-接口管理-接口用例-更新状态") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java index 7038c76e84..06a1e15793 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java @@ -88,6 +88,7 @@ public class ApiScenarioController { @Operation(summary = "接口测试-接口场景管理-更新场景") @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_UPDATE) @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = ApiScenarioLogService.class) + @CheckOwner(resourceId = "#request.getId()", resourceType = "api_scenario") public ApiScenario update(@Validated @RequestBody ApiScenarioUpdateRequest request) { return apiScenarioService.update(request, SessionUtils.getUserId()); } @@ -96,6 +97,7 @@ public class ApiScenarioController { @Operation(summary = "接口测试-接口场景管理-删除场景") @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_DELETE) @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = ApiScenarioLogService.class) + @CheckOwner(resourceId = "#id", resourceType = "api_scenario") public void delete(@PathVariable String id) { apiScenarioService.delete(id); } @@ -104,6 +106,7 @@ public class ApiScenarioController { @Operation(summary = "接口测试-接口场景管理-删除场景到回收站") @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_DELETE) @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = ApiScenarioLogService.class) + @CheckOwner(resourceId = "#id", resourceType = "api_scenario") public void deleteToGc(@PathVariable String id) { apiScenarioService.deleteToGc(id); } @@ -114,4 +117,23 @@ public class ApiScenarioController { public String debug(@RequestBody ApiScenarioDebugRequest request) { return apiScenarioService.debug(request); } + + @GetMapping(value = "/update-status/{id}/{status}") + @Operation(summary = "接口测试-接口场景管理-更新状态") + @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#id)", msClass = ApiScenarioLogService.class) + @CheckOwner(resourceId = "#id", resourceType = "api_scenario") + public void updateStatus(@PathVariable String id, @PathVariable String status) { + apiScenarioService.updateStatus(id, status, SessionUtils.getUserId()); + } + + @GetMapping(value = "/update-priority/{id}/{priority}") + @Operation(summary = "接口测试-接口场景管理-更新等级") + @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#id)", msClass = ApiScenarioLogService.class) + @CheckOwner(resourceId = "#id", resourceType = "api_scenario") + public void updatePriority(@PathVariable String id, @PathVariable String priority) { + apiScenarioService.updatePriority(id, priority, SessionUtils.getUserId()); + } + } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java index 9f4fc9cae8..e0fe84748a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java @@ -22,6 +22,12 @@ public class ApiScenarioDTO extends ApiScenario { private String modulePath; @Schema(description = "环境名称") private String environmentName; + @Schema(description = "是否有定时任务") + private Boolean schedule; + @Schema(description = "定时任务状态") + private Boolean enable; + @Schema(description = "下一次执行时间") + private Long scheduleExecuteTime; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java index 5250a51007..58eb8b4d2d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java @@ -418,7 +418,6 @@ public class ApiTestCaseService { updateCase.setUpdateTime(System.currentTimeMillis()); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); ApiTestCaseMapper mapper = sqlSession.getMapper(ApiTestCaseMapper.class); - switch (request.getType()) { case PRIORITY -> batchUpdatePriority(example, updateCase, request.getPriority()); case STATUS -> batchUpdateStatus(example, updateCase, request.getStatus()); @@ -426,6 +425,8 @@ public class ApiTestCaseService { case ENVIRONMENT -> batchUpdateEnvironment(example, updateCase, request.getEnvId()); default -> throw new MSException(Translator.get("batch_edit_type_error")); } + sqlSession.flushStatements(); + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); List caseInfoByIds = extApiTestCaseMapper.getCaseInfoByIds(ids, false); apiTestCaseLogService.batchEditLog(caseInfoByIds, userId, projectId); } @@ -577,4 +578,14 @@ public class ApiTestCaseService { return operationHistoryList; } + + public void updatePriority(String id, String priority, String userId) { + checkResourceExist(id); + ApiTestCase update = new ApiTestCase(); + update.setId(id); + update.setPriority(priority); + update.setUpdateUser(userId); + update.setUpdateTime(System.currentTimeMillis()); + apiTestCaseMapper.updateByPrimaryKeySelective(update); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioLogService.java index a35f1a8524..fdd18c17ac 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioLogService.java @@ -131,6 +131,22 @@ public class ApiScenarioLogService { return dto; } + public LogDTO updateLog(String id) { + ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(id); + // todo 记录完整的场景信息 + LogDTO dto = new LogDTO( + null, + null, + apiScenario.getId(), + null, + OperationLogType.UPDATE.name(), + OperationLogModule.API_SCENARIO, + apiScenario.getName()); + dto.setHistory(true); + dto.setOriginalValue(JSON.toJSONBytes(apiScenario)); + return dto; + } + public LogDTO deleteLog(String id) { ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(id); LogDTO dto = new LogDTO( diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index 0f1f7a1acd..fe4f3ba54d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -178,6 +178,8 @@ public class ApiScenarioService { case ENVIRONMENT -> batchUpdateEnvironment(example, updateScenario, request); default -> throw new MSException(Translator.get("batch_edit_type_error")); } + sqlSession.flushStatements(); + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); List scenarioInfoByIds = extApiScenarioMapper.getInfoByIds(ids, false); apiScenarioLogService.batchEditLog(scenarioInfoByIds, userId, projectId); } @@ -506,7 +508,7 @@ public class ApiScenarioService { * @return */ private List getApiScenarioSteps(ApiScenarioStepRequest parent, - List steps) { + List steps) { if (CollectionUtils.isEmpty(steps)) { return Collections.emptyList(); @@ -737,6 +739,7 @@ public class ApiScenarioService { /** * 设置部分引用的步骤的启用状态 + * * @param step * @param stepDetailMap */ @@ -751,6 +754,7 @@ public class ApiScenarioService { /** * 设置部分引用的步骤的启用状态 + * * @param steps * @param enableStepIds * @param stepDetailMap @@ -868,4 +872,24 @@ public class ApiScenarioService { } return scenarioConfig; } + + public void updateStatus(String id, String status, String userId) { + checkResourceExist(id); + ApiScenario update = new ApiScenario(); + update.setId(id); + update.setStatus(status); + update.setUpdateUser(userId); + update.setUpdateTime(System.currentTimeMillis()); + apiScenarioMapper.updateByPrimaryKeySelective(update); + } + + public void updatePriority(String id, String priority, String userId) { + checkResourceExist(id); + ApiScenario update = new ApiScenario(); + update.setId(id); + update.setPriority(priority); + update.setUpdateUser(userId); + update.setUpdateTime(System.currentTimeMillis()); + apiScenarioMapper.updateByPrimaryKeySelective(update); + } } \ No newline at end of file diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java index cb0fa16555..6e53f17a33 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java @@ -63,6 +63,8 @@ public class ApiScenarioControllerTests extends BaseTest { protected static final String UPLOAD_TEMP_FILE = "upload/temp/file"; protected static final String DELETE_TO_GC = "delete-to-gc/{0}"; protected static final String DEBUG = "debug"; + private static final String UPDATE_STATUS = "update-status"; + private static final String UPDATE_PRIORITY = "update-priority"; private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); @Resource @@ -609,6 +611,34 @@ public class ApiScenarioControllerTests extends BaseTest { assertErrorCode(this.requestGet(FOLLOW + "111"), NOT_FOUND); } + @Test + @Order(12) + public void updateStatus() throws Exception { + List apiScenarioList = apiScenarioMapper.selectByExample(new ApiScenarioExample()); + String scenarioId = apiScenarioList.getFirst().getId(); + // @@请求成功 + this.requestGetWithOk(UPDATE_STATUS + "/" + scenarioId + "/Underway"); + ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(scenarioId); + Assertions.assertEquals(apiScenario.getStatus(), "Underway"); + // @@校验日志 + checkLog(scenarioId, OperationLogType.UPDATE); + // @@校验权限 + requestGetPermissionTest(PermissionConstants.PROJECT_API_SCENARIO_UPDATE, UPDATE_STATUS + "/" + scenarioId + "/Underway"); + } + + @Test + @Order(12) + public void updatePriority() throws Exception { + // @@请求成功 + this.requestGetWithOk(UPDATE_PRIORITY + "/" + "api-scenario-id1" + "/P1"); + ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey("api-scenario-id1"); + Assertions.assertEquals(apiScenario.getPriority(), "P1"); + // @@校验日志 + checkLog("api-scenario-id1", OperationLogType.UPDATE); + // @@校验权限 + requestGetPermissionTest(PermissionConstants.PROJECT_API_SCENARIO_UPDATE, UPDATE_PRIORITY + "/" + "api-scenario-id1" + "/P1"); + } + @Test @Order(13) public void batchEdit() throws Exception { diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java index 878d8f6c9e..9600827a0f 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java @@ -77,6 +77,7 @@ public class ApiTestCaseControllerTests extends BaseTest { private static final String PAGE = BASE_PATH + "page"; private static final String TRASH_PAGE = BASE_PATH + "trash/page"; private static final String UPDATE_STATUS = BASE_PATH + "update-status"; + private static final String UPDATE_PRIORITY = BASE_PATH + "update-priority"; private static final String BATCH_EDIT = BASE_PATH + "batch/edit"; private static final String BATCH_DELETE = BASE_PATH + "batch/delete"; private static final String BATCH_MOVE_GC = BASE_PATH + "batch/move-gc"; @@ -696,6 +697,20 @@ public class ApiTestCaseControllerTests extends BaseTest { requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE, UPDATE_STATUS + "/" + apiTestCase.getId() + "/Underway"); } + @Test + @Order(12) + public void updatePriority() throws Exception { + // @@请求成功 + this.requestGetWithOk(UPDATE_PRIORITY + "/" + apiTestCase.getId() + "/P1"); + ApiTestCase apiCase = apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId()); + Assertions.assertEquals(apiCase.getPriority(), "P1"); + // @@校验日志 + checkLog(apiTestCase.getId(), OperationLogType.UPDATE); + this.requestGet(UPDATE_PRIORITY + "/" + "11111" + "/P1").andExpect(ERROR_REQUEST_MATCHER); + // @@校验权限 + requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE, UPDATE_PRIORITY + "/" + apiTestCase.getId() + "/P1"); + } + @Test @Order(13) public void batchEdit() throws Exception {