refactor(系统设置): 优化用户管理的树结构数据返回方式

This commit is contained in:
song-tianyang 2023-08-24 15:56:36 +08:00 committed by 刘瑞斌
parent 95c1f764f6
commit 11575a4169
11 changed files with 266 additions and 181 deletions

View File

@ -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<BaseTreeNode> 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);
}
}

View File

@ -22,7 +22,6 @@ import io.metersphere.system.request.user.UserRoleBatchRelationRequest;
import io.metersphere.system.response.user.UserImportResponse; import io.metersphere.system.response.user.UserImportResponse;
import io.metersphere.system.response.user.UserSelectOption; import io.metersphere.system.response.user.UserSelectOption;
import io.metersphere.system.response.user.UserTableResponse; import io.metersphere.system.response.user.UserTableResponse;
import io.metersphere.system.response.user.UserTreeSelectOption;
import io.metersphere.system.service.*; import io.metersphere.system.service.*;
import io.metersphere.system.utils.TreeNodeParseUtils; import io.metersphere.system.utils.TreeNodeParseUtils;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
@ -45,6 +44,8 @@ public class UserController {
@Resource @Resource
private UserService userService; private UserService userService;
@Resource @Resource
private UserToolService userToolService;
@Resource
private GlobalUserRoleService globalUserRoleService; private GlobalUserRoleService globalUserRoleService;
@Resource @Resource
private GlobalUserRoleRelationService globalUserRoleRelationService; private GlobalUserRoleRelationService globalUserRoleRelationService;
@ -134,7 +135,7 @@ public class UserController {
@GetMapping("/get/project") @GetMapping("/get/project")
@Operation(summary = "用户批量操作-查找项目") @Operation(summary = "用户批量操作-查找项目")
@RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ}, logical = Logical.AND) @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ}, logical = Logical.AND)
public List<UserTreeSelectOption> getProject() { public List<BaseTreeNode> getProject() {
Map<Organization, List<Project>> orgProjectMap = organizationService.getOrgProjectMap(); Map<Organization, List<Project>> orgProjectMap = organizationService.getOrgProjectMap();
return TreeNodeParseUtils.parseOrgProjectMap(orgProjectMap); 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) @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_READ_UPDATE, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_MEMBER_ADD}, logical = Logical.AND)
public TableBatchProcessResponse addMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) { public TableBatchProcessResponse addMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) {
//获取本次处理的用户 //获取本次处理的用户
userRoleBatchRelationRequest.setSelectIds(userService.getBatchUserIds(userRoleBatchRelationRequest)); userRoleBatchRelationRequest.setSelectIds(userToolService.getBatchUserIds(userRoleBatchRelationRequest));
OrganizationMemberBatchRequest request = new OrganizationMemberBatchRequest(); OrganizationMemberBatchRequest request = new OrganizationMemberBatchRequest();
request.setOrganizationIds(userRoleBatchRelationRequest.getRoleIds()); request.setOrganizationIds(userRoleBatchRelationRequest.getRoleIds());
request.setUserIds(userRoleBatchRelationRequest.getSelectIds()); request.setUserIds(userRoleBatchRelationRequest.getSelectIds());

View File

@ -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);
}
}

View File

@ -36,6 +36,8 @@ public class GlobalUserRoleRelationLogService {
private UserRoleMapper userRoleMapper; private UserRoleMapper userRoleMapper;
@Resource @Resource
private UserService userService; private UserService userService;
@Resource
private UserToolService userToolService;
/** /**
* 添加接口日志 * 添加接口日志
@ -64,7 +66,7 @@ public class GlobalUserRoleRelationLogService {
UserRoleExample example = new UserRoleExample(); UserRoleExample example = new UserRoleExample();
example.createCriteria().andIdIn(request.getRoleIds()); example.createCriteria().andIdIn(request.getRoleIds());
List<UserRole> userRoles = userRoleMapper.selectByExample(example); List<UserRole> userRoles = userRoleMapper.selectByExample(example);
List<String> userIds = userService.getBatchUserIds(request); List<String> userIds = userToolService.getBatchUserIds(request);
List<OptionDTO> users = baseUserMapper.selectUserOptionByIds(userIds); List<OptionDTO> users = baseUserMapper.selectUserOptionByIds(userIds);
List<LogDTO> returnList = new ArrayList<>(); List<LogDTO> returnList = new ArrayList<>();

View File

@ -38,6 +38,8 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService {
private GlobalUserRoleService globalUserRoleService; private GlobalUserRoleService globalUserRoleService;
@Resource @Resource
private UserService userService; private UserService userService;
@Resource
private UserToolService userToolService;
public List<UserRoleRelationUserDTO> list(GlobalUserRoleRelationQueryRequest request) { public List<UserRoleRelationUserDTO> list(GlobalUserRoleRelationQueryRequest request) {
List<UserRoleRelationUserDTO> userRoleRelationUserDTOS = extUserRoleRelationMapper.listGlobal(request); List<UserRoleRelationUserDTO> userRoleRelationUserDTOS = extUserRoleRelationMapper.listGlobal(request);
@ -89,7 +91,7 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService {
//检查角色的合法性 //检查角色的合法性
this.checkGlobalSystemUserRoleLegality(request.getRoleIds()); this.checkGlobalSystemUserRoleLegality(request.getRoleIds());
//获取本次处理的用户 //获取本次处理的用户
request.setSelectIds(userService.getBatchUserIds(request)); request.setSelectIds(userToolService.getBatchUserIds(request));
//检查用户的合法性 //检查用户的合法性
userService.checkUserLegality(request.getSelectIds()); userService.checkUserLegality(request.getSelectIds());
List<UserRoleRelation> savedUserRoleRelation = this.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds()); List<UserRoleRelation> savedUserRoleRelation = this.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds());

View File

@ -29,7 +29,7 @@ public class UserLogService {
@Resource @Resource
private UserMapper userMapper; private UserMapper userMapper;
@Resource @Resource
private UserService userService; private UserToolService userToolService;
@Resource @Resource
private OperationLogService operationLogService; private OperationLogService operationLogService;
@ -75,8 +75,8 @@ public class UserLogService {
public List<LogDTO> batchUpdateLog(TableBatchProcessDTO request) { public List<LogDTO> batchUpdateLog(TableBatchProcessDTO request) {
List<LogDTO> logDTOList = new ArrayList<>(); List<LogDTO> logDTOList = new ArrayList<>();
request.setSelectIds(userService.getBatchUserIds(request)); request.setSelectIds(userToolService.getBatchUserIds(request));
List<User> userList = userService.selectByIdList(request.getSelectIds()); List<User> userList = userToolService.selectByIdList(request.getSelectIds());
for (User user : userList) { for (User user : userList) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM, OperationLogConstants.SYSTEM,
@ -97,7 +97,7 @@ public class UserLogService {
* @param request 批量重置密码 用于记录Log使用 * @param request 批量重置密码 用于记录Log使用
*/ */
public List<LogDTO> resetPasswordLog(TableBatchProcessDTO request) { public List<LogDTO> resetPasswordLog(TableBatchProcessDTO request) {
request.setSelectIds(userService.getBatchUserIds(request)); request.setSelectIds(userToolService.getBatchUserIds(request));
List<LogDTO> returnList = new ArrayList<>(); List<LogDTO> returnList = new ArrayList<>();
UserExample example = new UserExample(); UserExample example = new UserExample();
example.createCriteria().andIdIn(request.getSelectIds()); example.createCriteria().andIdIn(request.getSelectIds());
@ -145,13 +145,14 @@ public class UserLogService {
public void batchAddProjectLog(UserRoleBatchRelationRequest request, String operator) { public void batchAddProjectLog(UserRoleBatchRelationRequest request, String operator) {
List<LogDTO> logs = new ArrayList<>(); List<LogDTO> logs = new ArrayList<>();
List<String> userIds = userService.getBatchUserIds(request); List<String> userIds = userToolService.getBatchUserIds(request);
List<User> userList = userService.selectByIdList(userIds); List<User> userList = userToolService.selectByIdList(userIds);
for (User user : userList) { for (User user : userList) {
//用户管理处修改了用户的组织 //用户管理处修改了用户的组织
LogDTO log = LogDTOBuilder.builder() LogDTO log = LogDTOBuilder.builder()
.projectId(OperationLogConstants.SYSTEM) .projectId(OperationLogConstants.SYSTEM)
.createUser(operator) .createUser(operator)
.method(HttpMethodConstants.POST.name())
.organizationId(OperationLogConstants.SYSTEM) .organizationId(OperationLogConstants.SYSTEM)
.sourceId(user.getId()) .sourceId(user.getId())
.type(OperationLogType.UPDATE.name()) .type(OperationLogType.UPDATE.name())
@ -167,8 +168,8 @@ public class UserLogService {
public void batchAddOrgLog(UserRoleBatchRelationRequest request, String operator) { public void batchAddOrgLog(UserRoleBatchRelationRequest request, String operator) {
List<LogDTO> logs = new ArrayList<>(); List<LogDTO> logs = new ArrayList<>();
List<String> userIds = userService.getBatchUserIds(request); List<String> userIds = userToolService.getBatchUserIds(request);
List<User> userList = userService.selectByIdList(userIds); List<User> userList = userToolService.selectByIdList(userIds);
for (User user : userList) { for (User user : userList) {
//用户管理处修改了用户的组织 //用户管理处修改了用户的组织
LogDTO log = LogDTOBuilder.builder() LogDTO log = LogDTOBuilder.builder()

View File

@ -34,7 +34,6 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils; import org.mybatis.spring.SqlSessionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -66,14 +65,9 @@ public class UserService {
private SqlSessionFactory sqlSessionFactory; private SqlSessionFactory sqlSessionFactory;
@Resource @Resource
@Lazy
private UserLogService userLogService; private UserLogService userLogService;
@Resource
public List<User> selectByIdList(@NotEmpty List<String> userIdList) { private UserToolService userToolService;
UserExample example = new UserExample();
example.createCriteria().andIdIn(userIdList);
return userMapper.selectByExample(example);
}
private void validateUserInfo(List<UserCreateInfo> userList) { private void validateUserInfo(List<UserCreateInfo> userList) {
//判断参数内是否含有重复邮箱 //判断参数内是否含有重复邮箱
@ -146,17 +140,19 @@ public class UserService {
public List<UserTableResponse> list(BasePageRequest request) { public List<UserTableResponse> list(BasePageRequest request) {
List<UserTableResponse> returnList = new ArrayList<>(); List<UserTableResponse> returnList = new ArrayList<>();
List<User> userList = baseUserMapper.selectByKeyword(request.getKeyword(), false); List<User> userList = baseUserMapper.selectByKeyword(request.getKeyword(), false);
List<String> userIdList = userList.stream().map(User::getId).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(userList)) {
Map<String, UserTableResponse> roleAndOrganizationMap = userRoleRelationService.selectGlobalUserRoleAndOrganization(userIdList); List<String> userIdList = userList.stream().map(User::getId).collect(Collectors.toList());
for (User user : userList) { Map<String, UserTableResponse> roleAndOrganizationMap = userRoleRelationService.selectGlobalUserRoleAndOrganization(userIdList);
UserTableResponse userInfo = new UserTableResponse(); for (User user : userList) {
BeanUtils.copyBean(userInfo, user); UserTableResponse userInfo = new UserTableResponse();
UserTableResponse roleOrgModel = roleAndOrganizationMap.get(user.getId()); BeanUtils.copyBean(userInfo, user);
if (roleOrgModel != null) { UserTableResponse roleOrgModel = roleAndOrganizationMap.get(user.getId());
userInfo.setUserRoleList(roleOrgModel.getUserRoleList()); if (roleOrgModel != null) {
userInfo.setOrganizationList(roleOrgModel.getOrganizationList()); userInfo.setUserRoleList(roleOrgModel.getUserRoleList());
userInfo.setOrganizationList(roleOrgModel.getOrganizationList());
}
returnList.add(userInfo);
} }
returnList.add(userInfo);
} }
return returnList; return returnList;
} }
@ -176,7 +172,7 @@ public class UserService {
} }
public TableBatchProcessResponse updateUserEnable(UserChangeEnableRequest request, String operator) { public TableBatchProcessResponse updateUserEnable(UserChangeEnableRequest request, String operator) {
request.setSelectIds(this.getBatchUserIds(request)); request.setSelectIds(userToolService.getBatchUserIds(request));
this.checkUserInDb(request.getSelectIds()); this.checkUserInDb(request.getSelectIds());
TableBatchProcessResponse response = new TableBatchProcessResponse(); TableBatchProcessResponse response = new TableBatchProcessResponse();
response.setTotalCount(request.getSelectIds().size()); response.setTotalCount(request.getSelectIds().size());
@ -261,7 +257,7 @@ public class UserService {
public TableBatchProcessResponse deleteUser(@Valid TableBatchProcessDTO request, String operator) { public TableBatchProcessResponse deleteUser(@Valid TableBatchProcessDTO request, String operator) {
List<String> userIdList = this.getBatchUserIds(request); List<String> userIdList = userToolService.getBatchUserIds(request);
this.checkUserInDb(userIdList); this.checkUserInDb(userIdList);
//检查是否含有Admin //检查是否含有Admin
this.checkAdminAndThrowException(userIdList); this.checkAdminAndThrowException(userIdList);
@ -318,14 +314,14 @@ public class UserService {
} }
public TableBatchProcessResponse resetPassword(TableBatchProcessDTO request, String operator) { public TableBatchProcessResponse resetPassword(TableBatchProcessDTO request, String operator) {
request.setSelectIds(this.getBatchUserIds(request)); request.setSelectIds(userToolService.getBatchUserIds(request));
this.checkUserInDb(request.getSelectIds()); this.checkUserInDb(request.getSelectIds());
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
UserMapper batchUpdateMapper = sqlSession.getMapper(UserMapper.class); UserMapper batchUpdateMapper = sqlSession.getMapper(UserMapper.class);
int insertIndex = 0; int insertIndex = 0;
long updateTime = System.currentTimeMillis(); long updateTime = System.currentTimeMillis();
List<User> userList = this.selectByIdList(request.getSelectIds()); List<User> userList = userToolService.selectByIdList(request.getSelectIds());
for (User user : userList) { for (User user : userList) {
User updateModel = new User(); User updateModel = new User();
updateModel.setId(user.getId()); updateModel.setId(user.getId());
@ -362,18 +358,6 @@ public class UserService {
} }
} }
public List<String> getBatchUserIds(TableBatchProcessDTO request) {
if (request.isSelectAll()) {
List<User> userList = baseUserMapper.selectByKeyword(request.getCondition().getKeyword(), true);
List<String> 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<User> getUserListByOrgId(String organizationId) { public List<User> getUserListByOrgId(String organizationId) {
return extUserMapper.getUserListByOrgId(organizationId); return extUserMapper.getUserListByOrgId(organizationId);

View File

@ -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<User> selectByIdList(List<String> userIdList) {
UserExample example = new UserExample();
example.createCriteria().andIdIn(userIdList);
return userMapper.selectByExample(example);
}
public List<String> getBatchUserIds(TableBatchProcessDTO request) {
if (request.isSelectAll()) {
List<User> userList = baseUserMapper.selectByKeyword(request.getCondition().getKeyword(), true);
List<String> userIdList = userList.stream().map(User::getId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(request.getExcludeIds())) {
userIdList.removeAll(request.getExcludeIds());
}
return userIdList;
} else {
return request.getSelectIds();
}
}
}

View File

@ -1,28 +1,28 @@
package io.metersphere.system.utils; package io.metersphere.system.utils;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.sdk.dto.BaseTreeNode;
import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Organization;
import io.metersphere.system.response.user.UserTreeSelectOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class TreeNodeParseUtils { public class TreeNodeParseUtils {
public static List<UserTreeSelectOption> parseOrgProjectMap(Map<Organization, List<Project>> orgProjectMap) { public static List<BaseTreeNode> parseOrgProjectMap(Map<Organization, List<Project>> orgProjectMap) {
List<UserTreeSelectOption> userTreeSelectOptions = new ArrayList<>(); List<BaseTreeNode> returnList = new ArrayList<>();
for (Map.Entry<Organization, List<Project>> entry : orgProjectMap.entrySet()) { for (Map.Entry<Organization, List<Project>> entry : orgProjectMap.entrySet()) {
Organization organization = entry.getKey(); Organization organization = entry.getKey();
List<Project> projects = entry.getValue(); List<Project> projects = entry.getValue();
UserTreeSelectOption orgNode = new UserTreeSelectOption(organization.getId(), organization.getName(), null); BaseTreeNode orgNode = new BaseTreeNode(organization.getId(), organization.getName(), Organization.class.getName(), true);
userTreeSelectOptions.add(orgNode); returnList.add(orgNode);
for (Project project : projects) { for (Project project : projects) {
UserTreeSelectOption projectNode = new UserTreeSelectOption(project.getId(), project.getName(), organization.getId()); BaseTreeNode projectNode = new BaseTreeNode(project.getId(), project.getName(), Project.class.getName(), true);
userTreeSelectOptions.add(projectNode); orgNode.addChild(projectNode);
} }
} }
return userTreeSelectOptions; return returnList;
} }
} }

View File

@ -24,9 +24,9 @@ import io.metersphere.system.request.user.UserRoleBatchRelationRequest;
import io.metersphere.system.response.user.UserImportResponse; import io.metersphere.system.response.user.UserImportResponse;
import io.metersphere.system.response.user.UserSelectOption; import io.metersphere.system.response.user.UserSelectOption;
import io.metersphere.system.response.user.UserTableResponse; 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.GlobalUserRoleRelationService;
import io.metersphere.system.service.UserService; 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.UserParamUtils;
import io.metersphere.system.utils.user.UserRequestUtils; import io.metersphere.system.utils.user.UserRequestUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -58,6 +58,8 @@ public class UserControllerTests extends BaseTest {
@Resource @Resource
private UserService userService; private UserService userService;
@Resource @Resource
private UserToolService userToolService;
@Resource
private UserMapper userMapper; private UserMapper userMapper;
@Resource @Resource
private GlobalUserRoleRelationService globalUserRoleRelationService; private GlobalUserRoleRelationService globalUserRoleRelationService;
@ -73,7 +75,7 @@ public class UserControllerTests extends BaseTest {
private static final List<UserCreateInfo> USER_LIST = new ArrayList<>(); private static final List<UserCreateInfo> USER_LIST = new ArrayList<>();
private static final List<UserSelectOption> USER_ROLE_LIST = new ArrayList<>(); private static final List<UserSelectOption> USER_ROLE_LIST = new ArrayList<>();
private static final List<UserSelectOption> ORG_LIST = new ArrayList<>(); private static final List<UserSelectOption> ORG_LIST = new ArrayList<>();
private static final List<UserTreeSelectOption> PROJECT_LIST = new ArrayList<>(); private static final List<BaseTreeNode> PROJECT_LIST = new ArrayList<>();
//默认数据 //默认数据
public static final String USER_DEFAULT_NAME = "tianyang.no.1"; public static final String USER_DEFAULT_NAME = "tianyang.no.1";
public static final String USER_DEFAULT_EMAIL = "tianyang.no.1@126.com"; public static final String USER_DEFAULT_EMAIL = "tianyang.no.1@126.com";
@ -95,7 +97,7 @@ public class UserControllerTests extends BaseTest {
@Test @Test
@Order(0) @Order(0)
public void testGetGlobalSystemUserRoleSuccess() throws Exception { 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); 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); this.addUser2List(mvcResult);
@ -136,7 +138,7 @@ public class UserControllerTests extends BaseTest {
Collections.singletonList(USER_ROLE_LIST.get(0)), Collections.singletonList(USER_ROLE_LIST.get(0)),
userCreateInfoList userCreateInfoList
); );
mvcResult = userRequestUtils.responsePost(userRequestUtils.URL_USER_CREATE, userMaintainRequest); mvcResult = userRequestUtils.responsePost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest);
this.addUser2List(mvcResult); 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); this.addUser2List(mvcResult);
} }
public final String URL_GET_ORGANIZATION = "/system/user/get/organization";
@Test @Test
@Order(2) @Order(2)
@Sql(scripts = {"/dml/init_user_controller_test.sql"}, @Sql(scripts = {"/dml/init_user_controller_test.sql"},
@ -184,7 +184,7 @@ public class UserControllerTests extends BaseTest {
} }
private UserDTO getUserByEmail(String email) throws Exception { 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); return userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responseGet(url), UserDTO.class);
} }
@ -193,7 +193,7 @@ public class UserControllerTests extends BaseTest {
public void testGetByEmailError() throws Exception { public void testGetByEmailError() throws Exception {
//测试使用任意参数不能获取到任何用户信息 //测试使用任意参数不能获取到任何用户信息
this.checkUserList(); this.checkUserList();
String url = userRequestUtils.URL_USER_GET + UUID.randomUUID(); String url = UserRequestUtils.URL_USER_GET + UUID.randomUUID();
MvcResult mvcResult = userRequestUtils.responseGet(url); MvcResult mvcResult = userRequestUtils.responseGet(url);
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
@ -207,7 +207,11 @@ public class UserControllerTests extends BaseTest {
@Test @Test
@Order(3) @Order(3)
public void testPageSuccess() throws Exception { public void testPageSuccess() throws Exception {
List<String> userRoleIdList = USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList()); if (CollectionUtils.isEmpty(USER_ROLE_LIST)) {
this.testGetGlobalSystemUserRoleSuccess();
}
List<String> userRoleIdList = USER_ROLE_LIST.stream().map(UserSelectOption::getId).toList();
this.checkUserList(); this.checkUserList();
BasePageRequest basePageRequest = UserParamUtils.getDefaultPageRequest(); BasePageRequest basePageRequest = UserParamUtils.getDefaultPageRequest();
@ -222,9 +226,7 @@ public class UserControllerTests extends BaseTest {
//用户组不存在非全局用户组 //用户组不存在非全局用户组
for (UserTableResponse response : userList) { for (UserTableResponse response : userList) {
if (CollectionUtils.isNotEmpty(response.getUserRoleList())) { if (CollectionUtils.isNotEmpty(response.getUserRoleList())) {
response.getUserRoleList().forEach(role -> { response.getUserRoleList().forEach(role -> Assertions.assertTrue(userRoleIdList.contains(role.getId())));
Assertions.assertTrue(userRoleIdList.contains(role.getId()));
});
} }
} }
@ -242,9 +244,7 @@ public class UserControllerTests extends BaseTest {
userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class); userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class);
for (UserTableResponse response : userList) { for (UserTableResponse response : userList) {
if (CollectionUtils.isNotEmpty(response.getUserRoleList())) { if (CollectionUtils.isNotEmpty(response.getUserRoleList())) {
response.getUserRoleList().forEach(role -> { response.getUserRoleList().forEach(role -> Assertions.assertTrue(userRoleIdList.contains(role.getId())));
Assertions.assertTrue(userRoleIdList.contains(role.getId()));
});
} }
} }
@ -264,12 +264,20 @@ public class UserControllerTests extends BaseTest {
userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class); userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class);
for (UserTableResponse response : userList) { for (UserTableResponse response : userList) {
if (CollectionUtils.isNotEmpty(response.getUserRoleList())) { if (CollectionUtils.isNotEmpty(response.getUserRoleList())) {
response.getUserRoleList().forEach(role -> { response.getUserRoleList().forEach(role -> Assertions.assertTrue(userRoleIdList.contains(role.getId())));
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 @Test
@ -278,15 +286,15 @@ public class UserControllerTests extends BaseTest {
//当前页码不大于0 //当前页码不大于0
BasePageRequest basePageRequest = new BasePageRequest(); BasePageRequest basePageRequest = new BasePageRequest();
basePageRequest.setPageSize(5); 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 //pageSize超过100
basePageRequest = UserParamUtils.getDefaultPageRequest(); basePageRequest = UserParamUtils.getDefaultPageRequest();
basePageRequest.setPageSize(250); basePageRequest.setPageSize(250);
this.requestPost(userRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); this.requestPost(UserRequestUtils.URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER);
//当前页数不大于5 //当前页数不大于5
basePageRequest = new BasePageRequest(); basePageRequest = new BasePageRequest();
basePageRequest.setCurrent(1); 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 = new BasePageRequest();
basePageRequest.setCurrent(1); basePageRequest.setCurrent(1);
@ -294,7 +302,7 @@ public class UserControllerTests extends BaseTest {
basePageRequest.setSort(new HashMap<>() {{ basePageRequest.setSort(new HashMap<>() {{
put("SELECT * FROM user", "asc"); 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 @Test
@ -309,21 +317,21 @@ public class UserControllerTests extends BaseTest {
//更改名字 //更改名字
user.setName("TEST-UPDATE"); user.setName("TEST-UPDATE");
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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); checkLog(response.getId(), OperationLogType.UPDATE);
checkDTO = this.getUserByEmail(user.getEmail()); checkDTO = this.getUserByEmail(user.getEmail());
UserParamUtils.compareUserDTO(response, checkDTO); UserParamUtils.compareUserDTO(response, checkDTO);
//更改邮箱 //更改邮箱
user.setEmail("songtianyang-test-email@12138.com"); user.setEmail("songtianyang-test-email@12138.com");
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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); checkLog(response.getId(), OperationLogType.UPDATE);
checkDTO = this.getUserByEmail(user.getEmail()); checkDTO = this.getUserByEmail(user.getEmail());
UserParamUtils.compareUserDTO(response, checkDTO); UserParamUtils.compareUserDTO(response, checkDTO);
//更改手机号 //更改手机号
user.setPhone("18511112222"); user.setPhone("18511112222");
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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); checkLog(response.getId(), OperationLogType.UPDATE);
checkDTO = this.getUserByEmail(user.getEmail()); checkDTO = this.getUserByEmail(user.getEmail());
UserParamUtils.compareUserDTO(response, checkDTO); UserParamUtils.compareUserDTO(response, checkDTO);
@ -331,13 +339,13 @@ public class UserControllerTests extends BaseTest {
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, userMaintainRequest = UserParamUtils.getUserUpdateDTO(user,
USER_ROLE_LIST.stream().filter(item -> StringUtils.equals(item.getId(), "member")).toList() 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()); checkDTO = this.getUserByEmail(user.getEmail());
checkLog(response.getId(), OperationLogType.UPDATE); checkLog(response.getId(), OperationLogType.UPDATE);
UserParamUtils.compareUserDTO(response, checkDTO); UserParamUtils.compareUserDTO(response, checkDTO);
//更改用户组(把上面的情况添加别的权限) //更改用户组(把上面的情况添加别的权限)
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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); checkLog(response.getId(), OperationLogType.UPDATE);
checkDTO = this.getUserByEmail(user.getEmail()); checkDTO = this.getUserByEmail(user.getEmail());
UserParamUtils.compareUserDTO(response, checkDTO); UserParamUtils.compareUserDTO(response, checkDTO);
@ -345,7 +353,7 @@ public class UserControllerTests extends BaseTest {
user = new UserCreateInfo(); user = new UserCreateInfo();
BeanUtils.copyBean(user, USER_LIST.get(0)); BeanUtils.copyBean(user, USER_LIST.get(0));
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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); checkLog(response.getId(), OperationLogType.UPDATE);
checkDTO = this.getUserByEmail(user.getEmail()); checkDTO = this.getUserByEmail(user.getEmail());
UserParamUtils.compareUserDTO(response, checkDTO); UserParamUtils.compareUserDTO(response, checkDTO);
@ -361,22 +369,22 @@ public class UserControllerTests extends BaseTest {
BeanUtils.copyBean(user, USER_LIST.get(0)); BeanUtils.copyBean(user, USER_LIST.get(0));
user.setName(""); user.setName("");
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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为空 //email为空
BeanUtils.copyBean(user, USER_LIST.get(0)); BeanUtils.copyBean(user, USER_LIST.get(0));
user.setEmail(""); user.setEmail("");
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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)); BeanUtils.copyBean(user, USER_LIST.get(0));
user.setEmail(""); user.setEmail("");
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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)); BeanUtils.copyBean(user, USER_LIST.get(0));
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, new ArrayList<>()); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, new ArrayList<>());
userMaintainRequest.setUserRoleIdList(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验证 // 500验证
//邮箱重复 //邮箱重复
@ -384,13 +392,13 @@ public class UserControllerTests extends BaseTest {
BeanUtils.copyBean(user, USER_LIST.get(0)); BeanUtils.copyBean(user, USER_LIST.get(0));
user.setEmail(USER_LIST.get(USER_LIST.size() - 1).getEmail()); user.setEmail(USER_LIST.get(USER_LIST.size() - 1).getEmail());
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); 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)); BeanUtils.copyBean(user, USER_LIST.get(0));
userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, userMaintainRequest = UserParamUtils.getUserUpdateDTO(user,
USER_ROLE_LIST.stream().filter(item -> !StringUtils.equals(item.getId(), "member")).toList() 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 @Test
@ -404,7 +412,7 @@ public class UserControllerTests extends BaseTest {
this.add(userInfo.getId()); this.add(userInfo.getId());
}}); }});
userChangeEnableRequest.setEnable(false); 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()) { for (String item : userChangeEnableRequest.getSelectIds()) {
checkLog(item, OperationLogType.UPDATE); checkLog(item, OperationLogType.UPDATE);
} }
@ -420,12 +428,12 @@ public class UserControllerTests extends BaseTest {
//用户不存在 //用户不存在
UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest(); UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest();
userChangeEnableRequest.setEnable(false); 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<>() {{ userChangeEnableRequest.setSelectIds(new ArrayList<>() {{
this.add("BCDEDIT"); 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(); 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)); MockMultipartFile file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath));
ExcelParseDTO<UserExcelRowDTO> userImportReportDTOByFile = userService.getUserExcelParseDTO(file); ExcelParseDTO<UserExcelRowDTO> 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);//检查返回值 UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);//检查返回值
List<UserDTO> userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 List<UserDTO> userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库
for (UserDTO item : userDTOList) { for (UserDTO item : userDTOList) {
@ -451,7 +459,7 @@ public class UserControllerTests extends BaseTest {
//导入空文件. 应当导入成功的数据为0 //导入空文件. 应当导入成功的数据为0
filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_success_empty.xlsx")).getPath(); 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)); 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; importSuccessData = 0;
errorDataIndex = new int[]{}; errorDataIndex = new int[]{};
UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);
@ -459,7 +467,7 @@ public class UserControllerTests extends BaseTest {
//文件内没有一条合格数据 应当导入成功的数据为0 //文件内没有一条合格数据 应当导入成功的数据为0
filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_error_all.xlsx")).getPath(); 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)); 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}; errorDataIndex = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); 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(); 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)); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath));
userImportReportDTOByFile = userService.getUserExcelParseDTO(file); 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; importSuccessData = 8;
errorDataIndex = new int[]{1, 7}; errorDataIndex = new int[]{1, 7};
UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); 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(); 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)); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath));
userImportReportDTOByFile = userService.getUserExcelParseDTO(file); 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}; errorDataIndex = new int[]{9, 10};
UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);
userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库
@ -491,7 +499,7 @@ public class UserControllerTests extends BaseTest {
//文件不符合规范 应当导入成功的数据为0 //文件不符合规范 应当导入成功的数据为0
filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/abcde.gif")).getPath(); 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)); 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; importSuccessData = 0;
errorDataIndex = new int[]{}; errorDataIndex = new int[]{};
UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); 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(); 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)); file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath));
userImportReportDTOByFile = userService.getUserExcelParseDTO(file); 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;//应该导入成功的数据数量 importSuccessData = 10;//应该导入成功的数据数量
errorDataIndex = new int[]{};//出错数据的行数 errorDataIndex = new int[]{};//出错数据的行数
UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);//检查返回值 UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);//检查返回值
@ -517,7 +525,7 @@ public class UserControllerTests extends BaseTest {
{ {
TableBatchProcessDTO request = new TableBatchProcessDTO(); TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectIds(Collections.singletonList("none user")); 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); 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为空 //用户组ID为空
userMaintainRequest = UserParamUtils.getUserCreateDTO( userMaintainRequest = UserParamUtils.getUserCreateDTO(
null, null,
errorUserList); errorUserList);
this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER);
//没有用户 //没有用户
userMaintainRequest = UserParamUtils.getUserCreateDTO( userMaintainRequest = UserParamUtils.getUserCreateDTO(
USER_ROLE_LIST, USER_ROLE_LIST,
null); null);
this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER);
//用户组含有null //用户组含有null
userMaintainRequest = UserParamUtils.getUserCreateDTO( userMaintainRequest = UserParamUtils.getUserCreateDTO(
USER_ROLE_LIST, USER_ROLE_LIST,
errorUserList); errorUserList);
userMaintainRequest.getUserRoleIdList().add(null); userMaintainRequest.getUserRoleIdList().add(null);
userMaintainRequest.getUserRoleIdList().add(""); 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( userMaintainRequest = UserParamUtils.getUserCreateDTO(
USER_ROLE_LIST, USER_ROLE_LIST,
@ -571,7 +579,7 @@ public class UserControllerTests extends BaseTest {
userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{
setEmail("tianyang.name.empty@126.com"); 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( userMaintainRequest = UserParamUtils.getUserCreateDTO(
USER_ROLE_LIST, USER_ROLE_LIST,
@ -580,7 +588,7 @@ public class UserControllerTests extends BaseTest {
userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{
setName("tianyang.email.empty"); 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( userMaintainRequest = UserParamUtils.getUserCreateDTO(
USER_ROLE_LIST, USER_ROLE_LIST,
@ -590,7 +598,7 @@ public class UserControllerTests extends BaseTest {
setName("用户邮箱放飞自我"); setName("用户邮箱放飞自我");
setEmail("用户邮箱放飞自我"); setEmail("用户邮箱放飞自我");
}}); }});
this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER);
/* /*
* 校验业务判断出错的反例 500 error) * 校验业务判断出错的反例 500 error)
* 需要保证数据库有正常数据 * 需要保证数据库有正常数据
@ -604,7 +612,7 @@ public class UserControllerTests extends BaseTest {
}}); }});
}}, }},
errorUserList); errorUserList);
this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); this.requestPost(UserRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER);
//含有重复的用户邮箱 //含有重复的用户邮箱
userMaintainRequest = UserParamUtils.getUserCreateDTO( userMaintainRequest = UserParamUtils.getUserCreateDTO(
USER_ROLE_LIST, USER_ROLE_LIST,
@ -615,7 +623,7 @@ public class UserControllerTests extends BaseTest {
setName("tianyang.no.error4"); setName("tianyang.no.error4");
setEmail(firstUserEmail); 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( userMaintainRequest = UserParamUtils.getUserCreateDTO(
USER_ROLE_LIST, 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 @Test
@ -640,7 +648,7 @@ public class UserControllerTests extends BaseTest {
{ {
TableBatchProcessDTO request = new TableBatchProcessDTO(); TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectIds(Collections.singletonList("admin")); 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 userExample = new UserExample();
userExample.createCriteria().andIdEqualTo("admin").andPasswordEqualTo(CodingUtil.md5("metersphere")); userExample.createCriteria().andIdEqualTo("admin").andPasswordEqualTo(CodingUtil.md5("metersphere"));
@ -657,11 +665,11 @@ public class UserControllerTests extends BaseTest {
TableBatchProcessDTO request = new TableBatchProcessDTO(); TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectIds(Collections.singletonList(userId)); request.setSelectIds(Collections.singletonList(userId));
TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(
this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), this.requestPostAndReturn(UserRequestUtils.URL_USER_RESET_PASSWORD, request),
TableBatchProcessResponse.class TableBatchProcessResponse.class
); );
Assertions.assertEquals(response.getTotalCount(), response.getSuccessCount(), 1); Assertions.assertEquals(response.getTotalCount(), response.getSuccessCount(), 1);
List<User> userList = userService.selectByIdList(Collections.singletonList(userId)); List<User> userList = userToolService.selectByIdList(Collections.singletonList(userId));
for (User checkUser : userList) { for (User checkUser : userList) {
UserExample userExample = new UserExample(); UserExample userExample = new UserExample();
userExample.createCriteria().andIdEqualTo(checkUser.getId()).andPasswordEqualTo(CodingUtil.md5(checkUser.getEmail())); 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.setExcludeIds(Collections.singletonList("admin"));
request.setSelectAll(true); request.setSelectAll(true);
TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(
this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), this.requestPostAndReturn(UserRequestUtils.URL_USER_RESET_PASSWORD, request),
TableBatchProcessResponse.class TableBatchProcessResponse.class
); );
UserExample example = new UserExample(); UserExample example = new UserExample();
@ -705,9 +713,9 @@ public class UserControllerTests extends BaseTest {
List<UserCreateInfo> last50Users = USER_LIST.subList(USER_LIST.size() - 50, USER_LIST.size()); List<UserCreateInfo> last50Users = USER_LIST.subList(USER_LIST.size() - 50, USER_LIST.size());
//测试添加角色权限 预期数据每个用户都会增加对应的权限 //测试添加角色权限 预期数据每个用户都会增加对应的权限
UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); UserRoleBatchRelationRequest 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.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).toList());
userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, null); userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, null);
//检查有权限的数据量是否一致 //检查有权限的数据量是否一致
Assertions.assertEquals( Assertions.assertEquals(
globalUserRoleRelationService.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds()).size(), 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( Assertions.assertEquals(
globalUserRoleRelationService.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds()).size(), globalUserRoleRelationService.selectByUserIdAndRuleId(request.getSelectIds(), request.getRoleIds()).size(),
@ -742,25 +750,25 @@ public class UserControllerTests extends BaseTest {
// 用户ID为空 // 用户ID为空
UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest();
request.setSelectIds(new ArrayList<>()); request.setSelectIds(new ArrayList<>());
request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).toList());
userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); userRequestUtils.requestPost(UserRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER);
// 角色id为空 // 角色id为空
request = new UserRoleBatchRelationRequest(); 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<>()); 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含有不存在的 // 用户ID含有不存在的
request = new UserRoleBatchRelationRequest(); request = new UserRoleBatchRelationRequest();
request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); 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"); 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含有不存在的 // 角色ID含有不存在的
request = new UserRoleBatchRelationRequest(); 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.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList()));
request.getRoleIds().add("none role"); 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 @Test
@ -769,20 +777,20 @@ public class UserControllerTests extends BaseTest {
config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED), config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED),
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public void testGetProjectAndOrganization() throws Exception { 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); ResultHolder rh = JSON.parseObject(str, ResultHolder.class);
List<UserTreeSelectOption> userTreeSelectOptions = JSON.parseArray( List<BaseTreeNode> userTreeSelectOptions = JSON.parseArray(
JSON.toJSONString(rh.getData()), JSON.toJSONString(rh.getData()),
UserTreeSelectOption.class); BaseTreeNode.class);
//返回值不为空 //返回值不为空
Assertions.assertTrue(CollectionUtils.isNotEmpty(userTreeSelectOptions)); Assertions.assertTrue(CollectionUtils.isNotEmpty(userTreeSelectOptions));
PROJECT_LIST.addAll(userTreeSelectOptions);
userTreeSelectOptions.forEach(item -> PROJECT_LIST.addAll(item.getChildren()));
List<UserSelectOption> userSelectOptions = JSON.parseArray( List<UserSelectOption> userSelectOptions = JSON.parseArray(
JSON.toJSONString( JSON.toJSONString(
JSON.parseObject( 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()), ResultHolder.class).getData()),
UserSelectOption.class); UserSelectOption.class);
ORG_LIST.addAll(userSelectOptions); ORG_LIST.addAll(userSelectOptions);
@ -799,14 +807,12 @@ public class UserControllerTests extends BaseTest {
this.testGetProjectAndOrganization(); this.testGetProjectAndOrganization();
} }
List<String> userIds = this.selectUserTableIds(50); List<String> userIds = this.selectUserTableIds(100);
UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest();
request.setSelectIds(userIds); request.setSelectIds(userIds);
request.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); request.setRoleIds(PROJECT_LIST.stream().map(BaseTreeNode::getId).toList());
//排除树结构中的组织ID this.requestPostWithOk(UserRequestUtils.URL_ADD_PROJECT_MEMBER, request);
request.getRoleIds().removeAll(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList()));
this.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, request);
//检查有权限的数据量是否一致 //检查有权限的数据量是否一致
UserRoleRelationExample checkExample = new UserRoleRelationExample(); UserRoleRelationExample checkExample = new UserRoleRelationExample();
@ -850,8 +856,8 @@ public class UserControllerTests extends BaseTest {
UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest();
request.setSelectIds(userIds); request.setSelectIds(userIds);
request.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); request.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).toList());
this.requestPostWithOk(userRequestUtils.URL_ADD_ORGANIZATION_MEMBER, request); this.requestPostWithOk(UserRequestUtils.URL_ADD_ORGANIZATION_MEMBER, request);
//检查有权限的数据量是否一致 //检查有权限的数据量是否一致
UserRoleRelationExample checkExample = new UserRoleRelationExample(); UserRoleRelationExample checkExample = new UserRoleRelationExample();
for (String orgId : request.getRoleIds()) { for (String orgId : request.getRoleIds()) {
@ -881,45 +887,45 @@ public class UserControllerTests extends BaseTest {
// 用户ID为空 // 用户ID为空
UserRoleBatchRelationRequest addToProjectRequest = new UserRoleBatchRelationRequest(); UserRoleBatchRelationRequest addToProjectRequest = new UserRoleBatchRelationRequest();
addToProjectRequest.setSelectIds(new ArrayList<>()); addToProjectRequest.setSelectIds(new ArrayList<>());
addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(BaseTreeNode::getId).toList());
userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER);
// 项目为空 // 项目为空
addToProjectRequest = new UserRoleBatchRelationRequest(); 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<>()); 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含有不存在的 // 用户ID含有不存在的
addToProjectRequest = new UserRoleBatchRelationRequest(); addToProjectRequest = new UserRoleBatchRelationRequest();
addToProjectRequest.setSelectIds(Collections.singletonList("none user")); addToProjectRequest.setSelectIds(Collections.singletonList("none user"));
addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(BaseTreeNode::getId).toList());
userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER);
// 项目ID含有不存在的 // 项目ID含有不存在的
addToProjectRequest = new UserRoleBatchRelationRequest(); 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")); 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为空 // 用户ID为空
UserRoleBatchRelationRequest orgRequest = new UserRoleBatchRelationRequest(); UserRoleBatchRelationRequest orgRequest = new UserRoleBatchRelationRequest();
orgRequest.setSelectIds(new ArrayList<>()); orgRequest.setSelectIds(new ArrayList<>());
orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).toList());
userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER);
// 项目为空 // 项目为空
orgRequest = new UserRoleBatchRelationRequest(); 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<>()); 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含有不存在的 // 用户ID含有不存在的
orgRequest = new UserRoleBatchRelationRequest(); orgRequest = new UserRoleBatchRelationRequest();
orgRequest.setSelectIds(Collections.singletonList("none user")); orgRequest.setSelectIds(Collections.singletonList("none user"));
orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).toList());
userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); userRequestUtils.requestPost(UserRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER);
// 项目ID含有不存在的 // 项目ID含有不存在的
orgRequest = new UserRoleBatchRelationRequest(); 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")); 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(); this.checkUserList();
//删除USER_LIST用户 //删除USER_LIST用户
TableBatchProcessDTO request = new TableBatchProcessDTO(); 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( 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.getTotalCount());
Assertions.assertEquals(request.getSelectIds().size(), response.getSuccessCount()); Assertions.assertEquals(request.getSelectIds().size(), response.getSuccessCount());
//检查数据库 //检查数据库
@ -950,21 +956,21 @@ public class UserControllerTests extends BaseTest {
public void testUserDeleteError() throws Exception { public void testUserDeleteError() throws Exception {
//参数为空 //参数为空
TableBatchProcessDTO request = new TableBatchProcessDTO(); 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")); 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)) { if (CollectionUtils.isEmpty(DELETED_USER_ID_LIST)) {
this.testUserDeleteSuccess(); this.testUserDeleteSuccess();
} }
request.setSelectIds(DELETED_USER_ID_LIST); 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用户 //测试包含Admin用户
request = new TableBatchProcessDTO(); request = new TableBatchProcessDTO();
request.setSelectAll(true); 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); Pager<?> returnPager = userRequestUtils.selectUserPage(basePageRequest);
//用户组不存在非全局用户组 //用户组不存在非全局用户组
List<UserTableResponse> userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class); List<UserTableResponse> 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();
} }
//成功入库的用户保存内存中其他用例会使用到 //成功入库的用户保存内存中其他用例会使用到

View File

@ -15,6 +15,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets; 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.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -73,6 +74,7 @@ public class UserRequestUtils {
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(param)) .content(JSON.toJSONString(param))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(resultMatcher) .andExpect(resultMatcher)
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); .andExpect(content().contentType(MediaType.APPLICATION_JSON));
} }