Commit bef06ef940394574a96be525d09fa1a2fcd1af46

Authored by YunaiV
1 parent 1011de32

完善 PostServiceImpl 单元测试

Showing 13 changed files with 201 additions and 140 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java
@@ -218,7 +218,7 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService { @@ -218,7 +218,7 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
218 BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) { 218 BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) {
219 deptApi.validateDeptList(options); 219 deptApi.validateDeptList(options);
220 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.POST.getType())) { 220 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.POST.getType())) {
221 - postApi.validPosts(options); 221 + postApi.validPostList(options);
222 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER.getType())) { 222 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER.getType())) {
223 adminUserApi.validUsers(options); 223 adminUserApi.validUsers(options);
224 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER_GROUP.getType())) { 224 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER_GROUP.getType())) {
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/dept/PostApi.java
@@ -16,6 +16,6 @@ public interface PostApi { @@ -16,6 +16,6 @@ public interface PostApi {
16 * 16 *
17 * @param ids 岗位编号数组 17 * @param ids 岗位编号数组
18 */ 18 */
19 - void validPosts(Collection<Long> ids); 19 + void validPostList(Collection<Long> ids);
20 20
21 } 21 }
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
@@ -50,7 +50,7 @@ public interface ErrorCodeConstants { @@ -50,7 +50,7 @@ public interface ErrorCodeConstants {
50 ErrorCode DEPT_EXITS_CHILDREN = new ErrorCode(1002004003, "存在子部门,无法删除"); 50 ErrorCode DEPT_EXITS_CHILDREN = new ErrorCode(1002004003, "存在子部门,无法删除");
51 ErrorCode DEPT_PARENT_ERROR = new ErrorCode(1002004004, "不能设置自己为父部门"); 51 ErrorCode DEPT_PARENT_ERROR = new ErrorCode(1002004004, "不能设置自己为父部门");
52 ErrorCode DEPT_EXISTS_USER = new ErrorCode(1002004005, "部门中存在员工,无法删除"); 52 ErrorCode DEPT_EXISTS_USER = new ErrorCode(1002004005, "部门中存在员工,无法删除");
53 - ErrorCode DEPT_NOT_ENABLE = new ErrorCode(1002004006, "部门不处于开启状态,不允许选择"); 53 + ErrorCode DEPT_NOT_ENABLE = new ErrorCode(1002004006, "部门({})不处于开启状态,不允许选择");
54 ErrorCode DEPT_PARENT_IS_CHILD = new ErrorCode(1002004007, "不能设置自己的子部门为父部门"); 54 ErrorCode DEPT_PARENT_IS_CHILD = new ErrorCode(1002004007, "不能设置自己的子部门为父部门");
55 55
56 // ========== 岗位模块 1002005000 ========== 56 // ========== 岗位模块 1002005000 ==========
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/dept/PostApiImpl.java
@@ -18,7 +18,8 @@ public class PostApiImpl implements PostApi { @@ -18,7 +18,8 @@ public class PostApiImpl implements PostApi {
18 private PostService postService; 18 private PostService postService;
19 19
20 @Override 20 @Override
21 - public void validPosts(Collection<Long> ids) {  
22 - postService.validPosts(ids); 21 + public void validPostList(Collection<Long> ids) {
  22 + postService.validatePostList(ids);
23 } 23 }
  24 +
24 } 25 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/PostController.java
@@ -72,7 +72,7 @@ public class PostController { @@ -72,7 +72,7 @@ public class PostController {
72 @ApiOperation(value = "获取岗位精简信息列表", notes = "只包含被开启的岗位,主要用于前端的下拉选项") 72 @ApiOperation(value = "获取岗位精简信息列表", notes = "只包含被开启的岗位,主要用于前端的下拉选项")
73 public CommonResult<List<PostSimpleRespVO>> getSimplePosts() { 73 public CommonResult<List<PostSimpleRespVO>> getSimplePosts() {
74 // 获得岗位列表,只要开启状态的 74 // 获得岗位列表,只要开启状态的
75 - List<PostDO> list = postService.getPosts(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); 75 + List<PostDO> list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
76 // 排序后,返回给前端 76 // 排序后,返回给前端
77 list.sort(Comparator.comparing(PostDO::getSort)); 77 list.sort(Comparator.comparing(PostDO::getSort));
78 return success(PostConvert.INSTANCE.convertList02(list)); 78 return success(PostConvert.INSTANCE.convertList02(list));
@@ -90,7 +90,7 @@ public class PostController { @@ -90,7 +90,7 @@ public class PostController {
90 @PreAuthorize("@ss.hasPermission('system:post:export')") 90 @PreAuthorize("@ss.hasPermission('system:post:export')")
91 @OperateLog(type = EXPORT) 91 @OperateLog(type = EXPORT)
92 public void export(HttpServletResponse response, @Validated PostExportReqVO reqVO) throws IOException { 92 public void export(HttpServletResponse response, @Validated PostExportReqVO reqVO) throws IOException {
93 - List<PostDO> posts = postService.getPosts(reqVO); 93 + List<PostDO> posts = postService.getPostList(reqVO);
94 List<PostExcelVO> data = PostConvert.INSTANCE.convertList03(posts); 94 List<PostExcelVO> data = PostConvert.INSTANCE.convertList03(posts);
95 // 输出 95 // 输出
96 ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostExcelVO.class, data); 96 ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostExcelVO.class, data);
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/oauth2/OAuth2UserController.java
@@ -61,7 +61,7 @@ public class OAuth2UserController { @@ -61,7 +61,7 @@ public class OAuth2UserController {
61 } 61 }
62 // 获得岗位信息 62 // 获得岗位信息
63 if (CollUtil.isNotEmpty(user.getPostIds())) { 63 if (CollUtil.isNotEmpty(user.getPostIds())) {
64 - List<PostDO> posts = postService.getPosts(user.getPostIds()); 64 + List<PostDO> posts = postService.getPostList(user.getPostIds());
65 resp.setPosts(OAuth2UserConvert.INSTANCE.convertList(posts)); 65 resp.setPosts(OAuth2UserConvert.INSTANCE.convertList(posts));
66 } 66 }
67 return success(resp); 67 return success(resp);
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java
@@ -72,7 +72,7 @@ public class UserProfileController { @@ -72,7 +72,7 @@ public class UserProfileController {
72 } 72 }
73 // 获得岗位信息 73 // 获得岗位信息
74 if (CollUtil.isNotEmpty(user.getPostIds())) { 74 if (CollUtil.isNotEmpty(user.getPostIds())) {
75 - List<PostDO> posts = postService.getPosts(user.getPostIds()); 75 + List<PostDO> posts = postService.getPostList(user.getPostIds());
76 resp.setPosts(UserConvert.INSTANCE.convertList02(posts)); 76 resp.setPosts(UserConvert.INSTANCE.convertList02(posts));
77 } 77 }
78 // 获得社交用户信息 78 // 获得社交用户信息
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostService.java
@@ -49,8 +49,8 @@ public interface PostService { @@ -49,8 +49,8 @@ public interface PostService {
49 * @param ids 岗位编号数组。如果为空,不进行筛选 49 * @param ids 岗位编号数组。如果为空,不进行筛选
50 * @return 部门列表 50 * @return 部门列表
51 */ 51 */
52 - default List<PostDO> getPosts(@Nullable Collection<Long> ids) {  
53 - return getPosts(ids, asSet(CommonStatusEnum.ENABLE.getStatus(), CommonStatusEnum.DISABLE.getStatus())); 52 + default List<PostDO> getPostList(@Nullable Collection<Long> ids) {
  53 + return getPostList(ids, asSet(CommonStatusEnum.ENABLE.getStatus(), CommonStatusEnum.DISABLE.getStatus()));
54 } 54 }
55 55
56 /** 56 /**
@@ -60,7 +60,7 @@ public interface PostService { @@ -60,7 +60,7 @@ public interface PostService {
60 * @param statuses 状态数组。如果为空,不进行筛选 60 * @param statuses 状态数组。如果为空,不进行筛选
61 * @return 部门列表 61 * @return 部门列表
62 */ 62 */
63 - List<PostDO> getPosts(@Nullable Collection<Long> ids, @Nullable Collection<Integer> statuses); 63 + List<PostDO> getPostList(@Nullable Collection<Long> ids, @Nullable Collection<Integer> statuses);
64 64
65 /** 65 /**
66 * 获得岗位分页列表 66 * 获得岗位分页列表
@@ -76,7 +76,7 @@ public interface PostService { @@ -76,7 +76,7 @@ public interface PostService {
76 * @param reqVO 查询条件 76 * @param reqVO 查询条件
77 * @return 部门列表 77 * @return 部门列表
78 */ 78 */
79 - List<PostDO> getPosts(PostExportReqVO reqVO); 79 + List<PostDO> getPostList(PostExportReqVO reqVO);
80 80
81 /** 81 /**
82 * 获得岗位信息 82 * 获得岗位信息
@@ -93,6 +93,6 @@ public interface PostService { @@ -93,6 +93,6 @@ public interface PostService {
93 * 93 *
94 * @param ids 岗位编号数组 94 * @param ids 岗位编号数组
95 */ 95 */
96 - void validPosts(Collection<Long> ids); 96 + void validatePostList(Collection<Long> ids);
97 97
98 } 98 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImpl.java
@@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.service.dept; @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.service.dept;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; 4 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
5 -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;  
6 import cn.iocoder.yudao.framework.common.pojo.PageResult; 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
7 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO; 6 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO;
8 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; 7 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
@@ -38,7 +37,8 @@ public class PostServiceImpl implements PostService { @@ -38,7 +37,8 @@ public class PostServiceImpl implements PostService {
38 @Override 37 @Override
39 public Long createPost(PostCreateReqVO reqVO) { 38 public Long createPost(PostCreateReqVO reqVO) {
40 // 校验正确性 39 // 校验正确性
41 - this.checkCreateOrUpdate(null, reqVO.getName(), reqVO.getCode()); 40 + validatePostForCreateOrUpdate(null, reqVO.getName(), reqVO.getCode());
  41 +
42 // 插入岗位 42 // 插入岗位
43 PostDO post = PostConvert.INSTANCE.convert(reqVO); 43 PostDO post = PostConvert.INSTANCE.convert(reqVO);
44 postMapper.insert(post); 44 postMapper.insert(post);
@@ -48,7 +48,8 @@ public class PostServiceImpl implements PostService { @@ -48,7 +48,8 @@ public class PostServiceImpl implements PostService {
48 @Override 48 @Override
49 public void updatePost(PostUpdateReqVO reqVO) { 49 public void updatePost(PostUpdateReqVO reqVO) {
50 // 校验正确性 50 // 校验正确性
51 - this.checkCreateOrUpdate(reqVO.getId(), reqVO.getName(), reqVO.getCode()); 51 + validatePostForCreateOrUpdate(reqVO.getId(), reqVO.getName(), reqVO.getCode());
  52 +
52 // 更新岗位 53 // 更新岗位
53 PostDO updateObj = PostConvert.INSTANCE.convert(reqVO); 54 PostDO updateObj = PostConvert.INSTANCE.convert(reqVO);
54 postMapper.updateById(updateObj); 55 postMapper.updateById(updateObj);
@@ -57,80 +58,79 @@ public class PostServiceImpl implements PostService { @@ -57,80 +58,79 @@ public class PostServiceImpl implements PostService {
57 @Override 58 @Override
58 public void deletePost(Long id) { 59 public void deletePost(Long id) {
59 // 校验是否存在 60 // 校验是否存在
60 - this.checkPostExists(id); 61 + validatePostExists(id);
61 // 删除部门 62 // 删除部门
62 postMapper.deleteById(id); 63 postMapper.deleteById(id);
63 } 64 }
64 65
65 - @Override  
66 - public List<PostDO> getPosts(Collection<Long> ids, Collection<Integer> statuses) {  
67 - return postMapper.selectList(ids, statuses);  
68 - }  
69 -  
70 - @Override  
71 - public PageResult<PostDO> getPostPage(PostPageReqVO reqVO) {  
72 - return postMapper.selectPage(reqVO);  
73 - }  
74 -  
75 - @Override  
76 - public List<PostDO> getPosts(PostExportReqVO reqVO) {  
77 - return postMapper.selectList(reqVO);  
78 - }  
79 -  
80 - @Override  
81 - public PostDO getPost(Long id) {  
82 - return postMapper.selectById(id);  
83 - }  
84 -  
85 - private void checkCreateOrUpdate(Long id, String name, String code) { 66 + private void validatePostForCreateOrUpdate(Long id, String name, String code) {
86 // 校验自己存在 67 // 校验自己存在
87 - checkPostExists(id); 68 + validatePostExists(id);
88 // 校验岗位名的唯一性 69 // 校验岗位名的唯一性
89 - checkPostNameUnique(id, name); 70 + validatePostNameUnique(id, name);
90 // 校验岗位编码的唯一性 71 // 校验岗位编码的唯一性
91 - checkPostCodeUnique(id, code); 72 + validatePostCodeUnique(id, code);
92 } 73 }
93 74
94 - private void checkPostNameUnique(Long id, String name) { 75 + private void validatePostNameUnique(Long id, String name) {
95 PostDO post = postMapper.selectByName(name); 76 PostDO post = postMapper.selectByName(name);
96 if (post == null) { 77 if (post == null) {
97 return; 78 return;
98 } 79 }
99 // 如果 id 为空,说明不用比较是否为相同 id 的岗位 80 // 如果 id 为空,说明不用比较是否为相同 id 的岗位
100 if (id == null) { 81 if (id == null) {
101 - throw ServiceExceptionUtil.exception(POST_NAME_DUPLICATE); 82 + throw exception(POST_NAME_DUPLICATE);
102 } 83 }
103 if (!post.getId().equals(id)) { 84 if (!post.getId().equals(id)) {
104 - throw ServiceExceptionUtil.exception(POST_NAME_DUPLICATE); 85 + throw exception(POST_NAME_DUPLICATE);
105 } 86 }
106 } 87 }
107 88
108 - private void checkPostCodeUnique(Long id, String code) { 89 + private void validatePostCodeUnique(Long id, String code) {
109 PostDO post = postMapper.selectByCode(code); 90 PostDO post = postMapper.selectByCode(code);
110 if (post == null) { 91 if (post == null) {
111 return; 92 return;
112 } 93 }
113 // 如果 id 为空,说明不用比较是否为相同 id 的岗位 94 // 如果 id 为空,说明不用比较是否为相同 id 的岗位
114 if (id == null) { 95 if (id == null) {
115 - throw ServiceExceptionUtil.exception(POST_CODE_DUPLICATE); 96 + throw exception(POST_CODE_DUPLICATE);
116 } 97 }
117 if (!post.getId().equals(id)) { 98 if (!post.getId().equals(id)) {
118 - throw ServiceExceptionUtil.exception(POST_CODE_DUPLICATE); 99 + throw exception(POST_CODE_DUPLICATE);
119 } 100 }
120 } 101 }
121 102
122 - private void checkPostExists(Long id) { 103 + private void validatePostExists(Long id) {
123 if (id == null) { 104 if (id == null) {
124 return; 105 return;
125 } 106 }
126 - PostDO post = postMapper.selectById(id);  
127 - if (post == null) {  
128 - throw ServiceExceptionUtil.exception(POST_NOT_FOUND); 107 + if (postMapper.selectById(id) == null) {
  108 + throw exception(POST_NOT_FOUND);
129 } 109 }
130 } 110 }
131 111
132 @Override 112 @Override
133 - public void validPosts(Collection<Long> ids) { 113 + public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses) {
  114 + return postMapper.selectList(ids, statuses);
  115 + }
  116 +
  117 + @Override
  118 + public PageResult<PostDO> getPostPage(PostPageReqVO reqVO) {
  119 + return postMapper.selectPage(reqVO);
  120 + }
  121 +
  122 + @Override
  123 + public List<PostDO> getPostList(PostExportReqVO reqVO) {
  124 + return postMapper.selectList(reqVO);
  125 + }
  126 +
  127 + @Override
  128 + public PostDO getPost(Long id) {
  129 + return postMapper.selectById(id);
  130 + }
  131 +
  132 + @Override
  133 + public void validatePostList(Collection<Long> ids) {
134 if (CollUtil.isEmpty(ids)) { 134 if (CollUtil.isEmpty(ids)) {
135 return; 135 return;
136 } 136 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
@@ -319,7 +319,7 @@ public class AdminUserServiceImpl implements AdminUserService { @@ -319,7 +319,7 @@ public class AdminUserServiceImpl implements AdminUserService {
319 // 校验部门处于开启状态 319 // 校验部门处于开启状态
320 deptService.validateDeptList(CollectionUtils.singleton(deptId)); 320 deptService.validateDeptList(CollectionUtils.singleton(deptId));
321 // 校验岗位处于开启状态 321 // 校验岗位处于开启状态
322 - postService.validPosts(postIds); 322 + postService.validatePostList(postIds);
323 } 323 }
324 324
325 @VisibleForTesting 325 @VisibleForTesting
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java
@@ -337,7 +337,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest { @@ -337,7 +337,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
337 List<Long> ids = singletonList(deptDO.getId()); 337 List<Long> ids = singletonList(deptDO.getId());
338 338
339 // 调用, 并断言异常 339 // 调用, 并断言异常
340 - assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE); 340 + assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE, deptDO.getName());
341 } 341 }
342 342
343 @SafeVarargs 343 @SafeVarargs
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/PostServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImplTest.java
1 package cn.iocoder.yudao.module.system.service.dept; 1 package cn.iocoder.yudao.module.system.service.dept;
2 2
3 -import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;  
4 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; 3 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
5 import cn.iocoder.yudao.framework.common.pojo.PageResult; 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; 5 import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
7 -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; 6 +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
8 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO; 7 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO;
9 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; 8 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
10 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; 9 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO;
11 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostUpdateReqVO; 10 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostUpdateReqVO;
  11 +import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
12 import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper; 12 import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper;
13 -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;  
14 import org.junit.jupiter.api.Test; 13 import org.junit.jupiter.api.Test;
15 import org.springframework.context.annotation.Import; 14 import org.springframework.context.annotation.Import;
16 15
17 import javax.annotation.Resource; 16 import javax.annotation.Resource;
  17 +import java.util.Arrays;
18 import java.util.List; 18 import java.util.List;
19 import java.util.function.Consumer; 19 import java.util.function.Consumer;
20 20
21 import static cn.hutool.core.util.RandomUtil.randomEle; 21 import static cn.hutool.core.util.RandomUtil.randomEle;
  22 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
22 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; 23 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
23 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; 24 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
24 -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;  
25 -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; 25 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
26 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; 26 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
  27 +import static java.util.Collections.singletonList;
27 import static org.junit.jupiter.api.Assertions.*; 28 import static org.junit.jupiter.api.Assertions.*;
28 29
  30 +/**
  31 + * {@link PostServiceImpl} 的单元测试类
  32 + *
  33 + * @author niudehua
  34 + */
29 @Import(PostServiceImpl.class) 35 @Import(PostServiceImpl.class)
30 -public class PostServiceTest extends BaseDbUnitTest { 36 +public class PostServiceImplTest extends BaseDbUnitTest {
31 37
32 @Resource 38 @Resource
33 private PostServiceImpl postService; 39 private PostServiceImpl postService;
  40 +
34 @Resource 41 @Resource
35 private PostMapper postMapper; 42 private PostMapper postMapper;
36 43
37 @Test 44 @Test
38 - void testPagePosts() { 45 + public void testCreatePost_success() {
  46 + // 准备参数
  47 + PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,
  48 + o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()));
  49 + // 调用
  50 + Long postId = postService.createPost(reqVO);
  51 +
  52 + // 断言
  53 + assertNotNull(postId);
  54 + // 校验记录的属性是否正确
  55 + PostDO post = postMapper.selectById(postId);
  56 + assertPojoEquals(reqVO, post);
  57 + }
  58 +
  59 + @Test
  60 + public void testUpdatePost_success() {
  61 + // mock 数据
  62 + PostDO postDO = randomPostDO();
  63 + postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
  64 + // 准备参数
  65 + PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> {
  66 + // 设置更新的 ID
  67 + o.setId(postDO.getId());
  68 + o.setStatus(randomEle(CommonStatusEnum.values()).getStatus());
  69 + });
  70 +
  71 + // 调用
  72 + postService.updatePost(reqVO);
  73 + // 校验是否更新正确
  74 + PostDO post = postMapper.selectById(reqVO.getId());
  75 + assertPojoEquals(reqVO, post);
  76 + }
  77 +
  78 + @Test
  79 + public void testDeletePost_success() {
  80 + // mock 数据
  81 + PostDO postDO = randomPostDO();
  82 + postMapper.insert(postDO);
  83 + // 准备参数
  84 + Long id = postDO.getId();
  85 +
  86 + // 调用
  87 + postService.deletePost(id);
  88 + assertNull(postMapper.selectById(id));
  89 + }
  90 +
  91 + @Test
  92 + public void testValidatePost_notFoundForDelete() {
  93 + // 准备参数
  94 + Long id = randomLongId();
  95 +
  96 + // 调用, 并断言异常
  97 + assertServiceException(() -> postService.deletePost(id), POST_NOT_FOUND);
  98 + }
  99 +
  100 + @Test
  101 + public void testValidatePost_nameDuplicateForCreate() {
  102 + // mock 数据
  103 + PostDO postDO = randomPostDO();
  104 + postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
  105 + // 准备参数
  106 + PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,
  107 + // 模拟 name 重复
  108 + o -> o.setName(postDO.getName()));
  109 + assertServiceException(() -> postService.createPost(reqVO), POST_NAME_DUPLICATE);
  110 + }
  111 +
  112 + @Test
  113 + public void testValidatePost_codeDuplicateForUpdate() {
  114 + // mock 数据
  115 + PostDO postDO = randomPostDO();
  116 + postMapper.insert(postDO);
  117 + // mock 数据:稍后模拟重复它的 code
  118 + PostDO codePostDO = randomPostDO();
  119 + postMapper.insert(codePostDO);
  120 + // 准备参数
  121 + PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> {
  122 + // 设置更新的 ID
  123 + o.setId(postDO.getId());
  124 + // 模拟 code 重复
  125 + o.setCode(codePostDO.getCode());
  126 + });
  127 +
  128 + // 调用, 并断言异常
  129 + assertServiceException(() -> postService.updatePost(reqVO), POST_CODE_DUPLICATE);
  130 + }
  131 +
  132 + @Test
  133 + public void testGetPostPage() {
39 // mock 数据 134 // mock 数据
40 PostDO postDO = randomPojo(PostDO.class, o -> { 135 PostDO postDO = randomPojo(PostDO.class, o -> {
41 o.setName("码仔"); 136 o.setName("码仔");
@@ -43,10 +138,9 @@ public class PostServiceTest extends BaseDbUnitTest { @@ -43,10 +138,9 @@ public class PostServiceTest extends BaseDbUnitTest {
43 }); 138 });
44 postMapper.insert(postDO); 139 postMapper.insert(postDO);
45 // 测试 name 不匹配 140 // 测试 name 不匹配
46 - postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员"))); 141 + postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员")));
47 // 测试 status 不匹配 142 // 测试 status 不匹配
48 - postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));  
49 - 143 + postMapper.insert(cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
50 // 准备参数 144 // 准备参数
51 PostPageReqVO reqVO = new PostPageReqVO(); 145 PostPageReqVO reqVO = new PostPageReqVO();
52 reqVO.setName("码"); 146 reqVO.setName("码");
@@ -54,7 +148,6 @@ public class PostServiceTest extends BaseDbUnitTest { @@ -54,7 +148,6 @@ public class PostServiceTest extends BaseDbUnitTest {
54 148
55 // 调用 149 // 调用
56 PageResult<PostDO> pageResult = postService.getPostPage(reqVO); 150 PageResult<PostDO> pageResult = postService.getPostPage(reqVO);
57 -  
58 // 断言 151 // 断言
59 assertEquals(1, pageResult.getTotal()); 152 assertEquals(1, pageResult.getTotal());
60 assertEquals(1, pageResult.getList().size()); 153 assertEquals(1, pageResult.getList().size());
@@ -62,7 +155,7 @@ public class PostServiceTest extends BaseDbUnitTest { @@ -62,7 +155,7 @@ public class PostServiceTest extends BaseDbUnitTest {
62 } 155 }
63 156
64 @Test 157 @Test
65 - void testListPosts() { 158 + public void testGetPostList_export() {
66 // mock 数据 159 // mock 数据
67 PostDO postDO = randomPojo(PostDO.class, o -> { 160 PostDO postDO = randomPojo(PostDO.class, o -> {
68 o.setName("码仔"); 161 o.setName("码仔");
@@ -70,124 +163,91 @@ public class PostServiceTest extends BaseDbUnitTest { @@ -70,124 +163,91 @@ public class PostServiceTest extends BaseDbUnitTest {
70 }); 163 });
71 postMapper.insert(postDO); 164 postMapper.insert(postDO);
72 // 测试 name 不匹配 165 // 测试 name 不匹配
73 - postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员"))); 166 + postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员")));
74 // 测试 status 不匹配 167 // 测试 status 不匹配
75 - postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); 168 + postMapper.insert(cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
76 // 准备参数 169 // 准备参数
77 PostExportReqVO reqVO = new PostExportReqVO(); 170 PostExportReqVO reqVO = new PostExportReqVO();
78 reqVO.setName("码"); 171 reqVO.setName("码");
79 reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); 172 reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
80 173
81 // 调用 174 // 调用
82 - List<PostDO> list = postService.getPosts(reqVO); 175 + List<PostDO> list = postService.getPostList(reqVO);
83 // 断言 176 // 断言
84 assertEquals(1, list.size()); 177 assertEquals(1, list.size());
85 assertPojoEquals(postDO, list.get(0)); 178 assertPojoEquals(postDO, list.get(0));
86 } 179 }
87 180
88 @Test 181 @Test
89 - void testGetPost() { 182 + public void testGetPostList() {
90 // mock 数据 183 // mock 数据
91 - PostDO dbPostDO = randomPostDO();  
92 - postMapper.insert(dbPostDO); 184 + PostDO postDO01 = randomPojo(PostDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
  185 + postMapper.insert(postDO01);
  186 + // 测试 status 不匹配
  187 + PostDO postDO02 = randomPojo(PostDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
  188 + postMapper.insert(postDO02);
93 // 准备参数 189 // 准备参数
94 - Long id = dbPostDO.getId();  
95 - // 调用  
96 - PostDO post = postService.getPost(id);  
97 - // 断言  
98 - assertNotNull(post);  
99 - assertPojoEquals(dbPostDO, post);  
100 - } 190 + List<Long> ids = Arrays.asList(postDO01.getId(), postDO02.getId());
101 191
102 - @Test  
103 - void testCreatePost_success() {  
104 - // 准备参数  
105 - PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,  
106 - o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()));  
107 // 调用 192 // 调用
108 - Long postId = postService.createPost(reqVO); 193 + List<PostDO> list = postService.getPostList(ids, singletonList(CommonStatusEnum.ENABLE.getStatus()));
109 // 断言 194 // 断言
110 - assertNotNull(postId);  
111 - // 校验记录的属性是否正确  
112 - PostDO post = postMapper.selectById(postId);  
113 - assertPojoEquals(reqVO, post); 195 + assertEquals(1, list.size());
  196 + assertPojoEquals(postDO01, list.get(0));
114 } 197 }
115 198
116 @Test 199 @Test
117 - void testUpdatePost_success() { 200 + public void testGetPost() {
118 // mock 数据 201 // mock 数据
119 - PostDO postDO = randomPostDO();  
120 - postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据 202 + PostDO dbPostDO = randomPostDO();
  203 + postMapper.insert(dbPostDO);
121 // 准备参数 204 // 准备参数
122 - PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class,  
123 - o -> {  
124 - // 设置更新的 ID  
125 - o.setId(postDO.getId());  
126 - o.setStatus(randomEle(CommonStatusEnum.values()).getStatus());  
127 - }); 205 + Long id = dbPostDO.getId();
128 // 调用 206 // 调用
129 - postService.updatePost(reqVO);  
130 - // 校验是否更新正确  
131 - PostDO post = postMapper.selectById(reqVO.getId());// 获取最新的  
132 - assertPojoEquals(reqVO, post); 207 + PostDO post = postService.getPost(id);
  208 + // 断言
  209 + assertNotNull(post);
  210 + assertPojoEquals(dbPostDO, post);
133 } 211 }
134 212
135 @Test 213 @Test
136 - void testDeletePost_success() { 214 + public void testValidatePostList_success() {
137 // mock 数据 215 // mock 数据
138 - PostDO postDO = randomPostDO(); 216 + PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
139 postMapper.insert(postDO); 217 postMapper.insert(postDO);
140 // 准备参数 218 // 准备参数
141 - Long id = postDO.getId();  
142 - // 调用  
143 - postService.deletePost(id);  
144 - assertNull(postMapper.selectById(id));  
145 - } 219 + List<Long> ids = singletonList(postDO.getId());
146 220
147 - @Test  
148 - void testCheckPost_notFoundForDelete() {  
149 - // 准备参数  
150 - Long id = randomLongId();  
151 - // 调用, 并断言异常  
152 - assertServiceException(() -> postService.deletePost(id), POST_NOT_FOUND); 221 + // 调用,无需断言
  222 + postService.validatePostList(ids);
153 } 223 }
154 224
155 @Test 225 @Test
156 - void testCheckPost_nameDuplicateForCreate() {  
157 - // mock 数据  
158 - PostDO postDO = randomPostDO();  
159 - postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据 226 + public void testValidatePostList_notFound() {
160 // 准备参数 227 // 准备参数
161 - PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,  
162 - // 模拟 name 重复  
163 - o -> o.setName(postDO.getName()));  
164 - assertServiceException(() -> postService.createPost(reqVO), POST_NAME_DUPLICATE); 228 + List<Long> ids = singletonList(randomLongId());
  229 +
  230 + // 调用, 并断言异常
  231 + assertServiceException(() -> postService.validatePostList(ids), POST_NOT_FOUND);
165 } 232 }
166 233
167 @Test 234 @Test
168 - void testCheckPost_codeDuplicateForUpdate() { 235 + public void testValidatePostList_notEnable() {
169 // mock 数据 236 // mock 数据
170 - PostDO postDO = randomPostDO(); 237 + PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
171 postMapper.insert(postDO); 238 postMapper.insert(postDO);
172 - // mock 数据 稍后模拟重复它的 code  
173 - PostDO codePostDO = randomPostDO();  
174 - postMapper.insert(codePostDO);  
175 // 准备参数 239 // 准备参数
176 - PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class,  
177 - o -> {  
178 - // 设置更新的 ID  
179 - o.setId(postDO.getId());  
180 - // 模拟 code 重复  
181 - o.setCode(codePostDO.getCode());  
182 - }); 240 + List<Long> ids = singletonList(postDO.getId());
  241 +
183 // 调用, 并断言异常 242 // 调用, 并断言异常
184 - assertServiceException(() -> postService.updatePost(reqVO), POST_CODE_DUPLICATE); 243 + assertServiceException(() -> postService.validatePostList(ids), POST_NOT_ENABLE,
  244 + postDO.getName());
185 } 245 }
186 246
187 @SafeVarargs 247 @SafeVarargs
188 private static PostDO randomPostDO(Consumer<PostDO>... consumers) { 248 private static PostDO randomPostDO(Consumer<PostDO>... consumers) {
189 Consumer<PostDO> consumer = (o) -> { 249 Consumer<PostDO> consumer = (o) -> {
190 - o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 250 + o.setStatus(randomCommonStatus()); // 保证 status 的范围
191 }; 251 };
192 return randomPojo(PostDO.class, ArrayUtils.append(consumer, consumers)); 252 return randomPojo(PostDO.class, ArrayUtils.append(consumer, consumers));
193 } 253 }
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java
@@ -102,7 +102,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { @@ -102,7 +102,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
102 o.setId(postId); 102 o.setId(postId);
103 o.setStatus(CommonStatusEnum.ENABLE.getStatus()); 103 o.setStatus(CommonStatusEnum.ENABLE.getStatus());
104 })); 104 }));
105 - when(postService.getPosts(eq(reqVO.getPostIds()), isNull())).thenReturn(posts); 105 + when(postService.getPostList(eq(reqVO.getPostIds()), isNull())).thenReturn(posts);
106 // mock passwordEncoder 的方法 106 // mock passwordEncoder 的方法
107 when(passwordEncoder.encode(eq(reqVO.getPassword()))).thenReturn("yudaoyuanma"); 107 when(passwordEncoder.encode(eq(reqVO.getPassword()))).thenReturn("yudaoyuanma");
108 108
@@ -160,7 +160,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { @@ -160,7 +160,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
160 o.setId(postId); 160 o.setId(postId);
161 o.setStatus(CommonStatusEnum.ENABLE.getStatus()); 161 o.setStatus(CommonStatusEnum.ENABLE.getStatus());
162 })); 162 }));
163 - when(postService.getPosts(eq(reqVO.getPostIds()), isNull())).thenReturn(posts); 163 + when(postService.getPostList(eq(reqVO.getPostIds()), isNull())).thenReturn(posts);
164 164
165 // 调用 165 // 调用
166 userService.updateUser(reqVO); 166 userService.updateUser(reqVO);