完善 PostServiceImpl 单元测试

This commit is contained in:
YunaiV
2023-02-01 00:42:38 +08:00
parent 1011de3278
commit bef06ef940
13 changed files with 217 additions and 156 deletions

View File

@@ -18,7 +18,8 @@ public class PostApiImpl implements PostApi {
private PostService postService;
@Override
public void validPosts(Collection<Long> ids) {
postService.validPosts(ids);
public void validPostList(Collection<Long> ids) {
postService.validatePostList(ids);
}
}

View File

@@ -72,7 +72,7 @@ public class PostController {
@ApiOperation(value = "获取岗位精简信息列表", notes = "只包含被开启的岗位,主要用于前端的下拉选项")
public CommonResult<List<PostSimpleRespVO>> getSimplePosts() {
// 获得岗位列表,只要开启状态的
List<PostDO> list = postService.getPosts(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
List<PostDO> list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
// 排序后,返回给前端
list.sort(Comparator.comparing(PostDO::getSort));
return success(PostConvert.INSTANCE.convertList02(list));
@@ -90,7 +90,7 @@ public class PostController {
@PreAuthorize("@ss.hasPermission('system:post:export')")
@OperateLog(type = EXPORT)
public void export(HttpServletResponse response, @Validated PostExportReqVO reqVO) throws IOException {
List<PostDO> posts = postService.getPosts(reqVO);
List<PostDO> posts = postService.getPostList(reqVO);
List<PostExcelVO> data = PostConvert.INSTANCE.convertList03(posts);
// 输出
ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostExcelVO.class, data);

View File

@@ -61,7 +61,7 @@ public class OAuth2UserController {
}
// 获得岗位信息
if (CollUtil.isNotEmpty(user.getPostIds())) {
List<PostDO> posts = postService.getPosts(user.getPostIds());
List<PostDO> posts = postService.getPostList(user.getPostIds());
resp.setPosts(OAuth2UserConvert.INSTANCE.convertList(posts));
}
return success(resp);

View File

@@ -72,7 +72,7 @@ public class UserProfileController {
}
// 获得岗位信息
if (CollUtil.isNotEmpty(user.getPostIds())) {
List<PostDO> posts = postService.getPosts(user.getPostIds());
List<PostDO> posts = postService.getPostList(user.getPostIds());
resp.setPosts(UserConvert.INSTANCE.convertList02(posts));
}
// 获得社交用户信息

View File

@@ -49,8 +49,8 @@ public interface PostService {
* @param ids 岗位编号数组。如果为空,不进行筛选
* @return 部门列表
*/
default List<PostDO> getPosts(@Nullable Collection<Long> ids) {
return getPosts(ids, asSet(CommonStatusEnum.ENABLE.getStatus(), CommonStatusEnum.DISABLE.getStatus()));
default List<PostDO> getPostList(@Nullable Collection<Long> ids) {
return getPostList(ids, asSet(CommonStatusEnum.ENABLE.getStatus(), CommonStatusEnum.DISABLE.getStatus()));
}
/**
@@ -60,7 +60,7 @@ public interface PostService {
* @param statuses 状态数组。如果为空,不进行筛选
* @return 部门列表
*/
List<PostDO> getPosts(@Nullable Collection<Long> ids, @Nullable Collection<Integer> statuses);
List<PostDO> getPostList(@Nullable Collection<Long> ids, @Nullable Collection<Integer> statuses);
/**
* 获得岗位分页列表
@@ -76,7 +76,7 @@ public interface PostService {
* @param reqVO 查询条件
* @return 部门列表
*/
List<PostDO> getPosts(PostExportReqVO reqVO);
List<PostDO> getPostList(PostExportReqVO reqVO);
/**
* 获得岗位信息
@@ -93,6 +93,6 @@ public interface PostService {
*
* @param ids 岗位编号数组
*/
void validPosts(Collection<Long> ids);
void validatePostList(Collection<Long> ids);
}

View File

@@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.service.dept;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
@@ -38,7 +37,8 @@ public class PostServiceImpl implements PostService {
@Override
public Long createPost(PostCreateReqVO reqVO) {
// 校验正确性
this.checkCreateOrUpdate(null, reqVO.getName(), reqVO.getCode());
validatePostForCreateOrUpdate(null, reqVO.getName(), reqVO.getCode());
// 插入岗位
PostDO post = PostConvert.INSTANCE.convert(reqVO);
postMapper.insert(post);
@@ -48,7 +48,8 @@ public class PostServiceImpl implements PostService {
@Override
public void updatePost(PostUpdateReqVO reqVO) {
// 校验正确性
this.checkCreateOrUpdate(reqVO.getId(), reqVO.getName(), reqVO.getCode());
validatePostForCreateOrUpdate(reqVO.getId(), reqVO.getName(), reqVO.getCode());
// 更新岗位
PostDO updateObj = PostConvert.INSTANCE.convert(reqVO);
postMapper.updateById(updateObj);
@@ -57,13 +58,59 @@ public class PostServiceImpl implements PostService {
@Override
public void deletePost(Long id) {
// 校验是否存在
this.checkPostExists(id);
validatePostExists(id);
// 删除部门
postMapper.deleteById(id);
}
private void validatePostForCreateOrUpdate(Long id, String name, String code) {
// 校验自己存在
validatePostExists(id);
// 校验岗位名的唯一性
validatePostNameUnique(id, name);
// 校验岗位编码的唯一性
validatePostCodeUnique(id, code);
}
private void validatePostNameUnique(Long id, String name) {
PostDO post = postMapper.selectByName(name);
if (post == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
if (id == null) {
throw exception(POST_NAME_DUPLICATE);
}
if (!post.getId().equals(id)) {
throw exception(POST_NAME_DUPLICATE);
}
}
private void validatePostCodeUnique(Long id, String code) {
PostDO post = postMapper.selectByCode(code);
if (post == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
if (id == null) {
throw exception(POST_CODE_DUPLICATE);
}
if (!post.getId().equals(id)) {
throw exception(POST_CODE_DUPLICATE);
}
}
private void validatePostExists(Long id) {
if (id == null) {
return;
}
if (postMapper.selectById(id) == null) {
throw exception(POST_NOT_FOUND);
}
}
@Override
public List<PostDO> getPosts(Collection<Long> ids, Collection<Integer> statuses) {
public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses) {
return postMapper.selectList(ids, statuses);
}
@@ -73,7 +120,7 @@ public class PostServiceImpl implements PostService {
}
@Override
public List<PostDO> getPosts(PostExportReqVO reqVO) {
public List<PostDO> getPostList(PostExportReqVO reqVO) {
return postMapper.selectList(reqVO);
}
@@ -82,55 +129,8 @@ public class PostServiceImpl implements PostService {
return postMapper.selectById(id);
}
private void checkCreateOrUpdate(Long id, String name, String code) {
// 校验自己存在
checkPostExists(id);
// 校验岗位名的唯一性
checkPostNameUnique(id, name);
// 校验岗位编码的唯一性
checkPostCodeUnique(id, code);
}
private void checkPostNameUnique(Long id, String name) {
PostDO post = postMapper.selectByName(name);
if (post == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
if (id == null) {
throw ServiceExceptionUtil.exception(POST_NAME_DUPLICATE);
}
if (!post.getId().equals(id)) {
throw ServiceExceptionUtil.exception(POST_NAME_DUPLICATE);
}
}
private void checkPostCodeUnique(Long id, String code) {
PostDO post = postMapper.selectByCode(code);
if (post == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
if (id == null) {
throw ServiceExceptionUtil.exception(POST_CODE_DUPLICATE);
}
if (!post.getId().equals(id)) {
throw ServiceExceptionUtil.exception(POST_CODE_DUPLICATE);
}
}
private void checkPostExists(Long id) {
if (id == null) {
return;
}
PostDO post = postMapper.selectById(id);
if (post == null) {
throw ServiceExceptionUtil.exception(POST_NOT_FOUND);
}
}
@Override
public void validPosts(Collection<Long> ids) {
public void validatePostList(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}

View File

@@ -319,7 +319,7 @@ public class AdminUserServiceImpl implements AdminUserService {
// 校验部门处于开启状态
deptService.validateDeptList(CollectionUtils.singleton(deptId));
// 校验岗位处于开启状态
postService.validPosts(postIds);
postService.validatePostList(postIds);
}
@VisibleForTesting