From f424fd1a4b3bef5fafd77374ad0dd1e81a5a3329 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Wed, 6 Mar 2024 17:07:29 +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=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definition/ApiDefinitionController.java | 42 +++---- .../definition/ApiTestCaseController.java | 4 +- .../api/dto/debug/ApiDebugAddRequest.java | 2 +- .../api/dto/debug/ApiDebugUpdateRequest.java | 2 +- .../definition/ApiDefinitionAddRequest.java | 2 +- .../definition/ApiDefinitionLogService.java | 104 ++++++++---------- .../definition/ApiDefinitionService.java | 15 +-- .../definition/ApiTestCaseLogService.java | 27 +---- .../ApiDefinitionControllerTests.java | 90 ++++++--------- .../ApiTestCaseControllerTests.java | 20 ++-- .../system/log/aspect/OperationLogAspect.java | 8 +- 11 files changed, 134 insertions(+), 182 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java index c4910c2bee..1532acbbba 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java @@ -82,22 +82,22 @@ public class ApiDefinitionController { apiDefinitionService.batchUpdate(request, SessionUtils.getUserId()); } - @PostMapping(value = "/delete") + @GetMapping("/delete-to-gc/{id}") @Operation(summary = "接口测试-接口管理-删除接口定义到回收站") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DELETE) - @Log(type = OperationLogType.DELETE, expression = "#msClass.delLog(#request)", msClass = ApiDefinitionLogService.class) - @CheckOwner(resourceId = "#request.getId()", resourceType = "api_definition") - public void delete(@Validated @RequestBody ApiDefinitionDeleteRequest request) { - apiDefinitionService.delete(request, SessionUtils.getUserId()); + @Log(type = OperationLogType.DELETE, expression = "#msClass.moveToGcLog(#id)", msClass = ApiDefinitionLogService.class) + @CheckOwner(resourceId = "#id", resourceType = "api_definition") + public void deleteToGc(@PathVariable String id, @RequestParam(required = false) boolean deleteAllVersion) { + apiDefinitionService.deleteToGc(id, deleteAllVersion, SessionUtils.getUserId()); } - @PostMapping(value = "/batch-del") + @PostMapping(value = "/batch/delete-to-gc") @Operation(summary = "接口测试-接口管理-批量删除接口定义到回收站") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DELETE) @CheckOwner(resourceId = "#request.getSelectIds()", resourceType = "api_definition") @SendNotice(taskType = NoticeConstants.TaskType.API_DEFINITION_TASK, event = NoticeConstants.Event.DELETE, target = "#targetClass.getBatchEditApiDTO(#request)", targetClass = ApiDefinitionNoticeService.class) - public void batchDelete(@Validated @RequestBody ApiDefinitionBatchRequest request) { - apiDefinitionService.batchDelete(request, SessionUtils.getUserId()); + public void batchDeleteToGc(@Validated @RequestBody ApiDefinitionBatchRequest request) { + apiDefinitionService.batchDeleteToGc(request, SessionUtils.getUserId()); } @PostMapping(value = "/copy") @@ -161,13 +161,21 @@ public class ApiDefinitionController { apiDefinitionService.recover(request, SessionUtils.getUserId()); } - @PostMapping(value = "/trash-del") + @GetMapping("/delete/{id}") @Operation(summary = "接口测试-接口管理-删除回收站接口定义") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DELETE) - @Log(type = OperationLogType.DELETE, expression = "#msClass.trashDelLog(#request)", msClass = ApiDefinitionLogService.class) - @CheckOwner(resourceId = "#request.getId()", resourceType = "api_definition") - public void trashDel(@Validated @RequestBody ApiDefinitionDeleteRequest request) { - apiDefinitionService.trashDel(request, SessionUtils.getUserId()); + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = ApiDefinitionLogService.class) + @CheckOwner(resourceId = "#id", resourceType = "api_definition") + public void delete(@PathVariable String id) { + apiDefinitionService.delete(id, SessionUtils.getUserId()); + } + + @PostMapping("/batch/delete") + @Operation(summary = "接口测试-接口管理-批量从回收站删除接口定义") + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DELETE) + @CheckOwner(resourceId = "#request.getSelectIds()", resourceType = "api_definition") + public void batchDelete(@Validated @RequestBody ApiDefinitionBatchRequest request) { + apiDefinitionService.batchDelete(request, SessionUtils.getUserId()); } @PostMapping(value = "/batch-recover") @@ -178,14 +186,6 @@ public class ApiDefinitionController { apiDefinitionService.batchRecover(request, SessionUtils.getUserId()); } - @PostMapping(value = "/batch-trash-del") - @Operation(summary = "接口测试-接口管理-批量从回收站删除接口定义") - @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DELETE) - @CheckOwner(resourceId = "#request.getSelectIds()", resourceType = "api_definition") - public void batchTrashDel(@Validated @RequestBody ApiDefinitionBatchRequest request) { - apiDefinitionService.batchTrashDel(request, SessionUtils.getUserId()); - } - @PostMapping("/page-doc") @Operation(summary = "接口测试-接口管理-接口文档列表") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DOC_READ) 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 c904903a0b..fbfa3c7039 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 @@ -65,7 +65,7 @@ public class ApiTestCaseController { return apiTestCaseService.get(id, SessionUtils.getUserId()); } - @GetMapping("/move-gc/{id}") + @GetMapping("/delete-to-gc/{id}") @Operation(summary = "接口测试-接口管理-接口用例-移动到回收站") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE) @Log(type = OperationLogType.DELETE, expression = "#msClass.moveToGcLog(#id)", msClass = ApiTestCaseLogService.class) @@ -159,7 +159,7 @@ public class ApiTestCaseController { apiTestCaseService.batchDelete(request, SessionUtils.getUserId()); } - @PostMapping("/batch/move-gc") + @PostMapping("/batch/delete-to-gc") @Operation(summary = "接口测试-接口管理-接口用例-批量移动到回收站") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE) @CheckOwner(resourceId = "#request.getSelectIds()", resourceType = "api_test_case") diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugAddRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugAddRequest.java index 16600a442c..fe2e9c68df 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugAddRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugAddRequest.java @@ -33,7 +33,7 @@ public class ApiDebugAddRequest implements Serializable { private String method; @Schema(description = "http协议url/其它协议则为空") - @Size(max = 500, message = "{api_debug.protocol.length_range}") + @Size(max = 500, message = "{api_debug.path.length_range}") private String path; @Schema(description = "项目fk", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugUpdateRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugUpdateRequest.java index 4d460671c0..41fe420609 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugUpdateRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugUpdateRequest.java @@ -31,7 +31,7 @@ public class ApiDebugUpdateRequest implements Serializable { private String method; @Schema(description = "http协议路径/其它协议则为空") - @Size(max = 500, message = "{api_debug.protocol.length_range}") + @Size(max = 500, message = "{api_debug.path.length_range}") private String path; @Schema(description = "模块fk", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java index cdb0a27943..21c10125e0 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java @@ -47,7 +47,7 @@ public class ApiDefinitionAddRequest implements Serializable { private String method; @Schema(description = "http协议路径/其它协议则为空") - @Size(max = 500, message = "{api_debug.protocol.length_range}") + @Size(max = 500, message = "{api_debug.path.length_range}") private String path; @Schema(description = "接口状态/进行中/已完成", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java index 66668a74c5..ef7a554923 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java @@ -11,6 +11,7 @@ import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; +import io.metersphere.system.log.aspect.OperationLogAspect; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; @@ -52,8 +53,8 @@ public class ApiDefinitionLogService { OperationLogType.ADD.name(), OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, request.getName()); + dto.setHistory(false); - dto.setPath("/api/definition/add"); dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(request)); return dto; @@ -77,7 +78,6 @@ public class ApiDefinitionLogService { OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, request.getName()); dto.setHistory(true); - dto.setPath("/api/definition/update"); dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); return dto; @@ -85,40 +85,50 @@ public class ApiDefinitionLogService { return null; } - /** - * 删除接口日志 - * - * @param request - * @return - */ - public LogDTO delLog(ApiDefinitionDeleteRequest request) { - ApiDefinitionDTO apiDefinition = getOriginalValue(request.getId()); - if (apiDefinition.getId() != null) { - LogDTO dto = new LogDTO( - request.getProjectId(), - null, - request.getId(), - null, - OperationLogType.DELETE.name(), - OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, - apiDefinition.getName()); - dto.setHistory(false); - dto.setPath("/api/definition/delete"); - dto.setMethod(HttpMethodConstants.POST.name()); - dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); - return dto; - } + public LogDTO moveToGcLog(String id) { + ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(id); + Project project = projectMapper.selectByPrimaryKey(apiDefinition.getProjectId()); + LogDTO dto = new LogDTO( + apiDefinition.getProjectId(), + project.getOrganizationId(), + id, + null, + OperationLogType.DELETE.name(), + OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, + apiDefinition.getName()); - return null; + dto.setMethod(HttpMethodConstants.GET.name()); + dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); + return dto; } + public LogDTO deleteLog(String id) { + ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(id); + Project project = projectMapper.selectByPrimaryKey(apiDefinition.getProjectId()); + LogDTO dto = new LogDTO( + apiDefinition.getProjectId(), + project.getOrganizationId(), + id, + null, + OperationLogType.DELETE.name(), + OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, + apiDefinition.getName()); + + dto.setMethod(HttpMethodConstants.GET.name()); + dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); + operationLogService.deleteBySourceIds(List.of(id)); + return dto; + } + + + /** * 批量删除接口日志 * * @return */ public void batchDelLog(List ids, String userId, String projectId) { - saveBatchLog(projectId, ids, "/api/definition/batch-del", userId, OperationLogType.DELETE.name(), false); + saveBatchLog(projectId, ids, userId, OperationLogType.DELETE.name(), false); } /** @@ -127,7 +137,7 @@ public class ApiDefinitionLogService { * @return */ public void batchUpdateLog(List ids, String userId, String projectId) { - saveBatchLog(projectId, ids, "/api/definition/batch-update", userId, OperationLogType.UPDATE.name(), true); + saveBatchLog(projectId, ids, userId, OperationLogType.UPDATE.name(), true); } public LogDTO copyLog(ApiDefinitionCopyRequest request) { @@ -142,7 +152,6 @@ public class ApiDefinitionLogService { OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, apiDefinition.getName()); dto.setHistory(false); - dto.setPath("/api/definition/copy"); dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); return dto; @@ -151,7 +160,7 @@ public class ApiDefinitionLogService { } public void batchMoveLog(List ids, String userId, String projectId) { - saveBatchLog(projectId, ids, "/api/definition/batch-move", userId, OperationLogType.UPDATE.name(), false); + saveBatchLog(projectId, ids, userId, OperationLogType.UPDATE.name(), false); } public LogDTO followLog(String id) { @@ -167,7 +176,6 @@ public class ApiDefinitionLogService { OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, Translator.get("follow") + apiDefinition.getName()); - dto.setPath("/api/definition/follow/" + id); dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); return dto; @@ -193,7 +201,6 @@ public class ApiDefinitionLogService { OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, apiDefinition.getName()); dto.setHistory(false); - dto.setPath("/api/definition/recover"); dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); return dto; @@ -209,39 +216,14 @@ public class ApiDefinitionLogService { * @return */ public void batchRecoverLog(List ids, String userId, String projectId) { - saveBatchLog(projectId, ids, "/api/definition/batch-recover", userId, OperationLogType.RECOVER.name(), false); - } - - - /** - * 删除回收站接口定义接口日志 - */ - public LogDTO trashDelLog(ApiDefinitionDeleteRequest request) { - ApiDefinitionDTO apiDefinition = getOriginalValue(request.getId()); - if (apiDefinition.getId() != null) { - LogDTO dto = new LogDTO( - request.getProjectId(), - null, - request.getId(), - null, - OperationLogType.DELETE.name(), - OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, - apiDefinition.getName()); - - dto.setPath("/api/definition/trash-del"); - dto.setMethod(HttpMethodConstants.POST.name()); - dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); - return dto; - } - - return null; + saveBatchLog(projectId, ids, userId, OperationLogType.RECOVER.name(), false); } /** * 删除回收站接口定义接口日志 */ public void batchTrashDelLog(List ids, String userId, String projectId) { - saveBatchLog(projectId, ids, "/api/definition/batch-trash-del", userId, OperationLogType.DELETE.name(), false); + saveBatchLog(projectId, ids, userId, OperationLogType.DELETE.name(), false); } private ApiDefinitionDTO getOriginalValue(String id) { @@ -257,7 +239,7 @@ public class ApiDefinitionLogService { - private void saveBatchLog(String projectId, List ids, String path, String userId, String operationType, boolean isHistory) { + private void saveBatchLog(String projectId, List ids, String userId, String operationType, boolean isHistory) { List dtoList = new ArrayList<>(); if (CollectionUtils.isNotEmpty(ids)) { Project project = projectMapper.selectByPrimaryKey(projectId); @@ -278,7 +260,7 @@ public class ApiDefinitionLogService { item.getName()); dto.setHistory(isHistory); - dto.setPath(path); + dto.setPath(OperationLogAspect.getPath()); dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(apiDefinitionDTO)); dtoList.add(dto); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index fc3e6bee13..a93df9da7d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -422,12 +422,12 @@ public class ApiDefinitionService { return apiDefinition; } - public void delete(ApiDefinitionDeleteRequest request, String userId) { - checkApiDefinition(request.getId()); - handleDeleteApiDefinition(Collections.singletonList(request.getId()), request.getDeleteAll(), request.getProjectId(), userId, false); + public void deleteToGc(String id, boolean deleteAllVersion, String userId) { + ApiDefinition apiDefinition = checkApiDefinition(id); + handleDeleteApiDefinition(Collections.singletonList(id), deleteAllVersion, apiDefinition.getProjectId(), userId, false); } - public void batchDelete(ApiDefinitionBatchRequest request, String userId) { + public void batchDeleteToGc(ApiDefinitionBatchRequest request, String userId) { List ids = getBatchApiIds(request, request.getProjectId(), request.getProtocol(), false, userId); if (CollectionUtils.isNotEmpty(ids)) { handleDeleteApiDefinition(ids, request.getDeleteAll(), request.getProjectId(), userId, true); @@ -797,8 +797,9 @@ public class ApiDefinitionService { } } - public void trashDel(ApiDefinitionDeleteRequest request, String userId) { - handleTrashDelApiDefinition(Collections.singletonList(request.getId()), userId, request.getProjectId(), false); + public void delete(String id, String userId) { + ApiDefinition apiDefinition = checkApiDefinition(id); + handleTrashDelApiDefinition(Collections.singletonList(id), userId, apiDefinition.getProjectId(), false); } public void batchRecover(ApiDefinitionBatchRequest request, String userId) { @@ -808,7 +809,7 @@ public class ApiDefinitionService { } } - public void batchTrashDel(ApiDefinitionBatchRequest request, String userId) { + public void batchDelete(ApiDefinitionBatchRequest request, String userId) { List ids = getBatchApiIds(request, request.getProjectId(), request.getProtocol(), true, userId); if (CollectionUtils.isNotEmpty(ids)) { handleTrashDelApiDefinition(ids, userId, request.getProjectId(), true); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseLogService.java index a62cc8d1f2..66c70ca6ad 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseLogService.java @@ -18,6 +18,7 @@ import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; import io.metersphere.system.dto.builder.LogDTOBuilder; +import io.metersphere.system.log.aspect.OperationLogAspect; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; @@ -58,8 +59,6 @@ public class ApiTestCaseLogService { OperationLogType.ADD.name(), OperationLogModule.API_TEST_MANAGEMENT_CASE, request.getName()); - - dto.setPath("/api/case/add"); dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(request)); return dto; @@ -76,8 +75,6 @@ public class ApiTestCaseLogService { OperationLogType.DELETE.name(), OperationLogModule.API_TEST_MANAGEMENT_CASE, apiTestCase.getName()); - - dto.setPath("/api/case/delete/" + id); dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(apiTestCase)); operationLogService.deleteBySourceIds(List.of(id)); @@ -95,8 +92,6 @@ public class ApiTestCaseLogService { OperationLogType.DELETE.name(), OperationLogModule.API_TEST_MANAGEMENT_CASE, apiTestCase.getName()); - - dto.setPath("/api/case/move-gc/" + id); dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(apiTestCase)); return dto; @@ -114,7 +109,6 @@ public class ApiTestCaseLogService { OperationLogModule.API_TEST_MANAGEMENT_CASE, apiTestCase.getName()); dto.setHistory(false); - dto.setPath("/api/case/recover/" + id); dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(apiTestCase)); return dto; @@ -131,8 +125,6 @@ public class ApiTestCaseLogService { OperationLogType.UPDATE.name(), OperationLogModule.API_TEST_MANAGEMENT_CASE, Translator.get("follow") + apiTestCase.getName()); - - dto.setPath("/api/case/follow/" + id); dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(apiTestCase)); return dto; @@ -149,8 +141,6 @@ public class ApiTestCaseLogService { OperationLogType.UPDATE.name(), OperationLogModule.API_TEST_MANAGEMENT_CASE, Translator.get("unfollow") + apiTestCase.getName()); - - dto.setPath("/api/case/unfollow/" + id); dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(apiTestCase)); return dto; @@ -168,8 +158,6 @@ public class ApiTestCaseLogService { OperationLogType.UPDATE.name(), OperationLogModule.API_TEST_MANAGEMENT_CASE, request.getName()); - - dto.setPath("/api/case/update"); dto.setHistory(true); dto.setMethod(HttpMethodConstants.POST.name()); ApiTestCaseLogDTO apiTestCaseDTO = new ApiTestCaseLogDTO(); @@ -191,8 +179,6 @@ public class ApiTestCaseLogService { OperationLogType.UPDATE.name(), OperationLogModule.API_TEST_MANAGEMENT_CASE, apiTestCase.getName()); - - dto.setPath("/api/case/update"); dto.setHistory(true); dto.setMethod(HttpMethodConstants.POST.name()); ApiTestCaseLogDTO apiTestCaseDTO = new ApiTestCaseLogDTO(); @@ -212,7 +198,6 @@ public class ApiTestCaseLogService { .type(OperationLogType.DELETE.name()) .module(OperationLogModule.API_TEST_MANAGEMENT_CASE) .method(HttpMethodConstants.POST.name()) - .path("/api/case/batch/delete") .sourceId(item.getId()) .content(item.getName()) .createUser(operator) @@ -225,18 +210,18 @@ public class ApiTestCaseLogService { } public void batchToGcLog(List apiTestCases, String operator, String projectId) { - saveBatchLog(projectId, apiTestCases, "/api/case/batch/move-gc", operator, OperationLogType.DELETE.name(), false); + saveBatchLog(projectId, apiTestCases, operator, OperationLogType.DELETE.name(), false); } public void batchEditLog(List apiTestCases, String operator, String projectId) { - saveBatchLog(projectId, apiTestCases, "/api/case/batch/edit", operator, OperationLogType.UPDATE.name(), true); + saveBatchLog(projectId, apiTestCases, operator, OperationLogType.UPDATE.name(), true); } public void batchRecoverLog(List apiTestCases, String operator, String projectId) { - saveBatchLog(projectId, apiTestCases, "/api/case/recover", operator, OperationLogType.RECOVER.name(), false); + saveBatchLog(projectId, apiTestCases, operator, OperationLogType.RECOVER.name(), false); } - private void saveBatchLog(String projectId, List apiTestCases, String path, String operator, String operationType, boolean isHistory) { + private void saveBatchLog(String projectId, List apiTestCases, String operator, String operationType, boolean isHistory) { Project project = projectMapper.selectByPrimaryKey(projectId); //取出apiTestCases所有的id为新的list List caseId = apiTestCases.stream().map(ApiTestCase::getId).distinct().toList(); @@ -263,7 +248,7 @@ public class ApiTestCaseLogService { .type(operationType) .module(OperationLogModule.API_TEST_MANAGEMENT_CASE) .method(HttpMethodConstants.POST.name()) - .path(path) + .path(OperationLogAspect.getPath()) .sourceId(item.getId()) .content(item.getName()) .createUser(operator) diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index 2a7dca9ed0..b399991fd7 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -80,16 +80,16 @@ public class ApiDefinitionControllerTests extends BaseTest { private final static String ADD = "add"; private final static String UPDATE = "update"; private final static String BATCH_UPDATE = "batch-update"; - private final static String DELETE = "delete"; - private final static String BATCH_DELETE = "batch-del"; + private final static String DELETE_TO_GC = "delete-to-gc/{0}?deleteAllVersion={1}"; + private final static String BATCH_DELETE_TO_GC = "batch/delete-to-gc"; private final static String COPY = "copy"; private final static String BATCH_MOVE = "batch-move"; private final static String RESTORE = "recover"; private final static String BATCH_RESTORE = "batch-recover"; - private final static String TRASH_DEL = "trash-del"; - private final static String BATCH_TRASH_DEL = "batch-trash-del"; + private final static String DELETE = "delete/{0}"; + private final static String BATCH_DELETE = "batch/delete"; private final static String PAGE = "page"; private final static String PAGE_DOC = "page-doc"; @@ -1210,27 +1210,22 @@ public class ApiDefinitionControllerTests extends BaseTest { LogUtils.info("delete api test"); apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001"); // @只存在一个版本 - ApiDefinitionDeleteRequest apiDefinitionDeleteRequest = new ApiDefinitionDeleteRequest(); - apiDefinitionDeleteRequest.setId(apiDefinition.getId()); - apiDefinitionDeleteRequest.setProjectId(DEFAULT_PROJECT_ID); - apiDefinitionDeleteRequest.setDeleteAll(false); // @@请求成功 - this.requestPostWithOkAndReturn(DELETE, apiDefinitionDeleteRequest); - checkLogModelList.add(new CheckLogModel(apiDefinition.getId(), OperationLogType.DELETE, DELETE)); + this.requestGetWithOkAndReturn(DELETE_TO_GC, apiDefinition.getId(), false); + checkLogModelList.add(new CheckLogModel(apiDefinition.getId(), OperationLogType.DELETE, DELETE_TO_GC.replace("{0}", apiDefinition.getId()).replace("{1}", "false"))); ApiDefinition apiDefinitionInfo = apiDefinitionMapper.selectByPrimaryKey(apiDefinition.getId()); Assertions.assertTrue(apiDefinitionInfo.getDeleted()); Assertions.assertEquals("admin", apiDefinitionInfo.getDeleteUser()); Assertions.assertNotNull(apiDefinitionInfo.getDeleteTime()); // @存在多个版本 + String id = "1004"; // 列表删除 - apiDefinitionDeleteRequest.setDeleteAll(false); - apiDefinitionDeleteRequest.setId("1004"); - ApiDefinition delApiDefinition = apiDefinitionMapper.selectByPrimaryKey(apiDefinitionDeleteRequest.getId()); - MvcResult mvcResult = this.requestGetWithOk(VERSION + apiDefinitionDeleteRequest.getId()).andReturn(); + ApiDefinition delApiDefinition = apiDefinitionMapper.selectByPrimaryKey(id); + MvcResult mvcResult = this.requestGetWithOk(VERSION + id).andReturn(); List apiDefinitionVersionDTO = getResultDataArray(mvcResult, ApiDefinitionVersionDTO.class); if (!apiDefinitionVersionDTO.isEmpty()) { - this.requestPostWithOkAndReturn(DELETE, apiDefinitionDeleteRequest); + this.requestGetWithOkAndReturn(DELETE_TO_GC, id, false); // 效验数据 // 删除的数据为最新版本需要更新最近一条数据为最新数据 if (delApiDefinition.getLatest()) { @@ -1243,21 +1238,20 @@ public class ApiDefinitionControllerTests extends BaseTest { Assertions.assertFalse(updateApiDefinition.getDeleted()); } } - ApiDefinition delApiDefinitionInfo = apiDefinitionMapper.selectByPrimaryKey(apiDefinitionDeleteRequest.getId()); + ApiDefinition delApiDefinitionInfo = apiDefinitionMapper.selectByPrimaryKey(id); if (delApiDefinitionInfo != null) { Assertions.assertTrue(delApiDefinitionInfo.getDeleted()); Assertions.assertEquals("admin", delApiDefinitionInfo.getDeleteUser()); Assertions.assertNotNull(delApiDefinitionInfo.getDeleteTime()); } - checkLogModelList.add(new CheckLogModel(apiDefinitionDeleteRequest.getId(), OperationLogType.DELETE, DELETE)); + checkLogModelList.add(new CheckLogModel(id, OperationLogType.DELETE, DELETE_TO_GC.replace("{0}", id).replace("{1}", "false"))); } // 全部删除 - apiDefinitionDeleteRequest.setDeleteAll(true); - apiDefinitionDeleteRequest.setId("1002"); + id = "1002"; // @@请求成功 - this.requestPostWithOkAndReturn(DELETE, apiDefinitionDeleteRequest); + this.requestGetWithOkAndReturn(DELETE_TO_GC, id, true); - List ids = extApiDefinitionMapper.getApiDefinitionByRefId(apiDefinitionDeleteRequest.getId()).stream().map(ApiDefinitionVersionDTO::getId).toList(); + List ids = extApiDefinitionMapper.getApiDefinitionByRefId(id).stream().map(ApiDefinitionVersionDTO::getId).toList(); ApiDefinitionExample apiDefinitionExample = new ApiDefinitionExample(); apiDefinitionExample.createCriteria().andIdIn(ids); List apiDefinitions = apiDefinitionMapper.selectByExample(apiDefinitionExample); @@ -1268,13 +1262,11 @@ public class ApiDefinitionControllerTests extends BaseTest { Assertions.assertNotNull(item.getDeleteTime()); }); } - checkLogModelList.add(new CheckLogModel(apiDefinitionDeleteRequest.getId(), OperationLogType.DELETE, DELETE)); - apiDefinitionDeleteRequest.setId("121"); - apiDefinitionDeleteRequest.setDeleteAll(false); - assertErrorCode(this.requestPost(DELETE, apiDefinitionDeleteRequest), ApiResultCode.API_DEFINITION_NOT_EXIST); + checkLogModelList.add(new CheckLogModel(id, OperationLogType.DELETE, DELETE_TO_GC.replace("{0}", id).replace("{1}", "true"))); + id = "121"; + assertErrorCode(this.requestGet(DELETE_TO_GC, id, false), ApiResultCode.API_DEFINITION_NOT_EXIST); // @@校验权限 - apiDefinitionDeleteRequest.setId(apiDefinition.getId()); - requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, DELETE, apiDefinitionDeleteRequest); + requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, DELETE_TO_GC, apiDefinition.getId(), false); } @Test @@ -1288,14 +1280,14 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setSelectIds(List.of("1004")); request.setDeleteAll(false); request.setSelectAll(false); - this.requestPostWithOkAndReturn(BATCH_DELETE, request); + this.requestPostWithOkAndReturn(BATCH_DELETE_TO_GC, request); // @@校验日志 - checkLogModelList.add(new CheckLogModel("1004", OperationLogType.DELETE, BATCH_DELETE)); + checkLogModelList.add(new CheckLogModel("1004", OperationLogType.DELETE, BATCH_DELETE_TO_GC)); request.setSelectIds(List.of("1002")); request.setDeleteAll(false); request.setSelectAll(false); - assertErrorCode(this.requestPost(BATCH_DELETE, request), ApiResultCode.API_DEFINITION_NOT_EXIST); + assertErrorCode(this.requestPost(BATCH_DELETE_TO_GC, request), ApiResultCode.API_DEFINITION_NOT_EXIST); // 删除全部 条件为关键字为st-6的数据 request.setDeleteAll(true); request.setExcludeIds(List.of("1005")); @@ -1303,11 +1295,11 @@ public class ApiDefinitionControllerTests extends BaseTest { BaseCondition baseCondition = new BaseCondition(); baseCondition.setKeyword("st-6"); request.setCondition(baseCondition); - this.requestPostWithOkAndReturn(BATCH_DELETE, request); + this.requestPostWithOkAndReturn(BATCH_DELETE_TO_GC, request); // @@校验日志 - checkLogModelList.add(new CheckLogModel("1006", OperationLogType.DELETE, BATCH_DELETE)); + checkLogModelList.add(new CheckLogModel("1006", OperationLogType.DELETE, BATCH_DELETE_TO_GC)); // @@校验权限 - requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, BATCH_DELETE, request); + requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, BATCH_DELETE_TO_GC, request); } @@ -1410,26 +1402,17 @@ public class ApiDefinitionControllerTests extends BaseTest { LogUtils.info("trashDel api test"); apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001"); if (!apiDefinition.getDeleted()) { - ApiDefinitionDeleteRequest apiDefinitionDeleteRequest = new ApiDefinitionDeleteRequest(); - apiDefinitionDeleteRequest.setId(apiDefinition.getId()); - apiDefinitionDeleteRequest.setProjectId(DEFAULT_PROJECT_ID); - apiDefinitionDeleteRequest.setDeleteAll(false); // @@请求成功 - this.requestPostWithOkAndReturn(DELETE, apiDefinitionDeleteRequest); - checkLogModelList.add(new CheckLogModel(apiDefinition.getId(), OperationLogType.DELETE, DELETE)); + this.requestGetWithOkAndReturn(DELETE_TO_GC, apiDefinition.getId(), false); apiDefinition = apiDefinitionMapper.selectByPrimaryKey(apiDefinition.getId()); Assertions.assertTrue(apiDefinition.getDeleted()); Assertions.assertEquals("admin", apiDefinition.getDeleteUser()); Assertions.assertNotNull(apiDefinition.getDeleteTime()); } // @只存在一个版本 - ApiDefinitionDeleteRequest apiDefinitionDeleteRequest = new ApiDefinitionDeleteRequest(); - apiDefinitionDeleteRequest.setId(apiDefinition.getId()); - apiDefinitionDeleteRequest.setProjectId(DEFAULT_PROJECT_ID); - apiDefinitionDeleteRequest.setDeleteAll(false); // @@请求成功 - this.requestPostWithOk(TRASH_DEL, apiDefinitionDeleteRequest); - checkLogModelList.add(new CheckLogModel(apiDefinition.getId(), OperationLogType.DELETE, TRASH_DEL)); + this.requestGetWithOk(DELETE, apiDefinition.getId()); + checkLogModelList.add(new CheckLogModel(apiDefinition.getId(), OperationLogType.DELETE, DELETE.replace("{0}", apiDefinition.getId()))); // 验证数据 ApiDefinition apiDefinitionInfo = apiDefinitionMapper.selectByPrimaryKey(apiDefinition.getId()); Assertions.assertNull(apiDefinitionInfo); @@ -1443,7 +1426,7 @@ public class ApiDefinitionControllerTests extends BaseTest { Assertions.assertEquals(0, caseLists.size()); // @@校验权限 - requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, TRASH_DEL, apiDefinitionDeleteRequest); + requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, DELETE, apiDefinition.getId()); } @Test @@ -1456,7 +1439,7 @@ public class ApiDefinitionControllerTests extends BaseTest { // 删除选中 request.setSelectIds(List.of("1003")); request.setSelectAll(false); - this.requestPostWithOk(BATCH_TRASH_DEL, request); + this.requestPostWithOk(BATCH_DELETE, request); // 效验数据结果 ApiDefinitionExample apiDefinitionExample = new ApiDefinitionExample(); apiDefinitionExample.createCriteria().andIdIn(request.getSelectIds()); @@ -1475,13 +1458,8 @@ public class ApiDefinitionControllerTests extends BaseTest { // @@校验日志 apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1006"); if (!apiDefinition.getDeleted()) { - ApiDefinitionDeleteRequest apiDefinitionDeleteRequest = new ApiDefinitionDeleteRequest(); - apiDefinitionDeleteRequest.setId(apiDefinition.getId()); - apiDefinitionDeleteRequest.setProjectId(DEFAULT_PROJECT_ID); - apiDefinitionDeleteRequest.setDeleteAll(false); // @@请求成功 - this.requestPostWithOkAndReturn(DELETE, apiDefinitionDeleteRequest); - checkLogModelList.add(new CheckLogModel(apiDefinition.getId(), OperationLogType.DELETE, DELETE)); + this.requestGetWithOkAndReturn(DELETE_TO_GC, apiDefinition.getId(), false); apiDefinition = apiDefinitionMapper.selectByPrimaryKey(apiDefinition.getId()); Assertions.assertTrue(apiDefinition.getDeleted()); Assertions.assertEquals("admin", apiDefinition.getDeleteUser()); @@ -1493,14 +1471,14 @@ public class ApiDefinitionControllerTests extends BaseTest { BaseCondition baseCondition = new BaseCondition(); baseCondition.setKeyword("st-6"); request.setCondition(baseCondition); - this.requestPostWithOk(BATCH_TRASH_DEL, request); + this.requestPostWithOk(BATCH_DELETE, request); // @@校验日志 String[] ids = {"1003", "1006"}; for (String id : ids) { - checkLogModelList.add(new CheckLogModel(id, OperationLogType.DELETE, BATCH_TRASH_DEL)); + checkLogModelList.add(new CheckLogModel(id, OperationLogType.DELETE, BATCH_DELETE)); } // @@校验权限 - requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, BATCH_TRASH_DEL, request); + requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, BATCH_DELETE, request); } @Test 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 8b18df72b9..5225dbcaca 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 @@ -75,7 +75,7 @@ public class ApiTestCaseControllerTests extends BaseTest { private static final String BASE_PATH = "/api/case/"; private static final String ADD = "add"; private static final String GET = "get-detail/"; - private static final String MOVE_TO_GC = "move-gc/"; + private static final String DELETE_TO_GC = "delete-to-gc/{0}"; private static final String RECOVER = "recover/"; private static final String FOLLOW = "follow/"; private static final String UNFOLLOW = "unfollow/"; @@ -87,7 +87,7 @@ public class ApiTestCaseControllerTests extends BaseTest { private static final String UPDATE_PRIORITY = "update-priority"; private static final String BATCH_EDIT = "batch/edit"; private static final String BATCH_DELETE = "batch/delete"; - private static final String BATCH_MOVE_GC = "batch/move-gc"; + private static final String BATCH_DELETE_TO_GC = "batch/delete-to-gc"; private static final String BATCH_RECOVER = "batch/recover"; private static final String POS_URL = "edit/pos"; private static final String UPLOAD_TEMP_FILE = "upload/temp/file"; @@ -465,16 +465,16 @@ public class ApiTestCaseControllerTests extends BaseTest { @Order(4) public void moveToGC() throws Exception { // @@请求成功 - this.requestGetWithOk(MOVE_TO_GC + apiTestCase.getId()); + this.requestGetWithOk(DELETE_TO_GC, apiTestCase.getId()); ApiTestCase apiCase = apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId()); Assertions.assertTrue(apiCase.getDeleted()); Assertions.assertEquals(apiCase.getDeleteUser(), "admin"); Assertions.assertNotNull(apiCase.getDeleteTime()); // @@校验日志 checkLog(apiTestCase.getId(), OperationLogType.DELETE); - this.requestGet(MOVE_TO_GC + "111").andExpect(ERROR_REQUEST_MATCHER); + this.requestGet(DELETE_TO_GC, "111").andExpect(ERROR_REQUEST_MATCHER); // @@校验权限 - requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, MOVE_TO_GC + apiTestCase.getId()); + requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, DELETE_TO_GC, apiTestCase.getId()); } @Test @@ -942,13 +942,13 @@ public class ApiTestCaseControllerTests extends BaseTest { request.setSelectAll(false); request.setSelectIds(List.of(apiTestCase.getId())); request.setExcludeIds(List.of(apiTestCase.getId())); - requestPostWithOkAndReturn(BATCH_MOVE_GC, request); + requestPostWithOkAndReturn(BATCH_DELETE_TO_GC, request); request.setSelectAll(true); request.setExcludeIds(new ArrayList<>()); request.setApiDefinitionId("apiDefinitionId"); request.setModuleIds(List.of("case-moduleId")); - requestPostWithOkAndReturn(BATCH_MOVE_GC, request); + requestPostWithOkAndReturn(BATCH_DELETE_TO_GC, request); ApiTestCaseExample example = new ApiTestCaseExample(); example.createCriteria().andProjectIdEqualTo(DEFAULT_PROJECT_ID).andApiDefinitionIdEqualTo("apiDefinitionId").andDeletedEqualTo(true); List caseList = apiTestCaseMapper.selectByExample(example); @@ -957,11 +957,11 @@ public class ApiTestCaseControllerTests extends BaseTest { request.setSelectAll(true); request.setExcludeIds(new ArrayList<>()); request.setModuleIds(List.of("case-moduleId")); - requestPostWithOkAndReturn(BATCH_MOVE_GC, request); + requestPostWithOkAndReturn(BATCH_DELETE_TO_GC, request); //校验日志 checkLog(apiTestCase.getId(), OperationLogType.DELETE); //校验权限 - requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, BATCH_MOVE_GC, request); + requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, BATCH_DELETE_TO_GC, request); } @Test @@ -1053,7 +1053,7 @@ public class ApiTestCaseControllerTests extends BaseTest { gcRequest.setSelectAll(true); gcRequest.setExcludeIds(new ArrayList<>()); gcRequest.setApiDefinitionId("apiDefinitionId"); - requestPostWithOkAndReturn(BATCH_MOVE_GC, gcRequest); + requestPostWithOkAndReturn(BATCH_DELETE_TO_GC, gcRequest); ApiTestCaseExample example1 = new ApiTestCaseExample(); example1.createCriteria().andProjectIdEqualTo(DEFAULT_PROJECT_ID).andApiDefinitionIdEqualTo("apiDefinitionId").andDeletedEqualTo(true); List caseList1 = apiTestCaseMapper.selectByExample(example1); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/log/aspect/OperationLogAspect.java b/backend/services/system-setting/src/main/java/io/metersphere/system/log/aspect/OperationLogAspect.java index 0396ef992b..640c7fa726 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/log/aspect/OperationLogAspect.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/log/aspect/OperationLogAspect.java @@ -280,7 +280,13 @@ public class OperationLogAspect { public static String getPath() { HttpServletRequest httpRequest = getHttpRequest(); - String path = httpRequest == null ? StringUtils.EMPTY : httpRequest.getRequestURI(); + String path = StringUtils.EMPTY; + if (httpRequest != null) { + path = httpRequest.getRequestURI(); + if (StringUtils.isNotBlank(httpRequest.getQueryString())) { + path += "?" + httpRequest.getQueryString(); + } + } return path.length() > 255 ? path.substring(0, 255) : path; }