Commit bef06ef940394574a96be525d09fa1a2fcd1af46
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 | 218 | BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) { |
| 219 | 219 | deptApi.validateDeptList(options); |
| 220 | 220 | } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.POST.getType())) { |
| 221 | - postApi.validPosts(options); | |
| 221 | + postApi.validPostList(options); | |
| 222 | 222 | } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER.getType())) { |
| 223 | 223 | adminUserApi.validUsers(options); |
| 224 | 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
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 | 50 | ErrorCode DEPT_EXITS_CHILDREN = new ErrorCode(1002004003, "存在子部门,无法删除"); |
| 51 | 51 | ErrorCode DEPT_PARENT_ERROR = new ErrorCode(1002004004, "不能设置自己为父部门"); |
| 52 | 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 | 54 | ErrorCode DEPT_PARENT_IS_CHILD = new ErrorCode(1002004007, "不能设置自己的子部门为父部门"); |
| 55 | 55 | |
| 56 | 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 | 18 | private PostService postService; |
| 19 | 19 | |
| 20 | 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 | 72 | @ApiOperation(value = "获取岗位精简信息列表", notes = "只包含被开启的岗位,主要用于前端的下拉选项") |
| 73 | 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 | 77 | list.sort(Comparator.comparing(PostDO::getSort)); |
| 78 | 78 | return success(PostConvert.INSTANCE.convertList02(list)); |
| ... | ... | @@ -90,7 +90,7 @@ public class PostController { |
| 90 | 90 | @PreAuthorize("@ss.hasPermission('system:post:export')") |
| 91 | 91 | @OperateLog(type = EXPORT) |
| 92 | 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 | 94 | List<PostExcelVO> data = PostConvert.INSTANCE.convertList03(posts); |
| 95 | 95 | // 输出 |
| 96 | 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 | 61 | } |
| 62 | 62 | // 获得岗位信息 |
| 63 | 63 | if (CollUtil.isNotEmpty(user.getPostIds())) { |
| 64 | - List<PostDO> posts = postService.getPosts(user.getPostIds()); | |
| 64 | + List<PostDO> posts = postService.getPostList(user.getPostIds()); | |
| 65 | 65 | resp.setPosts(OAuth2UserConvert.INSTANCE.convertList(posts)); |
| 66 | 66 | } |
| 67 | 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 | 72 | } |
| 73 | 73 | // 获得岗位信息 |
| 74 | 74 | if (CollUtil.isNotEmpty(user.getPostIds())) { |
| 75 | - List<PostDO> posts = postService.getPosts(user.getPostIds()); | |
| 75 | + List<PostDO> posts = postService.getPostList(user.getPostIds()); | |
| 76 | 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 | 49 | * @param ids 岗位编号数组。如果为空,不进行筛选 |
| 50 | 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 | 60 | * @param statuses 状态数组。如果为空,不进行筛选 |
| 61 | 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 | 76 | * @param reqVO 查询条件 |
| 77 | 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 | 93 | * |
| 94 | 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 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | |
| 6 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 7 | 6 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO; |
| 8 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; |
| ... | ... | @@ -38,7 +37,8 @@ public class PostServiceImpl implements PostService { |
| 38 | 37 | @Override |
| 39 | 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 | 43 | PostDO post = PostConvert.INSTANCE.convert(reqVO); |
| 44 | 44 | postMapper.insert(post); |
| ... | ... | @@ -48,7 +48,8 @@ public class PostServiceImpl implements PostService { |
| 48 | 48 | @Override |
| 49 | 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 | 54 | PostDO updateObj = PostConvert.INSTANCE.convert(reqVO); |
| 54 | 55 | postMapper.updateById(updateObj); |
| ... | ... | @@ -57,80 +58,79 @@ public class PostServiceImpl implements PostService { |
| 57 | 58 | @Override |
| 58 | 59 | public void deletePost(Long id) { |
| 59 | 60 | // 校验是否存在 |
| 60 | - this.checkPostExists(id); | |
| 61 | + validatePostExists(id); | |
| 61 | 62 | // 删除部门 |
| 62 | 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 | 76 | PostDO post = postMapper.selectByName(name); |
| 96 | 77 | if (post == null) { |
| 97 | 78 | return; |
| 98 | 79 | } |
| 99 | 80 | // 如果 id 为空,说明不用比较是否为相同 id 的岗位 |
| 100 | 81 | if (id == null) { |
| 101 | - throw ServiceExceptionUtil.exception(POST_NAME_DUPLICATE); | |
| 82 | + throw exception(POST_NAME_DUPLICATE); | |
| 102 | 83 | } |
| 103 | 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 | 90 | PostDO post = postMapper.selectByCode(code); |
| 110 | 91 | if (post == null) { |
| 111 | 92 | return; |
| 112 | 93 | } |
| 113 | 94 | // 如果 id 为空,说明不用比较是否为相同 id 的岗位 |
| 114 | 95 | if (id == null) { |
| 115 | - throw ServiceExceptionUtil.exception(POST_CODE_DUPLICATE); | |
| 96 | + throw exception(POST_CODE_DUPLICATE); | |
| 116 | 97 | } |
| 117 | 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 | 104 | if (id == null) { |
| 124 | 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 | 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 | 134 | if (CollUtil.isEmpty(ids)) { |
| 135 | 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 | 319 | // 校验部门处于开启状态 |
| 320 | 320 | deptService.validateDeptList(CollectionUtils.singleton(deptId)); |
| 321 | 321 | // 校验岗位处于开启状态 |
| 322 | - postService.validPosts(postIds); | |
| 322 | + postService.validatePostList(postIds); | |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 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 | 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 | 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 | 1 | package cn.iocoder.yudao.module.system.service.dept; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; | |
| 4 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 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 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO; |
| 9 | 8 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; |
| 10 | 9 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; |
| 11 | 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 | 12 | import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper; |
| 13 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 14 | 13 | import org.junit.jupiter.api.Test; |
| 15 | 14 | import org.springframework.context.annotation.Import; |
| 16 | 15 | |
| 17 | 16 | import javax.annotation.Resource; |
| 17 | +import java.util.Arrays; | |
| 18 | 18 | import java.util.List; |
| 19 | 19 | import java.util.function.Consumer; |
| 20 | 20 | |
| 21 | 21 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 22 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 22 | 23 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 23 | 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 | 26 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 27 | +import static java.util.Collections.singletonList; | |
| 27 | 28 | import static org.junit.jupiter.api.Assertions.*; |
| 28 | 29 | |
| 30 | +/** | |
| 31 | + * {@link PostServiceImpl} 的单元测试类 | |
| 32 | + * | |
| 33 | + * @author niudehua | |
| 34 | + */ | |
| 29 | 35 | @Import(PostServiceImpl.class) |
| 30 | -public class PostServiceTest extends BaseDbUnitTest { | |
| 36 | +public class PostServiceImplTest extends BaseDbUnitTest { | |
| 31 | 37 | |
| 32 | 38 | @Resource |
| 33 | 39 | private PostServiceImpl postService; |
| 40 | + | |
| 34 | 41 | @Resource |
| 35 | 42 | private PostMapper postMapper; |
| 36 | 43 | |
| 37 | 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 | 134 | // mock 数据 |
| 40 | 135 | PostDO postDO = randomPojo(PostDO.class, o -> { |
| 41 | 136 | o.setName("码仔"); |
| ... | ... | @@ -43,10 +138,9 @@ public class PostServiceTest extends BaseDbUnitTest { |
| 43 | 138 | }); |
| 44 | 139 | postMapper.insert(postDO); |
| 45 | 140 | // 测试 name 不匹配 |
| 46 | - postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员"))); | |
| 141 | + postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员"))); | |
| 47 | 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 | 145 | PostPageReqVO reqVO = new PostPageReqVO(); |
| 52 | 146 | reqVO.setName("码"); |
| ... | ... | @@ -54,7 +148,6 @@ public class PostServiceTest extends BaseDbUnitTest { |
| 54 | 148 | |
| 55 | 149 | // 调用 |
| 56 | 150 | PageResult<PostDO> pageResult = postService.getPostPage(reqVO); |
| 57 | - | |
| 58 | 151 | // 断言 |
| 59 | 152 | assertEquals(1, pageResult.getTotal()); |
| 60 | 153 | assertEquals(1, pageResult.getList().size()); |
| ... | ... | @@ -62,7 +155,7 @@ public class PostServiceTest extends BaseDbUnitTest { |
| 62 | 155 | } |
| 63 | 156 | |
| 64 | 157 | @Test |
| 65 | - void testListPosts() { | |
| 158 | + public void testGetPostList_export() { | |
| 66 | 159 | // mock 数据 |
| 67 | 160 | PostDO postDO = randomPojo(PostDO.class, o -> { |
| 68 | 161 | o.setName("码仔"); |
| ... | ... | @@ -70,124 +163,91 @@ public class PostServiceTest extends BaseDbUnitTest { |
| 70 | 163 | }); |
| 71 | 164 | postMapper.insert(postDO); |
| 72 | 165 | // 测试 name 不匹配 |
| 73 | - postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员"))); | |
| 166 | + postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员"))); | |
| 74 | 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 | 170 | PostExportReqVO reqVO = new PostExportReqVO(); |
| 78 | 171 | reqVO.setName("码"); |
| 79 | 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 | 177 | assertEquals(1, list.size()); |
| 85 | 178 | assertPojoEquals(postDO, list.get(0)); |
| 86 | 179 | } |
| 87 | 180 | |
| 88 | 181 | @Test |
| 89 | - void testGetPost() { | |
| 182 | + public void testGetPostList() { | |
| 90 | 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 | 199 | @Test |
| 117 | - void testUpdatePost_success() { | |
| 200 | + public void testGetPost() { | |
| 118 | 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 | 213 | @Test |
| 136 | - void testDeletePost_success() { | |
| 214 | + public void testValidatePostList_success() { | |
| 137 | 215 | // mock 数据 |
| 138 | - PostDO postDO = randomPostDO(); | |
| 216 | + PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 139 | 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 | 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 | 234 | @Test |
| 168 | - void testCheckPost_codeDuplicateForUpdate() { | |
| 235 | + public void testValidatePostList_notEnable() { | |
| 169 | 236 | // mock 数据 |
| 170 | - PostDO postDO = randomPostDO(); | |
| 237 | + PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.DISABLE.getStatus()); | |
| 171 | 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 | 247 | @SafeVarargs |
| 188 | 248 | private static PostDO randomPostDO(Consumer<PostDO>... consumers) { |
| 189 | 249 | Consumer<PostDO> consumer = (o) -> { |
| 190 | - o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 | |
| 250 | + o.setStatus(randomCommonStatus()); // 保证 status 的范围 | |
| 191 | 251 | }; |
| 192 | 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 | 102 | o.setId(postId); |
| 103 | 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 | 106 | // mock passwordEncoder 的方法 |
| 107 | 107 | when(passwordEncoder.encode(eq(reqVO.getPassword()))).thenReturn("yudaoyuanma"); |
| 108 | 108 | |
| ... | ... | @@ -160,7 +160,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 160 | 160 | o.setId(postId); |
| 161 | 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 | 166 | userService.updateUser(reqVO); | ... | ... |