Commit 10790958ef8295abd20ff2bbf6a12ffca9151312
1 parent
81e7e046
完成 SysUserServiceImplTest 的单元测试
Showing
3 changed files
with
353 additions
and
49 deletions
ruoyi-ui/src/views/system/user/index.vue
| @@ -362,7 +362,7 @@ export default { | @@ -362,7 +362,7 @@ export default { | ||
| 362 | created() { | 362 | created() { |
| 363 | this.getList(); | 363 | this.getList(); |
| 364 | this.getTreeselect(); | 364 | this.getTreeselect(); |
| 365 | - this.getConfigKey("sys.user.initPassword").then(response => { | 365 | + this.getConfigKey("sys.user.init-password").then(response => { |
| 366 | this.initPassword = response.msg; | 366 | this.initPassword = response.msg; |
| 367 | }); | 367 | }); |
| 368 | }, | 368 | }, |
src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java
| @@ -20,7 +20,9 @@ import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService; | @@ -20,7 +20,9 @@ import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService; | ||
| 20 | import cn.iocoder.dashboard.modules.system.service.dept.SysPostService; | 20 | import cn.iocoder.dashboard.modules.system.service.dept.SysPostService; |
| 21 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; | 21 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; |
| 22 | import cn.iocoder.dashboard.util.collection.CollectionUtils; | 22 | import cn.iocoder.dashboard.util.collection.CollectionUtils; |
| 23 | +import com.google.common.annotations.VisibleForTesting; | ||
| 23 | import lombok.extern.slf4j.Slf4j; | 24 | import lombok.extern.slf4j.Slf4j; |
| 25 | +import org.springframework.beans.factory.annotation.Value; | ||
| 24 | import org.springframework.security.crypto.password.PasswordEncoder; | 26 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 25 | import org.springframework.stereotype.Service; | 27 | import org.springframework.stereotype.Service; |
| 26 | import org.springframework.transaction.annotation.Transactional; | 28 | import org.springframework.transaction.annotation.Transactional; |
| @@ -42,6 +44,9 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; | @@ -42,6 +44,9 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; | ||
| 42 | @Slf4j | 44 | @Slf4j |
| 43 | public class SysUserServiceImpl implements SysUserService { | 45 | public class SysUserServiceImpl implements SysUserService { |
| 44 | 46 | ||
| 47 | + @Value("${sys.user.init-password:yudaoyuanma}") | ||
| 48 | + private String userInitPassword; | ||
| 49 | + | ||
| 45 | @Resource | 50 | @Resource |
| 46 | private SysUserMapper userMapper; | 51 | private SysUserMapper userMapper; |
| 47 | 52 | ||
| @@ -210,7 +215,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -210,7 +215,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 210 | this.checkPostEnable(postIds); | 215 | this.checkPostEnable(postIds); |
| 211 | } | 216 | } |
| 212 | 217 | ||
| 213 | - private void checkUserExists(Long id) { | 218 | + @VisibleForTesting |
| 219 | + void checkUserExists(Long id) { | ||
| 214 | if (id == null) { | 220 | if (id == null) { |
| 215 | return; | 221 | return; |
| 216 | } | 222 | } |
| @@ -220,7 +226,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -220,7 +226,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 220 | } | 226 | } |
| 221 | } | 227 | } |
| 222 | 228 | ||
| 223 | - private void checkUsernameUnique(Long id, String username) { | 229 | + @VisibleForTesting |
| 230 | + void checkUsernameUnique(Long id, String username) { | ||
| 224 | if (StrUtil.isBlank(username)) { | 231 | if (StrUtil.isBlank(username)) { |
| 225 | return; | 232 | return; |
| 226 | } | 233 | } |
| @@ -237,7 +244,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -237,7 +244,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 237 | } | 244 | } |
| 238 | } | 245 | } |
| 239 | 246 | ||
| 240 | - private void checkEmailUnique(Long id, String email) { | 247 | + @VisibleForTesting |
| 248 | + void checkEmailUnique(Long id, String email) { | ||
| 241 | if (StrUtil.isBlank(email)) { | 249 | if (StrUtil.isBlank(email)) { |
| 242 | return; | 250 | return; |
| 243 | } | 251 | } |
| @@ -254,7 +262,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -254,7 +262,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 254 | } | 262 | } |
| 255 | } | 263 | } |
| 256 | 264 | ||
| 257 | - private void checkMobileUnique(Long id, String mobile) { | 265 | + @VisibleForTesting |
| 266 | + void checkMobileUnique(Long id, String mobile) { | ||
| 258 | if (StrUtil.isBlank(mobile)) { | 267 | if (StrUtil.isBlank(mobile)) { |
| 259 | return; | 268 | return; |
| 260 | } | 269 | } |
| @@ -271,7 +280,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -271,7 +280,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 271 | } | 280 | } |
| 272 | } | 281 | } |
| 273 | 282 | ||
| 274 | - private void checkDeptEnable(Long deptId) { | 283 | + @VisibleForTesting |
| 284 | + void checkDeptEnable(Long deptId) { | ||
| 275 | if (deptId == null) { // 允许不选择 | 285 | if (deptId == null) { // 允许不选择 |
| 276 | return; | 286 | return; |
| 277 | } | 287 | } |
| @@ -284,7 +294,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -284,7 +294,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 284 | } | 294 | } |
| 285 | } | 295 | } |
| 286 | 296 | ||
| 287 | - private void checkPostEnable(Set<Long> postIds) { | 297 | + @VisibleForTesting |
| 298 | + void checkPostEnable(Set<Long> postIds) { | ||
| 288 | if (CollUtil.isEmpty(postIds)) { // 允许不选择 | 299 | if (CollUtil.isEmpty(postIds)) { // 允许不选择 |
| 289 | return; | 300 | return; |
| 290 | } | 301 | } |
| @@ -310,7 +321,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -310,7 +321,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 310 | * @param id 用户 id | 321 | * @param id 用户 id |
| 311 | * @param oldPassword 旧密码 | 322 | * @param oldPassword 旧密码 |
| 312 | */ | 323 | */ |
| 313 | - private void checkOldPassword(Long id, String oldPassword) { | 324 | + @VisibleForTesting |
| 325 | + void checkOldPassword(Long id, String oldPassword) { | ||
| 314 | SysUserDO user = userMapper.selectById(id); | 326 | SysUserDO user = userMapper.selectById(id); |
| 315 | if (user == null) { | 327 | if (user == null) { |
| 316 | throw exception(USER_NOT_EXISTS); | 328 | throw exception(USER_NOT_EXISTS); |
| @@ -340,8 +352,8 @@ public class SysUserServiceImpl implements SysUserService { | @@ -340,8 +352,8 @@ public class SysUserServiceImpl implements SysUserService { | ||
| 340 | // 判断如果不存在,在进行插入 | 352 | // 判断如果不存在,在进行插入 |
| 341 | SysUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); | 353 | SysUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); |
| 342 | if (existUser == null) { | 354 | if (existUser == null) { |
| 343 | - // TODO 芋艿:初始密码 | ||
| 344 | - userMapper.insert(SysUserConvert.INSTANCE.convert(importUser)); | 355 | + userMapper.insert(SysUserConvert.INSTANCE.convert(importUser) |
| 356 | + .setPassword(passwordEncoder.encode(userInitPassword))); // 设置默认密码 | ||
| 345 | respVO.getCreateUsernames().add(importUser.getUsername()); | 357 | respVO.getCreateUsernames().add(importUser.getUsername()); |
| 346 | return; | 358 | return; |
| 347 | } | 359 | } |
src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java
| 1 | package cn.iocoder.dashboard.modules.system.service.user; | 1 | package cn.iocoder.dashboard.modules.system.service.user; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 3 | import cn.hutool.core.util.RandomUtil; | 4 | import cn.hutool.core.util.RandomUtil; |
| 4 | import cn.iocoder.dashboard.BaseDbUnitTest; | 5 | import cn.iocoder.dashboard.BaseDbUnitTest; |
| 5 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | 6 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 7 | +import cn.iocoder.dashboard.common.pojo.PageResult; | ||
| 6 | import cn.iocoder.dashboard.modules.infra.service.file.InfFileService; | 8 | import cn.iocoder.dashboard.modules.infra.service.file.InfFileService; |
| 7 | import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVO; | 9 | import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVO; |
| 8 | import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO; | 10 | import cn.iocoder.dashboard.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO; |
| 9 | -import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO; | ||
| 10 | -import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO; | ||
| 11 | -import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportRespVO; | ||
| 12 | -import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; | 11 | +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.*; |
| 13 | import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO; | 12 | import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO; |
| 14 | import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysPostDO; | 13 | import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysPostDO; |
| 15 | import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; | 14 | import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; |
| @@ -20,6 +19,7 @@ import cn.iocoder.dashboard.modules.system.service.dept.SysPostService; | @@ -20,6 +19,7 @@ import cn.iocoder.dashboard.modules.system.service.dept.SysPostService; | ||
| 20 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; | 19 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; |
| 21 | import cn.iocoder.dashboard.util.collection.ArrayUtils; | 20 | import cn.iocoder.dashboard.util.collection.ArrayUtils; |
| 22 | import cn.iocoder.dashboard.util.collection.CollectionUtils; | 21 | import cn.iocoder.dashboard.util.collection.CollectionUtils; |
| 22 | +import cn.iocoder.dashboard.util.object.ObjectUtils; | ||
| 23 | import org.junit.jupiter.api.Test; | 23 | import org.junit.jupiter.api.Test; |
| 24 | import org.mockito.stubbing.Answer; | 24 | import org.mockito.stubbing.Answer; |
| 25 | import org.springframework.boot.test.mock.mockito.MockBean; | 25 | import org.springframework.boot.test.mock.mockito.MockBean; |
| @@ -28,14 +28,18 @@ import org.springframework.security.crypto.password.PasswordEncoder; | @@ -28,14 +28,18 @@ import org.springframework.security.crypto.password.PasswordEncoder; | ||
| 28 | 28 | ||
| 29 | import javax.annotation.Resource; | 29 | import javax.annotation.Resource; |
| 30 | import java.io.ByteArrayInputStream; | 30 | import java.io.ByteArrayInputStream; |
| 31 | -import java.util.ArrayList; | ||
| 32 | import java.util.List; | 31 | import java.util.List; |
| 32 | +import java.util.Set; | ||
| 33 | import java.util.function.Consumer; | 33 | import java.util.function.Consumer; |
| 34 | 34 | ||
| 35 | import static cn.hutool.core.util.RandomUtil.randomBytes; | 35 | import static cn.hutool.core.util.RandomUtil.randomBytes; |
| 36 | import static cn.hutool.core.util.RandomUtil.randomEle; | 36 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 37 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; | ||
| 37 | import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | 38 | import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; |
| 39 | +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | ||
| 38 | import static cn.iocoder.dashboard.util.RandomUtils.*; | 40 | import static cn.iocoder.dashboard.util.RandomUtils.*; |
| 41 | +import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; | ||
| 42 | +import static org.assertj.core.util.Lists.newArrayList; | ||
| 39 | import static org.junit.jupiter.api.Assertions.assertEquals; | 43 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | import static org.junit.jupiter.api.Assertions.assertNull; | 44 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 41 | import static org.mockito.ArgumentMatchers.*; | 45 | import static org.mockito.ArgumentMatchers.*; |
| @@ -228,7 +232,7 @@ public class SysUserServiceImplTest extends BaseDbUnitTest { | @@ -228,7 +232,7 @@ public class SysUserServiceImplTest extends BaseDbUnitTest { | ||
| 228 | } | 232 | } |
| 229 | 233 | ||
| 230 | @Test | 234 | @Test |
| 231 | - public void testDeleteUser(){ | 235 | + public void testDeleteUser_success(){ |
| 232 | // mock 数据 | 236 | // mock 数据 |
| 233 | SysUserDO dbUser = randomSysUserDO(); | 237 | SysUserDO dbUser = randomSysUserDO(); |
| 234 | userMapper.insert(dbUser); | 238 | userMapper.insert(dbUser); |
| @@ -243,45 +247,333 @@ public class SysUserServiceImplTest extends BaseDbUnitTest { | @@ -243,45 +247,333 @@ public class SysUserServiceImplTest extends BaseDbUnitTest { | ||
| 243 | verify(permissionService, times(1)).processUserDeleted(eq(userId)); | 247 | verify(permissionService, times(1)).processUserDeleted(eq(userId)); |
| 244 | } | 248 | } |
| 245 | 249 | ||
| 250 | + @Test | ||
| 251 | + public void testGetUserPage() { | ||
| 252 | + // mock 数据 | ||
| 253 | + SysUserDO dbUser = initGetUserPageData(); | ||
| 254 | + // 准备参数 | ||
| 255 | + SysUserPageReqVO reqVO = new SysUserPageReqVO(); | ||
| 256 | + reqVO.setUsername("yudao"); | ||
| 257 | + reqVO.setMobile("1560"); | ||
| 258 | + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 259 | + reqVO.setBeginTime(buildTime(2020, 12, 1)); | ||
| 260 | + reqVO.setEndTime(buildTime(2020, 12, 24)); | ||
| 261 | + reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门 | ||
| 262 | + // mock 方法 | ||
| 263 | + List<SysDeptDO> deptList = newArrayList(randomPojo(SysDeptDO.class, o -> o.setId(2L))); | ||
| 264 | + when(deptService.getDeptsByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | ||
| 246 | 265 | ||
| 266 | + // 调用 | ||
| 267 | + PageResult<SysUserDO> pageResult = userService.getUserPage(reqVO); | ||
| 268 | + // 断言 | ||
| 269 | + assertEquals(1, pageResult.getTotal()); | ||
| 270 | + assertEquals(1, pageResult.getList().size()); | ||
| 271 | + assertPojoEquals(dbUser, pageResult.getList().get(0)); | ||
| 272 | + } | ||
| 247 | 273 | ||
| 248 | @Test | 274 | @Test |
| 249 | - public void test_importUsers(){ | ||
| 250 | - SysDeptDO dept = randomPojo(SysDeptDO.class, o -> { // 等会查询到 | ||
| 251 | - o.setName("开发部"); | ||
| 252 | - o.setSort(1); | 275 | + public void testGetUsers() { |
| 276 | + // mock 数据 | ||
| 277 | + SysUserDO dbUser = initGetUserPageData(); | ||
| 278 | + // 准备参数 | ||
| 279 | + SysUserExportReqVO reqVO = new SysUserExportReqVO(); | ||
| 280 | + reqVO.setUsername("yudao"); | ||
| 281 | + reqVO.setMobile("1560"); | ||
| 282 | + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 283 | + reqVO.setBeginTime(buildTime(2020, 12, 1)); | ||
| 284 | + reqVO.setEndTime(buildTime(2020, 12, 24)); | ||
| 285 | + reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门 | ||
| 286 | + // mock 方法 | ||
| 287 | + List<SysDeptDO> deptList = newArrayList(randomPojo(SysDeptDO.class, o -> o.setId(2L))); | ||
| 288 | + when(deptService.getDeptsByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | ||
| 289 | + | ||
| 290 | + // 调用 | ||
| 291 | + List<SysUserDO> list = userService.getUsers(reqVO); | ||
| 292 | + // 断言 | ||
| 293 | + assertEquals(1, list.size()); | ||
| 294 | + assertPojoEquals(dbUser, list.get(0)); | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + /** | ||
| 298 | + * 初始化 getUserPage 方法的测试数据 | ||
| 299 | + */ | ||
| 300 | + private SysUserDO initGetUserPageData() { | ||
| 301 | + // mock 数据 | ||
| 302 | + SysUserDO dbUser = randomSysUserDO(o -> { // 等会查询到 | ||
| 303 | + o.setUsername("yudaoyuanma"); | ||
| 304 | + o.setMobile("15601691300"); | ||
| 253 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | 305 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 306 | + o.setCreateTime(buildTime(2020, 12, 12)); | ||
| 307 | + o.setDeptId(2L); | ||
| 254 | }); | 308 | }); |
| 255 | -// int depId = deptMapper.insert(dept); | ||
| 256 | - int depId = 0; | 309 | + userMapper.insert(dbUser); |
| 310 | + // 测试 username 不匹配 | ||
| 311 | + userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setUsername("yuanma"))); | ||
| 312 | + // 测试 mobile 不匹配 | ||
| 313 | + userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setMobile("18818260888"))); | ||
| 314 | + // 测试 status 不匹配 | ||
| 315 | + userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||
| 316 | + // 测试 createTime 不匹配 | ||
| 317 | + userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setCreateTime(buildTime(2020, 11, 11)))); | ||
| 318 | + // 测试 dept 不匹配 | ||
| 319 | + userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setDeptId(0L))); | ||
| 320 | + return dbUser; | ||
| 321 | + } | ||
| 322 | + | ||
| 323 | + /** | ||
| 324 | + * 情况一,校验不通过,导致插入失败 | ||
| 325 | + */ | ||
| 326 | + @Test | ||
| 327 | + public void testImportUsers_01() { | ||
| 257 | // 准备参数 | 328 | // 准备参数 |
| 258 | - List<SysUserImportExcelVO> list = new ArrayList<>(); | ||
| 259 | - list.add(randomPojo(SysUserImportExcelVO.class, o->{ | ||
| 260 | - o.setDeptId(dept.getId()); | ||
| 261 | - o.setSex(1); | ||
| 262 | - o.setStatus(1); | ||
| 263 | - })); | ||
| 264 | - list.add(randomPojo(SysUserImportExcelVO.class, o->{ | ||
| 265 | - o.setDeptId(dept.getId()); | ||
| 266 | - o.setSex(1); | ||
| 267 | - o.setStatus(1); | ||
| 268 | - })); | ||
| 269 | - list.add(randomPojo(SysUserImportExcelVO.class, o->{ | ||
| 270 | - o.setDeptId(dept.getId()); | ||
| 271 | - o.setSex(1); | ||
| 272 | - o.setStatus(1); | ||
| 273 | - })); | ||
| 274 | - // 批量插入 | ||
| 275 | - SysUserImportRespVO respVO = userService.importUsers(list,false); | ||
| 276 | - System.out.println(respVO.getCreateUsernames().size()); | ||
| 277 | - // 校验结果 | ||
| 278 | - assertEquals(respVO.getCreateUsernames().size(),3); | ||
| 279 | - // 批量更新 | ||
| 280 | - list.get(0).setSex(0); | ||
| 281 | - SysUserImportRespVO respVOUpdate = userService.importUsers(list,true); | ||
| 282 | - System.out.println(respVOUpdate.getUpdateUsernames().size()); | ||
| 283 | - // 校验结果 | ||
| 284 | - assertEquals(respVOUpdate.getUpdateUsernames().size(),3); | 329 | + SysUserImportExcelVO importUser = randomPojo(SysUserImportExcelVO.class); |
| 330 | + // mock 方法 | ||
| 331 | + | ||
| 332 | + // 调用 | ||
| 333 | + SysUserImportRespVO respVO = userService.importUsers(newArrayList(importUser), true); | ||
| 334 | + // 断言 | ||
| 335 | + assertEquals(0, respVO.getCreateUsernames().size()); | ||
| 336 | + assertEquals(0, respVO.getUpdateUsernames().size()); | ||
| 337 | + assertEquals(1, respVO.getFailureUsernames().size()); | ||
| 338 | + assertEquals(DEPT_NOT_FOUND.getMsg(), respVO.getFailureUsernames().get(importUser.getUsername())); | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + /** | ||
| 342 | + * 情况二,不存在,进行插入 | ||
| 343 | + */ | ||
| 344 | + @Test | ||
| 345 | + public void testImportUsers_02() { | ||
| 346 | + // 准备参数 | ||
| 347 | + SysUserImportExcelVO importUser = randomPojo(SysUserImportExcelVO.class, o -> { | ||
| 348 | + o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 | ||
| 349 | + o.setSex(randomEle(SysSexEnum.values()).getSex()); // 保证 sex 的范围 | ||
| 350 | + }); | ||
| 351 | + // mock deptService 的方法 | ||
| 352 | + SysDeptDO dept = randomPojo(SysDeptDO.class, o -> { | ||
| 353 | + o.setId(importUser.getDeptId()); | ||
| 354 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 355 | + }); | ||
| 356 | + when(deptService.getDept(eq(dept.getId()))).thenReturn(dept); | ||
| 357 | + // mock passwordEncoder 的方法 | ||
| 358 | + when(passwordEncoder.encode(eq("yudaoyuanma"))).thenReturn("java"); | ||
| 359 | + | ||
| 360 | + // 调用 | ||
| 361 | + SysUserImportRespVO respVO = userService.importUsers(newArrayList(importUser), true); | ||
| 362 | + // 断言 | ||
| 363 | + assertEquals(1, respVO.getCreateUsernames().size()); | ||
| 364 | + SysUserDO user = userMapper.selectByUsername(respVO.getCreateUsernames().get(0)); | ||
| 365 | + assertPojoEquals(importUser, user); | ||
| 366 | + assertEquals("java", user.getPassword()); | ||
| 367 | + assertEquals(0, respVO.getUpdateUsernames().size()); | ||
| 368 | + assertEquals(0, respVO.getFailureUsernames().size()); | ||
| 369 | + } | ||
| 370 | + | ||
| 371 | + /** | ||
| 372 | + * 情况三,存在,但是不强制更新 | ||
| 373 | + */ | ||
| 374 | + @Test | ||
| 375 | + public void testImportUsers_03() { | ||
| 376 | + // mock 数据 | ||
| 377 | + SysUserDO dbUser = randomSysUserDO(); | ||
| 378 | + userMapper.insert(dbUser); | ||
| 379 | + // 准备参数 | ||
| 380 | + SysUserImportExcelVO importUser = randomPojo(SysUserImportExcelVO.class, o -> { | ||
| 381 | + o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 | ||
| 382 | + o.setSex(randomEle(SysSexEnum.values()).getSex()); // 保证 sex 的范围 | ||
| 383 | + o.setUsername(dbUser.getUsername()); | ||
| 384 | + }); | ||
| 385 | + // mock deptService 的方法 | ||
| 386 | + SysDeptDO dept = randomPojo(SysDeptDO.class, o -> { | ||
| 387 | + o.setId(importUser.getDeptId()); | ||
| 388 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 389 | + }); | ||
| 390 | + when(deptService.getDept(eq(dept.getId()))).thenReturn(dept); | ||
| 391 | + | ||
| 392 | + // 调用 | ||
| 393 | + SysUserImportRespVO respVO = userService.importUsers(newArrayList(importUser), false); | ||
| 394 | + // 断言 | ||
| 395 | + assertEquals(0, respVO.getCreateUsernames().size()); | ||
| 396 | + assertEquals(0, respVO.getUpdateUsernames().size()); | ||
| 397 | + assertEquals(1, respVO.getFailureUsernames().size()); | ||
| 398 | + assertEquals(USER_USERNAME_EXISTS.getMsg(), respVO.getFailureUsernames().get(importUser.getUsername())); | ||
| 399 | + } | ||
| 400 | + | ||
| 401 | + /** | ||
| 402 | + * 情况四,存在,强制更新 | ||
| 403 | + */ | ||
| 404 | + @Test | ||
| 405 | + public void testImportUsers_04() { | ||
| 406 | + // mock 数据 | ||
| 407 | + SysUserDO dbUser = randomSysUserDO(); | ||
| 408 | + userMapper.insert(dbUser); | ||
| 409 | + // 准备参数 | ||
| 410 | + SysUserImportExcelVO importUser = randomPojo(SysUserImportExcelVO.class, o -> { | ||
| 411 | + o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 | ||
| 412 | + o.setSex(randomEle(SysSexEnum.values()).getSex()); // 保证 sex 的范围 | ||
| 413 | + o.setUsername(dbUser.getUsername()); | ||
| 414 | + }); | ||
| 415 | + // mock deptService 的方法 | ||
| 416 | + SysDeptDO dept = randomPojo(SysDeptDO.class, o -> { | ||
| 417 | + o.setId(importUser.getDeptId()); | ||
| 418 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 419 | + }); | ||
| 420 | + when(deptService.getDept(eq(dept.getId()))).thenReturn(dept); | ||
| 421 | + | ||
| 422 | + // 调用 | ||
| 423 | + SysUserImportRespVO respVO = userService.importUsers(newArrayList(importUser), true); | ||
| 424 | + // 断言 | ||
| 425 | + assertEquals(0, respVO.getCreateUsernames().size()); | ||
| 426 | + assertEquals(1, respVO.getUpdateUsernames().size()); | ||
| 427 | + SysUserDO user = userMapper.selectByUsername(respVO.getUpdateUsernames().get(0)); | ||
| 428 | + assertPojoEquals(importUser, user); | ||
| 429 | + assertEquals(0, respVO.getFailureUsernames().size()); | ||
| 430 | + } | ||
| 431 | + | ||
| 432 | + @Test | ||
| 433 | + public void testCheckUserExists_notExists() { | ||
| 434 | + assertServiceException(() -> userService.checkUserExists(randomLongId()), USER_NOT_EXISTS); | ||
| 435 | + } | ||
| 436 | + | ||
| 437 | + @Test | ||
| 438 | + public void testCheckUsernameUnique_usernameExistsForCreate() { | ||
| 439 | + // 准备参数 | ||
| 440 | + String username = randomString(); | ||
| 441 | + // mock 数据 | ||
| 442 | + userMapper.insert(randomSysUserDO(o -> o.setUsername(username))); | ||
| 443 | + | ||
| 444 | + // 调用,校验异常 | ||
| 445 | + assertServiceException(() -> userService.checkUsernameUnique(null, username), | ||
| 446 | + USER_USERNAME_EXISTS); | ||
| 447 | + } | ||
| 448 | + | ||
| 449 | + @Test | ||
| 450 | + public void testCheckUsernameUnique_usernameExistsForUpdate() { | ||
| 451 | + // 准备参数 | ||
| 452 | + Long id = randomLongId(); | ||
| 453 | + String username = randomString(); | ||
| 454 | + // mock 数据 | ||
| 455 | + userMapper.insert(randomSysUserDO(o -> o.setUsername(username))); | ||
| 456 | + | ||
| 457 | + // 调用,校验异常 | ||
| 458 | + assertServiceException(() -> userService.checkUsernameUnique(id, username), | ||
| 459 | + USER_USERNAME_EXISTS); | ||
| 460 | + } | ||
| 461 | + | ||
| 462 | + @Test | ||
| 463 | + public void testCheckEmailUnique_emailExistsForCreate() { | ||
| 464 | + // 准备参数 | ||
| 465 | + String email = randomString(); | ||
| 466 | + // mock 数据 | ||
| 467 | + userMapper.insert(randomSysUserDO(o -> o.setEmail(email))); | ||
| 468 | + | ||
| 469 | + // 调用,校验异常 | ||
| 470 | + assertServiceException(() -> userService.checkEmailUnique(null, email), | ||
| 471 | + USER_EMAIL_EXISTS); | ||
| 472 | + } | ||
| 473 | + | ||
| 474 | + @Test | ||
| 475 | + public void testCheckEmailUnique_emailExistsForUpdate() { | ||
| 476 | + // 准备参数 | ||
| 477 | + Long id = randomLongId(); | ||
| 478 | + String email = randomString(); | ||
| 479 | + // mock 数据 | ||
| 480 | + userMapper.insert(randomSysUserDO(o -> o.setEmail(email))); | ||
| 481 | + | ||
| 482 | + // 调用,校验异常 | ||
| 483 | + assertServiceException(() -> userService.checkEmailUnique(id, email), | ||
| 484 | + USER_EMAIL_EXISTS); | ||
| 485 | + } | ||
| 486 | + | ||
| 487 | + @Test | ||
| 488 | + public void testCheckMobileUnique_mobileExistsForCreate() { | ||
| 489 | + // 准备参数 | ||
| 490 | + String mobile = randomString(); | ||
| 491 | + // mock 数据 | ||
| 492 | + userMapper.insert(randomSysUserDO(o -> o.setMobile(mobile))); | ||
| 493 | + | ||
| 494 | + // 调用,校验异常 | ||
| 495 | + assertServiceException(() -> userService.checkMobileUnique(null, mobile), | ||
| 496 | + USER_MOBILE_EXISTS); | ||
| 497 | + } | ||
| 498 | + | ||
| 499 | + @Test | ||
| 500 | + public void testCheckMobileUnique_mobileExistsForUpdate() { | ||
| 501 | + // 准备参数 | ||
| 502 | + Long id = randomLongId(); | ||
| 503 | + String mobile = randomString(); | ||
| 504 | + // mock 数据 | ||
| 505 | + userMapper.insert(randomSysUserDO(o -> o.setMobile(mobile))); | ||
| 506 | + | ||
| 507 | + // 调用,校验异常 | ||
| 508 | + assertServiceException(() -> userService.checkMobileUnique(id, mobile), | ||
| 509 | + USER_MOBILE_EXISTS); | ||
| 510 | + } | ||
| 511 | + | ||
| 512 | + @Test | ||
| 513 | + public void testCheckDeptEnable_notFound() { | ||
| 514 | + assertServiceException(() -> userService.checkDeptEnable(randomLongId()), | ||
| 515 | + DEPT_NOT_FOUND); | ||
| 516 | + } | ||
| 517 | + | ||
| 518 | + @Test | ||
| 519 | + public void testCheckDeptEnable_notEnable() { | ||
| 520 | + // 准备参数 | ||
| 521 | + Long deptId = randomLongId(); | ||
| 522 | + // mock deptService 的方法 | ||
| 523 | + SysDeptDO dept = randomPojo(SysDeptDO.class, o -> { | ||
| 524 | + o.setId(deptId); | ||
| 525 | + o.setStatus(CommonStatusEnum.DISABLE.getStatus()); | ||
| 526 | + }); | ||
| 527 | + when(deptService.getDept(eq(dept.getId()))).thenReturn(dept); | ||
| 528 | + | ||
| 529 | + // 调用,校验异常 | ||
| 530 | + assertServiceException(() -> userService.checkDeptEnable(deptId), | ||
| 531 | + DEPT_NOT_ENABLE); | ||
| 532 | + } | ||
| 533 | + | ||
| 534 | + @Test | ||
| 535 | + public void testCheckPostEnable_notFound() { | ||
| 536 | + assertServiceException(() -> userService.checkPostEnable(randomSet(Long.class)), | ||
| 537 | + POST_NOT_FOUND); | ||
| 538 | + } | ||
| 539 | + | ||
| 540 | + @Test | ||
| 541 | + public void testCheckPostEnable_notEnable() { | ||
| 542 | + // 准备参数 | ||
| 543 | + Set<Long> postIds = randomSet(Long.class); | ||
| 544 | + // mock postService 的方法 | ||
| 545 | + List<SysPostDO> posts = CollectionUtils.convertList(postIds, postId -> | ||
| 546 | + randomPojo(SysPostDO.class, o -> { | ||
| 547 | + o.setId(postId); | ||
| 548 | + o.setStatus(CommonStatusEnum.DISABLE.getStatus()); | ||
| 549 | + })); | ||
| 550 | + when(postService.getPosts(eq(postIds), isNull())).thenReturn(posts); | ||
| 551 | + | ||
| 552 | + // 调用,校验异常 | ||
| 553 | + assertServiceException(() -> userService.checkPostEnable(postIds), | ||
| 554 | + POST_NOT_ENABLE, CollUtil.getFirst(posts).getName()); | ||
| 555 | + } | ||
| 556 | + | ||
| 557 | + @Test | ||
| 558 | + public void testCheckOldPassword_notExists() { | ||
| 559 | + assertServiceException(() -> userService.checkOldPassword(randomLongId(), randomString()), | ||
| 560 | + USER_NOT_EXISTS); | ||
| 561 | + } | ||
| 562 | + | ||
| 563 | + @Test | ||
| 564 | + public void testCheckOldPassword_passwordFailed() { | ||
| 565 | + // mock 数据 | ||
| 566 | + SysUserDO user = randomSysUserDO(); | ||
| 567 | + userMapper.insert(user); | ||
| 568 | + // 准备参数 | ||
| 569 | + Long id = user.getId(); | ||
| 570 | + String oldPassword = user.getPassword(); | ||
| 571 | + | ||
| 572 | + // 调用,校验异常 | ||
| 573 | + assertServiceException(() -> userService.checkOldPassword(id, oldPassword), | ||
| 574 | + USER_PASSWORD_FAILED); | ||
| 575 | + // 校验调用 | ||
| 576 | + verify(passwordEncoder, times(1)).matches(eq(oldPassword), eq(user.getPassword())); | ||
| 285 | } | 577 | } |
| 286 | 578 | ||
| 287 | // ========== 随机对象 ========== | 579 | // ========== 随机对象 ========== |