From f5de73cd73d1c4012244cf2941e5a2d1fc40af9d Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 12 Jun 2023 18:47:07 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=A1=A8=E6=A0=BC=E6=9F=A5=E8=AF=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/domain/UserRoleRelation.java | 14 ++- .../domain/UserRoleRelationExample.java | 58 +++++---- .../system/mapper/UserRoleRelationMapper.xml | 28 ++--- .../3.0.0/ddl/V3.0.0_11__system_ddl.sql | 27 ++-- .../migration/3.0.0/dml/V3.0.0_11_1__data.sql | 2 +- .../metersphere/sdk/dto/BasePageRequest.java | 26 +++- .../sdk/mapper/BaseUserMapper.java | 2 + .../metersphere/sdk/mapper/BaseUserMapper.xml | 8 ++ .../system/controller/UserController.java | 29 ++++- ...inRequest.java => UserBatchCreateDTO.java} | 15 ++- .../system/dto/response/UserInfo.java | 17 +++ .../service/UserRoleRelationService.java | 71 +++++++++++ .../system/service/UserService.java | 35 ++++-- .../system/utils/BatchProcessUtils.java | 6 + .../controller/UserControllerTests.java | 117 +++++++++++++----- .../system/utils/UserTestUtils.java | 38 ++++-- 16 files changed, 376 insertions(+), 117 deletions(-) rename backend/services/system-setting/src/main/java/io/metersphere/system/dto/{UserMaintainRequest.java => UserBatchCreateDTO.java} (67%) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserInfo.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/utils/BatchProcessUtils.java diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelation.java b/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelation.java index 82090eebc3..4dfc80997c 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelation.java +++ b/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelation.java @@ -1,15 +1,17 @@ package io.metersphere.system.domain; -import io.metersphere.validation.groups.*; +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; import java.io.Serializable; import lombok.Data; @Data public class UserRoleRelation implements Serializable { @Schema(title = "用户组关系ID", requiredMode = Schema.RequiredMode.REQUIRED) - @NotBlank(message = "{user_role_relation.id.not_blank}", groups = {Updated.class}) + @NotBlank(message = "{user_role_relation.id.not_blank}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 50, message = "{user_role_relation.id.length_range}", groups = {Created.class, Updated.class}) private String id; @@ -31,8 +33,10 @@ public class UserRoleRelation implements Serializable { @Schema(title = "创建时间") private Long createTime; - @Schema(title = "更新时间") - private Long updateTime; + @Schema(title = "创建人", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{user_role_relation.create_user.not_blank}", groups = {Created.class}) + @Size(min = 1, max = 50, message = "{user_role_relation.create_user.length_range}", groups = {Created.class, Updated.class}) + private String createUser; private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelationExample.java b/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelationExample.java index 5743834197..e9ceff5489 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelationExample.java +++ b/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserRoleRelationExample.java @@ -444,63 +444,73 @@ public class UserRoleRelationExample { return (Criteria) this; } - public Criteria andUpdateTimeIsNull() { - addCriterion("update_time is null"); + public Criteria andCreateUserIsNull() { + addCriterion("create_user is null"); return (Criteria) this; } - public Criteria andUpdateTimeIsNotNull() { - addCriterion("update_time is not null"); + public Criteria andCreateUserIsNotNull() { + addCriterion("create_user is not null"); return (Criteria) this; } - public Criteria andUpdateTimeEqualTo(Long value) { - addCriterion("update_time =", value, "updateTime"); + public Criteria andCreateUserEqualTo(String value) { + addCriterion("create_user =", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeNotEqualTo(Long value) { - addCriterion("update_time <>", value, "updateTime"); + public Criteria andCreateUserNotEqualTo(String value) { + addCriterion("create_user <>", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeGreaterThan(Long value) { - addCriterion("update_time >", value, "updateTime"); + public Criteria andCreateUserGreaterThan(String value) { + addCriterion("create_user >", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeGreaterThanOrEqualTo(Long value) { - addCriterion("update_time >=", value, "updateTime"); + public Criteria andCreateUserGreaterThanOrEqualTo(String value) { + addCriterion("create_user >=", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeLessThan(Long value) { - addCriterion("update_time <", value, "updateTime"); + public Criteria andCreateUserLessThan(String value) { + addCriterion("create_user <", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeLessThanOrEqualTo(Long value) { - addCriterion("update_time <=", value, "updateTime"); + public Criteria andCreateUserLessThanOrEqualTo(String value) { + addCriterion("create_user <=", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeIn(List values) { - addCriterion("update_time in", values, "updateTime"); + public Criteria andCreateUserLike(String value) { + addCriterion("create_user like", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeNotIn(List values) { - addCriterion("update_time not in", values, "updateTime"); + public Criteria andCreateUserNotLike(String value) { + addCriterion("create_user not like", value, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeBetween(Long value1, Long value2) { - addCriterion("update_time between", value1, value2, "updateTime"); + public Criteria andCreateUserIn(List values) { + addCriterion("create_user in", values, "createUser"); return (Criteria) this; } - public Criteria andUpdateTimeNotBetween(Long value1, Long value2) { - addCriterion("update_time not between", value1, value2, "updateTime"); + public Criteria andCreateUserNotIn(List values) { + addCriterion("create_user not in", values, "createUser"); + return (Criteria) this; + } + + public Criteria andCreateUserBetween(String value1, String value2) { + addCriterion("create_user between", value1, value2, "createUser"); + return (Criteria) this; + } + + public Criteria andCreateUserNotBetween(String value1, String value2) { + addCriterion("create_user not between", value1, value2, "createUser"); return (Criteria) this; } } diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserRoleRelationMapper.xml b/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserRoleRelationMapper.xml index ef148d35b2..515de8af30 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserRoleRelationMapper.xml +++ b/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserRoleRelationMapper.xml @@ -7,7 +7,7 @@ - + @@ -68,7 +68,7 @@ - id, user_id, role_id, source_id, create_time, update_time + id, user_id, role_id, source_id, create_time, create_user + \ No newline at end of file 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 c5af493cea..ed1469a300 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 @@ -1,17 +1,27 @@ package io.metersphere.system.controller; +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.dto.UserDTO; +import io.metersphere.sdk.util.PageUtils; +import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.SessionUtils; -import io.metersphere.system.dto.UserMaintainRequest; +import io.metersphere.system.dto.UserBatchCreateDTO; +import io.metersphere.system.dto.response.UserInfo; import io.metersphere.system.service.UserService; import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; import jakarta.annotation.Resource; +import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequestMapping("/user") public class UserController { @@ -26,8 +36,23 @@ public class UserController { @PostMapping("/add") @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_ADD) - public UserMaintainRequest addUser(@Validated({Created.class}) @RequestBody UserMaintainRequest userCreateDTO) { + public UserBatchCreateDTO addUser(@Validated({Created.class}) @RequestBody UserBatchCreateDTO userCreateDTO) { userCreateDTO.setCreateUserToList(SessionUtils.getSessionId()); return userService.add(userCreateDTO); } + + @PostMapping("/update") + @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE) + public UserBatchCreateDTO updateUser(@Validated({Updated.class}) @RequestBody UserBatchCreateDTO userCreateDTO) { + userCreateDTO.setCreateUserToList(SessionUtils.getSessionId()); + return userService.add(userCreateDTO); + } + + @PostMapping("/page") + @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_ADD) + public Pager> list(@Validated @RequestBody BasePageRequest request) { + Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), + StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc"); + return PageUtils.setPageInfo(page, userService.list(request)); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserMaintainRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserBatchCreateDTO.java similarity index 67% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserMaintainRequest.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserBatchCreateDTO.java index 1deaefbb43..804a5c7b0b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserMaintainRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserBatchCreateDTO.java @@ -12,16 +12,19 @@ import lombok.Data; import java.util.List; @Data -public class UserMaintainRequest { +public class UserBatchCreateDTO { + @Schema(title = "用户信息集合", requiredMode = Schema.RequiredMode.REQUIRED) @NotEmpty(groups = {Created.class, Updated.class}, message = "{user.info.not_empty}") List<@Valid User> userInfoList; + @Schema(title = "组织Id", requiredMode = Schema.RequiredMode.REQUIRED) - @NotBlank(groups = {Created.class, Updated.class}, message = "{user.organizationId.not_blank}") - String organizationId; - @Schema(title = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) - @NotBlank(groups = {Created.class, Updated.class}, message = "{user.projectId.not_blank}") - String projectId; + @NotEmpty(groups = {Created.class, Updated.class}, message = "{user.organizationId.not_blank}") + List<@Valid @NotBlank(message = "{user.organizationId.not_blank}", groups = {Created.class, Updated.class}) String> organizationIdList; + + @Schema(title = "用户组", requiredMode = Schema.RequiredMode.REQUIRED) + @NotEmpty(groups = {Created.class, Updated.class}, message = "{user_role.id.not_blank}") + List<@Valid @NotBlank(message = "{user_role.id.not_blank}", groups = {Created.class, Updated.class}) String> userRoleIdList; public void setCreateUserToList(String userSessionId) { userInfoList.forEach(user -> { diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserInfo.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserInfo.java new file mode 100644 index 0000000000..99c2db190e --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserInfo.java @@ -0,0 +1,17 @@ +package io.metersphere.system.dto.response; + +import io.metersphere.system.domain.Organization; +import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserRole; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +@Data +public class UserInfo extends User { + @Schema(title = "用户所属组织") + List organizationList; + @Schema(title = "用户所属用户组") + List userRoleList; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java new file mode 100644 index 0000000000..def5751723 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java @@ -0,0 +1,71 @@ +package io.metersphere.system.service; + +import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserRoleRelation; +import io.metersphere.system.mapper.UserRoleRelationMapper; +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +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.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.annotation.Validated; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Service +@Transactional(rollbackFor = Exception.class) +public class UserRoleRelationService { + @Resource + private SqlSessionFactory sqlSessionFactory; + private UserRoleRelationMapper userRoleRelationMapper; + + public void batchSave(@Validated({Created.class, Updated.class}) + @NotEmpty(groups = {Created.class, Updated.class}, message = "{user.organizationId.not_blank}") + List<@Valid @NotBlank(message = "{user.organizationId.not_blank}", groups = {Created.class, Updated.class}) String> organizationIdList, + @NotEmpty(groups = {Created.class, Updated.class}, message = "{user_role.id.not_blank}") + List<@Valid @NotBlank(message = "{user_role.id.not_blank}", groups = {Created.class, Updated.class}) String> userRoleIdList, + @NotEmpty(groups = {Created.class, Updated.class}, message = "{user.info.not_empty}") + List<@Valid User> userInfoList) { + long operationTime = System.currentTimeMillis(); + List userRoleRelationSaveList = new ArrayList<>(); + //添加用户组织关系 + for (String orgId : organizationIdList) { + for (String userRoleId : userRoleIdList) { + for (User user : userInfoList) { + UserRoleRelation userRoleRelation = new UserRoleRelation(); + userRoleRelation.setId(UUID.randomUUID().toString()); + userRoleRelation.setUserId(user.getId()); + userRoleRelation.setRoleId(userRoleId); + userRoleRelation.setSourceId(orgId); + userRoleRelation.setCreateTime(operationTime); + userRoleRelation.setCreateUser(user.getCreateUser()); + userRoleRelationSaveList.add(userRoleRelation); + } + } + } + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + UserRoleRelationMapper batchSaveMapper = sqlSession.getMapper(UserRoleRelationMapper.class); + try { + int insertIndex = 0; + for (UserRoleRelation userRoleRelation : userRoleRelationSaveList) { + batchSaveMapper.insert(userRoleRelation); + insertIndex++; + if (insertIndex % 50 == 0) { + sqlSession.flushStatements(); + } + } + sqlSession.flushStatements(); + } finally { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } + } +} 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 8f43d1df60..5785f2acd8 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 @@ -1,16 +1,18 @@ package io.metersphere.system.service; +import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.mapper.BaseUserMapper; +import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.CodingUtil; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.User; -import io.metersphere.system.dto.UserMaintainRequest; +import io.metersphere.system.dto.UserBatchCreateDTO; +import io.metersphere.system.dto.response.UserInfo; import io.metersphere.system.mapper.UserMapper; import jakarta.annotation.Resource; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,6 +28,8 @@ public class UserService { private BaseUserMapper baseUserMapper; @Resource private UserMapper userMapper; + @Resource + private UserRoleRelationService userRoleRelationService; private void validateUserInfo(List userList) { //判断参数内是否含有重复邮箱 @@ -46,24 +50,31 @@ public class UserService { } } } - if (CollectionUtils.isNotEmpty(repeatEmailList)) { throw new MSException(Translator.get("user.email.repeat", repeatEmailList.toString())); } } - public UserMaintainRequest add(UserMaintainRequest userCreateDTO) { + private void validateOrgAndProject(List orgIdList, List userRoleIdList) { + // todo 判断组织和用户组是否合法 + } + + public UserBatchCreateDTO add(UserBatchCreateDTO userCreateDTO) { this.validateUserInfo(userCreateDTO.getUserInfoList()); + this.validateOrgAndProject(userCreateDTO.getOrganizationIdList(), userCreateDTO.getUserRoleIdList()); long createTime = System.currentTimeMillis(); + //添加用户 for (User user : userCreateDTO.getUserInfoList()) { user.setId(UUID.randomUUID().toString()); - if (StringUtils.isEmpty(user.getPassword())) { - user.setPassword(CodingUtil.md5(user.getEmail())); - } + user.setPassword(CodingUtil.md5(user.getEmail())); user.setCreateTime(createTime); user.setUpdateTime(createTime); userMapper.insertSelective(user); } + + userRoleRelationService.batchSave( + userCreateDTO.getOrganizationIdList(), userCreateDTO.getUserRoleIdList(), userCreateDTO.getUserInfoList()); + return userCreateDTO; } @@ -72,4 +83,14 @@ public class UserService { return baseUserMapper.selectByEmail(email); } + public List list(BasePageRequest request) { + List returnList = new ArrayList<>(); + List userList = baseUserMapper.selectByKeyword(request.getKeyword()); + for (User user : userList) { + UserInfo userInfo = new UserInfo(); + BeanUtils.copyBean(userInfo, user); + returnList.add(userInfo); + } + return returnList; + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/utils/BatchProcessUtils.java b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/BatchProcessUtils.java new file mode 100644 index 0000000000..fe0e9cbf69 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/BatchProcessUtils.java @@ -0,0 +1,6 @@ +package io.metersphere.system.utils; + +//批量处理类 +public class BatchProcessUtils { + private static int BATCH_SIZE = 100; +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java index d5431cbca7..00354eff92 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java @@ -1,12 +1,16 @@ package io.metersphere.system.controller; import com.jayway.jsonpath.JsonPath; +import io.metersphere.api.domain.ApiDefinition; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.controller.handler.ResultHolder; +import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.Pager; import io.metersphere.system.domain.User; -import io.metersphere.system.dto.UserMaintainRequest; +import io.metersphere.system.dto.UserBatchCreateDTO; +import io.metersphere.system.dto.response.UserInfo; import io.metersphere.system.utils.UserTestUtils; import io.metersphere.utils.JsonUtils; import jakarta.annotation.Resource; @@ -22,6 +26,7 @@ import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -39,6 +44,13 @@ public class UserControllerTests { private static String sessionId; private static String csrfToken; + private static final String URL_USER_CREATE = "/user/add"; + private static final String URL_USER_GET = "/user/get/"; + private static final String URL_USER_PAGE = "/user/page"; + + private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest(); + private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); + private static final List USER_LIST = new ArrayList<>(); //成功入库的用户保存内存中,其他用例会使用到 @@ -49,7 +61,7 @@ public class UserControllerTests { //返回请求正常 Assertions.assertNotNull(resultHolder); - UserMaintainRequest userMaintainRequest = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), UserMaintainRequest.class); + UserBatchCreateDTO userMaintainRequest = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), UserBatchCreateDTO.class); //返回值不为空 Assertions.assertNotNull(userMaintainRequest); @@ -110,10 +122,9 @@ public class UserControllerTests { @Test @Order(1) public void testAddSuccess() throws Exception { - String url = "/user/add"; //模拟前台批量添加用户 - UserMaintainRequest userMaintainRequest = UserTestUtils.getSimpleUserCreateDTO(); - MvcResult mvcResult = this.responsePost(url, userMaintainRequest); + UserBatchCreateDTO userMaintainRequest = UserTestUtils.getSimpleUserCreateDTO(); + MvcResult mvcResult = this.responsePost(URL_USER_CREATE, userMaintainRequest); this.addUser2List(mvcResult); //含有重复的用户名称 @@ -133,49 +144,57 @@ public class UserControllerTests { }}); }} ); - mvcResult = this.responsePost(url, userMaintainRequest); + mvcResult = this.responsePost(URL_USER_CREATE, userMaintainRequest); this.addUser2List(mvcResult); } @Test @Order(2) public void testAddError() throws Exception { - String url = "/user/add"; - UserMaintainRequest userMaintainRequest; - boolean projectIsEmpty = true; + UserBatchCreateDTO userMaintainRequest; + boolean roleIsEmpty = true; boolean organizationIsEmpty = true; boolean userIsEmpty = true; /* * 校验参数不合法的反例 * 每一次校验,使用getErrorUserCreateDTO方法重新获取参数,避免上一步的参数干扰 */ - ResultMatcher resultMatcher = status().isBadRequest(); //所有参数都为空 - userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(organizationIsEmpty, projectIsEmpty, userIsEmpty); - this.requestPost(url, userMaintainRequest, resultMatcher); + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(organizationIsEmpty, roleIsEmpty, userIsEmpty); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //组织ID为空 - userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(organizationIsEmpty, !projectIsEmpty, !userIsEmpty); - this.requestPost(url, userMaintainRequest, resultMatcher); - //项目ID为空 - userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, projectIsEmpty, !userIsEmpty); - this.requestPost(url, userMaintainRequest, resultMatcher); + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(organizationIsEmpty, !roleIsEmpty, !userIsEmpty); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //用户组ID为空 + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, roleIsEmpty, !userIsEmpty); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //没有用户 - userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !projectIsEmpty, userIsEmpty); - this.requestPost(url, userMaintainRequest, resultMatcher); + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !roleIsEmpty, userIsEmpty); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //含有不存在的组织 + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !roleIsEmpty, !userIsEmpty); + userMaintainRequest.getOrganizationIdList().add(null); + userMaintainRequest.getOrganizationIdList().add(""); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //含有不存在的用户组 + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !roleIsEmpty, !userIsEmpty); + userMaintainRequest.getUserRoleIdList().add(null); + userMaintainRequest.getUserRoleIdList().add(""); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //含有用户名称为空的数据 userMaintainRequest = UserTestUtils.getSimpleUserCreateDTO(); userMaintainRequest.getUserInfoList().add(new User() {{ setEmail("tianyang.name.empty@126.com"); setSource("LOCAL"); }}); - this.requestPost(url, userMaintainRequest, resultMatcher); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //含有用户邮箱为空的数据 userMaintainRequest = UserTestUtils.getSimpleUserCreateDTO(); userMaintainRequest.getUserInfoList().add(new User() {{ setName("tianyang.email.empty"); setSource("LOCAL"); }}); - this.requestPost(url, userMaintainRequest, resultMatcher); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //用户邮箱不符合标准 userMaintainRequest = UserTestUtils.getSimpleUserCreateDTO(); userMaintainRequest.getUserInfoList().add(new User() {{ @@ -183,32 +202,31 @@ public class UserControllerTests { setEmail("用户邮箱放飞自我"); setSource("LOCAL"); }}); - this.requestPost(url, userMaintainRequest, resultMatcher); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); //用户来源为空 userMaintainRequest = UserTestUtils.getSimpleUserCreateDTO(); userMaintainRequest.getUserInfoList().add(new User() {{ setName("tianyang.source.empty"); setEmail("tianyang.source.empty@126.com"); }}); - this.requestPost(url, userMaintainRequest, resultMatcher); + this.requestPost(URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); /* * 校验业务判断出错的反例 (500 error) * 需要保证数据库有正常数据 */ - resultMatcher = status().is5xxServerError(); this.checkUserList(); //含有重复的用户邮箱 - userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !projectIsEmpty, !userIsEmpty); + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !roleIsEmpty, !userIsEmpty); String firstUserEmail = userMaintainRequest.getUserInfoList().get(0).getEmail(); userMaintainRequest.getUserInfoList().add(new User() {{ setName("tianyang.no.error4"); setEmail(firstUserEmail); setSource("LOCAL"); }}); - this.requestPost(url, userMaintainRequest, resultMatcher); + this.requestPost(URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); //测试请求参数中含有数据库中已存在的邮箱情况 - userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !projectIsEmpty, userIsEmpty); + userMaintainRequest = UserTestUtils.getErrorUserCreateDTO(!organizationIsEmpty, !roleIsEmpty, userIsEmpty); userMaintainRequest.setUserInfoList( new ArrayList<>() {{ add(new User() {{ @@ -218,14 +236,14 @@ public class UserControllerTests { }}); }} ); - this.requestPost(url, userMaintainRequest, resultMatcher); + this.requestPost(URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); } @Test @Order(3) public void testGetByEmailSuccess() throws Exception { this.checkUserList(); - String url = "/user/get/" + UserTestUtils.USER_DEFAULT_EMAIL; + String url = URL_USER_GET + UserTestUtils.USER_DEFAULT_EMAIL; MvcResult mvcResult = this.responseGet(url); String returnData = mvcResult.getResponse().getContentAsString(); @@ -248,7 +266,7 @@ public class UserControllerTests { public void testGetByEmailError() throws Exception { //测试使用任意参数,不能获取到任何用户信息 this.checkUserList(); - String url = "/user/get/" + UUID.randomUUID(); + String url = URL_USER_GET + UUID.randomUUID(); MvcResult mvcResult = this.responseGet(url); String returnData = mvcResult.getResponse().getContentAsString(); @@ -258,4 +276,43 @@ public class UserControllerTests { //返回值为空 Assertions.assertNull(resultHolder.getData()); } + + @Test + @Order(4) + public void testPageSuccess() throws Exception { + BasePageRequest basePageRequest = UserTestUtils.getDefaultPageRequest(); + MvcResult mvcResult = this.responsePost(URL_USER_PAGE, basePageRequest); + String returnData = mvcResult.getResponse().getContentAsString(); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + //返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager returnPager = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + //返回值不为空 + Assertions.assertNotNull(returnPager); + //返回值的页码和当前页码相同 + Assertions.assertEquals(returnPager.getCurrent(), basePageRequest.getCurrent()); + //返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(((List) returnPager.getList()).size() <= basePageRequest.getPageSize()); + } + + @Test + @Order(4) + public void testPageError() throws Exception { + //当前页码不大于0 + BasePageRequest basePageRequest = new BasePageRequest(); + basePageRequest.setPageSize(5); + this.requestPost(URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); + //当前页数不大于5 + basePageRequest = new BasePageRequest(); + basePageRequest.setCurrent(1); + this.requestPost(URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); + //排序字段不合法 + basePageRequest = new BasePageRequest(); + basePageRequest.setCurrent(1); + basePageRequest.setPageSize(5); + basePageRequest.setSort(new HashMap<>() {{ + put("SELECT * FROM user", "asc"); + }}); + this.requestPost(URL_USER_PAGE, basePageRequest, BAD_REQUEST_MATCHER); + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java index 0ce2c76795..81951d9a8d 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java @@ -1,7 +1,8 @@ package io.metersphere.system.utils; +import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.system.domain.User; -import io.metersphere.system.dto.UserMaintainRequest; +import io.metersphere.system.dto.UserBatchCreateDTO; import java.util.ArrayList; @@ -10,12 +11,16 @@ public class UserTestUtils { 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 ORGANIZATION_ID = "ms-organization"; - public static final String PROJECT_ID = "ms-project"; + public static final String USER_ROLE_ID = "ms-user-role"; - public static UserMaintainRequest getSimpleUserCreateDTO() { - UserMaintainRequest userMaintainRequest = new UserMaintainRequest(); - userMaintainRequest.setOrganizationId(ORGANIZATION_ID); - userMaintainRequest.setProjectId(PROJECT_ID); + public static UserBatchCreateDTO getSimpleUserCreateDTO() { + UserBatchCreateDTO userMaintainRequest = new UserBatchCreateDTO(); + userMaintainRequest.setOrganizationIdList(new ArrayList<>() {{ + add(ORGANIZATION_ID); + }}); + userMaintainRequest.setUserRoleIdList(new ArrayList<>() {{ + add(USER_ROLE_ID); + }}); userMaintainRequest.setUserInfoList(new ArrayList<>() {{ add(new User() {{ setName(USER_DEFAULT_NAME); @@ -31,13 +36,17 @@ public class UserTestUtils { return userMaintainRequest; } - public static UserMaintainRequest getErrorUserCreateDTO(boolean organizationIsEmpty, boolean projectIsEmpty, boolean userIsEmpty) { - UserMaintainRequest userMaintainRequest = new UserMaintainRequest(); + public static UserBatchCreateDTO getErrorUserCreateDTO(boolean organizationIsEmpty, boolean roleIsEmpty, boolean userIsEmpty) { + UserBatchCreateDTO userMaintainRequest = new UserBatchCreateDTO(); if (!organizationIsEmpty) { - userMaintainRequest.setOrganizationId(ORGANIZATION_ID); + userMaintainRequest.setOrganizationIdList(new ArrayList<>() {{ + add(ORGANIZATION_ID); + }}); } - if (!projectIsEmpty) { - userMaintainRequest.setProjectId(PROJECT_ID); + if (!roleIsEmpty) { + userMaintainRequest.setUserRoleIdList(new ArrayList<>() {{ + add(USER_ROLE_ID); + }}); } if (!userIsEmpty) { userMaintainRequest.setUserInfoList(new ArrayList<>() {{ @@ -55,4 +64,11 @@ public class UserTestUtils { } return userMaintainRequest; } + + public static BasePageRequest getDefaultPageRequest() { + return new BasePageRequest() {{ + this.setCurrent(1); + this.setPageSize(10); + }}; + } }