diff --git a/pom.xml b/pom.xml
index 949978260..532f7e200 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,7 +46,12 @@
2.2.7
2.2
1.0.5
+ 7.2.6.RELEASE
+ 0.1.16
+ 4.5.18
+ 2.1.0
+ 1.2.7
@@ -213,14 +218,14 @@
com.github.fppt
jedis-mock
- 0.1.16
+ ${jedis-mock.version}
test
uk.co.jemos.podam
podam
- 7.2.6.RELEASE
+ ${podam.version}
test
@@ -278,18 +283,18 @@
com.yunpian.sdk
yunpian-java-sdk
- 1.2.7
+ ${yunpian-java-sdk.version}
com.aliyun
aliyun-java-sdk-core
- 4.5.18
+ ${aliyun-java-sdk-core.version}
com.aliyun
aliyun-java-sdk-dysmsapi
- 2.1.0
+ ${aliyun-java-sdk-dysmsapi.version}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java
index 17dcff200..dcfce4d5b 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java
@@ -149,9 +149,9 @@ public class SysUserController {
// 手动创建导出 demo
List list = Arrays.asList(
SysUserImportExcelVO.builder().username("yudao").deptId(1L).email("yudao@iocoder.cn").mobile("15601691300")
- .nickname("芋道").status(CommonStatusEnum.ENABLE.getStatus()).sex(SysSexEnum.MALE.getSEX()).build(),
+ .nickname("芋道").status(CommonStatusEnum.ENABLE.getStatus()).sex(SysSexEnum.MALE.getSex()).build(),
SysUserImportExcelVO.builder().username("yuanma").deptId(2L).email("yuanma@iocoder.cn").mobile("15601701300")
- .nickname("源码").status(CommonStatusEnum.DISABLE.getStatus()).sex(SysSexEnum.FEMALE.getSEX()).build()
+ .nickname("源码").status(CommonStatusEnum.DISABLE.getStatus()).sex(SysSexEnum.FEMALE.getSex()).build()
);
// 输出
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/profile/SysUserProfileUpdateReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/profile/SysUserProfileUpdateReqVO.java
index 1f02e553f..95051f0a1 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/profile/SysUserProfileUpdateReqVO.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/profile/SysUserProfileUpdateReqVO.java
@@ -3,9 +3,9 @@ package cn.iocoder.dashboard.modules.system.controller.user.vo.profile;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
+import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Email;
-import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
@ApiModel("用户个人信息更新 Request VO")
@@ -22,7 +22,7 @@ public class SysUserProfileUpdateReqVO {
private String email;
@ApiModelProperty(value = "手机号码", example = "15601691300")
- @Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "手机号码格式错误")
+ @Length(min = 11, max = 11, message = "手机号长度必须 11 位")
private String mobile;
@ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SysSexEnum 枚举类")
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserBaseVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserBaseVO.java
index 7361bb342..4fe0f9b15 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserBaseVO.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserBaseVO.java
@@ -2,10 +2,10 @@ package cn.iocoder.dashboard.modules.system.controller.user.vo.user;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
+import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
-import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Set;
@@ -40,7 +40,7 @@ public class SysUserBaseVO {
private String email;
@ApiModelProperty(value = "手机号码", example = "15601691300")
- @Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "手机号码格式错误")
+ @Length(min = 11, max = 11, message = "手机号长度必须 11 位")
private String mobile;
@ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SysSexEnum 枚举类")
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/enums/common/SysSexEnum.java b/src/main/java/cn/iocoder/dashboard/modules/system/enums/common/SysSexEnum.java
index fb9bfb588..63dec921c 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/enums/common/SysSexEnum.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/enums/common/SysSexEnum.java
@@ -3,6 +3,11 @@ package cn.iocoder.dashboard.modules.system.enums.common;
import lombok.AllArgsConstructor;
import lombok.Getter;
+/**
+ * 性别的枚举值
+ *
+ * @author 芋道源码
+ */
@Getter
@AllArgsConstructor
public enum SysSexEnum {
@@ -10,6 +15,9 @@ public enum SysSexEnum {
MALE(1), // 男
FEMALE(2); // 女
- private final Integer SEX;
+ /**
+ * 性别
+ */
+ private final Integer sex;
}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java
index 6fefbbdb0..334d0f05f 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java
@@ -6,17 +6,11 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.common.exception.ServiceException;
-import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.infra.service.file.InfFileService;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
-import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO;
-import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO;
-import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO;
-import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportRespVO;
-import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO;
-import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO;
+import cn.iocoder.dashboard.modules.system.controller.user.vo.user.*;
import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysPostDO;
@@ -33,14 +27,9 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
+import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
@@ -142,8 +131,6 @@ public class SysUserServiceImpl implements SysUserService {
updateObj.setId(id);
updateObj.setStatus(status);
userMapper.updateById(updateObj);
- // 删除用户关联数据
- permissionService.processUserDeleted(id);
}
@Override
@@ -152,6 +139,8 @@ public class SysUserServiceImpl implements SysUserService {
this.checkUserExists(id);
// 删除用户
userMapper.deleteById(id);
+ // 删除用户关联数据
+ permissionService.processUserDeleted(id);
}
@Override
@@ -227,7 +216,7 @@ public class SysUserServiceImpl implements SysUserService {
}
SysUserDO user = userMapper.selectById(id);
if (user == null) {
- throw ServiceExceptionUtil.exception(USER_NOT_EXISTS);
+ throw exception(USER_NOT_EXISTS);
}
}
@@ -241,10 +230,10 @@ public class SysUserServiceImpl implements SysUserService {
}
// 如果 id 为空,说明不用比较是否为相同 id 的用户
if (id == null) {
- throw ServiceExceptionUtil.exception(USER_USERNAME_EXISTS);
+ throw exception(USER_USERNAME_EXISTS);
}
if (!user.getId().equals(id)) {
- throw ServiceExceptionUtil.exception(USER_USERNAME_EXISTS);
+ throw exception(USER_USERNAME_EXISTS);
}
}
@@ -258,10 +247,10 @@ public class SysUserServiceImpl implements SysUserService {
}
// 如果 id 为空,说明不用比较是否为相同 id 的用户
if (id == null) {
- throw ServiceExceptionUtil.exception(USER_EMAIL_EXISTS);
+ throw exception(USER_EMAIL_EXISTS);
}
if (!user.getId().equals(id)) {
- throw ServiceExceptionUtil.exception(USER_EMAIL_EXISTS);
+ throw exception(USER_EMAIL_EXISTS);
}
}
@@ -275,10 +264,10 @@ public class SysUserServiceImpl implements SysUserService {
}
// 如果 id 为空,说明不用比较是否为相同 id 的用户
if (id == null) {
- throw ServiceExceptionUtil.exception(USER_MOBILE_EXISTS);
+ throw exception(USER_MOBILE_EXISTS);
}
if (!user.getId().equals(id)) {
- throw ServiceExceptionUtil.exception(USER_MOBILE_EXISTS);
+ throw exception(USER_MOBILE_EXISTS);
}
}
@@ -288,10 +277,10 @@ public class SysUserServiceImpl implements SysUserService {
}
SysDeptDO dept = deptService.getDept(deptId);
if (dept == null) {
- throw ServiceExceptionUtil.exception(DEPT_NOT_FOUND);
+ throw exception(DEPT_NOT_FOUND);
}
if (!CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) {
- throw ServiceExceptionUtil.exception(DEPT_NOT_ENABLE);
+ throw exception(DEPT_NOT_ENABLE);
}
}
@@ -301,16 +290,16 @@ public class SysUserServiceImpl implements SysUserService {
}
List posts = postService.getPosts(postIds, null);
if (CollUtil.isEmpty(posts)) {
- throw ServiceExceptionUtil.exception(POST_NOT_FOUND);
+ throw exception(POST_NOT_FOUND);
}
Map postMap = CollectionUtils.convertMap(posts, SysPostDO::getId);
postIds.forEach(postId -> {
SysPostDO post = postMap.get(postId);
if (post == null) {
- throw ServiceExceptionUtil.exception(POST_NOT_FOUND);
+ throw exception(POST_NOT_FOUND);
}
if (!CommonStatusEnum.ENABLE.getStatus().equals(post.getStatus())) {
- throw ServiceExceptionUtil.exception(POST_NOT_ENABLE, post.getName());
+ throw exception(POST_NOT_ENABLE, post.getName());
}
});
}
@@ -324,10 +313,10 @@ public class SysUserServiceImpl implements SysUserService {
private void checkOldPassword(Long id, String oldPassword) {
SysUserDO user = userMapper.selectById(id);
if (user == null) {
- throw ServiceExceptionUtil.exception(USER_NOT_EXISTS);
+ throw exception(USER_NOT_EXISTS);
}
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
- throw ServiceExceptionUtil.exception(USER_PASSWORD_FAILED);
+ throw exception(USER_PASSWORD_FAILED);
}
}
@@ -335,7 +324,7 @@ public class SysUserServiceImpl implements SysUserService {
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
public SysUserImportRespVO importUsers(List importUsers, boolean isUpdateSupport) {
if (CollUtil.isEmpty(importUsers)) {
- throw ServiceExceptionUtil.exception(USER_IMPORT_LIST_IS_EMPTY);
+ throw exception(USER_IMPORT_LIST_IS_EMPTY);
}
SysUserImportRespVO respVO = SysUserImportRespVO.builder().createUsernames(new ArrayList<>())
.updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build();
diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysUserSessionServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysUserSessionServiceImplTest.java
index 92cc9c32f..740519430 100644
--- a/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysUserSessionServiceImplTest.java
+++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysUserSessionServiceImplTest.java
@@ -1,7 +1,6 @@
package cn.iocoder.dashboard.modules.system.service.auth;
import static cn.hutool.core.util.RandomUtil.randomEle;
-import static cn.iocoder.dashboard.modules.system.dal.redis.SysRedisKeyConstants.LOGIN_USER;
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.dashboard.util.RandomUtils.randomDate;
import static cn.iocoder.dashboard.util.RandomUtils.randomLongId;
@@ -25,7 +24,6 @@ import javax.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
-import org.springframework.data.redis.core.StringRedisTemplate;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.dashboard.BaseDbAndRedisUnitTest;
@@ -33,10 +31,6 @@ import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.security.config.SecurityProperties;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
-import cn.iocoder.dashboard.modules.infra.controller.job.vo.job.InfJobPageReqVO;
-import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobDO;
-import cn.iocoder.dashboard.modules.infra.enums.config.InfConfigTypeEnum;
-import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.auth.SysUserSessionDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
@@ -50,7 +44,6 @@ import cn.iocoder.dashboard.modules.system.service.logger.impl.SysLoginLogServic
import cn.iocoder.dashboard.modules.system.service.user.SysUserServiceImpl;
import cn.iocoder.dashboard.util.AssertUtils;
import cn.iocoder.dashboard.util.RandomUtils;
-import cn.iocoder.dashboard.util.json.JsonUtils;
import cn.iocoder.dashboard.util.object.ObjectUtils;
/**
@@ -167,12 +160,12 @@ public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest {
String userIp = randomString();
SysUserDO dbUser1 = randomPojo(SysUserDO.class, o -> {
o.setUsername("testUsername1");
- o.setSex(randomEle(SysSexEnum.values()).getSEX());
+ o.setSex(randomEle(SysSexEnum.values()).getSex());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
SysUserDO dbUser2 = randomPojo(SysUserDO.class, o -> {
o.setUsername("testUsername2");
- o.setSex(randomEle(SysSexEnum.values()).getSEX());
+ o.setSex(randomEle(SysSexEnum.values()).getSex());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
SysUserSessionDO dbSession = randomPojo(SysUserSessionDO.class, o -> {
diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/logger/SysOperateLogServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/logger/SysOperateLogServiceImplTest.java
index f0a235ff4..67f99e35f 100644
--- a/src/test/java/cn/iocoder/dashboard/modules/system/service/logger/SysOperateLogServiceImplTest.java
+++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/logger/SysOperateLogServiceImplTest.java
@@ -68,7 +68,7 @@ public class SysOperateLogServiceImplTest extends BaseDbUnitTest {
// 先构造用户
SysUserDO user = RandomUtils.randomPojo(SysUserDO.class, o -> {
o.setNickname("wangkai");
- o.setSex(SysSexEnum.MALE.getSEX());
+ o.setSex(SysSexEnum.MALE.getSex());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user));
@@ -119,7 +119,7 @@ public class SysOperateLogServiceImplTest extends BaseDbUnitTest {
// 先构造用户
SysUserDO user = RandomUtils.randomPojo(SysUserDO.class, o -> {
o.setNickname("wangkai");
- o.setSex(SysSexEnum.MALE.getSEX());
+ o.setSex(SysSexEnum.MALE.getSex());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user));
diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java
index a662b82aa..e1b7b145c 100644
--- a/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java
+++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java
@@ -56,7 +56,7 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
@Test
public void testInitLocalCache_success() {
- // mock 数据s
+ // mock 数据
SysSmsChannelDO smsChannelDO01 = randomSmsChannelDO();
smsChannelMapper.insert(smsChannelDO01);
SysSmsChannelDO smsChannelDO02 = randomSmsChannelDO();
diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java
index ddb8832e3..5c96a46b3 100644
--- a/src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java
+++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java
@@ -1,128 +1,259 @@
package cn.iocoder.dashboard.modules.system.service.user;
import cn.hutool.core.util.RandomUtil;
-import cn.iocoder.dashboard.BaseSpringBootUnitTest;
+import cn.iocoder.dashboard.BaseDbUnitTest;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
+import cn.iocoder.dashboard.modules.infra.service.file.InfFileService;
+import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVO;
+import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportRespVO;
import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO;
-import cn.iocoder.dashboard.modules.system.dal.mysql.dept.SysDeptMapper;
+import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysPostDO;
+import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
+import cn.iocoder.dashboard.modules.system.dal.mysql.user.SysUserMapper;
+import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum;
import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService;
-import cn.iocoder.dashboard.util.RandomUtils;
+import cn.iocoder.dashboard.modules.system.service.dept.SysPostService;
+import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
+import cn.iocoder.dashboard.util.collection.ArrayUtils;
+import cn.iocoder.dashboard.util.collection.CollectionUtils;
import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.mockito.stubbing.Answer;
+import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.security.crypto.password.PasswordEncoder;
import javax.annotation.Resource;
+import java.io.ByteArrayInputStream;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
+import java.util.function.Consumer;
-import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
-import static cn.iocoder.dashboard.util.RandomUtils.randomString;
-import static org.junit.jupiter.api.Assertions.*;
+import static cn.hutool.core.util.RandomUtil.randomBytes;
+import static cn.hutool.core.util.RandomUtil.randomEle;
+import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
+import static cn.iocoder.dashboard.util.RandomUtils.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
/**
- * @Author zxl
- * @Date 2021/3/6 14:28
- * @Desc
+ * {@link SysUserService} 的单元测试类
+ *
+ * @author zxl
*/
-@Import(PasswordEncoder.class)
-public class SysUserServiceImplTest extends BaseSpringBootUnitTest {
+@Import(SysUserServiceImpl.class)
+public class SysUserServiceImplTest extends BaseDbUnitTest {
- @Autowired
- private SysUserService userService;
- @Autowired
- private PasswordEncoder passwordEncoder;
- @Autowired
- private SysDeptService deptService;
@Resource
- private SysDeptMapper deptMapper;
+ private SysUserServiceImpl userService;
+ @Resource
+ private SysUserMapper userMapper;
+
+ @MockBean
+ private SysDeptService deptService;
+ @MockBean
+ private SysPostService postService;
+ @MockBean
+ private SysPermissionService permissionService;
+ @MockBean
+ private PasswordEncoder passwordEncoder;
+ @MockBean
+ private InfFileService fileService;
@Test
- public void test_creatUser(){
+ public void testCreatUser_success() {
// 准备参数
- SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{
- o.setDeptId(null);
- o.setPostIds(Collections.emptySet());
- o.setSex(1);
- o.setPassword(randomString());
+ SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class, o -> {
+ o.setSex(RandomUtil.randomEle(SysSexEnum.values()).getSex());
+ o.setMobile(randomString());
});
- // 调用方法
- Long userId = userService.createUser(reqVO);
- // 校验数据是否存在
- assertNotNull(userService.getUser(userId));
+ // mock deptService 的方法
+ SysDeptDO dept = randomPojo(SysDeptDO.class, o -> {
+ o.setId(reqVO.getDeptId());
+ o.setStatus(CommonStatusEnum.ENABLE.getStatus());
+ });
+ when(deptService.getDept(eq(dept.getId()))).thenReturn(dept);
+ // mock postService 的方法
+ List posts = CollectionUtils.convertList(reqVO.getPostIds(), postId ->
+ randomPojo(SysPostDO.class, o -> {
+ o.setId(postId);
+ o.setStatus(CommonStatusEnum.ENABLE.getStatus());
+ }));
+ when(postService.getPosts(eq(reqVO.getPostIds()), isNull())).thenReturn(posts);
+ // mock passwordEncoder 的方法
+ when(passwordEncoder.encode(eq(reqVO.getPassword()))).thenReturn("yudaoyuanma");
+
+ // 调用
+ Long userId = userService.createUser(reqVO);
+ // 断言
+ SysUserDO user = userMapper.selectById(userId);
+ assertPojoEquals(reqVO, user, "password");
+ assertEquals("yudaoyuanma", user.getPassword());
+ assertEquals(CommonStatusEnum.ENABLE.getStatus(), user.getStatus());
}
@Test
- public void test_updateUser(){
+ public void testUpdateUser_success() {
+ // mock 数据
+ SysUserDO dbUser = randomSysUserDO();
+ userMapper.insert(dbUser);
// 准备参数
- SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{
- o.setDeptId(null);
- o.setPostIds(Collections.emptySet());
- o.setSex(1);
- o.setPassword(randomString());
+ SysUserUpdateReqVO reqVO = randomPojo(SysUserUpdateReqVO.class, o -> {
+ o.setId(dbUser.getId());
+ o.setSex(RandomUtil.randomEle(SysSexEnum.values()).getSex());
+ o.setMobile(randomString());
});
- // 先插入一条
- Long userId = userService.createUser(reqVO);
- // 准备更新参数:更新手机号
- SysUserUpdateReqVO updateVo = new SysUserUpdateReqVO();
- updateVo.setId(userId);
- updateVo.setMobile(RandomUtil.randomNumbers(11));
- // 调用方法、
- userService.updateUser(updateVo);
- // 校验结果
- assertEquals(userService.getUser(userId).getMobile(),updateVo.getMobile());
+ // mock deptService 的方法
+ SysDeptDO dept = randomPojo(SysDeptDO.class, o -> {
+ o.setId(reqVO.getDeptId());
+ o.setStatus(CommonStatusEnum.ENABLE.getStatus());
+ });
+ when(deptService.getDept(eq(dept.getId()))).thenReturn(dept);
+ // mock postService 的方法
+ List posts = CollectionUtils.convertList(reqVO.getPostIds(), postId ->
+ randomPojo(SysPostDO.class, o -> {
+ o.setId(postId);
+ o.setStatus(CommonStatusEnum.ENABLE.getStatus());
+ }));
+ when(postService.getPosts(eq(reqVO.getPostIds()), isNull())).thenReturn(posts);
+ // 调用
+ userService.updateUser(reqVO);
+ // 断言
+ SysUserDO user = userMapper.selectById(reqVO.getId());
+ assertPojoEquals(reqVO, user);
}
@Test
- public void test_deleteUser(){
+ public void testUpdateUserProfile_success() {
+ // mock 数据
+ SysUserDO dbUser = randomSysUserDO();
+ userMapper.insert(dbUser);
// 准备参数
- SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{
- o.setDeptId(null);
- o.setPostIds(Collections.emptySet());
- o.setSex(1);
- o.setPassword(randomString());
+ Long userId = dbUser.getId();
+ SysUserProfileUpdateReqVO reqVO = randomPojo(SysUserProfileUpdateReqVO.class, o -> {
+ o.setMobile(randomString());
+ o.setSex(RandomUtil.randomEle(SysSexEnum.values()).getSex());
});
- // 先插入一条
- Long userId = userService.createUser(reqVO);
+
+ // 调用
+ userService.updateUserProfile(userId, reqVO);
+ // 断言
+ SysUserDO user = userMapper.selectById(userId);
+ assertPojoEquals(reqVO, user);
+ }
+
+ @Test
+ public void testUpdateUserPassword_success() {
+ // mock 数据
+ SysUserDO dbUser = randomSysUserDO(o -> o.setPassword("encode:yudao"));
+ userMapper.insert(dbUser);
+ // 准备参数
+ Long userId = dbUser.getId();
+ SysUserProfileUpdatePasswordReqVO reqVO = randomPojo(SysUserProfileUpdatePasswordReqVO.class, o -> {
+ o.setOldPassword("yudao");
+ o.setNewPassword("yuanma");
+ });
+ // mock 方法
+ when(passwordEncoder.encode(anyString())).then(
+ (Answer) invocationOnMock -> "encode:" + invocationOnMock.getArgument(0));
+ when(passwordEncoder.matches(eq(reqVO.getOldPassword()), eq(dbUser.getPassword()))).thenReturn(true);
+
+ // 调用
+ userService.updateUserPassword(userId, reqVO);
+ // 断言
+ SysUserDO user = userMapper.selectById(userId);
+ assertEquals("encode:yuanma", user.getPassword());
+ }
+
+ @Test
+ public void testUpdateUserAvatar_success() {
+ // mock 数据
+ SysUserDO dbUser = randomSysUserDO();
+ userMapper.insert(dbUser);
+ // 准备参数
+ Long userId = dbUser.getId();
+ byte[] avatarFileBytes = randomBytes(10);
+ ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
+ // mock 方法
+ String avatar = randomString();
+ when(fileService.createFile(anyString(), eq(avatarFileBytes))).thenReturn(avatar);
+
+ // 调用
+ userService.updateUserAvatar(userId, avatarFile);
+ // 断言
+ SysUserDO user = userMapper.selectById(userId);
+ assertEquals(avatar, user.getAvatar());
+ }
+
+ @Test
+ public void testUpdateUserPassword02_success() {
+ // mock 数据
+ SysUserDO dbUser = randomSysUserDO();
+ userMapper.insert(dbUser);
+ // 准备参数
+ Long userId = dbUser.getId();
+ String password = "yudao";
+ // mock 方法
+ when(passwordEncoder.encode(anyString())).then(
+ (Answer) invocationOnMock -> "encode:" + invocationOnMock.getArgument(0));
+
+ // 调用
+ userService.updateUserPassword(userId, password);
+ // 断言
+ SysUserDO user = userMapper.selectById(userId);
+ assertEquals("encode:" + password, user.getPassword());
+ }
+
+ @Test
+ public void testUpdateUserStatus() {
+ // mock 数据
+ SysUserDO dbUser = randomSysUserDO();
+ userMapper.insert(dbUser);
+ // 准备参数
+ Long userId = dbUser.getId();
+ Integer status = randomCommonStatus();
+
+ // 调用
+ userService.updateUserStatus(userId, status);
+ // 断言
+ SysUserDO user = userMapper.selectById(userId);
+ assertEquals(status, user.getStatus());
+ }
+
+ @Test
+ public void testDeleteUser(){
+ // mock 数据
+ SysUserDO dbUser = randomSysUserDO();
+ userMapper.insert(dbUser);
+ // 准备参数
+ Long userId = dbUser.getId();
+
// 调用数据
userService.deleteUser(userId);
// 校验结果
assertNull(userService.getUser(userId));
+ // 校验调用次数
+ verify(permissionService, times(1)).processUserDeleted(eq(userId));
}
- @Test
- public void test_updateUserPassword(){
- // 准备参数
- SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{
- o.setDeptId(null);
- o.setPostIds(Collections.emptySet());
- o.setSex(1);
- o.setPassword("123");
- });
- // 先插入一条
- Long userId = userService.createUser(reqVO);
- String newPassword = RandomUtils.randomString();
- // 调用
- userService.updateUserPassword(userId,newPassword);
- // 校验结果
- assertNotEquals(passwordEncoder.encode(newPassword),userService.getUser(userId).getPassword());
- }
+
@Test
public void test_importUsers(){
SysDeptDO dept = randomPojo(SysDeptDO.class, o -> { // 等会查询到
o.setName("开发部");
- o.setSort("1");
+ o.setSort(1);
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
- int depId = deptMapper.insert(dept);
+// int depId = deptMapper.insert(dept);
+ int depId = 0;
// 准备参数
List list = new ArrayList<>();
list.add(randomPojo(SysUserImportExcelVO.class, o->{
@@ -153,18 +284,15 @@ public class SysUserServiceImplTest extends BaseSpringBootUnitTest {
assertEquals(respVOUpdate.getUpdateUsernames().size(),3);
}
- public SysUserCreateReqVO randomUserVO(){
-
- SysUserCreateReqVO userVO = new SysUserCreateReqVO();
- userVO.setUsername(RandomUtils.randomString());
- userVO.setNickname(RandomUtils.randomString());
- userVO.setMobile(RandomUtil.randomNumbers(11));
- userVO.setEmail(RandomUtils.randomString()+"@ruoyi.com");
- userVO.setDeptId(null);
- userVO.setPostIds(Collections.emptySet());
-
- userVO.setPassword(RandomUtils.randomString());
- return userVO;
+ // ========== 随机对象 ==========
+ @SafeVarargs
+ private static SysUserDO randomSysUserDO(Consumer... consumers) {
+ Consumer consumer = (o) -> {
+ o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围
+ o.setSex(randomEle(SysSexEnum.values()).getSex()); // 保证 sex 的范围
+ };
+ return randomPojo(SysUserDO.class, ArrayUtils.append(consumer, consumers));
}
+
}