From 3aa5e50732bc4301a6e0ae0809acd5daddb0b198 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Fri, 2 Feb 2024 17:46:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):?= =?UTF-8?q?=20=E5=B0=86xpack=E7=9A=84=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E7=A7=BB=E5=88=B0=E5=BC=80=E6=BA=90=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definition/ApiDefinitionService.java | 6 +- .../definition/ApiTestCaseService.java | 7 +- .../definition/XpackApiDefinitionService.java | 20 -- .../ApiTestCaseControllerTests.java | 4 +- .../bug/service/BugHistoryService.java | 45 ++- .../bug/service/XpackBugService.java | 10 - .../bug/mock/XpackBugMockServiceImpl.java | 9 - .../service/FunctionalCaseService.java | 10 +- .../service/XpackFunctionalCaseService.java | 19 -- .../config/CaseTestConfiguration.java | 4 - .../FunctionalCaseControllerTests.java | 2 +- .../controller/FakeErrorController.java | 88 +++++ .../metersphere/project/dto/FakeErrorDTO.java | 48 +++ .../request/FakeErrorDelRequest.java | 22 ++ .../request/FakeErrorRequest.java | 21 ++ .../request/FakeErrorStatusRequest.java | 24 ++ .../response/FakeErrorResponse.java | 34 ++ .../project/mapper/ExtFakeErrorMapper.java | 13 + .../project/mapper/ExtFakeErrorMapper.xml | 14 + .../project/service/FakeErrorService.java | 315 ++++++++++++++++++ .../controller/FakeErrorControllerTests.java | 267 +++++++++++++++ .../mapper/BaseOperationHistoryMapper.java | 2 +- .../mapper/BaseOperationHistoryMapper.xml | 2 +- .../service/OperationHistoryService.java | 4 +- 24 files changed, 899 insertions(+), 91 deletions(-) delete mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/definition/XpackApiDefinitionService.java delete mode 100644 backend/services/case-management/src/main/java/io/metersphere/functional/service/XpackFunctionalCaseService.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/controller/FakeErrorController.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/dto/FakeErrorDTO.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorDelRequest.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorRequest.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorStatusRequest.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/response/FakeErrorResponse.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.xml create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/service/FakeErrorService.java create mode 100644 backend/services/project-management/src/test/java/io/metersphere/project/controller/FakeErrorControllerTests.java 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 a47e956047..4b306c31b2 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 @@ -923,11 +923,7 @@ public class ApiDefinitionService { } public List list(OperationHistoryRequest request) { - XpackApiDefinitionService xpackApiDefinitionService = CommonBeanFactory.getBean(XpackApiDefinitionService.class); - if (xpackApiDefinitionService != null) { - return xpackApiDefinitionService.listHis(request, API_TABLE); - } - return new ArrayList<>(); + return operationHistoryService.listWidthTable(request, API_TABLE); } /** 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 7fc945f9bc..9b7ceae3be 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 @@ -568,13 +568,8 @@ public class ApiTestCaseService { return executeList; } - public List operationHistoryList(OperationHistoryRequest request) { - XpackApiDefinitionService xpackApiDefinitionService = CommonBeanFactory.getBean(XpackApiDefinitionService.class); - if (xpackApiDefinitionService != null) { - return xpackApiDefinitionService.listHis(request, CASE_TABLE); - } - return new ArrayList<>(); + return operationHistoryService.listWidthTable(request, CASE_TABLE); } public void updatePriority(String id, String priority, String userId) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/XpackApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/XpackApiDefinitionService.java deleted file mode 100644 index b24eaf813c..0000000000 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/XpackApiDefinitionService.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.metersphere.api.service.definition; - -import io.metersphere.system.dto.OperationHistoryDTO; -import io.metersphere.system.dto.request.OperationHistoryRequest; - -import java.util.List; - -/** - * 接口管理Xpack功能接口 - */ -public interface XpackApiDefinitionService { - - /** - * 功能用例变更历史分页列表 - * - * @param request 请求参数 - * @return 变更历史集合 - */ - List listHis(OperationHistoryRequest request, String table); -} 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 0003896644..cbb1174cf8 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 @@ -841,8 +841,8 @@ public class ApiTestCaseControllerTests extends BaseTest { List reportDTOS = JSON.parseArray(JSON.toJSONString(returnPager.getList()), OperationHistoryDTO.class); reportDTOS.forEach(reportDTO -> Assertions.assertEquals(reportDTO.getSourceId(), first.getId())); - List operationHistoryDTOS = operationHistoryService.listWidthLimit(request, "api_test_case"); - Assertions.assertTrue(org.apache.commons.collections4.CollectionUtils.isNotEmpty(operationHistoryDTOS)); + List operationHistoryDTOS = operationHistoryService.listWidthTable(request, "api_test_case"); + Assertions.assertTrue(CollectionUtils.isNotEmpty(operationHistoryDTOS)); request = new OperationHistoryRequest(); request.setProjectId(DEFAULT_PROJECT_ID); diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugHistoryService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugHistoryService.java index c111c51974..cd5a12dffb 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugHistoryService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugHistoryService.java @@ -1,22 +1,57 @@ package io.metersphere.bug.service; -import io.metersphere.sdk.util.CommonBeanFactory; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.system.domain.OperationHistory; +import io.metersphere.system.domain.OperationHistoryExample; +import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserExample; import io.metersphere.system.dto.OperationHistoryDTO; import io.metersphere.system.dto.request.OperationHistoryRequest; +import io.metersphere.system.log.constants.OperationLogModule; +import io.metersphere.system.mapper.OperationHistoryMapper; +import io.metersphere.system.mapper.UserMapper; +import jakarta.annotation.Resource; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) public class BugHistoryService { + @Resource + private UserMapper userMapper; + + @Resource + private OperationHistoryMapper operationHistoryMapper; + + /** + * 缺陷变更历史分页列表 + * @param request 请求参数 + * @return 变更历史集合 + */ public List list(OperationHistoryRequest request) { - XpackBugService bugService = CommonBeanFactory.getBean(XpackBugService.class); - if (bugService != null) { - return bugService.listHis(request); + OperationHistoryExample example = new OperationHistoryExample(); + example.createCriteria().andProjectIdEqualTo(request.getProjectId()).andModuleEqualTo(OperationLogModule.BUG_MANAGEMENT) + .andSourceIdEqualTo(request.getSourceId()); + List history = operationHistoryMapper.selectByExample(example); + if (CollectionUtils.isEmpty(history)) { + return List.of(); } - return List.of(); + List userIds = history.stream().map(OperationHistory::getCreateUser).toList(); + UserExample userExample = new UserExample(); + userExample.createCriteria().andIdIn(userIds); + List users = userMapper.selectByExample(userExample); + Map userMap = users.stream().collect(Collectors.toMap(User::getId, User::getName)); + return history.stream().map(h -> { + OperationHistoryDTO dto = new OperationHistoryDTO(); + BeanUtils.copyBean(dto, h); + dto.setCreateUserName(userMap.get(h.getCreateUser()) == null ? h.getCreateUser() : userMap.get(h.getCreateUser())); + return dto; + }).toList(); } } diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/XpackBugService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/XpackBugService.java index 9bdec14ac5..7323f5461c 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/XpackBugService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/XpackBugService.java @@ -2,10 +2,6 @@ package io.metersphere.bug.service; import io.metersphere.bug.dto.request.BugSyncRequest; import io.metersphere.project.domain.Project; -import io.metersphere.system.dto.OperationHistoryDTO; -import io.metersphere.system.dto.request.OperationHistoryRequest; - -import java.util.List; /** * 缺陷相关xpack功能接口 @@ -27,10 +23,4 @@ public interface XpackBugService { */ void syncPlatformBugs(Project project, BugSyncRequest request, String currentUser); - /** - * 缺陷变更历史分页列表 - * @param request 请求参数 - * @return 变更历史集合 - */ - List listHis(OperationHistoryRequest request); } diff --git a/backend/services/bug-management/src/test/java/io/metersphere/bug/mock/XpackBugMockServiceImpl.java b/backend/services/bug-management/src/test/java/io/metersphere/bug/mock/XpackBugMockServiceImpl.java index 06c3370771..bf87497a0b 100644 --- a/backend/services/bug-management/src/test/java/io/metersphere/bug/mock/XpackBugMockServiceImpl.java +++ b/backend/services/bug-management/src/test/java/io/metersphere/bug/mock/XpackBugMockServiceImpl.java @@ -3,12 +3,8 @@ package io.metersphere.bug.mock; import io.metersphere.bug.dto.request.BugSyncRequest; import io.metersphere.bug.service.XpackBugService; import io.metersphere.project.domain.Project; -import io.metersphere.system.dto.OperationHistoryDTO; -import io.metersphere.system.dto.request.OperationHistoryRequest; import org.springframework.stereotype.Service; -import java.util.List; - @Service public class XpackBugMockServiceImpl implements XpackBugService { @@ -21,9 +17,4 @@ public class XpackBugMockServiceImpl implements XpackBugService { public void syncPlatformBugs(Project project, BugSyncRequest request, String currentUser) { } - - @Override - public List listHis(OperationHistoryRequest request) { - return List.of(); - } } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java index b6999912c4..222f6f1e7d 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java @@ -28,7 +28,6 @@ import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.TemplateScene; import io.metersphere.sdk.exception.MSException; 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.domain.CustomFieldOption; @@ -44,6 +43,7 @@ import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.notice.sender.AfterReturningNoticeSendService; import io.metersphere.system.service.BaseCustomFieldOptionService; import io.metersphere.system.service.BaseCustomFieldService; +import io.metersphere.system.service.OperationHistoryService; import io.metersphere.system.service.UserLoginService; import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.NumGenerator; @@ -151,6 +151,8 @@ public class FunctionalCaseService { private FileAssociationMapper fileAssociationMapper; @Resource private AfterReturningNoticeSendService afterReturningNoticeSendService; + @Resource + private OperationHistoryService operationHistoryService; public FunctionalCase addFunctionalCase(FunctionalCaseAddRequest request, List files, String userId, String organizationId) { @@ -1110,10 +1112,6 @@ public class FunctionalCaseService { } public List operationHistoryList(OperationHistoryRequest request) { - XpackFunctionalCaseService functionalCaseService = CommonBeanFactory.getBean(XpackFunctionalCaseService.class); - if (functionalCaseService != null) { - return functionalCaseService.listHis(request, CASE_TABLE); - } - return List.of(); + return operationHistoryService.listWidthTable(request, CASE_TABLE); } } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/XpackFunctionalCaseService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/XpackFunctionalCaseService.java deleted file mode 100644 index 47052da5d4..0000000000 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/XpackFunctionalCaseService.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.metersphere.functional.service; - -import io.metersphere.system.dto.OperationHistoryDTO; -import io.metersphere.system.dto.request.OperationHistoryRequest; - -import java.util.List; - -/** - * 用例管理Xpack功能接口 - */ -public interface XpackFunctionalCaseService { - - /** - * 功能用例变更历史分页列表 - * @param request 请求参数 - * @return 变更历史集合 - */ - List listHis(OperationHistoryRequest request,String table); -} diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/config/CaseTestConfiguration.java b/backend/services/case-management/src/test/java/io/metersphere/functional/config/CaseTestConfiguration.java index 4b2b022bf3..9763a92dd6 100644 --- a/backend/services/case-management/src/test/java/io/metersphere/functional/config/CaseTestConfiguration.java +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/config/CaseTestConfiguration.java @@ -1,6 +1,5 @@ package io.metersphere.functional.config; -import io.metersphere.functional.service.XpackFunctionalCaseService; import io.metersphere.provider.BaseAssociateApiProvider; import io.metersphere.provider.BaseAssociateBugProvider; import io.metersphere.provider.BaseAssociateScenarioProvider; @@ -19,7 +18,4 @@ public class CaseTestConfiguration { @MockBean BaseAssociateBugProvider baseAssociateBugProvider; - @MockBean - XpackFunctionalCaseService xpackFunctionalCaseService; - } diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java index 2c73f0cc87..abd2b0db8c 100644 --- a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java @@ -667,7 +667,7 @@ public class FunctionalCaseControllerTests extends BaseTest { Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= request.getPageSize()); request.setSort(Map.of()); this.requestPost(OPERATION_HISTORY_URL, request); - List operationHistoryDTOS = operationHistoryService.listWidthLimit(request, "functional_case"); + List operationHistoryDTOS = operationHistoryService.listWidthTable(request, "functional_case"); Assertions.assertTrue(CollectionUtils.isNotEmpty(operationHistoryDTOS)); } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/FakeErrorController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/FakeErrorController.java new file mode 100644 index 0000000000..9b7eb50ff8 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/FakeErrorController.java @@ -0,0 +1,88 @@ +package io.metersphere.project.controller; + +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; +import io.metersphere.project.domain.FakeError; +import io.metersphere.project.dto.FakeErrorDTO; +import io.metersphere.project.dto.filemanagement.request.FakeErrorDelRequest; +import io.metersphere.project.dto.filemanagement.request.FakeErrorRequest; +import io.metersphere.project.dto.filemanagement.request.FakeErrorStatusRequest; +import io.metersphere.project.service.FakeErrorService; +import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.system.controller.handler.ResultHolder; +import io.metersphere.system.log.annotation.Log; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.utils.PageUtils; +import io.metersphere.system.utils.Pager; +import io.metersphere.system.utils.SessionUtils; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + + +@Tag(name = "项目管理-项目与权限-接口测试-误报规则配置") +@RestController +@RequestMapping(value = "/fake/error") +public class FakeErrorController { + + @Resource + private FakeErrorService fakeErrorService; + + + @PostMapping("/add") + @Operation(summary = "项目与权限-接口测试-新增误报规则") + @RequiresPermissions(PermissionConstants.PROJECT_APPLICATION_API_UPDATE) + public ResultHolder add(@Validated @RequestBody List dto) { + String userId = SessionUtils.getUserId(); + return fakeErrorService.save(dto, userId); + } + + + @PostMapping("/list") + @Operation(summary = "项目与权限-接口测试-获取误报规则列表") + @RequiresPermissions(PermissionConstants.PROJECT_APPLICATION_API_READ) + public Pager> list(@Validated @RequestBody FakeErrorRequest request) { + Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), + StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc"); + return PageUtils.setPageInfo(page, fakeErrorService.list(request)); + } + + + @PostMapping("/update") + @Operation(summary = "项目与权限-接口测试-编辑误报规则") + @RequiresPermissions(PermissionConstants.PROJECT_APPLICATION_API_UPDATE) + public ResultHolder update(@Validated @RequestBody List requests) { + String userId = SessionUtils.getUserId(); + return fakeErrorService.update(requests, userId); + } + + + @PostMapping("/delete") + @Operation(summary = "应用设置-接口测试-删除误报规则") + @RequiresPermissions(PermissionConstants.PROJECT_APPLICATION_API_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.deleteLog(#request)", msClass = FakeErrorService.class) + public void delete(@Validated @RequestBody FakeErrorDelRequest request) { + fakeErrorService.delete(request); + } + + + @PostMapping("/update/enable") + @Operation(summary = "项目与权限-接口测试-启用/禁用误报规则") + @RequiresPermissions(PermissionConstants.PROJECT_APPLICATION_API_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateEnableLog(#request)", msClass = FakeErrorService.class) + public void updateEnable(@Validated @RequestBody FakeErrorStatusRequest request) { + String userId = SessionUtils.getUserId(); + fakeErrorService.updateEnable(request, userId); + } + + +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/FakeErrorDTO.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/FakeErrorDTO.java new file mode 100644 index 0000000000..5c076ff7fd --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/FakeErrorDTO.java @@ -0,0 +1,48 @@ +package io.metersphere.project.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author wx + */ +@Data +public class FakeErrorDTO implements Serializable { + + private static final long serialVersionUID = 1L; + + @Schema(description = "ID") + private String id; + + @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.project_id.not_blank}") + private String projectId; + + @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.name.not_blank}") + private String name; + + @Schema(description = "标签", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.type.not_blank}") + private String type; + + @Schema(description = "匹配规则-内容类型/header/data/body", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.resp_type.not_blank}") + private String respType; + + @Schema(description = "匹配规则-操作类型", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.relation.not_blank}") + private String relation; + + @Schema(description = "匹配规则-表达式", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.expression.not_blank}") + private String expression; + + @Schema(description = "状态") + private Boolean enable; + + +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorDelRequest.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorDelRequest.java new file mode 100644 index 0000000000..1206700a08 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorDelRequest.java @@ -0,0 +1,22 @@ +package io.metersphere.project.dto.filemanagement.request; + +import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author wx + */ + +@Data +public class FakeErrorDelRequest extends TableBatchProcessDTO implements Serializable { + private static final long serialVersionUID = 1L; + + + @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.project_id.not_blank}") + private String projectId; +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorRequest.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorRequest.java new file mode 100644 index 0000000000..53465236a9 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorRequest.java @@ -0,0 +1,21 @@ +package io.metersphere.project.dto.filemanagement.request; + + +import io.metersphere.system.dto.sdk.BasePageRequest; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.Getter; +import lombok.Setter; + +/** + * @author wx + */ +@Getter +@Setter +public class FakeErrorRequest extends BasePageRequest { + + @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.project_id.not_blank}") + private String projectId; + +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorStatusRequest.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorStatusRequest.java new file mode 100644 index 0000000000..1ec1dacf8f --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/request/FakeErrorStatusRequest.java @@ -0,0 +1,24 @@ +package io.metersphere.project.dto.filemanagement.request; + +import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author wx + */ +@Data +public class FakeErrorStatusRequest extends TableBatchProcessDTO implements Serializable { + + private static final long serialVersionUID = 1L; + + @Schema(description = "是否禁用") + private Boolean enable; + + @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{fake_error.project_id.not_blank}") + private String projectId; +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/response/FakeErrorResponse.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/response/FakeErrorResponse.java new file mode 100644 index 0000000000..be4b1de490 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/filemanagement/response/FakeErrorResponse.java @@ -0,0 +1,34 @@ +package io.metersphere.project.dto.filemanagement.response; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serializable; + +@Data +public class FakeErrorResponse implements Serializable { + @Schema(description = "ID") + private String id; + + @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) + private String projectId; + + @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED) + private String name; + + @Schema(description = "标签", requiredMode = Schema.RequiredMode.REQUIRED) + private String type; + + @Schema(description = "匹配规则-内容类型/header/data/body", requiredMode = Schema.RequiredMode.REQUIRED) + private String respType; + + @Schema(description = "匹配规则-操作类型", requiredMode = Schema.RequiredMode.REQUIRED) + private String relation; + + @Schema(description = "匹配规则-表达式", requiredMode = Schema.RequiredMode.REQUIRED) + private String expression; + + @Schema(description = "状态") + private Boolean enable; + +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.java b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.java new file mode 100644 index 0000000000..dc2fe0626e --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.java @@ -0,0 +1,13 @@ +package io.metersphere.project.mapper; + +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author wx + */ +public interface ExtFakeErrorMapper { + + List selectByKeyword(@Param("keyword") String keyword); +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.xml b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.xml new file mode 100644 index 0000000000..6a1aa3eed5 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtFakeErrorMapper.xml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/FakeErrorService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/FakeErrorService.java new file mode 100644 index 0000000000..385a852f3a --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/FakeErrorService.java @@ -0,0 +1,315 @@ +package io.metersphere.project.service; + +import io.metersphere.project.domain.FakeError; +import io.metersphere.project.domain.FakeErrorExample; +import io.metersphere.project.domain.Project; +import io.metersphere.project.dto.FakeErrorDTO; +import io.metersphere.project.dto.filemanagement.request.FakeErrorDelRequest; +import io.metersphere.project.dto.filemanagement.request.FakeErrorRequest; +import io.metersphere.project.dto.filemanagement.request.FakeErrorStatusRequest; +import io.metersphere.project.dto.filemanagement.response.FakeErrorResponse; +import io.metersphere.project.mapper.ExtFakeErrorMapper; +import io.metersphere.project.mapper.FakeErrorMapper; +import io.metersphere.project.mapper.ProjectMapper; +import io.metersphere.sdk.constants.HttpMethodConstants; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.controller.handler.ResultHolder; +import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.metersphere.system.log.constants.OperationLogModule; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.dto.LogDTO; +import io.metersphere.system.log.service.OperationLogService; +import io.metersphere.system.uid.IDGenerator; +import jakarta.annotation.Resource; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +@Transactional(rollbackFor = Exception.class) +public class FakeErrorService { + + @Resource + private FakeErrorMapper fakeErrorMapper; + @Resource + private ProjectMapper projectMapper; + + @Resource + private OperationLogService operationLogService; + + @Resource + private ExtFakeErrorMapper extFakeErrorMapper; + + private static final String ADD_TYPE = "ADD"; + + private static final String UPDATE_TYPE = "UPDATE"; + + + /** + * 保存误报规则 + * + * @param dto + * @return + */ + public ResultHolder save(List dto, String userId) { + List responseList = checkNameRepeat(dto, ADD_TYPE); + if (CollectionUtils.isNotEmpty(responseList)) { + return ResultHolder.success(responseList); + } + doSave(dto, userId); + return ResultHolder.success(responseList); + } + + private void doSave(List dto, String userId) { + dto.forEach(item -> { + FakeError fakeError = new FakeError(); + BeanUtils.copyBean(fakeError, item); + fakeError.setId(IDGenerator.nextStr()); + fakeError.setCreateTime(System.currentTimeMillis()); + fakeError.setUpdateTime(System.currentTimeMillis()); + fakeError.setCreateUser(userId); + fakeError.setUpdateUser(userId); + fakeErrorMapper.insertSelective(fakeError); + doSaveLog(fakeError, userId); + }); + } + + + private List checkNameRepeat(List dto, String type) { + List responseList = new ArrayList<>(); + Map nameMap = new HashMap<>(); + dto.forEach(item -> { + if (nameMap.containsKey(item.getName())) { + addRepeatResponse(responseList, item); + return; + } + nameMap.put(item.getName(), item); + + FakeErrorExample example = new FakeErrorExample(); + FakeErrorExample.Criteria criteria = example.createCriteria(); + criteria.andProjectIdEqualTo(item.getProjectId()).andNameEqualTo(item.getName()); + if (StringUtils.isNotBlank(item.getId()) && StringUtils.equalsIgnoreCase(type,UPDATE_TYPE)) { + criteria.andIdNotEqualTo(item.getId()); + } + if (fakeErrorMapper.countByExample(example) > 0) { + addRepeatResponse(responseList, item); + } + }); + return responseList; + } + + private void addRepeatResponse(List responseList, FakeErrorDTO item) { + FakeErrorResponse fakeErrorResponse = BeanUtils.copyBean(new FakeErrorResponse(), item); + responseList.add(fakeErrorResponse); + } + + + /** + * 获取误报列表 + * + * @param request + * @return + */ + public List list(FakeErrorRequest request) { + FakeErrorExample example = new FakeErrorExample(); + FakeErrorExample.Criteria criteria = example.createCriteria(); + criteria.andProjectIdEqualTo(request.getProjectId()); + if (StringUtils.isNotBlank(request.getKeyword())) { + criteria.andNameLike("%" + request.getKeyword() + "%"); + } + return fakeErrorMapper.selectByExample(example); + } + + + /** + * 更新误报规则 + * + * @param dto + * @param userId + * @return + */ + public ResultHolder update(List dto, String userId) { + List responseList = checkNameRepeat(dto, UPDATE_TYPE); + if (CollectionUtils.isNotEmpty(responseList)) { + return ResultHolder.success(responseList); + } + doUpdate(dto, userId); + return ResultHolder.success(responseList); + } + + private void doUpdate(List dto, String userId) { + dto.forEach(item -> { + FakeError fakeError = new FakeError(); + BeanUtils.copyBean(fakeError, item); + fakeError.setUpdateUser(userId); + fakeError.setUpdateTime(System.currentTimeMillis()); + fakeErrorMapper.updateByPrimaryKeySelective(fakeError); + updateLog(fakeError, userId); + }); + } + + + /** + * 删除误报规则 + * + * @param request + * @return + */ + public void delete(FakeErrorDelRequest request) { + List ids = doSelectIds(request); + if (CollectionUtils.isEmpty(ids)) { + return; + } + FakeErrorExample example = new FakeErrorExample(); + example.createCriteria().andIdIn(ids); + fakeErrorMapper.deleteByExample(example); + } + + + /** + * 保存日志 + * + * @param fakeError + * @return + */ + private void doSaveLog(FakeError fakeError, String userId) { + Project project = projectMapper.selectByPrimaryKey(fakeError.getProjectId()); + LogDTO dto = new LogDTO( + fakeError.getProjectId(), + project.getOrganizationId(), + fakeError.getId(), + userId, + OperationLogType.ADD.name(), + OperationLogModule.PROJECT_PROJECT_MANAGER, + fakeError.getName()); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setPath("/fake/error/add"); + dto.setModifiedValue(JSON.toJSONBytes(fakeError)); + operationLogService.add(dto); + } + + + /** + * 更新日志 + * + * @param fakeError + * @return + */ + public void updateLog(FakeError fakeError, String userId) { + Project project = projectMapper.selectByPrimaryKey(fakeError.getProjectId()); + FakeError originalValue = fakeErrorMapper.selectByPrimaryKey(fakeError.getId()); + LogDTO dto = new LogDTO( + fakeError.getProjectId(), + project.getOrganizationId(), + fakeError.getId(), + userId, + OperationLogType.UPDATE.name(), + OperationLogModule.PROJECT_PROJECT_MANAGER, + fakeError.getName()); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setPath("/fake/error/update"); + dto.setOriginalValue(JSON.toJSONBytes(fakeError)); + dto.setOriginalValue(JSON.toJSONBytes(originalValue)); + operationLogService.add(dto); + } + + + /** + * 删除日志 + * + * @param request + * @return + */ + public List deleteLog(FakeErrorDelRequest request) { + List ids = doSelectIds(request); + if (CollectionUtils.isEmpty(ids)) { + return new ArrayList<>(); + } + Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); + FakeErrorExample example = new FakeErrorExample(); + example.createCriteria().andIdIn(ids).andProjectIdEqualTo(request.getProjectId()); + List fakeErrors = fakeErrorMapper.selectByExample(example); + List logs = new ArrayList<>(); + fakeErrors.forEach(fakeError -> { + LogDTO dto = new LogDTO( + fakeError.getProjectId(), + project.getOrganizationId(), + fakeError.getId(), + null, + OperationLogType.UPDATE.name(), + OperationLogModule.PROJECT_PROJECT_MANAGER, + fakeError.getName()); + dto.setOriginalValue(JSON.toJSONBytes(fakeError)); + logs.add(dto); + }); + return logs; + } + + + /** + * 更新状态 + * + * @param request + * @param userId + * @return + */ + public void updateEnable(FakeErrorStatusRequest request, String userId) { + List ids = doSelectIds(request); + if (CollectionUtils.isEmpty(ids)) { + return; + } + FakeErrorExample example = new FakeErrorExample(); + example.createCriteria().andIdIn(ids).andProjectIdEqualTo(request.getProjectId()); + FakeError fakeError = new FakeError(); + fakeError.setEnable(request.getEnable()); + fakeError.setUpdateUser(userId); + fakeError.setUpdateTime(System.currentTimeMillis()); + fakeErrorMapper.updateByExampleSelective(fakeError, example); + } + + private List doSelectIds(T dto) { + TableBatchProcessDTO request = (TableBatchProcessDTO) dto; + if (request.isSelectAll()) { + List ids = extFakeErrorMapper.selectByKeyword(request.getCondition().getKeyword()); + if (CollectionUtils.isNotEmpty(request.getExcludeIds())) { + ids.removeAll(request.getExcludeIds()); + } + return ids; + } else { + return request.getSelectIds(); + } + } + + + public List batchUpdateEnableLog(FakeErrorStatusRequest request) { + List ids = doSelectIds(request); + if (CollectionUtils.isEmpty(ids)) { + return new ArrayList<>(); + } + Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); + FakeErrorExample example = new FakeErrorExample(); + example.createCriteria().andIdIn(ids).andProjectIdEqualTo(request.getProjectId()); + List fakeErrors = fakeErrorMapper.selectByExample(example); + List logs = new ArrayList<>(); + fakeErrors.forEach(fakeError -> { + LogDTO dto = new LogDTO( + fakeError.getProjectId(), + project.getOrganizationId(), + fakeError.getId(), + null, + OperationLogType.UPDATE.name(), + OperationLogModule.PROJECT_PROJECT_MANAGER, + fakeError.getName()); + dto.setOriginalValue(JSON.toJSONBytes(fakeError)); + logs.add(dto); + }); + return logs; + } +} diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/FakeErrorControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/FakeErrorControllerTests.java new file mode 100644 index 0000000000..769d5accfe --- /dev/null +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/FakeErrorControllerTests.java @@ -0,0 +1,267 @@ +package io.metersphere.project.controller; + +import io.metersphere.project.domain.FakeError; +import io.metersphere.project.domain.FakeErrorExample; +import io.metersphere.project.dto.FakeErrorDTO; +import io.metersphere.project.dto.filemanagement.request.FakeErrorDelRequest; +import io.metersphere.project.dto.filemanagement.request.FakeErrorRequest; +import io.metersphere.project.dto.filemanagement.request.FakeErrorStatusRequest; +import io.metersphere.project.mapper.FakeErrorMapper; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.base.BaseTest; +import io.metersphere.system.controller.handler.ResultHolder; +import io.metersphere.system.utils.Pager; +import jakarta.annotation.Resource; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.*; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MvcResult; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@AutoConfigureMockMvc +public class FakeErrorControllerTests extends BaseTest { + + @Resource + private FakeErrorMapper fakeErrorMapper; + + private static final String prefix = "/fake/error"; + private static final String ADD = prefix + "/add"; + private static final String UPDATE = prefix + "/update"; + private static final String UPDATE_ENABLE = UPDATE + "/enable"; + private static final String DELETE = prefix + "/delete"; + private static final String LIST = prefix + "/list"; + + @Test + @Order(1) + public void addFakeErrorRuleSuccess() throws Exception { + List dtoList = new ArrayList<>(); + FakeErrorDTO dto = new FakeErrorDTO(); + dto.setName("测试新增误报规则"); + dto.setExpression("ok"); + dto.setProjectId(DEFAULT_PROJECT_ID); + dto.setRelation("equal"); + dto.setType("dd"); + dto.setRespType("data"); + dtoList.add(dto); + MvcResult mvcResult = this.requestPostWithOkAndReturn(ADD, dtoList); + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + List fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + Assertions.assertEquals(1,fakeErrors.size()); + FakeErrorDTO dto1 = new FakeErrorDTO(); + dto1.setName("测试新增误报规则"); + dto1.setExpression("ok"); + dto1.setProjectId(DEFAULT_PROJECT_ID); + dto1.setRelation("equal"); + dto1.setType("dd"); + dto1.setRespType("headers"); + dtoList.add(dto1); + mvcResult = this.requestPostWithOkAndReturn(ADD, dtoList); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + //名字重复不添加 + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + Assertions.assertEquals(1,fakeErrors.size()); + } + + @Test + @Order(2) + public void getListFakeErrorRuleSuccess() throws Exception { + FakeErrorRequest request = new FakeErrorRequest(); + request.setProjectId(DEFAULT_PROJECT_ID); + request.setCurrent(1); + request.setPageSize(10); + MvcResult mvcResult = this.requestPostWithOkAndReturn(LIST, request); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), request.getCurrent()); + // 返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= request.getPageSize()); + + request.setKeyword("测试"); + mvcResult = this.requestPostWithOkAndReturn(LIST, request); + // 获取返回值 + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } + + @Test + @Order(3) + public void updateFakeErrorRuleSuccess() throws Exception { + List fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + List dtoList = new ArrayList<>(); + FakeErrorDTO dto = new FakeErrorDTO(); + dto.setId(fakeErrors.get(0).getId()); + dto.setName("测试新增误报规则更新"); + dto.setExpression("ok"); + dto.setProjectId(DEFAULT_PROJECT_ID); + dto.setRelation("equal"); + dto.setType("dd"); + dto.setRespType("data"); + dto.setEnable(true); + dtoList.add(dto); + MvcResult mvcResult = this.requestPostWithOkAndReturn(UPDATE, dtoList); + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + Assertions.assertNotNull(resultHolder); + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + Assertions.assertTrue(fakeErrors.get(0).getEnable()); + + mvcResult = this.requestPostWithOkAndReturn(ADD, dtoList); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + Assertions.assertEquals(1, fakeErrors.size()); + + FakeErrorDTO dtoTwo = new FakeErrorDTO(); + dtoTwo.setName("测试新增误报规则更新2"); + dtoTwo.setExpression("ok"); + dtoTwo.setProjectId(DEFAULT_PROJECT_ID); + dtoTwo.setRelation("equal"); + dtoTwo.setType("dd"); + dtoTwo.setRespType("data"); + dtoTwo.setEnable(true); + List dtoListTwo = new ArrayList<>(); + + dtoListTwo.add(dtoTwo); + mvcResult = this.requestPostWithOkAndReturn(ADD, dtoListTwo); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + Assertions.assertEquals(2, fakeErrors.size()); + + List updateList = new ArrayList<>(); + for (FakeError fakeError : fakeErrors) { + FakeErrorDTO fakeErrorDTO = new FakeErrorDTO(); + BeanUtils.copyBean(fakeErrorDTO,fakeError); + fakeErrorDTO.setName("更新同名"); + updateList.add(fakeErrorDTO); + } + mvcResult = this.requestPostWithOkAndReturn(UPDATE, updateList); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + //名字重复不更新 + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + Assertions.assertFalse(StringUtils.equalsIgnoreCase(fakeErrors.get(0).getName(),fakeErrors.get(1).getName())); + } + + @Test + @Order(4) + public void updateEnableFakeErrorRuleSuccess() throws Exception { + List fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + List ids = fakeErrors.stream().map(FakeError::getId).toList(); + FakeErrorStatusRequest fakeErrorStatusRequest = new FakeErrorStatusRequest(); + fakeErrorStatusRequest.setProjectId(DEFAULT_PROJECT_ID); + fakeErrorStatusRequest.setEnable(false); + fakeErrorStatusRequest.setSelectAll(true); + MvcResult mvcResult = this.requestPostWithOkAndReturn(UPDATE_ENABLE, fakeErrorStatusRequest); + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + //名字重复不更新 + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + List list = fakeErrors.stream().filter(FakeError::getEnable).toList(); + Assertions.assertEquals(0,list.size()); + fakeErrorStatusRequest = new FakeErrorStatusRequest(); + fakeErrorStatusRequest.setProjectId(DEFAULT_PROJECT_ID); + fakeErrorStatusRequest.setEnable(true); + fakeErrorStatusRequest.setSelectAll(false); + mvcResult = this.requestPostWithOkAndReturn(UPDATE_ENABLE, fakeErrorStatusRequest); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + list = fakeErrors.stream().filter(FakeError::getEnable).toList(); + Assertions.assertEquals(0,list.size()); + String oneId = ids.get(0); + fakeErrorStatusRequest = new FakeErrorStatusRequest(); + fakeErrorStatusRequest.setProjectId(DEFAULT_PROJECT_ID); + fakeErrorStatusRequest.setEnable(true); + fakeErrorStatusRequest.setSelectAll(false); + fakeErrorStatusRequest.setSelectIds(List.of(oneId)); + mvcResult = this.requestPostWithOkAndReturn(UPDATE_ENABLE, fakeErrorStatusRequest); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + fakeErrors = fakeErrorMapper.selectByExample(new FakeErrorExample()); + list = fakeErrors.stream().filter(FakeError::getEnable).toList(); + Assertions.assertEquals(1,list.size()); + + } + + @Test + @Order(5) + public void deleteFakeErrorRuleSuccess() throws Exception { + List dtoList = new ArrayList<>(); + FakeErrorDTO dto = new FakeErrorDTO(); + dto.setName("用来删除"); + dto.setExpression("ok"); + dto.setProjectId(DEFAULT_PROJECT_ID); + dto.setRelation("equal"); + dto.setType("dd"); + dto.setRespType("data"); + dtoList.add(dto); + MvcResult mvcResult = this.requestPostWithOkAndReturn(ADD, dtoList); + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + Assertions.assertNotNull(resultHolder); + FakeErrorExample fakeErrorExample = new FakeErrorExample(); + fakeErrorExample.createCriteria().andNameLike("用来删除"); + List fakeErrors = fakeErrorMapper.selectByExample(fakeErrorExample); + String deleteId = fakeErrors.get(0).getId(); + Assertions.assertEquals(1,fakeErrors.size()); + FakeErrorDelRequest request = new FakeErrorDelRequest(); + request.setProjectId(DEFAULT_PROJECT_ID); + request.setSelectAll(false); + mvcResult = this.requestPostWithOkAndReturn(DELETE, request); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + Assertions.assertNotNull(resultHolder); + request.setSelectAll(true); + fakeErrorExample = new FakeErrorExample(); + fakeErrorExample.createCriteria().andNameLike("测试"); + fakeErrors = fakeErrorMapper.selectByExample(fakeErrorExample); + List ids = fakeErrors.stream().map(FakeError::getId).toList(); + request.setExcludeIds(ids); + mvcResult = this.requestPostWithOkAndReturn(DELETE, request); + returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(returnData, ResultHolder.class); + Assertions.assertNotNull(resultHolder); + FakeError fakeError = fakeErrorMapper.selectByPrimaryKey(deleteId); + Assertions.assertNull(fakeError); + + } + + + + +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.java index 91f5259eb9..d7e8f38b0c 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.java @@ -17,5 +17,5 @@ public interface BaseOperationHistoryMapper { List list(@Param("request") OperationHistoryRequest request); - List listWidthLimit(@Param("request") OperationHistoryRequest request, @Param("table") String table); + List listWidthTable(@Param("request") OperationHistoryRequest request, @Param("table") String table); } \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.xml index 7d30d5317e..9c1b269c97 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/BaseOperationHistoryMapper.xml @@ -41,7 +41,7 @@ - SELECT operation_history.id, operation_history.project_id, operation_history.create_time, operation_history.create_user, operation_history.source_id, operation_history.`type`, operation_history.`module`, operation_history.ref_id, project_version.name as versionName FROM operation_history LEFT JOIN ${table} sourceTable ON sourceTable.id = operation_history.source_id diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OperationHistoryService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OperationHistoryService.java index c88f5a5eac..39e07485a2 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OperationHistoryService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OperationHistoryService.java @@ -47,8 +47,8 @@ public class OperationHistoryService { } - public List listWidthLimit(OperationHistoryRequest request, String table) { - List list = baseOperationHistoryMapper.listWidthLimit(request, table); + public List listWidthTable(OperationHistoryRequest request, String table) { + List list = baseOperationHistoryMapper.listWidthTable(request, table); if (CollectionUtils.isNotEmpty(list)) { List userIds = list.stream().distinct() .map(OperationHistoryDTO::getCreateUser).toList();