From 11575a4169675e75cab003de8bc15a75a98770e3 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 24 Aug 2023 15:56:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=9A=84=E6=A0=91=E7=BB=93=E6=9E=84=E6=95=B0=E6=8D=AE=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/sdk/dto/BaseTreeNode.java | 40 ++++ .../system/controller/UserController.java | 7 +- .../response/user/UserTreeSelectOption.java | 18 -- .../GlobalUserRoleRelationLogService.java | 4 +- .../GlobalUserRoleRelationService.java | 4 +- .../system/service/UserLogService.java | 17 +- .../system/service/UserService.java | 52 ++-- .../system/service/UserToolService.java | 65 +++++ .../system/utils/TreeNodeParseUtils.java | 16 +- .../controller/user/UserControllerTests.java | 222 +++++++++--------- .../system/utils/user/UserRequestUtils.java | 2 + 11 files changed, 266 insertions(+), 181 deletions(-) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseTreeNode.java delete mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/UserToolService.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseTreeNode.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseTreeNode.java new file mode 100644 index 0000000000..faa89b35d6 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BaseTreeNode.java @@ -0,0 +1,40 @@ +package io.metersphere.sdk.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Data +@NoArgsConstructor +public class BaseTreeNode { + + @Schema(description = "节点ID") + private String id; + + @Schema(description = "节点名称") + private String name; + + @Schema(description = "节点类型") + private String type; + + @Schema(description = "是否是叶子节点") + private boolean leafNode; + + @Schema(description = "子节点") + private List children = new ArrayList<>(); + + public BaseTreeNode(String id, String name, String type, boolean isLeafNode) { + this.id = id; + this.name = name; + this.type = type; + this.leafNode = isLeafNode; + } + + public void addChild(BaseTreeNode node) { + this.leafNode = false; + children.add(node); + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java index b3f183393e..58298c924a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java @@ -22,7 +22,6 @@ import io.metersphere.system.request.user.UserRoleBatchRelationRequest; import io.metersphere.system.response.user.UserImportResponse; import io.metersphere.system.response.user.UserSelectOption; import io.metersphere.system.response.user.UserTableResponse; -import io.metersphere.system.response.user.UserTreeSelectOption; import io.metersphere.system.service.*; import io.metersphere.system.utils.TreeNodeParseUtils; import io.metersphere.validation.groups.Created; @@ -45,6 +44,8 @@ public class UserController { @Resource private UserService userService; @Resource + private UserToolService userToolService; + @Resource private GlobalUserRoleService globalUserRoleService; @Resource private GlobalUserRoleRelationService globalUserRoleRelationService; @@ -134,7 +135,7 @@ public class UserController { @GetMapping("/get/project") @Operation(summary = "用户批量操作-查找项目") @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ}, logical = Logical.AND) - public List getProject() { + public List getProject() { Map> orgProjectMap = organizationService.getOrgProjectMap(); return TreeNodeParseUtils.parseOrgProjectMap(orgProjectMap); } @@ -164,7 +165,7 @@ public class UserController { @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_READ_UPDATE, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_MEMBER_ADD}, logical = Logical.AND) public TableBatchProcessResponse addMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) { //获取本次处理的用户 - userRoleBatchRelationRequest.setSelectIds(userService.getBatchUserIds(userRoleBatchRelationRequest)); + userRoleBatchRelationRequest.setSelectIds(userToolService.getBatchUserIds(userRoleBatchRelationRequest)); OrganizationMemberBatchRequest request = new OrganizationMemberBatchRequest(); request.setOrganizationIds(userRoleBatchRelationRequest.getRoleIds()); request.setUserIds(userRoleBatchRelationRequest.getSelectIds()); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java deleted file mode 100644 index c81f230742..0000000000 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.metersphere.system.response.user; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@NoArgsConstructor -public class UserTreeSelectOption extends UserSelectOption { - @Schema(description = "父节点ID") - private String parentId; - - public UserTreeSelectOption(String id, String name, String parentId) { - this.setId(id); - this.setName(name); - this.setParentId(parentId); - } -} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java index 7f84caaa21..b09e17e80c 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java @@ -36,6 +36,8 @@ public class GlobalUserRoleRelationLogService { private UserRoleMapper userRoleMapper; @Resource private UserService userService; + @Resource + private UserToolService userToolService; /** * 添加接口日志 @@ -64,7 +66,7 @@ public class GlobalUserRoleRelationLogService { UserRoleExample example = new UserRoleExample(); example.createCriteria().andIdIn(request.getRoleIds()); List userRoles = userRoleMapper.selectByExample(example); - List userIds = userService.getBatchUserIds(request); + List userIds = userToolService.getBatchUserIds(request); List users = baseUserMapper.selectUserOptionByIds(userIds); List returnList = new ArrayList<>(); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java index 5e88866fd9..5df2b5c3d1 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java @@ -38,6 +38,8 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService { private GlobalUserRoleService globalUserRoleService; @Resource private UserService userService; + @Resource + private UserToolService userToolService; public List list(GlobalUserRoleRelationQueryRequest request) { List userRoleRelationUserDTOS = extUserRoleRelationMapper.listGlobal(request); @@ -89,7 +91,7 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService { //检查角色的合法性 this.checkGlobalSystemUserRoleLegality(request.getRoleIds()); //获取本次处理的用户 - request.setSelectIds(userService.getBatchUserIds(request)); + request.setSelectIds(userToolService.getBatchUserIds(request)); //检查用户的合法性 userService.checkUserLegality(request.getSelectIds()); List savedUserRoleRelation = this.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds()); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserLogService.java index b56b7ef2f9..174d3e5409 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserLogService.java @@ -29,7 +29,7 @@ public class UserLogService { @Resource private UserMapper userMapper; @Resource - private UserService userService; + private UserToolService userToolService; @Resource private OperationLogService operationLogService; @@ -75,8 +75,8 @@ public class UserLogService { public List batchUpdateLog(TableBatchProcessDTO request) { List logDTOList = new ArrayList<>(); - request.setSelectIds(userService.getBatchUserIds(request)); - List userList = userService.selectByIdList(request.getSelectIds()); + request.setSelectIds(userToolService.getBatchUserIds(request)); + List userList = userToolService.selectByIdList(request.getSelectIds()); for (User user : userList) { LogDTO dto = new LogDTO( OperationLogConstants.SYSTEM, @@ -97,7 +97,7 @@ public class UserLogService { * @param request 批量重置密码 用于记录Log使用 */ public List resetPasswordLog(TableBatchProcessDTO request) { - request.setSelectIds(userService.getBatchUserIds(request)); + request.setSelectIds(userToolService.getBatchUserIds(request)); List returnList = new ArrayList<>(); UserExample example = new UserExample(); example.createCriteria().andIdIn(request.getSelectIds()); @@ -145,13 +145,14 @@ public class UserLogService { public void batchAddProjectLog(UserRoleBatchRelationRequest request, String operator) { List logs = new ArrayList<>(); - List userIds = userService.getBatchUserIds(request); - List userList = userService.selectByIdList(userIds); + List userIds = userToolService.getBatchUserIds(request); + List userList = userToolService.selectByIdList(userIds); for (User user : userList) { //用户管理处修改了用户的组织。 LogDTO log = LogDTOBuilder.builder() .projectId(OperationLogConstants.SYSTEM) .createUser(operator) + .method(HttpMethodConstants.POST.name()) .organizationId(OperationLogConstants.SYSTEM) .sourceId(user.getId()) .type(OperationLogType.UPDATE.name()) @@ -167,8 +168,8 @@ public class UserLogService { public void batchAddOrgLog(UserRoleBatchRelationRequest request, String operator) { List logs = new ArrayList<>(); - List userIds = userService.getBatchUserIds(request); - List userList = userService.selectByIdList(userIds); + List userIds = userToolService.getBatchUserIds(request); + List userList = userToolService.selectByIdList(userIds); for (User user : userList) { //用户管理处修改了用户的组织。 LogDTO log = LogDTOBuilder.builder() diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java index 983ce518f1..97369276cb 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java @@ -34,7 +34,6 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; -import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -66,14 +65,9 @@ public class UserService { private SqlSessionFactory sqlSessionFactory; @Resource - @Lazy private UserLogService userLogService; - - public List selectByIdList(@NotEmpty List userIdList) { - UserExample example = new UserExample(); - example.createCriteria().andIdIn(userIdList); - return userMapper.selectByExample(example); - } + @Resource + private UserToolService userToolService; private void validateUserInfo(List userList) { //判断参数内是否含有重复邮箱 @@ -146,17 +140,19 @@ public class UserService { public List list(BasePageRequest request) { List returnList = new ArrayList<>(); List userList = baseUserMapper.selectByKeyword(request.getKeyword(), false); - List userIdList = userList.stream().map(User::getId).collect(Collectors.toList()); - Map roleAndOrganizationMap = userRoleRelationService.selectGlobalUserRoleAndOrganization(userIdList); - for (User user : userList) { - UserTableResponse userInfo = new UserTableResponse(); - BeanUtils.copyBean(userInfo, user); - UserTableResponse roleOrgModel = roleAndOrganizationMap.get(user.getId()); - if (roleOrgModel != null) { - userInfo.setUserRoleList(roleOrgModel.getUserRoleList()); - userInfo.setOrganizationList(roleOrgModel.getOrganizationList()); + if (CollectionUtils.isNotEmpty(userList)) { + List userIdList = userList.stream().map(User::getId).collect(Collectors.toList()); + Map roleAndOrganizationMap = userRoleRelationService.selectGlobalUserRoleAndOrganization(userIdList); + for (User user : userList) { + UserTableResponse userInfo = new UserTableResponse(); + BeanUtils.copyBean(userInfo, user); + UserTableResponse roleOrgModel = roleAndOrganizationMap.get(user.getId()); + if (roleOrgModel != null) { + userInfo.setUserRoleList(roleOrgModel.getUserRoleList()); + userInfo.setOrganizationList(roleOrgModel.getOrganizationList()); + } + returnList.add(userInfo); } - returnList.add(userInfo); } return returnList; } @@ -176,7 +172,7 @@ public class UserService { } public TableBatchProcessResponse updateUserEnable(UserChangeEnableRequest request, String operator) { - request.setSelectIds(this.getBatchUserIds(request)); + request.setSelectIds(userToolService.getBatchUserIds(request)); this.checkUserInDb(request.getSelectIds()); TableBatchProcessResponse response = new TableBatchProcessResponse(); response.setTotalCount(request.getSelectIds().size()); @@ -261,7 +257,7 @@ public class UserService { public TableBatchProcessResponse deleteUser(@Valid TableBatchProcessDTO request, String operator) { - List userIdList = this.getBatchUserIds(request); + List userIdList = userToolService.getBatchUserIds(request); this.checkUserInDb(userIdList); //检查是否含有Admin this.checkAdminAndThrowException(userIdList); @@ -318,14 +314,14 @@ public class UserService { } public TableBatchProcessResponse resetPassword(TableBatchProcessDTO request, String operator) { - request.setSelectIds(this.getBatchUserIds(request)); + request.setSelectIds(userToolService.getBatchUserIds(request)); this.checkUserInDb(request.getSelectIds()); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); UserMapper batchUpdateMapper = sqlSession.getMapper(UserMapper.class); int insertIndex = 0; long updateTime = System.currentTimeMillis(); - List userList = this.selectByIdList(request.getSelectIds()); + List userList = userToolService.selectByIdList(request.getSelectIds()); for (User user : userList) { User updateModel = new User(); updateModel.setId(user.getId()); @@ -362,18 +358,6 @@ public class UserService { } } - public List getBatchUserIds(TableBatchProcessDTO request) { - if (request.isSelectAll()) { - List userList = baseUserMapper.selectByKeyword(request.getCondition().getKeyword(), true); - List userIdList = userList.stream().map(User::getId).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(request.getExcludeIds())) { - userIdList.removeAll(request.getExcludeIds()); - } - return userIdList; - } else { - return request.getSelectIds(); - } - } public List getUserListByOrgId(String organizationId) { return extUserMapper.getUserListByOrgId(organizationId); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserToolService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserToolService.java new file mode 100644 index 0000000000..0e1e504438 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserToolService.java @@ -0,0 +1,65 @@ +package io.metersphere.system.service; + +import io.metersphere.sdk.dto.TableBatchProcessDTO; +import io.metersphere.sdk.log.service.OperationLogService; +import io.metersphere.sdk.mapper.BaseUserMapper; +import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserExample; +import io.metersphere.system.mapper.ExtUserMapper; +import io.metersphere.system.mapper.UserMapper; +import jakarta.annotation.Resource; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.ibatis.session.SqlSessionFactory; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * 用户工具服务 + */ +@Service +@Transactional(rollbackFor = Exception.class) +public class UserToolService { + @Resource + private BaseUserMapper baseUserMapper; + @Resource + private UserMapper userMapper; + @Resource + private ExtUserMapper extUserMapper; + @Resource + private UserRoleRelationService userRoleRelationService; + @Resource + private OperationLogService operationLogService; + @Resource + private GlobalUserRoleService globalUserRoleService; + @Resource + private UserRoleService userRoleService; + @Resource + private SqlSessionFactory sqlSessionFactory; + + @Resource + @Lazy + private UserLogService userLogService; + + public List selectByIdList(List userIdList) { + UserExample example = new UserExample(); + example.createCriteria().andIdIn(userIdList); + return userMapper.selectByExample(example); + } + + public List getBatchUserIds(TableBatchProcessDTO request) { + if (request.isSelectAll()) { + List userList = baseUserMapper.selectByKeyword(request.getCondition().getKeyword(), true); + List userIdList = userList.stream().map(User::getId).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(request.getExcludeIds())) { + userIdList.removeAll(request.getExcludeIds()); + } + return userIdList; + } else { + return request.getSelectIds(); + } + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java index cdf4ba6880..caccf30f2b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java @@ -1,28 +1,28 @@ package io.metersphere.system.utils; import io.metersphere.project.domain.Project; +import io.metersphere.sdk.dto.BaseTreeNode; import io.metersphere.system.domain.Organization; -import io.metersphere.system.response.user.UserTreeSelectOption; import java.util.ArrayList; import java.util.List; import java.util.Map; public class TreeNodeParseUtils { - public static List parseOrgProjectMap(Map> orgProjectMap) { - List userTreeSelectOptions = new ArrayList<>(); + public static List parseOrgProjectMap(Map> orgProjectMap) { + List returnList = new ArrayList<>(); for (Map.Entry> entry : orgProjectMap.entrySet()) { Organization organization = entry.getKey(); List projects = entry.getValue(); - UserTreeSelectOption orgNode = new UserTreeSelectOption(organization.getId(), organization.getName(), null); - userTreeSelectOptions.add(orgNode); + BaseTreeNode orgNode = new BaseTreeNode(organization.getId(), organization.getName(), Organization.class.getName(), true); + returnList.add(orgNode); for (Project project : projects) { - UserTreeSelectOption projectNode = new UserTreeSelectOption(project.getId(), project.getName(), organization.getId()); - userTreeSelectOptions.add(projectNode); + BaseTreeNode projectNode = new BaseTreeNode(project.getId(), project.getName(), Project.class.getName(), true); + orgNode.addChild(projectNode); } } - return userTreeSelectOptions; + return returnList; } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java index ffce4c3daf..c9e0b456b3 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java @@ -24,9 +24,9 @@ import io.metersphere.system.request.user.UserRoleBatchRelationRequest; import io.metersphere.system.response.user.UserImportResponse; import io.metersphere.system.response.user.UserSelectOption; import io.metersphere.system.response.user.UserTableResponse; -import io.metersphere.system.response.user.UserTreeSelectOption; import io.metersphere.system.service.GlobalUserRoleRelationService; import io.metersphere.system.service.UserService; +import io.metersphere.system.service.UserToolService; import io.metersphere.system.utils.user.UserParamUtils; import io.metersphere.system.utils.user.UserRequestUtils; import jakarta.annotation.Resource; @@ -58,6 +58,8 @@ public class UserControllerTests extends BaseTest { @Resource private UserService userService; @Resource + private UserToolService userToolService; + @Resource private UserMapper userMapper; @Resource private GlobalUserRoleRelationService globalUserRoleRelationService; @@ -73,7 +75,7 @@ public class UserControllerTests extends BaseTest { private static final List USER_LIST = new ArrayList<>(); private static final List USER_ROLE_LIST = new ArrayList<>(); private static final List ORG_LIST = new ArrayList<>(); - private static final List PROJECT_LIST = new ArrayList<>(); + private static final List PROJECT_LIST = new ArrayList<>(); //默认数据 public static final String USER_DEFAULT_NAME = "tianyang.no.1"; public static final String USER_DEFAULT_EMAIL = "tianyang.no.1@126.com"; @@ -95,7 +97,7 @@ public class UserControllerTests extends BaseTest { @Test @Order(0) public void testGetGlobalSystemUserRoleSuccess() throws Exception { - MvcResult mvcResult = userRequestUtils.responseGet(userRequestUtils.URL_GET_GLOBAL_SYSTEM); + MvcResult mvcResult = userRequestUtils.responseGet(UserRequestUtils.URL_GET_GLOBAL_SYSTEM); this.setDefaultUserRoleList(mvcResult); } @@ -119,7 +121,7 @@ public class UserControllerTests extends BaseTest { }}); }} ); - MvcResult mvcResult = userRequestUtils.responsePost(userRequestUtils.URL_USER_CREATE, userMaintainRequest); + MvcResult mvcResult = userRequestUtils.responsePost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest); this.addUser2List(mvcResult); @@ -136,7 +138,7 @@ public class UserControllerTests extends BaseTest { Collections.singletonList(USER_ROLE_LIST.get(0)), userCreateInfoList ); - mvcResult = userRequestUtils.responsePost(userRequestUtils.URL_USER_CREATE, userMaintainRequest); + mvcResult = userRequestUtils.responsePost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest); this.addUser2List(mvcResult); //含有重复的用户名称 @@ -154,12 +156,10 @@ public class UserControllerTests extends BaseTest { }}); }} ); - mvcResult = userRequestUtils.responsePost(userRequestUtils.URL_USER_CREATE, userMaintainRequest); + mvcResult = userRequestUtils.responsePost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest); this.addUser2List(mvcResult); } - public final String URL_GET_ORGANIZATION = "/system/user/get/organization"; - @Test @Order(2) @Sql(scripts = {"/dml/init_user_controller_test.sql"}, @@ -184,7 +184,7 @@ public class UserControllerTests extends BaseTest { } private UserDTO getUserByEmail(String email) throws Exception { - String url = String.format(userRequestUtils.URL_USER_GET, email); + String url = String.format(UserRequestUtils.URL_USER_GET, email); return userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseGet(url), UserDTO.class); } @@ -193,7 +193,7 @@ public class UserControllerTests extends BaseTest { public void testGetByEmailError() throws Exception { //测试使用任意参数,不能获取到任何用户信息 this.checkUserList(); - String url = userRequestUtils.URL_USER_GET + UUID.randomUUID(); + String url = UserRequestUtils.URL_USER_GET + UUID.randomUUID(); MvcResult mvcResult = userRequestUtils.responseGet(url); String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); @@ -207,7 +207,11 @@ public class UserControllerTests extends BaseTest { @Test @Order(3) public void testPageSuccess() throws Exception { - List userRoleIdList = USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList()); + if (CollectionUtils.isEmpty(USER_ROLE_LIST)) { + this.testGetGlobalSystemUserRoleSuccess(); + } + + List userRoleIdList = USER_ROLE_LIST.stream().map(UserSelectOption::getId).toList(); this.checkUserList(); BasePageRequest basePageRequest = UserParamUtils.getDefaultPageRequest(); @@ -222,9 +226,7 @@ public class UserControllerTests extends BaseTest { //用户组不存在非全局用户组 for (UserTableResponse response : userList) { if (CollectionUtils.isNotEmpty(response.getUserRoleList())) { - response.getUserRoleList().forEach(role -> { - Assertions.assertTrue(userRoleIdList.contains(role.getId())); - }); + response.getUserRoleList().forEach(role -> Assertions.assertTrue(userRoleIdList.contains(role.getId()))); } } @@ -242,9 +244,7 @@ public class UserControllerTests extends BaseTest { userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class); for (UserTableResponse response : userList) { if (CollectionUtils.isNotEmpty(response.getUserRoleList())) { - response.getUserRoleList().forEach(role -> { - Assertions.assertTrue(userRoleIdList.contains(role.getId())); - }); + response.getUserRoleList().forEach(role -> Assertions.assertTrue(userRoleIdList.contains(role.getId()))); } } @@ -264,12 +264,20 @@ public class UserControllerTests extends BaseTest { userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class); for (UserTableResponse response : userList) { if (CollectionUtils.isNotEmpty(response.getUserRoleList())) { - response.getUserRoleList().forEach(role -> { - Assertions.assertTrue(userRoleIdList.contains(role.getId())); - }); + response.getUserRoleList().forEach(role -> Assertions.assertTrue(userRoleIdList.contains(role.getId()))); } } + //查找不存在的用户 + basePageRequest = UserParamUtils.getDefaultPageRequest(); + basePageRequest.setKeyword(UUID.randomUUID().toString()); + returnPager = userRequestUtils.selectUserPage(basePageRequest); + //返回值不为空 + Assertions.assertNotNull(returnPager); + //返回值的页码和当前页码相同 + Assertions.assertEquals(returnPager.getTotal(), 0); + //返回的数据量不超过规定要返回的数据量相同 + Assertions.assertEquals(0, JSON.parseArray(JSON.toJSONString(returnPager.getList())).size()); } @Test @@ -278,15 +286,15 @@ public class UserControllerTests extends BaseTest { //当前页码不大于0 BasePageRequest basePageRequest = new BasePageRequest(); basePageRequest.setPageSize(5); - this.requestPost(userRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); //pageSize超过100 basePageRequest = UserParamUtils.getDefaultPageRequest(); basePageRequest.setPageSize(250); - this.requestPost(userRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); //当前页数不大于5 basePageRequest = new BasePageRequest(); basePageRequest.setCurrent(1); - this.requestPost(userRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); //排序字段不合法 basePageRequest = new BasePageRequest(); basePageRequest.setCurrent(1); @@ -294,7 +302,7 @@ public class UserControllerTests extends BaseTest { basePageRequest.setSort(new HashMap<>() {{ put("SELECT * FROM user", "asc"); }}); - this.requestPost(userRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); } @Test @@ -309,21 +317,21 @@ public class UserControllerTests extends BaseTest { //更改名字 user.setName("TEST-UPDATE"); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); checkLog(response.getId(), OperationLogType.UPDATE); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); //更改邮箱 user.setEmail("songtianyang-test-email@12138.com"); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); checkLog(response.getId(), OperationLogType.UPDATE); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); //更改手机号 user.setPhone("18511112222"); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); checkLog(response.getId(), OperationLogType.UPDATE); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); @@ -331,13 +339,13 @@ public class UserControllerTests extends BaseTest { userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST.stream().filter(item -> StringUtils.equals(item.getId(), "member")).toList() ); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); checkDTO = this.getUserByEmail(user.getEmail()); checkLog(response.getId(), OperationLogType.UPDATE); UserParamUtils.compareUserDTO(response, checkDTO); //更改用户组(把上面的情况添加别的权限) userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); checkLog(response.getId(), OperationLogType.UPDATE); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); @@ -345,7 +353,7 @@ public class UserControllerTests extends BaseTest { user = new UserCreateInfo(); BeanUtils.copyBean(user, USER_LIST.get(0)); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); checkLog(response.getId(), OperationLogType.UPDATE); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); @@ -361,22 +369,22 @@ public class UserControllerTests extends BaseTest { BeanUtils.copyBean(user, USER_LIST.get(0)); user.setName(""); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - this.requestPost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); //email为空 BeanUtils.copyBean(user, USER_LIST.get(0)); user.setEmail(""); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - this.requestPost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); //手机号为空 BeanUtils.copyBean(user, USER_LIST.get(0)); user.setEmail(""); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - this.requestPost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); //用户组为空 BeanUtils.copyBean(user, USER_LIST.get(0)); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, new ArrayList<>()); userMaintainRequest.setUserRoleIdList(new ArrayList<>()); - this.requestPost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest, BAD_REQUEST_MATCHER); // 500验证 //邮箱重复 @@ -384,13 +392,13 @@ public class UserControllerTests extends BaseTest { BeanUtils.copyBean(user, USER_LIST.get(0)); user.setEmail(USER_LIST.get(USER_LIST.size() - 1).getEmail()); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); - this.requestPost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest, ERROR_REQUEST_MATCHER); //用户组不包含系统成员 BeanUtils.copyBean(user, USER_LIST.get(0)); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST.stream().filter(item -> !StringUtils.equals(item.getId(), "member")).toList() ); - this.requestPost(userRequestUtils.URL_USER_UPDATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest, ERROR_REQUEST_MATCHER); } @Test @@ -404,7 +412,7 @@ public class UserControllerTests extends BaseTest { this.add(userInfo.getId()); }}); userChangeEnableRequest.setEnable(false); - this.requestPost(userRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, status().isOk()); + this.requestPost(UserRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, status().isOk()); for (String item : userChangeEnableRequest.getSelectIds()) { checkLog(item, OperationLogType.UPDATE); } @@ -420,12 +428,12 @@ public class UserControllerTests extends BaseTest { //用户不存在 UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest(); userChangeEnableRequest.setEnable(false); - this.requestPost(userRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, BAD_REQUEST_MATCHER); //含有非法用户 userChangeEnableRequest.setSelectIds(new ArrayList<>() {{ this.add("BCDEDIT"); }}); - this.requestPost(userRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, ERROR_REQUEST_MATCHER); } @@ -441,7 +449,7 @@ public class UserControllerTests extends BaseTest { String filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_success.xlsx")).getPath(); MockMultipartFile file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath)); ExcelParseDTO userImportReportDTOByFile = userService.getUserExcelParseDTO(file); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(userRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(UserRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);//检查返回值 List userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 for (UserDTO item : userDTOList) { @@ -451,7 +459,7 @@ public class UserControllerTests extends BaseTest { //导入空文件. 应当导入成功的数据为0 filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_success_empty.xlsx")).getPath(); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath)); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(userRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(UserRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); importSuccessData = 0; errorDataIndex = new int[]{}; UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); @@ -459,7 +467,7 @@ public class UserControllerTests extends BaseTest { //文件内没有一条合格数据 应当导入成功的数据为0 filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_error_all.xlsx")).getPath(); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath)); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(userRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(UserRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); errorDataIndex = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); @@ -467,7 +475,7 @@ public class UserControllerTests extends BaseTest { filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_error_email_repeat_db.xlsx")).getPath(); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath)); userImportReportDTOByFile = userService.getUserExcelParseDTO(file); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(userRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(UserRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); importSuccessData = 8; errorDataIndex = new int[]{1, 7}; UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); @@ -480,7 +488,7 @@ public class UserControllerTests extends BaseTest { filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_error_email_repeat_in_file.xlsx")).getPath(); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath)); userImportReportDTOByFile = userService.getUserExcelParseDTO(file); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(userRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(UserRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); errorDataIndex = new int[]{9, 10}; UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 @@ -491,7 +499,7 @@ public class UserControllerTests extends BaseTest { //文件不符合规范 应当导入成功的数据为0 filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/abcde.gif")).getPath(); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath)); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(userRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(UserRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); importSuccessData = 0; errorDataIndex = new int[]{}; UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); @@ -500,7 +508,7 @@ public class UserControllerTests extends BaseTest { filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_success_03.xls")).getPath(); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath)); userImportReportDTOByFile = userService.getUserExcelParseDTO(file); - response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(userRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); + response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseFile(UserRequestUtils.URL_USER_IMPORT, file), UserImportResponse.class); importSuccessData = 10;//应该导入成功的数据数量 errorDataIndex = new int[]{};//出错数据的行数 UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);//检查返回值 @@ -517,7 +525,7 @@ public class UserControllerTests extends BaseTest { { TableBatchProcessDTO request = new TableBatchProcessDTO(); request.setSelectIds(Collections.singletonList("none user")); - this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request, ERROR_REQUEST_MATCHER); + this.requestPostAndReturn(UserRequestUtils.URL_USER_RESET_PASSWORD, request, ERROR_REQUEST_MATCHER); } } @@ -545,24 +553,24 @@ public class UserControllerTests extends BaseTest { */ //所有参数都为空 userMaintainRequest = UserParamUtils.getUserCreateDTO(null, null); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //用户组ID为空 userMaintainRequest = UserParamUtils.getUserCreateDTO( null, errorUserList); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //没有用户 userMaintainRequest = UserParamUtils.getUserCreateDTO( USER_ROLE_LIST, null); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //用户组含有null userMaintainRequest = UserParamUtils.getUserCreateDTO( USER_ROLE_LIST, errorUserList); userMaintainRequest.getUserRoleIdList().add(null); userMaintainRequest.getUserRoleIdList().add(""); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //含有用户名称为空的数据 userMaintainRequest = UserParamUtils.getUserCreateDTO( USER_ROLE_LIST, @@ -571,7 +579,7 @@ public class UserControllerTests extends BaseTest { userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ setEmail("tianyang.name.empty@126.com"); }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //含有用户邮箱为空的数据 userMaintainRequest = UserParamUtils.getUserCreateDTO( USER_ROLE_LIST, @@ -580,7 +588,7 @@ public class UserControllerTests extends BaseTest { userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ setName("tianyang.email.empty"); }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //用户邮箱不符合标准 userMaintainRequest = UserParamUtils.getUserCreateDTO( USER_ROLE_LIST, @@ -590,7 +598,7 @@ public class UserControllerTests extends BaseTest { setName("用户邮箱放飞自我"); setEmail("用户邮箱放飞自我"); }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); /* * 校验业务判断出错的反例 (500 error) * 需要保证数据库有正常数据 @@ -604,7 +612,7 @@ public class UserControllerTests extends BaseTest { }}); }}, errorUserList); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); //含有重复的用户邮箱 userMaintainRequest = UserParamUtils.getUserCreateDTO( USER_ROLE_LIST, @@ -615,7 +623,7 @@ public class UserControllerTests extends BaseTest { setName("tianyang.no.error4"); setEmail(firstUserEmail); }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); //测试请求参数中含有数据库中已存在的邮箱情况 userMaintainRequest = UserParamUtils.getUserCreateDTO( USER_ROLE_LIST, @@ -629,7 +637,7 @@ public class UserControllerTests extends BaseTest { }}); }} ); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); } @Test @@ -640,7 +648,7 @@ public class UserControllerTests extends BaseTest { { TableBatchProcessDTO request = new TableBatchProcessDTO(); request.setSelectIds(Collections.singletonList("admin")); - this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request); + this.requestPostAndReturn(UserRequestUtils.URL_USER_RESET_PASSWORD, request); //检查数据库 UserExample userExample = new UserExample(); userExample.createCriteria().andIdEqualTo("admin").andPasswordEqualTo(CodingUtil.md5("metersphere")); @@ -657,11 +665,11 @@ public class UserControllerTests extends BaseTest { TableBatchProcessDTO request = new TableBatchProcessDTO(); request.setSelectIds(Collections.singletonList(userId)); TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( - this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), + this.requestPostAndReturn(UserRequestUtils.URL_USER_RESET_PASSWORD, request), TableBatchProcessResponse.class ); Assertions.assertEquals(response.getTotalCount(), response.getSuccessCount(), 1); - List userList = userService.selectByIdList(Collections.singletonList(userId)); + List userList = userToolService.selectByIdList(Collections.singletonList(userId)); for (User checkUser : userList) { UserExample userExample = new UserExample(); userExample.createCriteria().andIdEqualTo(checkUser.getId()).andPasswordEqualTo(CodingUtil.md5(checkUser.getEmail())); @@ -675,7 +683,7 @@ public class UserControllerTests extends BaseTest { request.setExcludeIds(Collections.singletonList("admin")); request.setSelectAll(true); TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( - this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), + this.requestPostAndReturn(UserRequestUtils.URL_USER_RESET_PASSWORD, request), TableBatchProcessResponse.class ); UserExample example = new UserExample(); @@ -705,9 +713,9 @@ public class UserControllerTests extends BaseTest { List last50Users = USER_LIST.subList(USER_LIST.size() - 50, USER_LIST.size()); //测试添加角色权限。 预期数据:每个用户都会增加对应的权限 UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); - request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); - request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, null); + request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).toList()); + request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).toList()); + userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, null); //检查有权限的数据量是否一致 Assertions.assertEquals( globalUserRoleRelationService.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds()).size(), @@ -719,7 +727,7 @@ public class UserControllerTests extends BaseTest { } //测试重复添加用户权限。预期结果:不会额外增加数据 - userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, null); + userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, null); //检查有权限的数据量是否一致 Assertions.assertEquals( globalUserRoleRelationService.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds()).size(), @@ -742,25 +750,25 @@ public class UserControllerTests extends BaseTest { // 用户ID为空 UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); request.setSelectIds(new ArrayList<>()); - request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); + request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).toList()); + userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); // 角色id为空 request = new UserRoleBatchRelationRequest(); - request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).toList()); request.setRoleIds(new ArrayList<>()); - userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); + userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); // 用户ID含有不存在的 request = new UserRoleBatchRelationRequest(); request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); - request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); request.getSelectIds().add("none user"); - userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); + request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).toList()); + userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); // 角色ID含有不存在的 request = new UserRoleBatchRelationRequest(); - request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).toList()); request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); request.getRoleIds().add("none role"); - userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); + userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); } @Test @@ -769,20 +777,20 @@ public class UserControllerTests extends BaseTest { config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED), executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) public void testGetProjectAndOrganization() throws Exception { - String str = userRequestUtils.responseGet(userRequestUtils.URL_GET_PROJECT).getResponse().getContentAsString(StandardCharsets.UTF_8); + String str = userRequestUtils.responseGet(UserRequestUtils.URL_GET_PROJECT).getResponse().getContentAsString(StandardCharsets.UTF_8); ResultHolder rh = JSON.parseObject(str, ResultHolder.class); - List userTreeSelectOptions = JSON.parseArray( + List userTreeSelectOptions = JSON.parseArray( JSON.toJSONString(rh.getData()), - UserTreeSelectOption.class); + BaseTreeNode.class); //返回值不为空 Assertions.assertTrue(CollectionUtils.isNotEmpty(userTreeSelectOptions)); - PROJECT_LIST.addAll(userTreeSelectOptions); + userTreeSelectOptions.forEach(item -> PROJECT_LIST.addAll(item.getChildren())); List userSelectOptions = JSON.parseArray( JSON.toJSONString( JSON.parseObject( - userRequestUtils.responseGet(userRequestUtils.URL_GET_ORGANIZATION).getResponse().getContentAsString(StandardCharsets.UTF_8), + userRequestUtils.responseGet(UserRequestUtils.URL_GET_ORGANIZATION).getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData()), UserSelectOption.class); ORG_LIST.addAll(userSelectOptions); @@ -799,14 +807,12 @@ public class UserControllerTests extends BaseTest { this.testGetProjectAndOrganization(); } - List userIds = this.selectUserTableIds(50); + List userIds = this.selectUserTableIds(100); UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); request.setSelectIds(userIds); - request.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - //排除树结构中的组织ID - request.getRoleIds().removeAll(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - this.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, request); + request.setRoleIds(PROJECT_LIST.stream().map(BaseTreeNode::getId).toList()); + this.requestPostWithOk(UserRequestUtils.URL_ADD_PROJECT_MEMBER, request); //检查有权限的数据量是否一致 UserRoleRelationExample checkExample = new UserRoleRelationExample(); @@ -850,8 +856,8 @@ public class UserControllerTests extends BaseTest { UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); request.setSelectIds(userIds); - request.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - this.requestPostWithOk(userRequestUtils.URL_ADD_ORGANIZATION_MEMBER, request); + request.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).toList()); + this.requestPostWithOk(UserRequestUtils.URL_ADD_ORGANIZATION_MEMBER, request); //检查有权限的数据量是否一致 UserRoleRelationExample checkExample = new UserRoleRelationExample(); for (String orgId : request.getRoleIds()) { @@ -881,45 +887,45 @@ public class UserControllerTests extends BaseTest { // 用户ID为空 UserRoleBatchRelationRequest addToProjectRequest = new UserRoleBatchRelationRequest(); addToProjectRequest.setSelectIds(new ArrayList<>()); - addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); + addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(BaseTreeNode::getId).toList()); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); // 项目为空 addToProjectRequest = new UserRoleBatchRelationRequest(); - addToProjectRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + addToProjectRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).toList()); addToProjectRequest.setRoleIds(new ArrayList<>()); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, BAD_REQUEST_MATCHER); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, BAD_REQUEST_MATCHER); // 用户ID含有不存在的 addToProjectRequest = new UserRoleBatchRelationRequest(); addToProjectRequest.setSelectIds(Collections.singletonList("none user")); - addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); + addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(BaseTreeNode::getId).toList()); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); // 项目ID含有不存在的 addToProjectRequest = new UserRoleBatchRelationRequest(); - addToProjectRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + addToProjectRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).toList()); addToProjectRequest.setRoleIds(Collections.singletonList("none role")); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); // 用户ID为空 UserRoleBatchRelationRequest orgRequest = new UserRoleBatchRelationRequest(); orgRequest.setSelectIds(new ArrayList<>()); - orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); + orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).toList()); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); // 项目为空 orgRequest = new UserRoleBatchRelationRequest(); - orgRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + orgRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).toList()); orgRequest.setRoleIds(new ArrayList<>()); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, BAD_REQUEST_MATCHER); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, BAD_REQUEST_MATCHER); // 用户ID含有不存在的 orgRequest = new UserRoleBatchRelationRequest(); orgRequest.setSelectIds(Collections.singletonList("none user")); - orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); + orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).toList()); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); // 项目ID含有不存在的 orgRequest = new UserRoleBatchRelationRequest(); - orgRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + orgRequest.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).toList()); orgRequest.setRoleIds(Collections.singletonList("none role")); - userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); + userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); } //本测试类中会用到很多次用户数据。所以测试删除的方法放于最后 @@ -929,9 +935,9 @@ public class UserControllerTests extends BaseTest { this.checkUserList(); //删除USER_LIST用户 TableBatchProcessDTO request = new TableBatchProcessDTO(); - request.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + request.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).toList()); TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( - userRequestUtils.responsePost(userRequestUtils.URL_USER_DELETE, request), TableBatchProcessResponse.class); + userRequestUtils.responsePost(UserRequestUtils.URL_USER_DELETE, request), TableBatchProcessResponse.class); Assertions.assertEquals(request.getSelectIds().size(), response.getTotalCount()); Assertions.assertEquals(request.getSelectIds().size(), response.getSuccessCount()); //检查数据库 @@ -950,21 +956,21 @@ public class UserControllerTests extends BaseTest { public void testUserDeleteError() throws Exception { //参数为空 TableBatchProcessDTO request = new TableBatchProcessDTO(); - this.requestPost(userRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); //用户不存在 request.setSelectIds(Collections.singletonList("none user")); - this.requestPost(userRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); //测试用户已经被删除的 if (CollectionUtils.isEmpty(DELETED_USER_ID_LIST)) { this.testUserDeleteSuccess(); } request.setSelectIds(DELETED_USER_ID_LIST); - this.requestPost(userRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); //测试包含Admin用户 request = new TableBatchProcessDTO(); request.setSelectAll(true); - this.requestPost(userRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); + this.requestPost(UserRequestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); } @@ -987,7 +993,7 @@ public class UserControllerTests extends BaseTest { Pager returnPager = userRequestUtils.selectUserPage(basePageRequest); //用户组不存在非全局用户组 List userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class); - return userList.stream().map(User::getId).collect(Collectors.toList()); + return userList.stream().map(User::getId).toList(); } //成功入库的用户保存内存中,其他用例会使用到 diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java index f5976f458b..0093c52df5 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java @@ -15,6 +15,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.nio.charset.StandardCharsets; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -73,6 +74,7 @@ public class UserRequestUtils { .header(SessionConstants.CSRF_TOKEN, csrfToken) .content(JSON.toJSONString(param)) .contentType(MediaType.APPLICATION_JSON)) + .andDo(print()) .andExpect(resultMatcher) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); }