Commit 045c60fcd70c0da1185223a1a9fbd91d2316ef35
Committed by
Gitee
!392 完善 system 模块的单元测试,单测数量 423,方法行覆盖率 95%,行覆盖率 93%
Merge pull request !392 from 芋道源码/feature/dev-yunai
Showing
120 changed files
with
3175 additions
and
1707 deletions
pom.xml
| ... | ... | @@ -17,7 +17,7 @@ |
| 17 | 17 | <module>yudao-module-system</module> |
| 18 | 18 | <module>yudao-module-infra</module> |
| 19 | 19 | <module>yudao-module-pay</module> |
| 20 | -<!-- <module>yudao-module-bpm</module>--> | |
| 20 | + <module>yudao-module-bpm</module> | |
| 21 | 21 | <!-- <module>yudao-module-visualization</module>--> |
| 22 | 22 | <!-- <module>yudao-module-mp</module>--> |
| 23 | 23 | <!-- <module>yudao-module-mall</module>--> | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/util/DataPermissionUtils.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.datapermission.core.util; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission; | |
| 4 | +import cn.iocoder.yudao.framework.datapermission.core.aop.DataPermissionContextHolder; | |
| 5 | +import lombok.SneakyThrows; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * 数据权限 Util | |
| 9 | + * | |
| 10 | + * @author 芋道源码 | |
| 11 | + */ | |
| 12 | +public class DataPermissionUtils { | |
| 13 | + | |
| 14 | + private static DataPermission DATA_PERMISSION_DISABLE; | |
| 15 | + | |
| 16 | + @DataPermission(enable = false) | |
| 17 | + @SneakyThrows | |
| 18 | + private static DataPermission getDisableDataPermissionDisable() { | |
| 19 | + if (DATA_PERMISSION_DISABLE == null) { | |
| 20 | + DATA_PERMISSION_DISABLE = DataPermissionUtils.class | |
| 21 | + .getDeclaredMethod("getDisableDataPermissionDisable") | |
| 22 | + .getAnnotation(DataPermission.class); | |
| 23 | + } | |
| 24 | + return DATA_PERMISSION_DISABLE; | |
| 25 | + } | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * 忽略数据权限,执行对应的逻辑 | |
| 29 | + * | |
| 30 | + * @param runnable 逻辑 | |
| 31 | + */ | |
| 32 | + public static void executeIgnore(Runnable runnable) { | |
| 33 | + DataPermission dataPermission = getDisableDataPermissionDisable(); | |
| 34 | + DataPermissionContextHolder.add(dataPermission); | |
| 35 | + try { | |
| 36 | + // 执行 runnable | |
| 37 | + runnable.run(); | |
| 38 | + } finally { | |
| 39 | + DataPermissionContextHolder.remove(); | |
| 40 | + } | |
| 41 | + } | |
| 42 | + | |
| 43 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/test/java/cn/iocoder/yudao/framework/datapermission/core/util/DataPermissionUtilsTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.datapermission.core.util; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.datapermission.core.aop.DataPermissionContextHolder; | |
| 4 | +import org.junit.jupiter.api.Test; | |
| 5 | + | |
| 6 | +import static org.junit.jupiter.api.Assertions.*; | |
| 7 | + | |
| 8 | +public class DataPermissionUtilsTest { | |
| 9 | + | |
| 10 | + @Test | |
| 11 | + public void testExecuteIgnore() { | |
| 12 | + DataPermissionUtils.executeIgnore(() -> assertFalse(DataPermissionContextHolder.get().enable())); | |
| 13 | + } | |
| 14 | + | |
| 15 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java
| ... | ... | @@ -49,7 +49,7 @@ public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator { |
| 49 | 49 | log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); |
| 50 | 50 | |
| 51 | 51 | // 第二步,写入到 system 服务 |
| 52 | - errorCodeApi.autoGenerateErrorCodes(autoGenerateDTOs); | |
| 52 | + errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); | |
| 53 | 53 | log.info("[execute][写入到 system 组件完成]"); |
| 54 | 54 | } |
| 55 | 55 | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java
| ... | ... | @@ -32,7 +32,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService { |
| 32 | 32 | |
| 33 | 33 | @Override |
| 34 | 34 | public List<Long> load(Object key) { |
| 35 | - return tenantApi.getTenantIds(); | |
| 35 | + return tenantApi.getTenantIdList(); | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | }); |
| ... | ... | @@ -47,7 +47,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService { |
| 47 | 47 | @Override |
| 48 | 48 | public ServiceException load(Long id) { |
| 49 | 49 | try { |
| 50 | - tenantApi.validTenant(id); | |
| 50 | + tenantApi.validateTenant(id); | |
| 51 | 51 | return SERVICE_EXCEPTION_NULL; |
| 52 | 52 | } catch (ServiceException ex) { |
| 53 | 53 | return ex; | ... | ... |
yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/util/RandomUtils.java
| ... | ... | @@ -50,7 +50,7 @@ public class RandomUtils { |
| 50 | 50 | } |
| 51 | 51 | // 如果是 type、status 结尾的字段,返回 tinyint 范围 |
| 52 | 52 | if (StrUtil.endWithAnyIgnoreCase(attributeMetadata.getAttributeName(), |
| 53 | - "type", "status", "category", "scope")) { | |
| 53 | + "type", "status", "category", "scope", "result")) { | |
| 54 | 54 | return RandomUtil.randomInt(0, TINYINT_MAX + 1); |
| 55 | 55 | } |
| 56 | 56 | return RandomUtil.randomInt(); | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java
| ... | ... | @@ -213,18 +213,18 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService { |
| 213 | 213 | |
| 214 | 214 | private void validTaskAssignRuleOptions(Integer type, Set<Long> options) { |
| 215 | 215 | if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.ROLE.getType())) { |
| 216 | - roleApi.validRoles(options); | |
| 216 | + roleApi.validRoleList(options); | |
| 217 | 217 | } else if (ObjectUtils.equalsAny(type, BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(), |
| 218 | 218 | BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) { |
| 219 | - deptApi.validDepts(options); | |
| 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 | - adminUserApi.validUsers(options); | |
| 223 | + adminUserApi.validateUserList(options); | |
| 224 | 224 | } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER_GROUP.getType())) { |
| 225 | 225 | userGroupService.validUserGroups(options); |
| 226 | 226 | } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.SCRIPT.getType())) { |
| 227 | - dictDataApi.validDictDatas(DictTypeConstants.TASK_ASSIGN_SCRIPT, | |
| 227 | + dictDataApi.validateDictDataList(DictTypeConstants.TASK_ASSIGN_SCRIPT, | |
| 228 | 228 | CollectionUtils.convertSet(options, String::valueOf)); |
| 229 | 229 | } else { |
| 230 | 230 | throw new IllegalArgumentException(format("未知的规则类型({})", type)); |
| ... | ... | @@ -288,12 +288,12 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService { |
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | private Set<Long> calculateTaskCandidateUsersByDeptMember(BpmTaskAssignRuleDO rule) { |
| 291 | - List<AdminUserRespDTO> users = adminUserApi.getUsersByDeptIds(rule.getOptions()); | |
| 291 | + List<AdminUserRespDTO> users = adminUserApi.getUserListByDeptIds(rule.getOptions()); | |
| 292 | 292 | return convertSet(users, AdminUserRespDTO::getId); |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | private Set<Long> calculateTaskCandidateUsersByDeptLeader(BpmTaskAssignRuleDO rule) { |
| 296 | - List<DeptRespDTO> depts = deptApi.getDepts(rule.getOptions()); | |
| 296 | + List<DeptRespDTO> depts = deptApi.getDeptList(rule.getOptions()); | |
| 297 | 297 | return convertSet(depts, DeptRespDTO::getLeaderUserId); |
| 298 | 298 | } |
| 299 | 299 | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImplTest.java
| ... | ... | @@ -86,7 +86,7 @@ public class BpmTaskAssignRuleServiceImplTest extends BaseDbUnitTest { |
| 86 | 86 | // mock 方法 |
| 87 | 87 | List<AdminUserRespDTO> users = CollectionUtils.convertList(asSet(11L, 22L), |
| 88 | 88 | id -> new AdminUserRespDTO().setId(id)); |
| 89 | - when(adminUserApi.getUsersByDeptIds(eq(rule.getOptions()))).thenReturn(users); | |
| 89 | + when(adminUserApi.getUserListByDeptIds(eq(rule.getOptions()))).thenReturn(users); | |
| 90 | 90 | mockGetUserMap(asSet(11L, 22L)); |
| 91 | 91 | |
| 92 | 92 | // 调用 | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApi.java
| ... | ... | @@ -29,7 +29,7 @@ public interface DeptApi { |
| 29 | 29 | * @param ids 部门编号数组 |
| 30 | 30 | * @return 部门信息数组 |
| 31 | 31 | */ |
| 32 | - List<DeptRespDTO> getDepts(Collection<Long> ids); | |
| 32 | + List<DeptRespDTO> getDeptList(Collection<Long> ids); | |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * 校验部门们是否有效。如下情况,视为无效: |
| ... | ... | @@ -38,7 +38,7 @@ public interface DeptApi { |
| 38 | 38 | * |
| 39 | 39 | * @param ids 角色编号数组 |
| 40 | 40 | */ |
| 41 | - void validDepts(Collection<Long> ids); | |
| 41 | + void validateDeptList(Collection<Long> ids); | |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * 获得指定编号的部门 Map |
| ... | ... | @@ -47,7 +47,7 @@ public interface DeptApi { |
| 47 | 47 | * @return 部门 Map |
| 48 | 48 | */ |
| 49 | 49 | default Map<Long, DeptRespDTO> getDeptMap(Set<Long> ids) { |
| 50 | - List<DeptRespDTO> list = getDepts(ids); | |
| 50 | + List<DeptRespDTO> list = getDeptList(ids); | |
| 51 | 51 | return CollectionUtils.convertMap(list, DeptRespDTO::getId); |
| 52 | 52 | } |
| 53 | 53 | ... | ... |
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/api/dict/DictDataApi.java
| ... | ... | @@ -19,7 +19,7 @@ public interface DictDataApi { |
| 19 | 19 | * @param dictType 字典类型 |
| 20 | 20 | * @param values 字典数据值的数组 |
| 21 | 21 | */ |
| 22 | - void validDictDatas(String dictType, Collection<String> values); | |
| 22 | + void validateDictDataList(String dictType, Collection<String> values); | |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * 获得指定的字典数据,从缓存中 | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java
| ... | ... | @@ -19,7 +19,7 @@ public interface ErrorCodeApi { |
| 19 | 19 | * |
| 20 | 20 | * @param autoGenerateDTOs 错误码信息 |
| 21 | 21 | */ |
| 22 | - void autoGenerateErrorCodes(@Valid List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs); | |
| 22 | + void autoGenerateErrorCodeList(@Valid List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs); | |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * 增量获得错误码数组 | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApi.java
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/sms/SmsCodeApi.java
| 1 | 1 | package cn.iocoder.yudao.module.system.api.sms; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 4 | -import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO; | |
| 4 | +import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO; | |
| 5 | 5 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; |
| 6 | 6 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; |
| 7 | 7 | |
| ... | ... | @@ -35,6 +35,6 @@ public interface SmsCodeApi { |
| 35 | 35 | * |
| 36 | 36 | * @param reqDTO 校验请求 |
| 37 | 37 | */ |
| 38 | - void checkSmsCode(@Valid SmsCodeCheckReqDTO reqDTO); | |
| 38 | + void validateSmsCode(@Valid SmsCodeValidateReqDTO reqDTO); | |
| 39 | 39 | |
| 40 | 40 | } | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/sms/dto/code/SmsCodeCheckReqDTO.java renamed to yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/sms/dto/code/SmsCodeValidateReqDTO.java
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/tenant/TenantApi.java
| ... | ... | @@ -14,13 +14,13 @@ public interface TenantApi { |
| 14 | 14 | * |
| 15 | 15 | * @return 租户编号数组 |
| 16 | 16 | */ |
| 17 | - List<Long> getTenantIds(); | |
| 17 | + List<Long> getTenantIdList(); | |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * 校验租户是否合法 |
| 21 | 21 | * |
| 22 | 22 | * @param id 租户编号 |
| 23 | 23 | */ |
| 24 | - void validTenant(Long id); | |
| 24 | + void validateTenant(Long id); | |
| 25 | 25 | |
| 26 | 26 | } | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApi.java
| ... | ... | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| 6 | 6 | import java.util.Collection; |
| 7 | 7 | import java.util.List; |
| 8 | 8 | import java.util.Map; |
| 9 | -import java.util.Set; | |
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | 11 | * Admin 用户 API 接口 |
| ... | ... | @@ -29,7 +28,7 @@ public interface AdminUserApi { |
| 29 | 28 | * @param ids 用户 ID 们 |
| 30 | 29 | * @return 用户对象信息 |
| 31 | 30 | */ |
| 32 | - List<AdminUserRespDTO> getUsers(Collection<Long> ids); | |
| 31 | + List<AdminUserRespDTO> getUserList(Collection<Long> ids); | |
| 33 | 32 | |
| 34 | 33 | /** |
| 35 | 34 | * 获得指定部门的用户数组 |
| ... | ... | @@ -37,7 +36,7 @@ public interface AdminUserApi { |
| 37 | 36 | * @param deptIds 部门数组 |
| 38 | 37 | * @return 用户数组 |
| 39 | 38 | */ |
| 40 | - List<AdminUserRespDTO> getUsersByDeptIds(Collection<Long> deptIds); | |
| 39 | + List<AdminUserRespDTO> getUserListByDeptIds(Collection<Long> deptIds); | |
| 41 | 40 | |
| 42 | 41 | /** |
| 43 | 42 | * 获得指定岗位的用户数组 |
| ... | ... | @@ -54,7 +53,7 @@ public interface AdminUserApi { |
| 54 | 53 | * @return 用户 Map |
| 55 | 54 | */ |
| 56 | 55 | default Map<Long, AdminUserRespDTO> getUserMap(Collection<Long> ids) { |
| 57 | - List<AdminUserRespDTO> users = getUsers(ids); | |
| 56 | + List<AdminUserRespDTO> users = getUserList(ids); | |
| 58 | 57 | return CollectionUtils.convertMap(users, AdminUserRespDTO::getId); |
| 59 | 58 | } |
| 60 | 59 | |
| ... | ... | @@ -65,6 +64,6 @@ public interface AdminUserApi { |
| 65 | 64 | * |
| 66 | 65 | * @param ids 用户编号数组 |
| 67 | 66 | */ |
| 68 | - void validUsers(Set<Long> ids); | |
| 67 | + void validateUserList(Collection<Long> ids); | |
| 69 | 68 | |
| 70 | 69 | } | ... | ... |
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-api/src/main/java/cn/iocoder/yudao/module/system/enums/permission/MenuIdEnum.java deleted
| 1 | -package cn.iocoder.yudao.module.system.enums.permission; | |
| 2 | - | |
| 3 | -import lombok.AllArgsConstructor; | |
| 4 | -import lombok.Getter; | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * Menu 编号枚举 | |
| 8 | - */ | |
| 9 | -@Getter | |
| 10 | -@AllArgsConstructor | |
| 11 | -public enum MenuIdEnum { | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * 根节点 | |
| 15 | - */ | |
| 16 | - ROOT(0L); | |
| 17 | - | |
| 18 | - private final Long id; | |
| 19 | - | |
| 20 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApiImpl.java
| ... | ... | @@ -28,14 +28,14 @@ public class DeptApiImpl implements DeptApi { |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | @Override |
| 31 | - public List<DeptRespDTO> getDepts(Collection<Long> ids) { | |
| 32 | - List<DeptDO> depts = deptService.getDepts(ids); | |
| 31 | + public List<DeptRespDTO> getDeptList(Collection<Long> ids) { | |
| 32 | + List<DeptDO> depts = deptService.getDeptList(ids); | |
| 33 | 33 | return DeptConvert.INSTANCE.convertList03(depts); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | @Override |
| 37 | - public void validDepts(Collection<Long> ids) { | |
| 38 | - deptService.validDepts(ids); | |
| 37 | + public void validateDeptList(Collection<Long> ids) { | |
| 38 | + deptService.validateDeptList(ids); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | } | ... | ... |
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/api/dict/DictDataApiImpl.java
| ... | ... | @@ -21,8 +21,8 @@ public class DictDataApiImpl implements DictDataApi { |
| 21 | 21 | private DictDataService dictDataService; |
| 22 | 22 | |
| 23 | 23 | @Override |
| 24 | - public void validDictDatas(String dictType, Collection<String> values) { | |
| 25 | - dictDataService.validDictDatas(dictType, values); | |
| 24 | + public void validateDictDataList(String dictType, Collection<String> values) { | |
| 25 | + dictDataService.validateDictDataList(dictType, values); | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | @Override | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java
| ... | ... | @@ -21,7 +21,7 @@ public class ErrorCodeApiImpl implements ErrorCodeApi { |
| 21 | 21 | private ErrorCodeService errorCodeService; |
| 22 | 22 | |
| 23 | 23 | @Override |
| 24 | - public void autoGenerateErrorCodes(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) { | |
| 24 | + public void autoGenerateErrorCodeList(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) { | |
| 25 | 25 | errorCodeService.autoGenerateErrorCodes(autoGenerateDTOs); |
| 26 | 26 | } |
| 27 | 27 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApiImpl.java
| ... | ... | @@ -18,7 +18,7 @@ public class RoleApiImpl implements RoleApi { |
| 18 | 18 | private RoleService roleService; |
| 19 | 19 | |
| 20 | 20 | @Override |
| 21 | - public void validRoles(Collection<Long> ids) { | |
| 22 | - roleService.validRoles(ids); | |
| 21 | + public void validRoleList(Collection<Long> ids) { | |
| 22 | + roleService.validateRoleList(ids); | |
| 23 | 23 | } |
| 24 | 24 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/sms/SmsCodeApiImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.system.api.sms; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO; | |
| 3 | +import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO; | |
| 4 | 4 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; |
| 5 | 5 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; |
| 6 | 6 | import cn.iocoder.yudao.module.system.service.sms.SmsCodeService; |
| ... | ... | @@ -32,8 +32,8 @@ public class SmsCodeApiImpl implements SmsCodeApi { |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | @Override |
| 35 | - public void checkSmsCode(SmsCodeCheckReqDTO reqDTO) { | |
| 36 | - smsCodeService.checkSmsCode(reqDTO); | |
| 35 | + public void validateSmsCode(SmsCodeValidateReqDTO reqDTO) { | |
| 36 | + smsCodeService.validateSmsCode(reqDTO); | |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/tenant/TenantApiImpl.java
| ... | ... | @@ -18,12 +18,12 @@ public class TenantApiImpl implements TenantApi { |
| 18 | 18 | private TenantService tenantService; |
| 19 | 19 | |
| 20 | 20 | @Override |
| 21 | - public List<Long> getTenantIds() { | |
| 22 | - return tenantService.getTenantIds(); | |
| 21 | + public List<Long> getTenantIdList() { | |
| 22 | + return tenantService.getTenantIdList(); | |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | @Override |
| 26 | - public void validTenant(Long id) { | |
| 26 | + public void validateTenant(Long id) { | |
| 27 | 27 | tenantService.validTenant(id); |
| 28 | 28 | } |
| 29 | 29 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApiImpl.java
| ... | ... | @@ -9,7 +9,6 @@ import org.springframework.stereotype.Service; |
| 9 | 9 | import javax.annotation.Resource; |
| 10 | 10 | import java.util.Collection; |
| 11 | 11 | import java.util.List; |
| 12 | -import java.util.Set; | |
| 13 | 12 | |
| 14 | 13 | /** |
| 15 | 14 | * Admin 用户 API 实现类 |
| ... | ... | @@ -29,26 +28,26 @@ public class AdminUserApiImpl implements AdminUserApi { |
| 29 | 28 | } |
| 30 | 29 | |
| 31 | 30 | @Override |
| 32 | - public List<AdminUserRespDTO> getUsers(Collection<Long> ids) { | |
| 33 | - List<AdminUserDO> users = userService.getUsers(ids); | |
| 31 | + public List<AdminUserRespDTO> getUserList(Collection<Long> ids) { | |
| 32 | + List<AdminUserDO> users = userService.getUserList(ids); | |
| 34 | 33 | return UserConvert.INSTANCE.convertList4(users); |
| 35 | 34 | } |
| 36 | 35 | |
| 37 | 36 | @Override |
| 38 | - public List<AdminUserRespDTO> getUsersByDeptIds(Collection<Long> deptIds) { | |
| 39 | - List<AdminUserDO> users = userService.getUsersByDeptIds(deptIds); | |
| 37 | + public List<AdminUserRespDTO> getUserListByDeptIds(Collection<Long> deptIds) { | |
| 38 | + List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds); | |
| 40 | 39 | return UserConvert.INSTANCE.convertList4(users); |
| 41 | 40 | } |
| 42 | 41 | |
| 43 | 42 | @Override |
| 44 | 43 | public List<AdminUserRespDTO> getUsersByPostIds(Collection<Long> postIds) { |
| 45 | - List<AdminUserDO> users = userService.getUsersByPostIds(postIds); | |
| 44 | + List<AdminUserDO> users = userService.getUserListByPostIds(postIds); | |
| 46 | 45 | return UserConvert.INSTANCE.convertList4(users); |
| 47 | 46 | } |
| 48 | 47 | |
| 49 | 48 | @Override |
| 50 | - public void validUsers(Set<Long> ids) { | |
| 51 | - userService.validUsers(ids); | |
| 49 | + public void validateUserList(Collection<Long> ids) { | |
| 50 | + userService.validateUserList(ids); | |
| 52 | 51 | } |
| 53 | 52 | |
| 54 | 53 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
| ... | ... | @@ -97,7 +97,7 @@ public class AuthController { |
| 97 | 97 | } |
| 98 | 98 | // 获得角色列表 |
| 99 | 99 | Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(getLoginUserId(), singleton(CommonStatusEnum.ENABLE.getStatus())); |
| 100 | - List<RoleDO> roleList = roleService.getRolesFromCache(roleIds); | |
| 100 | + List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds); | |
| 101 | 101 | // 获得菜单列表 |
| 102 | 102 | List<MenuDO> menuList = permissionService.getRoleMenuListFromCache(roleIds, |
| 103 | 103 | SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()), |
| ... | ... | @@ -108,7 +108,7 @@ public class AuthController { |
| 108 | 108 | |
| 109 | 109 | @GetMapping("/list-menus") |
| 110 | 110 | @ApiOperation("获得登录用户的菜单列表") |
| 111 | - public CommonResult<List<AuthMenuRespVO>> getMenus() { | |
| 111 | + public CommonResult<List<AuthMenuRespVO>> getMenuList() { | |
| 112 | 112 | // 获得角色列表 |
| 113 | 113 | Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(getLoginUserId(), singleton(CommonStatusEnum.ENABLE.getStatus())); |
| 114 | 114 | // 获得用户拥有的菜单列表 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/DeptController.java
| ... | ... | @@ -57,19 +57,19 @@ public class DeptController { |
| 57 | 57 | @GetMapping("/list") |
| 58 | 58 | @ApiOperation("获取部门列表") |
| 59 | 59 | @PreAuthorize("@ss.hasPermission('system:dept:query')") |
| 60 | - public CommonResult<List<DeptRespVO>> listDepts(DeptListReqVO reqVO) { | |
| 61 | - List<DeptDO> list = deptService.getSimpleDepts(reqVO); | |
| 60 | + public CommonResult<List<DeptRespVO>> getDeptList(DeptListReqVO reqVO) { | |
| 61 | + List<DeptDO> list = deptService.getDeptList(reqVO); | |
| 62 | 62 | list.sort(Comparator.comparing(DeptDO::getSort)); |
| 63 | 63 | return success(DeptConvert.INSTANCE.convertList(list)); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | @GetMapping("/list-all-simple") |
| 67 | 67 | @ApiOperation(value = "获取部门精简信息列表", notes = "只包含被开启的部门,主要用于前端的下拉选项") |
| 68 | - public CommonResult<List<DeptSimpleRespVO>> getSimpleDepts() { | |
| 68 | + public CommonResult<List<DeptSimpleRespVO>> getSimpleDeptList() { | |
| 69 | 69 | // 获得部门列表,只要开启状态的 |
| 70 | 70 | DeptListReqVO reqVO = new DeptListReqVO(); |
| 71 | 71 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 72 | - List<DeptDO> list = deptService.getSimpleDepts(reqVO); | |
| 72 | + List<DeptDO> list = deptService.getDeptList(reqVO); | |
| 73 | 73 | // 排序后,返回给前端 |
| 74 | 74 | list.sort(Comparator.comparing(DeptDO::getSort)); |
| 75 | 75 | return success(DeptConvert.INSTANCE.convertList02(list)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/PostController.java
| ... | ... | @@ -70,9 +70,9 @@ public class PostController { |
| 70 | 70 | |
| 71 | 71 | @GetMapping("/list-all-simple") |
| 72 | 72 | @ApiOperation(value = "获取岗位精简信息列表", notes = "只包含被开启的岗位,主要用于前端的下拉选项") |
| 73 | - public CommonResult<List<PostSimpleRespVO>> getSimplePosts() { | |
| 73 | + public CommonResult<List<PostSimpleRespVO>> getSimplePostList() { | |
| 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/dict/DictDataController.java
| ... | ... | @@ -61,8 +61,8 @@ public class DictDataController { |
| 61 | 61 | @GetMapping("/list-all-simple") |
| 62 | 62 | @ApiOperation(value = "获得全部字典数据列表", notes = "一般用于管理后台缓存字典数据在本地") |
| 63 | 63 | // 无需添加权限认证,因为前端全局都需要 |
| 64 | - public CommonResult<List<DictDataSimpleRespVO>> getSimpleDictDatas() { | |
| 65 | - List<DictDataDO> list = dictDataService.getDictDatas(); | |
| 64 | + public CommonResult<List<DictDataSimpleRespVO>> getSimpleDictDataList() { | |
| 65 | + List<DictDataDO> list = dictDataService.getDictDataList(); | |
| 66 | 66 | return success(DictDataConvert.INSTANCE.convertList(list)); |
| 67 | 67 | } |
| 68 | 68 | |
| ... | ... | @@ -86,7 +86,7 @@ public class DictDataController { |
| 86 | 86 | @PreAuthorize("@ss.hasPermission('system:dict:export')") |
| 87 | 87 | @OperateLog(type = EXPORT) |
| 88 | 88 | public void export(HttpServletResponse response, @Valid DictDataExportReqVO reqVO) throws IOException { |
| 89 | - List<DictDataDO> list = dictDataService.getDictDatas(reqVO); | |
| 89 | + List<DictDataDO> list = dictDataService.getDictDataList(reqVO); | |
| 90 | 90 | List<DictDataExcelVO> data = DictDataConvert.INSTANCE.convertList02(list); |
| 91 | 91 | // 输出 |
| 92 | 92 | ExcelUtils.write(response, "字典数据.xls", "数据列表", DictDataExcelVO.class, data); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dict/DictTypeController.java
| ... | ... | @@ -76,7 +76,7 @@ public class DictTypeController { |
| 76 | 76 | @GetMapping("/list-all-simple") |
| 77 | 77 | @ApiOperation(value = "获得全部字典类型列表", notes = "包括开启 + 禁用的字典类型,主要用于前端的下拉选项") |
| 78 | 78 | // 无需添加权限认证,因为前端全局都需要 |
| 79 | - public CommonResult<List<DictTypeSimpleRespVO>> listSimpleDictTypes() { | |
| 79 | + public CommonResult<List<DictTypeSimpleRespVO>> getSimpleDictTypeList() { | |
| 80 | 80 | List<DictTypeDO> list = dictTypeService.getDictTypeList(); |
| 81 | 81 | return success(DictTypeConvert.INSTANCE.convertList(list)); |
| 82 | 82 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/logger/OperateLogController.java
| ... | ... | @@ -71,7 +71,7 @@ public class OperateLogController { |
| 71 | 71 | @PreAuthorize("@ss.hasPermission('system:operate-log:export')") |
| 72 | 72 | @OperateLog(type = EXPORT) |
| 73 | 73 | public void exportOperateLog(HttpServletResponse response, @Valid OperateLogExportReqVO reqVO) throws IOException { |
| 74 | - List<OperateLogDO> list = operateLogService.getOperateLogs(reqVO); | |
| 74 | + List<OperateLogDO> list = operateLogService.getOperateLogList(reqVO); | |
| 75 | 75 | |
| 76 | 76 | // 获得拼接需要的数据 |
| 77 | 77 | Collection<Long> userIds = CollectionUtils.convertList(list, OperateLogDO::getUserId); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notice/NoticeController.java
| ... | ... | @@ -57,8 +57,8 @@ public class NoticeController { |
| 57 | 57 | @GetMapping("/page") |
| 58 | 58 | @ApiOperation("获取通知公告列表") |
| 59 | 59 | @PreAuthorize("@ss.hasPermission('system:notice:query')") |
| 60 | - public CommonResult<PageResult<NoticeRespVO>> pageNotices(@Validated NoticePageReqVO reqVO) { | |
| 61 | - return success(NoticeConvert.INSTANCE.convertPage(noticeService.pageNotices(reqVO))); | |
| 60 | + public CommonResult<PageResult<NoticeRespVO>> getNoticePage(@Validated NoticePageReqVO reqVO) { | |
| 61 | + return success(NoticeConvert.INSTANCE.convertPage(noticeService.getNoticePage(reqVO))); | |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | @GetMapping("/get") | ... | ... |
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/permission/MenuController.java
| ... | ... | @@ -57,8 +57,8 @@ public class MenuController { |
| 57 | 57 | @GetMapping("/list") |
| 58 | 58 | @ApiOperation(value = "获取菜单列表", notes = "用于【菜单管理】界面") |
| 59 | 59 | @PreAuthorize("@ss.hasPermission('system:menu:query')") |
| 60 | - public CommonResult<List<MenuRespVO>> getMenus(MenuListReqVO reqVO) { | |
| 61 | - List<MenuDO> list = menuService.getMenus(reqVO); | |
| 60 | + public CommonResult<List<MenuRespVO>> getMenuList(MenuListReqVO reqVO) { | |
| 61 | + List<MenuDO> list = menuService.getMenuList(reqVO); | |
| 62 | 62 | list.sort(Comparator.comparing(MenuDO::getSort)); |
| 63 | 63 | return success(MenuConvert.INSTANCE.convertList(list)); |
| 64 | 64 | } |
| ... | ... | @@ -66,11 +66,11 @@ public class MenuController { |
| 66 | 66 | @GetMapping("/list-all-simple") |
| 67 | 67 | @ApiOperation(value = "获取菜单精简信息列表", notes = "只包含被开启的菜单,用于【角色分配菜单】功能的选项。" + |
| 68 | 68 | "在多租户的场景下,会只返回租户所在套餐有的菜单") |
| 69 | - public CommonResult<List<MenuSimpleRespVO>> getSimpleMenus() { | |
| 69 | + public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() { | |
| 70 | 70 | // 获得菜单列表,只要开启状态的 |
| 71 | 71 | MenuListReqVO reqVO = new MenuListReqVO(); |
| 72 | 72 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 73 | - List<MenuDO> list = menuService.getTenantMenus(reqVO); | |
| 73 | + List<MenuDO> list = menuService.getMenuListByTenant(reqVO); | |
| 74 | 74 | // 排序后,返回给前端 |
| 75 | 75 | list.sort(Comparator.comparing(MenuDO::getSort)); |
| 76 | 76 | return success(MenuConvert.INSTANCE.convertList02(list)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/RoleController.java
| ... | ... | @@ -20,12 +20,12 @@ import javax.annotation.Resource; |
| 20 | 20 | import javax.servlet.http.HttpServletResponse; |
| 21 | 21 | import javax.validation.Valid; |
| 22 | 22 | import java.io.IOException; |
| 23 | -import java.util.Collections; | |
| 24 | 23 | import java.util.Comparator; |
| 25 | 24 | import java.util.List; |
| 26 | 25 | |
| 27 | 26 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 28 | 27 | import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; |
| 28 | +import static java.util.Collections.singleton; | |
| 29 | 29 | |
| 30 | 30 | @Api(tags = "管理后台 - 角色") |
| 31 | 31 | @RestController |
| ... | ... | @@ -85,9 +85,9 @@ public class RoleController { |
| 85 | 85 | |
| 86 | 86 | @GetMapping("/list-all-simple") |
| 87 | 87 | @ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色,主要用于前端的下拉选项") |
| 88 | - public CommonResult<List<RoleSimpleRespVO>> getSimpleRoles() { | |
| 88 | + public CommonResult<List<RoleSimpleRespVO>> getSimpleRoleList() { | |
| 89 | 89 | // 获得角色列表,只要开启状态的 |
| 90 | - List<RoleDO> list = roleService.getRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); | |
| 90 | + List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus())); | |
| 91 | 91 | // 排序后,返回给前端 |
| 92 | 92 | list.sort(Comparator.comparing(RoleDO::getSort)); |
| 93 | 93 | return success(RoleConvert.INSTANCE.convertList02(list)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/sensitiveword/SensitiveWordController.java
| ... | ... | @@ -90,8 +90,8 @@ public class SensitiveWordController { |
| 90 | 90 | @GetMapping("/get-tags") |
| 91 | 91 | @ApiOperation("获取所有敏感词的标签数组") |
| 92 | 92 | @PreAuthorize("@ss.hasPermission('system:sensitive-word:query')") |
| 93 | - public CommonResult<Set<String>> getSensitiveWordTags() throws IOException { | |
| 94 | - return success(sensitiveWordService.getSensitiveWordTags()); | |
| 93 | + public CommonResult<Set<String>> getSensitiveWordTagSet() { | |
| 94 | + return success(sensitiveWordService.getSensitiveWordTagSet()); | |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | @GetMapping("/validate-text") | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/sms/SmsChannelController.java
| ... | ... | @@ -70,7 +70,7 @@ public class SmsChannelController { |
| 70 | 70 | |
| 71 | 71 | @GetMapping("/list-all-simple") |
| 72 | 72 | @ApiOperation(value = "获得短信渠道精简列表", notes = "包含被禁用的短信渠道") |
| 73 | - public CommonResult<List<SmsChannelSimpleRespVO>> getSimpleSmsChannels() { | |
| 73 | + public CommonResult<List<SmsChannelSimpleRespVO>> getSimpleSmsChannelList() { | |
| 74 | 74 | List<SmsChannelDO> list = smsChannelService.getSmsChannelList(); |
| 75 | 75 | // 排序后,返回给前端 |
| 76 | 76 | list.sort(Comparator.comparing(SmsChannelDO::getId)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserController.java
| ... | ... | @@ -111,9 +111,9 @@ public class UserController { |
| 111 | 111 | |
| 112 | 112 | @GetMapping("/list-all-simple") |
| 113 | 113 | @ApiOperation(value = "获取用户精简信息列表", notes = "只包含被开启的用户,主要用于前端的下拉选项") |
| 114 | - public CommonResult<List<UserSimpleRespVO>> getSimpleUsers() { | |
| 114 | + public CommonResult<List<UserSimpleRespVO>> getSimpleUserList() { | |
| 115 | 115 | // 获用户列表,只要开启状态的 |
| 116 | - List<AdminUserDO> list = userService.getUsersByStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 116 | + List<AdminUserDO> list = userService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 117 | 117 | // 排序后,返回给前端 |
| 118 | 118 | return success(UserConvert.INSTANCE.convertList04(list)); |
| 119 | 119 | } |
| ... | ... | @@ -122,7 +122,7 @@ public class UserController { |
| 122 | 122 | @ApiOperation("获得用户详情") |
| 123 | 123 | @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) |
| 124 | 124 | @PreAuthorize("@ss.hasPermission('system:user:query')") |
| 125 | - public CommonResult<UserRespVO> getInfo(@RequestParam("id") Long id) { | |
| 125 | + public CommonResult<UserRespVO> getUser(@RequestParam("id") Long id) { | |
| 126 | 126 | return success(UserConvert.INSTANCE.convert(userService.getUser(id))); |
| 127 | 127 | } |
| 128 | 128 | |
| ... | ... | @@ -130,10 +130,10 @@ public class UserController { |
| 130 | 130 | @ApiOperation("导出用户") |
| 131 | 131 | @PreAuthorize("@ss.hasPermission('system:user:export')") |
| 132 | 132 | @OperateLog(type = EXPORT) |
| 133 | - public void exportUsers(@Validated UserExportReqVO reqVO, | |
| 134 | - HttpServletResponse response) throws IOException { | |
| 133 | + public void exportUserList(@Validated UserExportReqVO reqVO, | |
| 134 | + HttpServletResponse response) throws IOException { | |
| 135 | 135 | // 获得用户列表 |
| 136 | - List<AdminUserDO> users = userService.getUsers(reqVO); | |
| 136 | + List<AdminUserDO> users = userService.getUserList(reqVO); | |
| 137 | 137 | |
| 138 | 138 | // 获得拼接需要的数据 |
| 139 | 139 | Collection<Long> deptIds = convertList(users, AdminUserDO::getDeptId); |
| ... | ... | @@ -183,7 +183,7 @@ public class UserController { |
| 183 | 183 | public CommonResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file, |
| 184 | 184 | @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception { |
| 185 | 185 | List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class); |
| 186 | - return success(userService.importUsers(list, updateSupport)); | |
| 186 | + return success(userService.importUserList(list, updateSupport)); | |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java
| ... | ... | @@ -63,7 +63,7 @@ public class UserProfileController { |
| 63 | 63 | AdminUserDO user = userService.getUser(getLoginUserId()); |
| 64 | 64 | UserProfileRespVO resp = UserConvert.INSTANCE.convert03(user); |
| 65 | 65 | // 获得用户角色 |
| 66 | - List<RoleDO> userRoles = roleService.getRolesFromCache(permissionService.getUserRoleIdListByUserId(user.getId())); | |
| 66 | + List<RoleDO> userRoles = roleService.getRoleListFromCache(permissionService.getUserRoleIdListByUserId(user.getId())); | |
| 67 | 67 | resp.setRoles(UserConvert.INSTANCE.convertList(userRoles)); |
| 68 | 68 | // 获得部门信息 |
| 69 | 69 | if (user.getDeptId() != null) { |
| ... | ... | @@ -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/convert/auth/AuthConvert.java
| ... | ... | @@ -9,13 +9,15 @@ import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; |
| 9 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; |
| 10 | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; |
| 11 | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 12 | -import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum; | |
| 13 | 12 | import org.mapstruct.Mapper; |
| 14 | 13 | import org.mapstruct.factory.Mappers; |
| 15 | 14 | import org.slf4j.LoggerFactory; |
| 16 | 15 | |
| 17 | 16 | import java.util.*; |
| 18 | 17 | |
| 18 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; | |
| 19 | +import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; | |
| 20 | + | |
| 19 | 21 | @Mapper |
| 20 | 22 | public interface AuthConvert { |
| 21 | 23 | |
| ... | ... | @@ -47,7 +49,7 @@ public interface AuthConvert { |
| 47 | 49 | Map<Long, AuthMenuRespVO> treeNodeMap = new LinkedHashMap<>(); |
| 48 | 50 | menuList.forEach(menu -> treeNodeMap.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu))); |
| 49 | 51 | // 处理父子关系 |
| 50 | - treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(MenuIdEnum.ROOT.getId())).forEach(childNode -> { | |
| 52 | + treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> { | |
| 51 | 53 | // 获得父节点 |
| 52 | 54 | AuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId()); |
| 53 | 55 | if (parentNode == null) { |
| ... | ... | @@ -62,7 +64,7 @@ public interface AuthConvert { |
| 62 | 64 | parentNode.getChildren().add(childNode); |
| 63 | 65 | }); |
| 64 | 66 | // 获得到所有的根节点 |
| 65 | - return CollectionUtils.filterList(treeNodeMap.values(), node -> MenuIdEnum.ROOT.getId().equals(node.getParentId())); | |
| 67 | + return filterList(treeNodeMap.values(), node -> ID_ROOT.equals(node.getParentId())); | |
| 66 | 68 | } |
| 67 | 69 | |
| 68 | 70 | SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialLoginReqVO reqVO); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/permission/MenuDO.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java
| ... | ... | @@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 5 | 5 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO; |
| 6 | 6 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; |
| 7 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 8 | 7 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 8 | |
| 10 | 9 | import java.util.List; |
| ... | ... | @@ -19,9 +18,7 @@ public interface DeptMapper extends BaseMapperX<DeptDO> { |
| 19 | 18 | } |
| 20 | 19 | |
| 21 | 20 | default DeptDO selectByParentIdAndName(Long parentId, String name) { |
| 22 | - return selectOne(new LambdaQueryWrapper<DeptDO>() | |
| 23 | - .eq(DeptDO::getParentId, parentId) | |
| 24 | - .eq(DeptDO::getName, name)); | |
| 21 | + return selectOne(DeptDO::getParentId, parentId, DeptDO::getName, name); | |
| 25 | 22 | } |
| 26 | 23 | |
| 27 | 24 | default Long selectCountByParentId(Long parentId) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/PostMapper.java
| ... | ... | @@ -2,11 +2,10 @@ package cn.iocoder.yudao.module.system.dal.mysql.dept; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | -import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; | |
| 5 | +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | |
| 6 | 6 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; |
| 9 | -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 10 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 11 | 10 | |
| 12 | 11 | import java.util.Collection; |
| ... | ... | @@ -16,31 +15,32 @@ import java.util.List; |
| 16 | 15 | public interface PostMapper extends BaseMapperX<PostDO> { |
| 17 | 16 | |
| 18 | 17 | default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses) { |
| 19 | - return selectList(new QueryWrapperX<PostDO>().inIfPresent("id", ids) | |
| 20 | - .inIfPresent("status", statuses)); | |
| 18 | + return selectList(new LambdaQueryWrapperX<PostDO>() | |
| 19 | + .inIfPresent(PostDO::getId, ids) | |
| 20 | + .inIfPresent(PostDO::getStatus, statuses)); | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | default PageResult<PostDO> selectPage(PostPageReqVO reqVO) { |
| 24 | - return selectPage(reqVO, new QueryWrapperX<PostDO>() | |
| 25 | - .likeIfPresent("code", reqVO.getCode()) | |
| 26 | - .likeIfPresent("name", reqVO.getName()) | |
| 27 | - .eqIfPresent("status", reqVO.getStatus()) | |
| 28 | - .orderByDesc("id")); | |
| 24 | + return selectPage(reqVO, new LambdaQueryWrapperX<PostDO>() | |
| 25 | + .likeIfPresent(PostDO::getCode, reqVO.getCode()) | |
| 26 | + .likeIfPresent(PostDO::getName, reqVO.getName()) | |
| 27 | + .eqIfPresent(PostDO::getStatus, reqVO.getStatus()) | |
| 28 | + .orderByDesc(PostDO::getId)); | |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | default List<PostDO> selectList(PostExportReqVO reqVO) { |
| 32 | - return selectList(new QueryWrapperX<PostDO>() | |
| 33 | - .likeIfPresent("code", reqVO.getCode()) | |
| 34 | - .likeIfPresent("name", reqVO.getName()) | |
| 35 | - .eqIfPresent("status", reqVO.getStatus())); | |
| 32 | + return selectList(new LambdaQueryWrapperX<PostDO>() | |
| 33 | + .likeIfPresent(PostDO::getCode, reqVO.getCode()) | |
| 34 | + .likeIfPresent(PostDO::getName, reqVO.getName()) | |
| 35 | + .eqIfPresent(PostDO::getStatus, reqVO.getStatus())); | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | default PostDO selectByName(String name) { |
| 39 | - return selectOne(new QueryWrapper<PostDO>().eq("name", name)); | |
| 39 | + return selectOne(PostDO::getName, name); | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | default PostDO selectByCode(String code) { |
| 43 | - return selectOne(new QueryWrapper<PostDO>().eq("code", code)); | |
| 43 | + return selectOne(PostDO::getCode, code); | |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java
| ... | ... | @@ -13,8 +13,7 @@ import java.util.List; |
| 13 | 13 | public interface UserPostMapper extends BaseMapperX<UserPostDO> { |
| 14 | 14 | |
| 15 | 15 | default List<UserPostDO> selectListByUserId(Long userId) { |
| 16 | - return selectList(new LambdaQueryWrapperX<UserPostDO>() | |
| 17 | - .eq(UserPostDO::getUserId, userId)); | |
| 16 | + return selectList(UserPostDO::getUserId, userId); | |
| 18 | 17 | } |
| 19 | 18 | |
| 20 | 19 | default void deleteByUserIdAndPostId(Long userId, Collection<Long> postIds) { |
| ... | ... | @@ -24,11 +23,10 @@ public interface UserPostMapper extends BaseMapperX<UserPostDO> { |
| 24 | 23 | } |
| 25 | 24 | |
| 26 | 25 | default List<UserPostDO> selectListByPostIds(Collection<Long> postIds) { |
| 27 | - return selectList(new LambdaQueryWrapperX<UserPostDO>() | |
| 28 | - .in(UserPostDO::getPostId, postIds)); | |
| 26 | + return selectList(UserPostDO::getPostId, postIds); | |
| 29 | 27 | } |
| 30 | 28 | |
| 31 | - default void deleteByUserId(Long userId){ | |
| 29 | + default void deleteByUserId(Long userId) { | |
| 32 | 30 | delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId)); |
| 33 | 31 | } |
| 34 | 32 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dict/DictDataMapper.java
| ... | ... | @@ -17,13 +17,11 @@ import java.util.List; |
| 17 | 17 | public interface DictDataMapper extends BaseMapperX<DictDataDO> { |
| 18 | 18 | |
| 19 | 19 | default DictDataDO selectByDictTypeAndValue(String dictType, String value) { |
| 20 | - return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType) | |
| 21 | - .eq(DictDataDO::getValue, value)); | |
| 20 | + return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getValue, value); | |
| 22 | 21 | } |
| 23 | 22 | |
| 24 | 23 | default DictDataDO selectByDictTypeAndLabel(String dictType, String label) { |
| 25 | - return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType) | |
| 26 | - .eq(DictDataDO::getLabel, label)); | |
| 24 | + return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getLabel, label); | |
| 27 | 25 | } |
| 28 | 26 | |
| 29 | 27 | default List<DictDataDO> selectByDictTypeAndValues(String dictType, Collection<String> values) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/errorcode/ErrorCodeMapper.java
| ... | ... | @@ -36,11 +36,11 @@ public interface ErrorCodeMapper extends BaseMapperX<ErrorCodeDO> { |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) { |
| 39 | - return selectList(new LambdaQueryWrapperX<ErrorCodeDO>().in(ErrorCodeDO::getCode, codes)); | |
| 39 | + return selectList(ErrorCodeDO::getCode, codes); | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | default ErrorCodeDO selectByCode(Integer code) { |
| 43 | - return selectOne(new LambdaQueryWrapperX<ErrorCodeDO>().eq(ErrorCodeDO::getCode, code)); | |
| 43 | + return selectOne(ErrorCodeDO::getCode, code); | |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | default List<ErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/MenuMapper.java
| ... | ... | @@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 5 | 5 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
| 6 | 6 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; |
| 7 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 8 | 7 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 8 | |
| 10 | 9 | import java.util.List; |
| ... | ... | @@ -13,8 +12,7 @@ import java.util.List; |
| 13 | 12 | public interface MenuMapper extends BaseMapperX<MenuDO> { |
| 14 | 13 | |
| 15 | 14 | default MenuDO selectByParentIdAndName(Long parentId, String name) { |
| 16 | - return selectOne(new LambdaQueryWrapper<MenuDO>().eq(MenuDO::getParentId, parentId) | |
| 17 | - .eq(MenuDO::getName, name)); | |
| 15 | + return selectOne(MenuDO::getParentId, parentId, MenuDO::getName, name); | |
| 18 | 16 | } |
| 19 | 17 | |
| 20 | 18 | default Long selectCountByParentId(Long parentId) { |
| ... | ... | @@ -22,7 +20,8 @@ public interface MenuMapper extends BaseMapperX<MenuDO> { |
| 22 | 20 | } |
| 23 | 21 | |
| 24 | 22 | default List<MenuDO> selectList(MenuListReqVO reqVO) { |
| 25 | - return selectList(new LambdaQueryWrapperX<MenuDO>().likeIfPresent(MenuDO::getName, reqVO.getName()) | |
| 23 | + return selectList(new LambdaQueryWrapperX<MenuDO>() | |
| 24 | + .likeIfPresent(MenuDO::getName, reqVO.getName()) | |
| 26 | 25 | .eqIfPresent(MenuDO::getStatus, reqVO.getStatus())); |
| 27 | 26 | } |
| 28 | 27 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserBindMapper.java
| ... | ... | @@ -24,9 +24,8 @@ public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> { |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | default SocialUserBindDO selectByUserTypeAndSocialUserId(Integer userType, Long socialUserId) { |
| 27 | - return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>() | |
| 28 | - .eq(SocialUserBindDO::getUserType, userType) | |
| 29 | - .eq(SocialUserBindDO::getSocialUserId, socialUserId)); | |
| 27 | + return selectOne(SocialUserBindDO::getUserType, userType, | |
| 28 | + SocialUserBindDO::getSocialUserId, socialUserId); | |
| 30 | 29 | } |
| 31 | 30 | |
| 32 | 31 | default List<SocialUserBindDO> selectListByUserIdAndUserType(Long userId, Integer userType) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/user/AdminUserMapper.java
| ... | ... | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 6 | 6 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserExportReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 9 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 10 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 11 | 10 | |
| 12 | 11 | import java.util.Collection; |
| ... | ... | @@ -16,15 +15,15 @@ import java.util.List; |
| 16 | 15 | public interface AdminUserMapper extends BaseMapperX<AdminUserDO> { |
| 17 | 16 | |
| 18 | 17 | default AdminUserDO selectByUsername(String username) { |
| 19 | - return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getUsername, username)); | |
| 18 | + return selectOne(AdminUserDO::getUsername, username); | |
| 20 | 19 | } |
| 21 | 20 | |
| 22 | 21 | default AdminUserDO selectByEmail(String email) { |
| 23 | - return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getEmail, email)); | |
| 22 | + return selectOne(AdminUserDO::getEmail, email); | |
| 24 | 23 | } |
| 25 | 24 | |
| 26 | 25 | default AdminUserDO selectByMobile(String mobile) { |
| 27 | - return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getMobile, mobile)); | |
| 26 | + return selectOne(AdminUserDO::getMobile, mobile); | |
| 28 | 27 | } |
| 29 | 28 | |
| 30 | 29 | default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds) { |
| ... | ... | @@ -50,10 +49,6 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> { |
| 50 | 49 | return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getNickname, nickname)); |
| 51 | 50 | } |
| 52 | 51 | |
| 53 | - default List<AdminUserDO> selectListByUsername(String username) { | |
| 54 | - return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getUsername, username)); | |
| 55 | - } | |
| 56 | - | |
| 57 | 52 | default List<AdminUserDO> selectListByStatus(Integer status) { |
| 58 | 53 | return selectList(AdminUserDO::getStatus, status); |
| 59 | 54 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
| ... | ... | @@ -94,7 +94,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 94 | 94 | @Override |
| 95 | 95 | public AuthLoginRespVO login(AuthLoginReqVO reqVO) { |
| 96 | 96 | // 校验验证码 |
| 97 | - verifyCaptcha(reqVO); | |
| 97 | + validateCaptcha(reqVO); | |
| 98 | 98 | |
| 99 | 99 | // 使用账号密码,进行登录 |
| 100 | 100 | AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword()); |
| ... | ... | @@ -171,14 +171,8 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 171 | 171 | return createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - @Override | |
| 175 | - public AuthLoginRespVO refreshToken(String refreshToken) { | |
| 176 | - OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, OAuth2ClientConstants.CLIENT_ID_DEFAULT); | |
| 177 | - return AuthConvert.INSTANCE.convert(accessTokenDO); | |
| 178 | - } | |
| 179 | - | |
| 180 | 174 | @VisibleForTesting |
| 181 | - void verifyCaptcha(AuthLoginReqVO reqVO) { | |
| 175 | + void validateCaptcha(AuthLoginReqVO reqVO) { | |
| 182 | 176 | // 如果验证码关闭,则不进行校验 |
| 183 | 177 | if (!captchaEnable) { |
| 184 | 178 | return; |
| ... | ... | @@ -207,6 +201,12 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 207 | 201 | } |
| 208 | 202 | |
| 209 | 203 | @Override |
| 204 | + public AuthLoginRespVO refreshToken(String refreshToken) { | |
| 205 | + OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, OAuth2ClientConstants.CLIENT_ID_DEFAULT); | |
| 206 | + return AuthConvert.INSTANCE.convert(accessTokenDO); | |
| 207 | + } | |
| 208 | + | |
| 209 | + @Override | |
| 210 | 210 | public void logout(String token, Integer logType) { |
| 211 | 211 | // 删除访问令牌 |
| 212 | 212 | OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.removeAccessToken(token); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java
| ... | ... | @@ -52,7 +52,7 @@ public interface DeptService { |
| 52 | 52 | * @param reqVO 筛选条件请求 VO |
| 53 | 53 | * @return 部门列表 |
| 54 | 54 | */ |
| 55 | - List<DeptDO> getSimpleDepts(DeptListReqVO reqVO); | |
| 55 | + List<DeptDO> getDeptList(DeptListReqVO reqVO); | |
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * 获得所有子部门,从缓存中 |
| ... | ... | @@ -61,7 +61,7 @@ public interface DeptService { |
| 61 | 61 | * @param recursive 是否递归获取所有 |
| 62 | 62 | * @return 子部门列表 |
| 63 | 63 | */ |
| 64 | - List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive); | |
| 64 | + List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive); | |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * 获得部门信息数组 |
| ... | ... | @@ -69,7 +69,21 @@ public interface DeptService { |
| 69 | 69 | * @param ids 部门编号数组 |
| 70 | 70 | * @return 部门信息数组 |
| 71 | 71 | */ |
| 72 | - List<DeptDO> getDepts(Collection<Long> ids); | |
| 72 | + List<DeptDO> getDeptList(Collection<Long> ids); | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * 获得指定编号的部门 Map | |
| 76 | + * | |
| 77 | + * @param ids 部门编号数组 | |
| 78 | + * @return 部门 Map | |
| 79 | + */ | |
| 80 | + default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) { | |
| 81 | + if (CollUtil.isEmpty(ids)) { | |
| 82 | + return Collections.emptyMap(); | |
| 83 | + } | |
| 84 | + List<DeptDO> list = getDeptList(ids); | |
| 85 | + return CollectionUtils.convertMap(list, DeptDO::getId); | |
| 86 | + } | |
| 73 | 87 | |
| 74 | 88 | /** |
| 75 | 89 | * 获得部门信息 |
| ... | ... | @@ -86,27 +100,6 @@ public interface DeptService { |
| 86 | 100 | * |
| 87 | 101 | * @param ids 角色编号数组 |
| 88 | 102 | */ |
| 89 | - void validDepts(Collection<Long> ids); | |
| 103 | + void validateDeptList(Collection<Long> ids); | |
| 90 | 104 | |
| 91 | - /** | |
| 92 | - * 获得指定编号的部门列表 | |
| 93 | - * | |
| 94 | - * @param ids 部门编号数组 | |
| 95 | - * @return 部门列表 | |
| 96 | - */ | |
| 97 | - List<DeptDO> getSimpleDepts(Collection<Long> ids); | |
| 98 | - | |
| 99 | - /** | |
| 100 | - * 获得指定编号的部门 Map | |
| 101 | - * | |
| 102 | - * @param ids 部门编号数组 | |
| 103 | - * @return 部门 Map | |
| 104 | - */ | |
| 105 | - default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) { | |
| 106 | - if (CollUtil.isEmpty(ids)) { | |
| 107 | - return Collections.emptyMap(); | |
| 108 | - } | |
| 109 | - List<DeptDO> list = getSimpleDepts(ids); | |
| 110 | - return CollectionUtils.convertMap(list, DeptDO::getId); | |
| 111 | - } | |
| 112 | 105 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
| ... | ... | @@ -2,8 +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 | -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | |
| 7 | 5 | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; |
| 8 | 6 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 9 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptCreateReqVO; |
| ... | ... | @@ -19,13 +17,11 @@ import com.google.common.collect.ImmutableMultimap; |
| 19 | 17 | import com.google.common.collect.Multimap; |
| 20 | 18 | import lombok.Getter; |
| 21 | 19 | import lombok.extern.slf4j.Slf4j; |
| 22 | -import org.springframework.scheduling.annotation.Scheduled; | |
| 23 | 20 | import org.springframework.stereotype.Service; |
| 24 | 21 | import org.springframework.validation.annotation.Validated; |
| 25 | 22 | |
| 26 | 23 | import javax.annotation.PostConstruct; |
| 27 | 24 | import javax.annotation.Resource; |
| 28 | -import java.time.LocalDateTime; | |
| 29 | 25 | import java.util.*; |
| 30 | 26 | |
| 31 | 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| ... | ... | @@ -95,7 +91,7 @@ public class DeptServiceImpl implements DeptService { |
| 95 | 91 | if (reqVO.getParentId() == null) { |
| 96 | 92 | reqVO.setParentId(DeptIdEnum.ROOT.getId()); |
| 97 | 93 | } |
| 98 | - checkCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName()); | |
| 94 | + validateForCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName()); | |
| 99 | 95 | // 插入部门 |
| 100 | 96 | DeptDO dept = DeptConvert.INSTANCE.convert(reqVO); |
| 101 | 97 | deptMapper.insert(dept); |
| ... | ... | @@ -110,7 +106,7 @@ public class DeptServiceImpl implements DeptService { |
| 110 | 106 | if (reqVO.getParentId() == null) { |
| 111 | 107 | reqVO.setParentId(DeptIdEnum.ROOT.getId()); |
| 112 | 108 | } |
| 113 | - checkCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName()); | |
| 109 | + validateForCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName()); | |
| 114 | 110 | // 更新部门 |
| 115 | 111 | DeptDO updateObj = DeptConvert.INSTANCE.convert(reqVO); |
| 116 | 112 | deptMapper.updateById(updateObj); |
| ... | ... | @@ -121,10 +117,10 @@ public class DeptServiceImpl implements DeptService { |
| 121 | 117 | @Override |
| 122 | 118 | public void deleteDept(Long id) { |
| 123 | 119 | // 校验是否存在 |
| 124 | - checkDeptExists(id); | |
| 120 | + validateDeptExists(id); | |
| 125 | 121 | // 校验是否有子部门 |
| 126 | 122 | if (deptMapper.selectCountByParentId(id) > 0) { |
| 127 | - throw ServiceExceptionUtil.exception(DEPT_EXITS_CHILDREN); | |
| 123 | + throw exception(DEPT_EXITS_CHILDREN); | |
| 128 | 124 | } |
| 129 | 125 | // 删除部门 |
| 130 | 126 | deptMapper.deleteById(id); |
| ... | ... | @@ -133,20 +129,20 @@ public class DeptServiceImpl implements DeptService { |
| 133 | 129 | } |
| 134 | 130 | |
| 135 | 131 | @Override |
| 136 | - public List<DeptDO> getSimpleDepts(DeptListReqVO reqVO) { | |
| 132 | + public List<DeptDO> getDeptList(DeptListReqVO reqVO) { | |
| 137 | 133 | return deptMapper.selectList(reqVO); |
| 138 | 134 | } |
| 139 | 135 | |
| 140 | 136 | @Override |
| 141 | - public List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive) { | |
| 137 | + public List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive) { | |
| 142 | 138 | if (parentId == null) { |
| 143 | 139 | return Collections.emptyList(); |
| 144 | 140 | } |
| 145 | - List<DeptDO> result = new ArrayList<>(); // TODO 芋艿:待优化,新增缓存,避免每次遍历的计算 | |
| 141 | + List<DeptDO> result = new ArrayList<>(); | |
| 146 | 142 | // 递归,简单粗暴 |
| 147 | - this.getDeptsByParentIdFromCache(result, parentId, | |
| 148 | - recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次 | |
| 149 | - parentDeptCache); | |
| 143 | + getDeptsByParentIdFromCache(result, parentId, | |
| 144 | + recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次 | |
| 145 | + parentDeptCache); | |
| 150 | 146 | return result; |
| 151 | 147 | } |
| 152 | 148 | |
| ... | ... | @@ -182,65 +178,65 @@ public class DeptServiceImpl implements DeptService { |
| 182 | 178 | recursiveCount - 1, parentDeptMap)); |
| 183 | 179 | } |
| 184 | 180 | |
| 185 | - private void checkCreateOrUpdate(Long id, Long parentId, String name) { | |
| 181 | + private void validateForCreateOrUpdate(Long id, Long parentId, String name) { | |
| 186 | 182 | // 校验自己存在 |
| 187 | - checkDeptExists(id); | |
| 183 | + validateDeptExists(id); | |
| 188 | 184 | // 校验父部门的有效性 |
| 189 | - checkParentDeptEnable(id, parentId); | |
| 185 | + validateParentDeptEnable(id, parentId); | |
| 190 | 186 | // 校验部门名的唯一性 |
| 191 | - checkDeptNameUnique(id, parentId, name); | |
| 187 | + validateDeptNameUnique(id, parentId, name); | |
| 192 | 188 | } |
| 193 | 189 | |
| 194 | - private void checkParentDeptEnable(Long id, Long parentId) { | |
| 190 | + private void validateParentDeptEnable(Long id, Long parentId) { | |
| 195 | 191 | if (parentId == null || DeptIdEnum.ROOT.getId().equals(parentId)) { |
| 196 | 192 | return; |
| 197 | 193 | } |
| 198 | 194 | // 不能设置自己为父部门 |
| 199 | 195 | if (parentId.equals(id)) { |
| 200 | - throw ServiceExceptionUtil.exception(DEPT_PARENT_ERROR); | |
| 196 | + throw exception(DEPT_PARENT_ERROR); | |
| 201 | 197 | } |
| 202 | 198 | // 父岗位不存在 |
| 203 | 199 | DeptDO dept = deptMapper.selectById(parentId); |
| 204 | 200 | if (dept == null) { |
| 205 | - throw ServiceExceptionUtil.exception(DEPT_PARENT_NOT_EXITS); | |
| 201 | + throw exception(DEPT_PARENT_NOT_EXITS); | |
| 206 | 202 | } |
| 207 | 203 | // 父部门被禁用 |
| 208 | 204 | if (!CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) { |
| 209 | - throw ServiceExceptionUtil.exception(DEPT_NOT_ENABLE); | |
| 205 | + throw exception(DEPT_NOT_ENABLE); | |
| 210 | 206 | } |
| 211 | 207 | // 父部门不能是原来的子部门 |
| 212 | - List<DeptDO> children = this.getDeptsByParentIdFromCache(id, true); | |
| 208 | + List<DeptDO> children = getDeptListByParentIdFromCache(id, true); | |
| 213 | 209 | if (children.stream().anyMatch(dept1 -> dept1.getId().equals(parentId))) { |
| 214 | - throw ServiceExceptionUtil.exception(DEPT_PARENT_IS_CHILD); | |
| 210 | + throw exception(DEPT_PARENT_IS_CHILD); | |
| 215 | 211 | } |
| 216 | 212 | } |
| 217 | 213 | |
| 218 | - private void checkDeptExists(Long id) { | |
| 214 | + private void validateDeptExists(Long id) { | |
| 219 | 215 | if (id == null) { |
| 220 | 216 | return; |
| 221 | 217 | } |
| 222 | 218 | DeptDO dept = deptMapper.selectById(id); |
| 223 | 219 | if (dept == null) { |
| 224 | - throw ServiceExceptionUtil.exception(DEPT_NOT_FOUND); | |
| 220 | + throw exception(DEPT_NOT_FOUND); | |
| 225 | 221 | } |
| 226 | 222 | } |
| 227 | 223 | |
| 228 | - private void checkDeptNameUnique(Long id, Long parentId, String name) { | |
| 224 | + private void validateDeptNameUnique(Long id, Long parentId, String name) { | |
| 229 | 225 | DeptDO menu = deptMapper.selectByParentIdAndName(parentId, name); |
| 230 | 226 | if (menu == null) { |
| 231 | 227 | return; |
| 232 | 228 | } |
| 233 | 229 | // 如果 id 为空,说明不用比较是否为相同 id 的岗位 |
| 234 | 230 | if (id == null) { |
| 235 | - throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE); | |
| 231 | + throw exception(DEPT_NAME_DUPLICATE); | |
| 236 | 232 | } |
| 237 | 233 | if (!menu.getId().equals(id)) { |
| 238 | - throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE); | |
| 234 | + throw exception(DEPT_NAME_DUPLICATE); | |
| 239 | 235 | } |
| 240 | 236 | } |
| 241 | 237 | |
| 242 | 238 | @Override |
| 243 | - public List<DeptDO> getDepts(Collection<Long> ids) { | |
| 239 | + public List<DeptDO> getDeptList(Collection<Long> ids) { | |
| 244 | 240 | return deptMapper.selectBatchIds(ids); |
| 245 | 241 | } |
| 246 | 242 | |
| ... | ... | @@ -250,13 +246,12 @@ public class DeptServiceImpl implements DeptService { |
| 250 | 246 | } |
| 251 | 247 | |
| 252 | 248 | @Override |
| 253 | - public void validDepts(Collection<Long> ids) { | |
| 249 | + public void validateDeptList(Collection<Long> ids) { | |
| 254 | 250 | if (CollUtil.isEmpty(ids)) { |
| 255 | 251 | return; |
| 256 | 252 | } |
| 257 | 253 | // 获得科室信息 |
| 258 | - List<DeptDO> depts = deptMapper.selectBatchIds(ids); | |
| 259 | - Map<Long, DeptDO> deptMap = CollectionUtils.convertMap(depts, DeptDO::getId); | |
| 254 | + Map<Long, DeptDO> deptMap = getDeptMap(ids); | |
| 260 | 255 | // 校验 |
| 261 | 256 | ids.forEach(id -> { |
| 262 | 257 | DeptDO dept = deptMap.get(id); |
| ... | ... | @@ -269,9 +264,4 @@ public class DeptServiceImpl implements DeptService { |
| 269 | 264 | }); |
| 270 | 265 | } |
| 271 | 266 | |
| 272 | - @Override | |
| 273 | - public List<DeptDO> getSimpleDepts(Collection<Long> ids) { | |
| 274 | - return deptMapper.selectBatchIds(ids); | |
| 275 | - } | |
| 276 | - | |
| 277 | 267 | } | ... | ... |
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/dict/DictDataService.java
| ... | ... | @@ -44,7 +44,7 @@ public interface DictDataService { |
| 44 | 44 | * |
| 45 | 45 | * @return 字典数据全列表 |
| 46 | 46 | */ |
| 47 | - List<DictDataDO> getDictDatas(); | |
| 47 | + List<DictDataDO> getDictDataList(); | |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * 获得字典数据分页列表 |
| ... | ... | @@ -60,7 +60,7 @@ public interface DictDataService { |
| 60 | 60 | * @param reqVO 列表请求 |
| 61 | 61 | * @return 字典数据列表 |
| 62 | 62 | */ |
| 63 | - List<DictDataDO> getDictDatas(DictDataExportReqVO reqVO); | |
| 63 | + List<DictDataDO> getDictDataList(DictDataExportReqVO reqVO); | |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * 获得字典数据详情 |
| ... | ... | @@ -86,7 +86,7 @@ public interface DictDataService { |
| 86 | 86 | * @param dictType 字典类型 |
| 87 | 87 | * @param values 字典数据值的数组 |
| 88 | 88 | */ |
| 89 | - void validDictDatas(String dictType, Collection<String> values); | |
| 89 | + void validateDictDataList(String dictType, Collection<String> values); | |
| 90 | 90 | |
| 91 | 91 | /** |
| 92 | 92 | * 获得指定的字典数据 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictDataServiceImpl.java
| ... | ... | @@ -48,7 +48,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 48 | 48 | private DictDataMapper dictDataMapper; |
| 49 | 49 | |
| 50 | 50 | @Override |
| 51 | - public List<DictDataDO> getDictDatas() { | |
| 51 | + public List<DictDataDO> getDictDataList() { | |
| 52 | 52 | List<DictDataDO> list = dictDataMapper.selectList(); |
| 53 | 53 | list.sort(COMPARATOR_TYPE_AND_SORT); |
| 54 | 54 | return list; |
| ... | ... | @@ -60,7 +60,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | @Override |
| 63 | - public List<DictDataDO> getDictDatas(DictDataExportReqVO reqVO) { | |
| 63 | + public List<DictDataDO> getDictDataList(DictDataExportReqVO reqVO) { | |
| 64 | 64 | List<DictDataDO> list = dictDataMapper.selectList(reqVO); |
| 65 | 65 | list.sort(COMPARATOR_TYPE_AND_SORT); |
| 66 | 66 | return list; |
| ... | ... | @@ -74,7 +74,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 74 | 74 | @Override |
| 75 | 75 | public Long createDictData(DictDataCreateReqVO reqVO) { |
| 76 | 76 | // 校验正确性 |
| 77 | - checkCreateOrUpdate(null, reqVO.getValue(), reqVO.getDictType()); | |
| 77 | + validateDictDataForCreateOrUpdate(null, reqVO.getValue(), reqVO.getDictType()); | |
| 78 | 78 | |
| 79 | 79 | // 插入字典类型 |
| 80 | 80 | DictDataDO dictData = DictDataConvert.INSTANCE.convert(reqVO); |
| ... | ... | @@ -85,7 +85,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 85 | 85 | @Override |
| 86 | 86 | public void updateDictData(DictDataUpdateReqVO reqVO) { |
| 87 | 87 | // 校验正确性 |
| 88 | - checkCreateOrUpdate(reqVO.getId(), reqVO.getValue(), reqVO.getDictType()); | |
| 88 | + validateDictDataForCreateOrUpdate(reqVO.getId(), reqVO.getValue(), reqVO.getDictType()); | |
| 89 | 89 | |
| 90 | 90 | // 更新字典类型 |
| 91 | 91 | DictDataDO updateObj = DictDataConvert.INSTANCE.convert(reqVO); |
| ... | ... | @@ -95,7 +95,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 95 | 95 | @Override |
| 96 | 96 | public void deleteDictData(Long id) { |
| 97 | 97 | // 校验是否存在 |
| 98 | - checkDictDataExists(id); | |
| 98 | + validateDictDataExists(id); | |
| 99 | 99 | |
| 100 | 100 | // 删除字典数据 |
| 101 | 101 | dictDataMapper.deleteById(id); |
| ... | ... | @@ -106,18 +106,17 @@ public class DictDataServiceImpl implements DictDataService { |
| 106 | 106 | return dictDataMapper.selectCountByDictType(dictType); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - | |
| 110 | - private void checkCreateOrUpdate(Long id, String value, String dictType) { | |
| 109 | + private void validateDictDataForCreateOrUpdate(Long id, String value, String dictType) { | |
| 111 | 110 | // 校验自己存在 |
| 112 | - checkDictDataExists(id); | |
| 111 | + validateDictDataExists(id); | |
| 113 | 112 | // 校验字典类型有效 |
| 114 | - checkDictTypeValid(dictType); | |
| 113 | + validateDictTypeExists(dictType); | |
| 115 | 114 | // 校验字典数据的值的唯一性 |
| 116 | - checkDictDataValueUnique(id, dictType, value); | |
| 115 | + validateDictDataValueUnique(id, dictType, value); | |
| 117 | 116 | } |
| 118 | 117 | |
| 119 | 118 | @VisibleForTesting |
| 120 | - public void checkDictDataValueUnique(Long id, String dictType, String value) { | |
| 119 | + public void validateDictDataValueUnique(Long id, String dictType, String value) { | |
| 121 | 120 | DictDataDO dictData = dictDataMapper.selectByDictTypeAndValue(dictType, value); |
| 122 | 121 | if (dictData == null) { |
| 123 | 122 | return; |
| ... | ... | @@ -132,7 +131,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 132 | 131 | } |
| 133 | 132 | |
| 134 | 133 | @VisibleForTesting |
| 135 | - public void checkDictDataExists(Long id) { | |
| 134 | + public void validateDictDataExists(Long id) { | |
| 136 | 135 | if (id == null) { |
| 137 | 136 | return; |
| 138 | 137 | } |
| ... | ... | @@ -143,7 +142,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 143 | 142 | } |
| 144 | 143 | |
| 145 | 144 | @VisibleForTesting |
| 146 | - public void checkDictTypeValid(String type) { | |
| 145 | + public void validateDictTypeExists(String type) { | |
| 147 | 146 | DictTypeDO dictType = dictTypeService.getDictType(type); |
| 148 | 147 | if (dictType == null) { |
| 149 | 148 | throw exception(DICT_TYPE_NOT_EXISTS); |
| ... | ... | @@ -154,7 +153,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 154 | 153 | } |
| 155 | 154 | |
| 156 | 155 | @Override |
| 157 | - public void validDictDatas(String dictType, Collection<String> values) { | |
| 156 | + public void validateDictDataList(String dictType, Collection<String> values) { | |
| 158 | 157 | if (CollUtil.isEmpty(values)) { |
| 159 | 158 | return; |
| 160 | 159 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceImpl.java
| ... | ... | @@ -57,7 +57,8 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 57 | 57 | @Override |
| 58 | 58 | public Long createDictType(DictTypeCreateReqVO reqVO) { |
| 59 | 59 | // 校验正确性 |
| 60 | - checkCreateOrUpdate(null, reqVO.getName(), reqVO.getType()); | |
| 60 | + validateDictTypeForCreateOrUpdate(null, reqVO.getName(), reqVO.getType()); | |
| 61 | + | |
| 61 | 62 | // 插入字典类型 |
| 62 | 63 | DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO) |
| 63 | 64 | .setDeletedTime(LocalDateTimeUtils.EMPTY); // 唯一索引,避免 null 值 |
| ... | ... | @@ -68,7 +69,8 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 68 | 69 | @Override |
| 69 | 70 | public void updateDictType(DictTypeUpdateReqVO reqVO) { |
| 70 | 71 | // 校验正确性 |
| 71 | - checkCreateOrUpdate(reqVO.getId(), reqVO.getName(), null); | |
| 72 | + validateDictTypeForCreateOrUpdate(reqVO.getId(), reqVO.getName(), null); | |
| 73 | + | |
| 72 | 74 | // 更新字典类型 |
| 73 | 75 | DictTypeDO updateObj = DictTypeConvert.INSTANCE.convert(reqVO); |
| 74 | 76 | dictTypeMapper.updateById(updateObj); |
| ... | ... | @@ -77,7 +79,7 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 77 | 79 | @Override |
| 78 | 80 | public void deleteDictType(Long id) { |
| 79 | 81 | // 校验是否存在 |
| 80 | - DictTypeDO dictType = checkDictTypeExists(id); | |
| 82 | + DictTypeDO dictType = validateDictTypeExists(id); | |
| 81 | 83 | // 校验是否有字典数据 |
| 82 | 84 | if (dictDataService.countByDictType(dictType.getType()) > 0) { |
| 83 | 85 | throw exception(DICT_TYPE_HAS_CHILDREN); |
| ... | ... | @@ -91,17 +93,17 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 91 | 93 | return dictTypeMapper.selectList(); |
| 92 | 94 | } |
| 93 | 95 | |
| 94 | - private void checkCreateOrUpdate(Long id, String name, String type) { | |
| 96 | + private void validateDictTypeForCreateOrUpdate(Long id, String name, String type) { | |
| 95 | 97 | // 校验自己存在 |
| 96 | - checkDictTypeExists(id); | |
| 98 | + validateDictTypeExists(id); | |
| 97 | 99 | // 校验字典类型的名字的唯一性 |
| 98 | - checkDictTypeNameUnique(id, name); | |
| 100 | + validateDictTypeNameUnique(id, name); | |
| 99 | 101 | // 校验字典类型的类型的唯一性 |
| 100 | - checkDictTypeUnique(id, type); | |
| 102 | + validateDictTypeUnique(id, type); | |
| 101 | 103 | } |
| 102 | 104 | |
| 103 | 105 | @VisibleForTesting |
| 104 | - public void checkDictTypeNameUnique(Long id, String name) { | |
| 106 | + void validateDictTypeNameUnique(Long id, String name) { | |
| 105 | 107 | DictTypeDO dictType = dictTypeMapper.selectByName(name); |
| 106 | 108 | if (dictType == null) { |
| 107 | 109 | return; |
| ... | ... | @@ -116,7 +118,7 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 116 | 118 | } |
| 117 | 119 | |
| 118 | 120 | @VisibleForTesting |
| 119 | - public void checkDictTypeUnique(Long id, String type) { | |
| 121 | + void validateDictTypeUnique(Long id, String type) { | |
| 120 | 122 | if (StrUtil.isEmpty(type)) { |
| 121 | 123 | return; |
| 122 | 124 | } |
| ... | ... | @@ -134,7 +136,7 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 134 | 136 | } |
| 135 | 137 | |
| 136 | 138 | @VisibleForTesting |
| 137 | - public DictTypeDO checkDictTypeExists(Long id) { | |
| 139 | + DictTypeDO validateDictTypeExists(Long id) { | |
| 138 | 140 | if (id == null) { |
| 139 | 141 | return null; |
| 140 | 142 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java
| ... | ... | @@ -58,7 +58,7 @@ public class ErrorCodeServiceImpl implements ErrorCodeService { |
| 58 | 58 | @Override |
| 59 | 59 | public void updateErrorCode(ErrorCodeUpdateReqVO updateReqVO) { |
| 60 | 60 | // 校验存在 |
| 61 | - this.validateErrorCodeExists(updateReqVO.getId()); | |
| 61 | + validateErrorCodeExists(updateReqVO.getId()); | |
| 62 | 62 | // 校验 code 重复 |
| 63 | 63 | validateCodeDuplicate(updateReqVO.getCode(), updateReqVO.getId()); |
| 64 | 64 | |
| ... | ... | @@ -71,7 +71,7 @@ public class ErrorCodeServiceImpl implements ErrorCodeService { |
| 71 | 71 | @Override |
| 72 | 72 | public void deleteErrorCode(Long id) { |
| 73 | 73 | // 校验存在 |
| 74 | - this.validateErrorCodeExists(id); | |
| 74 | + validateErrorCodeExists(id); | |
| 75 | 75 | // 删除 |
| 76 | 76 | errorCodeMapper.deleteById(id); |
| 77 | 77 | } |
| ... | ... | @@ -100,7 +100,7 @@ public class ErrorCodeServiceImpl implements ErrorCodeService { |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | @VisibleForTesting |
| 103 | - public void validateErrorCodeExists(Long id) { | |
| 103 | + void validateErrorCodeExists(Long id) { | |
| 104 | 104 | if (errorCodeMapper.selectById(id) == null) { |
| 105 | 105 | throw exception(ERROR_CODE_NOT_EXISTS); |
| 106 | 106 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/logger/OperateLogService.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.logger; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | -import cn.iocoder.yudao.framework.operatelog.core.service.OperateLog; | |
| 5 | -import cn.iocoder.yudao.framework.operatelog.core.service.OperateLogFrameworkService; | |
| 6 | 4 | import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO; |
| 7 | 5 | import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogExportReqVO; |
| 8 | 6 | import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO; |
| ... | ... | @@ -38,6 +36,6 @@ public interface OperateLogService { |
| 38 | 36 | * @param reqVO 列表条件 |
| 39 | 37 | * @return 日志列表 |
| 40 | 38 | */ |
| 41 | - List<OperateLogDO> getOperateLogs(OperateLogExportReqVO reqVO); | |
| 39 | + List<OperateLogDO> getOperateLogList(OperateLogExportReqVO reqVO); | |
| 42 | 40 | |
| 43 | 41 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/logger/OperateLogServiceImpl.java
| ... | ... | @@ -49,7 +49,7 @@ public class OperateLogServiceImpl implements OperateLogService { |
| 49 | 49 | // 处理基于用户昵称的查询 |
| 50 | 50 | Collection<Long> userIds = null; |
| 51 | 51 | if (StrUtil.isNotEmpty(reqVO.getUserNickname())) { |
| 52 | - userIds = convertSet(userService.getUsersByNickname(reqVO.getUserNickname()), AdminUserDO::getId); | |
| 52 | + userIds = convertSet(userService.getUserListByNickname(reqVO.getUserNickname()), AdminUserDO::getId); | |
| 53 | 53 | if (CollUtil.isEmpty(userIds)) { |
| 54 | 54 | return PageResult.empty(); |
| 55 | 55 | } |
| ... | ... | @@ -59,11 +59,11 @@ public class OperateLogServiceImpl implements OperateLogService { |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | @Override |
| 62 | - public List<OperateLogDO> getOperateLogs(OperateLogExportReqVO reqVO) { | |
| 62 | + public List<OperateLogDO> getOperateLogList(OperateLogExportReqVO reqVO) { | |
| 63 | 63 | // 处理基于用户昵称的查询 |
| 64 | 64 | Collection<Long> userIds = null; |
| 65 | 65 | if (StrUtil.isNotEmpty(reqVO.getUserNickname())) { |
| 66 | - userIds = convertSet(userService.getUsersByNickname(reqVO.getUserNickname()), AdminUserDO::getId); | |
| 66 | + userIds = convertSet(userService.getUserListByNickname(reqVO.getUserNickname()), AdminUserDO::getId); | |
| 67 | 67 | if (CollUtil.isEmpty(userIds)) { |
| 68 | 68 | return Collections.emptyList(); |
| 69 | 69 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImpl.java
| ... | ... | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.system.service.mail; |
| 3 | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | 4 | import cn.hutool.extra.mail.MailAccount; |
| 5 | 5 | import cn.hutool.extra.mail.MailUtil; |
| 6 | -import cn.iocoder.yudao.framework.common.core.KeyValue; | |
| 7 | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 8 | 7 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 9 | 8 | import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; |
| ... | ... | @@ -20,15 +19,13 @@ import org.springframework.stereotype.Service; |
| 20 | 19 | import org.springframework.validation.annotation.Validated; |
| 21 | 20 | |
| 22 | 21 | import javax.annotation.Resource; |
| 23 | -import java.util.List; | |
| 24 | 22 | import java.util.Map; |
| 25 | -import java.util.stream.Collectors; | |
| 26 | 23 | |
| 27 | 24 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 28 | 25 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 29 | 26 | |
| 30 | 27 | /** |
| 31 | - * 邮箱模版 服务实现类 | |
| 28 | + * 邮箱发送 Service 实现类 | |
| 32 | 29 | * |
| 33 | 30 | * @author wangjingyi |
| 34 | 31 | * @since 2022-03-21 |
| ... | ... | @@ -82,13 +79,13 @@ public class MailSendServiceImpl implements MailSendService { |
| 82 | 79 | public Long sendSingleMail(String mail, Long userId, Integer userType, |
| 83 | 80 | String templateCode, Map<String, Object> templateParams) { |
| 84 | 81 | // 校验邮箱模版是否合法 |
| 85 | - MailTemplateDO template = checkMailTemplateValid(templateCode); | |
| 82 | + MailTemplateDO template = validateMailTemplate(templateCode); | |
| 86 | 83 | // 校验邮箱账号是否合法 |
| 87 | - MailAccountDO account = checkMailAccountValid(template.getAccountId()); | |
| 84 | + MailAccountDO account = validateMailAccount(template.getAccountId()); | |
| 88 | 85 | |
| 89 | 86 | // 校验邮箱是否存在 |
| 90 | - mail = checkMail(mail); | |
| 91 | - checkTemplateParams(template, templateParams); | |
| 87 | + mail = validateMail(mail); | |
| 88 | + validateTemplateParams(template, templateParams); | |
| 92 | 89 | |
| 93 | 90 | // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志 |
| 94 | 91 | Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus()); |
| ... | ... | @@ -106,7 +103,7 @@ public class MailSendServiceImpl implements MailSendService { |
| 106 | 103 | @Override |
| 107 | 104 | public void doSendMail(MailSendMessage message) { |
| 108 | 105 | // 1. 创建发送账号 |
| 109 | - MailAccountDO account = checkMailAccountValid(message.getAccountId()); | |
| 106 | + MailAccountDO account = validateMailAccount(message.getAccountId()); | |
| 110 | 107 | MailAccount mailAccount = MailAccountConvert.INSTANCE.convert(account, message.getNickname()); |
| 111 | 108 | // 2. 发送邮件 |
| 112 | 109 | try { |
| ... | ... | @@ -121,7 +118,7 @@ public class MailSendServiceImpl implements MailSendService { |
| 121 | 118 | } |
| 122 | 119 | |
| 123 | 120 | @VisibleForTesting |
| 124 | - public MailTemplateDO checkMailTemplateValid(String templateCode) { | |
| 121 | + MailTemplateDO validateMailTemplate(String templateCode) { | |
| 125 | 122 | // 获得邮件模板。考虑到效率,从缓存中获取 |
| 126 | 123 | MailTemplateDO template = mailTemplateService.getMailTemplateByCodeFromCache(templateCode); |
| 127 | 124 | // 邮件模板不存在 |
| ... | ... | @@ -132,7 +129,7 @@ public class MailSendServiceImpl implements MailSendService { |
| 132 | 129 | } |
| 133 | 130 | |
| 134 | 131 | @VisibleForTesting |
| 135 | - public MailAccountDO checkMailAccountValid(Long accountId) { | |
| 132 | + MailAccountDO validateMailAccount(Long accountId) { | |
| 136 | 133 | // 获得邮箱账号。考虑到效率,从缓存中获取 |
| 137 | 134 | MailAccountDO account = mailAccountService.getMailAccountFromCache(accountId); |
| 138 | 135 | // 邮箱账号不存在 |
| ... | ... | @@ -143,7 +140,7 @@ public class MailSendServiceImpl implements MailSendService { |
| 143 | 140 | } |
| 144 | 141 | |
| 145 | 142 | @VisibleForTesting |
| 146 | - public String checkMail(String mail) { | |
| 143 | + String validateMail(String mail) { | |
| 147 | 144 | if (StrUtil.isEmpty(mail)) { |
| 148 | 145 | throw exception(MAIL_SEND_MAIL_NOT_EXISTS); |
| 149 | 146 | } |
| ... | ... | @@ -157,7 +154,7 @@ public class MailSendServiceImpl implements MailSendService { |
| 157 | 154 | * @param templateParams 参数列表 |
| 158 | 155 | */ |
| 159 | 156 | @VisibleForTesting |
| 160 | - public void checkTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) { | |
| 157 | + void validateTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) { | |
| 161 | 158 | template.getParams().forEach(key -> { |
| 162 | 159 | Object value = templateParams.get(key); |
| 163 | 160 | if (value == null) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notice/NoticeService.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notice/NoticeServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.notice; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | |
| 4 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 4 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; |
| 6 | 5 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; |
| 7 | 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; |
| 8 | 7 | import cn.iocoder.yudao.module.system.convert.notice.NoticeConvert; |
| 9 | -import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; | |
| 10 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; |
| 9 | +import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; | |
| 11 | 10 | import com.google.common.annotations.VisibleForTesting; |
| 12 | 11 | import org.springframework.stereotype.Service; |
| 13 | 12 | |
| 14 | 13 | import javax.annotation.Resource; |
| 15 | 14 | |
| 15 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | |
| 16 | 16 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; |
| 17 | 17 | |
| 18 | 18 | /** |
| ... | ... | @@ -36,7 +36,7 @@ public class NoticeServiceImpl implements NoticeService { |
| 36 | 36 | @Override |
| 37 | 37 | public void updateNotice(NoticeUpdateReqVO reqVO) { |
| 38 | 38 | // 校验是否存在 |
| 39 | - this.checkNoticeExists(reqVO.getId()); | |
| 39 | + validateNoticeExists(reqVO.getId()); | |
| 40 | 40 | // 更新通知公告 |
| 41 | 41 | NoticeDO updateObj = NoticeConvert.INSTANCE.convert(reqVO); |
| 42 | 42 | noticeMapper.updateById(updateObj); |
| ... | ... | @@ -45,13 +45,13 @@ public class NoticeServiceImpl implements NoticeService { |
| 45 | 45 | @Override |
| 46 | 46 | public void deleteNotice(Long id) { |
| 47 | 47 | // 校验是否存在 |
| 48 | - this.checkNoticeExists(id); | |
| 48 | + validateNoticeExists(id); | |
| 49 | 49 | // 删除通知公告 |
| 50 | 50 | noticeMapper.deleteById(id); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | @Override |
| 54 | - public PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO) { | |
| 54 | + public PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO) { | |
| 55 | 55 | return noticeMapper.selectPage(reqVO); |
| 56 | 56 | } |
| 57 | 57 | |
| ... | ... | @@ -61,13 +61,13 @@ public class NoticeServiceImpl implements NoticeService { |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | @VisibleForTesting |
| 64 | - public void checkNoticeExists(Long id) { | |
| 64 | + public void validateNoticeExists(Long id) { | |
| 65 | 65 | if (id == null) { |
| 66 | 66 | return; |
| 67 | 67 | } |
| 68 | 68 | NoticeDO notice = noticeMapper.selectById(id); |
| 69 | 69 | if (notice == null) { |
| 70 | - throw ServiceExceptionUtil.exception(NOTICE_NOT_FOUND); | |
| 70 | + throw exception(NOTICE_NOT_FOUND); | |
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImpl.java
| ... | ... | @@ -44,13 +44,13 @@ public class NotifySendServiceImpl implements NotifySendService { |
| 44 | 44 | @Override |
| 45 | 45 | public Long sendSingleNotify(Long userId, Integer userType, String templateCode, Map<String, Object> templateParams) { |
| 46 | 46 | // 校验模版 |
| 47 | - NotifyTemplateDO template = checkNotifyTemplateValid(templateCode); | |
| 47 | + NotifyTemplateDO template = validateNotifyTemplate(templateCode); | |
| 48 | 48 | if (Objects.equals(template.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { |
| 49 | 49 | log.info("[sendSingleNotify][模版({})已经关闭,无法给用户({}/{})发送]", templateCode, userId, userType); |
| 50 | 50 | return null; |
| 51 | 51 | } |
| 52 | 52 | // 校验参数 |
| 53 | - checkTemplateParams(template, templateParams); | |
| 53 | + validateTemplateParams(template, templateParams); | |
| 54 | 54 | |
| 55 | 55 | // 发送站内信 |
| 56 | 56 | String content = notifyTemplateService.formatNotifyTemplateContent(template.getContent(), templateParams); |
| ... | ... | @@ -58,7 +58,7 @@ public class NotifySendServiceImpl implements NotifySendService { |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | @VisibleForTesting |
| 61 | - public NotifyTemplateDO checkNotifyTemplateValid(String templateCode) { | |
| 61 | + public NotifyTemplateDO validateNotifyTemplate(String templateCode) { | |
| 62 | 62 | // 获得站内信模板。考虑到效率,从缓存中获取 |
| 63 | 63 | NotifyTemplateDO template = notifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode); |
| 64 | 64 | // 站内信模板不存在 |
| ... | ... | @@ -75,7 +75,7 @@ public class NotifySendServiceImpl implements NotifySendService { |
| 75 | 75 | * @param templateParams 参数列表 |
| 76 | 76 | */ |
| 77 | 77 | @VisibleForTesting |
| 78 | - public void checkTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) { | |
| 78 | + public void validateTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) { | |
| 79 | 79 | template.getParams().forEach(key -> { |
| 80 | 80 | Object value = templateParams.get(key); |
| 81 | 81 | if (value == null) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyTemplateServiceImpl.java
| ... | ... | @@ -76,7 +76,7 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService { |
| 76 | 76 | @Override |
| 77 | 77 | public Long createNotifyTemplate(NotifyTemplateCreateReqVO createReqVO) { |
| 78 | 78 | // 校验站内信编码是否重复 |
| 79 | - checkNotifyTemplateCodeDuplicate(null, createReqVO.getCode()); | |
| 79 | + validateNotifyTemplateCodeDuplicate(null, createReqVO.getCode()); | |
| 80 | 80 | |
| 81 | 81 | // 插入 |
| 82 | 82 | NotifyTemplateDO notifyTemplate = NotifyTemplateConvert.INSTANCE.convert(createReqVO); |
| ... | ... | @@ -93,7 +93,7 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService { |
| 93 | 93 | // 校验存在 |
| 94 | 94 | validateNotifyTemplateExists(updateReqVO.getId()); |
| 95 | 95 | // 校验站内信编码是否重复 |
| 96 | - checkNotifyTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode()); | |
| 96 | + validateNotifyTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode()); | |
| 97 | 97 | |
| 98 | 98 | // 更新 |
| 99 | 99 | NotifyTemplateDO updateObj = NotifyTemplateConvert.INSTANCE.convert(updateReqVO); |
| ... | ... | @@ -136,7 +136,7 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService { |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | @VisibleForTesting |
| 139 | - public void checkNotifyTemplateCodeDuplicate(Long id, String code) { | |
| 139 | + public void validateNotifyTemplateCodeDuplicate(Long id, String code) { | |
| 140 | 140 | NotifyTemplateDO template = notifyTemplateMapper.selectByCode(code); |
| 141 | 141 | if (template == null) { |
| 142 | 142 | return; | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java
| ... | ... | @@ -47,7 +47,7 @@ public interface MenuService { |
| 47 | 47 | * |
| 48 | 48 | * @return 菜单列表 |
| 49 | 49 | */ |
| 50 | - List<MenuDO> getMenus(); | |
| 50 | + List<MenuDO> getMenuList(); | |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * 基于租户,筛选菜单列表 |
| ... | ... | @@ -56,7 +56,7 @@ public interface MenuService { |
| 56 | 56 | * @param reqVO 筛选条件请求 VO |
| 57 | 57 | * @return 菜单列表 |
| 58 | 58 | */ |
| 59 | - List<MenuDO> getTenantMenus(MenuListReqVO reqVO); | |
| 59 | + List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO); | |
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | 62 | * 筛选菜单列表 |
| ... | ... | @@ -64,7 +64,7 @@ public interface MenuService { |
| 64 | 64 | * @param reqVO 筛选条件请求 VO |
| 65 | 65 | * @return 菜单列表 |
| 66 | 66 | */ |
| 67 | - List<MenuDO> getMenus(MenuListReqVO reqVO); | |
| 67 | + List<MenuDO> getMenuList(MenuListReqVO reqVO); | |
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * 获得所有菜单,从缓存中 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java
| ... | ... | @@ -10,7 +10,6 @@ import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuUp |
| 10 | 10 | import cn.iocoder.yudao.module.system.convert.permission.MenuConvert; |
| 11 | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; |
| 12 | 12 | import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper; |
| 13 | -import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum; | |
| 14 | 13 | import cn.iocoder.yudao.module.system.enums.permission.MenuTypeEnum; |
| 15 | 14 | import cn.iocoder.yudao.module.system.mq.producer.permission.MenuProducer; |
| 16 | 15 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; |
| ... | ... | @@ -19,6 +18,7 @@ import com.google.common.collect.ImmutableMap; |
| 19 | 18 | import com.google.common.collect.ImmutableMultimap; |
| 20 | 19 | import com.google.common.collect.Multimap; |
| 21 | 20 | import lombok.Getter; |
| 21 | +import lombok.Setter; | |
| 22 | 22 | import lombok.extern.slf4j.Slf4j; |
| 23 | 23 | import org.springframework.context.annotation.Lazy; |
| 24 | 24 | import org.springframework.stereotype.Service; |
| ... | ... | @@ -31,6 +31,7 @@ import javax.annotation.Resource; |
| 31 | 31 | import java.util.*; |
| 32 | 32 | import java.util.stream.Collectors; |
| 33 | 33 | |
| 34 | +import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; | |
| 34 | 35 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 35 | 36 | |
| 36 | 37 | /** |
| ... | ... | @@ -49,6 +50,7 @@ public class MenuServiceImpl implements MenuService { |
| 49 | 50 | * 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向 |
| 50 | 51 | */ |
| 51 | 52 | @Getter |
| 53 | + @Setter | |
| 52 | 54 | private volatile Map<Long, MenuDO> menuCache; |
| 53 | 55 | /** |
| 54 | 56 | * 权限与菜单缓存 |
| ... | ... | @@ -58,6 +60,7 @@ public class MenuServiceImpl implements MenuService { |
| 58 | 60 | * 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向 |
| 59 | 61 | */ |
| 60 | 62 | @Getter |
| 63 | + @Setter | |
| 61 | 64 | private volatile Multimap<String, MenuDO> permissionMenuCache; |
| 62 | 65 | |
| 63 | 66 | @Resource |
| ... | ... | @@ -97,9 +100,10 @@ public class MenuServiceImpl implements MenuService { |
| 97 | 100 | @Override |
| 98 | 101 | public Long createMenu(MenuCreateReqVO reqVO) { |
| 99 | 102 | // 校验父菜单存在 |
| 100 | - checkParentResource(reqVO.getParentId(), null); | |
| 103 | + validateParentMenu(reqVO.getParentId(), null); | |
| 101 | 104 | // 校验菜单(自己) |
| 102 | - checkResource(reqVO.getParentId(), reqVO.getName(), null); | |
| 105 | + validateMenu(reqVO.getParentId(), reqVO.getName(), null); | |
| 106 | + | |
| 103 | 107 | // 插入数据库 |
| 104 | 108 | MenuDO menu = MenuConvert.INSTANCE.convert(reqVO); |
| 105 | 109 | initMenuProperty(menu); |
| ... | ... | @@ -117,9 +121,10 @@ public class MenuServiceImpl implements MenuService { |
| 117 | 121 | throw ServiceExceptionUtil.exception(MENU_NOT_EXISTS); |
| 118 | 122 | } |
| 119 | 123 | // 校验父菜单存在 |
| 120 | - checkParentResource(reqVO.getParentId(), reqVO.getId()); | |
| 124 | + validateParentMenu(reqVO.getParentId(), reqVO.getId()); | |
| 121 | 125 | // 校验菜单(自己) |
| 122 | - checkResource(reqVO.getParentId(), reqVO.getName(), reqVO.getId()); | |
| 126 | + validateMenu(reqVO.getParentId(), reqVO.getName(), reqVO.getId()); | |
| 127 | + | |
| 123 | 128 | // 更新到数据库 |
| 124 | 129 | MenuDO updateObject = MenuConvert.INSTANCE.convert(reqVO); |
| 125 | 130 | initMenuProperty(updateObject); |
| ... | ... | @@ -128,13 +133,8 @@ public class MenuServiceImpl implements MenuService { |
| 128 | 133 | menuProducer.sendMenuRefreshMessage(); |
| 129 | 134 | } |
| 130 | 135 | |
| 131 | - /** | |
| 132 | - * 删除菜单 | |
| 133 | - * | |
| 134 | - * @param menuId 菜单编号 | |
| 135 | - */ | |
| 136 | - @Transactional(rollbackFor = Exception.class) | |
| 137 | 136 | @Override |
| 137 | + @Transactional(rollbackFor = Exception.class) | |
| 138 | 138 | public void deleteMenu(Long menuId) { |
| 139 | 139 | // 校验是否还有子菜单 |
| 140 | 140 | if (menuMapper.selectCountByParentId(menuId) > 0) { |
| ... | ... | @@ -160,20 +160,20 @@ public class MenuServiceImpl implements MenuService { |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | @Override |
| 163 | - public List<MenuDO> getMenus() { | |
| 163 | + public List<MenuDO> getMenuList() { | |
| 164 | 164 | return menuMapper.selectList(); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | @Override |
| 168 | - public List<MenuDO> getTenantMenus(MenuListReqVO reqVO) { | |
| 169 | - List<MenuDO> menus = getMenus(reqVO); | |
| 168 | + public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) { | |
| 169 | + List<MenuDO> menus = getMenuList(reqVO); | |
| 170 | 170 | // 开启多租户的情况下,需要过滤掉未开通的菜单 |
| 171 | 171 | tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); |
| 172 | 172 | return menus; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | @Override |
| 176 | - public List<MenuDO> getMenus(MenuListReqVO reqVO) { | |
| 176 | + public List<MenuDO> getMenuList(MenuListReqVO reqVO) { | |
| 177 | 177 | return menuMapper.selectList(reqVO); |
| 178 | 178 | } |
| 179 | 179 | |
| ... | ... | @@ -223,8 +223,8 @@ public class MenuServiceImpl implements MenuService { |
| 223 | 223 | * @param childId 当前菜单编号 |
| 224 | 224 | */ |
| 225 | 225 | @VisibleForTesting |
| 226 | - public void checkParentResource(Long parentId, Long childId) { | |
| 227 | - if (parentId == null || MenuIdEnum.ROOT.getId().equals(parentId)) { | |
| 226 | + void validateParentMenu(Long parentId, Long childId) { | |
| 227 | + if (parentId == null || ID_ROOT.equals(parentId)) { | |
| 228 | 228 | return; |
| 229 | 229 | } |
| 230 | 230 | // 不能设置自己为父菜单 |
| ... | ... | @@ -253,7 +253,7 @@ public class MenuServiceImpl implements MenuService { |
| 253 | 253 | * @param id 菜单编号 |
| 254 | 254 | */ |
| 255 | 255 | @VisibleForTesting |
| 256 | - public void checkResource(Long parentId, String name, Long id) { | |
| 256 | + void validateMenu(Long parentId, String name, Long id) { | |
| 257 | 257 | MenuDO menu = menuMapper.selectByParentIdAndName(parentId, name); |
| 258 | 258 | if (menu == null) { |
| 259 | 259 | return; | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java
| ... | ... | @@ -158,7 +158,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // 判断角色是否包含超级管理员。如果是超级管理员,获取到全部 |
| 161 | - List<RoleDO> roleList = roleService.getRolesFromCache(roleIds); | |
| 161 | + List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds); | |
| 162 | 162 | if (roleService.hasAnySuperAdmin(roleList)) { |
| 163 | 163 | return menuService.getMenuListFromCache(menuTypes, menusStatuses); |
| 164 | 164 | } |
| ... | ... | @@ -190,7 +190,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 190 | 190 | public Set<Long> getRoleMenuIds(Long roleId) { |
| 191 | 191 | // 如果是管理员的情况下,获取全部菜单编号 |
| 192 | 192 | if (roleService.hasAnySuperAdmin(Collections.singleton(roleId))) { |
| 193 | - return convertSet(menuService.getMenus(), MenuDO::getId); | |
| 193 | + return convertSet(menuService.getMenuList(), MenuDO::getId); | |
| 194 | 194 | } |
| 195 | 195 | // 如果是非管理员的情况下,获得拥有的菜单编号 |
| 196 | 196 | return convertSet(roleMenuMapper.selectListByRoleId(roleId), RoleMenuDO::getMenuId); |
| ... | ... | @@ -371,7 +371,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 371 | 371 | if (roleService.hasAnySuperAdmin(roleIds)) { |
| 372 | 372 | return true; |
| 373 | 373 | } |
| 374 | - Set<String> userRoles = convertSet(roleService.getRolesFromCache(roleIds), | |
| 374 | + Set<String> userRoles = convertSet(roleService.getRoleListFromCache(roleIds), | |
| 375 | 375 | RoleDO::getCode); |
| 376 | 376 | return CollUtil.containsAny(userRoles, Sets.newHashSet(roles)); |
| 377 | 377 | } |
| ... | ... | @@ -388,7 +388,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 388 | 388 | result.setSelf(true); |
| 389 | 389 | return result; |
| 390 | 390 | } |
| 391 | - List<RoleDO> roles = roleService.getRolesFromCache(roleIds); | |
| 391 | + List<RoleDO> roles = roleService.getRoleListFromCache(roleIds); | |
| 392 | 392 | |
| 393 | 393 | // 获得用户的部门编号的缓存,通过 Guava 的 Suppliers 惰性求值,即有且仅有第一次发起 DB 的查询 |
| 394 | 394 | Supplier<Long> userDeptIdCache = Suppliers.memoize(() -> userService.getUser(userId).getDeptId()); |
| ... | ... | @@ -418,7 +418,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 418 | 418 | } |
| 419 | 419 | // 情况四,DEPT_DEPT_AND_CHILD |
| 420 | 420 | if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_AND_CHILD.getScope())) { |
| 421 | - List<DeptDO> depts = deptService.getDeptsByParentIdFromCache(userDeptIdCache.get(), true); | |
| 421 | + List<DeptDO> depts = deptService.getDeptListByParentIdFromCache(userDeptIdCache.get(), true); | |
| 422 | 422 | CollUtil.addAll(result.getDeptIds(), CollectionUtils.convertList(depts, DeptDO::getId)); |
| 423 | 423 | // 添加本身部门编号 |
| 424 | 424 | CollUtil.addAll(result.getDeptIds(), userDeptIdCache.get()); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleService.java
| ... | ... | @@ -79,7 +79,7 @@ public interface RoleService { |
| 79 | 79 | * @param statuses 筛选的状态。允许空,空时不筛选 |
| 80 | 80 | * @return 角色列表 |
| 81 | 81 | */ |
| 82 | - List<RoleDO> getRoles(@Nullable Collection<Integer> statuses); | |
| 82 | + List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses); | |
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * 获得角色数组,从缓存中 |
| ... | ... | @@ -87,7 +87,7 @@ public interface RoleService { |
| 87 | 87 | * @param ids 角色编号数组 |
| 88 | 88 | * @return 角色数组 |
| 89 | 89 | */ |
| 90 | - List<RoleDO> getRolesFromCache(Collection<Long> ids); | |
| 90 | + List<RoleDO> getRoleListFromCache(Collection<Long> ids); | |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | 93 | * 判断角色数组中,是否有超级管理员 |
| ... | ... | @@ -104,7 +104,7 @@ public interface RoleService { |
| 104 | 104 | * @return 是否有管理员 |
| 105 | 105 | */ |
| 106 | 106 | default boolean hasAnySuperAdmin(Set<Long> ids) { |
| 107 | - return hasAnySuperAdmin(getRolesFromCache(ids)); | |
| 107 | + return hasAnySuperAdmin(getRoleListFromCache(ids)); | |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
| ... | ... | @@ -138,6 +138,6 @@ public interface RoleService { |
| 138 | 138 | * |
| 139 | 139 | * @param ids 角色编号数组 |
| 140 | 140 | */ |
| 141 | - void validRoles(Collection<Long> ids); | |
| 141 | + void validateRoleList(Collection<Long> ids); | |
| 142 | 142 | |
| 143 | 143 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java
| ... | ... | @@ -5,7 +5,6 @@ import cn.hutool.core.collection.CollectionUtil; |
| 5 | 5 | import cn.hutool.core.util.ObjectUtil; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | |
| 9 | 8 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 10 | 9 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO; |
| 11 | 10 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleExportReqVO; |
| ... | ... | @@ -34,6 +33,7 @@ import java.util.*; |
| 34 | 33 | import java.util.stream.Collectors; |
| 35 | 34 | |
| 36 | 35 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 36 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 37 | 37 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 38 | 38 | |
| 39 | 39 | /** |
| ... | ... | @@ -76,7 +76,7 @@ public class RoleServiceImpl implements RoleService { |
| 76 | 76 | log.info("[initLocalCache][缓存角色,数量为:{}]", roleList.size()); |
| 77 | 77 | |
| 78 | 78 | // 第二步:构建缓存 |
| 79 | - roleCache = CollectionUtils.convertMap(roleList, RoleDO::getId); | |
| 79 | + roleCache = convertMap(roleList, RoleDO::getId); | |
| 80 | 80 | }); |
| 81 | 81 | } |
| 82 | 82 | |
| ... | ... | @@ -84,7 +84,7 @@ public class RoleServiceImpl implements RoleService { |
| 84 | 84 | @Transactional |
| 85 | 85 | public Long createRole(RoleCreateReqVO reqVO, Integer type) { |
| 86 | 86 | // 校验角色 |
| 87 | - checkDuplicateRole(reqVO.getName(), reqVO.getCode(), null); | |
| 87 | + validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), null); | |
| 88 | 88 | // 插入到数据库 |
| 89 | 89 | RoleDO role = RoleConvert.INSTANCE.convert(reqVO); |
| 90 | 90 | role.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType())); |
| ... | ... | @@ -105,13 +105,13 @@ public class RoleServiceImpl implements RoleService { |
| 105 | 105 | @Override |
| 106 | 106 | public void updateRole(RoleUpdateReqVO reqVO) { |
| 107 | 107 | // 校验是否可以更新 |
| 108 | - checkUpdateRole(reqVO.getId()); | |
| 108 | + validateRoleForUpdate(reqVO.getId()); | |
| 109 | 109 | // 校验角色的唯一字段是否重复 |
| 110 | - checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); | |
| 110 | + validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), reqVO.getId()); | |
| 111 | 111 | |
| 112 | 112 | // 更新到数据库 |
| 113 | - RoleDO updateObject = RoleConvert.INSTANCE.convert(reqVO); | |
| 114 | - roleMapper.updateById(updateObject); | |
| 113 | + RoleDO updateObj = RoleConvert.INSTANCE.convert(reqVO); | |
| 114 | + roleMapper.updateById(updateObj); | |
| 115 | 115 | // 发送刷新消息 |
| 116 | 116 | roleProducer.sendRoleRefreshMessage(); |
| 117 | 117 | } |
| ... | ... | @@ -119,12 +119,11 @@ public class RoleServiceImpl implements RoleService { |
| 119 | 119 | @Override |
| 120 | 120 | public void updateRoleStatus(Long id, Integer status) { |
| 121 | 121 | // 校验是否可以更新 |
| 122 | - checkUpdateRole(id); | |
| 122 | + validateRoleForUpdate(id); | |
| 123 | + | |
| 123 | 124 | // 更新状态 |
| 124 | - RoleDO updateObject = new RoleDO(); | |
| 125 | - updateObject.setId(id); | |
| 126 | - updateObject.setStatus(status); | |
| 127 | - roleMapper.updateById(updateObject); | |
| 125 | + RoleDO updateObj = new RoleDO().setId(id).setStatus(status); | |
| 126 | + roleMapper.updateById(updateObj); | |
| 128 | 127 | // 发送刷新消息 |
| 129 | 128 | roleProducer.sendRoleRefreshMessage(); |
| 130 | 129 | } |
| ... | ... | @@ -132,7 +131,8 @@ public class RoleServiceImpl implements RoleService { |
| 132 | 131 | @Override |
| 133 | 132 | public void updateRoleDataScope(Long id, Integer dataScope, Set<Long> dataScopeDeptIds) { |
| 134 | 133 | // 校验是否可以更新 |
| 135 | - checkUpdateRole(id); | |
| 134 | + validateRoleForUpdate(id); | |
| 135 | + | |
| 136 | 136 | // 更新数据范围 |
| 137 | 137 | RoleDO updateObject = new RoleDO(); |
| 138 | 138 | updateObject.setId(id); |
| ... | ... | @@ -147,7 +147,7 @@ public class RoleServiceImpl implements RoleService { |
| 147 | 147 | @Transactional(rollbackFor = Exception.class) |
| 148 | 148 | public void deleteRole(Long id) { |
| 149 | 149 | // 校验是否可以更新 |
| 150 | - this.checkUpdateRole(id); | |
| 150 | + validateRoleForUpdate(id); | |
| 151 | 151 | // 标记删除 |
| 152 | 152 | roleMapper.deleteById(id); |
| 153 | 153 | // 删除相关数据 |
| ... | ... | @@ -169,7 +169,7 @@ public class RoleServiceImpl implements RoleService { |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | @Override |
| 172 | - public List<RoleDO> getRoles(@Nullable Collection<Integer> statuses) { | |
| 172 | + public List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses) { | |
| 173 | 173 | if (CollUtil.isEmpty(statuses)) { |
| 174 | 174 | return roleMapper.selectList(); |
| 175 | 175 | } |
| ... | ... | @@ -177,7 +177,7 @@ public class RoleServiceImpl implements RoleService { |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | @Override |
| 180 | - public List<RoleDO> getRolesFromCache(Collection<Long> ids) { | |
| 180 | + public List<RoleDO> getRoleListFromCache(Collection<Long> ids) { | |
| 181 | 181 | if (CollectionUtil.isEmpty(ids)) { |
| 182 | 182 | return Collections.emptyList(); |
| 183 | 183 | } |
| ... | ... | @@ -219,7 +219,7 @@ public class RoleServiceImpl implements RoleService { |
| 219 | 219 | * @param id 角色编号 |
| 220 | 220 | */ |
| 221 | 221 | @VisibleForTesting |
| 222 | - public void checkDuplicateRole(String name, String code, Long id) { | |
| 222 | + void validateRoleDuplicate(String name, String code, Long id) { | |
| 223 | 223 | // 0. 超级管理员,不允许创建 |
| 224 | 224 | if (RoleCodeEnum.isSuperAdmin(code)) { |
| 225 | 225 | throw exception(ROLE_ADMIN_CODE_ERROR, code); |
| ... | ... | @@ -246,7 +246,7 @@ public class RoleServiceImpl implements RoleService { |
| 246 | 246 | * @param id 角色编号 |
| 247 | 247 | */ |
| 248 | 248 | @VisibleForTesting |
| 249 | - public void checkUpdateRole(Long id) { | |
| 249 | + void validateRoleForUpdate(Long id) { | |
| 250 | 250 | RoleDO roleDO = roleMapper.selectById(id); |
| 251 | 251 | if (roleDO == null) { |
| 252 | 252 | throw exception(ROLE_NOT_EXISTS); |
| ... | ... | @@ -258,13 +258,13 @@ public class RoleServiceImpl implements RoleService { |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | @Override |
| 261 | - public void validRoles(Collection<Long> ids) { | |
| 261 | + public void validateRoleList(Collection<Long> ids) { | |
| 262 | 262 | if (CollUtil.isEmpty(ids)) { |
| 263 | 263 | return; |
| 264 | 264 | } |
| 265 | 265 | // 获得角色信息 |
| 266 | 266 | List<RoleDO> roles = roleMapper.selectBatchIds(ids); |
| 267 | - Map<Long, RoleDO> roleMap = CollectionUtils.convertMap(roles, RoleDO::getId); | |
| 267 | + Map<Long, RoleDO> roleMap = convertMap(roles, RoleDO::getId); | |
| 268 | 268 | // 校验 |
| 269 | 269 | ids.forEach(id -> { |
| 270 | 270 | RoleDO role = roleMap.get(id); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordService.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImpl.java
| ... | ... | @@ -25,6 +25,7 @@ import javax.annotation.Resource; |
| 25 | 25 | import java.util.*; |
| 26 | 26 | |
| 27 | 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 28 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; | |
| 28 | 29 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS; |
| 29 | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS; |
| 30 | 31 | |
| ... | ... | @@ -85,7 +86,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 85 | 86 | |
| 86 | 87 | private void initSensitiveWordTrie(List<SensitiveWordDO> wordDOs) { |
| 87 | 88 | // 过滤禁用的敏感词 |
| 88 | - wordDOs = CollectionUtils.filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())); | |
| 89 | + wordDOs = filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())); | |
| 89 | 90 | |
| 90 | 91 | // 初始化默认的 defaultSensitiveWordTrie |
| 91 | 92 | this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName)); |
| ... | ... | @@ -107,7 +108,8 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 107 | 108 | @Override |
| 108 | 109 | public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) { |
| 109 | 110 | // 校验唯一性 |
| 110 | - checkSensitiveWordNameUnique(null, createReqVO.getName()); | |
| 111 | + validateSensitiveWordNameUnique(null, createReqVO.getName()); | |
| 112 | + | |
| 111 | 113 | // 插入 |
| 112 | 114 | SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO); |
| 113 | 115 | sensitiveWordMapper.insert(sensitiveWord); |
| ... | ... | @@ -119,8 +121,9 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 119 | 121 | @Override |
| 120 | 122 | public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) { |
| 121 | 123 | // 校验唯一性 |
| 122 | - checkSensitiveWordExists(updateReqVO.getId()); | |
| 123 | - checkSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName()); | |
| 124 | + validateSensitiveWordExists(updateReqVO.getId()); | |
| 125 | + validateSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName()); | |
| 126 | + | |
| 124 | 127 | // 更新 |
| 125 | 128 | SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO); |
| 126 | 129 | sensitiveWordMapper.updateById(updateObj); |
| ... | ... | @@ -131,14 +134,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 131 | 134 | @Override |
| 132 | 135 | public void deleteSensitiveWord(Long id) { |
| 133 | 136 | // 校验存在 |
| 134 | - checkSensitiveWordExists(id); | |
| 137 | + validateSensitiveWordExists(id); | |
| 135 | 138 | // 删除 |
| 136 | 139 | sensitiveWordMapper.deleteById(id); |
| 137 | 140 | // 发送消息,刷新缓存 |
| 138 | 141 | sensitiveWordProducer.sendSensitiveWordRefreshMessage(); |
| 139 | 142 | } |
| 140 | 143 | |
| 141 | - private void checkSensitiveWordNameUnique(Long id, String name) { | |
| 144 | + private void validateSensitiveWordNameUnique(Long id, String name) { | |
| 142 | 145 | SensitiveWordDO word = sensitiveWordMapper.selectByName(name); |
| 143 | 146 | if (word == null) { |
| 144 | 147 | return; |
| ... | ... | @@ -152,7 +155,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 152 | 155 | } |
| 153 | 156 | } |
| 154 | 157 | |
| 155 | - private void checkSensitiveWordExists(Long id) { | |
| 158 | + private void validateSensitiveWordExists(Long id) { | |
| 156 | 159 | if (sensitiveWordMapper.selectById(id) == null) { |
| 157 | 160 | throw exception(SENSITIVE_WORD_NOT_EXISTS); |
| 158 | 161 | } |
| ... | ... | @@ -179,7 +182,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 179 | 182 | } |
| 180 | 183 | |
| 181 | 184 | @Override |
| 182 | - public Set<String> getSensitiveWordTags() { | |
| 185 | + public Set<String> getSensitiveWordTagSet() { | |
| 183 | 186 | return sensitiveWordTagsCache; |
| 184 | 187 | } |
| 185 | 188 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsChannelService.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.sms; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 4 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 4 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelCreateReqVO; |
| 6 | 5 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO; |
| 7 | 6 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelUpdateReqVO; |
| 7 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 8 | 8 | |
| 9 | 9 | import javax.validation.Valid; |
| 10 | -import java.util.Collection; | |
| 11 | 10 | import java.util.List; |
| 12 | 11 | |
| 13 | 12 | /** |
| ... | ... | @@ -54,14 +53,6 @@ public interface SmsChannelService { |
| 54 | 53 | SmsChannelDO getSmsChannel(Long id); |
| 55 | 54 | |
| 56 | 55 | /** |
| 57 | - * 获得短信渠道列表 | |
| 58 | - * | |
| 59 | - * @param ids 编号 | |
| 60 | - * @return 短信渠道列表 | |
| 61 | - */ | |
| 62 | - List<SmsChannelDO> getSmsChannelList(Collection<Long> ids); | |
| 63 | - | |
| 64 | - /** | |
| 65 | 56 | * 获得所有短信渠道列表 |
| 66 | 57 | * |
| 67 | 58 | * @return 短信渠道列表 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsChannelServiceImpl.java
| ... | ... | @@ -15,7 +15,6 @@ import org.springframework.stereotype.Service; |
| 15 | 15 | |
| 16 | 16 | import javax.annotation.PostConstruct; |
| 17 | 17 | import javax.annotation.Resource; |
| 18 | -import java.util.Collection; | |
| 19 | 18 | import java.util.List; |
| 20 | 19 | |
| 21 | 20 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| ... | ... | @@ -69,7 +68,7 @@ public class SmsChannelServiceImpl implements SmsChannelService { |
| 69 | 68 | @Override |
| 70 | 69 | public void updateSmsChannel(SmsChannelUpdateReqVO updateReqVO) { |
| 71 | 70 | // 校验存在 |
| 72 | - this.validateSmsChannelExists(updateReqVO.getId()); | |
| 71 | + validateSmsChannelExists(updateReqVO.getId()); | |
| 73 | 72 | // 更新 |
| 74 | 73 | SmsChannelDO updateObj = SmsChannelConvert.INSTANCE.convert(updateReqVO); |
| 75 | 74 | smsChannelMapper.updateById(updateObj); |
| ... | ... | @@ -80,7 +79,7 @@ public class SmsChannelServiceImpl implements SmsChannelService { |
| 80 | 79 | @Override |
| 81 | 80 | public void deleteSmsChannel(Long id) { |
| 82 | 81 | // 校验存在 |
| 83 | - this.validateSmsChannelExists(id); | |
| 82 | + validateSmsChannelExists(id); | |
| 84 | 83 | // 校验是否有在使用该账号的模版 |
| 85 | 84 | if (smsTemplateService.countByChannelId(id) > 0) { |
| 86 | 85 | throw exception(SMS_CHANNEL_HAS_CHILDREN); |
| ... | ... | @@ -103,11 +102,6 @@ public class SmsChannelServiceImpl implements SmsChannelService { |
| 103 | 102 | } |
| 104 | 103 | |
| 105 | 104 | @Override |
| 106 | - public List<SmsChannelDO> getSmsChannelList(Collection<Long> ids) { | |
| 107 | - return smsChannelMapper.selectBatchIds(ids); | |
| 108 | - } | |
| 109 | - | |
| 110 | - @Override | |
| 111 | 105 | public List<SmsChannelDO> getSmsChannelList() { |
| 112 | 106 | return smsChannelMapper.selectList(); |
| 113 | 107 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeService.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.sms; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 4 | -import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO; | |
| 4 | +import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO; | |
| 5 | 5 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; |
| 6 | 6 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; |
| 7 | 7 | |
| ... | ... | @@ -35,6 +35,6 @@ public interface SmsCodeService { |
| 35 | 35 | * |
| 36 | 36 | * @param reqDTO 校验请求 |
| 37 | 37 | */ |
| 38 | - void checkSmsCode(@Valid SmsCodeCheckReqDTO reqDTO); | |
| 38 | + void validateSmsCode(@Valid SmsCodeValidateReqDTO reqDTO); | |
| 39 | 39 | |
| 40 | 40 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImpl.java
| ... | ... | @@ -3,11 +3,9 @@ package cn.iocoder.yudao.module.system.service.sms; |
| 3 | 3 | import cn.hutool.core.date.LocalDateTimeUtil; |
| 4 | 4 | import cn.hutool.core.lang.Assert; |
| 5 | 5 | import cn.hutool.core.map.MapUtil; |
| 6 | -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | |
| 7 | -import cn.iocoder.yudao.framework.common.util.date.DateUtils; | |
| 8 | -import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO; | |
| 9 | 6 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; |
| 10 | 7 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; |
| 8 | +import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO; | |
| 11 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsCodeDO; |
| 12 | 10 | import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsCodeMapper; |
| 13 | 11 | import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; |
| ... | ... | @@ -16,11 +14,11 @@ import org.springframework.stereotype.Service; |
| 16 | 14 | import org.springframework.validation.annotation.Validated; |
| 17 | 15 | |
| 18 | 16 | import javax.annotation.Resource; |
| 19 | - | |
| 20 | 17 | import java.time.LocalDateTime; |
| 21 | -import java.time.temporal.ChronoUnit; | |
| 22 | 18 | |
| 23 | 19 | import static cn.hutool.core.util.RandomUtil.randomInt; |
| 20 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | |
| 21 | +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.isToday; | |
| 24 | 22 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 25 | 23 | |
| 26 | 24 | /** |
| ... | ... | @@ -58,11 +56,11 @@ public class SmsCodeServiceImpl implements SmsCodeService { |
| 58 | 56 | if (lastSmsCode != null) { |
| 59 | 57 | if (LocalDateTimeUtil.between(lastSmsCode.getCreateTime(), LocalDateTime.now()).toMillis() |
| 60 | 58 | < smsCodeProperties.getSendFrequency().toMillis()) { // 发送过于频繁 |
| 61 | - throw ServiceExceptionUtil.exception(SMS_CODE_SEND_TOO_FAST); | |
| 59 | + throw exception(SMS_CODE_SEND_TOO_FAST); | |
| 62 | 60 | } |
| 63 | - if (DateUtils.isToday(lastSmsCode.getCreateTime()) && // 必须是今天,才能计算超过当天的上限 | |
| 61 | + if (isToday(lastSmsCode.getCreateTime()) && // 必须是今天,才能计算超过当天的上限 | |
| 64 | 62 | lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。 |
| 65 | - throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY); | |
| 63 | + throw exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY); | |
| 66 | 64 | } |
| 67 | 65 | // TODO 芋艿:提升,每个 IP 每天可发送数量 |
| 68 | 66 | // TODO 芋艿:提升,每个 IP 每小时可发送数量 |
| ... | ... | @@ -71,7 +69,7 @@ public class SmsCodeServiceImpl implements SmsCodeService { |
| 71 | 69 | // 创建验证码记录 |
| 72 | 70 | String code = String.valueOf(randomInt(smsCodeProperties.getBeginCode(), smsCodeProperties.getEndCode() + 1)); |
| 73 | 71 | SmsCodeDO newSmsCode = SmsCodeDO.builder().mobile(mobile).code(code).scene(scene) |
| 74 | - .todayIndex(lastSmsCode != null && DateUtils.isToday(lastSmsCode.getCreateTime()) ? lastSmsCode.getTodayIndex() + 1 : 1) | |
| 72 | + .todayIndex(lastSmsCode != null && isToday(lastSmsCode.getCreateTime()) ? lastSmsCode.getTodayIndex() + 1 : 1) | |
| 75 | 73 | .createIp(ip).used(false).build(); |
| 76 | 74 | smsCodeMapper.insert(newSmsCode); |
| 77 | 75 | return code; |
| ... | ... | @@ -80,32 +78,32 @@ public class SmsCodeServiceImpl implements SmsCodeService { |
| 80 | 78 | @Override |
| 81 | 79 | public void useSmsCode(SmsCodeUseReqDTO reqDTO) { |
| 82 | 80 | // 检测验证码是否有效 |
| 83 | - SmsCodeDO lastSmsCode = this.checkSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene()); | |
| 81 | + SmsCodeDO lastSmsCode = validateSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene()); | |
| 84 | 82 | // 使用验证码 |
| 85 | 83 | smsCodeMapper.updateById(SmsCodeDO.builder().id(lastSmsCode.getId()) |
| 86 | 84 | .used(true).usedTime(LocalDateTime.now()).usedIp(reqDTO.getUsedIp()).build()); |
| 87 | 85 | } |
| 88 | 86 | |
| 89 | 87 | @Override |
| 90 | - public void checkSmsCode(SmsCodeCheckReqDTO reqDTO) { | |
| 91 | - checkSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene()); | |
| 88 | + public void validateSmsCode(SmsCodeValidateReqDTO reqDTO) { | |
| 89 | + validateSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene()); | |
| 92 | 90 | } |
| 93 | 91 | |
| 94 | - public SmsCodeDO checkSmsCode0(String mobile, String code, Integer scene) { | |
| 92 | + private SmsCodeDO validateSmsCode0(String mobile, String code, Integer scene) { | |
| 95 | 93 | // 校验验证码 |
| 96 | 94 | SmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile, code, scene); |
| 97 | 95 | // 若验证码不存在,抛出异常 |
| 98 | 96 | if (lastSmsCode == null) { |
| 99 | - throw ServiceExceptionUtil.exception(SMS_CODE_NOT_FOUND); | |
| 97 | + throw exception(SMS_CODE_NOT_FOUND); | |
| 100 | 98 | } |
| 101 | 99 | // 超过时间 |
| 102 | 100 | if (LocalDateTimeUtil.between(lastSmsCode.getCreateTime(), LocalDateTime.now()).toMillis() |
| 103 | 101 | >= smsCodeProperties.getExpireTimes().toMillis()) { // 验证码已过期 |
| 104 | - throw ServiceExceptionUtil.exception(SMS_CODE_EXPIRED); | |
| 102 | + throw exception(SMS_CODE_EXPIRED); | |
| 105 | 103 | } |
| 106 | 104 | // 判断验证码是否已被使用 |
| 107 | 105 | if (Boolean.TRUE.equals(lastSmsCode.getUsed())) { |
| 108 | - throw ServiceExceptionUtil.exception(SMS_CODE_USED); | |
| 106 | + throw exception(SMS_CODE_USED); | |
| 109 | 107 | } |
| 110 | 108 | return lastSmsCode; |
| 111 | 109 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImpl.java
| ... | ... | @@ -66,7 +66,7 @@ public class SmsSendServiceImpl implements SmsSendService { |
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | 68 | // 执行发送 |
| 69 | - return this.sendSingleSms(mobile, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams); | |
| 69 | + return sendSingleSms(mobile, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams); | |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | @Override |
| ... | ... | @@ -76,21 +76,21 @@ public class SmsSendServiceImpl implements SmsSendService { |
| 76 | 76 | mobile = memberService.getMemberUserMobile(userId); |
| 77 | 77 | } |
| 78 | 78 | // 执行发送 |
| 79 | - return this.sendSingleSms(mobile, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams); | |
| 79 | + return sendSingleSms(mobile, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams); | |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | @Override |
| 83 | 83 | public Long sendSingleSms(String mobile, Long userId, Integer userType, |
| 84 | 84 | String templateCode, Map<String, Object> templateParams) { |
| 85 | 85 | // 校验短信模板是否合法 |
| 86 | - SmsTemplateDO template = this.checkSmsTemplateValid(templateCode); | |
| 86 | + SmsTemplateDO template = validateSmsTemplate(templateCode); | |
| 87 | 87 | // 校验短信渠道是否合法 |
| 88 | - SmsChannelDO smsChannel = this.checkSmsChannelValid(template.getChannelId()); | |
| 88 | + SmsChannelDO smsChannel = validateSmsChannel(template.getChannelId()); | |
| 89 | 89 | |
| 90 | 90 | // 校验手机号码是否存在 |
| 91 | - mobile = this.checkMobile(mobile); | |
| 91 | + mobile = validateMobile(mobile); | |
| 92 | 92 | // 构建有序的模板参数。为什么放在这个位置,是提前保证模板参数的正确性,而不是到了插入发送日志 |
| 93 | - List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams); | |
| 93 | + List<KeyValue<String, Object>> newTemplateParams = buildTemplateParams(template, templateParams); | |
| 94 | 94 | |
| 95 | 95 | // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志 |
| 96 | 96 | Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus()) |
| ... | ... | @@ -108,7 +108,7 @@ public class SmsSendServiceImpl implements SmsSendService { |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | @VisibleForTesting |
| 111 | - public SmsChannelDO checkSmsChannelValid(Long channelId) { | |
| 111 | + SmsChannelDO validateSmsChannel(Long channelId) { | |
| 112 | 112 | // 获得短信模板。考虑到效率,从缓存中获取 |
| 113 | 113 | SmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId); |
| 114 | 114 | // 短信模板不存在 |
| ... | ... | @@ -119,7 +119,7 @@ public class SmsSendServiceImpl implements SmsSendService { |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | @VisibleForTesting |
| 122 | - public SmsTemplateDO checkSmsTemplateValid(String templateCode) { | |
| 122 | + SmsTemplateDO validateSmsTemplate(String templateCode) { | |
| 123 | 123 | // 获得短信模板。考虑到效率,从缓存中获取 |
| 124 | 124 | SmsTemplateDO template = smsTemplateService.getSmsTemplateByCodeFromCache(templateCode); |
| 125 | 125 | // 短信模板不存在 |
| ... | ... | @@ -139,7 +139,7 @@ public class SmsSendServiceImpl implements SmsSendService { |
| 139 | 139 | * @return 处理后的参数 |
| 140 | 140 | */ |
| 141 | 141 | @VisibleForTesting |
| 142 | - public List<KeyValue<String, Object>> buildTemplateParams(SmsTemplateDO template, Map<String, Object> templateParams) { | |
| 142 | + List<KeyValue<String, Object>> buildTemplateParams(SmsTemplateDO template, Map<String, Object> templateParams) { | |
| 143 | 143 | return template.getParams().stream().map(key -> { |
| 144 | 144 | Object value = templateParams.get(key); |
| 145 | 145 | if (value == null) { |
| ... | ... | @@ -150,7 +150,7 @@ public class SmsSendServiceImpl implements SmsSendService { |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | @VisibleForTesting |
| 153 | - public String checkMobile(String mobile) { | |
| 153 | + public String validateMobile(String mobile) { | |
| 154 | 154 | if (StrUtil.isEmpty(mobile)) { |
| 155 | 155 | throw exception(SMS_SEND_MOBILE_NOT_EXISTS); |
| 156 | 156 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImpl.java
| ... | ... | @@ -105,11 +105,11 @@ public class SmsTemplateServiceImpl implements SmsTemplateService { |
| 105 | 105 | @Override |
| 106 | 106 | public Long createSmsTemplate(SmsTemplateCreateReqVO createReqVO) { |
| 107 | 107 | // 校验短信渠道 |
| 108 | - SmsChannelDO channelDO = checkSmsChannel(createReqVO.getChannelId()); | |
| 108 | + SmsChannelDO channelDO = validateSmsChannel(createReqVO.getChannelId()); | |
| 109 | 109 | // 校验短信编码是否重复 |
| 110 | - checkSmsTemplateCodeDuplicate(null, createReqVO.getCode()); | |
| 110 | + validateSmsTemplateCodeDuplicate(null, createReqVO.getCode()); | |
| 111 | 111 | // 校验短信模板 |
| 112 | - checkApiTemplate(createReqVO.getChannelId(), createReqVO.getApiTemplateId()); | |
| 112 | + validateApiTemplate(createReqVO.getChannelId(), createReqVO.getApiTemplateId()); | |
| 113 | 113 | |
| 114 | 114 | // 插入 |
| 115 | 115 | SmsTemplateDO template = SmsTemplateConvert.INSTANCE.convert(createReqVO); |
| ... | ... | @@ -125,13 +125,13 @@ public class SmsTemplateServiceImpl implements SmsTemplateService { |
| 125 | 125 | @Override |
| 126 | 126 | public void updateSmsTemplate(SmsTemplateUpdateReqVO updateReqVO) { |
| 127 | 127 | // 校验存在 |
| 128 | - this.validateSmsTemplateExists(updateReqVO.getId()); | |
| 128 | + validateSmsTemplateExists(updateReqVO.getId()); | |
| 129 | 129 | // 校验短信渠道 |
| 130 | - SmsChannelDO channelDO = checkSmsChannel(updateReqVO.getChannelId()); | |
| 130 | + SmsChannelDO channelDO = validateSmsChannel(updateReqVO.getChannelId()); | |
| 131 | 131 | // 校验短信编码是否重复 |
| 132 | - checkSmsTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode()); | |
| 132 | + validateSmsTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode()); | |
| 133 | 133 | // 校验短信模板 |
| 134 | - checkApiTemplate(updateReqVO.getChannelId(), updateReqVO.getApiTemplateId()); | |
| 134 | + validateApiTemplate(updateReqVO.getChannelId(), updateReqVO.getApiTemplateId()); | |
| 135 | 135 | |
| 136 | 136 | // 更新 |
| 137 | 137 | SmsTemplateDO updateObj = SmsTemplateConvert.INSTANCE.convert(updateReqVO); |
| ... | ... | @@ -145,7 +145,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService { |
| 145 | 145 | @Override |
| 146 | 146 | public void deleteSmsTemplate(Long id) { |
| 147 | 147 | // 校验存在 |
| 148 | - this.validateSmsTemplateExists(id); | |
| 148 | + validateSmsTemplateExists(id); | |
| 149 | 149 | // 更新 |
| 150 | 150 | smsTemplateMapper.deleteById(id); |
| 151 | 151 | // 发送刷新消息 |
| ... | ... | @@ -184,7 +184,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService { |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | @VisibleForTesting |
| 187 | - public SmsChannelDO checkSmsChannel(Long channelId) { | |
| 187 | + public SmsChannelDO validateSmsChannel(Long channelId) { | |
| 188 | 188 | SmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId); |
| 189 | 189 | if (channelDO == null) { |
| 190 | 190 | throw exception(SMS_CHANNEL_NOT_EXISTS); |
| ... | ... | @@ -196,7 +196,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService { |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | @VisibleForTesting |
| 199 | - public void checkSmsTemplateCodeDuplicate(Long id, String code) { | |
| 199 | + public void validateSmsTemplateCodeDuplicate(Long id, String code) { | |
| 200 | 200 | SmsTemplateDO template = smsTemplateMapper.selectByCode(code); |
| 201 | 201 | if (template == null) { |
| 202 | 202 | return; |
| ... | ... | @@ -217,7 +217,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService { |
| 217 | 217 | * @param apiTemplateId API 模板编号 |
| 218 | 218 | */ |
| 219 | 219 | @VisibleForTesting |
| 220 | - public void checkApiTemplate(Long channelId, String apiTemplateId) { | |
| 220 | + public void validateApiTemplate(Long channelId, String apiTemplateId) { | |
| 221 | 221 | // 获得短信模板 |
| 222 | 222 | SmsClient smsClient = smsClientFactory.getSmsClient(channelId); |
| 223 | 223 | Assert.notNull(smsClient, String.format("短信客户端(%d) 不存在", channelId)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImpl.java
| ... | ... | @@ -64,9 +64,9 @@ public class TenantPackageServiceImpl implements TenantPackageService { |
| 64 | 64 | @Override |
| 65 | 65 | public void deleteTenantPackage(Long id) { |
| 66 | 66 | // 校验存在 |
| 67 | - this.validateTenantPackageExists(id); | |
| 67 | + validateTenantPackageExists(id); | |
| 68 | 68 | // 校验正在使用 |
| 69 | - this.validateTenantUsed(id); | |
| 69 | + validateTenantUsed(id); | |
| 70 | 70 | // 删除 |
| 71 | 71 | tenantPackageMapper.deleteById(id); |
| 72 | 72 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantService.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java
| ... | ... | @@ -75,7 +75,7 @@ public class TenantServiceImpl implements TenantService { |
| 75 | 75 | private PermissionService permissionService; |
| 76 | 76 | |
| 77 | 77 | @Override |
| 78 | - public List<Long> getTenantIds() { | |
| 78 | + public List<Long> getTenantIdList() { | |
| 79 | 79 | List<TenantDO> tenants = tenantMapper.selectList(); |
| 80 | 80 | return CollectionUtils.convertList(tenants, TenantDO::getId); |
| 81 | 81 | } |
| ... | ... | @@ -138,7 +138,7 @@ public class TenantServiceImpl implements TenantService { |
| 138 | 138 | @Transactional(rollbackFor = Exception.class) |
| 139 | 139 | public void updateTenant(TenantUpdateReqVO updateReqVO) { |
| 140 | 140 | // 校验存在 |
| 141 | - TenantDO tenant = checkUpdateTenant(updateReqVO.getId()); | |
| 141 | + TenantDO tenant = validateUpdateTenant(updateReqVO.getId()); | |
| 142 | 142 | // 校验套餐被禁用 |
| 143 | 143 | TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId()); |
| 144 | 144 | |
| ... | ... | @@ -156,7 +156,7 @@ public class TenantServiceImpl implements TenantService { |
| 156 | 156 | public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) { |
| 157 | 157 | TenantUtils.execute(tenantId, () -> { |
| 158 | 158 | // 获得所有角色 |
| 159 | - List<RoleDO> roles = roleService.getRoles(null); | |
| 159 | + List<RoleDO> roles = roleService.getRoleListByStatus(null); | |
| 160 | 160 | roles.forEach(role -> Assert.isTrue(tenantId.equals(role.getTenantId()), "角色({}/{}) 租户不匹配", |
| 161 | 161 | role.getId(), role.getTenantId(), tenantId)); // 兜底校验 |
| 162 | 162 | // 重新分配每个角色的权限 |
| ... | ... | @@ -179,12 +179,12 @@ public class TenantServiceImpl implements TenantService { |
| 179 | 179 | @Override |
| 180 | 180 | public void deleteTenant(Long id) { |
| 181 | 181 | // 校验存在 |
| 182 | - checkUpdateTenant(id); | |
| 182 | + validateUpdateTenant(id); | |
| 183 | 183 | // 删除 |
| 184 | 184 | tenantMapper.deleteById(id); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - private TenantDO checkUpdateTenant(Long id) { | |
| 187 | + private TenantDO validateUpdateTenant(Long id) { | |
| 188 | 188 | TenantDO tenant = tenantMapper.selectById(id); |
| 189 | 189 | if (tenant == null) { |
| 190 | 190 | throw exception(TENANT_NOT_EXISTS); |
| ... | ... | @@ -248,7 +248,7 @@ public class TenantServiceImpl implements TenantService { |
| 248 | 248 | TenantDO tenant = getTenant(TenantContextHolder.getRequiredTenantId()); |
| 249 | 249 | Set<Long> menuIds; |
| 250 | 250 | if (isSystemTenant(tenant)) { // 系统租户,菜单是全量的 |
| 251 | - menuIds = CollectionUtils.convertSet(menuService.getMenus(), MenuDO::getId); | |
| 251 | + menuIds = CollectionUtils.convertSet(menuService.getMenuList(), MenuDO::getId); | |
| 252 | 252 | } else { |
| 253 | 253 | menuIds = tenantPackageService.getTenantPackage(tenant.getPackageId()).getMenuIds(); |
| 254 | 254 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java
| ... | ... | @@ -127,7 +127,7 @@ public interface AdminUserService { |
| 127 | 127 | * @param deptIds 部门数组 |
| 128 | 128 | * @return 用户数组 |
| 129 | 129 | */ |
| 130 | - List<AdminUserDO> getUsersByDeptIds(Collection<Long> deptIds); | |
| 130 | + List<AdminUserDO> getUserListByDeptIds(Collection<Long> deptIds); | |
| 131 | 131 | |
| 132 | 132 | /** |
| 133 | 133 | * 获得指定岗位的用户数组 |
| ... | ... | @@ -135,7 +135,7 @@ public interface AdminUserService { |
| 135 | 135 | * @param postIds 岗位数组 |
| 136 | 136 | * @return 用户数组 |
| 137 | 137 | */ |
| 138 | - List<AdminUserDO> getUsersByPostIds(Collection<Long> postIds); | |
| 138 | + List<AdminUserDO> getUserListByPostIds(Collection<Long> postIds); | |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * 获得用户列表 |
| ... | ... | @@ -143,7 +143,7 @@ public interface AdminUserService { |
| 143 | 143 | * @param ids 用户编号数组 |
| 144 | 144 | * @return 用户列表 |
| 145 | 145 | */ |
| 146 | - List<AdminUserDO> getUsers(Collection<Long> ids); | |
| 146 | + List<AdminUserDO> getUserList(Collection<Long> ids); | |
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * 校验用户们是否有效。如下情况,视为无效: |
| ... | ... | @@ -152,7 +152,7 @@ public interface AdminUserService { |
| 152 | 152 | * |
| 153 | 153 | * @param ids 用户编号数组 |
| 154 | 154 | */ |
| 155 | - void validUsers(Set<Long> ids); | |
| 155 | + void validateUserList(Collection<Long> ids); | |
| 156 | 156 | |
| 157 | 157 | /** |
| 158 | 158 | * 获得用户 Map |
| ... | ... | @@ -164,7 +164,7 @@ public interface AdminUserService { |
| 164 | 164 | if (CollUtil.isEmpty(ids)) { |
| 165 | 165 | return new HashMap<>(); |
| 166 | 166 | } |
| 167 | - return CollectionUtils.convertMap(getUsers(ids), AdminUserDO::getId); | |
| 167 | + return CollectionUtils.convertMap(getUserList(ids), AdminUserDO::getId); | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
| ... | ... | @@ -173,7 +173,7 @@ public interface AdminUserService { |
| 173 | 173 | * @param reqVO 列表请求 |
| 174 | 174 | * @return 用户列表 |
| 175 | 175 | */ |
| 176 | - List<AdminUserDO> getUsers(UserExportReqVO reqVO); | |
| 176 | + List<AdminUserDO> getUserList(UserExportReqVO reqVO); | |
| 177 | 177 | |
| 178 | 178 | /** |
| 179 | 179 | * 获得用户列表,基于昵称模糊匹配 |
| ... | ... | @@ -181,15 +181,7 @@ public interface AdminUserService { |
| 181 | 181 | * @param nickname 昵称 |
| 182 | 182 | * @return 用户列表 |
| 183 | 183 | */ |
| 184 | - List<AdminUserDO> getUsersByNickname(String nickname); | |
| 185 | - | |
| 186 | - /** | |
| 187 | - * 获得用户列表,基于用户账号模糊匹配 | |
| 188 | - * | |
| 189 | - * @param username 用户账号 | |
| 190 | - * @return 用户列表 | |
| 191 | - */ | |
| 192 | - List<AdminUserDO> getUsersByUsername(String username); | |
| 184 | + List<AdminUserDO> getUserListByNickname(String nickname); | |
| 193 | 185 | |
| 194 | 186 | /** |
| 195 | 187 | * 批量导入用户 |
| ... | ... | @@ -198,7 +190,7 @@ public interface AdminUserService { |
| 198 | 190 | * @param isUpdateSupport 是否支持更新 |
| 199 | 191 | * @return 导入结果 |
| 200 | 192 | */ |
| 201 | - UserImportRespVO importUsers(List<UserImportExcelVO> importUsers, boolean isUpdateSupport); | |
| 193 | + UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport); | |
| 202 | 194 | |
| 203 | 195 | /** |
| 204 | 196 | * 获得指定状态的用户们 |
| ... | ... | @@ -206,7 +198,7 @@ public interface AdminUserService { |
| 206 | 198 | * @param status 状态 |
| 207 | 199 | * @return 用户们 |
| 208 | 200 | */ |
| 209 | - List<AdminUserDO> getUsersByStatus(Integer status); | |
| 201 | + List<AdminUserDO> getUserListByStatus(Integer status); | |
| 210 | 202 | |
| 211 | 203 | /** |
| 212 | 204 | * 判断密码是否匹配 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
| ... | ... | @@ -8,7 +8,7 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 9 | 9 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 10 | 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 11 | -import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission; | |
| 11 | +import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils; | |
| 12 | 12 | import cn.iocoder.yudao.module.infra.api.file.FileApi; |
| 13 | 13 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; |
| 14 | 14 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; |
| ... | ... | @@ -43,6 +43,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * 后台用户 Service 实现类 |
| 46 | + * | |
| 46 | 47 | * @author 芋道源码 |
| 47 | 48 | */ |
| 48 | 49 | @Service("adminUserService") |
| ... | ... | @@ -73,10 +74,6 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 73 | 74 | @Resource |
| 74 | 75 | private FileApi fileApi; |
| 75 | 76 | |
| 76 | - @Resource | |
| 77 | - @Lazy // 循环依赖(自己依赖自己),避免报错 | |
| 78 | - private AdminUserServiceImpl self; | |
| 79 | - | |
| 80 | 77 | @Override |
| 81 | 78 | @Transactional(rollbackFor = Exception.class) |
| 82 | 79 | public Long createUser(UserCreateReqVO reqVO) { |
| ... | ... | @@ -88,7 +85,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 88 | 85 | } |
| 89 | 86 | }); |
| 90 | 87 | // 校验正确性 |
| 91 | - self.checkCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), | |
| 88 | + validateUserForCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), | |
| 92 | 89 | reqVO.getDeptId(), reqVO.getPostIds()); |
| 93 | 90 | // 插入用户 |
| 94 | 91 | AdminUserDO user = UserConvert.INSTANCE.convert(reqVO); |
| ... | ... | @@ -107,7 +104,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 107 | 104 | @Transactional(rollbackFor = Exception.class) |
| 108 | 105 | public void updateUser(UserUpdateReqVO reqVO) { |
| 109 | 106 | // 校验正确性 |
| 110 | - self.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), | |
| 107 | + validateUserForCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), | |
| 111 | 108 | reqVO.getDeptId(), reqVO.getPostIds()); |
| 112 | 109 | // 更新用户 |
| 113 | 110 | AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); |
| ... | ... | @@ -141,9 +138,9 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 141 | 138 | @Override |
| 142 | 139 | public void updateUserProfile(Long id, UserProfileUpdateReqVO reqVO) { |
| 143 | 140 | // 校验正确性 |
| 144 | - checkUserExists(id); | |
| 145 | - checkEmailUnique(id, reqVO.getEmail()); | |
| 146 | - checkMobileUnique(id, reqVO.getMobile()); | |
| 141 | + validateUserExists(id); | |
| 142 | + validateEmailUnique(id, reqVO.getEmail()); | |
| 143 | + validateMobileUnique(id, reqVO.getMobile()); | |
| 147 | 144 | // 执行更新 |
| 148 | 145 | userMapper.updateById(UserConvert.INSTANCE.convert(reqVO).setId(id)); |
| 149 | 146 | } |
| ... | ... | @@ -151,7 +148,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 151 | 148 | @Override |
| 152 | 149 | public void updateUserPassword(Long id, UserProfileUpdatePasswordReqVO reqVO) { |
| 153 | 150 | // 校验旧密码密码 |
| 154 | - checkOldPassword(id, reqVO.getOldPassword()); | |
| 151 | + validateOldPassword(id, reqVO.getOldPassword()); | |
| 155 | 152 | // 执行更新 |
| 156 | 153 | AdminUserDO updateObj = new AdminUserDO().setId(id); |
| 157 | 154 | updateObj.setPassword(encodePassword(reqVO.getNewPassword())); // 加密密码 |
| ... | ... | @@ -160,7 +157,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 160 | 157 | |
| 161 | 158 | @Override |
| 162 | 159 | public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception { |
| 163 | - checkUserExists(id); | |
| 160 | + validateUserExists(id); | |
| 164 | 161 | // 存储文件 |
| 165 | 162 | String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile)); |
| 166 | 163 | // 更新路径 |
| ... | ... | @@ -174,7 +171,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 174 | 171 | @Override |
| 175 | 172 | public void updateUserPassword(Long id, String password) { |
| 176 | 173 | // 校验用户存在 |
| 177 | - checkUserExists(id); | |
| 174 | + validateUserExists(id); | |
| 178 | 175 | // 更新密码 |
| 179 | 176 | AdminUserDO updateObj = new AdminUserDO(); |
| 180 | 177 | updateObj.setId(id); |
| ... | ... | @@ -185,7 +182,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 185 | 182 | @Override |
| 186 | 183 | public void updateUserStatus(Long id, Integer status) { |
| 187 | 184 | // 校验用户存在 |
| 188 | - checkUserExists(id); | |
| 185 | + validateUserExists(id); | |
| 189 | 186 | // 更新状态 |
| 190 | 187 | AdminUserDO updateObj = new AdminUserDO(); |
| 191 | 188 | updateObj.setId(id); |
| ... | ... | @@ -197,7 +194,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 197 | 194 | @Transactional(rollbackFor = Exception.class) |
| 198 | 195 | public void deleteUser(Long id) { |
| 199 | 196 | // 校验用户存在 |
| 200 | - checkUserExists(id); | |
| 197 | + validateUserExists(id); | |
| 201 | 198 | // 删除用户 |
| 202 | 199 | userMapper.deleteById(id); |
| 203 | 200 | // 删除用户关联数据 |
| ... | ... | @@ -227,7 +224,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 227 | 224 | } |
| 228 | 225 | |
| 229 | 226 | @Override |
| 230 | - public List<AdminUserDO> getUsersByDeptIds(Collection<Long> deptIds) { | |
| 227 | + public List<AdminUserDO> getUserListByDeptIds(Collection<Long> deptIds) { | |
| 231 | 228 | if (CollUtil.isEmpty(deptIds)) { |
| 232 | 229 | return Collections.emptyList(); |
| 233 | 230 | } |
| ... | ... | @@ -235,7 +232,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 235 | 232 | } |
| 236 | 233 | |
| 237 | 234 | @Override |
| 238 | - public List<AdminUserDO> getUsersByPostIds(Collection<Long> postIds) { | |
| 235 | + public List<AdminUserDO> getUserListByPostIds(Collection<Long> postIds) { | |
| 239 | 236 | if (CollUtil.isEmpty(postIds)) { |
| 240 | 237 | return Collections.emptyList(); |
| 241 | 238 | } |
| ... | ... | @@ -247,7 +244,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 247 | 244 | } |
| 248 | 245 | |
| 249 | 246 | @Override |
| 250 | - public List<AdminUserDO> getUsers(Collection<Long> ids) { | |
| 247 | + public List<AdminUserDO> getUserList(Collection<Long> ids) { | |
| 251 | 248 | if (CollUtil.isEmpty(ids)) { |
| 252 | 249 | return Collections.emptyList(); |
| 253 | 250 | } |
| ... | ... | @@ -255,7 +252,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 255 | 252 | } |
| 256 | 253 | |
| 257 | 254 | @Override |
| 258 | - public void validUsers(Set<Long> ids) { | |
| 255 | + public void validateUserList(Collection<Long> ids) { | |
| 259 | 256 | if (CollUtil.isEmpty(ids)) { |
| 260 | 257 | return; |
| 261 | 258 | } |
| ... | ... | @@ -275,20 +272,15 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 275 | 272 | } |
| 276 | 273 | |
| 277 | 274 | @Override |
| 278 | - public List<AdminUserDO> getUsers(UserExportReqVO reqVO) { | |
| 275 | + public List<AdminUserDO> getUserList(UserExportReqVO reqVO) { | |
| 279 | 276 | return userMapper.selectList(reqVO, getDeptCondition(reqVO.getDeptId())); |
| 280 | 277 | } |
| 281 | 278 | |
| 282 | 279 | @Override |
| 283 | - public List<AdminUserDO> getUsersByNickname(String nickname) { | |
| 280 | + public List<AdminUserDO> getUserListByNickname(String nickname) { | |
| 284 | 281 | return userMapper.selectListByNickname(nickname); |
| 285 | 282 | } |
| 286 | 283 | |
| 287 | - @Override | |
| 288 | - public List<AdminUserDO> getUsersByUsername(String username) { | |
| 289 | - return userMapper.selectListByUsername(username); | |
| 290 | - } | |
| 291 | - | |
| 292 | 284 | /** |
| 293 | 285 | * 获得部门条件:查询指定部门的子部门编号们,包括自身 |
| 294 | 286 | * @param deptId 部门编号 |
| ... | ... | @@ -298,31 +290,33 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 298 | 290 | if (deptId == null) { |
| 299 | 291 | return Collections.emptySet(); |
| 300 | 292 | } |
| 301 | - Set<Long> deptIds = convertSet(deptService.getDeptsByParentIdFromCache( | |
| 293 | + Set<Long> deptIds = convertSet(deptService.getDeptListByParentIdFromCache( | |
| 302 | 294 | deptId, true), DeptDO::getId); |
| 303 | 295 | deptIds.add(deptId); // 包括自身 |
| 304 | 296 | return deptIds; |
| 305 | 297 | } |
| 306 | 298 | |
| 307 | - @DataPermission(enable = false) // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确 | |
| 308 | - public void checkCreateOrUpdate(Long id, String username, String mobile, String email, | |
| 309 | - Long deptId, Set<Long> postIds) { | |
| 310 | - // 校验用户存在 | |
| 311 | - checkUserExists(id); | |
| 312 | - // 校验用户名唯一 | |
| 313 | - checkUsernameUnique(id, username); | |
| 314 | - // 校验手机号唯一 | |
| 315 | - checkMobileUnique(id, mobile); | |
| 316 | - // 校验邮箱唯一 | |
| 317 | - checkEmailUnique(id, email); | |
| 318 | - // 校验部门处于开启状态 | |
| 319 | - deptService.validDepts(CollectionUtils.singleton(deptId)); | |
| 320 | - // 校验岗位处于开启状态 | |
| 321 | - postService.validPosts(postIds); | |
| 299 | + private void validateUserForCreateOrUpdate(Long id, String username, String mobile, String email, | |
| 300 | + Long deptId, Set<Long> postIds) { | |
| 301 | + // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确 | |
| 302 | + DataPermissionUtils.executeIgnore(() -> { | |
| 303 | + // 校验用户存在 | |
| 304 | + validateUserExists(id); | |
| 305 | + // 校验用户名唯一 | |
| 306 | + validateUsernameUnique(id, username); | |
| 307 | + // 校验手机号唯一 | |
| 308 | + validateMobileUnique(id, mobile); | |
| 309 | + // 校验邮箱唯一 | |
| 310 | + validateEmailUnique(id, email); | |
| 311 | + // 校验部门处于开启状态 | |
| 312 | + deptService.validateDeptList(CollectionUtils.singleton(deptId)); | |
| 313 | + // 校验岗位处于开启状态 | |
| 314 | + postService.validatePostList(postIds); | |
| 315 | + }); | |
| 322 | 316 | } |
| 323 | 317 | |
| 324 | 318 | @VisibleForTesting |
| 325 | - public void checkUserExists(Long id) { | |
| 319 | + void validateUserExists(Long id) { | |
| 326 | 320 | if (id == null) { |
| 327 | 321 | return; |
| 328 | 322 | } |
| ... | ... | @@ -333,7 +327,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 333 | 327 | } |
| 334 | 328 | |
| 335 | 329 | @VisibleForTesting |
| 336 | - public void checkUsernameUnique(Long id, String username) { | |
| 330 | + void validateUsernameUnique(Long id, String username) { | |
| 337 | 331 | if (StrUtil.isBlank(username)) { |
| 338 | 332 | return; |
| 339 | 333 | } |
| ... | ... | @@ -351,7 +345,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 351 | 345 | } |
| 352 | 346 | |
| 353 | 347 | @VisibleForTesting |
| 354 | - public void checkEmailUnique(Long id, String email) { | |
| 348 | + void validateEmailUnique(Long id, String email) { | |
| 355 | 349 | if (StrUtil.isBlank(email)) { |
| 356 | 350 | return; |
| 357 | 351 | } |
| ... | ... | @@ -369,7 +363,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 369 | 363 | } |
| 370 | 364 | |
| 371 | 365 | @VisibleForTesting |
| 372 | - public void checkMobileUnique(Long id, String mobile) { | |
| 366 | + void validateMobileUnique(Long id, String mobile) { | |
| 373 | 367 | if (StrUtil.isBlank(mobile)) { |
| 374 | 368 | return; |
| 375 | 369 | } |
| ... | ... | @@ -392,7 +386,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 392 | 386 | * @param oldPassword 旧密码 |
| 393 | 387 | */ |
| 394 | 388 | @VisibleForTesting |
| 395 | - public void checkOldPassword(Long id, String oldPassword) { | |
| 389 | + void validateOldPassword(Long id, String oldPassword) { | |
| 396 | 390 | AdminUserDO user = userMapper.selectById(id); |
| 397 | 391 | if (user == null) { |
| 398 | 392 | throw exception(USER_NOT_EXISTS); |
| ... | ... | @@ -404,7 +398,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 404 | 398 | |
| 405 | 399 | @Override |
| 406 | 400 | @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入 |
| 407 | - public UserImportRespVO importUsers(List<UserImportExcelVO> importUsers, boolean isUpdateSupport) { | |
| 401 | + public UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport) { | |
| 408 | 402 | if (CollUtil.isEmpty(importUsers)) { |
| 409 | 403 | throw exception(USER_IMPORT_LIST_IS_EMPTY); |
| 410 | 404 | } |
| ... | ... | @@ -413,7 +407,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 413 | 407 | importUsers.forEach(importUser -> { |
| 414 | 408 | // 校验,判断是否有不符合的原因 |
| 415 | 409 | try { |
| 416 | - checkCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), | |
| 410 | + validateUserForCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), | |
| 417 | 411 | importUser.getDeptId(), null); |
| 418 | 412 | } catch (ServiceException ex) { |
| 419 | 413 | respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage()); |
| ... | ... | @@ -441,7 +435,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 441 | 435 | } |
| 442 | 436 | |
| 443 | 437 | @Override |
| 444 | - public List<AdminUserDO> getUsersByStatus(Integer status) { | |
| 438 | + public List<AdminUserDO> getUserListByStatus(Integer status) { | |
| 445 | 439 | return userMapper.selectListByStatus(status); |
| 446 | 440 | } |
| 447 | 441 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.auth; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.util.ReflectUtil; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 5 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 5 | 6 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 6 | -import cn.iocoder.yudao.framework.test.core.util.AssertUtils; | |
| 7 | 7 | import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi; |
| 8 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | |
| 9 | +import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*; | |
| 8 | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; |
| 9 | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 10 | 12 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; |
| 11 | 13 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; |
| 14 | +import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; | |
| 15 | +import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; | |
| 12 | 16 | import cn.iocoder.yudao.module.system.service.logger.LoginLogService; |
| 13 | 17 | import cn.iocoder.yudao.module.system.service.member.MemberService; |
| 14 | 18 | import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService; |
| 15 | 19 | import cn.iocoder.yudao.module.system.service.social.SocialUserService; |
| 16 | 20 | import cn.iocoder.yudao.module.system.service.user.AdminUserService; |
| 21 | +import com.xingyuv.captcha.model.common.ResponseModel; | |
| 17 | 22 | import com.xingyuv.captcha.service.CaptchaService; |
| 23 | +import org.junit.jupiter.api.BeforeEach; | |
| 18 | 24 | import org.junit.jupiter.api.Test; |
| 19 | 25 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 20 | 26 | import org.springframework.context.annotation.Import; |
| 21 | 27 | |
| 22 | 28 | import javax.annotation.Resource; |
| 29 | +import javax.validation.ConstraintViolationException; | |
| 30 | +import javax.validation.Validation; | |
| 23 | 31 | import javax.validation.Validator; |
| 24 | 32 | |
| 33 | +import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 25 | 34 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 35 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | |
| 26 | 36 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 27 | 37 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; |
| 28 | 38 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 40 | +import static org.junit.jupiter.api.Assertions.assertThrows; | |
| 29 | 41 | import static org.mockito.ArgumentMatchers.eq; |
| 30 | 42 | import static org.mockito.Mockito.*; |
| 31 | 43 | |
| ... | ... | @@ -42,17 +54,24 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { |
| 42 | 54 | @MockBean |
| 43 | 55 | private LoginLogService loginLogService; |
| 44 | 56 | @MockBean |
| 45 | - private SocialUserService socialService; | |
| 57 | + private SocialUserService socialUserService; | |
| 46 | 58 | @MockBean |
| 47 | 59 | private SmsCodeApi smsCodeApi; |
| 48 | 60 | @MockBean |
| 49 | 61 | private OAuth2TokenService oauth2TokenService; |
| 50 | 62 | @MockBean |
| 51 | 63 | private MemberService memberService; |
| 52 | - | |
| 53 | 64 | @MockBean |
| 54 | 65 | private Validator validator; |
| 55 | 66 | |
| 67 | + @BeforeEach | |
| 68 | + public void setUp() { | |
| 69 | + ReflectUtil.setFieldValue(authService, "captchaEnable", true); | |
| 70 | + // 注入一个 Validator 对象 | |
| 71 | + ReflectUtil.setFieldValue(authService, "validator", | |
| 72 | + Validation.buildDefaultValidatorFactory().getValidator()); | |
| 73 | + } | |
| 74 | + | |
| 56 | 75 | @Test |
| 57 | 76 | public void testAuthenticate_success() { |
| 58 | 77 | // 准备参数 |
| ... | ... | @@ -78,7 +97,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { |
| 78 | 97 | String password = randomString(); |
| 79 | 98 | |
| 80 | 99 | // 调用, 并断言异常 |
| 81 | - AssertUtils.assertServiceException(() -> authService.authenticate(username, password), | |
| 100 | + assertServiceException(() -> authService.authenticate(username, password), | |
| 82 | 101 | AUTH_LOGIN_BAD_CREDENTIALS); |
| 83 | 102 | verify(loginLogService).createLoginLog( |
| 84 | 103 | argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) |
| ... | ... | @@ -98,7 +117,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { |
| 98 | 117 | when(userService.getUserByUsername(eq(username))).thenReturn(user); |
| 99 | 118 | |
| 100 | 119 | // 调用, 并断言异常 |
| 101 | - AssertUtils.assertServiceException(() -> authService.authenticate(username, password), | |
| 120 | + assertServiceException(() -> authService.authenticate(username, password), | |
| 102 | 121 | AUTH_LOGIN_BAD_CREDENTIALS); |
| 103 | 122 | verify(loginLogService).createLoginLog( |
| 104 | 123 | argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) |
| ... | ... | @@ -120,7 +139,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { |
| 120 | 139 | when(userService.isPasswordMatch(eq(password), eq(user.getPassword()))).thenReturn(true); |
| 121 | 140 | |
| 122 | 141 | // 调用, 并断言异常 |
| 123 | - AssertUtils.assertServiceException(() -> authService.authenticate(username, password), | |
| 142 | + assertServiceException(() -> authService.authenticate(username, password), | |
| 124 | 143 | AUTH_LOGIN_USER_DISABLED); |
| 125 | 144 | verify(loginLogService).createLoginLog( |
| 126 | 145 | argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) |
| ... | ... | @@ -129,82 +148,194 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { |
| 129 | 148 | ); |
| 130 | 149 | } |
| 131 | 150 | |
| 132 | -// @Test | |
| 133 | -// public void testCaptcha_success() { | |
| 134 | -// // 准备参数 | |
| 135 | -// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); | |
| 136 | -// | |
| 137 | -// // mock 验证码正确 | |
| 138 | -// when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); | |
| 139 | -// | |
| 140 | -// // 调用 | |
| 141 | -// authService.verifyCaptcha(reqVO); | |
| 142 | -// // 断言 | |
| 143 | -// verify(captchaService).deleteCaptchaCode(reqVO.getUuid()); | |
| 144 | -// } | |
| 145 | -// | |
| 146 | -// @Test | |
| 147 | -// public void testCaptcha_notFound() { | |
| 148 | -// // 准备参数 | |
| 149 | -// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); | |
| 150 | -// | |
| 151 | -// // 调用, 并断言异常 | |
| 152 | -// assertServiceException(() -> authService.verifyCaptcha(reqVO), AUTH_LOGIN_CAPTCHA_NOT_FOUND); | |
| 153 | -// // 校验调用参数 | |
| 154 | -// verify(loginLogService, times(1)).createLoginLog( | |
| 155 | -// argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) | |
| 156 | -// && o.getResult().equals(LoginResultEnum.CAPTCHA_NOT_FOUND.getResult())) | |
| 157 | -// ); | |
| 158 | -// } | |
| 159 | - | |
| 160 | -// @Test | |
| 161 | -// public void testCaptcha_codeError() { | |
| 162 | -// // 准备参数 | |
| 163 | -// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); | |
| 164 | -// | |
| 165 | -// // mock 验证码不正确 | |
| 166 | -// String code = randomString(); | |
| 167 | -// when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(code); | |
| 168 | -// | |
| 169 | -// // 调用, 并断言异常 | |
| 170 | -// assertServiceException(() -> authService.verifyCaptcha(reqVO), AUTH_LOGIN_CAPTCHA_CODE_ERROR); | |
| 171 | -// // 校验调用参数 | |
| 172 | -// verify(loginLogService).createLoginLog( | |
| 173 | -// argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) | |
| 174 | -// && o.getResult().equals(LoginResultEnum.CAPTCHA_CODE_ERROR.getResult())) | |
| 175 | -// ); | |
| 176 | -// } | |
| 177 | - | |
| 178 | -// @Test | |
| 179 | -// public void testLogin_success() { | |
| 180 | -// // 准备参数 | |
| 181 | -// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class, o -> | |
| 182 | -// o.setUsername("test_username").setPassword("test_password")); | |
| 183 | -// | |
| 184 | -// // mock 验证码正确 | |
| 185 | -// when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); | |
| 186 | -// // mock user 数据 | |
| 187 | -// AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username") | |
| 188 | -// .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 189 | -// when(userService.getUserByUsername(eq("test_username"))).thenReturn(user); | |
| 190 | -// // mock password 匹配 | |
| 191 | -// when(userService.isPasswordMatch(eq("test_password"), eq(user.getPassword()))).thenReturn(true); | |
| 192 | -// // mock 缓存登录用户到 Redis | |
| 193 | -// OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L) | |
| 194 | -// .setUserType(UserTypeEnum.ADMIN.getValue())); | |
| 195 | -// when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull())) | |
| 196 | -// .thenReturn(accessTokenDO); | |
| 197 | -// | |
| 198 | -// // 调用, 并断言异常 | |
| 199 | -// AuthLoginRespVO loginRespVO = authService.login(reqVO); | |
| 200 | -// assertPojoEquals(accessTokenDO, loginRespVO); | |
| 201 | -// // 校验调用参数 | |
| 202 | -// verify(loginLogService).createLoginLog( | |
| 203 | -// argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) | |
| 204 | -// && o.getResult().equals(LoginResultEnum.SUCCESS.getResult()) | |
| 205 | -// && o.getUserId().equals(user.getId())) | |
| 206 | -// ); | |
| 207 | -// } | |
| 151 | + @Test | |
| 152 | + public void testLogin_success() { | |
| 153 | + // 准备参数 | |
| 154 | + AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class, o -> | |
| 155 | + o.setUsername("test_username").setPassword("test_password") | |
| 156 | + .setSocialType(randomEle(SocialTypeEnum.values()).getType())); | |
| 157 | + | |
| 158 | + // mock 验证码正确 | |
| 159 | + ReflectUtil.setFieldValue(authService, "captchaEnable", false); | |
| 160 | + // mock user 数据 | |
| 161 | + AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username") | |
| 162 | + .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 163 | + when(userService.getUserByUsername(eq("test_username"))).thenReturn(user); | |
| 164 | + // mock password 匹配 | |
| 165 | + when(userService.isPasswordMatch(eq("test_password"), eq(user.getPassword()))).thenReturn(true); | |
| 166 | + // mock 缓存登录用户到 Redis | |
| 167 | + OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L) | |
| 168 | + .setUserType(UserTypeEnum.ADMIN.getValue())); | |
| 169 | + when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull())) | |
| 170 | + .thenReturn(accessTokenDO); | |
| 171 | + | |
| 172 | + // 调用,并校验 | |
| 173 | + AuthLoginRespVO loginRespVO = authService.login(reqVO); | |
| 174 | + assertPojoEquals(accessTokenDO, loginRespVO); | |
| 175 | + // 校验调用参数 | |
| 176 | + verify(loginLogService).createLoginLog( | |
| 177 | + argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) | |
| 178 | + && o.getResult().equals(LoginResultEnum.SUCCESS.getResult()) | |
| 179 | + && o.getUserId().equals(user.getId())) | |
| 180 | + ); | |
| 181 | + verify(socialUserService).bindSocialUser(eq(new SocialUserBindReqDTO( | |
| 182 | + user.getId(), UserTypeEnum.ADMIN.getValue(), | |
| 183 | + reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState()))); | |
| 184 | + } | |
| 185 | + | |
| 186 | + @Test | |
| 187 | + public void testSendSmsCode() { | |
| 188 | + // 准备参数 | |
| 189 | + String mobile = randomString(); | |
| 190 | + Integer scene = randomEle(SmsSceneEnum.values()).getScene(); | |
| 191 | + AuthSmsSendReqVO reqVO = new AuthSmsSendReqVO(mobile, scene); | |
| 192 | + // mock 方法(用户信息) | |
| 193 | + AdminUserDO user = randomPojo(AdminUserDO.class); | |
| 194 | + when(userService.getUserByMobile(eq(mobile))).thenReturn(user); | |
| 195 | + | |
| 196 | + // 调用 | |
| 197 | + authService.sendSmsCode(reqVO); | |
| 198 | + // 断言 | |
| 199 | + verify(smsCodeApi).sendSmsCode(argThat(sendReqDTO -> { | |
| 200 | + assertEquals(mobile, sendReqDTO.getMobile()); | |
| 201 | + assertEquals(scene, sendReqDTO.getScene()); | |
| 202 | + return true; | |
| 203 | + })); | |
| 204 | + } | |
| 205 | + | |
| 206 | + @Test | |
| 207 | + public void testSmsLogin_success() { | |
| 208 | + // 准备参数 | |
| 209 | + String mobile = randomString(); | |
| 210 | + String scene = randomString(); | |
| 211 | + AuthSmsLoginReqVO reqVO = new AuthSmsLoginReqVO(mobile, scene); | |
| 212 | + // mock 方法(用户信息) | |
| 213 | + AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L)); | |
| 214 | + when(userService.getUserByMobile(eq(mobile))).thenReturn(user); | |
| 215 | + // mock 缓存登录用户到 Redis | |
| 216 | + OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L) | |
| 217 | + .setUserType(UserTypeEnum.ADMIN.getValue())); | |
| 218 | + when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull())) | |
| 219 | + .thenReturn(accessTokenDO); | |
| 220 | + | |
| 221 | + // 调用,并断言 | |
| 222 | + AuthLoginRespVO loginRespVO = authService.smsLogin(reqVO); | |
| 223 | + assertPojoEquals(accessTokenDO, loginRespVO); | |
| 224 | + // 断言调用 | |
| 225 | + verify(loginLogService).createLoginLog( | |
| 226 | + argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_MOBILE.getType()) | |
| 227 | + && o.getResult().equals(LoginResultEnum.SUCCESS.getResult()) | |
| 228 | + && o.getUserId().equals(user.getId())) | |
| 229 | + ); | |
| 230 | + } | |
| 231 | + | |
| 232 | + @Test | |
| 233 | + public void testSocialLogin_success() { | |
| 234 | + // 准备参数 | |
| 235 | + AuthSocialLoginReqVO reqVO = randomPojo(AuthSocialLoginReqVO.class); | |
| 236 | + // mock 方法(绑定的用户编号) | |
| 237 | + Long userId = 1L; | |
| 238 | + when(socialUserService.getBindUserId(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()), | |
| 239 | + eq(reqVO.getCode()), eq(reqVO.getState()))).thenReturn(userId); | |
| 240 | + // mock(用户) | |
| 241 | + AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(userId)); | |
| 242 | + when(userService.getUser(eq(userId))).thenReturn(user); | |
| 243 | + // mock 缓存登录用户到 Redis | |
| 244 | + OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L) | |
| 245 | + .setUserType(UserTypeEnum.ADMIN.getValue())); | |
| 246 | + when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull())) | |
| 247 | + .thenReturn(accessTokenDO); | |
| 248 | + | |
| 249 | + // 调用,并断言 | |
| 250 | + AuthLoginRespVO loginRespVO = authService.socialLogin(reqVO); | |
| 251 | + assertPojoEquals(accessTokenDO, loginRespVO); | |
| 252 | + // 断言调用 | |
| 253 | + verify(loginLogService).createLoginLog( | |
| 254 | + argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_SOCIAL.getType()) | |
| 255 | + && o.getResult().equals(LoginResultEnum.SUCCESS.getResult()) | |
| 256 | + && o.getUserId().equals(user.getId())) | |
| 257 | + ); | |
| 258 | + } | |
| 259 | + | |
| 260 | + @Test | |
| 261 | + public void testValidateCaptcha_successWithEnable() { | |
| 262 | + // 准备参数 | |
| 263 | + AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); | |
| 264 | + | |
| 265 | + // mock 验证码打开 | |
| 266 | + ReflectUtil.setFieldValue(authService, "captchaEnable", true); | |
| 267 | + // mock 验证通过 | |
| 268 | + when(captchaService.verification(argThat(captchaVO -> { | |
| 269 | + assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification()); | |
| 270 | + return true; | |
| 271 | + }))).thenReturn(ResponseModel.success()); | |
| 272 | + | |
| 273 | + // 调用,无需断言 | |
| 274 | + authService.validateCaptcha(reqVO); | |
| 275 | + } | |
| 276 | + | |
| 277 | + @Test | |
| 278 | + public void testValidateCaptcha_successWithDisable() { | |
| 279 | + // 准备参数 | |
| 280 | + AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); | |
| 281 | + | |
| 282 | + // mock 验证码关闭 | |
| 283 | + ReflectUtil.setFieldValue(authService, "captchaEnable", false); | |
| 284 | + | |
| 285 | + // 调用,无需断言 | |
| 286 | + authService.validateCaptcha(reqVO); | |
| 287 | + } | |
| 288 | + | |
| 289 | + @Test | |
| 290 | + public void testValidateCaptcha_constraintViolationException() { | |
| 291 | + // 准备参数 | |
| 292 | + AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class).setCaptchaVerification(null); | |
| 293 | + | |
| 294 | + // mock 验证码打开 | |
| 295 | + ReflectUtil.setFieldValue(authService, "captchaEnable", true); | |
| 296 | + | |
| 297 | + // 调用,并断言异常 | |
| 298 | + assertThrows(ConstraintViolationException.class, () -> authService.validateCaptcha(reqVO), | |
| 299 | + "验证码不能为空"); | |
| 300 | + } | |
| 301 | + | |
| 302 | + | |
| 303 | + @Test | |
| 304 | + public void testCaptcha_fail() { | |
| 305 | + // 准备参数 | |
| 306 | + AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); | |
| 307 | + | |
| 308 | + // mock 验证码打开 | |
| 309 | + ReflectUtil.setFieldValue(authService, "captchaEnable", true); | |
| 310 | + // mock 验证通过 | |
| 311 | + when(captchaService.verification(argThat(captchaVO -> { | |
| 312 | + assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification()); | |
| 313 | + return true; | |
| 314 | + }))).thenReturn(ResponseModel.errorMsg("就是不对")); | |
| 315 | + | |
| 316 | + // 调用, 并断言异常 | |
| 317 | + assertServiceException(() -> authService.validateCaptcha(reqVO), AUTH_LOGIN_CAPTCHA_CODE_ERROR, "就是不对"); | |
| 318 | + // 校验调用参数 | |
| 319 | + verify(loginLogService).createLoginLog( | |
| 320 | + argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) | |
| 321 | + && o.getResult().equals(LoginResultEnum.CAPTCHA_CODE_ERROR.getResult())) | |
| 322 | + ); | |
| 323 | + } | |
| 324 | + | |
| 325 | + @Test | |
| 326 | + public void testRefreshToken() { | |
| 327 | + // 准备参数 | |
| 328 | + String refreshToken = randomString(); | |
| 329 | + // mock 方法 | |
| 330 | + OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class); | |
| 331 | + when(oauth2TokenService.refreshAccessToken(eq(refreshToken), eq("default"))) | |
| 332 | + .thenReturn(accessTokenDO); | |
| 333 | + | |
| 334 | + // 调用 | |
| 335 | + AuthLoginRespVO loginRespVO = authService.refreshToken(refreshToken); | |
| 336 | + // 断言 | |
| 337 | + assertPojoEquals(accessTokenDO, loginRespVO); | |
| 338 | + } | |
| 208 | 339 | |
| 209 | 340 | @Test |
| 210 | 341 | public void testLogout_success() { |
| ... | ... | @@ -221,6 +352,8 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { |
| 221 | 352 | verify(loginLogService).createLoginLog(argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGOUT_SELF.getType()) |
| 222 | 353 | && o.getResult().equals(LoginResultEnum.SUCCESS.getResult())) |
| 223 | 354 | ); |
| 355 | + // 调用,并校验 | |
| 356 | + | |
| 224 | 357 | } |
| 225 | 358 | |
| 226 | 359 | @Test | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java
| ... | ... | @@ -19,6 +19,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 19 | 19 | import org.springframework.context.annotation.Import; |
| 20 | 20 | |
| 21 | 21 | import javax.annotation.Resource; |
| 22 | +import java.util.Arrays; | |
| 22 | 23 | import java.util.List; |
| 23 | 24 | import java.util.Map; |
| 24 | 25 | import java.util.function.Consumer; |
| ... | ... | @@ -28,8 +29,8 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq |
| 28 | 29 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 29 | 30 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 30 | 31 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 32 | +import static java.util.Collections.singletonList; | |
| 31 | 33 | import static org.junit.jupiter.api.Assertions.*; |
| 32 | -import static org.mockito.Mockito.times; | |
| 33 | 34 | import static org.mockito.Mockito.verify; |
| 34 | 35 | |
| 35 | 36 | /** |
| ... | ... | @@ -38,7 +39,7 @@ import static org.mockito.Mockito.verify; |
| 38 | 39 | * @author niudehua |
| 39 | 40 | */ |
| 40 | 41 | @Import(DeptServiceImpl.class) |
| 41 | -public class DeptServiceTest extends BaseDbUnitTest { | |
| 42 | +public class DeptServiceImplTest extends BaseDbUnitTest { | |
| 42 | 43 | |
| 43 | 44 | @Resource |
| 44 | 45 | private DeptServiceImpl deptService; |
| ... | ... | @@ -76,7 +77,7 @@ public class DeptServiceTest extends BaseDbUnitTest { |
| 76 | 77 | } |
| 77 | 78 | |
| 78 | 79 | @Test |
| 79 | - void testListDepts() { | |
| 80 | + public void testListDepts() { | |
| 80 | 81 | // mock 数据 |
| 81 | 82 | DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到 |
| 82 | 83 | o.setName("开发部"); |
| ... | ... | @@ -91,21 +92,22 @@ public class DeptServiceTest extends BaseDbUnitTest { |
| 91 | 92 | DeptListReqVO reqVO = new DeptListReqVO(); |
| 92 | 93 | reqVO.setName("开"); |
| 93 | 94 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 95 | + | |
| 94 | 96 | // 调用 |
| 95 | - List<DeptDO> sysDeptDOS = deptService.getSimpleDepts(reqVO); | |
| 97 | + List<DeptDO> sysDeptDOS = deptService.getDeptList(reqVO); | |
| 96 | 98 | // 断言 |
| 97 | 99 | assertEquals(1, sysDeptDOS.size()); |
| 98 | 100 | assertPojoEquals(dept, sysDeptDOS.get(0)); |
| 99 | 101 | } |
| 100 | 102 | |
| 101 | 103 | @Test |
| 102 | - void testCreateDept_success() { | |
| 104 | + public void testCreateDept_success() { | |
| 103 | 105 | // 准备参数 |
| 104 | - DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, | |
| 105 | - o -> { | |
| 106 | - o.setParentId(DeptIdEnum.ROOT.getId()); | |
| 107 | - o.setStatus(randomCommonStatus()); | |
| 108 | - }); | |
| 106 | + DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> { | |
| 107 | + o.setParentId(DeptIdEnum.ROOT.getId()); | |
| 108 | + o.setStatus(randomCommonStatus()); | |
| 109 | + }); | |
| 110 | + | |
| 109 | 111 | // 调用 |
| 110 | 112 | Long deptId = deptService.createDept(reqVO); |
| 111 | 113 | // 断言 |
| ... | ... | @@ -114,11 +116,11 @@ public class DeptServiceTest extends BaseDbUnitTest { |
| 114 | 116 | DeptDO deptDO = deptMapper.selectById(deptId); |
| 115 | 117 | assertPojoEquals(reqVO, deptDO); |
| 116 | 118 | // 校验调用 |
| 117 | - verify(deptProducer, times(1)).sendDeptRefreshMessage(); | |
| 119 | + verify(deptProducer).sendDeptRefreshMessage(); | |
| 118 | 120 | } |
| 119 | 121 | |
| 120 | 122 | @Test |
| 121 | - void testUpdateDept_success() { | |
| 123 | + public void testUpdateDept_success() { | |
| 122 | 124 | // mock 数据 |
| 123 | 125 | DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); |
| 124 | 126 | deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 |
| ... | ... | @@ -129,28 +131,34 @@ public class DeptServiceTest extends BaseDbUnitTest { |
| 129 | 131 | o.setId(dbDeptDO.getId()); |
| 130 | 132 | o.setStatus(randomCommonStatus()); |
| 131 | 133 | }); |
| 134 | + | |
| 132 | 135 | // 调用 |
| 133 | 136 | deptService.updateDept(reqVO); |
| 134 | 137 | // 校验是否更新正确 |
| 135 | 138 | DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的 |
| 136 | 139 | assertPojoEquals(reqVO, deptDO); |
| 140 | + // 校验调用 | |
| 141 | + verify(deptProducer).sendDeptRefreshMessage(); | |
| 137 | 142 | } |
| 138 | 143 | |
| 139 | 144 | @Test |
| 140 | - void testDeleteDept_success() { | |
| 145 | + public void testDeleteDept_success() { | |
| 141 | 146 | // mock 数据 |
| 142 | 147 | DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); |
| 143 | 148 | deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 |
| 144 | 149 | // 准备参数 |
| 145 | 150 | Long id = dbDeptDO.getId(); |
| 151 | + | |
| 146 | 152 | // 调用 |
| 147 | 153 | deptService.deleteDept(id); |
| 148 | 154 | // 校验数据不存在了 |
| 149 | 155 | assertNull(deptMapper.selectById(id)); |
| 156 | + // 校验调用 | |
| 157 | + verify(deptProducer).sendDeptRefreshMessage(); | |
| 150 | 158 | } |
| 151 | 159 | |
| 152 | 160 | @Test |
| 153 | - void testCheckDept_nameDuplicateForUpdate() { | |
| 161 | + public void testValidateDept_nameDuplicateForUpdate() { | |
| 154 | 162 | // mock 数据 |
| 155 | 163 | DeptDO deptDO = randomDeptDO(); |
| 156 | 164 | // 设置根节点部门 |
| ... | ... | @@ -162,37 +170,40 @@ public class DeptServiceTest extends BaseDbUnitTest { |
| 162 | 170 | nameDeptDO.setParentId(DeptIdEnum.ROOT.getId()); |
| 163 | 171 | deptMapper.insert(nameDeptDO); |
| 164 | 172 | // 准备参数 |
| 165 | - DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, | |
| 166 | - o -> { | |
| 167 | - // 设置根节点部门 | |
| 168 | - o.setParentId(DeptIdEnum.ROOT.getId()); | |
| 169 | - // 设置更新的 ID | |
| 170 | - o.setId(deptDO.getId()); | |
| 171 | - // 模拟 name 重复 | |
| 172 | - o.setName(nameDeptDO.getName()); | |
| 173 | - }); | |
| 173 | + DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | |
| 174 | + // 设置根节点部门 | |
| 175 | + o.setParentId(DeptIdEnum.ROOT.getId()); | |
| 176 | + // 设置更新的 ID | |
| 177 | + o.setId(deptDO.getId()); | |
| 178 | + // 模拟 name 重复 | |
| 179 | + o.setName(nameDeptDO.getName()); | |
| 180 | + }); | |
| 181 | + | |
| 174 | 182 | // 调用, 并断言异常 |
| 175 | 183 | assertServiceException(() -> deptService.updateDept(reqVO), DEPT_NAME_DUPLICATE); |
| 176 | 184 | } |
| 177 | 185 | |
| 178 | 186 | @Test |
| 179 | - void testCheckDept_parentNotExitsForCreate() { | |
| 187 | + public void testValidateDept_parentNotExitsForCreate() { | |
| 188 | + // 准备参数 | |
| 180 | 189 | DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, |
| 181 | 190 | o -> o.setStatus(randomCommonStatus())); |
| 191 | + | |
| 182 | 192 | // 调用,并断言异常 |
| 183 | 193 | assertServiceException(() -> deptService.createDept(reqVO), DEPT_PARENT_NOT_EXITS); |
| 184 | 194 | } |
| 185 | 195 | |
| 186 | 196 | @Test |
| 187 | - void testCheckDept_notFoundForDelete() { | |
| 197 | + public void testValidateDept_notFoundForDelete() { | |
| 188 | 198 | // 准备参数 |
| 189 | 199 | Long id = randomLongId(); |
| 200 | + | |
| 190 | 201 | // 调用, 并断言异常 |
| 191 | 202 | assertServiceException(() -> deptService.deleteDept(id), DEPT_NOT_FOUND); |
| 192 | 203 | } |
| 193 | 204 | |
| 194 | 205 | @Test |
| 195 | - void testCheckDept_exitsChildrenForDelete() { | |
| 206 | + public void testValidateDept_exitsChildrenForDelete() { | |
| 196 | 207 | // mock 数据 |
| 197 | 208 | DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); |
| 198 | 209 | deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据 |
| ... | ... | @@ -208,39 +219,39 @@ public class DeptServiceTest extends BaseDbUnitTest { |
| 208 | 219 | } |
| 209 | 220 | |
| 210 | 221 | @Test |
| 211 | - void testCheckDept_parentErrorForUpdate() { | |
| 222 | + public void testValidateDept_parentErrorForUpdate() { | |
| 212 | 223 | // mock 数据 |
| 213 | 224 | DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); |
| 214 | 225 | deptMapper.insert(dbDeptDO); |
| 215 | 226 | // 准备参数 |
| 216 | - DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, | |
| 217 | - o -> { | |
| 218 | - // 设置自己为父部门 | |
| 219 | - o.setParentId(dbDeptDO.getId()); | |
| 220 | - // 设置更新的 ID | |
| 221 | - o.setId(dbDeptDO.getId()); | |
| 222 | - }); | |
| 227 | + DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | |
| 228 | + // 设置自己为父部门 | |
| 229 | + o.setParentId(dbDeptDO.getId()); | |
| 230 | + // 设置更新的 ID | |
| 231 | + o.setId(dbDeptDO.getId()); | |
| 232 | + }); | |
| 233 | + | |
| 223 | 234 | // 调用, 并断言异常 |
| 224 | 235 | assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_ERROR); |
| 225 | 236 | } |
| 226 | 237 | |
| 227 | 238 | @Test |
| 228 | - void testCheckDept_notEnableForCreate() { | |
| 239 | + public void testValidateDept_notEnableForCreate() { | |
| 229 | 240 | // mock 数据 |
| 230 | 241 | DeptDO deptDO = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); |
| 231 | 242 | deptMapper.insert(deptDO); |
| 232 | 243 | // 准备参数 |
| 233 | - DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, | |
| 234 | - o -> { | |
| 235 | - // 设置未启用的部门为副部门 | |
| 236 | - o.setParentId(deptDO.getId()); | |
| 237 | - }); | |
| 244 | + DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> { | |
| 245 | + // 设置未启用的部门为父部门 | |
| 246 | + o.setParentId(deptDO.getId()); | |
| 247 | + }); | |
| 248 | + | |
| 238 | 249 | // 调用, 并断言异常 |
| 239 | 250 | assertServiceException(() -> deptService.createDept(reqVO), DEPT_NOT_ENABLE); |
| 240 | 251 | } |
| 241 | 252 | |
| 242 | 253 | @Test |
| 243 | - void testCheckDept_parentIsChildForUpdate() { | |
| 254 | + public void testCheckDept_parentIsChildForUpdate() { | |
| 244 | 255 | // mock 数据 |
| 245 | 256 | DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 246 | 257 | deptMapper.insert(parentDept); |
| ... | ... | @@ -251,18 +262,84 @@ public class DeptServiceTest extends BaseDbUnitTest { |
| 251 | 262 | deptMapper.insert(childDept); |
| 252 | 263 | // 初始化本地缓存 |
| 253 | 264 | deptService.initLocalCache(); |
| 265 | + | |
| 254 | 266 | // 准备参数 |
| 255 | - DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, | |
| 256 | - o -> { | |
| 257 | - // 设置自己的子部门为父部门 | |
| 258 | - o.setParentId(childDept.getId()); | |
| 259 | - // 设置更新的 ID | |
| 260 | - o.setId(parentDept.getId()); | |
| 261 | - }); | |
| 267 | + DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | |
| 268 | + // 设置自己的子部门为父部门 | |
| 269 | + o.setParentId(childDept.getId()); | |
| 270 | + // 设置更新的 ID | |
| 271 | + o.setId(parentDept.getId()); | |
| 272 | + }); | |
| 273 | + | |
| 262 | 274 | // 调用, 并断言异常 |
| 263 | 275 | assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_IS_CHILD); |
| 264 | 276 | } |
| 265 | 277 | |
| 278 | + @Test | |
| 279 | + public void testGetDeptList() { | |
| 280 | + // mock 数据 | |
| 281 | + DeptDO deptDO01 = randomDeptDO(); | |
| 282 | + deptMapper.insert(deptDO01); | |
| 283 | + DeptDO deptDO02 = randomDeptDO(); | |
| 284 | + deptMapper.insert(deptDO02); | |
| 285 | + // 准备参数 | |
| 286 | + List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId()); | |
| 287 | + | |
| 288 | + // 调用 | |
| 289 | + List<DeptDO> deptDOList = deptService.getDeptList(ids); | |
| 290 | + // 断言 | |
| 291 | + assertEquals(2, deptDOList.size()); | |
| 292 | + assertEquals(deptDO01, deptDOList.get(0)); | |
| 293 | + assertEquals(deptDO02, deptDOList.get(1)); | |
| 294 | + } | |
| 295 | + | |
| 296 | + @Test | |
| 297 | + public void testGetDept() { | |
| 298 | + // mock 数据 | |
| 299 | + DeptDO deptDO = randomDeptDO(); | |
| 300 | + deptMapper.insert(deptDO); | |
| 301 | + // 准备参数 | |
| 302 | + Long id = deptDO.getId(); | |
| 303 | + | |
| 304 | + // 调用 | |
| 305 | + DeptDO dbDept = deptService.getDept(id); | |
| 306 | + // 断言 | |
| 307 | + assertEquals(deptDO, dbDept); | |
| 308 | + } | |
| 309 | + | |
| 310 | + @Test | |
| 311 | + public void testValidateDeptList_success() { | |
| 312 | + // mock 数据 | |
| 313 | + DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 314 | + deptMapper.insert(deptDO); | |
| 315 | + // 准备参数 | |
| 316 | + List<Long> ids = singletonList(deptDO.getId()); | |
| 317 | + | |
| 318 | + // 调用,无需断言 | |
| 319 | + deptService.validateDeptList(ids); | |
| 320 | + } | |
| 321 | + | |
| 322 | + @Test | |
| 323 | + public void testValidateDeptList_notFound() { | |
| 324 | + // 准备参数 | |
| 325 | + List<Long> ids = singletonList(randomLongId()); | |
| 326 | + | |
| 327 | + // 调用, 并断言异常 | |
| 328 | + assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_FOUND); | |
| 329 | + } | |
| 330 | + | |
| 331 | + @Test | |
| 332 | + public void testValidateDeptList_notEnable() { | |
| 333 | + // mock 数据 | |
| 334 | + DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.DISABLE.getStatus()); | |
| 335 | + deptMapper.insert(deptDO); | |
| 336 | + // 准备参数 | |
| 337 | + List<Long> ids = singletonList(deptDO.getId()); | |
| 338 | + | |
| 339 | + // 调用, 并断言异常 | |
| 340 | + assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE, deptDO.getName()); | |
| 341 | + } | |
| 342 | + | |
| 266 | 343 | @SafeVarargs |
| 267 | 344 | private static DeptDO randomDeptDO(Consumer<DeptDO>... consumers) { |
| 268 | 345 | Consumer<DeptDO> consumer = (o) -> { | ... | ... |
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/dict/DictDataServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dict/DictDataServiceImplTest.java
| ... | ... | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.system.service.dict; |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 6 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 7 | 6 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 8 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataCreateReqVO; |
| 9 | 8 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExportReqVO; |
| ... | ... | @@ -20,16 +19,18 @@ import javax.annotation.Resource; |
| 20 | 19 | import java.util.List; |
| 21 | 20 | import java.util.function.Consumer; |
| 22 | 21 | |
| 22 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 23 | 23 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 24 | 24 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 25 | 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 | import static org.mockito.ArgumentMatchers.eq; |
| 29 | 30 | import static org.mockito.Mockito.when; |
| 30 | 31 | |
| 31 | 32 | @Import(DictDataServiceImpl.class) |
| 32 | -public class DictDataServiceTest extends BaseDbUnitTest { | |
| 33 | +public class DictDataServiceImplTest extends BaseDbUnitTest { | |
| 33 | 34 | |
| 34 | 35 | @Resource |
| 35 | 36 | private DictDataServiceImpl dictDataService; |
| ... | ... | @@ -40,6 +41,23 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 40 | 41 | private DictTypeService dictTypeService; |
| 41 | 42 | |
| 42 | 43 | @Test |
| 44 | + public void testGetDictDataList() { | |
| 45 | + // mock 数据 | |
| 46 | + DictDataDO dictDataDO01 = randomDictDataDO().setDictType("yunai").setSort(2); | |
| 47 | + dictDataMapper.insert(dictDataDO01); | |
| 48 | + DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setSort(1); | |
| 49 | + dictDataMapper.insert(dictDataDO02); | |
| 50 | + // 准备参数 | |
| 51 | + | |
| 52 | + // 调用 | |
| 53 | + List<DictDataDO> dictDataDOList = dictDataService.getDictDataList(); | |
| 54 | + // 断言 | |
| 55 | + assertEquals(2, dictDataDOList.size()); | |
| 56 | + assertPojoEquals(dictDataDO02, dictDataDOList.get(0)); | |
| 57 | + assertPojoEquals(dictDataDO01, dictDataDOList.get(1)); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Test | |
| 43 | 61 | public void testGetDictDataPage() { |
| 44 | 62 | // mock 数据 |
| 45 | 63 | DictDataDO dbDictData = randomPojo(DictDataDO.class, o -> { // 等会查询到 |
| ... | ... | @@ -49,11 +67,11 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 49 | 67 | }); |
| 50 | 68 | dictDataMapper.insert(dbDictData); |
| 51 | 69 | // 测试 label 不匹配 |
| 52 | - dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("艿"))); | |
| 70 | + dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setLabel("艿"))); | |
| 53 | 71 | // 测试 dictType 不匹配 |
| 54 | - dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai"))); | |
| 72 | + dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setDictType("nai"))); | |
| 55 | 73 | // 测试 status 不匹配 |
| 56 | - dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 74 | + dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 57 | 75 | // 准备参数 |
| 58 | 76 | DictDataPageReqVO reqVO = new DictDataPageReqVO(); |
| 59 | 77 | reqVO.setLabel("芋"); |
| ... | ... | @@ -69,7 +87,7 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 69 | 87 | } |
| 70 | 88 | |
| 71 | 89 | @Test |
| 72 | - public void testGetDictDataList() { | |
| 90 | + public void testGetDictDataList_export() { | |
| 73 | 91 | // mock 数据 |
| 74 | 92 | DictDataDO dbDictData = randomPojo(DictDataDO.class, o -> { // 等会查询到 |
| 75 | 93 | o.setLabel("芋艿"); |
| ... | ... | @@ -78,11 +96,11 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 78 | 96 | }); |
| 79 | 97 | dictDataMapper.insert(dbDictData); |
| 80 | 98 | // 测试 label 不匹配 |
| 81 | - dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("艿"))); | |
| 99 | + dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setLabel("艿"))); | |
| 82 | 100 | // 测试 dictType 不匹配 |
| 83 | - dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai"))); | |
| 101 | + dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setDictType("nai"))); | |
| 84 | 102 | // 测试 status 不匹配 |
| 85 | - dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 103 | + dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 86 | 104 | // 准备参数 |
| 87 | 105 | DictDataExportReqVO reqVO = new DictDataExportReqVO(); |
| 88 | 106 | reqVO.setLabel("芋"); |
| ... | ... | @@ -90,13 +108,27 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 90 | 108 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 91 | 109 | |
| 92 | 110 | // 调用 |
| 93 | - List<DictDataDO> list = dictDataService.getDictDatas(reqVO); | |
| 111 | + List<DictDataDO> list = dictDataService.getDictDataList(reqVO); | |
| 94 | 112 | // 断言 |
| 95 | 113 | assertEquals(1, list.size()); |
| 96 | 114 | assertPojoEquals(dbDictData, list.get(0)); |
| 97 | 115 | } |
| 98 | 116 | |
| 99 | 117 | @Test |
| 118 | + public void testGetDictData() { | |
| 119 | + // mock 数据 | |
| 120 | + DictDataDO dbDictData = randomDictDataDO(); | |
| 121 | + dictDataMapper.insert(dbDictData); | |
| 122 | + // 准备参数 | |
| 123 | + Long id = dbDictData.getId(); | |
| 124 | + | |
| 125 | + // 调用 | |
| 126 | + DictDataDO dictData = dictDataService.getDictData(id); | |
| 127 | + // 断言 | |
| 128 | + assertPojoEquals(dbDictData, dictData); | |
| 129 | + } | |
| 130 | + | |
| 131 | + @Test | |
| 100 | 132 | public void testCreateDictData_success() { |
| 101 | 133 | // 准备参数 |
| 102 | 134 | DictDataCreateReqVO reqVO = randomPojo(DictDataCreateReqVO.class, |
| ... | ... | @@ -148,54 +180,54 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 148 | 180 | } |
| 149 | 181 | |
| 150 | 182 | @Test |
| 151 | - public void testCheckDictDataExists_success() { | |
| 183 | + public void testValidateDictDataExists_success() { | |
| 152 | 184 | // mock 数据 |
| 153 | 185 | DictDataDO dbDictData = randomDictDataDO(); |
| 154 | 186 | dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据 |
| 155 | 187 | |
| 156 | 188 | // 调用成功 |
| 157 | - dictDataService.checkDictDataExists(dbDictData.getId()); | |
| 189 | + dictDataService.validateDictDataExists(dbDictData.getId()); | |
| 158 | 190 | } |
| 159 | 191 | |
| 160 | 192 | @Test |
| 161 | - public void testCheckDictDataExists_notExists() { | |
| 162 | - assertServiceException(() -> dictDataService.checkDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS); | |
| 193 | + public void testValidateDictDataExists_notExists() { | |
| 194 | + assertServiceException(() -> dictDataService.validateDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS); | |
| 163 | 195 | } |
| 164 | 196 | |
| 165 | 197 | @Test |
| 166 | - public void testCheckDictTypeValid_success() { | |
| 198 | + public void testValidateDictTypeExists_success() { | |
| 167 | 199 | // mock 方法,数据类型被禁用 |
| 168 | 200 | String type = randomString(); |
| 169 | 201 | when(dictTypeService.getDictType(eq(type))).thenReturn(randomDictTypeDO(type)); |
| 170 | 202 | |
| 171 | 203 | // 调用, 成功 |
| 172 | - dictDataService.checkDictTypeValid(type); | |
| 204 | + dictDataService.validateDictTypeExists(type); | |
| 173 | 205 | } |
| 174 | 206 | |
| 175 | 207 | @Test |
| 176 | - public void testCheckDictTypeValid_notExists() { | |
| 177 | - assertServiceException(() -> dictDataService.checkDictTypeValid(randomString()), DICT_TYPE_NOT_EXISTS); | |
| 208 | + public void testValidateDictTypeExists_notExists() { | |
| 209 | + assertServiceException(() -> dictDataService.validateDictTypeExists(randomString()), DICT_TYPE_NOT_EXISTS); | |
| 178 | 210 | } |
| 179 | 211 | |
| 180 | 212 | @Test |
| 181 | - public void testCheckDictTypeValid_notEnable() { | |
| 213 | + public void testValidateDictTypeExists_notEnable() { | |
| 182 | 214 | // mock 方法,数据类型被禁用 |
| 183 | 215 | String dictType = randomString(); |
| 184 | 216 | when(dictTypeService.getDictType(eq(dictType))).thenReturn( |
| 185 | 217 | randomPojo(DictTypeDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
| 186 | 218 | |
| 187 | 219 | // 调用, 并断言异常 |
| 188 | - assertServiceException(() -> dictDataService.checkDictTypeValid(dictType), DICT_TYPE_NOT_ENABLE); | |
| 220 | + assertServiceException(() -> dictDataService.validateDictTypeExists(dictType), DICT_TYPE_NOT_ENABLE); | |
| 189 | 221 | } |
| 190 | 222 | |
| 191 | 223 | @Test |
| 192 | - public void testCheckDictDataValueUnique_success() { | |
| 224 | + public void testValidateDictDataValueUnique_success() { | |
| 193 | 225 | // 调用,成功 |
| 194 | - dictDataService.checkDictDataValueUnique(randomLongId(), randomString(), randomString()); | |
| 226 | + dictDataService.validateDictDataValueUnique(randomLongId(), randomString(), randomString()); | |
| 195 | 227 | } |
| 196 | 228 | |
| 197 | 229 | @Test |
| 198 | - public void testCheckDictDataValueUnique_valueDuplicateForCreate() { | |
| 230 | + public void testValidateDictDataValueUnique_valueDuplicateForCreate() { | |
| 199 | 231 | // 准备参数 |
| 200 | 232 | String dictType = randomString(); |
| 201 | 233 | String value = randomString(); |
| ... | ... | @@ -206,12 +238,12 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 206 | 238 | })); |
| 207 | 239 | |
| 208 | 240 | // 调用,校验异常 |
| 209 | - assertServiceException(() -> dictDataService.checkDictDataValueUnique(null, dictType, value), | |
| 241 | + assertServiceException(() -> dictDataService.validateDictDataValueUnique(null, dictType, value), | |
| 210 | 242 | DICT_DATA_VALUE_DUPLICATE); |
| 211 | 243 | } |
| 212 | 244 | |
| 213 | 245 | @Test |
| 214 | - public void testCheckDictDataValueUnique_valueDuplicateForUpdate() { | |
| 246 | + public void testValidateDictDataValueUnique_valueDuplicateForUpdate() { | |
| 215 | 247 | // 准备参数 |
| 216 | 248 | Long id = randomLongId(); |
| 217 | 249 | String dictType = randomString(); |
| ... | ... | @@ -223,10 +255,96 @@ public class DictDataServiceTest extends BaseDbUnitTest { |
| 223 | 255 | })); |
| 224 | 256 | |
| 225 | 257 | // 调用,校验异常 |
| 226 | - assertServiceException(() -> dictDataService.checkDictDataValueUnique(id, dictType, value), | |
| 258 | + assertServiceException(() -> dictDataService.validateDictDataValueUnique(id, dictType, value), | |
| 227 | 259 | DICT_DATA_VALUE_DUPLICATE); |
| 228 | 260 | } |
| 229 | 261 | |
| 262 | + @Test | |
| 263 | + public void testCountByDictType() { | |
| 264 | + // mock 数据 | |
| 265 | + dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai"))); | |
| 266 | + dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("tudou"))); | |
| 267 | + dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai"))); | |
| 268 | + // 准备参数 | |
| 269 | + String dictType = "yunai"; | |
| 270 | + | |
| 271 | + // 调用 | |
| 272 | + long count = dictDataService.countByDictType(dictType); | |
| 273 | + // 校验 | |
| 274 | + assertEquals(2L, count); | |
| 275 | + } | |
| 276 | + | |
| 277 | + @Test | |
| 278 | + public void testValidateDictDataList_success() { | |
| 279 | + // mock 数据 | |
| 280 | + DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 281 | + dictDataMapper.insert(dictDataDO); | |
| 282 | + // 准备参数 | |
| 283 | + String dictType = dictDataDO.getDictType(); | |
| 284 | + List<String> values = singletonList(dictDataDO.getValue()); | |
| 285 | + | |
| 286 | + // 调用,无需断言 | |
| 287 | + dictDataService.validateDictDataList(dictType, values); | |
| 288 | + } | |
| 289 | + | |
| 290 | + @Test | |
| 291 | + public void testValidateDictDataList_notFound() { | |
| 292 | + // 准备参数 | |
| 293 | + String dictType = randomString(); | |
| 294 | + List<String> values = singletonList(randomString()); | |
| 295 | + | |
| 296 | + // 调用, 并断言异常 | |
| 297 | + assertServiceException(() -> dictDataService.validateDictDataList(dictType, values), DICT_DATA_NOT_EXISTS); | |
| 298 | + } | |
| 299 | + | |
| 300 | + @Test | |
| 301 | + public void testValidateDictDataList_notEnable() { | |
| 302 | + // mock 数据 | |
| 303 | + DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.DISABLE.getStatus()); | |
| 304 | + dictDataMapper.insert(dictDataDO); | |
| 305 | + // 准备参数 | |
| 306 | + String dictType = dictDataDO.getDictType(); | |
| 307 | + List<String> values = singletonList(dictDataDO.getValue()); | |
| 308 | + | |
| 309 | + // 调用, 并断言异常 | |
| 310 | + assertServiceException(() -> dictDataService.validateDictDataList(dictType, values), | |
| 311 | + DICT_DATA_NOT_ENABLE, dictDataDO.getLabel()); | |
| 312 | + } | |
| 313 | + | |
| 314 | + @Test | |
| 315 | + public void testGetDictData_dictType() { | |
| 316 | + // mock 数据 | |
| 317 | + DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setValue("1"); | |
| 318 | + dictDataMapper.insert(dictDataDO); | |
| 319 | + DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setValue("2"); | |
| 320 | + dictDataMapper.insert(dictDataDO02); | |
| 321 | + // 准备参数 | |
| 322 | + String dictType = "yunai"; | |
| 323 | + String value = "1"; | |
| 324 | + | |
| 325 | + // 调用 | |
| 326 | + DictDataDO dbDictData = dictDataService.getDictData(dictType, value); | |
| 327 | + // 断言 | |
| 328 | + assertEquals(dictDataDO, dbDictData); | |
| 329 | + } | |
| 330 | + | |
| 331 | + @Test | |
| 332 | + public void testParseDictData() { | |
| 333 | + // mock 数据 | |
| 334 | + DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setLabel("1"); | |
| 335 | + dictDataMapper.insert(dictDataDO); | |
| 336 | + DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setLabel("2"); | |
| 337 | + dictDataMapper.insert(dictDataDO02); | |
| 338 | + // 准备参数 | |
| 339 | + String dictType = "yunai"; | |
| 340 | + String label = "1"; | |
| 341 | + | |
| 342 | + // 调用 | |
| 343 | + DictDataDO dbDictData = dictDataService.parseDictData(dictType, label); | |
| 344 | + // 断言 | |
| 345 | + assertEquals(dictDataDO, dbDictData); | |
| 346 | + } | |
| 347 | + | |
| 230 | 348 | // ========== 随机对象 ========== |
| 231 | 349 | |
| 232 | 350 | @SafeVarargs | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceImplTest.java
| ... | ... | @@ -2,36 +2,36 @@ package cn.iocoder.yudao.module.system.service.dict; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | |
| 6 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 5 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeCreateReqVO; |
| 6 | -import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO; | |
| 7 | 8 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; |
| 10 | +import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO; | |
| 9 | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO; |
| 10 | 12 | import cn.iocoder.yudao.module.system.dal.mysql.dict.DictTypeMapper; |
| 11 | -import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | |
| 12 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 13 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 14 | 13 | import org.junit.jupiter.api.Test; |
| 15 | 14 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 16 | 15 | import org.springframework.context.annotation.Import; |
| 17 | 16 | |
| 18 | 17 | import javax.annotation.Resource; |
| 19 | -import java.time.LocalDateTime; | |
| 20 | 18 | import java.util.List; |
| 21 | 19 | import java.util.function.Consumer; |
| 22 | 20 | |
| 23 | 21 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 24 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 22 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 23 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; | |
| 24 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 25 | 25 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 26 | 26 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 27 | -import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; | |
| 28 | 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 28 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 29 | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | 30 | import static org.mockito.ArgumentMatchers.eq; |
| 31 | 31 | import static org.mockito.Mockito.when; |
| 32 | 32 | |
| 33 | 33 | @Import(DictTypeServiceImpl.class) |
| 34 | -public class DictTypeServiceTest extends BaseDbUnitTest { | |
| 34 | +public class DictTypeServiceImplTest extends BaseDbUnitTest { | |
| 35 | 35 | |
| 36 | 36 | @Resource |
| 37 | 37 | private DictTypeServiceImpl dictTypeService; |
| ... | ... | @@ -52,19 +52,19 @@ public class DictTypeServiceTest extends BaseDbUnitTest { |
| 52 | 52 | }); |
| 53 | 53 | dictTypeMapper.insert(dbDictType); |
| 54 | 54 | // 测试 name 不匹配 |
| 55 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); | |
| 55 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); | |
| 56 | 56 | // 测试 type 不匹配 |
| 57 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); | |
| 57 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); | |
| 58 | 58 | // 测试 status 不匹配 |
| 59 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 59 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 60 | 60 | // 测试 createTime 不匹配 |
| 61 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); | |
| 61 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); | |
| 62 | 62 | // 准备参数 |
| 63 | 63 | DictTypePageReqVO reqVO = new DictTypePageReqVO(); |
| 64 | 64 | reqVO.setName("nai"); |
| 65 | 65 | reqVO.setType("艿"); |
| 66 | 66 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 67 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 10),buildTime(2021, 1, 20)})); | |
| 67 | + reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20)); | |
| 68 | 68 | |
| 69 | 69 | // 调用 |
| 70 | 70 | PageResult<DictTypeDO> pageResult = dictTypeService.getDictTypePage(reqVO); |
| ... | ... | @@ -75,7 +75,7 @@ public class DictTypeServiceTest extends BaseDbUnitTest { |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | @Test |
| 78 | - public void testGetDictTypeList() { | |
| 78 | + public void testGetDictTypeList_export() { | |
| 79 | 79 | // mock 数据 |
| 80 | 80 | DictTypeDO dbDictType = randomPojo(DictTypeDO.class, o -> { // 等会查询到 |
| 81 | 81 | o.setName("yunai"); |
| ... | ... | @@ -85,19 +85,19 @@ public class DictTypeServiceTest extends BaseDbUnitTest { |
| 85 | 85 | }); |
| 86 | 86 | dictTypeMapper.insert(dbDictType); |
| 87 | 87 | // 测试 name 不匹配 |
| 88 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); | |
| 88 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); | |
| 89 | 89 | // 测试 type 不匹配 |
| 90 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); | |
| 90 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); | |
| 91 | 91 | // 测试 status 不匹配 |
| 92 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 92 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 93 | 93 | // 测试 createTime 不匹配 |
| 94 | - dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); | |
| 94 | + dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); | |
| 95 | 95 | // 准备参数 |
| 96 | 96 | DictTypeExportReqVO reqVO = new DictTypeExportReqVO(); |
| 97 | 97 | reqVO.setName("nai"); |
| 98 | 98 | reqVO.setType("艿"); |
| 99 | 99 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 100 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 10),buildTime(2021, 1, 20)})); | |
| 100 | + reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20)); | |
| 101 | 101 | |
| 102 | 102 | // 调用 |
| 103 | 103 | List<DictTypeDO> list = dictTypeService.getDictTypeList(reqVO); |
| ... | ... | @@ -107,7 +107,22 @@ public class DictTypeServiceTest extends BaseDbUnitTest { |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | @Test |
| 110 | - public void testGetDictType() { | |
| 110 | + public void testGetDictType_id() { | |
| 111 | + // mock 数据 | |
| 112 | + DictTypeDO dbDictType = randomDictTypeDO(); | |
| 113 | + dictTypeMapper.insert(dbDictType); | |
| 114 | + // 准备参数 | |
| 115 | + Long id = dbDictType.getId(); | |
| 116 | + | |
| 117 | + // 调用 | |
| 118 | + DictTypeDO dictType = dictTypeService.getDictType(id); | |
| 119 | + // 断言 | |
| 120 | + assertNotNull(dictType); | |
| 121 | + assertPojoEquals(dbDictType, dictType); | |
| 122 | + } | |
| 123 | + | |
| 124 | + @Test | |
| 125 | + public void testGetDictType_type() { | |
| 111 | 126 | // mock 数据 |
| 112 | 127 | DictTypeDO dbDictType = randomDictTypeDO(); |
| 113 | 128 | dictTypeMapper.insert(dbDictType); |
| ... | ... | @@ -183,40 +198,57 @@ public class DictTypeServiceTest extends BaseDbUnitTest { |
| 183 | 198 | } |
| 184 | 199 | |
| 185 | 200 | @Test |
| 186 | - public void testCheckDictDataExists_success() { | |
| 201 | + public void testGetDictTypeList() { | |
| 202 | + // 准备参数 | |
| 203 | + DictTypeDO dictTypeDO01 = randomDictTypeDO(); | |
| 204 | + dictTypeMapper.insert(dictTypeDO01); | |
| 205 | + DictTypeDO dictTypeDO02 = randomDictTypeDO(); | |
| 206 | + dictTypeMapper.insert(dictTypeDO02); | |
| 207 | + // mock 方法 | |
| 208 | + | |
| 209 | + // 调用 | |
| 210 | + List<DictTypeDO> dictTypeDOList = dictTypeService.getDictTypeList(); | |
| 211 | + // 断言 | |
| 212 | + assertEquals(2, dictTypeDOList.size()); | |
| 213 | + assertPojoEquals(dictTypeDO01, dictTypeDOList.get(0)); | |
| 214 | + assertPojoEquals(dictTypeDO02, dictTypeDOList.get(1)); | |
| 215 | + } | |
| 216 | + | |
| 217 | + @Test | |
| 218 | + public void testValidateDictDataExists_success() { | |
| 187 | 219 | // mock 数据 |
| 188 | 220 | DictTypeDO dbDictType = randomDictTypeDO(); |
| 189 | 221 | dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 |
| 190 | 222 | |
| 191 | 223 | // 调用成功 |
| 192 | - dictTypeService.checkDictTypeExists(dbDictType.getId()); | |
| 224 | + dictTypeService.validateDictTypeExists(dbDictType.getId()); | |
| 193 | 225 | } |
| 194 | 226 | |
| 195 | 227 | @Test |
| 196 | - public void testCheckDictDataExists_notExists() { | |
| 197 | - assertServiceException(() -> dictTypeService.checkDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS); | |
| 228 | + public void testValidateDictDataExists_notExists() { | |
| 229 | + assertServiceException(() -> dictTypeService.validateDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS); | |
| 198 | 230 | } |
| 199 | 231 | |
| 200 | 232 | @Test |
| 201 | - public void testCheckDictTypeUnique_success() { | |
| 233 | + public void testValidateDictTypeUnique_success() { | |
| 202 | 234 | // 调用,成功 |
| 203 | - dictTypeService.checkDictTypeUnique(randomLongId(), randomString()); | |
| 235 | + dictTypeService.validateDictTypeUnique(randomLongId(), randomString()); | |
| 204 | 236 | } |
| 205 | 237 | |
| 206 | 238 | @Test |
| 207 | - public void testCheckDictTypeUnique_valueDuplicateForCreate() { | |
| 239 | + public void testValidateDictTypeUnique_valueDuplicateForCreate() { | |
| 208 | 240 | // 准备参数 |
| 209 | 241 | String type = randomString(); |
| 210 | 242 | // mock 数据 |
| 211 | 243 | dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); |
| 212 | 244 | |
| 213 | 245 | // 调用,校验异常 |
| 214 | - assertServiceException(() -> dictTypeService.checkDictTypeUnique(null, type), | |
| 246 | + assertServiceException(() -> dictTypeService.validateDictTypeUnique(null, type), | |
| 215 | 247 | DICT_TYPE_TYPE_DUPLICATE); |
| 216 | 248 | } |
| 217 | 249 | |
| 218 | 250 | @Test |
| 219 | - public void testCheckDictTypeUnique_valueDuplicateForUpdate() { | |
| 251 | + public void testValidateDictTypeUnique_valueDuplicateForUpdate() { | |
| 220 | 252 | // 准备参数 |
| 221 | 253 | Long id = randomLongId(); |
| 222 | 254 | String type = randomString(); |
| ... | ... | @@ -224,30 +256,30 @@ public class DictTypeServiceTest extends BaseDbUnitTest { |
| 224 | 256 | dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); |
| 225 | 257 | |
| 226 | 258 | // 调用,校验异常 |
| 227 | - assertServiceException(() -> dictTypeService.checkDictTypeUnique(id, type), | |
| 259 | + assertServiceException(() -> dictTypeService.validateDictTypeUnique(id, type), | |
| 228 | 260 | DICT_TYPE_TYPE_DUPLICATE); |
| 229 | 261 | } |
| 230 | 262 | |
| 231 | 263 | @Test |
| 232 | - public void testCheckDictTypNameUnique_success() { | |
| 264 | + public void testValidateDictTypNameUnique_success() { | |
| 233 | 265 | // 调用,成功 |
| 234 | - dictTypeService.checkDictTypeNameUnique(randomLongId(), randomString()); | |
| 266 | + dictTypeService.validateDictTypeNameUnique(randomLongId(), randomString()); | |
| 235 | 267 | } |
| 236 | 268 | |
| 237 | 269 | @Test |
| 238 | - public void testCheckDictTypeNameUnique_nameDuplicateForCreate() { | |
| 270 | + public void testValidateDictTypeNameUnique_nameDuplicateForCreate() { | |
| 239 | 271 | // 准备参数 |
| 240 | 272 | String name = randomString(); |
| 241 | 273 | // mock 数据 |
| 242 | 274 | dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); |
| 243 | 275 | |
| 244 | 276 | // 调用,校验异常 |
| 245 | - assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(null, name), | |
| 277 | + assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(null, name), | |
| 246 | 278 | DICT_TYPE_NAME_DUPLICATE); |
| 247 | 279 | } |
| 248 | 280 | |
| 249 | 281 | @Test |
| 250 | - public void testCheckDictTypeNameUnique_nameDuplicateForUpdate() { | |
| 282 | + public void testValidateDictTypeNameUnique_nameDuplicateForUpdate() { | |
| 251 | 283 | // 准备参数 |
| 252 | 284 | Long id = randomLongId(); |
| 253 | 285 | String name = randomString(); |
| ... | ... | @@ -255,7 +287,7 @@ public class DictTypeServiceTest extends BaseDbUnitTest { |
| 255 | 287 | dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); |
| 256 | 288 | |
| 257 | 289 | // 调用,校验异常 |
| 258 | - assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(id, name), | |
| 290 | + assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(id, name), | |
| 259 | 291 | DICT_TYPE_NAME_DUPLICATE); |
| 260 | 292 | } |
| 261 | 293 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java
| ... | ... | @@ -2,9 +2,9 @@ package cn.iocoder.yudao.module.system.service.errorcode; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 5 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 6 | 5 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 7 | 6 | import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; |
| 7 | +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | |
| 8 | 8 | import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO; |
| 9 | 9 | import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO; |
| 10 | 10 | import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; |
| ... | ... | @@ -22,7 +22,9 @@ import java.util.List; |
| 22 | 22 | import java.util.function.Consumer; |
| 23 | 23 | |
| 24 | 24 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 25 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 25 | 26 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 27 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 26 | 28 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 27 | 29 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 28 | 30 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| ... | ... | @@ -57,7 +59,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 57 | 59 | @Test |
| 58 | 60 | public void testUpdateErrorCode_success() { |
| 59 | 61 | // mock 数据 |
| 60 | - ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(); | |
| 62 | + ErrorCodeDO dbErrorCode = randomErrorCodeDO(); | |
| 61 | 63 | errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 |
| 62 | 64 | // 准备参数 |
| 63 | 65 | ErrorCodeUpdateReqVO reqVO = randomPojo(ErrorCodeUpdateReqVO.class, o -> { |
| ... | ... | @@ -75,7 +77,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 75 | 77 | @Test |
| 76 | 78 | public void testDeleteErrorCode_success() { |
| 77 | 79 | // mock 数据 |
| 78 | - ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(); | |
| 80 | + ErrorCodeDO dbErrorCode = randomErrorCodeDO(); | |
| 79 | 81 | errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 |
| 80 | 82 | // 准备参数 |
| 81 | 83 | Long id = dbErrorCode.getId(); |
| ... | ... | @@ -96,7 +98,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 96 | 98 | reqVO.setApplicationName("tu"); |
| 97 | 99 | reqVO.setCode(1); |
| 98 | 100 | reqVO.setMessage("ma"); |
| 99 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 11, 1),buildTime(2020, 11, 30)})); | |
| 101 | + reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30)); | |
| 100 | 102 | |
| 101 | 103 | // 调用 |
| 102 | 104 | PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(reqVO); |
| ... | ... | @@ -110,7 +112,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 110 | 112 | * 初始化 getErrorCodePage 方法的测试数据 |
| 111 | 113 | */ |
| 112 | 114 | private ErrorCodeDO initGetErrorCodePage() { |
| 113 | - ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> { // 等会查询到 | |
| 115 | + ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> { // 等会查询到 | |
| 114 | 116 | o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); |
| 115 | 117 | o.setApplicationName("tudou"); |
| 116 | 118 | o.setCode(1); |
| ... | ... | @@ -119,20 +121,20 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 119 | 121 | }); |
| 120 | 122 | errorCodeMapper.insert(dbErrorCode); |
| 121 | 123 | // 测试 type 不匹配 |
| 122 | - errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); | |
| 124 | + errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); | |
| 123 | 125 | // 测试 applicationName 不匹配 |
| 124 | - errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan"))); | |
| 126 | + errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan"))); | |
| 125 | 127 | // 测试 code 不匹配 |
| 126 | - errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCode(2))); | |
| 128 | + errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCode(2))); | |
| 127 | 129 | // 测试 message 不匹配 |
| 128 | - errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai"))); | |
| 130 | + errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai"))); | |
| 129 | 131 | // 测试 createTime 不匹配 |
| 130 | - errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12)))); | |
| 132 | + errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12)))); | |
| 131 | 133 | return dbErrorCode; |
| 132 | 134 | } |
| 133 | 135 | |
| 134 | 136 | @Test |
| 135 | - public void testGetErrorCodeList() { | |
| 137 | + public void testGetErrorCodeList_export() { | |
| 136 | 138 | // mock 数据 |
| 137 | 139 | ErrorCodeDO dbErrorCode = initGetErrorCodePage(); |
| 138 | 140 | // 准备参数 |
| ... | ... | @@ -141,7 +143,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 141 | 143 | reqVO.setApplicationName("tu"); |
| 142 | 144 | reqVO.setCode(1); |
| 143 | 145 | reqVO.setMessage("ma"); |
| 144 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 11, 1),buildTime(2020, 11, 30)})); | |
| 146 | + reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30)); | |
| 145 | 147 | |
| 146 | 148 | // 调用 |
| 147 | 149 | List<ErrorCodeDO> list = errorCodeService.getErrorCodeList(reqVO); |
| ... | ... | @@ -155,7 +157,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 155 | 157 | // 准备参数 |
| 156 | 158 | Integer code = randomInteger(); |
| 157 | 159 | // mock 数据 |
| 158 | - errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code))); | |
| 160 | + errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code))); | |
| 159 | 161 | |
| 160 | 162 | // 调用,校验异常 |
| 161 | 163 | assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null), |
| ... | ... | @@ -168,7 +170,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 168 | 170 | Long id = randomLongId(); |
| 169 | 171 | Integer code = randomInteger(); |
| 170 | 172 | // mock 数据 |
| 171 | - errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code))); | |
| 173 | + errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code))); | |
| 172 | 174 | |
| 173 | 175 | // 调用,校验异常 |
| 174 | 176 | assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id), |
| ... | ... | @@ -204,7 +206,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 204 | 206 | @Test |
| 205 | 207 | public void testAutoGenerateErrorCodes_021() { |
| 206 | 208 | // mock 数据 |
| 207 | - ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())); | |
| 209 | + ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())); | |
| 208 | 210 | errorCodeMapper.insert(dbErrorCode); |
| 209 | 211 | // 准备参数 |
| 210 | 212 | ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, |
| ... | ... | @@ -224,7 +226,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 224 | 226 | @Test |
| 225 | 227 | public void testAutoGenerateErrorCodes_022() { |
| 226 | 228 | // mock 数据 |
| 227 | - ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 229 | + ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 228 | 230 | errorCodeMapper.insert(dbErrorCode); |
| 229 | 231 | // 准备参数 |
| 230 | 232 | ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, |
| ... | ... | @@ -244,7 +246,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 244 | 246 | @Test |
| 245 | 247 | public void testAutoGenerateErrorCodes_023() { |
| 246 | 248 | // mock 数据 |
| 247 | - ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 249 | + ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 248 | 250 | errorCodeMapper.insert(dbErrorCode); |
| 249 | 251 | // 准备参数 |
| 250 | 252 | ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, |
| ... | ... | @@ -265,7 +267,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 265 | 267 | @Test |
| 266 | 268 | public void testAutoGenerateErrorCodes_024() { |
| 267 | 269 | // mock 数据 |
| 268 | - ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 270 | + ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 269 | 271 | errorCodeMapper.insert(dbErrorCode); |
| 270 | 272 | // 准备参数 |
| 271 | 273 | ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, |
| ... | ... | @@ -279,10 +281,44 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest { |
| 279 | 281 | assertPojoEquals(generateReqDTO, errorCode); |
| 280 | 282 | } |
| 281 | 283 | |
| 284 | + @Test | |
| 285 | + public void testGetErrorCode() { | |
| 286 | + // 准备参数 | |
| 287 | + ErrorCodeDO errorCodeDO = randomErrorCodeDO(); | |
| 288 | + errorCodeMapper.insert(errorCodeDO); | |
| 289 | + // mock 方法 | |
| 290 | + Long id = errorCodeDO.getId(); | |
| 291 | + | |
| 292 | + // 调用 | |
| 293 | + ErrorCodeDO dbErrorCode = errorCodeService.getErrorCode(id); | |
| 294 | + // 断言 | |
| 295 | + assertPojoEquals(errorCodeDO, dbErrorCode); | |
| 296 | + } | |
| 297 | + | |
| 298 | + @Test | |
| 299 | + public void testGetErrorCodeList() { | |
| 300 | + // 准备参数 | |
| 301 | + ErrorCodeDO errorCodeDO01 = randomErrorCodeDO( | |
| 302 | + o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 10))); | |
| 303 | + errorCodeMapper.insert(errorCodeDO01); | |
| 304 | + ErrorCodeDO errorCodeDO02 = randomErrorCodeDO( | |
| 305 | + o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 12))); | |
| 306 | + errorCodeMapper.insert(errorCodeDO02); | |
| 307 | + // mock 方法 | |
| 308 | + String applicationName = "yunai_server"; | |
| 309 | + LocalDateTime minUpdateTime = buildTime(2022, 1, 11); | |
| 310 | + | |
| 311 | + // 调用 | |
| 312 | + List<ErrorCodeRespDTO> errorCodeList = errorCodeService.getErrorCodeList(applicationName, minUpdateTime); | |
| 313 | + // 断言 | |
| 314 | + assertEquals(1, errorCodeList.size()); | |
| 315 | + assertPojoEquals(errorCodeDO02, errorCodeList.get(0)); | |
| 316 | + } | |
| 317 | + | |
| 282 | 318 | // ========== 随机对象 ========== |
| 283 | 319 | |
| 284 | 320 | @SafeVarargs |
| 285 | - private static ErrorCodeDO randomInfErrorCodeDO(Consumer<ErrorCodeDO>... consumers) { | |
| 321 | + private static ErrorCodeDO randomErrorCodeDO(Consumer<ErrorCodeDO>... consumers) { | |
| 286 | 322 | Consumer<ErrorCodeDO> consumer = (o) -> { |
| 287 | 323 | o.setType(randomEle(ErrorCodeTypeEnum.values()).getType()); // 保证 key 的范围 |
| 288 | 324 | }; | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/logger/LoginLogServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.logger; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.util.RandomUtil; | |
| 4 | -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | |
| 5 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | -import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | |
| 7 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 8 | 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 9 | -import cn.iocoder.yudao.framework.test.core.util.RandomUtils; | |
| 10 | 5 | import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO; |
| 11 | 6 | import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogExportReqVO; |
| 12 | 7 | import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogPageReqVO; |
| 13 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.logger.LoginLogDO; |
| 14 | 9 | import cn.iocoder.yudao.module.system.dal.mysql.logger.LoginLogMapper; |
| 15 | -import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; | |
| 16 | 10 | import org.junit.jupiter.api.Test; |
| 17 | 11 | import org.springframework.context.annotation.Import; |
| 18 | 12 | |
| 19 | 13 | import javax.annotation.Resource; |
| 20 | -import java.time.LocalDateTime; | |
| 21 | 14 | import java.util.List; |
| 22 | 15 | |
| 23 | -import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 16 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 24 | 17 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 18 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 25 | 19 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 26 | -import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.*; | |
| 20 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 21 | +import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.CAPTCHA_CODE_ERROR; | |
| 22 | +import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.SUCCESS; | |
| 27 | 23 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | 24 | |
| 29 | 25 | @Import(LoginLogServiceImpl.class) |
| ... | ... | @@ -37,42 +33,31 @@ public class LoginLogServiceImplTest extends BaseDbUnitTest { |
| 37 | 33 | |
| 38 | 34 | @Test |
| 39 | 35 | public void testGetLoginLogPage() { |
| 40 | - // 构造测试数据 | |
| 41 | - // 登录成功的 | |
| 42 | - LoginLogDO loginLogDO = RandomUtils.randomPojo(LoginLogDO.class, logDO -> { | |
| 43 | - logDO.setLogType(RandomUtil.randomEle(cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum.values()).getType()); | |
| 44 | - logDO.setTraceId(TracerUtils.getTraceId()); | |
| 45 | - logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()); | |
| 46 | - | |
| 47 | - logDO.setUserIp("192.168.199.16"); | |
| 48 | - logDO.setUsername("wangkai"); | |
| 49 | - logDO.setCreateTime(buildTime(2021, 3, 6)); | |
| 50 | - logDO.setResult(SUCCESS.getResult()); | |
| 36 | + // mock 数据 | |
| 37 | + LoginLogDO loginLogDO = randomPojo(LoginLogDO.class, o -> { | |
| 38 | + o.setUserIp("192.168.199.16"); | |
| 39 | + o.setUsername("wang"); | |
| 40 | + o.setResult(SUCCESS.getResult()); | |
| 41 | + o.setCreateTime(buildTime(2021, 3, 6)); | |
| 51 | 42 | }); |
| 52 | 43 | loginLogMapper.insert(loginLogDO); |
| 53 | - | |
| 54 | - // 下面几个都是不匹配的数据 | |
| 55 | - // 登录失败的 | |
| 56 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(CAPTCHA_CODE_ERROR.getResult()))); | |
| 57 | - // 不同ip段的 | |
| 58 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); | |
| 59 | - // 不同username | |
| 60 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai"))); | |
| 61 | - // 构造一个早期时间 2021-02-06 00:00:00 | |
| 62 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6)))); | |
| 63 | - | |
| 64 | - | |
| 44 | + // 测试 status 不匹配 | |
| 45 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setResult(CAPTCHA_CODE_ERROR.getResult()))); | |
| 46 | + // 测试 ip 不匹配 | |
| 47 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUserIp("192.168.128.18"))); | |
| 48 | + // 测试 username 不匹配 | |
| 49 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUsername("yunai"))); | |
| 50 | + // 测试 createTime 不匹配 | |
| 51 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setCreateTime(buildTime(2021, 2, 6)))); | |
| 65 | 52 | // 构造调用参数 |
| 66 | 53 | LoginLogPageReqVO reqVO = new LoginLogPageReqVO(); |
| 67 | - reqVO.setUsername("wangkai"); | |
| 54 | + reqVO.setUsername("wang"); | |
| 68 | 55 | reqVO.setUserIp("192.168.199"); |
| 69 | 56 | reqVO.setStatus(true); |
| 70 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 3, 5), | |
| 71 | - buildTime(2021, 3, 7)})); | |
| 57 | + reqVO.setCreateTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7)); | |
| 72 | 58 | |
| 73 | - // 调用service方法 | |
| 59 | + // 调用 | |
| 74 | 60 | PageResult<LoginLogDO> pageResult = loginLogService.getLoginLogPage(reqVO); |
| 75 | - | |
| 76 | 61 | // 断言,只查到了一条符合条件的 |
| 77 | 62 | assertEquals(1, pageResult.getTotal()); |
| 78 | 63 | assertEquals(1, pageResult.getList().size()); |
| ... | ... | @@ -81,62 +66,45 @@ public class LoginLogServiceImplTest extends BaseDbUnitTest { |
| 81 | 66 | |
| 82 | 67 | @Test |
| 83 | 68 | public void testGetLoginLogList() { |
| 84 | - // 构造测试数据 | |
| 85 | - | |
| 86 | - // 登录成功的 | |
| 87 | - LoginLogDO loginLogDO = RandomUtils.randomPojo(LoginLogDO.class, logDO -> { | |
| 88 | - logDO.setLogType(RandomUtil.randomEle(cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum.values()).getType()); | |
| 89 | - logDO.setTraceId(TracerUtils.getTraceId()); | |
| 90 | - logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()); | |
| 91 | - | |
| 92 | - logDO.setUserIp("192.168.111.16"); | |
| 93 | - logDO.setUsername("wangxiaokai"); | |
| 94 | - logDO.setCreateTime(buildTime(2021, 3, 6)); | |
| 95 | - logDO.setResult(SUCCESS.getResult()); | |
| 69 | + // mock 数据 | |
| 70 | + LoginLogDO loginLogDO = randomPojo(LoginLogDO.class, o -> { | |
| 71 | + o.setUserIp("192.168.199.16"); | |
| 72 | + o.setUsername("wang"); | |
| 73 | + o.setResult(SUCCESS.getResult()); | |
| 74 | + o.setCreateTime(buildTime(2021, 3, 6)); | |
| 96 | 75 | }); |
| 97 | 76 | loginLogMapper.insert(loginLogDO); |
| 98 | - | |
| 99 | - // 下面几个都是不匹配的数据 | |
| 100 | - // 登录失败的 | |
| 101 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(CAPTCHA_CODE_ERROR.getResult()))); | |
| 102 | - // 不同ip段的 | |
| 103 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); | |
| 104 | - // 不同username | |
| 105 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai"))); | |
| 106 | - // 构造一个早期时间 2021-02-06 00:00:00 | |
| 107 | - loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6)))); | |
| 108 | - | |
| 77 | + // 测试 status 不匹配 | |
| 78 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setResult(CAPTCHA_CODE_ERROR.getResult()))); | |
| 79 | + // 测试 ip 不匹配 | |
| 80 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUserIp("192.168.128.18"))); | |
| 81 | + // 测试 username 不匹配 | |
| 82 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUsername("yunai"))); | |
| 83 | + // 测试 createTime 不匹配 | |
| 84 | + loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setCreateTime(buildTime(2021, 2, 6)))); | |
| 109 | 85 | // 构造调用参数 |
| 110 | 86 | LoginLogExportReqVO reqVO = new LoginLogExportReqVO(); |
| 111 | - reqVO.setUsername("wangxiaokai"); | |
| 112 | - reqVO.setUserIp("192.168.111"); | |
| 87 | + reqVO.setUsername("wang"); | |
| 88 | + reqVO.setUserIp("192.168.199"); | |
| 113 | 89 | reqVO.setStatus(true); |
| 114 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 3, 5), | |
| 115 | - buildTime(2021, 3, 7)})); | |
| 90 | + reqVO.setCreateTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7)); | |
| 116 | 91 | |
| 117 | 92 | // 调用service方法 |
| 118 | - List<LoginLogDO> loginLogList = loginLogService.getLoginLogList(reqVO); | |
| 119 | - | |
| 93 | + List<LoginLogDO> list = loginLogService.getLoginLogList(reqVO); | |
| 120 | 94 | // 断言 |
| 121 | - assertEquals(1, loginLogList.size()); | |
| 122 | - assertPojoEquals(loginLogDO, loginLogList.get(0)); | |
| 95 | + assertEquals(1, list.size()); | |
| 96 | + assertPojoEquals(loginLogDO, list.get(0)); | |
| 123 | 97 | } |
| 124 | 98 | |
| 125 | 99 | @Test |
| 126 | 100 | public void testCreateLoginLog() { |
| 127 | - LoginLogCreateReqDTO reqDTO = RandomUtils.randomPojo(LoginLogCreateReqDTO.class, vo -> { | |
| 128 | - // 指定随机的范围,避免超出范围入库失败 | |
| 129 | - vo.setUserType(randomEle(UserTypeEnum.values()).getValue()); | |
| 130 | - vo.setLogType(randomEle(LoginLogTypeEnum.values()).getType()); | |
| 131 | - vo.setResult(randomEle(values()).getResult()); | |
| 132 | - vo.setTraceId(TracerUtils.getTraceId()); | |
| 133 | - }); | |
| 101 | + LoginLogCreateReqDTO reqDTO = randomPojo(LoginLogCreateReqDTO.class); | |
| 134 | 102 | |
| 135 | 103 | // 调用 |
| 136 | 104 | loginLogService.createLoginLog(reqDTO); |
| 137 | - // 断言,忽略基本字段 | |
| 138 | - LoginLogDO sysLoginLogDO = loginLogMapper.selectOne(null); | |
| 139 | - assertPojoEquals(reqDTO, sysLoginLogDO); | |
| 105 | + // 断言 | |
| 106 | + LoginLogDO loginLogDO = loginLogMapper.selectOne(null); | |
| 107 | + assertPojoEquals(reqDTO, loginLogDO); | |
| 140 | 108 | } |
| 141 | 109 | |
| 142 | 110 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/logger/OperateLogServiceImplTest.java
| ... | ... | @@ -5,8 +5,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; |
| 7 | 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | -import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | |
| 9 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 10 | 8 | import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum; |
| 11 | 9 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 12 | 10 | import cn.iocoder.yudao.framework.test.core.util.RandomUtils; |
| ... | ... | @@ -16,20 +14,20 @@ import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.Oper |
| 16 | 14 | import cn.iocoder.yudao.module.system.dal.dataobject.logger.OperateLogDO; |
| 17 | 15 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 18 | 16 | import cn.iocoder.yudao.module.system.dal.mysql.logger.OperateLogMapper; |
| 19 | -import cn.iocoder.yudao.module.system.enums.common.SexEnum; | |
| 20 | 17 | import cn.iocoder.yudao.module.system.service.user.AdminUserService; |
| 21 | 18 | import org.junit.jupiter.api.Test; |
| 22 | 19 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 23 | 20 | import org.springframework.context.annotation.Import; |
| 24 | 21 | |
| 25 | 22 | import javax.annotation.Resource; |
| 26 | -import java.time.LocalDateTime; | |
| 27 | 23 | import java.util.Collections; |
| 28 | 24 | import java.util.List; |
| 29 | 25 | |
| 30 | 26 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 31 | 27 | import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST; |
| 28 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 32 | 29 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 30 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 33 | 31 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 34 | 32 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 35 | 33 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| ... | ... | @@ -49,34 +47,28 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest { |
| 49 | 47 | |
| 50 | 48 | @Test |
| 51 | 49 | public void testCreateOperateLogAsync() { |
| 52 | - String traceId = TracerUtils.getTraceId(); | |
| 53 | - OperateLogCreateReqDTO reqVO = RandomUtils.randomPojo(OperateLogCreateReqDTO.class, o -> { | |
| 54 | - o.setTraceId(traceId); | |
| 55 | - o.setUserId(randomLongId()); | |
| 56 | - o.setUserType(randomEle(UserTypeEnum.values()).getValue()); | |
| 57 | - o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build()); | |
| 58 | - }); | |
| 50 | + OperateLogCreateReqDTO reqVO = RandomUtils.randomPojo(OperateLogCreateReqDTO.class, | |
| 51 | + o -> o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build())); | |
| 59 | 52 | |
| 60 | - // 执行service方法 | |
| 53 | + // 调研 | |
| 61 | 54 | operateLogServiceImpl.createOperateLog(reqVO); |
| 62 | - // 断言插入是否正确 | |
| 63 | - OperateLogDO sysOperateLogDO = operateLogMapper.selectOne("trace_id", traceId); | |
| 64 | - assertPojoEquals(reqVO, sysOperateLogDO); | |
| 55 | + // 断言 | |
| 56 | + OperateLogDO operateLogDO = operateLogMapper.selectOne(null); | |
| 57 | + assertPojoEquals(reqVO, operateLogDO); | |
| 65 | 58 | } |
| 66 | 59 | |
| 67 | 60 | @Test |
| 68 | 61 | public void testGetOperateLogPage() { |
| 69 | - // 构造测试数据 | |
| 70 | - // 先构造用户 | |
| 62 | + // mock(用户信息) | |
| 71 | 63 | AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> { |
| 72 | - o.setNickname("wangkai"); | |
| 73 | - o.setSex(SexEnum.MALE.getSex()); | |
| 64 | + o.setNickname("wang"); | |
| 74 | 65 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 75 | 66 | }); |
| 76 | - when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user)); | |
| 67 | + when(userService.getUserListByNickname("wang")).thenReturn(Collections.singletonList(user)); | |
| 77 | 68 | Long userId = user.getId(); |
| 69 | + | |
| 78 | 70 | // 构造操作日志 |
| 79 | - OperateLogDO sysOperateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> { | |
| 71 | + OperateLogDO operateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> { | |
| 80 | 72 | o.setUserId(userId); |
| 81 | 73 | o.setUserType(randomEle(UserTypeEnum.values()).getValue()); |
| 82 | 74 | o.setModule("order"); |
| ... | ... | @@ -85,50 +77,46 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest { |
| 85 | 77 | o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode()); |
| 86 | 78 | o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build()); |
| 87 | 79 | }); |
| 88 | - operateLogMapper.insert(sysOperateLogDO); | |
| 89 | - | |
| 90 | - // 下面几个是不匹配的数据 | |
| 91 | - // 随机 userId | |
| 92 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); | |
| 93 | - // module 不同 | |
| 94 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user"))); | |
| 95 | - // type 不同 | |
| 96 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); | |
| 97 | - // createTime 不同 | |
| 98 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); | |
| 99 | - // resultCode 不同 | |
| 100 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(BAD_REQUEST.getCode()))); | |
| 80 | + operateLogMapper.insert(operateLogDO); | |
| 81 | + // 测试 userId 不匹配 | |
| 82 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setUserId(userId + 1))); | |
| 83 | + // 测试 module 不匹配 | |
| 84 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setModule("user"))); | |
| 85 | + // 测试 type 不匹配 | |
| 86 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setType(OperateTypeEnum.IMPORT.getType()))); | |
| 87 | + // 测试 createTime 不匹配 | |
| 88 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setStartTime(buildTime(2021, 2, 6)))); | |
| 89 | + // 测试 resultCode 不匹配 | |
| 90 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setResultCode(BAD_REQUEST.getCode()))); | |
| 101 | 91 | |
| 102 | 92 | // 构造调用参数 |
| 103 | 93 | OperateLogPageReqVO reqVO = new OperateLogPageReqVO(); |
| 104 | - reqVO.setUserNickname("wangkai"); | |
| 94 | + reqVO.setUserNickname("wang"); | |
| 105 | 95 | reqVO.setModule("order"); |
| 106 | 96 | reqVO.setType(OperateTypeEnum.CREATE.getType()); |
| 107 | - reqVO.setStartTime((new LocalDateTime[]{buildTime(2021, 3, 5), | |
| 108 | - buildTime(2021, 3, 7)})); | |
| 97 | + reqVO.setStartTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7)); | |
| 109 | 98 | reqVO.setSuccess(true); |
| 110 | 99 | |
| 111 | - // 调用service方法 | |
| 100 | + // 调用 | |
| 112 | 101 | PageResult<OperateLogDO> pageResult = operateLogServiceImpl.getOperateLogPage(reqVO); |
| 113 | 102 | // 断言,只查到了一条符合条件的 |
| 114 | 103 | assertEquals(1, pageResult.getTotal()); |
| 115 | 104 | assertEquals(1, pageResult.getList().size()); |
| 116 | - assertPojoEquals(sysOperateLogDO, pageResult.getList().get(0)); | |
| 105 | + assertPojoEquals(operateLogDO, pageResult.getList().get(0)); | |
| 117 | 106 | } |
| 118 | 107 | |
| 119 | 108 | @Test |
| 120 | 109 | public void testGetOperateLogs() { |
| 121 | - // 构造测试数据 | |
| 122 | - // 先构造用户 | |
| 110 | + // mock(用户信息) | |
| 123 | 111 | AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> { |
| 124 | - o.setNickname("wangkai"); | |
| 125 | - o.setSex(SexEnum.MALE.getSex()); | |
| 112 | + o.setNickname("wang"); | |
| 126 | 113 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 127 | 114 | }); |
| 128 | - when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user)); | |
| 115 | + when(userService.getUserListByNickname("wang")).thenReturn(Collections.singletonList(user)); | |
| 129 | 116 | Long userId = user.getId(); |
| 117 | + | |
| 130 | 118 | // 构造操作日志 |
| 131 | - OperateLogDO sysOperateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> { | |
| 119 | + OperateLogDO operateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> { | |
| 132 | 120 | o.setUserId(userId); |
| 133 | 121 | o.setUserType(randomEle(UserTypeEnum.values()).getValue()); |
| 134 | 122 | o.setModule("order"); |
| ... | ... | @@ -137,33 +125,31 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest { |
| 137 | 125 | o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode()); |
| 138 | 126 | o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build()); |
| 139 | 127 | }); |
| 140 | - operateLogMapper.insert(sysOperateLogDO); | |
| 141 | - | |
| 142 | - // 下面几个是不匹配的数据 | |
| 143 | - // 随机 userId | |
| 144 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); | |
| 145 | - // module 不同 | |
| 146 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user"))); | |
| 147 | - // type 不同 | |
| 148 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); | |
| 149 | - // createTime 不同 | |
| 150 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); | |
| 151 | - // resultCode 不同 | |
| 152 | - operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(BAD_REQUEST.getCode()))); | |
| 128 | + operateLogMapper.insert(operateLogDO); | |
| 129 | + // 测试 userId 不匹配 | |
| 130 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setUserId(userId + 1))); | |
| 131 | + // 测试 module 不匹配 | |
| 132 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setModule("user"))); | |
| 133 | + // 测试 type 不匹配 | |
| 134 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setType(OperateTypeEnum.IMPORT.getType()))); | |
| 135 | + // 测试 createTime 不匹配 | |
| 136 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setStartTime(buildTime(2021, 2, 6)))); | |
| 137 | + // 测试 resultCode 不匹配 | |
| 138 | + operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setResultCode(BAD_REQUEST.getCode()))); | |
| 153 | 139 | |
| 154 | 140 | // 构造调用参数 |
| 155 | 141 | OperateLogExportReqVO reqVO = new OperateLogExportReqVO(); |
| 156 | - reqVO.setUserNickname("wangkai"); | |
| 142 | + reqVO.setUserNickname("wang"); | |
| 157 | 143 | reqVO.setModule("order"); |
| 158 | 144 | reqVO.setType(OperateTypeEnum.CREATE.getType()); |
| 159 | - reqVO.setStartTime((new LocalDateTime[]{buildTime(2021, 3, 5),buildTime(2021, 3, 7)})); | |
| 145 | + reqVO.setStartTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7)); | |
| 160 | 146 | reqVO.setSuccess(true); |
| 161 | 147 | |
| 162 | 148 | // 调用 service 方法 |
| 163 | - List<OperateLogDO> list = operateLogServiceImpl.getOperateLogs(reqVO); | |
| 149 | + List<OperateLogDO> list = operateLogServiceImpl.getOperateLogList(reqVO); | |
| 164 | 150 | // 断言,只查到了一条符合条件的 |
| 165 | 151 | assertEquals(1, list.size()); |
| 166 | - assertPojoEquals(sysOperateLogDO, list.get(0)); | |
| 152 | + assertPojoEquals(operateLogDO, list.get(0)); | |
| 167 | 153 | } |
| 168 | 154 | |
| 169 | 155 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailAccountServiceImplTest.java
| ... | ... | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou |
| 6 | 6 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 9 | -import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; | |
| 10 | 9 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; |
| 11 | 10 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| 12 | 11 | import org.junit.jupiter.api.Test; |
| ... | ... | @@ -14,7 +13,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | 13 | import org.springframework.context.annotation.Import; |
| 15 | 14 | |
| 16 | 15 | import javax.annotation.Resource; |
| 17 | - | |
| 16 | +import java.util.List; | |
| 18 | 17 | import java.util.Map; |
| 19 | 18 | |
| 20 | 19 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| ... | ... | @@ -23,7 +22,9 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic |
| 23 | 22 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 24 | 23 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS; |
| 25 | 24 | import static org.junit.jupiter.api.Assertions.*; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; | |
| 26 | 26 | import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; | |
| 27 | 28 | |
| 28 | 29 | /** |
| 29 | 30 | * {@link MailAccountServiceImpl} 的单元测试类 |
| ... | ... | @@ -109,6 +110,8 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { |
| 109 | 110 | mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 |
| 110 | 111 | // 准备参数 |
| 111 | 112 | Long id = dbMailAccount.getId(); |
| 113 | + // mock 方法(无关联模版) | |
| 114 | + when(mailTemplateService.countByAccountId(eq(id))).thenReturn(0L); | |
| 112 | 115 | |
| 113 | 116 | // 调用 |
| 114 | 117 | mailAccountService.deleteMailAccount(id); |
| ... | ... | @@ -118,6 +121,21 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { |
| 118 | 121 | } |
| 119 | 122 | |
| 120 | 123 | @Test |
| 124 | + public void testGetMailAccountFromCache() { | |
| 125 | + // mock 数据 | |
| 126 | + MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); | |
| 127 | + mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | |
| 128 | + mailAccountService.initLocalCache(); | |
| 129 | + // 准备参数 | |
| 130 | + Long id = dbMailAccount.getId(); | |
| 131 | + | |
| 132 | + // 调用 | |
| 133 | + MailAccountDO mailAccount = mailAccountService.getMailAccountFromCache(id); | |
| 134 | + // 断言 | |
| 135 | + assertPojoEquals(dbMailAccount, mailAccount); | |
| 136 | + } | |
| 137 | + | |
| 138 | + @Test | |
| 121 | 139 | public void testDeleteMailAccount_notExists() { |
| 122 | 140 | // 准备参数 |
| 123 | 141 | Long id = randomLongId(); |
| ... | ... | @@ -151,4 +169,35 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { |
| 151 | 169 | assertPojoEquals(dbMailAccount, pageResult.getList().get(0)); |
| 152 | 170 | } |
| 153 | 171 | |
| 172 | + @Test | |
| 173 | + public void testGetMailAccount() { | |
| 174 | + // mock 数据 | |
| 175 | + MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); | |
| 176 | + mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | |
| 177 | + // 准备参数 | |
| 178 | + Long id = dbMailAccount.getId(); | |
| 179 | + | |
| 180 | + // 调用 | |
| 181 | + MailAccountDO mailAccount = mailAccountService.getMailAccount(id); | |
| 182 | + // 断言 | |
| 183 | + assertPojoEquals(dbMailAccount, mailAccount); | |
| 184 | + } | |
| 185 | + | |
| 186 | + @Test | |
| 187 | + public void testGetMailAccountList() { | |
| 188 | + // mock 数据 | |
| 189 | + MailAccountDO dbMailAccount01 = randomPojo(MailAccountDO.class); | |
| 190 | + mailAccountMapper.insert(dbMailAccount01); | |
| 191 | + MailAccountDO dbMailAccount02 = randomPojo(MailAccountDO.class); | |
| 192 | + mailAccountMapper.insert(dbMailAccount02); | |
| 193 | + // 准备参数 | |
| 194 | + | |
| 195 | + // 调用 | |
| 196 | + List<MailAccountDO> list = mailAccountService.getMailAccountList(); | |
| 197 | + // 断言 | |
| 198 | + assertEquals(2, list.size()); | |
| 199 | + assertPojoEquals(dbMailAccount01, list.get(0)); | |
| 200 | + assertPojoEquals(dbMailAccount02, list.get(1)); | |
| 201 | + } | |
| 202 | + | |
| 154 | 203 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailLogServiceImplTest.java
| ... | ... | @@ -117,6 +117,20 @@ public class MailLogServiceImplTest extends BaseDbUnitTest { |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | @Test |
| 120 | + public void testGetMailLog() { | |
| 121 | + // mock 数据 | |
| 122 | + MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> o.setTemplateParams(randomTemplateParams())); | |
| 123 | + mailLogMapper.insert(dbMailLog); | |
| 124 | + // 准备参数 | |
| 125 | + Long id = dbMailLog.getId(); | |
| 126 | + | |
| 127 | + // 调用 | |
| 128 | + MailLogDO mailLog = mailLogService.getMailLog(id); | |
| 129 | + // 断言 | |
| 130 | + assertPojoEquals(dbMailLog, mailLog); | |
| 131 | + } | |
| 132 | + | |
| 133 | + @Test | |
| 120 | 134 | public void testGetMailLogPage() { |
| 121 | 135 | // mock 数据 |
| 122 | 136 | MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> { // 等会查询到 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImplTest.java
| ... | ... | @@ -6,14 +6,20 @@ import cn.hutool.extra.mail.MailUtil; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | 7 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 8 | 8 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 9 | +import cn.iocoder.yudao.framework.test.core.util.RandomUtils; | |
| 9 | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 10 | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 12 | +import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | |
| 13 | +import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; | |
| 11 | 14 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| 15 | +import cn.iocoder.yudao.module.system.service.member.MemberService; | |
| 16 | +import cn.iocoder.yudao.module.system.service.user.AdminUserService; | |
| 12 | 17 | import org.assertj.core.util.Lists; |
| 13 | 18 | import org.junit.jupiter.api.Disabled; |
| 14 | 19 | import org.junit.jupiter.api.Test; |
| 15 | 20 | import org.mockito.InjectMocks; |
| 16 | 21 | import org.mockito.Mock; |
| 22 | +import org.mockito.MockedStatic; | |
| 17 | 23 | |
| 18 | 24 | import java.util.HashMap; |
| 19 | 25 | import java.util.Map; |
| ... | ... | @@ -23,6 +29,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic |
| 23 | 29 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 24 | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 25 | 31 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; | |
| 26 | 33 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | 34 | import static org.mockito.Mockito.*; |
| 28 | 35 | |
| ... | ... | @@ -32,6 +39,10 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 32 | 39 | private MailSendServiceImpl mailSendService; |
| 33 | 40 | |
| 34 | 41 | @Mock |
| 42 | + private AdminUserService adminUserService; | |
| 43 | + @Mock | |
| 44 | + private MemberService memberService; | |
| 45 | + @Mock | |
| 35 | 46 | private MailAccountService mailAccountService; |
| 36 | 47 | @Mock |
| 37 | 48 | private MailTemplateService mailTemplateService; |
| ... | ... | @@ -55,6 +66,82 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 55 | 66 | System.out.println("发送结果:" + messageId); |
| 56 | 67 | } |
| 57 | 68 | |
| 69 | + @Test | |
| 70 | + public void testSendSingleMailToAdmin() { | |
| 71 | + // 准备参数 | |
| 72 | + Long userId = randomLongId(); | |
| 73 | + String templateCode = RandomUtils.randomString(); | |
| 74 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | |
| 75 | + .put("op", "login").build(); | |
| 76 | + // mock adminUserService 的方法 | |
| 77 | + AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setMobile("15601691300")); | |
| 78 | + when(adminUserService.getUser(eq(userId))).thenReturn(user); | |
| 79 | + | |
| 80 | + // mock MailTemplateService 的方法 | |
| 81 | + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | |
| 82 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 83 | + o.setContent("验证码为{code}, 操作为{op}"); | |
| 84 | + o.setParams(Lists.newArrayList("code", "op")); | |
| 85 | + }); | |
| 86 | + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | |
| 87 | + String content = RandomUtils.randomString(); | |
| 88 | + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | |
| 89 | + .thenReturn(content); | |
| 90 | + // mock MailAccountService 的方法 | |
| 91 | + MailAccountDO account = randomPojo(MailAccountDO.class); | |
| 92 | + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | |
| 93 | + // mock MailLogService 的方法 | |
| 94 | + Long mailLogId = randomLongId(); | |
| 95 | + when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.ADMIN.getValue()), eq(user.getEmail()), | |
| 96 | + eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); | |
| 97 | + | |
| 98 | + // 调用 | |
| 99 | + Long resultMailLogId = mailSendService.sendSingleMailToAdmin(null, userId, templateCode, templateParams); | |
| 100 | + // 断言 | |
| 101 | + assertEquals(mailLogId, resultMailLogId); | |
| 102 | + // 断言调用 | |
| 103 | + verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(user.getEmail()), | |
| 104 | + eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); | |
| 105 | + } | |
| 106 | + | |
| 107 | + @Test | |
| 108 | + public void testSendSingleMailToMember() { | |
| 109 | + // 准备参数 | |
| 110 | + Long userId = randomLongId(); | |
| 111 | + String templateCode = RandomUtils.randomString(); | |
| 112 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | |
| 113 | + .put("op", "login").build(); | |
| 114 | + // mock memberService 的方法 | |
| 115 | + String mail = randomEmail(); | |
| 116 | + when(memberService.getMemberUserEmail(eq(userId))).thenReturn(mail); | |
| 117 | + | |
| 118 | + // mock MailTemplateService 的方法 | |
| 119 | + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | |
| 120 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 121 | + o.setContent("验证码为{code}, 操作为{op}"); | |
| 122 | + o.setParams(Lists.newArrayList("code", "op")); | |
| 123 | + }); | |
| 124 | + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | |
| 125 | + String content = RandomUtils.randomString(); | |
| 126 | + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | |
| 127 | + .thenReturn(content); | |
| 128 | + // mock MailAccountService 的方法 | |
| 129 | + MailAccountDO account = randomPojo(MailAccountDO.class); | |
| 130 | + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | |
| 131 | + // mock MailLogService 的方法 | |
| 132 | + Long mailLogId = randomLongId(); | |
| 133 | + when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.MEMBER.getValue()), eq(mail), | |
| 134 | + eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); | |
| 135 | + | |
| 136 | + // 调用 | |
| 137 | + Long resultMailLogId = mailSendService.sendSingleMailToMember(null, userId, templateCode, templateParams); | |
| 138 | + // 断言 | |
| 139 | + assertEquals(mailLogId, resultMailLogId); | |
| 140 | + // 断言调用 | |
| 141 | + verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(mail), | |
| 142 | + eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); | |
| 143 | + } | |
| 144 | + | |
| 58 | 145 | /** |
| 59 | 146 | * 发送成功,当短信模板开启时 |
| 60 | 147 | */ |
| ... | ... | @@ -64,7 +151,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 64 | 151 | String mail = randomEmail(); |
| 65 | 152 | Long userId = randomLongId(); |
| 66 | 153 | Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
| 67 | - String templateCode = randomString(); | |
| 154 | + String templateCode = RandomUtils.randomString(); | |
| 68 | 155 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
| 69 | 156 | .put("op", "login").build(); |
| 70 | 157 | // mock MailTemplateService 的方法 |
| ... | ... | @@ -74,7 +161,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 74 | 161 | o.setParams(Lists.newArrayList("code", "op")); |
| 75 | 162 | }); |
| 76 | 163 | when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
| 77 | - String content = randomString(); | |
| 164 | + String content = RandomUtils.randomString(); | |
| 78 | 165 | when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) |
| 79 | 166 | .thenReturn(content); |
| 80 | 167 | // mock MailAccountService 的方法 |
| ... | ... | @@ -103,7 +190,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 103 | 190 | String mail = randomEmail(); |
| 104 | 191 | Long userId = randomLongId(); |
| 105 | 192 | Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
| 106 | - String templateCode = randomString(); | |
| 193 | + String templateCode = RandomUtils.randomString(); | |
| 107 | 194 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
| 108 | 195 | .put("op", "login").build(); |
| 109 | 196 | // mock MailTemplateService 的方法 |
| ... | ... | @@ -113,7 +200,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 113 | 200 | o.setParams(Lists.newArrayList("code", "op")); |
| 114 | 201 | }); |
| 115 | 202 | when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
| 116 | - String content = randomString(); | |
| 203 | + String content = RandomUtils.randomString(); | |
| 117 | 204 | when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) |
| 118 | 205 | .thenReturn(content); |
| 119 | 206 | // mock MailAccountService 的方法 |
| ... | ... | @@ -134,18 +221,18 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 134 | 221 | } |
| 135 | 222 | |
| 136 | 223 | @Test |
| 137 | - public void testCheckMailTemplateValid_notExists() { | |
| 224 | + public void testValidateMailTemplateValid_notExists() { | |
| 138 | 225 | // 准备参数 |
| 139 | - String templateCode = randomString(); | |
| 226 | + String templateCode = RandomUtils.randomString(); | |
| 140 | 227 | // mock 方法 |
| 141 | 228 | |
| 142 | 229 | // 调用,并断言异常 |
| 143 | - assertServiceException(() -> mailSendService.checkMailTemplateValid(templateCode), | |
| 230 | + assertServiceException(() -> mailSendService.validateMailTemplate(templateCode), | |
| 144 | 231 | MAIL_TEMPLATE_NOT_EXISTS); |
| 145 | 232 | } |
| 146 | 233 | |
| 147 | 234 | @Test |
| 148 | - public void testCheckTemplateParams_paramMiss() { | |
| 235 | + public void testValidateTemplateParams_paramMiss() { | |
| 149 | 236 | // 准备参数 |
| 150 | 237 | MailTemplateDO template = randomPojo(MailTemplateDO.class, |
| 151 | 238 | o -> o.setParams(Lists.newArrayList("code"))); |
| ... | ... | @@ -153,18 +240,80 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { |
| 153 | 240 | // mock 方法 |
| 154 | 241 | |
| 155 | 242 | // 调用,并断言异常 |
| 156 | - assertServiceException(() -> mailSendService.checkTemplateParams(template, templateParams), | |
| 243 | + assertServiceException(() -> mailSendService.validateTemplateParams(template, templateParams), | |
| 157 | 244 | MAIL_SEND_TEMPLATE_PARAM_MISS, "code"); |
| 158 | 245 | } |
| 159 | 246 | |
| 160 | 247 | @Test |
| 161 | - public void testCheckMail_notExists() { | |
| 248 | + public void testValidateMail_notExists() { | |
| 162 | 249 | // 准备参数 |
| 163 | 250 | // mock 方法 |
| 164 | 251 | |
| 165 | 252 | // 调用,并断言异常 |
| 166 | - assertServiceException(() -> mailSendService.checkMail(null), | |
| 253 | + assertServiceException(() -> mailSendService.validateMail(null), | |
| 167 | 254 | MAIL_SEND_MAIL_NOT_EXISTS); |
| 168 | 255 | } |
| 169 | 256 | |
| 257 | + @Test | |
| 258 | + public void testDoSendMail_success() { | |
| 259 | + try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) { | |
| 260 | + // 准备参数 | |
| 261 | + MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿")); | |
| 262 | + // mock 方法(获得邮箱账号) | |
| 263 | + MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com")); | |
| 264 | + when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId()))) | |
| 265 | + .thenReturn(account); | |
| 266 | + | |
| 267 | + // mock 方法(发送邮件) | |
| 268 | + String messageId = randomString(); | |
| 269 | + mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> { | |
| 270 | + assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom()); | |
| 271 | + assertTrue(mailAccount.isAuth()); | |
| 272 | + assertEquals(account.getUsername(), mailAccount.getUser()); | |
| 273 | + assertEquals(account.getPassword(), mailAccount.getPass()); | |
| 274 | + assertEquals(account.getHost(), mailAccount.getHost()); | |
| 275 | + assertEquals(account.getPort(), mailAccount.getPort()); | |
| 276 | + assertEquals(account.getSslEnable(), mailAccount.isSslEnable()); | |
| 277 | + return true; | |
| 278 | + }), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true))) | |
| 279 | + .thenReturn(messageId); | |
| 280 | + | |
| 281 | + // 调用 | |
| 282 | + mailSendService.doSendMail(message); | |
| 283 | + // 断言 | |
| 284 | + verify(mailLogService).updateMailSendResult(eq(message.getLogId()), eq(messageId), isNull()); | |
| 285 | + } | |
| 286 | + } | |
| 287 | + | |
| 288 | + @Test | |
| 289 | + public void testDoSendMail_exception() { | |
| 290 | + try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) { | |
| 291 | + // 准备参数 | |
| 292 | + MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿")); | |
| 293 | + // mock 方法(获得邮箱账号) | |
| 294 | + MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com")); | |
| 295 | + when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId()))) | |
| 296 | + .thenReturn(account); | |
| 297 | + | |
| 298 | + // mock 方法(发送邮件) | |
| 299 | + Exception e = new NullPointerException("啦啦啦"); | |
| 300 | + mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> { | |
| 301 | + assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom()); | |
| 302 | + assertTrue(mailAccount.isAuth()); | |
| 303 | + assertEquals(account.getUsername(), mailAccount.getUser()); | |
| 304 | + assertEquals(account.getPassword(), mailAccount.getPass()); | |
| 305 | + assertEquals(account.getHost(), mailAccount.getHost()); | |
| 306 | + assertEquals(account.getPort(), mailAccount.getPort()); | |
| 307 | + assertEquals(account.getSslEnable(), mailAccount.isSslEnable()); | |
| 308 | + return true; | |
| 309 | + }), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true))) | |
| 310 | + .thenThrow(e); | |
| 311 | + | |
| 312 | + // 调用 | |
| 313 | + mailSendService.doSendMail(message); | |
| 314 | + // 断言 | |
| 315 | + verify(mailLogService).updateMailSendResult(eq(message.getLogId()), isNull(), same(e)); | |
| 316 | + } | |
| 317 | + } | |
| 318 | + | |
| 170 | 319 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateServiceImplTest.java
| ... | ... | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 6 | 6 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; |
| 9 | -import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | |
| 10 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 11 | 10 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; |
| 12 | 11 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| ... | ... | @@ -15,7 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 15 | 14 | import org.springframework.context.annotation.Import; |
| 16 | 15 | |
| 17 | 16 | import javax.annotation.Resource; |
| 18 | - | |
| 17 | +import java.util.HashMap; | |
| 18 | +import java.util.List; | |
| 19 | 19 | import java.util.Map; |
| 20 | 20 | |
| 21 | 21 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
| ... | ... | @@ -27,6 +27,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId |
| 27 | 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 28 | 28 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; |
| 29 | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | +import static org.mockito.Mockito.verify; | |
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * {@link MailTemplateServiceImpl} 的单元测试类 |
| ... | ... | @@ -72,6 +73,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { |
| 72 | 73 | // 校验记录的属性是否正确 |
| 73 | 74 | MailTemplateDO mailTemplate = mailTemplateMapper.selectById(mailTemplateId); |
| 74 | 75 | assertPojoEquals(reqVO, mailTemplate); |
| 76 | + verify(mailProducer).sendMailTemplateRefreshMessage(); | |
| 75 | 77 | } |
| 76 | 78 | |
| 77 | 79 | @Test |
| ... | ... | @@ -89,6 +91,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { |
| 89 | 91 | // 校验是否更新正确 |
| 90 | 92 | MailTemplateDO mailTemplate = mailTemplateMapper.selectById(reqVO.getId()); // 获取最新的 |
| 91 | 93 | assertPojoEquals(reqVO, mailTemplate); |
| 94 | + verify(mailProducer).sendMailTemplateRefreshMessage(); | |
| 92 | 95 | } |
| 93 | 96 | |
| 94 | 97 | @Test |
| ... | ... | @@ -110,8 +113,9 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { |
| 110 | 113 | |
| 111 | 114 | // 调用 |
| 112 | 115 | mailTemplateService.deleteMailTemplate(id); |
| 113 | - // 校验数据不存在了 | |
| 114 | - assertNull(mailTemplateMapper.selectById(id)); | |
| 116 | + // 校验数据不存在了 | |
| 117 | + assertNull(mailTemplateMapper.selectById(id)); | |
| 118 | + verify(mailProducer).sendMailTemplateRefreshMessage(); | |
| 115 | 119 | } |
| 116 | 120 | |
| 117 | 121 | @Test |
| ... | ... | @@ -160,4 +164,77 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { |
| 160 | 164 | assertPojoEquals(dbMailTemplate, pageResult.getList().get(0)); |
| 161 | 165 | } |
| 162 | 166 | |
| 167 | + @Test | |
| 168 | + public void testGetMailTemplateList() { | |
| 169 | + // mock 数据 | |
| 170 | + MailTemplateDO dbMailTemplate01 = randomPojo(MailTemplateDO.class); | |
| 171 | + mailTemplateMapper.insert(dbMailTemplate01); | |
| 172 | + MailTemplateDO dbMailTemplate02 = randomPojo(MailTemplateDO.class); | |
| 173 | + mailTemplateMapper.insert(dbMailTemplate02); | |
| 174 | + | |
| 175 | + // 调用 | |
| 176 | + List<MailTemplateDO> list = mailTemplateService.getMailTemplateList(); | |
| 177 | + // 断言 | |
| 178 | + assertEquals(2, list.size()); | |
| 179 | + assertEquals(dbMailTemplate01, list.get(0)); | |
| 180 | + assertEquals(dbMailTemplate02, list.get(1)); | |
| 181 | + } | |
| 182 | + | |
| 183 | + @Test | |
| 184 | + public void testGetMailTemplate() { | |
| 185 | + // mock 数据 | |
| 186 | + MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | |
| 187 | + mailTemplateMapper.insert(dbMailTemplate); | |
| 188 | + // 准备参数 | |
| 189 | + Long id = dbMailTemplate.getId(); | |
| 190 | + | |
| 191 | + // 调用 | |
| 192 | + MailTemplateDO mailTemplate = mailTemplateService.getMailTemplate(id); | |
| 193 | + // 断言 | |
| 194 | + assertPojoEquals(dbMailTemplate, mailTemplate); | |
| 195 | + } | |
| 196 | + | |
| 197 | + @Test | |
| 198 | + public void testGetMailTemplateByCodeFromCache() { | |
| 199 | + // mock 数据 | |
| 200 | + MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | |
| 201 | + mailTemplateMapper.insert(dbMailTemplate); | |
| 202 | + mailTemplateService.initLocalCache(); | |
| 203 | + // 准备参数 | |
| 204 | + String code = dbMailTemplate.getCode(); | |
| 205 | + | |
| 206 | + // 调用 | |
| 207 | + MailTemplateDO mailTemplate = mailTemplateService.getMailTemplateByCodeFromCache(code); | |
| 208 | + // 断言 | |
| 209 | + assertPojoEquals(dbMailTemplate, mailTemplate); | |
| 210 | + } | |
| 211 | + | |
| 212 | + @Test | |
| 213 | + public void testFormatMailTemplateContent() { | |
| 214 | + // 准备参数 | |
| 215 | + Map<String, Object> params = new HashMap<>(); | |
| 216 | + params.put("name", "小红"); | |
| 217 | + params.put("what", "饭"); | |
| 218 | + | |
| 219 | + // 调用,并断言 | |
| 220 | + assertEquals("小红,你好,饭吃了吗?", | |
| 221 | + mailTemplateService.formatMailTemplateContent("{name},你好,{what}吃了吗?", params)); | |
| 222 | + } | |
| 223 | + | |
| 224 | + @Test | |
| 225 | + public void testCountByAccountId() { | |
| 226 | + // mock 数据 | |
| 227 | + MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | |
| 228 | + mailTemplateMapper.insert(dbMailTemplate); | |
| 229 | + // 测试 accountId 不匹配 | |
| 230 | + mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L))); | |
| 231 | + // 准备参数 | |
| 232 | + Long accountId = dbMailTemplate.getAccountId(); | |
| 233 | + | |
| 234 | + // 调用 | |
| 235 | + long count = mailTemplateService.countByAccountId(accountId); | |
| 236 | + // 断言 | |
| 237 | + assertEquals(1, count); | |
| 238 | + } | |
| 239 | + | |
| 163 | 240 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notice/NoticeServiceImplTest.java
| ... | ... | @@ -2,75 +2,67 @@ package cn.iocoder.yudao.module.system.service.notice; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 5 | 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; |
| 9 | 10 | import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; |
| 10 | -import cn.iocoder.yudao.module.system.enums.notice.NoticeTypeEnum; | |
| 11 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 12 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 13 | 11 | import org.junit.jupiter.api.Test; |
| 14 | 12 | import org.springframework.context.annotation.Import; |
| 15 | 13 | |
| 16 | 14 | import javax.annotation.Resource; |
| 17 | 15 | |
| 18 | -import java.util.function.Consumer; | |
| 19 | - | |
| 20 | -import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 21 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; | |
| 16 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 22 | 17 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 23 | 18 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 24 | 19 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 25 | 20 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 21 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; | |
| 26 | 22 | import static org.junit.jupiter.api.Assertions.*; |
| 27 | 23 | |
| 28 | 24 | @Import(NoticeServiceImpl.class) |
| 29 | 25 | class NoticeServiceImplTest extends BaseDbUnitTest { |
| 30 | 26 | |
| 31 | 27 | @Resource |
| 32 | - private NoticeServiceImpl sysNoticeService; | |
| 28 | + private NoticeServiceImpl noticeService; | |
| 33 | 29 | |
| 34 | 30 | @Resource |
| 35 | - private NoticeMapper sysNoticeMapper; | |
| 31 | + private NoticeMapper noticeMapper; | |
| 36 | 32 | |
| 37 | 33 | @Test |
| 38 | - public void testPageNotices_success() { | |
| 34 | + public void testGetNoticePage_success() { | |
| 39 | 35 | // 插入前置数据 |
| 40 | 36 | NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> { |
| 41 | 37 | o.setTitle("尼古拉斯赵四来啦!"); |
| 42 | 38 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 43 | - o.setType(randomEle(NoticeTypeEnum.values()).getType()); | |
| 44 | 39 | }); |
| 45 | - sysNoticeMapper.insert(dbNotice); | |
| 46 | - | |
| 40 | + noticeMapper.insert(dbNotice); | |
| 47 | 41 | // 测试 title 不匹配 |
| 48 | - sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); | |
| 42 | + noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); | |
| 49 | 43 | // 测试 status 不匹配 |
| 50 | - sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 51 | - | |
| 52 | - | |
| 53 | - // 查询 | |
| 44 | + noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 45 | + // 准备参数 | |
| 54 | 46 | NoticePageReqVO reqVO = new NoticePageReqVO(); |
| 55 | 47 | reqVO.setTitle("尼古拉斯赵四来啦!"); |
| 56 | 48 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 57 | - PageResult<NoticeDO> pageResult = sysNoticeService.pageNotices(reqVO); | |
| 58 | 49 | |
| 50 | + // 调用 | |
| 51 | + PageResult<NoticeDO> pageResult = noticeService.getNoticePage(reqVO); | |
| 59 | 52 | // 验证查询结果经过筛选 |
| 60 | 53 | assertEquals(1, pageResult.getTotal()); |
| 61 | 54 | assertEquals(1, pageResult.getList().size()); |
| 62 | 55 | assertPojoEquals(dbNotice, pageResult.getList().get(0)); |
| 63 | - | |
| 64 | 56 | } |
| 65 | 57 | |
| 66 | 58 | @Test |
| 67 | 59 | public void testGetNotice_success() { |
| 68 | 60 | // 插入前置数据 |
| 69 | - NoticeDO dbNotice = randomNoticeDO(); | |
| 70 | - sysNoticeMapper.insert(dbNotice); | |
| 61 | + NoticeDO dbNotice = randomPojo(NoticeDO.class); | |
| 62 | + noticeMapper.insert(dbNotice); | |
| 71 | 63 | |
| 72 | 64 | // 查询 |
| 73 | - NoticeDO notice = sysNoticeService.getNotice(dbNotice.getId()); | |
| 65 | + NoticeDO notice = noticeService.getNotice(dbNotice.getId()); | |
| 74 | 66 | |
| 75 | 67 | // 验证插入与读取对象是否一致 |
| 76 | 68 | assertNotNull(notice); |
| ... | ... | @@ -80,84 +72,59 @@ class NoticeServiceImplTest extends BaseDbUnitTest { |
| 80 | 72 | @Test |
| 81 | 73 | public void testCreateNotice_success() { |
| 82 | 74 | // 准备参数 |
| 83 | - NoticeCreateReqVO reqVO = randomNoticeCreateReqVO(); | |
| 84 | - | |
| 85 | - // 校验插入是否成功 | |
| 86 | - Long noticeId = sysNoticeService.createNotice(reqVO); | |
| 87 | - assertNotNull(noticeId); | |
| 75 | + NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class); | |
| 88 | 76 | |
| 77 | + // 调用 | |
| 78 | + Long noticeId = noticeService.createNotice(reqVO); | |
| 89 | 79 | // 校验插入属性是否正确 |
| 90 | - NoticeDO notice = sysNoticeMapper.selectById(noticeId); | |
| 80 | + assertNotNull(noticeId); | |
| 81 | + NoticeDO notice = noticeMapper.selectById(noticeId); | |
| 91 | 82 | assertPojoEquals(reqVO, notice); |
| 92 | 83 | } |
| 93 | 84 | |
| 94 | 85 | @Test |
| 95 | 86 | public void testUpdateNotice_success() { |
| 96 | 87 | // 插入前置数据 |
| 97 | - NoticeDO dbNoticeDO = randomNoticeDO(); | |
| 98 | - sysNoticeMapper.insert(dbNoticeDO); | |
| 88 | + NoticeDO dbNoticeDO = randomPojo(NoticeDO.class); | |
| 89 | + noticeMapper.insert(dbNoticeDO); | |
| 99 | 90 | |
| 100 | 91 | // 准备更新参数 |
| 101 | - NoticeUpdateReqVO reqVO = randomNoticeUpdateReqVO(o -> o.setId(dbNoticeDO.getId())); | |
| 92 | + NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, o -> o.setId(dbNoticeDO.getId())); | |
| 102 | 93 | |
| 103 | 94 | // 更新 |
| 104 | - sysNoticeService.updateNotice(reqVO); | |
| 105 | - | |
| 95 | + noticeService.updateNotice(reqVO); | |
| 106 | 96 | // 检验是否更新成功 |
| 107 | - NoticeDO notice = sysNoticeMapper.selectById(reqVO.getId()); | |
| 97 | + NoticeDO notice = noticeMapper.selectById(reqVO.getId()); | |
| 108 | 98 | assertPojoEquals(reqVO, notice); |
| 109 | 99 | } |
| 110 | 100 | |
| 111 | 101 | @Test |
| 112 | 102 | public void testDeleteNotice_success() { |
| 113 | 103 | // 插入前置数据 |
| 114 | - NoticeDO dbNotice = randomNoticeDO(); | |
| 115 | - sysNoticeMapper.insert(dbNotice); | |
| 104 | + NoticeDO dbNotice = randomPojo(NoticeDO.class); | |
| 105 | + noticeMapper.insert(dbNotice); | |
| 116 | 106 | |
| 117 | 107 | // 删除 |
| 118 | - sysNoticeService.deleteNotice(dbNotice.getId()); | |
| 108 | + noticeService.deleteNotice(dbNotice.getId()); | |
| 119 | 109 | |
| 120 | 110 | // 检查是否删除成功 |
| 121 | - assertNull(sysNoticeMapper.selectById(dbNotice.getId())); | |
| 111 | + assertNull(noticeMapper.selectById(dbNotice.getId())); | |
| 122 | 112 | } |
| 123 | 113 | |
| 124 | 114 | @Test |
| 125 | - public void checkNoticeExists_success() { | |
| 115 | + public void testValidateNoticeExists_success() { | |
| 126 | 116 | // 插入前置数据 |
| 127 | - NoticeDO dbNotice = randomNoticeDO(); | |
| 128 | - sysNoticeMapper.insert(dbNotice); | |
| 117 | + NoticeDO dbNotice = randomPojo(NoticeDO.class); | |
| 118 | + noticeMapper.insert(dbNotice); | |
| 129 | 119 | |
| 130 | 120 | // 成功调用 |
| 131 | - sysNoticeService.checkNoticeExists(dbNotice.getId()); | |
| 121 | + noticeService.validateNoticeExists(dbNotice.getId()); | |
| 132 | 122 | } |
| 133 | 123 | |
| 134 | 124 | @Test |
| 135 | - public void checkNoticeExists_noExists() { | |
| 136 | - assertServiceException(() -> sysNoticeService.checkNoticeExists(randomLongId()), NOTICE_NOT_FOUND); | |
| 125 | + public void testValidateNoticeExists_noExists() { | |
| 126 | + assertServiceException(() -> | |
| 127 | + noticeService.validateNoticeExists(randomLongId()), NOTICE_NOT_FOUND); | |
| 137 | 128 | } |
| 138 | 129 | |
| 139 | - @SafeVarargs | |
| 140 | - private static NoticeDO randomNoticeDO(Consumer<NoticeDO>... consumers) { | |
| 141 | - NoticeDO notice = randomPojo(NoticeDO.class, consumers); | |
| 142 | - notice.setType(randomEle(NoticeTypeEnum.values()).getType()); | |
| 143 | - notice.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 144 | - return notice; | |
| 145 | - } | |
| 146 | - | |
| 147 | - @SafeVarargs | |
| 148 | - private static NoticeUpdateReqVO randomNoticeUpdateReqVO(Consumer<NoticeUpdateReqVO>... consumers) { | |
| 149 | - NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, consumers); | |
| 150 | - reqVO.setType(randomEle(NoticeTypeEnum.values()).getType()); | |
| 151 | - reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 152 | - return reqVO; | |
| 153 | - } | |
| 154 | - | |
| 155 | - private static NoticeCreateReqVO randomNoticeCreateReqVO() { | |
| 156 | - NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class); | |
| 157 | - reqVO.setType(randomEle(NoticeTypeEnum.values()).getType()); | |
| 158 | - reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 159 | - return reqVO; | |
| 160 | - } | |
| 161 | - | |
| 162 | - | |
| 163 | 130 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImplTest.java
| ... | ... | @@ -109,6 +109,20 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | @Test |
| 112 | + public void testGetNotifyMessage() { | |
| 113 | + // mock 数据 | |
| 114 | + NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, | |
| 115 | + o -> o.setTemplateParams(randomTemplateParams())); | |
| 116 | + notifyMessageMapper.insert(dbNotifyMessage); | |
| 117 | + // 准备参数 | |
| 118 | + Long id = dbNotifyMessage.getId(); | |
| 119 | + | |
| 120 | + // 调用 | |
| 121 | + NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id); | |
| 122 | + assertPojoEquals(dbNotifyMessage, notifyMessage); | |
| 123 | + } | |
| 124 | + | |
| 125 | + @Test | |
| 112 | 126 | public void testGetMyNotifyMessagePage() { |
| 113 | 127 | // mock 数据 |
| 114 | 128 | NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImplTest.java
| ... | ... | @@ -4,8 +4,6 @@ import cn.hutool.core.map.MapUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 6 | 6 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 7 | -import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | |
| 8 | -import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | |
| 9 | 7 | import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO; |
| 10 | 8 | import org.assertj.core.util.Lists; |
| 11 | 9 | import org.junit.jupiter.api.Test; |
| ... | ... | @@ -18,7 +16,8 @@ import java.util.Map; |
| 18 | 16 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 19 | 17 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 20 | 18 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 21 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 19 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; | |
| 20 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_SEND_TEMPLATE_PARAM_MISS; | |
| 22 | 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | 22 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 24 | 23 | import static org.mockito.ArgumentMatchers.eq; |
| ... | ... | @@ -34,6 +33,62 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest { |
| 34 | 33 | @Mock |
| 35 | 34 | private NotifyMessageService notifyMessageService; |
| 36 | 35 | |
| 36 | + @Test | |
| 37 | + public void testSendSingleNotifyToAdmin() { | |
| 38 | + // 准备参数 | |
| 39 | + Long userId = randomLongId(); | |
| 40 | + String templateCode = randomString(); | |
| 41 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | |
| 42 | + .put("op", "login").build(); | |
| 43 | + // mock NotifyTemplateService 的方法 | |
| 44 | + NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> { | |
| 45 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 46 | + o.setContent("验证码为{code}, 操作为{op}"); | |
| 47 | + o.setParams(Lists.newArrayList("code", "op")); | |
| 48 | + }); | |
| 49 | + when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | |
| 50 | + String content = randomString(); | |
| 51 | + when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams))) | |
| 52 | + .thenReturn(content); | |
| 53 | + // mock NotifyMessageService 的方法 | |
| 54 | + Long messageId = randomLongId(); | |
| 55 | + when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.ADMIN.getValue()), | |
| 56 | + eq(template), eq(content), eq(templateParams))).thenReturn(messageId); | |
| 57 | + | |
| 58 | + // 调用 | |
| 59 | + Long resultMessageId = notifySendService.sendSingleNotifyToAdmin(userId, templateCode, templateParams); | |
| 60 | + // 断言 | |
| 61 | + assertEquals(messageId, resultMessageId); | |
| 62 | + } | |
| 63 | + | |
| 64 | + @Test | |
| 65 | + public void testSendSingleNotifyToMember() { | |
| 66 | + // 准备参数 | |
| 67 | + Long userId = randomLongId(); | |
| 68 | + String templateCode = randomString(); | |
| 69 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | |
| 70 | + .put("op", "login").build(); | |
| 71 | + // mock NotifyTemplateService 的方法 | |
| 72 | + NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> { | |
| 73 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 74 | + o.setContent("验证码为{code}, 操作为{op}"); | |
| 75 | + o.setParams(Lists.newArrayList("code", "op")); | |
| 76 | + }); | |
| 77 | + when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | |
| 78 | + String content = randomString(); | |
| 79 | + when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams))) | |
| 80 | + .thenReturn(content); | |
| 81 | + // mock NotifyMessageService 的方法 | |
| 82 | + Long messageId = randomLongId(); | |
| 83 | + when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.MEMBER.getValue()), | |
| 84 | + eq(template), eq(content), eq(templateParams))).thenReturn(messageId); | |
| 85 | + | |
| 86 | + // 调用 | |
| 87 | + Long resultMessageId = notifySendService.sendSingleNotifyToMember(userId, templateCode, templateParams); | |
| 88 | + // 断言 | |
| 89 | + assertEquals(messageId, resultMessageId); | |
| 90 | + } | |
| 91 | + | |
| 37 | 92 | /** |
| 38 | 93 | * 发送成功,当短信模板开启时 |
| 39 | 94 | */ |
| ... | ... | @@ -100,7 +155,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest { |
| 100 | 155 | // mock 方法 |
| 101 | 156 | |
| 102 | 157 | // 调用,并断言异常 |
| 103 | - assertServiceException(() -> notifySendService.checkNotifyTemplateValid(templateCode), | |
| 158 | + assertServiceException(() -> notifySendService.validateNotifyTemplate(templateCode), | |
| 104 | 159 | NOTICE_NOT_FOUND); |
| 105 | 160 | } |
| 106 | 161 | |
| ... | ... | @@ -113,7 +168,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest { |
| 113 | 168 | // mock 方法 |
| 114 | 169 | |
| 115 | 170 | // 调用,并断言异常 |
| 116 | - assertServiceException(() -> notifySendService.checkTemplateParams(template, templateParams), | |
| 171 | + assertServiceException(() -> notifySendService.validateTemplateParams(template, templateParams), | |
| 117 | 172 | NOTIFY_SEND_TEMPLATE_PARAM_MISS, "code"); |
| 118 | 173 | } |
| 119 | 174 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyTemplateServiceImplTest.java
| ... | ... | @@ -14,6 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | 14 | import org.springframework.context.annotation.Import; |
| 15 | 15 | |
| 16 | 16 | import javax.annotation.Resource; |
| 17 | +import java.util.HashMap; | |
| 18 | +import java.util.Map; | |
| 17 | 19 | |
| 18 | 20 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
| 19 | 21 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| ... | ... | @@ -143,4 +145,44 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { |
| 143 | 145 | assertPojoEquals(dbNotifyTemplate, pageResult.getList().get(0)); |
| 144 | 146 | } |
| 145 | 147 | |
| 148 | + @Test | |
| 149 | + public void testGetNotifyTemplate() { | |
| 150 | + // mock 数据 | |
| 151 | + NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class); | |
| 152 | + notifyTemplateMapper.insert(dbNotifyTemplate); | |
| 153 | + // 准备参数 | |
| 154 | + Long id = dbNotifyTemplate.getId(); | |
| 155 | + | |
| 156 | + // 调用 | |
| 157 | + NotifyTemplateDO notifyTemplate = notifyTemplateService.getNotifyTemplate(id); | |
| 158 | + // 断言 | |
| 159 | + assertPojoEquals(dbNotifyTemplate, notifyTemplate); | |
| 160 | + } | |
| 161 | + | |
| 162 | + @Test | |
| 163 | + public void testGetNotifyTemplateByCodeFromCache() { | |
| 164 | + // mock 数据 | |
| 165 | + NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class); | |
| 166 | + notifyTemplateMapper.insert(dbNotifyTemplate); | |
| 167 | + notifyTemplateService.initLocalCache(); | |
| 168 | + // 准备参数 | |
| 169 | + String code = dbNotifyTemplate.getCode(); | |
| 170 | + | |
| 171 | + // 调用 | |
| 172 | + NotifyTemplateDO notifyTemplate = notifyTemplateService.getNotifyTemplateByCodeFromCache(code); | |
| 173 | + // 断言 | |
| 174 | + assertPojoEquals(dbNotifyTemplate, notifyTemplate); | |
| 175 | + } | |
| 176 | + | |
| 177 | + @Test | |
| 178 | + public void testFormatNotifyTemplateContent() { | |
| 179 | + // 准备参数 | |
| 180 | + Map<String, Object> params = new HashMap<>(); | |
| 181 | + params.put("name", "小红"); | |
| 182 | + params.put("what", "饭"); | |
| 183 | + | |
| 184 | + // 调用,并断言 | |
| 185 | + assertEquals("小红,你好,饭吃了吗?", | |
| 186 | + notifyTemplateService.formatNotifyTemplateContent("{name},你好,{what}吃了吗?", params)); | |
| 187 | + } | |
| 146 | 188 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2ClientServiceImplTest.java
| ... | ... | @@ -154,6 +154,19 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | @Test |
| 157 | + public void testGetOAuth2Client() { | |
| 158 | + // mock 数据 | |
| 159 | + OAuth2ClientDO clientDO = randomPojo(OAuth2ClientDO.class); | |
| 160 | + oauth2ClientMapper.insert(clientDO); | |
| 161 | + // 准备参数 | |
| 162 | + Long id = clientDO.getId(); | |
| 163 | + | |
| 164 | + // 调用,并断言 | |
| 165 | + OAuth2ClientDO dbClientDO = oauth2ClientService.getOAuth2Client(id); | |
| 166 | + assertPojoEquals(clientDO, dbClientDO); | |
| 167 | + } | |
| 168 | + | |
| 169 | + @Test | |
| 157 | 170 | public void testGetOAuth2ClientPage() { |
| 158 | 171 | // mock 数据 |
| 159 | 172 | OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class, o -> { // 等会查询到 |
| ... | ... | @@ -203,10 +216,13 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { |
| 203 | 216 | null, null, Collections.singleton(randomString()), null), OAUTH2_CLIENT_SCOPE_OVER); |
| 204 | 217 | assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", |
| 205 | 218 | null, null, null, "test"), OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, "test"); |
| 206 | - // 成功调用 | |
| 219 | + // 成功调用(1:参数完整) | |
| 207 | 220 | OAuth2ClientDO result = oauth2ClientService.validOAuthClientFromCache(client.getClientId(), client.getSecret(), |
| 208 | 221 | client.getAuthorizedGrantTypes().get(0), client.getScopes(), client.getRedirectUris().get(0)); |
| 209 | 222 | assertPojoEquals(client, result); |
| 223 | + // 成功调用(2:只有 clientId 参数) | |
| 224 | + result = oauth2ClientService.validOAuthClientFromCache(client.getClientId()); | |
| 225 | + assertPojoEquals(client, result); | |
| 210 | 226 | } |
| 211 | 227 | |
| 212 | 228 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImplTest.java
| ... | ... | @@ -16,6 +16,7 @@ import java.util.List; |
| 16 | 16 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 17 | 17 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 18 | 18 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 19 | +import static java.util.Collections.emptyList; | |
| 19 | 20 | import static org.junit.jupiter.api.Assertions.*; |
| 20 | 21 | import static org.mockito.ArgumentMatchers.eq; |
| 21 | 22 | import static org.mockito.Mockito.when; |
| ... | ... | @@ -135,6 +136,13 @@ public class OAuth2GrantServiceImplTest extends BaseMockitoUnitTest { |
| 135 | 136 | } |
| 136 | 137 | |
| 137 | 138 | @Test |
| 139 | + public void testGrantClientCredentials() { | |
| 140 | + assertThrows(UnsupportedOperationException.class, | |
| 141 | + () -> oauth2GrantService.grantClientCredentials(randomString(), emptyList()), | |
| 142 | + "暂时不支持 client_credentials 授权模式"); | |
| 143 | + } | |
| 144 | + | |
| 145 | + @Test | |
| 138 | 146 | public void testRevokeToken_clientIdError() { |
| 139 | 147 | // 准备参数 |
| 140 | 148 | String clientId = randomString(); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImplTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.service.permission; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | |
| 4 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 5 | +import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuCreateReqVO; | |
| 6 | +import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; | |
| 7 | +import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuUpdateReqVO; | |
| 8 | +import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; | |
| 9 | +import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper; | |
| 10 | +import cn.iocoder.yudao.module.system.enums.permission.MenuTypeEnum; | |
| 11 | +import cn.iocoder.yudao.module.system.mq.producer.permission.MenuProducer; | |
| 12 | +import cn.iocoder.yudao.module.system.service.tenant.TenantService; | |
| 13 | +import com.google.common.collect.LinkedListMultimap; | |
| 14 | +import com.google.common.collect.Multimap; | |
| 15 | +import org.junit.jupiter.api.Test; | |
| 16 | +import org.springframework.boot.test.mock.mockito.MockBean; | |
| 17 | +import org.springframework.context.annotation.Import; | |
| 18 | + | |
| 19 | +import javax.annotation.Resource; | |
| 20 | +import java.util.*; | |
| 21 | + | |
| 22 | +import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; | |
| 23 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 24 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | |
| 25 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | |
| 26 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | |
| 27 | +import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; | |
| 28 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 29 | +import static java.util.Arrays.asList; | |
| 30 | +import static java.util.Collections.singletonList; | |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNull; | |
| 33 | +import static org.mockito.ArgumentMatchers.argThat; | |
| 34 | +import static org.mockito.Mockito.doNothing; | |
| 35 | +import static org.mockito.Mockito.verify; | |
| 36 | + | |
| 37 | +@Import(MenuServiceImpl.class) | |
| 38 | +public class MenuServiceImplTest extends BaseDbUnitTest { | |
| 39 | + | |
| 40 | + @Resource | |
| 41 | + private MenuServiceImpl menuService; | |
| 42 | + | |
| 43 | + @Resource | |
| 44 | + private MenuMapper menuMapper; | |
| 45 | + | |
| 46 | + @MockBean | |
| 47 | + private PermissionService permissionService; | |
| 48 | + @MockBean | |
| 49 | + private MenuProducer menuProducer; | |
| 50 | + @MockBean | |
| 51 | + private TenantService tenantService; | |
| 52 | + | |
| 53 | + @Test | |
| 54 | + public void testInitLocalCache_success() { | |
| 55 | + MenuDO menuDO1 = randomPojo(MenuDO.class); | |
| 56 | + menuMapper.insert(menuDO1); | |
| 57 | + MenuDO menuDO2 = randomPojo(MenuDO.class); | |
| 58 | + menuMapper.insert(menuDO2); | |
| 59 | + | |
| 60 | + // 调用 | |
| 61 | + menuService.initLocalCache(); | |
| 62 | + // 校验 menuCache 缓存 | |
| 63 | + Map<Long, MenuDO> menuCache = menuService.getMenuCache(); | |
| 64 | + assertEquals(2, menuCache.size()); | |
| 65 | + assertPojoEquals(menuDO1, menuCache.get(menuDO1.getId())); | |
| 66 | + assertPojoEquals(menuDO2, menuCache.get(menuDO2.getId())); | |
| 67 | + // 校验 permissionMenuCache 缓存 | |
| 68 | + Multimap<String, MenuDO> permissionMenuCache = menuService.getPermissionMenuCache(); | |
| 69 | + assertEquals(2, permissionMenuCache.size()); | |
| 70 | + assertPojoEquals(menuDO1, permissionMenuCache.get(menuDO1.getPermission())); | |
| 71 | + assertPojoEquals(menuDO2, permissionMenuCache.get(menuDO2.getPermission())); | |
| 72 | + } | |
| 73 | + | |
| 74 | + @Test | |
| 75 | + public void testCreateMenu_success() { | |
| 76 | + // mock 数据(构造父菜单) | |
| 77 | + MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, | |
| 78 | + "parent", 0L); | |
| 79 | + menuMapper.insert(menuDO); | |
| 80 | + Long parentId = menuDO.getId(); | |
| 81 | + // 准备参数 | |
| 82 | + MenuCreateReqVO reqVO = randomPojo(MenuCreateReqVO.class, o -> { | |
| 83 | + o.setParentId(parentId); | |
| 84 | + o.setName("testSonName"); | |
| 85 | + o.setType(MenuTypeEnum.MENU.getType()); | |
| 86 | + }); | |
| 87 | + Long menuId = menuService.createMenu(reqVO); | |
| 88 | + | |
| 89 | + // 校验记录的属性是否正确 | |
| 90 | + MenuDO dbMenu = menuMapper.selectById(menuId); | |
| 91 | + assertPojoEquals(reqVO, dbMenu); | |
| 92 | + // 校验调用 | |
| 93 | + verify(menuProducer).sendMenuRefreshMessage(); | |
| 94 | + } | |
| 95 | + | |
| 96 | + @Test | |
| 97 | + public void testUpdateMenu_success() { | |
| 98 | + // mock 数据(构造父子菜单) | |
| 99 | + MenuDO sonMenuDO = initParentAndSonMenu(); | |
| 100 | + Long sonId = sonMenuDO.getId(); | |
| 101 | + // 准备参数 | |
| 102 | + MenuUpdateReqVO reqVO = randomPojo(MenuUpdateReqVO.class, o -> { | |
| 103 | + o.setId(sonId); | |
| 104 | + o.setName("testSonName"); // 修改名字 | |
| 105 | + o.setParentId(sonMenuDO.getParentId()); | |
| 106 | + o.setType(MenuTypeEnum.MENU.getType()); | |
| 107 | + }); | |
| 108 | + | |
| 109 | + // 调用 | |
| 110 | + menuService.updateMenu(reqVO); | |
| 111 | + // 校验记录的属性是否正确 | |
| 112 | + MenuDO dbMenu = menuMapper.selectById(sonId); | |
| 113 | + assertPojoEquals(reqVO, dbMenu); | |
| 114 | + // 校验调用 | |
| 115 | + verify(menuProducer).sendMenuRefreshMessage(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + @Test | |
| 119 | + public void testUpdateMenu_sonIdNotExist() { | |
| 120 | + // 准备参数 | |
| 121 | + MenuUpdateReqVO reqVO = randomPojo(MenuUpdateReqVO.class); | |
| 122 | + // 调用,并断言异常 | |
| 123 | + assertServiceException(() -> menuService.updateMenu(reqVO), MENU_NOT_EXISTS); | |
| 124 | + } | |
| 125 | + | |
| 126 | + @Test | |
| 127 | + public void testDeleteMenu_success() { | |
| 128 | + // mock 数据 | |
| 129 | + MenuDO menuDO = randomPojo(MenuDO.class); | |
| 130 | + menuMapper.insert(menuDO); | |
| 131 | + // 准备参数 | |
| 132 | + Long id = menuDO.getId(); | |
| 133 | + | |
| 134 | + // 调用 | |
| 135 | + menuService.deleteMenu(id); | |
| 136 | + // 断言 | |
| 137 | + MenuDO dbMenuDO = menuMapper.selectById(id); | |
| 138 | + assertNull(dbMenuDO); | |
| 139 | + verify(permissionService).processMenuDeleted(id); | |
| 140 | + verify(menuProducer).sendMenuRefreshMessage(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + @Test | |
| 144 | + public void testDeleteMenu_menuNotExist() { | |
| 145 | + assertServiceException(() -> menuService.deleteMenu(randomLongId()), | |
| 146 | + MENU_NOT_EXISTS); | |
| 147 | + } | |
| 148 | + | |
| 149 | + @Test | |
| 150 | + public void testDeleteMenu_existChildren() { | |
| 151 | + // mock 数据(构造父子菜单) | |
| 152 | + MenuDO sonMenu = initParentAndSonMenu(); | |
| 153 | + // 准备参数 | |
| 154 | + Long parentId = sonMenu.getParentId(); | |
| 155 | + | |
| 156 | + // 调用并断言异常 | |
| 157 | + assertServiceException(() -> menuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); | |
| 158 | + } | |
| 159 | + | |
| 160 | + @Test | |
| 161 | + public void testGetMenuList_all() { | |
| 162 | + // mock 数据 | |
| 163 | + MenuDO menu100 = randomPojo(MenuDO.class); | |
| 164 | + menuMapper.insert(menu100); | |
| 165 | + MenuDO menu101 = randomPojo(MenuDO.class); | |
| 166 | + menuMapper.insert(menu101); | |
| 167 | + // 准备参数 | |
| 168 | + | |
| 169 | + // 调用 | |
| 170 | + List<MenuDO> list = menuService.getMenuList(); | |
| 171 | + // 断言 | |
| 172 | + assertEquals(2, list.size()); | |
| 173 | + assertPojoEquals(menu100, list.get(0)); | |
| 174 | + assertPojoEquals(menu101, list.get(1)); | |
| 175 | + } | |
| 176 | + | |
| 177 | + @Test | |
| 178 | + public void testGetMenuList() { | |
| 179 | + // mock 数据 | |
| 180 | + MenuDO menuDO = randomPojo(MenuDO.class, o -> o.setName("芋艿").setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 181 | + menuMapper.insert(menuDO); | |
| 182 | + // 测试 status 不匹配 | |
| 183 | + menuMapper.insert(cloneIgnoreId(menuDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 184 | + // 测试 name 不匹配 | |
| 185 | + menuMapper.insert(cloneIgnoreId(menuDO, o -> o.setName("艿"))); | |
| 186 | + // 准备参数 | |
| 187 | + MenuListReqVO reqVO = new MenuListReqVO().setName("芋").setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 188 | + | |
| 189 | + // 调用 | |
| 190 | + List<MenuDO> result = menuService.getMenuList(reqVO); | |
| 191 | + // 断言 | |
| 192 | + assertEquals(1, result.size()); | |
| 193 | + assertPojoEquals(menuDO, result.get(0)); | |
| 194 | + } | |
| 195 | + | |
| 196 | + @Test | |
| 197 | + public void testGetMenuListByTenant() { | |
| 198 | + // mock 数据 | |
| 199 | + MenuDO menu100 = randomPojo(MenuDO.class, o -> o.setId(100L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 200 | + menuMapper.insert(menu100); | |
| 201 | + MenuDO menu101 = randomPojo(MenuDO.class, o -> o.setId(101L).setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 202 | + menuMapper.insert(menu101); | |
| 203 | + MenuDO menu102 = randomPojo(MenuDO.class, o -> o.setId(102L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 204 | + menuMapper.insert(menu102); | |
| 205 | + // mock 过滤菜单 | |
| 206 | + Set<Long> menuIds = asSet(100L, 101L); | |
| 207 | + doNothing().when(tenantService).handleTenantMenu(argThat(handler -> { | |
| 208 | + handler.handle(menuIds); | |
| 209 | + return true; | |
| 210 | + })); | |
| 211 | + // 准备参数 | |
| 212 | + MenuListReqVO reqVO = new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 213 | + | |
| 214 | + // 调用 | |
| 215 | + List<MenuDO> result = menuService.getMenuListByTenant(reqVO); | |
| 216 | + // 断言 | |
| 217 | + assertEquals(1, result.size()); | |
| 218 | + assertPojoEquals(menu100, result.get(0)); | |
| 219 | + } | |
| 220 | + | |
| 221 | + @Test | |
| 222 | + public void testListMenusFromCache_withoutId() { | |
| 223 | + // mock 缓存 | |
| 224 | + Map<Long, MenuDO> menuCache = new HashMap<>(); | |
| 225 | + // 可被匹配 | |
| 226 | + MenuDO menuDO = randomPojo(MenuDO.class, o -> o.setId(1L) | |
| 227 | + .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 228 | + menuCache.put(menuDO.getId(), menuDO); | |
| 229 | + // 测试 type 不匹配 | |
| 230 | + menuCache.put(3L, randomPojo(MenuDO.class, o -> o.setId(3L) | |
| 231 | + .setType(MenuTypeEnum.BUTTON.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus()))); | |
| 232 | + // 测试 status 不匹配 | |
| 233 | + menuCache.put(4L, randomPojo(MenuDO.class, o -> o.setId(4L) | |
| 234 | + .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 235 | + menuService.setMenuCache(menuCache); | |
| 236 | + // 准备参数 | |
| 237 | + Collection<Integer> menuTypes = singletonList(MenuTypeEnum.MENU.getType()); | |
| 238 | + Collection<Integer> menusStatuses = singletonList(CommonStatusEnum.ENABLE.getStatus()); | |
| 239 | + | |
| 240 | + // 调用 | |
| 241 | + List<MenuDO> list = menuService.getMenuListFromCache(menuTypes, menusStatuses); | |
| 242 | + // 断言 | |
| 243 | + assertEquals(1, list.size()); | |
| 244 | + assertPojoEquals(menuDO, list.get(0)); | |
| 245 | + } | |
| 246 | + | |
| 247 | + @Test | |
| 248 | + public void testListMenusFromCache_withId() { | |
| 249 | + // mock 缓存 | |
| 250 | + Map<Long, MenuDO> menuCache = new HashMap<>(); | |
| 251 | + // 可被匹配 | |
| 252 | + MenuDO menuDO = randomPojo(MenuDO.class, o -> o.setId(1L) | |
| 253 | + .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 254 | + menuCache.put(menuDO.getId(), menuDO); | |
| 255 | + // 测试 id 不匹配 | |
| 256 | + menuCache.put(2L, randomPojo(MenuDO.class, o -> o.setId(2L) | |
| 257 | + .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus()))); | |
| 258 | + // 测试 type 不匹配 | |
| 259 | + menuCache.put(3L, randomPojo(MenuDO.class, o -> o.setId(3L) | |
| 260 | + .setType(MenuTypeEnum.BUTTON.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus()))); | |
| 261 | + // 测试 status 不匹配 | |
| 262 | + menuCache.put(4L, randomPojo(MenuDO.class, o -> o.setId(4L) | |
| 263 | + .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 264 | + menuService.setMenuCache(menuCache); | |
| 265 | + // 准备参数 | |
| 266 | + Collection<Long> menuIds = asList(1L, 3L, 4L); | |
| 267 | + Collection<Integer> menuTypes = singletonList(MenuTypeEnum.MENU.getType()); | |
| 268 | + Collection<Integer> menusStatuses = singletonList(CommonStatusEnum.ENABLE.getStatus()); | |
| 269 | + | |
| 270 | + // 调用 | |
| 271 | + List<MenuDO> list = menuService.getMenuListFromCache(menuIds, menuTypes, menusStatuses); | |
| 272 | + // 断言 | |
| 273 | + assertEquals(1, list.size()); | |
| 274 | + assertPojoEquals(menuDO, list.get(0)); | |
| 275 | + } | |
| 276 | + | |
| 277 | + @Test | |
| 278 | + public void testGetMenuListByPermissionFromCache() { | |
| 279 | + // mock 缓存 | |
| 280 | + Multimap<String, MenuDO> permissionMenuCache = LinkedListMultimap.create(); | |
| 281 | + // 可被匹配 | |
| 282 | + MenuDO menuDO01 = randomPojo(MenuDO.class, o -> o.setId(1L).setPermission("123")); | |
| 283 | + permissionMenuCache.put(menuDO01.getPermission(), menuDO01); | |
| 284 | + MenuDO menuDO02 = randomPojo(MenuDO.class, o -> o.setId(2L).setPermission("123")); | |
| 285 | + permissionMenuCache.put(menuDO02.getPermission(), menuDO02); | |
| 286 | + // 不可匹配 | |
| 287 | + permissionMenuCache.put("456", randomPojo(MenuDO.class, o -> o.setId(3L).setPermission("456"))); | |
| 288 | + menuService.setPermissionMenuCache(permissionMenuCache); | |
| 289 | + // 准备参数 | |
| 290 | + String permission = "123"; | |
| 291 | + | |
| 292 | + // 调用 | |
| 293 | + List<MenuDO> list = menuService.getMenuListByPermissionFromCache(permission); | |
| 294 | + // 断言 | |
| 295 | + assertEquals(2, list.size()); | |
| 296 | + assertPojoEquals(menuDO01, list.get(0)); | |
| 297 | + assertPojoEquals(menuDO02, list.get(1)); | |
| 298 | + } | |
| 299 | + | |
| 300 | + @Test | |
| 301 | + public void testGetMenu() { | |
| 302 | + // mock 数据 | |
| 303 | + MenuDO menu = randomPojo(MenuDO.class); | |
| 304 | + menuMapper.insert(menu); | |
| 305 | + // 准备参数 | |
| 306 | + Long id = menu.getId(); | |
| 307 | + | |
| 308 | + // 调用 | |
| 309 | + MenuDO dbMenu = menuService.getMenu(id); | |
| 310 | + // 断言 | |
| 311 | + assertPojoEquals(menu, dbMenu); | |
| 312 | + } | |
| 313 | + | |
| 314 | + @Test | |
| 315 | + public void testValidateParentMenu_success() { | |
| 316 | + // mock 数据 | |
| 317 | + MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | |
| 318 | + menuMapper.insert(menuDO); | |
| 319 | + // 准备参数 | |
| 320 | + Long parentId = menuDO.getId(); | |
| 321 | + | |
| 322 | + // 调用,无需断言 | |
| 323 | + menuService.validateParentMenu(parentId, null); | |
| 324 | + } | |
| 325 | + | |
| 326 | + @Test | |
| 327 | + public void testValidateParentMenu_canNotSetSelfToBeParent() { | |
| 328 | + // 调用,并断言异常 | |
| 329 | + assertServiceException(() -> menuService.validateParentMenu(1L, 1L), | |
| 330 | + MENU_PARENT_ERROR); | |
| 331 | + } | |
| 332 | + | |
| 333 | + @Test | |
| 334 | + public void testValidateParentMenu_parentNotExist() { | |
| 335 | + // 调用,并断言异常 | |
| 336 | + assertServiceException(() -> menuService.validateParentMenu(randomLongId(), null), | |
| 337 | + MENU_PARENT_NOT_EXISTS); | |
| 338 | + } | |
| 339 | + | |
| 340 | + @Test | |
| 341 | + public void testValidateParentMenu_parentTypeError() { | |
| 342 | + // mock 数据 | |
| 343 | + MenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); | |
| 344 | + menuMapper.insert(menuDO); | |
| 345 | + // 准备参数 | |
| 346 | + Long parentId = menuDO.getId(); | |
| 347 | + | |
| 348 | + // 调用,并断言异常 | |
| 349 | + assertServiceException(() -> menuService.validateParentMenu(parentId, null), | |
| 350 | + MENU_PARENT_NOT_DIR_OR_MENU); | |
| 351 | + } | |
| 352 | + | |
| 353 | + @Test | |
| 354 | + public void testValidateMenu_success() { | |
| 355 | + // mock 父子菜单 | |
| 356 | + MenuDO sonMenu = initParentAndSonMenu(); | |
| 357 | + // 准备参数 | |
| 358 | + Long parentId = sonMenu.getParentId(); | |
| 359 | + Long otherSonMenuId = randomLongId(); | |
| 360 | + String otherSonMenuName = randomString(); | |
| 361 | + | |
| 362 | + // 调用,无需断言 | |
| 363 | + menuService.validateMenu(parentId, otherSonMenuName, otherSonMenuId); | |
| 364 | + } | |
| 365 | + | |
| 366 | + @Test | |
| 367 | + public void testValidateMenu_sonMenuNameDuplicate() { | |
| 368 | + // mock 父子菜单 | |
| 369 | + MenuDO sonMenu = initParentAndSonMenu(); | |
| 370 | + // 准备参数 | |
| 371 | + Long parentId = sonMenu.getParentId(); | |
| 372 | + Long otherSonMenuId = randomLongId(); | |
| 373 | + String otherSonMenuName = sonMenu.getName(); //相同名称 | |
| 374 | + | |
| 375 | + // 调用,并断言异常 | |
| 376 | + assertServiceException(() -> menuService.validateMenu(parentId, otherSonMenuName, otherSonMenuId), | |
| 377 | + MENU_NAME_DUPLICATE); | |
| 378 | + } | |
| 379 | + | |
| 380 | + // ====================== 初始化方法 ====================== | |
| 381 | + | |
| 382 | + /** | |
| 383 | + * 构造父子菜单,返回子菜单 | |
| 384 | + * | |
| 385 | + * @return 子菜单 | |
| 386 | + */ | |
| 387 | + private MenuDO initParentAndSonMenu() { | |
| 388 | + // 构造父子菜单 | |
| 389 | + MenuDO parentMenuDO = createMenuDO(MenuTypeEnum.MENU, "parent", ID_ROOT); | |
| 390 | + menuMapper.insert(parentMenuDO); | |
| 391 | + // 构建子菜单 | |
| 392 | + MenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "testSonName", | |
| 393 | + parentMenuDO.getParentId()); | |
| 394 | + menuMapper.insert(sonMenuDO); | |
| 395 | + return sonMenuDO; | |
| 396 | + } | |
| 397 | + | |
| 398 | + private MenuDO createMenuDO(MenuTypeEnum type, String name, Long parentId) { | |
| 399 | + return createMenuDO(type, name, parentId, randomCommonStatus()); | |
| 400 | + } | |
| 401 | + | |
| 402 | + private MenuDO createMenuDO(MenuTypeEnum type, String name, Long parentId, Integer status) { | |
| 403 | + return randomPojo(MenuDO.class, o -> o.setId(null).setName(name).setParentId(parentId) | |
| 404 | + .setType(type.getType()).setStatus(status)); | |
| 405 | + } | |
| 406 | + | |
| 407 | +} | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceTest.java deleted
| 1 | -package cn.iocoder.yudao.module.system.service.permission; | |
| 2 | - | |
| 3 | -import cn.hutool.core.bean.BeanUtil; | |
| 4 | -import cn.hutool.core.lang.Assert; | |
| 5 | -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | |
| 6 | -import cn.iocoder.yudao.framework.common.util.spring.SpringAopUtils; | |
| 7 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 8 | -import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuCreateReqVO; | |
| 9 | -import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; | |
| 10 | -import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuUpdateReqVO; | |
| 11 | -import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; | |
| 12 | -import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper; | |
| 13 | -import cn.iocoder.yudao.module.system.enums.permission.MenuTypeEnum; | |
| 14 | -import cn.iocoder.yudao.module.system.mq.producer.permission.MenuProducer; | |
| 15 | -import cn.iocoder.yudao.module.system.service.tenant.TenantService; | |
| 16 | -import com.google.common.collect.Multimap; | |
| 17 | -import org.junit.jupiter.api.Test; | |
| 18 | -import org.springframework.boot.test.mock.mockito.MockBean; | |
| 19 | -import org.springframework.context.annotation.Import; | |
| 20 | - | |
| 21 | -import javax.annotation.Resource; | |
| 22 | -import java.util.*; | |
| 23 | - | |
| 24 | -import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; | |
| 25 | -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | |
| 26 | -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | |
| 27 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | |
| 28 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 29 | -import static org.junit.jupiter.api.Assertions.*; | |
| 30 | -import static org.mockito.ArgumentMatchers.argThat; | |
| 31 | -import static org.mockito.Mockito.doNothing; | |
| 32 | -import static org.mockito.Mockito.verify; | |
| 33 | - | |
| 34 | -// TODO @芋艿:单测的代码质量可以提升下 | |
| 35 | -@Import(MenuServiceImpl.class) | |
| 36 | -public class MenuServiceTest extends BaseDbUnitTest { | |
| 37 | - | |
| 38 | - @Resource | |
| 39 | - private MenuServiceImpl menuService; | |
| 40 | - | |
| 41 | - @Resource | |
| 42 | - private MenuMapper menuMapper; | |
| 43 | - | |
| 44 | - @MockBean | |
| 45 | - private PermissionService permissionService; | |
| 46 | - @MockBean | |
| 47 | - private MenuProducer menuProducer; | |
| 48 | - @MockBean | |
| 49 | - private TenantService tenantService; | |
| 50 | - | |
| 51 | - @Test | |
| 52 | - public void testInitLocalCache_success() { | |
| 53 | - MenuDO menuDO1 = randomPojo(MenuDO.class); | |
| 54 | - menuMapper.insert(menuDO1); | |
| 55 | - MenuDO menuDO2 = randomPojo(MenuDO.class); | |
| 56 | - menuMapper.insert(menuDO2); | |
| 57 | - | |
| 58 | - // 调用 | |
| 59 | - menuService.initLocalCache(); | |
| 60 | - // 校验 menuCache 缓存 | |
| 61 | - Map<Long, MenuDO> menuCache = menuService.getMenuCache(); | |
| 62 | - Assert.isTrue(menuCache.size() == 2); | |
| 63 | - assertPojoEquals(menuDO1, menuCache.get(menuDO1.getId())); | |
| 64 | - assertPojoEquals(menuDO2, menuCache.get(menuDO2.getId())); | |
| 65 | - // 校验 permissionMenuCache 缓存 | |
| 66 | - Multimap<String, MenuDO> permissionMenuCache = menuService.getPermissionMenuCache(); | |
| 67 | - Assert.isTrue(permissionMenuCache.size() == 2); | |
| 68 | - assertPojoEquals(menuDO1, permissionMenuCache.get(menuDO1.getPermission())); | |
| 69 | - assertPojoEquals(menuDO2, permissionMenuCache.get(menuDO2.getPermission())); | |
| 70 | - } | |
| 71 | - | |
| 72 | - @Test | |
| 73 | - public void testCreateMenu_success() { | |
| 74 | - //构造父目录 | |
| 75 | - MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | |
| 76 | - menuMapper.insert(menuDO); | |
| 77 | - Long parentId = menuDO.getId(); | |
| 78 | - | |
| 79 | - //调用 | |
| 80 | - MenuCreateReqVO vo = randomPojo(MenuCreateReqVO.class, o -> { | |
| 81 | - o.setParentId(parentId); | |
| 82 | - o.setName("testSonName"); | |
| 83 | - o.setType(MenuTypeEnum.MENU.getType()); | |
| 84 | - o.setStatus(randomCommonStatus()); | |
| 85 | - }); | |
| 86 | - Long menuId = menuService.createMenu(vo); | |
| 87 | - | |
| 88 | - //断言 | |
| 89 | - assertNotNull(menuId); | |
| 90 | - // 校验记录的属性是否正确 | |
| 91 | - MenuDO ret = menuMapper.selectById(menuId); | |
| 92 | - assertPojoEquals(vo, ret); | |
| 93 | - // 校验调用 | |
| 94 | - verify(menuProducer).sendMenuRefreshMessage(); | |
| 95 | - } | |
| 96 | - | |
| 97 | - @Test | |
| 98 | - public void testUpdateMenu_success() { | |
| 99 | - //构造父子目录 | |
| 100 | - MenuDO sonMenuDO = initParentAndSonMenuDO(); | |
| 101 | - Long sonId = sonMenuDO.getId(); | |
| 102 | - Long parentId = sonMenuDO.getParentId(); | |
| 103 | - | |
| 104 | - //调用 | |
| 105 | - MenuUpdateReqVO vo = randomPojo(MenuUpdateReqVO.class, o -> { | |
| 106 | - o.setId(sonId); | |
| 107 | - o.setParentId(parentId); | |
| 108 | - o.setType(MenuTypeEnum.MENU.getType()); | |
| 109 | - o.setStatus(randomCommonStatus()); | |
| 110 | - o.setName("pppppp"); //修改名字 | |
| 111 | - }); | |
| 112 | - menuService.updateMenu(vo); | |
| 113 | - | |
| 114 | - //断言 | |
| 115 | - // 校验记录的属性是否正确 | |
| 116 | - MenuDO ret = menuMapper.selectById(sonId); | |
| 117 | - assertPojoEquals(vo, ret); | |
| 118 | - // 校验调用 | |
| 119 | - verify(menuProducer).sendMenuRefreshMessage(); | |
| 120 | - } | |
| 121 | - | |
| 122 | - @Test | |
| 123 | - public void testUpdateMenu_sonIdNotExist() { | |
| 124 | - Long sonId = 99999L; | |
| 125 | - Long parentId = 10000L; | |
| 126 | - | |
| 127 | - //调用 | |
| 128 | - MenuUpdateReqVO vo = randomPojo(MenuUpdateReqVO.class, o -> { | |
| 129 | - o.setId(sonId); | |
| 130 | - o.setParentId(parentId); | |
| 131 | - o.setType(MenuTypeEnum.MENU.getType()); | |
| 132 | - o.setStatus(randomCommonStatus()); | |
| 133 | - }); | |
| 134 | - //断言 | |
| 135 | - assertServiceException(() -> menuService.updateMenu(vo), MENU_NOT_EXISTS); | |
| 136 | - } | |
| 137 | - | |
| 138 | - @Test | |
| 139 | - public void testDeleteMenu_success() { | |
| 140 | - MenuDO sonMenuDO = initParentAndSonMenuDO(); | |
| 141 | - Long sonId = sonMenuDO.getId(); | |
| 142 | - | |
| 143 | - // 调用 | |
| 144 | - menuService.deleteMenu(sonId); | |
| 145 | - | |
| 146 | - // 断言 | |
| 147 | - MenuDO menuDO = menuMapper.selectById(sonId); | |
| 148 | - assertNull(menuDO); | |
| 149 | - verify(permissionService).processMenuDeleted(sonId); | |
| 150 | - verify(menuProducer).sendMenuRefreshMessage(); | |
| 151 | - } | |
| 152 | - | |
| 153 | - @Test | |
| 154 | - public void testDeleteMenu_menuNotExist() { | |
| 155 | - Long sonId = 99999L; | |
| 156 | - | |
| 157 | - assertServiceException(() -> menuService.deleteMenu(sonId), MENU_NOT_EXISTS); | |
| 158 | - } | |
| 159 | - | |
| 160 | - @Test | |
| 161 | - public void testDeleteMenu_existChildren() { | |
| 162 | - MenuDO sonMenu = initParentAndSonMenuDO(); | |
| 163 | - Long parentId = sonMenu.getParentId(); | |
| 164 | - | |
| 165 | - assertServiceException(() -> menuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); | |
| 166 | - } | |
| 167 | - | |
| 168 | - @Test | |
| 169 | - public void testGetMenus() { | |
| 170 | - // mock 数据 | |
| 171 | - MenuDO menu100 = randomPojo(MenuDO.class, o -> o.setId(100L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 172 | - menuMapper.insert(menu100); | |
| 173 | - MenuDO menu101 = randomPojo(MenuDO.class, o -> o.setId(101L).setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 174 | - menuMapper.insert(menu101); | |
| 175 | - // 准备参数 | |
| 176 | - MenuListReqVO reqVO = new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 177 | - | |
| 178 | - // 调用 | |
| 179 | - List<MenuDO> result = menuService.getMenus(reqVO); | |
| 180 | - // 断言 | |
| 181 | - assertEquals(1, result.size()); | |
| 182 | - assertPojoEquals(menu100, result.get(0)); | |
| 183 | - } | |
| 184 | - | |
| 185 | - @Test | |
| 186 | - public void testTenantMenus() { | |
| 187 | - // mock 数据 | |
| 188 | - MenuDO menu100 = randomPojo(MenuDO.class, o -> o.setId(100L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 189 | - menuMapper.insert(menu100); | |
| 190 | - MenuDO menu101 = randomPojo(MenuDO.class, o -> o.setId(101L).setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 191 | - menuMapper.insert(menu101); | |
| 192 | - MenuDO menu102 = randomPojo(MenuDO.class, o -> o.setId(102L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 193 | - menuMapper.insert(menu102); | |
| 194 | - // mock 过滤菜单 | |
| 195 | - // mock 账户额度充足 | |
| 196 | - Set<Long> menuIds = asSet(100L, 101L); | |
| 197 | - doNothing().when(tenantService).handleTenantMenu(argThat(handler -> { | |
| 198 | - handler.handle(menuIds); | |
| 199 | - return true; | |
| 200 | - })); | |
| 201 | - // 准备参数 | |
| 202 | - MenuListReqVO reqVO = new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 203 | - | |
| 204 | - // 调用 | |
| 205 | - List<MenuDO> result = menuService.getTenantMenus(reqVO); | |
| 206 | - // 断言 | |
| 207 | - assertEquals(1, result.size()); | |
| 208 | - assertPojoEquals(menu100, result.get(0)); | |
| 209 | - } | |
| 210 | - | |
| 211 | - @Test | |
| 212 | - public void testGetMenusReqVo_success() { | |
| 213 | - Map<Long, MenuDO> idMenuMap = new HashMap<>(); | |
| 214 | - // 用于验证可以模糊搜索名称包含"name",状态为1的menu | |
| 215 | - MenuDO menu = createMenuDO(MenuTypeEnum.MENU, "name2", 0L, 1); | |
| 216 | - menuMapper.insert(menu); | |
| 217 | - idMenuMap.put(menu.getId(), menu); | |
| 218 | - | |
| 219 | - menu = createMenuDO(MenuTypeEnum.MENU, "11name111", 0L, 1); | |
| 220 | - menuMapper.insert(menu); | |
| 221 | - idMenuMap.put(menu.getId(), menu); | |
| 222 | - | |
| 223 | - menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 1); | |
| 224 | - menuMapper.insert(menu); | |
| 225 | - idMenuMap.put(menu.getId(), menu); | |
| 226 | - | |
| 227 | - // 以下是不符合搜索条件的的menu | |
| 228 | - menu = createMenuDO(MenuTypeEnum.MENU, "xxxxxx", 0L, 1); | |
| 229 | - menuMapper.insert(menu); | |
| 230 | - menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 2); | |
| 231 | - menuMapper.insert(menu); | |
| 232 | - | |
| 233 | - // 调用 | |
| 234 | - MenuListReqVO reqVO = new MenuListReqVO(); | |
| 235 | - reqVO.setStatus(1); | |
| 236 | - reqVO.setName("name"); | |
| 237 | - List<MenuDO> menuDOS = menuService.getMenus(reqVO); | |
| 238 | - | |
| 239 | - // 断言 | |
| 240 | - assertEquals(menuDOS.size(), idMenuMap.size()); | |
| 241 | - menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | |
| 242 | - } | |
| 243 | - | |
| 244 | - @Test | |
| 245 | - public void testListMenusFromCache_success() throws Exception { | |
| 246 | - Map<Long, MenuDO> mockCacheMap = new HashMap<>(); | |
| 247 | - // 获取代理对象 | |
| 248 | - MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | |
| 249 | - BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); | |
| 250 | - | |
| 251 | - Map<Long, MenuDO> idMenuMap = new HashMap<>(); | |
| 252 | - // 用于验证搜索类型为MENU,状态为1的menu | |
| 253 | - MenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); | |
| 254 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 255 | - idMenuMap.put(menuDO.getId(), menuDO); | |
| 256 | - | |
| 257 | - menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1); | |
| 258 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 259 | - idMenuMap.put(menuDO.getId(), menuDO); | |
| 260 | - | |
| 261 | - // 以下是不符合搜索条件的menu | |
| 262 | - menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); | |
| 263 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 264 | - menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); | |
| 265 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 266 | - | |
| 267 | - List<MenuDO> menuDOS = menuService.getMenuListFromCache(Collections.singletonList(MenuTypeEnum.MENU.getType()), | |
| 268 | - Collections.singletonList(CommonStatusEnum.DISABLE.getStatus())); | |
| 269 | - assertEquals(menuDOS.size(), idMenuMap.size()); | |
| 270 | - menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | |
| 271 | - } | |
| 272 | - | |
| 273 | - @Test | |
| 274 | - public void testListMenusFromCache2_success() throws Exception { | |
| 275 | - Map<Long, MenuDO> mockCacheMap = new HashMap<>(); | |
| 276 | - // 获取代理对象 | |
| 277 | - MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | |
| 278 | - BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); | |
| 279 | - | |
| 280 | - Map<Long, MenuDO> idMenuMap = new HashMap<>(); | |
| 281 | - // 验证搜索id为1, 类型为MENU, 状态为1 的menu | |
| 282 | - MenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); | |
| 283 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 284 | - idMenuMap.put(menuDO.getId(), menuDO); | |
| 285 | - | |
| 286 | - // 以下是不符合搜索条件的menu | |
| 287 | - menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1); | |
| 288 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 289 | - menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); | |
| 290 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 291 | - menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); | |
| 292 | - mockCacheMap.put(menuDO.getId(), menuDO); | |
| 293 | - | |
| 294 | - List<MenuDO> menuDOS = menuService.getMenuListFromCache(Collections.singletonList(1L), | |
| 295 | - Collections.singletonList(MenuTypeEnum.MENU.getType()), Collections.singletonList(1)); | |
| 296 | - assertEquals(menuDOS.size(), idMenuMap.size()); | |
| 297 | - menuDOS.forEach(menu -> assertPojoEquals(idMenuMap.get(menu.getId()), menu)); | |
| 298 | - } | |
| 299 | - | |
| 300 | - @Test | |
| 301 | - public void testCheckParentResource_success() { | |
| 302 | - MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | |
| 303 | - menuMapper.insert(menuDO); | |
| 304 | - Long parentId = menuDO.getId(); | |
| 305 | - | |
| 306 | - menuService.checkParentResource(parentId, null); | |
| 307 | - } | |
| 308 | - | |
| 309 | - @Test | |
| 310 | - public void testCheckParentResource_canNotSetSelfToBeParent() { | |
| 311 | - assertServiceException(() -> menuService.checkParentResource(1L, 1L), MENU_PARENT_ERROR); | |
| 312 | - } | |
| 313 | - | |
| 314 | - @Test | |
| 315 | - public void testCheckParentResource_parentNotExist() { | |
| 316 | - assertServiceException(() -> menuService.checkParentResource(randomLongId(), null), MENU_PARENT_NOT_EXISTS); | |
| 317 | - } | |
| 318 | - | |
| 319 | - @Test | |
| 320 | - public void testCheckParentResource_parentTypeError() { | |
| 321 | - MenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); | |
| 322 | - menuMapper.insert(menuDO); | |
| 323 | - Long parentId = menuDO.getId(); | |
| 324 | - | |
| 325 | - assertServiceException(() -> menuService.checkParentResource(parentId, null), MENU_PARENT_NOT_DIR_OR_MENU); | |
| 326 | - } | |
| 327 | - | |
| 328 | - @Test | |
| 329 | - public void testCheckResource_success() { | |
| 330 | - MenuDO sonMenu = initParentAndSonMenuDO(); | |
| 331 | - Long parentId = sonMenu.getParentId(); | |
| 332 | - | |
| 333 | - Long otherSonMenuId = randomLongId(); | |
| 334 | - String otherSonMenuName = randomString(); | |
| 335 | - | |
| 336 | - menuService.checkResource(parentId, otherSonMenuName, otherSonMenuId); | |
| 337 | - } | |
| 338 | - | |
| 339 | - @Test | |
| 340 | - public void testCheckResource_sonMenuNameDuplicate(){ | |
| 341 | - MenuDO sonMenu=initParentAndSonMenuDO(); | |
| 342 | - Long parentId=sonMenu.getParentId(); | |
| 343 | - | |
| 344 | - Long otherSonMenuId=randomLongId(); | |
| 345 | - String otherSonMenuName=sonMenu.getName(); //相同名称 | |
| 346 | - | |
| 347 | - assertServiceException(() -> menuService.checkResource(parentId, otherSonMenuName, otherSonMenuId), MENU_NAME_DUPLICATE); | |
| 348 | - } | |
| 349 | - | |
| 350 | - /** | |
| 351 | - * 构造父子目录,返回子目录 | |
| 352 | - * | |
| 353 | - * @return | |
| 354 | - */ | |
| 355 | - private MenuDO initParentAndSonMenuDO() { | |
| 356 | - //构造父子目录 | |
| 357 | - MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | |
| 358 | - menuMapper.insert(menuDO); | |
| 359 | - Long parentId = menuDO.getId(); | |
| 360 | - | |
| 361 | - MenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "testSonName", parentId); | |
| 362 | - menuMapper.insert(sonMenuDO); | |
| 363 | - return sonMenuDO; | |
| 364 | - } | |
| 365 | - | |
| 366 | - private MenuDO createMenuDO(MenuTypeEnum typeEnum, String menuName, Long parentId) { | |
| 367 | - return createMenuDO(typeEnum, menuName, parentId, randomCommonStatus()); | |
| 368 | - } | |
| 369 | - | |
| 370 | - private MenuDO createMenuDO(MenuTypeEnum typeEnum, String menuName, Long parentId, Integer status) { | |
| 371 | - return createMenuDO(null, typeEnum, menuName, parentId, status); | |
| 372 | - } | |
| 373 | - | |
| 374 | - private MenuDO createMenuDO(Long id, MenuTypeEnum typeEnum, String menuName, Long parentId, Integer status) { | |
| 375 | - return randomPojo(MenuDO.class, o -> { | |
| 376 | - o.setId(id); | |
| 377 | - o.setParentId(parentId); | |
| 378 | - o.setType(typeEnum.getType()); | |
| 379 | - o.setStatus(status); | |
| 380 | - o.setName(menuName); | |
| 381 | - }); | |
| 382 | - } | |
| 383 | - | |
| 384 | -} |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java
| ... | ... | @@ -106,7 +106,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 106 | 106 | Collection<Integer> menusStatuses = asList(0, 1); |
| 107 | 107 | // mock 方法 |
| 108 | 108 | List<RoleDO> roleList = singletonList(randomPojo(RoleDO.class, o -> o.setId(100L))); |
| 109 | - when(roleService.getRolesFromCache(eq(roleIds))).thenReturn(roleList); | |
| 109 | + when(roleService.getRoleListFromCache(eq(roleIds))).thenReturn(roleList); | |
| 110 | 110 | when(roleService.hasAnySuperAdmin(same(roleList))).thenReturn(true); |
| 111 | 111 | List<MenuDO> menuList = randomPojoList(MenuDO.class); |
| 112 | 112 | when(menuService.getMenuListFromCache(eq(menuTypes), eq(menusStatuses))).thenReturn(menuList); |
| ... | ... | @@ -163,7 +163,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 163 | 163 | // mock 方法 |
| 164 | 164 | when(roleService.hasAnySuperAdmin(eq(singleton(100L)))).thenReturn(true); |
| 165 | 165 | List<MenuDO> menuList = singletonList(randomPojo(MenuDO.class).setId(1L)); |
| 166 | - when(menuService.getMenus()).thenReturn(menuList); | |
| 166 | + when(menuService.getMenuList()).thenReturn(menuList); | |
| 167 | 167 | |
| 168 | 168 | // 调用 |
| 169 | 169 | Set<Long> menuIds = permissionService.getRoleMenuIds(roleId); |
| ... | ... | @@ -419,7 +419,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 419 | 419 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 420 | 420 | when(roleService.getRoleFromCache(eq(100L))).thenReturn(role); |
| 421 | 421 | // mock 其它方法 |
| 422 | - when(roleService.getRolesFromCache(eq(asSet(100L)))).thenReturn(singletonList(role)); | |
| 422 | + when(roleService.getRoleListFromCache(eq(asSet(100L)))).thenReturn(singletonList(role)); | |
| 423 | 423 | |
| 424 | 424 | // 调用 |
| 425 | 425 | boolean has = permissionService.hasAnyRoles(userId, roles); |
| ... | ... | @@ -436,7 +436,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 436 | 436 | // mock 获得用户的角色 |
| 437 | 437 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope()) |
| 438 | 438 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 439 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 439 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 440 | 440 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 441 | 441 | |
| 442 | 442 | // 调用 |
| ... | ... | @@ -456,7 +456,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 456 | 456 | // mock 获得用户的角色 |
| 457 | 457 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope()) |
| 458 | 458 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 459 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 459 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 460 | 460 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 461 | 461 | // mock 部门的返回 |
| 462 | 462 | when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 |
| ... | ... | @@ -480,7 +480,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 480 | 480 | // mock 获得用户的角色 |
| 481 | 481 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope()) |
| 482 | 482 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 483 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 483 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 484 | 484 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 485 | 485 | // mock 部门的返回 |
| 486 | 486 | when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 |
| ... | ... | @@ -503,13 +503,13 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 503 | 503 | // mock 获得用户的角色 |
| 504 | 504 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope()) |
| 505 | 505 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 506 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 506 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 507 | 507 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 508 | 508 | // mock 部门的返回 |
| 509 | 509 | when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 |
| 510 | 510 | // mock 方法(部门) |
| 511 | 511 | DeptDO deptDO = randomPojo(DeptDO.class); |
| 512 | - when(deptService.getDeptsByParentIdFromCache(eq(3L), eq(true))) | |
| 512 | + when(deptService.getDeptListByParentIdFromCache(eq(3L), eq(true))) | |
| 513 | 513 | .thenReturn(singletonList(deptDO)); |
| 514 | 514 | |
| 515 | 515 | // 调用 |
| ... | ... | @@ -531,7 +531,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 531 | 531 | // mock 获得用户的角色 |
| 532 | 532 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope()) |
| 533 | 533 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 534 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 534 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 535 | 535 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 536 | 536 | |
| 537 | 537 | // 调用 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.permission; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.util.RandomUtil; | |
| 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.test.core.ut.BaseDbUnitTest; |
| ... | ... | @@ -18,21 +17,26 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 18 | 17 | import org.springframework.context.annotation.Import; |
| 19 | 18 | |
| 20 | 19 | import javax.annotation.Resource; |
| 21 | -import java.time.LocalDateTime; | |
| 22 | -import java.util.*; | |
| 20 | +import java.util.Collection; | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.Map; | |
| 23 | +import java.util.Set; | |
| 23 | 24 | |
| 25 | +import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 26 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 24 | 27 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 25 | 28 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| 26 | 29 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 27 | 30 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 28 | 31 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 29 | 32 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 33 | +import static java.util.Collections.singleton; | |
| 34 | +import static java.util.Collections.singletonList; | |
| 30 | 35 | import static org.junit.jupiter.api.Assertions.*; |
| 31 | 36 | import static org.mockito.Mockito.verify; |
| 32 | 37 | |
| 33 | -// TODO @芋艿:单测的代码质量可以提升下 | |
| 34 | 38 | @Import(RoleServiceImpl.class) |
| 35 | -public class RoleServiceTest extends BaseDbUnitTest { | |
| 39 | +public class RoleServiceImplTest extends BaseDbUnitTest { | |
| 36 | 40 | |
| 37 | 41 | @Resource |
| 38 | 42 | private RoleServiceImpl roleService; |
| ... | ... | @@ -47,9 +51,9 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 47 | 51 | |
| 48 | 52 | @Test |
| 49 | 53 | public void testInitLocalCache() { |
| 50 | - RoleDO roleDO1 = randomRole(); | |
| 54 | + RoleDO roleDO1 = randomPojo(RoleDO.class); | |
| 51 | 55 | roleMapper.insert(roleDO1); |
| 52 | - RoleDO roleDO2 = randomRole(); | |
| 56 | + RoleDO roleDO2 = randomPojo(RoleDO.class); | |
| 53 | 57 | roleMapper.insert(roleDO2); |
| 54 | 58 | |
| 55 | 59 | // 调用 |
| ... | ... | @@ -68,7 +72,6 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 68 | 72 | // 调用 |
| 69 | 73 | Long roleId = roleService.createRole(reqVO, null); |
| 70 | 74 | // 断言 |
| 71 | - assertNotNull(roleId); | |
| 72 | 75 | RoleDO roleDO = roleMapper.selectById(roleId); |
| 73 | 76 | assertPojoEquals(reqVO, roleDO); |
| 74 | 77 | assertEquals(RoleTypeEnum.CUSTOM.getType(), roleDO.getType()); |
| ... | ... | @@ -81,81 +84,158 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 81 | 84 | @Test |
| 82 | 85 | public void testUpdateRole_success() { |
| 83 | 86 | // mock 数据 |
| 84 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 87 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | |
| 85 | 88 | roleMapper.insert(roleDO); |
| 86 | - Long roleId = roleDO.getId(); | |
| 89 | + // 准备参数 | |
| 90 | + Long id = roleDO.getId(); | |
| 91 | + RoleUpdateReqVO reqVO = randomPojo(RoleUpdateReqVO.class, o -> o.setId(id)); | |
| 87 | 92 | |
| 88 | - //调用 | |
| 89 | - RoleUpdateReqVO reqVO = randomPojo(RoleUpdateReqVO.class, o -> { | |
| 90 | - o.setId(roleId); | |
| 91 | - o.setCode("role_code"); | |
| 92 | - o.setName("update_name"); | |
| 93 | - o.setSort(999); | |
| 94 | - }); | |
| 93 | + // 调用 | |
| 95 | 94 | roleService.updateRole(reqVO); |
| 96 | - | |
| 97 | - //断言 | |
| 98 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 95 | + // 断言 | |
| 96 | + RoleDO newRoleDO = roleMapper.selectById(id); | |
| 99 | 97 | assertPojoEquals(reqVO, newRoleDO); |
| 100 | - | |
| 98 | + // verify 发送刷新消息 | |
| 101 | 99 | verify(roleProducer).sendRoleRefreshMessage(); |
| 102 | 100 | } |
| 103 | 101 | |
| 104 | 102 | @Test |
| 105 | 103 | public void testUpdateRoleStatus_success() { |
| 106 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL, CommonStatusEnum.ENABLE.getStatus()); | |
| 104 | + // mock 数据 | |
| 105 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()) | |
| 106 | + .setType(RoleTypeEnum.CUSTOM.getType())); | |
| 107 | 107 | roleMapper.insert(roleDO); |
| 108 | + | |
| 109 | + // 准备参数 | |
| 108 | 110 | Long roleId = roleDO.getId(); |
| 109 | 111 | |
| 110 | - //调用 | |
| 112 | + // 调用 | |
| 111 | 113 | roleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus()); |
| 112 | - | |
| 113 | - //断言 | |
| 114 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 115 | - assertEquals(CommonStatusEnum.DISABLE.getStatus(), newRoleDO.getStatus()); | |
| 116 | - | |
| 114 | + // 断言 | |
| 115 | + RoleDO dbRoleDO = roleMapper.selectById(roleId); | |
| 116 | + assertEquals(CommonStatusEnum.DISABLE.getStatus(), dbRoleDO.getStatus()); | |
| 117 | + // verify 发送刷新消息 | |
| 117 | 118 | verify(roleProducer).sendRoleRefreshMessage(); |
| 118 | 119 | } |
| 119 | 120 | |
| 120 | 121 | @Test |
| 121 | 122 | public void testUpdateRoleDataScope_success() { |
| 122 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 123 | + // mock 数据 | |
| 124 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | |
| 123 | 125 | roleMapper.insert(roleDO); |
| 124 | - Long roleId = roleDO.getId(); | |
| 125 | - | |
| 126 | - //调用 | |
| 127 | - Set<Long> deptIdSet = new HashSet<>(Arrays.asList(1L, 2L, 3L, 4L, 5L)); | |
| 128 | - roleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet); | |
| 126 | + // 准备参数 | |
| 127 | + Long id = roleDO.getId(); | |
| 128 | + Integer dataScope = randomEle(DataScopeEnum.values()).getScope(); | |
| 129 | + Set<Long> dataScopeRoleIds = randomSet(Long.class); | |
| 129 | 130 | |
| 130 | - //断言 | |
| 131 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 132 | - assertEquals(DataScopeEnum.DEPT_CUSTOM.getScope(), newRoleDO.getDataScope()); | |
| 131 | + // 调用 | |
| 132 | + roleService.updateRoleDataScope(id, dataScope, dataScopeRoleIds); | |
| 133 | + // 断言 | |
| 134 | + RoleDO dbRoleDO = roleMapper.selectById(id); | |
| 135 | + assertEquals(dataScope, dbRoleDO.getDataScope()); | |
| 136 | + assertEquals(dataScopeRoleIds, dbRoleDO.getDataScopeDeptIds()); | |
| 137 | + // verify 发送刷新消息 | |
| 138 | + verify(roleProducer).sendRoleRefreshMessage(); | |
| 139 | + } | |
| 133 | 140 | |
| 134 | - Set<Long> newDeptIdSet = newRoleDO.getDataScopeDeptIds(); | |
| 135 | - assertEquals(deptIdSet.size(), newDeptIdSet.size()); | |
| 136 | - deptIdSet.stream().forEach(d -> assertTrue(newDeptIdSet.contains(d))); | |
| 141 | + @Test | |
| 142 | + public void testDeleteRole_success() { | |
| 143 | + // mock 数据 | |
| 144 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | |
| 145 | + roleMapper.insert(roleDO); | |
| 146 | + // 参数准备 | |
| 147 | + Long id = roleDO.getId(); | |
| 137 | 148 | |
| 149 | + // 调用 | |
| 150 | + roleService.deleteRole(id); | |
| 151 | + // 断言 | |
| 152 | + assertNull(roleMapper.selectById(id)); | |
| 153 | + // verify 删除相关数据 | |
| 154 | + verify(permissionService).processRoleDeleted(id); | |
| 155 | + // verify 发送刷新消息 | |
| 138 | 156 | verify(roleProducer).sendRoleRefreshMessage(); |
| 139 | 157 | } |
| 140 | 158 | |
| 141 | 159 | @Test |
| 142 | - public void testDeleteRole_success() { | |
| 143 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 160 | + public void testGetRoleFromCache() { | |
| 161 | + // mock 数据(缓存) | |
| 162 | + RoleDO roleDO = randomPojo(RoleDO.class); | |
| 144 | 163 | roleMapper.insert(roleDO); |
| 145 | - Long roleId = roleDO.getId(); | |
| 164 | + roleService.initLocalCache(); | |
| 165 | + // 参数准备 | |
| 166 | + Long id = roleDO.getId(); | |
| 146 | 167 | |
| 147 | - //调用 | |
| 148 | - roleService.deleteRole(roleId); | |
| 168 | + // 调用 | |
| 169 | + RoleDO dbRoleDO = roleService.getRoleFromCache(id); | |
| 170 | + // 断言 | |
| 171 | + assertPojoEquals(roleDO, dbRoleDO); | |
| 172 | + } | |
| 149 | 173 | |
| 150 | - //断言 | |
| 151 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 152 | - assertNull(newRoleDO); | |
| 174 | + @Test | |
| 175 | + public void testGetRole() { | |
| 176 | + // mock 数据 | |
| 177 | + RoleDO roleDO = randomPojo(RoleDO.class); | |
| 178 | + roleMapper.insert(roleDO); | |
| 179 | + // 参数准备 | |
| 180 | + Long id = roleDO.getId(); | |
| 153 | 181 | |
| 154 | - verify(roleProducer).sendRoleRefreshMessage(); | |
| 182 | + // 调用 | |
| 183 | + RoleDO dbRoleDO = roleService.getRole(id); | |
| 184 | + // 断言 | |
| 185 | + assertPojoEquals(roleDO, dbRoleDO); | |
| 155 | 186 | } |
| 156 | 187 | |
| 157 | 188 | @Test |
| 158 | - public void testGetRoles() { | |
| 189 | + public void testGetRoleListByStatus_statusNotEmpty() { | |
| 190 | + // mock 数据 | |
| 191 | + RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 192 | + roleMapper.insert(dbRole); | |
| 193 | + // 测试 status 不匹配 | |
| 194 | + roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 195 | + | |
| 196 | + // 调用 | |
| 197 | + List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus())); | |
| 198 | + // 断言 | |
| 199 | + assertEquals(1, list.size()); | |
| 200 | + assertPojoEquals(dbRole, list.get(0)); | |
| 201 | + } | |
| 202 | + | |
| 203 | + @Test | |
| 204 | + public void testGetRoleListByStatus_statusEmpty() { | |
| 205 | + // mock 数据 | |
| 206 | + RoleDO dbRole01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 207 | + roleMapper.insert(dbRole01); | |
| 208 | + RoleDO dbRole02 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 209 | + roleMapper.insert(dbRole02); | |
| 210 | + | |
| 211 | + // 调用 | |
| 212 | + List<RoleDO> list = roleService.getRoleListByStatus(null); | |
| 213 | + // 断言 | |
| 214 | + assertEquals(2, list.size()); | |
| 215 | + assertPojoEquals(dbRole01, list.get(0)); | |
| 216 | + assertPojoEquals(dbRole02, list.get(1)); | |
| 217 | + } | |
| 218 | + | |
| 219 | + @Test | |
| 220 | + public void testGetRoleListFromCache() { | |
| 221 | + // mock 数据 | |
| 222 | + RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 223 | + roleMapper.insert(dbRole); | |
| 224 | + // 测试 id 不匹配 | |
| 225 | + roleMapper.insert(cloneIgnoreId(dbRole, o -> {})); | |
| 226 | + roleService.initLocalCache(); | |
| 227 | + // 准备参数 | |
| 228 | + Collection<Long> ids = singleton(dbRole.getId()); | |
| 229 | + | |
| 230 | + // 调用 | |
| 231 | + List<RoleDO> list = roleService.getRoleListFromCache(ids); | |
| 232 | + // 断言 | |
| 233 | + assertEquals(1, list.size()); | |
| 234 | + assertPojoEquals(dbRole, list.get(0)); | |
| 235 | + } | |
| 236 | + | |
| 237 | + @Test | |
| 238 | + public void testGetRoleList() { | |
| 159 | 239 | // mock 数据 |
| 160 | 240 | RoleDO dbRole = randomPojo(RoleDO.class, o -> { // 等会查询到 |
| 161 | 241 | o.setName("土豆"); |
| ... | ... | @@ -175,7 +255,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 175 | 255 | reqVO.setName("土豆"); |
| 176 | 256 | reqVO.setCode("tu"); |
| 177 | 257 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 178 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 258 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 179 | 259 | |
| 180 | 260 | // 调用 |
| 181 | 261 | List<RoleDO> list = roleService.getRoleList(reqVO); |
| ... | ... | @@ -205,7 +285,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 205 | 285 | reqVO.setName("土豆"); |
| 206 | 286 | reqVO.setCode("tu"); |
| 207 | 287 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 208 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 288 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 209 | 289 | |
| 210 | 290 | // 调用 |
| 211 | 291 | PageResult<RoleDO> pageResult = roleService.getRolePage(reqVO); |
| ... | ... | @@ -216,86 +296,104 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 216 | 296 | } |
| 217 | 297 | |
| 218 | 298 | @Test |
| 219 | - public void testCheckDuplicateRole_success() { | |
| 220 | - roleService.checkDuplicateRole(randomString(), randomString(), null); | |
| 299 | + public void testHasAnySuperAdmin() { | |
| 300 | + // 是超级 | |
| 301 | + assertTrue(roleService.hasAnySuperAdmin(singletonList(randomPojo(RoleDO.class, | |
| 302 | + o -> o.setCode("super_admin"))))); | |
| 303 | + // 非超级 | |
| 304 | + assertFalse(roleService.hasAnySuperAdmin(singletonList(randomPojo(RoleDO.class, | |
| 305 | + o -> o.setCode("tenant_admin"))))); | |
| 221 | 306 | } |
| 222 | 307 | |
| 223 | 308 | @Test |
| 224 | - public void testCheckDuplicateRole_nameDuplicate() { | |
| 225 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 226 | - roleMapper.insert(roleDO); | |
| 309 | + public void testValidateRoleDuplicate_success() { | |
| 310 | + // 调用,不会抛异常 | |
| 311 | + roleService.validateRoleDuplicate(randomString(), randomString(), null); | |
| 312 | + } | |
| 227 | 313 | |
| 228 | - String duplicateName = "role_name"; | |
| 314 | + @Test | |
| 315 | + public void testValidateRoleDuplicate_nameDuplicate() { | |
| 316 | + // mock 数据 | |
| 317 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setName("role_name")); | |
| 318 | + roleMapper.insert(roleDO); | |
| 319 | + // 准备参数 | |
| 320 | + String name = "role_name"; | |
| 229 | 321 | |
| 230 | - assertServiceException(() -> roleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName); | |
| 322 | + // 调用,并断言异常 | |
| 323 | + assertServiceException(() -> roleService.validateRoleDuplicate(name, randomString(), null), | |
| 324 | + ROLE_NAME_DUPLICATE, name); | |
| 231 | 325 | } |
| 232 | 326 | |
| 233 | 327 | @Test |
| 234 | - public void testCheckDuplicateRole_codeDuplicate() { | |
| 235 | - RoleDO roleDO = randomPojo(RoleDO.class, o -> { | |
| 236 | - o.setName("role_999"); | |
| 237 | - o.setCode("code"); | |
| 238 | - o.setType(RoleTypeEnum.CUSTOM.getType()); | |
| 239 | - o.setStatus(1); | |
| 240 | - o.setDataScope(DataScopeEnum.ALL.getScope()); | |
| 241 | - }); | |
| 328 | + public void testValidateRoleDuplicate_codeDuplicate() { | |
| 329 | + // mock 数据 | |
| 330 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setCode("code")); | |
| 242 | 331 | roleMapper.insert(roleDO); |
| 332 | + // 准备参数 | |
| 333 | + String code = "code"; | |
| 243 | 334 | |
| 244 | - String randomName = randomString(); | |
| 245 | - String duplicateCode = "code"; | |
| 246 | - | |
| 247 | - assertServiceException(() -> roleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode); | |
| 335 | + // 调用,并断言异常 | |
| 336 | + assertServiceException(() -> roleService.validateRoleDuplicate(randomString(), code, null), | |
| 337 | + ROLE_CODE_DUPLICATE, code); | |
| 248 | 338 | } |
| 249 | 339 | |
| 250 | 340 | @Test |
| 251 | - public void testCheckUpdateRole_success() { | |
| 252 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 341 | + public void testValidateUpdateRole_success() { | |
| 342 | + RoleDO roleDO = randomPojo(RoleDO.class); | |
| 253 | 343 | roleMapper.insert(roleDO); |
| 254 | - Long roleId = roleDO.getId(); | |
| 344 | + // 准备参数 | |
| 345 | + Long id = roleDO.getId(); | |
| 255 | 346 | |
| 256 | - roleService.checkUpdateRole(roleId); | |
| 347 | + // 调用,无异常 | |
| 348 | + roleService.validateRoleForUpdate(id); | |
| 257 | 349 | } |
| 258 | 350 | |
| 259 | 351 | @Test |
| 260 | - public void testCheckUpdateRole_roleIdNotExist() { | |
| 261 | - assertServiceException(() -> roleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS); | |
| 352 | + public void testValidateUpdateRole_roleIdNotExist() { | |
| 353 | + assertServiceException(() -> roleService.validateRoleForUpdate(randomLongId()), ROLE_NOT_EXISTS); | |
| 262 | 354 | } |
| 263 | 355 | |
| 264 | 356 | @Test |
| 265 | - public void testCheckUpdateRole_systemRoleCanNotBeUpdate() { | |
| 266 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.SYSTEM, DataScopeEnum.ALL); | |
| 357 | + public void testValidateUpdateRole_systemRoleCanNotBeUpdate() { | |
| 358 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.SYSTEM.getType())); | |
| 267 | 359 | roleMapper.insert(roleDO); |
| 268 | - Long roleId = roleDO.getId(); | |
| 360 | + // 准备参数 | |
| 361 | + Long id = roleDO.getId(); | |
| 269 | 362 | |
| 270 | - assertServiceException(() -> roleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | |
| 363 | + assertServiceException(() -> roleService.validateRoleForUpdate(id), | |
| 364 | + ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | |
| 271 | 365 | } |
| 272 | 366 | |
| 273 | - private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status) { | |
| 274 | - return createRoleDO( name, typeEnum, scopeEnum, status, randomString()); | |
| 275 | - } | |
| 367 | + @Test | |
| 368 | + public void testValidateRoleList_success() { | |
| 369 | + // mock 数据 | |
| 370 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 371 | + roleMapper.insert(roleDO); | |
| 372 | + // 准备参数 | |
| 373 | + List<Long> ids = singletonList(roleDO.getId()); | |
| 276 | 374 | |
| 277 | - private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { | |
| 278 | - return createRoleDO(null, name, typeEnum, scopeEnum, status, code); | |
| 375 | + // 调用,无需断言 | |
| 376 | + roleService.validateRoleList(ids); | |
| 279 | 377 | } |
| 280 | 378 | |
| 281 | - private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum) { | |
| 282 | - return createRoleDO(null, name, typeEnum, scopeEnum, randomCommonStatus(), randomString()); | |
| 283 | - } | |
| 379 | + @Test | |
| 380 | + public void testValidateRoleList_notFound() { | |
| 381 | + // 准备参数 | |
| 382 | + List<Long> ids = singletonList(randomLongId()); | |
| 284 | 383 | |
| 285 | - private RoleDO createRoleDO(Long id, String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { | |
| 286 | - return randomPojo(RoleDO.class, o -> { | |
| 287 | - o.setId(id); | |
| 288 | - o.setName(name); | |
| 289 | - o.setType(typeEnum.getType()); | |
| 290 | - o.setStatus(status); | |
| 291 | - o.setDataScope(scopeEnum.getScope()); | |
| 292 | - o.setCode(code); | |
| 293 | - }); | |
| 384 | + // 调用, 并断言异常 | |
| 385 | + assertServiceException(() -> roleService.validateRoleList(ids), ROLE_NOT_EXISTS); | |
| 294 | 386 | } |
| 295 | 387 | |
| 296 | - private RoleDO randomRole() { | |
| 297 | - return randomPojo(RoleDO.class, | |
| 298 | - o -> o.setDataScope(RandomUtil.randomEle(DataScopeEnum.values()).getScope())); | |
| 299 | - } | |
| 388 | + @Test | |
| 389 | + public void testValidateRoleList_notEnable() { | |
| 390 | + // mock 数据 | |
| 391 | + RoleDO RoleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 392 | + roleMapper.insert(RoleDO); | |
| 393 | + // 准备参数 | |
| 394 | + List<Long> ids = singletonList(RoleDO.getId()); | |
| 300 | 395 | |
| 396 | + // 调用, 并断言异常 | |
| 397 | + assertServiceException(() -> roleService.validateRoleList(ids), ROLE_IS_DISABLE, RoleDO.getName()); | |
| 398 | + } | |
| 301 | 399 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImplTest.java
| ... | ... | @@ -16,13 +16,12 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 16 | 16 | import org.springframework.context.annotation.Import; |
| 17 | 17 | |
| 18 | 18 | import javax.annotation.Resource; |
| 19 | -import java.time.LocalDateTime; | |
| 20 | 19 | import java.util.Arrays; |
| 21 | 20 | import java.util.List; |
| 22 | 21 | |
| 22 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 23 | 23 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 24 | 24 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| 25 | -import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max; | |
| 26 | 25 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 27 | 26 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 28 | 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| ... | ... | @@ -61,7 +60,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 61 | 60 | // 调用 |
| 62 | 61 | sensitiveWordService.initLocalCache(); |
| 63 | 62 | // 断言 sensitiveWordTagsCache 缓存 |
| 64 | - assertEquals(SetUtils.asSet("论坛", "蔬菜"), sensitiveWordService.getSensitiveWordTags()); | |
| 63 | + assertEquals(SetUtils.asSet("论坛", "蔬菜"), sensitiveWordService.getSensitiveWordTagSet()); | |
| 65 | 64 | // 断言 tagSensitiveWordTries 缓存 |
| 66 | 65 | assertNotNull(sensitiveWordService.getDefaultSensitiveWordTrie()); |
| 67 | 66 | assertEquals(2, sensitiveWordService.getTagSensitiveWordTries().size()); |
| ... | ... | @@ -136,6 +135,36 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 136 | 135 | } |
| 137 | 136 | |
| 138 | 137 | @Test |
| 138 | + public void testGetSensitiveWord() { | |
| 139 | + // mock 数据 | |
| 140 | + SensitiveWordDO sensitiveWord = randomPojo(SensitiveWordDO.class); | |
| 141 | + sensitiveWordMapper.insert(sensitiveWord); | |
| 142 | + // 准备参数 | |
| 143 | + Long id = sensitiveWord.getId(); | |
| 144 | + | |
| 145 | + // 调用 | |
| 146 | + SensitiveWordDO dbSensitiveWord = sensitiveWordService.getSensitiveWord(id); | |
| 147 | + // 断言 | |
| 148 | + assertPojoEquals(sensitiveWord, dbSensitiveWord); | |
| 149 | + } | |
| 150 | + | |
| 151 | + @Test | |
| 152 | + public void testGetSensitiveWordList() { | |
| 153 | + // mock 数据 | |
| 154 | + SensitiveWordDO sensitiveWord01 = randomPojo(SensitiveWordDO.class); | |
| 155 | + sensitiveWordMapper.insert(sensitiveWord01); | |
| 156 | + SensitiveWordDO sensitiveWord02 = randomPojo(SensitiveWordDO.class); | |
| 157 | + sensitiveWordMapper.insert(sensitiveWord02); | |
| 158 | + | |
| 159 | + // 调用 | |
| 160 | + List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(); | |
| 161 | + // 断言 | |
| 162 | + assertEquals(2, list.size()); | |
| 163 | + assertEquals(sensitiveWord01, list.get(0)); | |
| 164 | + assertEquals(sensitiveWord02, list.get(1)); | |
| 165 | + } | |
| 166 | + | |
| 167 | + @Test | |
| 139 | 168 | public void testGetSensitiveWordPage() { |
| 140 | 169 | // mock 数据 |
| 141 | 170 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 |
| ... | ... | @@ -156,7 +185,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 156 | 185 | reqVO.setName("笨"); |
| 157 | 186 | reqVO.setTag("论坛"); |
| 158 | 187 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 159 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 188 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 160 | 189 | |
| 161 | 190 | // 调用 |
| 162 | 191 | PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(reqVO); |
| ... | ... | @@ -167,7 +196,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 167 | 196 | } |
| 168 | 197 | |
| 169 | 198 | @Test |
| 170 | - public void testGetSensitiveWordList() { | |
| 199 | + public void testGetSensitiveWordList_export() { | |
| 171 | 200 | // mock 数据 |
| 172 | 201 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 |
| 173 | 202 | o.setName("笨蛋"); |
| ... | ... | @@ -187,7 +216,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 187 | 216 | reqVO.setName("笨"); |
| 188 | 217 | reqVO.setTag("论坛"); |
| 189 | 218 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 190 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 219 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 191 | 220 | |
| 192 | 221 | // 调用 |
| 193 | 222 | List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(reqVO); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsChannelServiceTest.java
| ... | ... | @@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.system.service.sms; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | -import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | |
| 6 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 7 | 5 | import cn.iocoder.yudao.framework.sms.core.client.SmsClientFactory; |
| 8 | 6 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 9 | 7 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelCreateReqVO; |
| ... | ... | @@ -17,11 +15,12 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 17 | 15 | import org.springframework.context.annotation.Import; |
| 18 | 16 | |
| 19 | 17 | import javax.annotation.Resource; |
| 20 | -import java.time.LocalDateTime; | |
| 21 | -import java.util.function.Consumer; | |
| 22 | 18 | |
| 23 | -import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 24 | 22 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 23 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 25 | 24 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*; |
| 26 | 25 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 27 | 26 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN; |
| ... | ... | @@ -49,9 +48,9 @@ public class SmsChannelServiceTest extends BaseDbUnitTest { |
| 49 | 48 | @Test |
| 50 | 49 | public void testInitLocalCache_success() { |
| 51 | 50 | // mock 数据 |
| 52 | - SmsChannelDO smsChannelDO01 = randomSmsChannelDO(); | |
| 51 | + SmsChannelDO smsChannelDO01 = randomPojo(SmsChannelDO.class); | |
| 53 | 52 | smsChannelMapper.insert(smsChannelDO01); |
| 54 | - SmsChannelDO smsChannelDO02 = randomSmsChannelDO(); | |
| 53 | + SmsChannelDO smsChannelDO02 = randomPojo(SmsChannelDO.class); | |
| 55 | 54 | smsChannelMapper.insert(smsChannelDO02); |
| 56 | 55 | |
| 57 | 56 | // 调用 |
| ... | ... | @@ -82,7 +81,7 @@ public class SmsChannelServiceTest extends BaseDbUnitTest { |
| 82 | 81 | @Test |
| 83 | 82 | public void testUpdateSmsChannel_success() { |
| 84 | 83 | // mock 数据 |
| 85 | - SmsChannelDO dbSmsChannel = randomSmsChannelDO(); | |
| 84 | + SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class); | |
| 86 | 85 | smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据 |
| 87 | 86 | // 准备参数 |
| 88 | 87 | SmsChannelUpdateReqVO reqVO = randomPojo(SmsChannelUpdateReqVO.class, o -> { |
| ... | ... | @@ -112,7 +111,7 @@ public class SmsChannelServiceTest extends BaseDbUnitTest { |
| 112 | 111 | @Test |
| 113 | 112 | public void testDeleteSmsChannel_success() { |
| 114 | 113 | // mock 数据 |
| 115 | - SmsChannelDO dbSmsChannel = randomSmsChannelDO(); | |
| 114 | + SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class); | |
| 116 | 115 | smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据 |
| 117 | 116 | // 准备参数 |
| 118 | 117 | Long id = dbSmsChannel.getId(); |
| ... | ... | @@ -137,7 +136,7 @@ public class SmsChannelServiceTest extends BaseDbUnitTest { |
| 137 | 136 | @Test |
| 138 | 137 | public void testDeleteSmsChannel_hasChildren() { |
| 139 | 138 | // mock 数据 |
| 140 | - SmsChannelDO dbSmsChannel = randomSmsChannelDO(); | |
| 139 | + SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class); | |
| 141 | 140 | smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据 |
| 142 | 141 | // 准备参数 |
| 143 | 142 | Long id = dbSmsChannel.getId(); |
| ... | ... | @@ -149,6 +148,35 @@ public class SmsChannelServiceTest extends BaseDbUnitTest { |
| 149 | 148 | } |
| 150 | 149 | |
| 151 | 150 | @Test |
| 151 | + public void testGetSmsChannel() { | |
| 152 | + // mock 数据 | |
| 153 | + SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class); | |
| 154 | + smsChannelMapper.insert(dbSmsChannel); // @Sql: 先插入出一条存在的数据 | |
| 155 | + // 准备参数 | |
| 156 | + Long id = dbSmsChannel.getId(); | |
| 157 | + | |
| 158 | + // 调用,并断言 | |
| 159 | + assertPojoEquals(dbSmsChannel, smsChannelService.getSmsChannel(id)); | |
| 160 | + } | |
| 161 | + | |
| 162 | + @Test | |
| 163 | + public void testGetSmsChannelList() { | |
| 164 | + // mock 数据 | |
| 165 | + SmsChannelDO dbSmsChannel01 = randomPojo(SmsChannelDO.class); | |
| 166 | + smsChannelMapper.insert(dbSmsChannel01); | |
| 167 | + SmsChannelDO dbSmsChannel02 = randomPojo(SmsChannelDO.class); | |
| 168 | + smsChannelMapper.insert(dbSmsChannel02); | |
| 169 | + // 准备参数 | |
| 170 | + | |
| 171 | + // 调用 | |
| 172 | + List<SmsChannelDO> list = smsChannelService.getSmsChannelList(); | |
| 173 | + // 断言 | |
| 174 | + assertEquals(2, list.size()); | |
| 175 | + assertPojoEquals(dbSmsChannel01, list.get(0)); | |
| 176 | + assertPojoEquals(dbSmsChannel02, list.get(1)); | |
| 177 | + } | |
| 178 | + | |
| 179 | + @Test | |
| 152 | 180 | public void testGetSmsChannelPage() { |
| 153 | 181 | // mock 数据 |
| 154 | 182 | SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class, o -> { // 等会查询到 |
| ... | ... | @@ -158,16 +186,16 @@ public class SmsChannelServiceTest extends BaseDbUnitTest { |
| 158 | 186 | }); |
| 159 | 187 | smsChannelMapper.insert(dbSmsChannel); |
| 160 | 188 | // 测试 signature 不匹配 |
| 161 | - smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码"))); | |
| 189 | + smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码"))); | |
| 162 | 190 | // 测试 status 不匹配 |
| 163 | - smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 191 | + smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 164 | 192 | // 测试 createTime 不匹配 |
| 165 | - smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11)))); | |
| 193 | + smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11)))); | |
| 166 | 194 | // 准备参数 |
| 167 | 195 | SmsChannelPageReqVO reqVO = new SmsChannelPageReqVO(); |
| 168 | 196 | reqVO.setSignature("芋道"); |
| 169 | 197 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 170 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)})); | |
| 198 | + reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24)); | |
| 171 | 199 | |
| 172 | 200 | // 调用 |
| 173 | 201 | PageResult<SmsChannelDO> pageResult = smsChannelService.getSmsChannelPage(reqVO); |
| ... | ... | @@ -177,14 +205,4 @@ public class SmsChannelServiceTest extends BaseDbUnitTest { |
| 177 | 205 | assertPojoEquals(dbSmsChannel, pageResult.getList().get(0)); |
| 178 | 206 | } |
| 179 | 207 | |
| 180 | - // ========== 随机对象 ========== | |
| 181 | - | |
| 182 | - @SafeVarargs | |
| 183 | - private static SmsChannelDO randomSmsChannelDO(Consumer<SmsChannelDO>... consumers) { | |
| 184 | - Consumer<SmsChannelDO> consumer = (o) -> { | |
| 185 | - o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 | |
| 186 | - }; | |
| 187 | - return randomPojo(SmsChannelDO.class, ArrayUtils.append(consumer, consumers)); | |
| 188 | - } | |
| 189 | - | |
| 190 | 208 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImplTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.service.sms; | |
| 2 | + | |
| 3 | +import cn.hutool.core.map.MapUtil; | |
| 4 | +import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; | |
| 5 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 6 | +import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; | |
| 7 | +import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; | |
| 8 | +import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO; | |
| 9 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsCodeDO; | |
| 10 | +import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsCodeMapper; | |
| 11 | +import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; | |
| 12 | +import cn.iocoder.yudao.module.system.framework.sms.SmsCodeProperties; | |
| 13 | +import com.baomidou.mybatisplus.annotation.DbType; | |
| 14 | +import org.junit.jupiter.api.BeforeEach; | |
| 15 | +import org.junit.jupiter.api.Test; | |
| 16 | +import org.springframework.boot.test.mock.mockito.MockBean; | |
| 17 | +import org.springframework.context.annotation.Import; | |
| 18 | + | |
| 19 | +import javax.annotation.Resource; | |
| 20 | +import java.time.Duration; | |
| 21 | +import java.time.LocalDateTime; | |
| 22 | + | |
| 23 | +import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 24 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | |
| 25 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | |
| 26 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 27 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 28 | +import static org.junit.jupiter.api.Assertions.*; | |
| 29 | +import static org.mockito.ArgumentMatchers.eq; | |
| 30 | +import static org.mockito.ArgumentMatchers.isNull; | |
| 31 | +import static org.mockito.Mockito.verify; | |
| 32 | +import static org.mockito.Mockito.when; | |
| 33 | + | |
| 34 | +@Import(SmsCodeServiceImpl.class) | |
| 35 | +public class SmsCodeServiceImplTest extends BaseDbUnitTest { | |
| 36 | + | |
| 37 | + @Resource | |
| 38 | + private SmsCodeServiceImpl smsCodeService; | |
| 39 | + | |
| 40 | + @Resource | |
| 41 | + private SmsCodeMapper smsCodeMapper; | |
| 42 | + | |
| 43 | + @MockBean | |
| 44 | + private SmsCodeProperties smsCodeProperties; | |
| 45 | + @MockBean | |
| 46 | + private SmsSendService smsSendService; | |
| 47 | + | |
| 48 | + @BeforeEach | |
| 49 | + public void setUp() { | |
| 50 | + when(smsCodeProperties.getExpireTimes()).thenReturn(Duration.ofMinutes(5)); | |
| 51 | + when(smsCodeProperties.getSendFrequency()).thenReturn(Duration.ofMinutes(1)); | |
| 52 | + when(smsCodeProperties.getSendMaximumQuantityPerDay()).thenReturn(10); | |
| 53 | + when(smsCodeProperties.getBeginCode()).thenReturn(9999); | |
| 54 | + when(smsCodeProperties.getEndCode()).thenReturn(9999); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Test | |
| 58 | + public void sendSmsCode_success() { | |
| 59 | + // 准备参数 | |
| 60 | + SmsCodeSendReqDTO reqDTO = randomPojo(SmsCodeSendReqDTO.class, o -> { | |
| 61 | + o.setMobile("15601691300"); | |
| 62 | + o.setScene(SmsSceneEnum.MEMBER_LOGIN.getScene()); | |
| 63 | + }); | |
| 64 | + // mock 方法 | |
| 65 | + SqlConstants.init(DbType.MYSQL); | |
| 66 | + | |
| 67 | + // 调用 | |
| 68 | + smsCodeService.sendSmsCode(reqDTO); | |
| 69 | + // 断言 code 验证码 | |
| 70 | + SmsCodeDO smsCodeDO = smsCodeMapper.selectOne(null); | |
| 71 | + assertPojoEquals(reqDTO, smsCodeDO); | |
| 72 | + assertEquals("9999", smsCodeDO.getCode()); | |
| 73 | + assertEquals(1, smsCodeDO.getTodayIndex()); | |
| 74 | + assertFalse(smsCodeDO.getUsed()); | |
| 75 | + // 断言调用 | |
| 76 | + verify(smsSendService).sendSingleSms(eq(reqDTO.getMobile()), isNull(), isNull(), | |
| 77 | + eq("user-sms-login"), eq(MapUtil.of("code", "9999"))); | |
| 78 | + } | |
| 79 | + | |
| 80 | + @Test | |
| 81 | + public void sendSmsCode_tooFast() { | |
| 82 | + // mock 数据 | |
| 83 | + SmsCodeDO smsCodeDO = randomPojo(SmsCodeDO.class, | |
| 84 | + o -> o.setMobile("15601691300").setTodayIndex(1)); | |
| 85 | + smsCodeMapper.insert(smsCodeDO); | |
| 86 | + // 准备参数 | |
| 87 | + SmsCodeSendReqDTO reqDTO = randomPojo(SmsCodeSendReqDTO.class, o -> { | |
| 88 | + o.setMobile("15601691300"); | |
| 89 | + o.setScene(SmsSceneEnum.MEMBER_LOGIN.getScene()); | |
| 90 | + }); | |
| 91 | + // mock 方法 | |
| 92 | + SqlConstants.init(DbType.MYSQL); | |
| 93 | + | |
| 94 | + // 调用,并断言异常 | |
| 95 | + assertServiceException(() -> smsCodeService.sendSmsCode(reqDTO), | |
| 96 | + SMS_CODE_SEND_TOO_FAST); | |
| 97 | + } | |
| 98 | + | |
| 99 | + @Test | |
| 100 | + public void sendSmsCode_exceedDay() { | |
| 101 | + // mock 数据 | |
| 102 | + SmsCodeDO smsCodeDO = randomPojo(SmsCodeDO.class, | |
| 103 | + o -> o.setMobile("15601691300").setTodayIndex(10).setCreateTime(LocalDateTime.now())); | |
| 104 | + smsCodeMapper.insert(smsCodeDO); | |
| 105 | + // 准备参数 | |
| 106 | + SmsCodeSendReqDTO reqDTO = randomPojo(SmsCodeSendReqDTO.class, o -> { | |
| 107 | + o.setMobile("15601691300"); | |
| 108 | + o.setScene(SmsSceneEnum.MEMBER_LOGIN.getScene()); | |
| 109 | + }); | |
| 110 | + // mock 方法 | |
| 111 | + SqlConstants.init(DbType.MYSQL); | |
| 112 | + when(smsCodeProperties.getSendFrequency()).thenReturn(Duration.ofMillis(0)); | |
| 113 | + | |
| 114 | + // 调用,并断言异常 | |
| 115 | + assertServiceException(() -> smsCodeService.sendSmsCode(reqDTO), | |
| 116 | + SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY); | |
| 117 | + } | |
| 118 | + | |
| 119 | + @Test | |
| 120 | + public void testUseSmsCode_success() { | |
| 121 | + // 准备参数 | |
| 122 | + SmsCodeUseReqDTO reqDTO = randomPojo(SmsCodeUseReqDTO.class, o -> { | |
| 123 | + o.setMobile("15601691300"); | |
| 124 | + o.setScene(randomEle(SmsSceneEnum.values()).getScene()); | |
| 125 | + }); | |
| 126 | + // mock 数据 | |
| 127 | + SqlConstants.init(DbType.MYSQL); | |
| 128 | + smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> { | |
| 129 | + o.setMobile(reqDTO.getMobile()).setScene(reqDTO.getScene()) | |
| 130 | + .setCode(reqDTO.getCode()).setUsed(false); | |
| 131 | + })); | |
| 132 | + | |
| 133 | + // 调用 | |
| 134 | + smsCodeService.useSmsCode(reqDTO); | |
| 135 | + // 断言 | |
| 136 | + SmsCodeDO smsCodeDO = smsCodeMapper.selectOne(null); | |
| 137 | + assertTrue(smsCodeDO.getUsed()); | |
| 138 | + assertNotNull(smsCodeDO.getUsedTime()); | |
| 139 | + assertEquals(reqDTO.getUsedIp(), smsCodeDO.getUsedIp()); | |
| 140 | + } | |
| 141 | + | |
| 142 | + @Test | |
| 143 | + public void validateSmsCode_success() { | |
| 144 | + // 准备参数 | |
| 145 | + SmsCodeValidateReqDTO reqDTO = randomPojo(SmsCodeValidateReqDTO.class, o -> { | |
| 146 | + o.setMobile("15601691300"); | |
| 147 | + o.setScene(randomEle(SmsSceneEnum.values()).getScene()); | |
| 148 | + }); | |
| 149 | + // mock 数据 | |
| 150 | + SqlConstants.init(DbType.MYSQL); | |
| 151 | + smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> o.setMobile(reqDTO.getMobile()) | |
| 152 | + .setScene(reqDTO.getScene()).setCode(reqDTO.getCode()).setUsed(false))); | |
| 153 | + | |
| 154 | + // 调用 | |
| 155 | + smsCodeService.validateSmsCode(reqDTO); | |
| 156 | + } | |
| 157 | + | |
| 158 | + @Test | |
| 159 | + public void validateSmsCode_notFound() { | |
| 160 | + // 准备参数 | |
| 161 | + SmsCodeValidateReqDTO reqDTO = randomPojo(SmsCodeValidateReqDTO.class, o -> { | |
| 162 | + o.setMobile("15601691300"); | |
| 163 | + o.setScene(randomEle(SmsSceneEnum.values()).getScene()); | |
| 164 | + }); | |
| 165 | + // mock 数据 | |
| 166 | + SqlConstants.init(DbType.MYSQL); | |
| 167 | + | |
| 168 | + // 调用,并断言异常 | |
| 169 | + assertServiceException(() -> smsCodeService.validateSmsCode(reqDTO), | |
| 170 | + SMS_CODE_NOT_FOUND); | |
| 171 | + } | |
| 172 | + | |
| 173 | + @Test | |
| 174 | + public void validateSmsCode_expired() { | |
| 175 | + // 准备参数 | |
| 176 | + SmsCodeValidateReqDTO reqDTO = randomPojo(SmsCodeValidateReqDTO.class, o -> { | |
| 177 | + o.setMobile("15601691300"); | |
| 178 | + o.setScene(randomEle(SmsSceneEnum.values()).getScene()); | |
| 179 | + }); | |
| 180 | + // mock 数据 | |
| 181 | + SqlConstants.init(DbType.MYSQL); | |
| 182 | + smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> o.setMobile(reqDTO.getMobile()) | |
| 183 | + .setScene(reqDTO.getScene()).setCode(reqDTO.getCode()).setUsed(false) | |
| 184 | + .setCreateTime(LocalDateTime.now().minusMinutes(6)))); | |
| 185 | + | |
| 186 | + // 调用,并断言异常 | |
| 187 | + assertServiceException(() -> smsCodeService.validateSmsCode(reqDTO), | |
| 188 | + SMS_CODE_EXPIRED); | |
| 189 | + } | |
| 190 | + | |
| 191 | + @Test | |
| 192 | + public void validateSmsCode_used() { | |
| 193 | + // 准备参数 | |
| 194 | + SmsCodeValidateReqDTO reqDTO = randomPojo(SmsCodeValidateReqDTO.class, o -> { | |
| 195 | + o.setMobile("15601691300"); | |
| 196 | + o.setScene(randomEle(SmsSceneEnum.values()).getScene()); | |
| 197 | + }); | |
| 198 | + // mock 数据 | |
| 199 | + SqlConstants.init(DbType.MYSQL); | |
| 200 | + smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> o.setMobile(reqDTO.getMobile()) | |
| 201 | + .setScene(reqDTO.getScene()).setCode(reqDTO.getCode()).setUsed(true) | |
| 202 | + .setCreateTime(LocalDateTime.now()))); | |
| 203 | + | |
| 204 | + // 调用,并断言异常 | |
| 205 | + assertServiceException(() -> smsCodeService.validateSmsCode(reqDTO), | |
| 206 | + SMS_CODE_USED); | |
| 207 | + } | |
| 208 | + | |
| 209 | +} | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsLogServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsLogServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.sms; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.map.MapUtil; |
| 4 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 6 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 7 | +import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | |
| 8 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 5 | 9 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.log.SmsLogExportReqVO; |
| 6 | 10 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.log.SmsLogPageReqVO; |
| 7 | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsLogDO; |
| ... | ... | @@ -10,11 +14,6 @@ import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsLogMapper; |
| 10 | 14 | import cn.iocoder.yudao.module.system.enums.sms.SmsReceiveStatusEnum; |
| 11 | 15 | import cn.iocoder.yudao.module.system.enums.sms.SmsSendStatusEnum; |
| 12 | 16 | import cn.iocoder.yudao.module.system.enums.sms.SmsTemplateTypeEnum; |
| 13 | -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | |
| 14 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 15 | -import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | |
| 16 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 17 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 18 | 17 | import org.junit.jupiter.api.Test; |
| 19 | 18 | import org.springframework.context.annotation.Import; |
| 20 | 19 | |
| ... | ... | @@ -26,14 +25,16 @@ import java.util.function.Consumer; |
| 26 | 25 | |
| 27 | 26 | import static cn.hutool.core.util.RandomUtil.randomBoolean; |
| 28 | 27 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 28 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 29 | 29 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 30 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 30 | 31 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 31 | 32 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 32 | 33 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | 34 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 34 | 35 | |
| 35 | 36 | @Import(SmsLogServiceImpl.class) |
| 36 | -public class SmsLogServiceTest extends BaseDbUnitTest { | |
| 37 | +public class SmsLogServiceImplTest extends BaseDbUnitTest { | |
| 37 | 38 | |
| 38 | 39 | @Resource |
| 39 | 40 | private SmsLogServiceImpl smsLogService; |
| ... | ... | @@ -55,30 +56,28 @@ public class SmsLogServiceTest extends BaseDbUnitTest { |
| 55 | 56 | }); |
| 56 | 57 | smsLogMapper.insert(dbSmsLog); |
| 57 | 58 | // 测试 channelId 不匹配 |
| 58 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setChannelId(2L))); | |
| 59 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setChannelId(2L))); | |
| 59 | 60 | // 测试 templateId 不匹配 |
| 60 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setTemplateId(20L))); | |
| 61 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setTemplateId(20L))); | |
| 61 | 62 | // 测试 mobile 不匹配 |
| 62 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setMobile("18818260999"))); | |
| 63 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setMobile("18818260999"))); | |
| 63 | 64 | // 测试 sendStatus 不匹配 |
| 64 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendStatus(SmsSendStatusEnum.IGNORE.getStatus()))); | |
| 65 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setSendStatus(SmsSendStatusEnum.IGNORE.getStatus()))); | |
| 65 | 66 | // 测试 sendTime 不匹配 |
| 66 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); | |
| 67 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); | |
| 67 | 68 | // 测试 receiveStatus 不匹配 |
| 68 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveStatus(SmsReceiveStatusEnum.SUCCESS.getStatus()))); | |
| 69 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setReceiveStatus(SmsReceiveStatusEnum.SUCCESS.getStatus()))); | |
| 69 | 70 | // 测试 receiveTime 不匹配 |
| 70 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); | |
| 71 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); | |
| 71 | 72 | // 准备参数 |
| 72 | 73 | SmsLogPageReqVO reqVO = new SmsLogPageReqVO(); |
| 73 | 74 | reqVO.setChannelId(1L); |
| 74 | 75 | reqVO.setTemplateId(10L); |
| 75 | 76 | reqVO.setMobile("156"); |
| 76 | 77 | reqVO.setSendStatus(SmsSendStatusEnum.INIT.getStatus()); |
| 77 | - reqVO.setSendTime((new LocalDateTime[]{buildTime(2020, 11, 1), | |
| 78 | - buildTime(2020, 11, 30)})); | |
| 78 | + reqVO.setSendTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30)); | |
| 79 | 79 | reqVO.setReceiveStatus(SmsReceiveStatusEnum.INIT.getStatus()); |
| 80 | - reqVO.setReceiveTime((new LocalDateTime[]{buildTime(2021, 11, 1), | |
| 81 | - buildTime(2021, 11, 30)})); | |
| 80 | + reqVO.setReceiveTime(buildBetweenTime(2021, 11, 1, 2021, 11, 30)); | |
| 82 | 81 | |
| 83 | 82 | // 调用 |
| 84 | 83 | PageResult<SmsLogDO> pageResult = smsLogService.getSmsLogPage(reqVO); |
| ... | ... | @@ -102,30 +101,28 @@ public class SmsLogServiceTest extends BaseDbUnitTest { |
| 102 | 101 | }); |
| 103 | 102 | smsLogMapper.insert(dbSmsLog); |
| 104 | 103 | // 测试 channelId 不匹配 |
| 105 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setChannelId(2L))); | |
| 104 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setChannelId(2L))); | |
| 106 | 105 | // 测试 templateId 不匹配 |
| 107 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setTemplateId(20L))); | |
| 106 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setTemplateId(20L))); | |
| 108 | 107 | // 测试 mobile 不匹配 |
| 109 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setMobile("18818260999"))); | |
| 108 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setMobile("18818260999"))); | |
| 110 | 109 | // 测试 sendStatus 不匹配 |
| 111 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendStatus(SmsSendStatusEnum.IGNORE.getStatus()))); | |
| 110 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setSendStatus(SmsSendStatusEnum.IGNORE.getStatus()))); | |
| 112 | 111 | // 测试 sendTime 不匹配 |
| 113 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); | |
| 112 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); | |
| 114 | 113 | // 测试 receiveStatus 不匹配 |
| 115 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveStatus(SmsReceiveStatusEnum.SUCCESS.getStatus()))); | |
| 114 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setReceiveStatus(SmsReceiveStatusEnum.SUCCESS.getStatus()))); | |
| 116 | 115 | // 测试 receiveTime 不匹配 |
| 117 | - smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); | |
| 116 | + smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); | |
| 118 | 117 | // 准备参数 |
| 119 | 118 | SmsLogExportReqVO reqVO = new SmsLogExportReqVO(); |
| 120 | 119 | reqVO.setChannelId(1L); |
| 121 | 120 | reqVO.setTemplateId(10L); |
| 122 | 121 | reqVO.setMobile("156"); |
| 123 | 122 | reqVO.setSendStatus(SmsSendStatusEnum.INIT.getStatus()); |
| 124 | - reqVO.setSendTime((new LocalDateTime[]{buildTime(2020, 11, 1), | |
| 125 | - buildTime(2020, 11, 30)})); | |
| 123 | + reqVO.setSendTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30)); | |
| 126 | 124 | reqVO.setReceiveStatus(SmsReceiveStatusEnum.INIT.getStatus()); |
| 127 | - reqVO.setReceiveTime((new LocalDateTime[]{buildTime(2021, 11, 1), | |
| 128 | - buildTime(2021, 11, 30)})); | |
| 125 | + reqVO.setReceiveTime(buildBetweenTime(2021, 11, 1, 2021, 11, 30)); | |
| 129 | 126 | |
| 130 | 127 | // 调用 |
| 131 | 128 | List<SmsLogDO> list = smsLogService.getSmsLogList(reqVO); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.sms; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.map.MapUtil; |
| 4 | -import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 5 | -import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; | |
| 6 | -import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage; | |
| 7 | -import cn.iocoder.yudao.module.system.mq.producer.sms.SmsProducer; | |
| 8 | 4 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 9 | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 10 | 6 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| ... | ... | @@ -14,6 +10,13 @@ import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult; |
| 14 | 10 | import cn.iocoder.yudao.framework.sms.core.client.dto.SmsReceiveRespDTO; |
| 15 | 11 | import cn.iocoder.yudao.framework.sms.core.client.dto.SmsSendRespDTO; |
| 16 | 12 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 13 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 14 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; | |
| 15 | +import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | |
| 16 | +import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage; | |
| 17 | +import cn.iocoder.yudao.module.system.mq.producer.sms.SmsProducer; | |
| 18 | +import cn.iocoder.yudao.module.system.service.member.MemberService; | |
| 19 | +import cn.iocoder.yudao.module.system.service.user.AdminUserService; | |
| 17 | 20 | import org.assertj.core.util.Lists; |
| 18 | 21 | import org.junit.jupiter.api.Test; |
| 19 | 22 | import org.mockito.InjectMocks; |
| ... | ... | @@ -31,12 +34,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | 34 | import static org.mockito.ArgumentMatchers.eq; |
| 32 | 35 | import static org.mockito.Mockito.*; |
| 33 | 36 | |
| 34 | -public class SmsSendServiceTest extends BaseMockitoUnitTest { | |
| 37 | +public class SmsSendServiceImplTest extends BaseMockitoUnitTest { | |
| 35 | 38 | |
| 36 | 39 | @InjectMocks |
| 37 | 40 | private SmsSendServiceImpl smsService; |
| 38 | 41 | |
| 39 | 42 | @Mock |
| 43 | + private AdminUserService adminUserService; | |
| 44 | + @Mock | |
| 45 | + private MemberService memberService; | |
| 46 | + @Mock | |
| 40 | 47 | private SmsChannelService smsChannelService; |
| 41 | 48 | @Mock |
| 42 | 49 | private SmsTemplateService smsTemplateService; |
| ... | ... | @@ -48,6 +55,84 @@ public class SmsSendServiceTest extends BaseMockitoUnitTest { |
| 48 | 55 | @Mock |
| 49 | 56 | private SmsClientFactory smsClientFactory; |
| 50 | 57 | |
| 58 | + @Test | |
| 59 | + public void testSendSingleSmsToAdmin() { | |
| 60 | + // 准备参数 | |
| 61 | + Long userId = randomLongId(); | |
| 62 | + String templateCode = randomString(); | |
| 63 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | |
| 64 | + .put("op", "login").build(); | |
| 65 | + // mock adminUserService 的方法 | |
| 66 | + AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setMobile("15601691300")); | |
| 67 | + when(adminUserService.getUser(eq(userId))).thenReturn(user); | |
| 68 | + | |
| 69 | + // mock SmsTemplateService 的方法 | |
| 70 | + SmsTemplateDO template = randomPojo(SmsTemplateDO.class, o -> { | |
| 71 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 72 | + o.setContent("验证码为{code}, 操作为{op}"); | |
| 73 | + o.setParams(Lists.newArrayList("code", "op")); | |
| 74 | + }); | |
| 75 | + when(smsTemplateService.getSmsTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | |
| 76 | + String content = randomString(); | |
| 77 | + when(smsTemplateService.formatSmsTemplateContent(eq(template.getContent()), eq(templateParams))) | |
| 78 | + .thenReturn(content); | |
| 79 | + // mock SmsChannelService 的方法 | |
| 80 | + SmsChannelDO smsChannel = randomPojo(SmsChannelDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 81 | + when(smsChannelService.getSmsChannel(eq(template.getChannelId()))).thenReturn(smsChannel); | |
| 82 | + // mock SmsLogService 的方法 | |
| 83 | + Long smsLogId = randomLongId(); | |
| 84 | + when(smsLogService.createSmsLog(eq(user.getMobile()), eq(userId), eq(UserTypeEnum.ADMIN.getValue()), eq(Boolean.TRUE), eq(template), | |
| 85 | + eq(content), eq(templateParams))).thenReturn(smsLogId); | |
| 86 | + | |
| 87 | + // 调用 | |
| 88 | + Long resultSmsLogId = smsService.sendSingleSmsToAdmin(null, userId, templateCode, templateParams); | |
| 89 | + // 断言 | |
| 90 | + assertEquals(smsLogId, resultSmsLogId); | |
| 91 | + // 断言调用 | |
| 92 | + verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(user.getMobile()), | |
| 93 | + eq(template.getChannelId()), eq(template.getApiTemplateId()), | |
| 94 | + eq(Lists.newArrayList(new KeyValue<>("code", "1234"), new KeyValue<>("op", "login")))); | |
| 95 | + } | |
| 96 | + | |
| 97 | + @Test | |
| 98 | + public void testSendSingleSmsToUser() { | |
| 99 | + // 准备参数 | |
| 100 | + Long userId = randomLongId(); | |
| 101 | + String templateCode = randomString(); | |
| 102 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | |
| 103 | + .put("op", "login").build(); | |
| 104 | + // mock memberService 的方法 | |
| 105 | + String mobile = "15601691300"; | |
| 106 | + when(memberService.getMemberUserMobile(eq(userId))).thenReturn(mobile); | |
| 107 | + | |
| 108 | + // mock SmsTemplateService 的方法 | |
| 109 | + SmsTemplateDO template = randomPojo(SmsTemplateDO.class, o -> { | |
| 110 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 111 | + o.setContent("验证码为{code}, 操作为{op}"); | |
| 112 | + o.setParams(Lists.newArrayList("code", "op")); | |
| 113 | + }); | |
| 114 | + when(smsTemplateService.getSmsTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | |
| 115 | + String content = randomString(); | |
| 116 | + when(smsTemplateService.formatSmsTemplateContent(eq(template.getContent()), eq(templateParams))) | |
| 117 | + .thenReturn(content); | |
| 118 | + // mock SmsChannelService 的方法 | |
| 119 | + SmsChannelDO smsChannel = randomPojo(SmsChannelDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 120 | + when(smsChannelService.getSmsChannel(eq(template.getChannelId()))).thenReturn(smsChannel); | |
| 121 | + // mock SmsLogService 的方法 | |
| 122 | + Long smsLogId = randomLongId(); | |
| 123 | + when(smsLogService.createSmsLog(eq(mobile), eq(userId), eq(UserTypeEnum.MEMBER.getValue()), eq(Boolean.TRUE), eq(template), | |
| 124 | + eq(content), eq(templateParams))).thenReturn(smsLogId); | |
| 125 | + | |
| 126 | + // 调用 | |
| 127 | + Long resultSmsLogId = smsService.sendSingleSmsToMember(null, userId, templateCode, templateParams); | |
| 128 | + // 断言 | |
| 129 | + assertEquals(smsLogId, resultSmsLogId); | |
| 130 | + // 断言调用 | |
| 131 | + verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(mobile), | |
| 132 | + eq(template.getChannelId()), eq(template.getApiTemplateId()), | |
| 133 | + eq(Lists.newArrayList(new KeyValue<>("code", "1234"), new KeyValue<>("op", "login")))); | |
| 134 | + } | |
| 135 | + | |
| 51 | 136 | /** |
| 52 | 137 | * 发送成功,当短信模板开启时 |
| 53 | 138 | */ |
| ... | ... | @@ -83,7 +168,7 @@ public class SmsSendServiceTest extends BaseMockitoUnitTest { |
| 83 | 168 | // 断言 |
| 84 | 169 | assertEquals(smsLogId, resultSmsLogId); |
| 85 | 170 | // 断言调用 |
| 86 | - verify(smsProducer, times(1)).sendSmsSendMessage(eq(smsLogId), eq(mobile), | |
| 171 | + verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(mobile), | |
| 87 | 172 | eq(template.getChannelId()), eq(template.getApiTemplateId()), |
| 88 | 173 | eq(Lists.newArrayList(new KeyValue<>("code", "1234"), new KeyValue<>("op", "login")))); |
| 89 | 174 | } |
| ... | ... | @@ -134,7 +219,7 @@ public class SmsSendServiceTest extends BaseMockitoUnitTest { |
| 134 | 219 | // mock 方法 |
| 135 | 220 | |
| 136 | 221 | // 调用,并断言异常 |
| 137 | - assertServiceException(() -> smsService.checkSmsTemplateValid(templateCode), | |
| 222 | + assertServiceException(() -> smsService.validateSmsTemplate(templateCode), | |
| 138 | 223 | SMS_SEND_TEMPLATE_NOT_EXISTS); |
| 139 | 224 | } |
| 140 | 225 | |
| ... | ... | @@ -157,7 +242,7 @@ public class SmsSendServiceTest extends BaseMockitoUnitTest { |
| 157 | 242 | // mock 方法 |
| 158 | 243 | |
| 159 | 244 | // 调用,并断言异常 |
| 160 | - assertServiceException(() -> smsService.checkMobile(null), | |
| 245 | + assertServiceException(() -> smsService.validateMobile(null), | |
| 161 | 246 | SMS_SEND_MOBILE_NOT_EXISTS); |
| 162 | 247 | } |
| 163 | 248 | |
| ... | ... | @@ -177,7 +262,7 @@ public class SmsSendServiceTest extends BaseMockitoUnitTest { |
| 177 | 262 | // 调用 |
| 178 | 263 | smsService.doSendSms(message); |
| 179 | 264 | // 断言 |
| 180 | - verify(smsLogService, times(1)).updateSmsSendResult(eq(message.getLogId()), | |
| 265 | + verify(smsLogService).updateSmsSendResult(eq(message.getLogId()), | |
| 181 | 266 | eq(sendResult.getCode()), eq(sendResult.getMsg()), eq(sendResult.getApiCode()), |
| 182 | 267 | eq(sendResult.getApiMsg()), eq(sendResult.getApiRequestId()), eq(sendResult.getData().getSerialNo())); |
| 183 | 268 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.sms; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateCreateReqVO; | |
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateExportReqVO; | |
| 5 | -import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplatePageReqVO; | |
| 6 | -import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateUpdateReqVO; | |
| 7 | -import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 8 | -import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; | |
| 9 | -import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsTemplateMapper; | |
| 10 | -import cn.iocoder.yudao.module.system.mq.producer.sms.SmsProducer; | |
| 11 | -import cn.iocoder.yudao.module.system.enums.sms.SmsTemplateTypeEnum; | |
| 12 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 13 | 4 | import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; |
| 14 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| ... | ... | @@ -19,31 +10,38 @@ import cn.iocoder.yudao.framework.sms.core.client.SmsClientFactory; |
| 19 | 10 | import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult; |
| 20 | 11 | import cn.iocoder.yudao.framework.sms.core.client.dto.SmsTemplateRespDTO; |
| 21 | 12 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 13 | +import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateCreateReqVO; | |
| 14 | +import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateExportReqVO; | |
| 15 | +import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplatePageReqVO; | |
| 16 | +import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateUpdateReqVO; | |
| 17 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 18 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; | |
| 19 | +import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsTemplateMapper; | |
| 20 | +import cn.iocoder.yudao.module.system.enums.sms.SmsTemplateTypeEnum; | |
| 21 | +import cn.iocoder.yudao.module.system.mq.producer.sms.SmsProducer; | |
| 22 | 22 | import com.google.common.collect.Lists; |
| 23 | 23 | import org.junit.jupiter.api.Test; |
| 24 | 24 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 25 | 25 | import org.springframework.context.annotation.Import; |
| 26 | 26 | |
| 27 | 27 | import javax.annotation.Resource; |
| 28 | -import java.time.LocalDateTime; | |
| 29 | 28 | import java.util.List; |
| 30 | 29 | import java.util.Map; |
| 31 | 30 | import java.util.function.Consumer; |
| 32 | 31 | |
| 33 | -import static cn.hutool.core.bean.BeanUtil.getFieldValue; | |
| 34 | 32 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 35 | -import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max; | |
| 36 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 33 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 37 | 34 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 38 | 35 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 39 | 36 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 40 | 37 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 38 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 41 | 39 | import static org.junit.jupiter.api.Assertions.*; |
| 42 | 40 | import static org.mockito.ArgumentMatchers.eq; |
| 43 | 41 | import static org.mockito.Mockito.*; |
| 44 | 42 | |
| 45 | 43 | @Import(SmsTemplateServiceImpl.class) |
| 46 | -public class SmsTemplateServiceTest extends BaseDbUnitTest { | |
| 44 | +public class SmsTemplateServiceImplTest extends BaseDbUnitTest { | |
| 47 | 45 | |
| 48 | 46 | @Resource |
| 49 | 47 | private SmsTemplateServiceImpl smsTemplateService; |
| ... | ... | @@ -226,7 +224,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest { |
| 226 | 224 | reqVO.setContent("芋道"); |
| 227 | 225 | reqVO.setApiTemplateId("yu"); |
| 228 | 226 | reqVO.setChannelId(1L); |
| 229 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 11, 1),buildTime(2021, 12, 1)})); | |
| 227 | + reqVO.setCreateTime(buildBetweenTime(2021, 11, 1, 2021, 12, 1)); | |
| 230 | 228 | |
| 231 | 229 | // 调用 |
| 232 | 230 | PageResult<SmsTemplateDO> pageResult = smsTemplateService.getSmsTemplatePage(reqVO); |
| ... | ... | @@ -271,7 +269,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest { |
| 271 | 269 | reqVO.setContent("芋道"); |
| 272 | 270 | reqVO.setApiTemplateId("yu"); |
| 273 | 271 | reqVO.setChannelId(1L); |
| 274 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 11, 1),buildTime(2021, 12, 1)})); | |
| 272 | + reqVO.setCreateTime(buildBetweenTime(2021, 11, 1, 2021, 12, 1)); | |
| 275 | 273 | |
| 276 | 274 | // 调用 |
| 277 | 275 | List<SmsTemplateDO> list = smsTemplateService.getSmsTemplateList(reqVO); |
| ... | ... | @@ -281,7 +279,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest { |
| 281 | 279 | } |
| 282 | 280 | |
| 283 | 281 | @Test |
| 284 | - public void testCheckSmsChannel_success() { | |
| 282 | + public void testValidateSmsChannel_success() { | |
| 285 | 283 | // 准备参数 |
| 286 | 284 | Long channelId = randomLongId(); |
| 287 | 285 | // mock 方法 |
| ... | ... | @@ -292,23 +290,23 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest { |
| 292 | 290 | when(smsChannelService.getSmsChannel(eq(channelId))).thenReturn(channelDO); |
| 293 | 291 | |
| 294 | 292 | // 调用 |
| 295 | - SmsChannelDO returnChannelDO = smsTemplateService.checkSmsChannel(channelId); | |
| 293 | + SmsChannelDO returnChannelDO = smsTemplateService.validateSmsChannel(channelId); | |
| 296 | 294 | // 断言 |
| 297 | 295 | assertPojoEquals(returnChannelDO, channelDO); |
| 298 | 296 | } |
| 299 | 297 | |
| 300 | 298 | @Test |
| 301 | - public void testCheckSmsChannel_notExists() { | |
| 299 | + public void testValidateSmsChannel_notExists() { | |
| 302 | 300 | // 准备参数 |
| 303 | 301 | Long channelId = randomLongId(); |
| 304 | 302 | |
| 305 | 303 | // 调用,校验异常 |
| 306 | - assertServiceException(() -> smsTemplateService.checkSmsChannel(channelId), | |
| 304 | + assertServiceException(() -> smsTemplateService.validateSmsChannel(channelId), | |
| 307 | 305 | SMS_CHANNEL_NOT_EXISTS); |
| 308 | 306 | } |
| 309 | 307 | |
| 310 | 308 | @Test |
| 311 | - public void testCheckSmsChannel_disable() { | |
| 309 | + public void testValidateSmsChannel_disable() { | |
| 312 | 310 | // 准备参数 |
| 313 | 311 | Long channelId = randomLongId(); |
| 314 | 312 | // mock 方法 |
| ... | ... | @@ -319,30 +317,30 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest { |
| 319 | 317 | when(smsChannelService.getSmsChannel(eq(channelId))).thenReturn(channelDO); |
| 320 | 318 | |
| 321 | 319 | // 调用,校验异常 |
| 322 | - assertServiceException(() -> smsTemplateService.checkSmsChannel(channelId), | |
| 320 | + assertServiceException(() -> smsTemplateService.validateSmsChannel(channelId), | |
| 323 | 321 | SMS_CHANNEL_DISABLE); |
| 324 | 322 | } |
| 325 | 323 | |
| 326 | 324 | @Test |
| 327 | - public void testCheckDictDataValueUnique_success() { | |
| 325 | + public void testValidateDictDataValueUnique_success() { | |
| 328 | 326 | // 调用,成功 |
| 329 | - smsTemplateService.checkSmsTemplateCodeDuplicate(randomLongId(), randomString()); | |
| 327 | + smsTemplateService.validateSmsTemplateCodeDuplicate(randomLongId(), randomString()); | |
| 330 | 328 | } |
| 331 | 329 | |
| 332 | 330 | @Test |
| 333 | - public void testCheckSmsTemplateCodeDuplicate_valueDuplicateForCreate() { | |
| 331 | + public void testValidateSmsTemplateCodeDuplicate_valueDuplicateForCreate() { | |
| 334 | 332 | // 准备参数 |
| 335 | 333 | String code = randomString(); |
| 336 | 334 | // mock 数据 |
| 337 | 335 | smsTemplateMapper.insert(randomSmsTemplateDO(o -> o.setCode(code))); |
| 338 | 336 | |
| 339 | 337 | // 调用,校验异常 |
| 340 | - assertServiceException(() -> smsTemplateService.checkSmsTemplateCodeDuplicate(null, code), | |
| 338 | + assertServiceException(() -> smsTemplateService.validateSmsTemplateCodeDuplicate(null, code), | |
| 341 | 339 | SMS_TEMPLATE_CODE_DUPLICATE, code); |
| 342 | 340 | } |
| 343 | 341 | |
| 344 | 342 | @Test |
| 345 | - public void testCheckDictDataValueUnique_valueDuplicateForUpdate() { | |
| 343 | + public void testValidateDictDataValueUnique_valueDuplicateForUpdate() { | |
| 346 | 344 | // 准备参数 |
| 347 | 345 | Long id = randomLongId(); |
| 348 | 346 | String code = randomString(); |
| ... | ... | @@ -350,7 +348,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest { |
| 350 | 348 | smsTemplateMapper.insert(randomSmsTemplateDO(o -> o.setCode(code))); |
| 351 | 349 | |
| 352 | 350 | // 调用,校验异常 |
| 353 | - assertServiceException(() -> smsTemplateService.checkSmsTemplateCodeDuplicate(id, code), | |
| 351 | + assertServiceException(() -> smsTemplateService.validateSmsTemplateCodeDuplicate(id, code), | |
| 354 | 352 | SMS_TEMPLATE_CODE_DUPLICATE, code); |
| 355 | 353 | } |
| 356 | 354 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImplTest.java
| ... | ... | @@ -35,7 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | 35 | import static org.mockito.Mockito.*; |
| 36 | 36 | |
| 37 | 37 | @Import(SocialUserServiceImpl.class) |
| 38 | -public class SocialUserServiceTest extends BaseDbAndRedisUnitTest { | |
| 38 | +public class SocialUserServiceImplTest extends BaseDbAndRedisUnitTest { | |
| 39 | 39 | |
| 40 | 40 | @Resource |
| 41 | 41 | private SocialUserServiceImpl socialUserService; | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImplTest.java
| ... | ... | @@ -2,21 +2,22 @@ package cn.iocoder.yudao.module.system.service.tenant; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 5 | 6 | import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageCreateReqVO; |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackagePageReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageUpdateReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; |
| 9 | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; |
| 10 | 11 | import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantPackageMapper; |
| 11 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 12 | 12 | import org.junit.jupiter.api.Test; |
| 13 | 13 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | 14 | import org.springframework.context.annotation.Import; |
| 15 | 15 | |
| 16 | 16 | import javax.annotation.Resource; |
| 17 | 17 | |
| 18 | -import java.time.LocalDateTime; | |
| 18 | +import java.util.List; | |
| 19 | 19 | |
| 20 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 20 | 21 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 21 | 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; |
| ... | ... | @@ -158,7 +159,7 @@ public class TenantPackageServiceImplTest extends BaseDbUnitTest { |
| 158 | 159 | reqVO.setName("芋道"); |
| 159 | 160 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 160 | 161 | reqVO.setRemark("源码"); |
| 161 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 10, 9),buildTime(2022, 10, 11)})); | |
| 162 | + reqVO.setCreateTime(buildBetweenTime(2022, 10, 9, 2022, 10, 11)); | |
| 162 | 163 | |
| 163 | 164 | // 调用 |
| 164 | 165 | PageResult<TenantPackageDO> pageResult = tenantPackageService.getTenantPackagePage(reqVO); |
| ... | ... | @@ -201,4 +202,34 @@ public class TenantPackageServiceImplTest extends BaseDbUnitTest { |
| 201 | 202 | assertServiceException(() -> tenantPackageService.validTenantPackage(dbTenantPackage.getId()), |
| 202 | 203 | TENANT_PACKAGE_DISABLE, dbTenantPackage.getName()); |
| 203 | 204 | } |
| 205 | + | |
| 206 | + @Test | |
| 207 | + public void testGetTenantPackage() { | |
| 208 | + // mock 数据 | |
| 209 | + TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class); | |
| 210 | + tenantPackageMapper.insert(dbTenantPackage);// @Sql: 先插入出一条存在的数据 | |
| 211 | + | |
| 212 | + // 调用 | |
| 213 | + TenantPackageDO result = tenantPackageService.getTenantPackage(dbTenantPackage.getId()); | |
| 214 | + // 断言 | |
| 215 | + assertPojoEquals(result, dbTenantPackage); | |
| 216 | + } | |
| 217 | + | |
| 218 | + @Test | |
| 219 | + public void testGetTenantPackageListByStatus() { | |
| 220 | + // mock 数据 | |
| 221 | + TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class, | |
| 222 | + o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 223 | + tenantPackageMapper.insert(dbTenantPackage); | |
| 224 | + // 测试 status 不匹配 | |
| 225 | + tenantPackageMapper.insert(cloneIgnoreId(dbTenantPackage, | |
| 226 | + o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 227 | + | |
| 228 | + // 调用 | |
| 229 | + List<TenantPackageDO> list = tenantPackageService.getTenantPackageListByStatus( | |
| 230 | + CommonStatusEnum.ENABLE.getStatus()); | |
| 231 | + assertEquals(1, list.size()); | |
| 232 | + assertPojoEquals(dbTenantPackage, list.get(0)); | |
| 233 | + } | |
| 234 | + | |
| 204 | 235 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImplTest.java
| ... | ... | @@ -81,13 +81,13 @@ public class TenantServiceImplTest extends BaseDbUnitTest { |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | @Test |
| 84 | - public void testGetTenantIds() { | |
| 84 | + public void testGetTenantIdList() { | |
| 85 | 85 | // mock 数据 |
| 86 | 86 | TenantDO tenant = randomPojo(TenantDO.class, o -> o.setId(1L)); |
| 87 | 87 | tenantMapper.insert(tenant); |
| 88 | 88 | |
| 89 | 89 | // 调用,并断言业务异常 |
| 90 | - List<Long> result = tenantService.getTenantIds(); | |
| 90 | + List<Long> result = tenantService.getTenantIdList(); | |
| 91 | 91 | assertEquals(Collections.singletonList(1L), result); |
| 92 | 92 | } |
| 93 | 93 | |
| ... | ... | @@ -196,7 +196,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest { |
| 196 | 196 | role100.setTenantId(dbTenant.getId()); |
| 197 | 197 | RoleDO role101 = randomPojo(RoleDO.class, o -> o.setId(101L)); |
| 198 | 198 | role101.setTenantId(dbTenant.getId()); |
| 199 | - when(roleService.getRoles(isNull())).thenReturn(asList(role100, role101)); | |
| 199 | + when(roleService.getRoleListByStatus(isNull())).thenReturn(asList(role100, role101)); | |
| 200 | 200 | // mock 每个角色的权限 |
| 201 | 201 | when(permissionService.getRoleMenuIds(eq(101L))).thenReturn(asSet(201L, 202L)); |
| 202 | 202 | |
| ... | ... | @@ -453,7 +453,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest { |
| 453 | 453 | tenantMapper.insert(dbTenant);// @Sql: 先插入出一条存在的数据 |
| 454 | 454 | TenantContextHolder.setTenantId(dbTenant.getId()); |
| 455 | 455 | // mock 菜单 |
| 456 | - when(menuService.getMenus()).thenReturn(Arrays.asList(randomPojo(MenuDO.class, o -> o.setId(100L)), | |
| 456 | + when(menuService.getMenuList()).thenReturn(Arrays.asList(randomPojo(MenuDO.class, o -> o.setId(100L)), | |
| 457 | 457 | randomPojo(MenuDO.class, o -> o.setId(101L)))); |
| 458 | 458 | |
| 459 | 459 | // 调用 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java
| ... | ... | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 7 | 7 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 9 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 10 | 9 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 11 | 10 | import cn.iocoder.yudao.module.infra.api.file.FileApi; |
| 12 | 11 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; |
| ... | ... | @@ -32,23 +31,25 @@ import org.springframework.security.crypto.password.PasswordEncoder; |
| 32 | 31 | |
| 33 | 32 | import javax.annotation.Resource; |
| 34 | 33 | import java.io.ByteArrayInputStream; |
| 35 | -import java.time.LocalDateTime; | |
| 36 | 34 | import java.util.Collection; |
| 37 | 35 | import java.util.List; |
| 36 | +import java.util.Map; | |
| 38 | 37 | import java.util.function.Consumer; |
| 39 | 38 | |
| 40 | 39 | import static cn.hutool.core.util.RandomUtil.randomBytes; |
| 41 | 40 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 42 | 41 | import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; |
| 42 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 43 | 43 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 44 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 44 | 45 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 45 | 46 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 46 | 47 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 47 | 48 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 48 | 49 | import static java.util.Collections.singleton; |
| 50 | +import static java.util.Collections.singletonList; | |
| 49 | 51 | import static org.assertj.core.util.Lists.newArrayList; |
| 50 | -import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 51 | -import static org.junit.jupiter.api.Assertions.assertNull; | |
| 52 | +import static org.junit.jupiter.api.Assertions.*; | |
| 52 | 53 | import static org.mockito.ArgumentMatchers.*; |
| 53 | 54 | import static org.mockito.Mockito.*; |
| 54 | 55 | |
| ... | ... | @@ -102,7 +103,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 102 | 103 | o.setId(postId); |
| 103 | 104 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 104 | 105 | })); |
| 105 | - when(postService.getPosts(eq(reqVO.getPostIds()), isNull())).thenReturn(posts); | |
| 106 | + when(postService.getPostList(eq(reqVO.getPostIds()), isNull())).thenReturn(posts); | |
| 106 | 107 | // mock passwordEncoder 的方法 |
| 107 | 108 | when(passwordEncoder.encode(eq(reqVO.getPassword()))).thenReturn("yudaoyuanma"); |
| 108 | 109 | |
| ... | ... | @@ -160,7 +161,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 160 | 161 | o.setId(postId); |
| 161 | 162 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 162 | 163 | })); |
| 163 | - when(postService.getPosts(eq(reqVO.getPostIds()), isNull())).thenReturn(posts); | |
| 164 | + when(postService.getPostList(eq(reqVO.getPostIds()), isNull())).thenReturn(posts); | |
| 164 | 165 | |
| 165 | 166 | // 调用 |
| 166 | 167 | userService.updateUser(reqVO); |
| ... | ... | @@ -174,6 +175,23 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 174 | 175 | } |
| 175 | 176 | |
| 176 | 177 | @Test |
| 178 | + public void testUpdateUserLogin() { | |
| 179 | + // mock 数据 | |
| 180 | + AdminUserDO user = randomAdminUserDO(o -> o.setLoginDate(null)); | |
| 181 | + userMapper.insert(user); | |
| 182 | + // 准备参数 | |
| 183 | + Long id = user.getId(); | |
| 184 | + String loginIp = randomString(); | |
| 185 | + | |
| 186 | + // 调用 | |
| 187 | + userService.updateUserLogin(id, loginIp); | |
| 188 | + // 断言 | |
| 189 | + AdminUserDO dbUser = userMapper.selectById(id); | |
| 190 | + assertEquals(loginIp, dbUser.getLoginIp()); | |
| 191 | + assertNotNull(dbUser.getLoginDate()); | |
| 192 | + } | |
| 193 | + | |
| 194 | + @Test | |
| 177 | 195 | public void testUpdateUserProfile_success() { |
| 178 | 196 | // mock 数据 |
| 179 | 197 | AdminUserDO dbUser = randomAdminUserDO(); |
| ... | ... | @@ -287,6 +305,34 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 287 | 305 | } |
| 288 | 306 | |
| 289 | 307 | @Test |
| 308 | + public void testGetUserByUsername() { | |
| 309 | + // mock 数据 | |
| 310 | + AdminUserDO dbUser = randomAdminUserDO(); | |
| 311 | + userMapper.insert(dbUser); | |
| 312 | + // 准备参数 | |
| 313 | + String username = dbUser.getUsername(); | |
| 314 | + | |
| 315 | + // 调用 | |
| 316 | + AdminUserDO user = userService.getUserByUsername(username); | |
| 317 | + // 断言 | |
| 318 | + assertPojoEquals(dbUser, user); | |
| 319 | + } | |
| 320 | + | |
| 321 | + @Test | |
| 322 | + public void testGetUserByMobile() { | |
| 323 | + // mock 数据 | |
| 324 | + AdminUserDO dbUser = randomAdminUserDO(); | |
| 325 | + userMapper.insert(dbUser); | |
| 326 | + // 准备参数 | |
| 327 | + String mobile = dbUser.getMobile(); | |
| 328 | + | |
| 329 | + // 调用 | |
| 330 | + AdminUserDO user = userService.getUserByMobile(mobile); | |
| 331 | + // 断言 | |
| 332 | + assertPojoEquals(dbUser, user); | |
| 333 | + } | |
| 334 | + | |
| 335 | + @Test | |
| 290 | 336 | public void testGetUserPage() { |
| 291 | 337 | // mock 数据 |
| 292 | 338 | AdminUserDO dbUser = initGetUserPageData(); |
| ... | ... | @@ -295,11 +341,11 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 295 | 341 | reqVO.setUsername("tu"); |
| 296 | 342 | reqVO.setMobile("1560"); |
| 297 | 343 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 298 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)})); | |
| 344 | + reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24)); | |
| 299 | 345 | reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门 |
| 300 | 346 | // mock 方法 |
| 301 | 347 | List<DeptDO> deptList = newArrayList(randomPojo(DeptDO.class, o -> o.setId(2L))); |
| 302 | - when(deptService.getDeptsByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | |
| 348 | + when(deptService.getDeptListByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | |
| 303 | 349 | |
| 304 | 350 | // 调用 |
| 305 | 351 | PageResult<AdminUserDO> pageResult = userService.getUserPage(reqVO); |
| ... | ... | @@ -310,7 +356,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 310 | 356 | } |
| 311 | 357 | |
| 312 | 358 | @Test |
| 313 | - public void testGetUsers() { | |
| 359 | + public void testGetUserList_export() { | |
| 314 | 360 | // mock 数据 |
| 315 | 361 | AdminUserDO dbUser = initGetUserPageData(); |
| 316 | 362 | // 准备参数 |
| ... | ... | @@ -318,14 +364,14 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 318 | 364 | reqVO.setUsername("tu"); |
| 319 | 365 | reqVO.setMobile("1560"); |
| 320 | 366 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 321 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)})); | |
| 367 | + reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24)); | |
| 322 | 368 | reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门 |
| 323 | 369 | // mock 方法 |
| 324 | 370 | List<DeptDO> deptList = newArrayList(randomPojo(DeptDO.class, o -> o.setId(2L))); |
| 325 | - when(deptService.getDeptsByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | |
| 371 | + when(deptService.getDeptListByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | |
| 326 | 372 | |
| 327 | 373 | // 调用 |
| 328 | - List<AdminUserDO> list = userService.getUsers(reqVO); | |
| 374 | + List<AdminUserDO> list = userService.getUserList(reqVO); | |
| 329 | 375 | // 断言 |
| 330 | 376 | assertEquals(1, list.size()); |
| 331 | 377 | assertPojoEquals(dbUser, list.get(0)); |
| ... | ... | @@ -345,31 +391,62 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 345 | 391 | }); |
| 346 | 392 | userMapper.insert(dbUser); |
| 347 | 393 | // 测试 username 不匹配 |
| 348 | - userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setUsername("dou"))); | |
| 394 | + userMapper.insert(cloneIgnoreId(dbUser, o -> o.setUsername("dou"))); | |
| 349 | 395 | // 测试 mobile 不匹配 |
| 350 | - userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setMobile("18818260888"))); | |
| 396 | + userMapper.insert(cloneIgnoreId(dbUser, o -> o.setMobile("18818260888"))); | |
| 351 | 397 | // 测试 status 不匹配 |
| 352 | - userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 398 | + userMapper.insert(cloneIgnoreId(dbUser, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 353 | 399 | // 测试 createTime 不匹配 |
| 354 | - userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setCreateTime(buildTime(2020, 11, 11)))); | |
| 400 | + userMapper.insert(cloneIgnoreId(dbUser, o -> o.setCreateTime(buildTime(2020, 11, 11)))); | |
| 355 | 401 | // 测试 dept 不匹配 |
| 356 | - userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setDeptId(0L))); | |
| 402 | + userMapper.insert(cloneIgnoreId(dbUser, o -> o.setDeptId(0L))); | |
| 357 | 403 | return dbUser; |
| 358 | 404 | } |
| 359 | 405 | |
| 406 | + @Test | |
| 407 | + public void testGetUser() { | |
| 408 | + // mock 数据 | |
| 409 | + AdminUserDO dbUser = randomAdminUserDO(); | |
| 410 | + userMapper.insert(dbUser); | |
| 411 | + // 准备参数 | |
| 412 | + Long userId = dbUser.getId(); | |
| 413 | + | |
| 414 | + // 调用 | |
| 415 | + AdminUserDO user = userService.getUser(userId); | |
| 416 | + // 断言 | |
| 417 | + assertPojoEquals(dbUser, user); | |
| 418 | + } | |
| 419 | + | |
| 420 | + @Test | |
| 421 | + public void testGetUserListByDeptIds() { | |
| 422 | + // mock 数据 | |
| 423 | + AdminUserDO dbUser = randomAdminUserDO(o -> o.setDeptId(1L)); | |
| 424 | + userMapper.insert(dbUser); | |
| 425 | + // 测试 deptId 不匹配 | |
| 426 | + userMapper.insert(cloneIgnoreId(dbUser, o -> o.setDeptId(2L))); | |
| 427 | + // 准备参数 | |
| 428 | + Collection<Long> deptIds = singleton(1L); | |
| 429 | + | |
| 430 | + // 调用 | |
| 431 | + List<AdminUserDO> list = userService.getUserListByDeptIds(deptIds); | |
| 432 | + // 断言 | |
| 433 | + assertEquals(1, list.size()); | |
| 434 | + assertEquals(dbUser, list.get(0)); | |
| 435 | + } | |
| 436 | + | |
| 360 | 437 | /** |
| 361 | 438 | * 情况一,校验不通过,导致插入失败 |
| 362 | 439 | */ |
| 363 | 440 | @Test |
| 364 | - public void testImportUsers_01() { | |
| 441 | + public void testImportUserList_01() { | |
| 365 | 442 | // 准备参数 |
| 366 | 443 | UserImportExcelVO importUser = randomPojo(UserImportExcelVO.class, o -> { |
| 367 | 444 | }); |
| 368 | 445 | // mock 方法,模拟失败 |
| 369 | - doThrow(new ServiceException(DEPT_NOT_FOUND)).when(deptService).validDepts(any()); | |
| 446 | + doThrow(new ServiceException(DEPT_NOT_FOUND)).when(deptService).validateDeptList(any()); | |
| 370 | 447 | |
| 371 | 448 | // 调用 |
| 372 | - UserImportRespVO respVO = userService.importUsers(newArrayList(importUser), true); | |
| 449 | + UserImportRespVO respVO = userService.importUserList(newArrayList(importUser), true); | |
| 373 | 450 | // 断言 |
| 374 | 451 | assertEquals(0, respVO.getCreateUsernames().size()); |
| 375 | 452 | assertEquals(0, respVO.getUpdateUsernames().size()); |
| ... | ... | @@ -381,7 +458,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 381 | 458 | * 情况二,不存在,进行插入 |
| 382 | 459 | */ |
| 383 | 460 | @Test |
| 384 | - public void testImportUsers_02() { | |
| 461 | + public void testImportUserList_02() { | |
| 385 | 462 | // 准备参数 |
| 386 | 463 | UserImportExcelVO importUser = randomPojo(UserImportExcelVO.class, o -> { |
| 387 | 464 | o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 |
| ... | ... | @@ -397,7 +474,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 397 | 474 | when(passwordEncoder.encode(eq("yudaoyuanma"))).thenReturn("java"); |
| 398 | 475 | |
| 399 | 476 | // 调用 |
| 400 | - UserImportRespVO respVO = userService.importUsers(newArrayList(importUser), true); | |
| 477 | + UserImportRespVO respVO = userService.importUserList(newArrayList(importUser), true); | |
| 401 | 478 | // 断言 |
| 402 | 479 | assertEquals(1, respVO.getCreateUsernames().size()); |
| 403 | 480 | AdminUserDO user = userMapper.selectByUsername(respVO.getCreateUsernames().get(0)); |
| ... | ... | @@ -411,7 +488,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 411 | 488 | * 情况三,存在,但是不强制更新 |
| 412 | 489 | */ |
| 413 | 490 | @Test |
| 414 | - public void testImportUsers_03() { | |
| 491 | + public void testImportUserList_03() { | |
| 415 | 492 | // mock 数据 |
| 416 | 493 | AdminUserDO dbUser = randomAdminUserDO(); |
| 417 | 494 | userMapper.insert(dbUser); |
| ... | ... | @@ -429,7 +506,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 429 | 506 | when(deptService.getDept(eq(dept.getId()))).thenReturn(dept); |
| 430 | 507 | |
| 431 | 508 | // 调用 |
| 432 | - UserImportRespVO respVO = userService.importUsers(newArrayList(importUser), false); | |
| 509 | + UserImportRespVO respVO = userService.importUserList(newArrayList(importUser), false); | |
| 433 | 510 | // 断言 |
| 434 | 511 | assertEquals(0, respVO.getCreateUsernames().size()); |
| 435 | 512 | assertEquals(0, respVO.getUpdateUsernames().size()); |
| ... | ... | @@ -441,7 +518,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 441 | 518 | * 情况四,存在,强制更新 |
| 442 | 519 | */ |
| 443 | 520 | @Test |
| 444 | - public void testImportUsers_04() { | |
| 521 | + public void testImportUserList_04() { | |
| 445 | 522 | // mock 数据 |
| 446 | 523 | AdminUserDO dbUser = randomAdminUserDO(); |
| 447 | 524 | userMapper.insert(dbUser); |
| ... | ... | @@ -459,7 +536,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 459 | 536 | when(deptService.getDept(eq(dept.getId()))).thenReturn(dept); |
| 460 | 537 | |
| 461 | 538 | // 调用 |
| 462 | - UserImportRespVO respVO = userService.importUsers(newArrayList(importUser), true); | |
| 539 | + UserImportRespVO respVO = userService.importUserList(newArrayList(importUser), true); | |
| 463 | 540 | // 断言 |
| 464 | 541 | assertEquals(0, respVO.getCreateUsernames().size()); |
| 465 | 542 | assertEquals(1, respVO.getUpdateUsernames().size()); |
| ... | ... | @@ -469,24 +546,24 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 469 | 546 | } |
| 470 | 547 | |
| 471 | 548 | @Test |
| 472 | - public void testCheckUserExists_notExists() { | |
| 473 | - assertServiceException(() -> userService.checkUserExists(randomLongId()), USER_NOT_EXISTS); | |
| 549 | + public void testValidateUserExists_notExists() { | |
| 550 | + assertServiceException(() -> userService.validateUserExists(randomLongId()), USER_NOT_EXISTS); | |
| 474 | 551 | } |
| 475 | 552 | |
| 476 | 553 | @Test |
| 477 | - public void testCheckUsernameUnique_usernameExistsForCreate() { | |
| 554 | + public void testValidateUsernameUnique_usernameExistsForCreate() { | |
| 478 | 555 | // 准备参数 |
| 479 | 556 | String username = randomString(); |
| 480 | 557 | // mock 数据 |
| 481 | 558 | userMapper.insert(randomAdminUserDO(o -> o.setUsername(username))); |
| 482 | 559 | |
| 483 | 560 | // 调用,校验异常 |
| 484 | - assertServiceException(() -> userService.checkUsernameUnique(null, username), | |
| 561 | + assertServiceException(() -> userService.validateUsernameUnique(null, username), | |
| 485 | 562 | USER_USERNAME_EXISTS); |
| 486 | 563 | } |
| 487 | 564 | |
| 488 | 565 | @Test |
| 489 | - public void testCheckUsernameUnique_usernameExistsForUpdate() { | |
| 566 | + public void testValidateUsernameUnique_usernameExistsForUpdate() { | |
| 490 | 567 | // 准备参数 |
| 491 | 568 | Long id = randomLongId(); |
| 492 | 569 | String username = randomString(); |
| ... | ... | @@ -494,24 +571,24 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 494 | 571 | userMapper.insert(randomAdminUserDO(o -> o.setUsername(username))); |
| 495 | 572 | |
| 496 | 573 | // 调用,校验异常 |
| 497 | - assertServiceException(() -> userService.checkUsernameUnique(id, username), | |
| 574 | + assertServiceException(() -> userService.validateUsernameUnique(id, username), | |
| 498 | 575 | USER_USERNAME_EXISTS); |
| 499 | 576 | } |
| 500 | 577 | |
| 501 | 578 | @Test |
| 502 | - public void testCheckEmailUnique_emailExistsForCreate() { | |
| 579 | + public void testValidateEmailUnique_emailExistsForCreate() { | |
| 503 | 580 | // 准备参数 |
| 504 | 581 | String email = randomString(); |
| 505 | 582 | // mock 数据 |
| 506 | 583 | userMapper.insert(randomAdminUserDO(o -> o.setEmail(email))); |
| 507 | 584 | |
| 508 | 585 | // 调用,校验异常 |
| 509 | - assertServiceException(() -> userService.checkEmailUnique(null, email), | |
| 586 | + assertServiceException(() -> userService.validateEmailUnique(null, email), | |
| 510 | 587 | USER_EMAIL_EXISTS); |
| 511 | 588 | } |
| 512 | 589 | |
| 513 | 590 | @Test |
| 514 | - public void testCheckEmailUnique_emailExistsForUpdate() { | |
| 591 | + public void testValidateEmailUnique_emailExistsForUpdate() { | |
| 515 | 592 | // 准备参数 |
| 516 | 593 | Long id = randomLongId(); |
| 517 | 594 | String email = randomString(); |
| ... | ... | @@ -519,24 +596,24 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 519 | 596 | userMapper.insert(randomAdminUserDO(o -> o.setEmail(email))); |
| 520 | 597 | |
| 521 | 598 | // 调用,校验异常 |
| 522 | - assertServiceException(() -> userService.checkEmailUnique(id, email), | |
| 599 | + assertServiceException(() -> userService.validateEmailUnique(id, email), | |
| 523 | 600 | USER_EMAIL_EXISTS); |
| 524 | 601 | } |
| 525 | 602 | |
| 526 | 603 | @Test |
| 527 | - public void testCheckMobileUnique_mobileExistsForCreate() { | |
| 604 | + public void testValidateMobileUnique_mobileExistsForCreate() { | |
| 528 | 605 | // 准备参数 |
| 529 | 606 | String mobile = randomString(); |
| 530 | 607 | // mock 数据 |
| 531 | 608 | userMapper.insert(randomAdminUserDO(o -> o.setMobile(mobile))); |
| 532 | 609 | |
| 533 | 610 | // 调用,校验异常 |
| 534 | - assertServiceException(() -> userService.checkMobileUnique(null, mobile), | |
| 611 | + assertServiceException(() -> userService.validateMobileUnique(null, mobile), | |
| 535 | 612 | USER_MOBILE_EXISTS); |
| 536 | 613 | } |
| 537 | 614 | |
| 538 | 615 | @Test |
| 539 | - public void testCheckMobileUnique_mobileExistsForUpdate() { | |
| 616 | + public void testValidateMobileUnique_mobileExistsForUpdate() { | |
| 540 | 617 | // 准备参数 |
| 541 | 618 | Long id = randomLongId(); |
| 542 | 619 | String mobile = randomString(); |
| ... | ... | @@ -544,18 +621,18 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 544 | 621 | userMapper.insert(randomAdminUserDO(o -> o.setMobile(mobile))); |
| 545 | 622 | |
| 546 | 623 | // 调用,校验异常 |
| 547 | - assertServiceException(() -> userService.checkMobileUnique(id, mobile), | |
| 624 | + assertServiceException(() -> userService.validateMobileUnique(id, mobile), | |
| 548 | 625 | USER_MOBILE_EXISTS); |
| 549 | 626 | } |
| 550 | 627 | |
| 551 | 628 | @Test |
| 552 | - public void testCheckOldPassword_notExists() { | |
| 553 | - assertServiceException(() -> userService.checkOldPassword(randomLongId(), randomString()), | |
| 629 | + public void testValidateOldPassword_notExists() { | |
| 630 | + assertServiceException(() -> userService.validateOldPassword(randomLongId(), randomString()), | |
| 554 | 631 | USER_NOT_EXISTS); |
| 555 | 632 | } |
| 556 | 633 | |
| 557 | 634 | @Test |
| 558 | - public void testCheckOldPassword_passwordFailed() { | |
| 635 | + public void testValidateOldPassword_passwordFailed() { | |
| 559 | 636 | // mock 数据 |
| 560 | 637 | AdminUserDO user = randomAdminUserDO(); |
| 561 | 638 | userMapper.insert(user); |
| ... | ... | @@ -564,14 +641,14 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 564 | 641 | String oldPassword = user.getPassword(); |
| 565 | 642 | |
| 566 | 643 | // 调用,校验异常 |
| 567 | - assertServiceException(() -> userService.checkOldPassword(id, oldPassword), | |
| 644 | + assertServiceException(() -> userService.validateOldPassword(id, oldPassword), | |
| 568 | 645 | USER_PASSWORD_FAILED); |
| 569 | 646 | // 校验调用 |
| 570 | 647 | verify(passwordEncoder, times(1)).matches(eq(oldPassword), eq(user.getPassword())); |
| 571 | 648 | } |
| 572 | 649 | |
| 573 | 650 | @Test |
| 574 | - public void testUsersByPostIds() { | |
| 651 | + public void testUserListByPostIds() { | |
| 575 | 652 | // 准备参数 |
| 576 | 653 | Collection<Long> postIds = asSet(10L, 20L); |
| 577 | 654 | // mock user1 数据 |
| ... | ... | @@ -585,12 +662,114 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 585 | 662 | userPostMapper.insert(new UserPostDO().setUserId(user2.getId()).setPostId(100L)); |
| 586 | 663 | |
| 587 | 664 | // 调用 |
| 588 | - List<AdminUserDO> result = userService.getUsersByPostIds(postIds); | |
| 665 | + List<AdminUserDO> result = userService.getUserListByPostIds(postIds); | |
| 589 | 666 | // 断言 |
| 590 | 667 | assertEquals(1, result.size()); |
| 591 | 668 | assertEquals(user1, result.get(0)); |
| 592 | 669 | } |
| 593 | 670 | |
| 671 | + @Test | |
| 672 | + public void testGetUserList() { | |
| 673 | + // mock 数据 | |
| 674 | + AdminUserDO user = randomAdminUserDO(); | |
| 675 | + userMapper.insert(user); | |
| 676 | + // 测试 id 不匹配 | |
| 677 | + userMapper.insert(randomAdminUserDO()); | |
| 678 | + // 准备参数 | |
| 679 | + Collection<Long> ids = singleton(user.getId()); | |
| 680 | + | |
| 681 | + // 调用 | |
| 682 | + List<AdminUserDO> result = userService.getUserList(ids); | |
| 683 | + // 断言 | |
| 684 | + assertEquals(1, result.size()); | |
| 685 | + assertEquals(user, result.get(0)); | |
| 686 | + } | |
| 687 | + | |
| 688 | + @Test | |
| 689 | + public void testGetUserMap() { | |
| 690 | + // mock 数据 | |
| 691 | + AdminUserDO user = randomAdminUserDO(); | |
| 692 | + userMapper.insert(user); | |
| 693 | + // 测试 id 不匹配 | |
| 694 | + userMapper.insert(randomAdminUserDO()); | |
| 695 | + // 准备参数 | |
| 696 | + Collection<Long> ids = singleton(user.getId()); | |
| 697 | + | |
| 698 | + // 调用 | |
| 699 | + Map<Long, AdminUserDO> result = userService.getUserMap(ids); | |
| 700 | + // 断言 | |
| 701 | + assertEquals(1, result.size()); | |
| 702 | + assertEquals(user, result.get(user.getId())); | |
| 703 | + } | |
| 704 | + | |
| 705 | + @Test | |
| 706 | + public void testGetUserListByNickname() { | |
| 707 | + // mock 数据 | |
| 708 | + AdminUserDO user = randomAdminUserDO(o -> o.setNickname("芋头")); | |
| 709 | + userMapper.insert(user); | |
| 710 | + // 测试 nickname 不匹配 | |
| 711 | + userMapper.insert(randomAdminUserDO(o -> o.setNickname("源码"))); | |
| 712 | + // 准备参数 | |
| 713 | + String nickname = "芋"; | |
| 714 | + | |
| 715 | + // 调用 | |
| 716 | + List<AdminUserDO> result = userService.getUserListByNickname(nickname); | |
| 717 | + // 断言 | |
| 718 | + assertEquals(1, result.size()); | |
| 719 | + assertEquals(user, result.get(0)); | |
| 720 | + } | |
| 721 | + | |
| 722 | + @Test | |
| 723 | + public void testGetUserListByStatus() { | |
| 724 | + // mock 数据 | |
| 725 | + AdminUserDO user = randomAdminUserDO(o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 726 | + userMapper.insert(user); | |
| 727 | + // 测试 status 不匹配 | |
| 728 | + userMapper.insert(randomAdminUserDO(o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()))); | |
| 729 | + // 准备参数 | |
| 730 | + Integer status = CommonStatusEnum.DISABLE.getStatus(); | |
| 731 | + | |
| 732 | + // 调用 | |
| 733 | + List<AdminUserDO> result = userService.getUserListByStatus(status); | |
| 734 | + // 断言 | |
| 735 | + assertEquals(1, result.size()); | |
| 736 | + assertEquals(user, result.get(0)); | |
| 737 | + } | |
| 738 | + | |
| 739 | + @Test | |
| 740 | + public void testValidateUserList_success() { | |
| 741 | + // mock 数据 | |
| 742 | + AdminUserDO userDO = randomAdminUserDO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 743 | + userMapper.insert(userDO); | |
| 744 | + // 准备参数 | |
| 745 | + List<Long> ids = singletonList(userDO.getId()); | |
| 746 | + | |
| 747 | + // 调用,无需断言 | |
| 748 | + userService.validateUserList(ids); | |
| 749 | + } | |
| 750 | + | |
| 751 | + @Test | |
| 752 | + public void testValidateUserList_notFound() { | |
| 753 | + // 准备参数 | |
| 754 | + List<Long> ids = singletonList(randomLongId()); | |
| 755 | + | |
| 756 | + // 调用, 并断言异常 | |
| 757 | + assertServiceException(() -> userService.validateUserList(ids), USER_NOT_EXISTS); | |
| 758 | + } | |
| 759 | + | |
| 760 | + @Test | |
| 761 | + public void testValidateUserList_notEnable() { | |
| 762 | + // mock 数据 | |
| 763 | + AdminUserDO userDO = randomAdminUserDO().setStatus(CommonStatusEnum.DISABLE.getStatus()); | |
| 764 | + userMapper.insert(userDO); | |
| 765 | + // 准备参数 | |
| 766 | + List<Long> ids = singletonList(userDO.getId()); | |
| 767 | + | |
| 768 | + // 调用, 并断言异常 | |
| 769 | + assertServiceException(() -> userService.validateUserList(ids), USER_IS_DISABLE, | |
| 770 | + userDO.getNickname()); | |
| 771 | + } | |
| 772 | + | |
| 594 | 773 | // ========== 随机对象 ========== |
| 595 | 774 | |
| 596 | 775 | @SafeVarargs | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql
| ... | ... | @@ -14,6 +14,7 @@ DELETE FROM "system_users"; |
| 14 | 14 | DELETE FROM "system_sms_channel"; |
| 15 | 15 | DELETE FROM "system_sms_template"; |
| 16 | 16 | DELETE FROM "system_sms_log"; |
| 17 | +DELETE FROM "system_sms_code"; | |
| 17 | 18 | DELETE FROM "system_error_code"; |
| 18 | 19 | DELETE FROM "system_social_user"; |
| 19 | 20 | DELETE FROM "system_social_user_bind"; | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql
| ... | ... | @@ -377,6 +377,24 @@ CREATE TABLE IF NOT EXISTS "system_sms_log" ( |
| 377 | 377 | PRIMARY KEY ("id") |
| 378 | 378 | ) COMMENT '短信日志'; |
| 379 | 379 | |
| 380 | +CREATE TABLE IF NOT EXISTS "system_sms_code" ( | |
| 381 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 382 | + "mobile" varchar(11) NOT NULL, | |
| 383 | + "code" varchar(11) NOT NULL, | |
| 384 | + "scene" bigint NOT NULL, | |
| 385 | + "create_ip" varchar NOT NULL, | |
| 386 | + "today_index" int NOT NULL, | |
| 387 | + "used" bit NOT NULL DEFAULT FALSE, | |
| 388 | + "used_time" timestamp DEFAULT NULL, | |
| 389 | + "used_ip" varchar NULL, | |
| 390 | + "creator" varchar(64) DEFAULT '', | |
| 391 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 392 | + "updater" varchar(64) DEFAULT '', | |
| 393 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 394 | + "deleted" bit NOT NULL DEFAULT FALSE, | |
| 395 | + PRIMARY KEY ("id") | |
| 396 | +) COMMENT '短信日志'; | |
| 397 | + | |
| 380 | 398 | CREATE TABLE IF NOT EXISTS "system_error_code" ( |
| 381 | 399 | "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, |
| 382 | 400 | "type" tinyint NOT NULL DEFAULT '0', | ... | ... |
yudao-server/pom.xml
| ... | ... | @@ -48,11 +48,11 @@ |
| 48 | 48 | <!-- <version>${revision}</version>--> |
| 49 | 49 | <!-- </dependency>--> |
| 50 | 50 | <!-- 工作流 --> |
| 51 | -<!-- <dependency>--> | |
| 52 | -<!-- <groupId>cn.iocoder.boot</groupId>--> | |
| 53 | -<!-- <artifactId>yudao-module-bpm-biz</artifactId>--> | |
| 54 | -<!-- <version>${revision}</version>--> | |
| 55 | -<!-- </dependency>--> | |
| 51 | + <dependency> | |
| 52 | + <groupId>cn.iocoder.boot</groupId> | |
| 53 | + <artifactId>yudao-module-bpm-biz</artifactId> | |
| 54 | + <version>${revision}</version> | |
| 55 | + </dependency> | |
| 56 | 56 | <!-- 支付服务 --> |
| 57 | 57 | <dependency> |
| 58 | 58 | <groupId>cn.iocoder.boot</groupId> | ... | ... |