mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-02-17 09:40:34 +08:00
!392 完善 system 模块的单元测试,单测数量 423,方法行覆盖率 95%,行覆盖率 93%
Merge pull request !392 from 芋道源码/feature/dev-yunai
This commit is contained in:
commit
045c60fcd7
2
pom.xml
2
pom.xml
@ -17,7 +17,7 @@
|
||||
<module>yudao-module-system</module>
|
||||
<module>yudao-module-infra</module>
|
||||
<module>yudao-module-pay</module>
|
||||
<!-- <module>yudao-module-bpm</module>-->
|
||||
<module>yudao-module-bpm</module>
|
||||
<!-- <module>yudao-module-visualization</module>-->
|
||||
<!-- <module>yudao-module-mp</module>-->
|
||||
<!-- <module>yudao-module-mall</module>-->
|
||||
|
@ -0,0 +1,43 @@
|
||||
package cn.iocoder.yudao.framework.datapermission.core.util;
|
||||
|
||||
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
|
||||
import cn.iocoder.yudao.framework.datapermission.core.aop.DataPermissionContextHolder;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
/**
|
||||
* 数据权限 Util
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class DataPermissionUtils {
|
||||
|
||||
private static DataPermission DATA_PERMISSION_DISABLE;
|
||||
|
||||
@DataPermission(enable = false)
|
||||
@SneakyThrows
|
||||
private static DataPermission getDisableDataPermissionDisable() {
|
||||
if (DATA_PERMISSION_DISABLE == null) {
|
||||
DATA_PERMISSION_DISABLE = DataPermissionUtils.class
|
||||
.getDeclaredMethod("getDisableDataPermissionDisable")
|
||||
.getAnnotation(DataPermission.class);
|
||||
}
|
||||
return DATA_PERMISSION_DISABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略数据权限,执行对应的逻辑
|
||||
*
|
||||
* @param runnable 逻辑
|
||||
*/
|
||||
public static void executeIgnore(Runnable runnable) {
|
||||
DataPermission dataPermission = getDisableDataPermissionDisable();
|
||||
DataPermissionContextHolder.add(dataPermission);
|
||||
try {
|
||||
// 执行 runnable
|
||||
runnable.run();
|
||||
} finally {
|
||||
DataPermissionContextHolder.remove();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package cn.iocoder.yudao.framework.datapermission.core.util;
|
||||
|
||||
import cn.iocoder.yudao.framework.datapermission.core.aop.DataPermissionContextHolder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class DataPermissionUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testExecuteIgnore() {
|
||||
DataPermissionUtils.executeIgnore(() -> assertFalse(DataPermissionContextHolder.get().enable()));
|
||||
}
|
||||
|
||||
}
|
@ -49,7 +49,7 @@ public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator {
|
||||
log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size());
|
||||
|
||||
// 第二步,写入到 system 服务
|
||||
errorCodeApi.autoGenerateErrorCodes(autoGenerateDTOs);
|
||||
errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs);
|
||||
log.info("[execute][写入到 system 组件完成]");
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService {
|
||||
|
||||
@Override
|
||||
public List<Long> load(Object key) {
|
||||
return tenantApi.getTenantIds();
|
||||
return tenantApi.getTenantIdList();
|
||||
}
|
||||
|
||||
});
|
||||
@ -47,7 +47,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService {
|
||||
@Override
|
||||
public ServiceException load(Long id) {
|
||||
try {
|
||||
tenantApi.validTenant(id);
|
||||
tenantApi.validateTenant(id);
|
||||
return SERVICE_EXCEPTION_NULL;
|
||||
} catch (ServiceException ex) {
|
||||
return ex;
|
||||
|
@ -50,7 +50,7 @@ public class RandomUtils {
|
||||
}
|
||||
// 如果是 type、status 结尾的字段,返回 tinyint 范围
|
||||
if (StrUtil.endWithAnyIgnoreCase(attributeMetadata.getAttributeName(),
|
||||
"type", "status", "category", "scope")) {
|
||||
"type", "status", "category", "scope", "result")) {
|
||||
return RandomUtil.randomInt(0, TINYINT_MAX + 1);
|
||||
}
|
||||
return RandomUtil.randomInt();
|
||||
|
@ -213,18 +213,18 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
|
||||
|
||||
private void validTaskAssignRuleOptions(Integer type, Set<Long> options) {
|
||||
if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.ROLE.getType())) {
|
||||
roleApi.validRoles(options);
|
||||
roleApi.validRoleList(options);
|
||||
} else if (ObjectUtils.equalsAny(type, BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(),
|
||||
BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) {
|
||||
deptApi.validDepts(options);
|
||||
deptApi.validateDeptList(options);
|
||||
} else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.POST.getType())) {
|
||||
postApi.validPosts(options);
|
||||
postApi.validPostList(options);
|
||||
} else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER.getType())) {
|
||||
adminUserApi.validUsers(options);
|
||||
adminUserApi.validateUserList(options);
|
||||
} else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER_GROUP.getType())) {
|
||||
userGroupService.validUserGroups(options);
|
||||
} else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.SCRIPT.getType())) {
|
||||
dictDataApi.validDictDatas(DictTypeConstants.TASK_ASSIGN_SCRIPT,
|
||||
dictDataApi.validateDictDataList(DictTypeConstants.TASK_ASSIGN_SCRIPT,
|
||||
CollectionUtils.convertSet(options, String::valueOf));
|
||||
} else {
|
||||
throw new IllegalArgumentException(format("未知的规则类型({})", type));
|
||||
@ -288,12 +288,12 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
|
||||
}
|
||||
|
||||
private Set<Long> calculateTaskCandidateUsersByDeptMember(BpmTaskAssignRuleDO rule) {
|
||||
List<AdminUserRespDTO> users = adminUserApi.getUsersByDeptIds(rule.getOptions());
|
||||
List<AdminUserRespDTO> users = adminUserApi.getUserListByDeptIds(rule.getOptions());
|
||||
return convertSet(users, AdminUserRespDTO::getId);
|
||||
}
|
||||
|
||||
private Set<Long> calculateTaskCandidateUsersByDeptLeader(BpmTaskAssignRuleDO rule) {
|
||||
List<DeptRespDTO> depts = deptApi.getDepts(rule.getOptions());
|
||||
List<DeptRespDTO> depts = deptApi.getDeptList(rule.getOptions());
|
||||
return convertSet(depts, DeptRespDTO::getLeaderUserId);
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ public class BpmTaskAssignRuleServiceImplTest extends BaseDbUnitTest {
|
||||
// mock 方法
|
||||
List<AdminUserRespDTO> users = CollectionUtils.convertList(asSet(11L, 22L),
|
||||
id -> new AdminUserRespDTO().setId(id));
|
||||
when(adminUserApi.getUsersByDeptIds(eq(rule.getOptions()))).thenReturn(users);
|
||||
when(adminUserApi.getUserListByDeptIds(eq(rule.getOptions()))).thenReturn(users);
|
||||
mockGetUserMap(asSet(11L, 22L));
|
||||
|
||||
// 调用
|
||||
|
@ -29,7 +29,7 @@ public interface DeptApi {
|
||||
* @param ids 部门编号数组
|
||||
* @return 部门信息数组
|
||||
*/
|
||||
List<DeptRespDTO> getDepts(Collection<Long> ids);
|
||||
List<DeptRespDTO> getDeptList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 校验部门们是否有效。如下情况,视为无效:
|
||||
@ -38,7 +38,7 @@ public interface DeptApi {
|
||||
*
|
||||
* @param ids 角色编号数组
|
||||
*/
|
||||
void validDepts(Collection<Long> ids);
|
||||
void validateDeptList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得指定编号的部门 Map
|
||||
@ -47,7 +47,7 @@ public interface DeptApi {
|
||||
* @return 部门 Map
|
||||
*/
|
||||
default Map<Long, DeptRespDTO> getDeptMap(Set<Long> ids) {
|
||||
List<DeptRespDTO> list = getDepts(ids);
|
||||
List<DeptRespDTO> list = getDeptList(ids);
|
||||
return CollectionUtils.convertMap(list, DeptRespDTO::getId);
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,6 @@ public interface PostApi {
|
||||
*
|
||||
* @param ids 岗位编号数组
|
||||
*/
|
||||
void validPosts(Collection<Long> ids);
|
||||
void validPostList(Collection<Long> ids);
|
||||
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public interface DictDataApi {
|
||||
* @param dictType 字典类型
|
||||
* @param values 字典数据值的数组
|
||||
*/
|
||||
void validDictDatas(String dictType, Collection<String> values);
|
||||
void validateDictDataList(String dictType, Collection<String> values);
|
||||
|
||||
/**
|
||||
* 获得指定的字典数据,从缓存中
|
||||
|
@ -19,7 +19,7 @@ public interface ErrorCodeApi {
|
||||
*
|
||||
* @param autoGenerateDTOs 错误码信息
|
||||
*/
|
||||
void autoGenerateErrorCodes(@Valid List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs);
|
||||
void autoGenerateErrorCodeList(@Valid List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs);
|
||||
|
||||
/**
|
||||
* 增量获得错误码数组
|
||||
|
@ -16,6 +16,6 @@ public interface RoleApi {
|
||||
*
|
||||
* @param ids 角色编号数组
|
||||
*/
|
||||
void validRoles(Collection<Long> ids);
|
||||
void validRoleList(Collection<Long> ids);
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cn.iocoder.yudao.module.system.api.sms;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
||||
|
||||
@ -35,6 +35,6 @@ public interface SmsCodeApi {
|
||||
*
|
||||
* @param reqDTO 校验请求
|
||||
*/
|
||||
void checkSmsCode(@Valid SmsCodeCheckReqDTO reqDTO);
|
||||
void validateSmsCode(@Valid SmsCodeValidateReqDTO reqDTO);
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import javax.validation.constraints.NotNull;
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Data
|
||||
public class SmsCodeCheckReqDTO {
|
||||
public class SmsCodeValidateReqDTO {
|
||||
|
||||
/**
|
||||
* 手机号
|
@ -14,13 +14,13 @@ public interface TenantApi {
|
||||
*
|
||||
* @return 租户编号数组
|
||||
*/
|
||||
List<Long> getTenantIds();
|
||||
List<Long> getTenantIdList();
|
||||
|
||||
/**
|
||||
* 校验租户是否合法
|
||||
*
|
||||
* @param id 租户编号
|
||||
*/
|
||||
void validTenant(Long id);
|
||||
void validateTenant(Long id);
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Admin 用户 API 接口
|
||||
@ -29,7 +28,7 @@ public interface AdminUserApi {
|
||||
* @param ids 用户 ID 们
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
List<AdminUserRespDTO> getUsers(Collection<Long> ids);
|
||||
List<AdminUserRespDTO> getUserList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得指定部门的用户数组
|
||||
@ -37,7 +36,7 @@ public interface AdminUserApi {
|
||||
* @param deptIds 部门数组
|
||||
* @return 用户数组
|
||||
*/
|
||||
List<AdminUserRespDTO> getUsersByDeptIds(Collection<Long> deptIds);
|
||||
List<AdminUserRespDTO> getUserListByDeptIds(Collection<Long> deptIds);
|
||||
|
||||
/**
|
||||
* 获得指定岗位的用户数组
|
||||
@ -54,7 +53,7 @@ public interface AdminUserApi {
|
||||
* @return 用户 Map
|
||||
*/
|
||||
default Map<Long, AdminUserRespDTO> getUserMap(Collection<Long> ids) {
|
||||
List<AdminUserRespDTO> users = getUsers(ids);
|
||||
List<AdminUserRespDTO> users = getUserList(ids);
|
||||
return CollectionUtils.convertMap(users, AdminUserRespDTO::getId);
|
||||
}
|
||||
|
||||
@ -65,6 +64,6 @@ public interface AdminUserApi {
|
||||
*
|
||||
* @param ids 用户编号数组
|
||||
*/
|
||||
void validUsers(Set<Long> ids);
|
||||
void validateUserList(Collection<Long> ids);
|
||||
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ public interface ErrorCodeConstants {
|
||||
ErrorCode DEPT_EXITS_CHILDREN = new ErrorCode(1002004003, "存在子部门,无法删除");
|
||||
ErrorCode DEPT_PARENT_ERROR = new ErrorCode(1002004004, "不能设置自己为父部门");
|
||||
ErrorCode DEPT_EXISTS_USER = new ErrorCode(1002004005, "部门中存在员工,无法删除");
|
||||
ErrorCode DEPT_NOT_ENABLE = new ErrorCode(1002004006, "部门不处于开启状态,不允许选择");
|
||||
ErrorCode DEPT_NOT_ENABLE = new ErrorCode(1002004006, "部门({})不处于开启状态,不允许选择");
|
||||
ErrorCode DEPT_PARENT_IS_CHILD = new ErrorCode(1002004007, "不能设置自己的子部门为父部门");
|
||||
|
||||
// ========== 岗位模块 1002005000 ==========
|
||||
|
@ -1,20 +0,0 @@
|
||||
package cn.iocoder.yudao.module.system.enums.permission;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Menu 编号枚举
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum MenuIdEnum {
|
||||
|
||||
/**
|
||||
* 根节点
|
||||
*/
|
||||
ROOT(0L);
|
||||
|
||||
private final Long id;
|
||||
|
||||
}
|
@ -28,14 +28,14 @@ public class DeptApiImpl implements DeptApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptRespDTO> getDepts(Collection<Long> ids) {
|
||||
List<DeptDO> depts = deptService.getDepts(ids);
|
||||
public List<DeptRespDTO> getDeptList(Collection<Long> ids) {
|
||||
List<DeptDO> depts = deptService.getDeptList(ids);
|
||||
return DeptConvert.INSTANCE.convertList03(depts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validDepts(Collection<Long> ids) {
|
||||
deptService.validDepts(ids);
|
||||
public void validateDeptList(Collection<Long> ids) {
|
||||
deptService.validateDeptList(ids);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,7 +18,8 @@ public class PostApiImpl implements PostApi {
|
||||
private PostService postService;
|
||||
|
||||
@Override
|
||||
public void validPosts(Collection<Long> ids) {
|
||||
postService.validPosts(ids);
|
||||
public void validPostList(Collection<Long> ids) {
|
||||
postService.validatePostList(ids);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ public class DictDataApiImpl implements DictDataApi {
|
||||
private DictDataService dictDataService;
|
||||
|
||||
@Override
|
||||
public void validDictDatas(String dictType, Collection<String> values) {
|
||||
dictDataService.validDictDatas(dictType, values);
|
||||
public void validateDictDataList(String dictType, Collection<String> values) {
|
||||
dictDataService.validateDictDataList(dictType, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,7 +21,7 @@ public class ErrorCodeApiImpl implements ErrorCodeApi {
|
||||
private ErrorCodeService errorCodeService;
|
||||
|
||||
@Override
|
||||
public void autoGenerateErrorCodes(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) {
|
||||
public void autoGenerateErrorCodeList(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) {
|
||||
errorCodeService.autoGenerateErrorCodes(autoGenerateDTOs);
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ public class RoleApiImpl implements RoleApi {
|
||||
private RoleService roleService;
|
||||
|
||||
@Override
|
||||
public void validRoles(Collection<Long> ids) {
|
||||
roleService.validRoles(ids);
|
||||
public void validRoleList(Collection<Long> ids) {
|
||||
roleService.validateRoleList(ids);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.iocoder.yudao.module.system.api.sms;
|
||||
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
||||
import cn.iocoder.yudao.module.system.service.sms.SmsCodeService;
|
||||
@ -32,8 +32,8 @@ public class SmsCodeApiImpl implements SmsCodeApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkSmsCode(SmsCodeCheckReqDTO reqDTO) {
|
||||
smsCodeService.checkSmsCode(reqDTO);
|
||||
public void validateSmsCode(SmsCodeValidateReqDTO reqDTO) {
|
||||
smsCodeService.validateSmsCode(reqDTO);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,12 +18,12 @@ public class TenantApiImpl implements TenantApi {
|
||||
private TenantService tenantService;
|
||||
|
||||
@Override
|
||||
public List<Long> getTenantIds() {
|
||||
return tenantService.getTenantIds();
|
||||
public List<Long> getTenantIdList() {
|
||||
return tenantService.getTenantIdList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validTenant(Long id) {
|
||||
public void validateTenant(Long id) {
|
||||
tenantService.validTenant(id);
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,6 @@ import org.springframework.stereotype.Service;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Admin 用户 API 实现类
|
||||
@ -29,26 +28,26 @@ public class AdminUserApiImpl implements AdminUserApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserRespDTO> getUsers(Collection<Long> ids) {
|
||||
List<AdminUserDO> users = userService.getUsers(ids);
|
||||
public List<AdminUserRespDTO> getUserList(Collection<Long> ids) {
|
||||
List<AdminUserDO> users = userService.getUserList(ids);
|
||||
return UserConvert.INSTANCE.convertList4(users);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserRespDTO> getUsersByDeptIds(Collection<Long> deptIds) {
|
||||
List<AdminUserDO> users = userService.getUsersByDeptIds(deptIds);
|
||||
public List<AdminUserRespDTO> getUserListByDeptIds(Collection<Long> deptIds) {
|
||||
List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds);
|
||||
return UserConvert.INSTANCE.convertList4(users);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserRespDTO> getUsersByPostIds(Collection<Long> postIds) {
|
||||
List<AdminUserDO> users = userService.getUsersByPostIds(postIds);
|
||||
List<AdminUserDO> users = userService.getUserListByPostIds(postIds);
|
||||
return UserConvert.INSTANCE.convertList4(users);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validUsers(Set<Long> ids) {
|
||||
userService.validUsers(ids);
|
||||
public void validateUserList(Collection<Long> ids) {
|
||||
userService.validateUserList(ids);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ public class AuthController {
|
||||
}
|
||||
// 获得角色列表
|
||||
Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(getLoginUserId(), singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||
List<RoleDO> roleList = roleService.getRolesFromCache(roleIds);
|
||||
List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds);
|
||||
// 获得菜单列表
|
||||
List<MenuDO> menuList = permissionService.getRoleMenuListFromCache(roleIds,
|
||||
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()),
|
||||
@ -108,7 +108,7 @@ public class AuthController {
|
||||
|
||||
@GetMapping("/list-menus")
|
||||
@ApiOperation("获得登录用户的菜单列表")
|
||||
public CommonResult<List<AuthMenuRespVO>> getMenus() {
|
||||
public CommonResult<List<AuthMenuRespVO>> getMenuList() {
|
||||
// 获得角色列表
|
||||
Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(getLoginUserId(), singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||
// 获得用户拥有的菜单列表
|
||||
|
@ -57,19 +57,19 @@ public class DeptController {
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("获取部门列表")
|
||||
@PreAuthorize("@ss.hasPermission('system:dept:query')")
|
||||
public CommonResult<List<DeptRespVO>> listDepts(DeptListReqVO reqVO) {
|
||||
List<DeptDO> list = deptService.getSimpleDepts(reqVO);
|
||||
public CommonResult<List<DeptRespVO>> getDeptList(DeptListReqVO reqVO) {
|
||||
List<DeptDO> list = deptService.getDeptList(reqVO);
|
||||
list.sort(Comparator.comparing(DeptDO::getSort));
|
||||
return success(DeptConvert.INSTANCE.convertList(list));
|
||||
}
|
||||
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获取部门精简信息列表", notes = "只包含被开启的部门,主要用于前端的下拉选项")
|
||||
public CommonResult<List<DeptSimpleRespVO>> getSimpleDepts() {
|
||||
public CommonResult<List<DeptSimpleRespVO>> getSimpleDeptList() {
|
||||
// 获得部门列表,只要开启状态的
|
||||
DeptListReqVO reqVO = new DeptListReqVO();
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
List<DeptDO> list = deptService.getSimpleDepts(reqVO);
|
||||
List<DeptDO> list = deptService.getDeptList(reqVO);
|
||||
// 排序后,返回给前端
|
||||
list.sort(Comparator.comparing(DeptDO::getSort));
|
||||
return success(DeptConvert.INSTANCE.convertList02(list));
|
||||
|
@ -70,9 +70,9 @@ public class PostController {
|
||||
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获取岗位精简信息列表", notes = "只包含被开启的岗位,主要用于前端的下拉选项")
|
||||
public CommonResult<List<PostSimpleRespVO>> getSimplePosts() {
|
||||
public CommonResult<List<PostSimpleRespVO>> getSimplePostList() {
|
||||
// 获得岗位列表,只要开启状态的
|
||||
List<PostDO> list = postService.getPosts(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||
List<PostDO> list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||
// 排序后,返回给前端
|
||||
list.sort(Comparator.comparing(PostDO::getSort));
|
||||
return success(PostConvert.INSTANCE.convertList02(list));
|
||||
@ -90,7 +90,7 @@ public class PostController {
|
||||
@PreAuthorize("@ss.hasPermission('system:post:export')")
|
||||
@OperateLog(type = EXPORT)
|
||||
public void export(HttpServletResponse response, @Validated PostExportReqVO reqVO) throws IOException {
|
||||
List<PostDO> posts = postService.getPosts(reqVO);
|
||||
List<PostDO> posts = postService.getPostList(reqVO);
|
||||
List<PostExcelVO> data = PostConvert.INSTANCE.convertList03(posts);
|
||||
// 输出
|
||||
ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostExcelVO.class, data);
|
||||
|
@ -61,8 +61,8 @@ public class DictDataController {
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获得全部字典数据列表", notes = "一般用于管理后台缓存字典数据在本地")
|
||||
// 无需添加权限认证,因为前端全局都需要
|
||||
public CommonResult<List<DictDataSimpleRespVO>> getSimpleDictDatas() {
|
||||
List<DictDataDO> list = dictDataService.getDictDatas();
|
||||
public CommonResult<List<DictDataSimpleRespVO>> getSimpleDictDataList() {
|
||||
List<DictDataDO> list = dictDataService.getDictDataList();
|
||||
return success(DictDataConvert.INSTANCE.convertList(list));
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ public class DictDataController {
|
||||
@PreAuthorize("@ss.hasPermission('system:dict:export')")
|
||||
@OperateLog(type = EXPORT)
|
||||
public void export(HttpServletResponse response, @Valid DictDataExportReqVO reqVO) throws IOException {
|
||||
List<DictDataDO> list = dictDataService.getDictDatas(reqVO);
|
||||
List<DictDataDO> list = dictDataService.getDictDataList(reqVO);
|
||||
List<DictDataExcelVO> data = DictDataConvert.INSTANCE.convertList02(list);
|
||||
// 输出
|
||||
ExcelUtils.write(response, "字典数据.xls", "数据列表", DictDataExcelVO.class, data);
|
||||
|
@ -76,7 +76,7 @@ public class DictTypeController {
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获得全部字典类型列表", notes = "包括开启 + 禁用的字典类型,主要用于前端的下拉选项")
|
||||
// 无需添加权限认证,因为前端全局都需要
|
||||
public CommonResult<List<DictTypeSimpleRespVO>> listSimpleDictTypes() {
|
||||
public CommonResult<List<DictTypeSimpleRespVO>> getSimpleDictTypeList() {
|
||||
List<DictTypeDO> list = dictTypeService.getDictTypeList();
|
||||
return success(DictTypeConvert.INSTANCE.convertList(list));
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ public class OperateLogController {
|
||||
@PreAuthorize("@ss.hasPermission('system:operate-log:export')")
|
||||
@OperateLog(type = EXPORT)
|
||||
public void exportOperateLog(HttpServletResponse response, @Valid OperateLogExportReqVO reqVO) throws IOException {
|
||||
List<OperateLogDO> list = operateLogService.getOperateLogs(reqVO);
|
||||
List<OperateLogDO> list = operateLogService.getOperateLogList(reqVO);
|
||||
|
||||
// 获得拼接需要的数据
|
||||
Collection<Long> userIds = CollectionUtils.convertList(list, OperateLogDO::getUserId);
|
||||
|
@ -57,8 +57,8 @@ public class NoticeController {
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("获取通知公告列表")
|
||||
@PreAuthorize("@ss.hasPermission('system:notice:query')")
|
||||
public CommonResult<PageResult<NoticeRespVO>> pageNotices(@Validated NoticePageReqVO reqVO) {
|
||||
return success(NoticeConvert.INSTANCE.convertPage(noticeService.pageNotices(reqVO)));
|
||||
public CommonResult<PageResult<NoticeRespVO>> getNoticePage(@Validated NoticePageReqVO reqVO) {
|
||||
return success(NoticeConvert.INSTANCE.convertPage(noticeService.getNoticePage(reqVO)));
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
|
@ -61,7 +61,7 @@ public class OAuth2UserController {
|
||||
}
|
||||
// 获得岗位信息
|
||||
if (CollUtil.isNotEmpty(user.getPostIds())) {
|
||||
List<PostDO> posts = postService.getPosts(user.getPostIds());
|
||||
List<PostDO> posts = postService.getPostList(user.getPostIds());
|
||||
resp.setPosts(OAuth2UserConvert.INSTANCE.convertList(posts));
|
||||
}
|
||||
return success(resp);
|
||||
|
@ -57,8 +57,8 @@ public class MenuController {
|
||||
@GetMapping("/list")
|
||||
@ApiOperation(value = "获取菜单列表", notes = "用于【菜单管理】界面")
|
||||
@PreAuthorize("@ss.hasPermission('system:menu:query')")
|
||||
public CommonResult<List<MenuRespVO>> getMenus(MenuListReqVO reqVO) {
|
||||
List<MenuDO> list = menuService.getMenus(reqVO);
|
||||
public CommonResult<List<MenuRespVO>> getMenuList(MenuListReqVO reqVO) {
|
||||
List<MenuDO> list = menuService.getMenuList(reqVO);
|
||||
list.sort(Comparator.comparing(MenuDO::getSort));
|
||||
return success(MenuConvert.INSTANCE.convertList(list));
|
||||
}
|
||||
@ -66,11 +66,11 @@ public class MenuController {
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获取菜单精简信息列表", notes = "只包含被开启的菜单,用于【角色分配菜单】功能的选项。" +
|
||||
"在多租户的场景下,会只返回租户所在套餐有的菜单")
|
||||
public CommonResult<List<MenuSimpleRespVO>> getSimpleMenus() {
|
||||
public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
|
||||
// 获得菜单列表,只要开启状态的
|
||||
MenuListReqVO reqVO = new MenuListReqVO();
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
List<MenuDO> list = menuService.getTenantMenus(reqVO);
|
||||
List<MenuDO> list = menuService.getMenuListByTenant(reqVO);
|
||||
// 排序后,返回给前端
|
||||
list.sort(Comparator.comparing(MenuDO::getSort));
|
||||
return success(MenuConvert.INSTANCE.convertList02(list));
|
||||
|
@ -20,12 +20,12 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
|
||||
import static java.util.Collections.singleton;
|
||||
|
||||
@Api(tags = "管理后台 - 角色")
|
||||
@RestController
|
||||
@ -85,9 +85,9 @@ public class RoleController {
|
||||
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色,主要用于前端的下拉选项")
|
||||
public CommonResult<List<RoleSimpleRespVO>> getSimpleRoles() {
|
||||
public CommonResult<List<RoleSimpleRespVO>> getSimpleRoleList() {
|
||||
// 获得角色列表,只要开启状态的
|
||||
List<RoleDO> list = roleService.getRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||
List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||
// 排序后,返回给前端
|
||||
list.sort(Comparator.comparing(RoleDO::getSort));
|
||||
return success(RoleConvert.INSTANCE.convertList02(list));
|
||||
|
@ -90,8 +90,8 @@ public class SensitiveWordController {
|
||||
@GetMapping("/get-tags")
|
||||
@ApiOperation("获取所有敏感词的标签数组")
|
||||
@PreAuthorize("@ss.hasPermission('system:sensitive-word:query')")
|
||||
public CommonResult<Set<String>> getSensitiveWordTags() throws IOException {
|
||||
return success(sensitiveWordService.getSensitiveWordTags());
|
||||
public CommonResult<Set<String>> getSensitiveWordTagSet() {
|
||||
return success(sensitiveWordService.getSensitiveWordTagSet());
|
||||
}
|
||||
|
||||
@GetMapping("/validate-text")
|
||||
|
@ -70,7 +70,7 @@ public class SmsChannelController {
|
||||
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获得短信渠道精简列表", notes = "包含被禁用的短信渠道")
|
||||
public CommonResult<List<SmsChannelSimpleRespVO>> getSimpleSmsChannels() {
|
||||
public CommonResult<List<SmsChannelSimpleRespVO>> getSimpleSmsChannelList() {
|
||||
List<SmsChannelDO> list = smsChannelService.getSmsChannelList();
|
||||
// 排序后,返回给前端
|
||||
list.sort(Comparator.comparing(SmsChannelDO::getId));
|
||||
|
@ -111,9 +111,9 @@ public class UserController {
|
||||
|
||||
@GetMapping("/list-all-simple")
|
||||
@ApiOperation(value = "获取用户精简信息列表", notes = "只包含被开启的用户,主要用于前端的下拉选项")
|
||||
public CommonResult<List<UserSimpleRespVO>> getSimpleUsers() {
|
||||
public CommonResult<List<UserSimpleRespVO>> getSimpleUserList() {
|
||||
// 获用户列表,只要开启状态的
|
||||
List<AdminUserDO> list = userService.getUsersByStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
List<AdminUserDO> list = userService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
// 排序后,返回给前端
|
||||
return success(UserConvert.INSTANCE.convertList04(list));
|
||||
}
|
||||
@ -122,7 +122,7 @@ public class UserController {
|
||||
@ApiOperation("获得用户详情")
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
|
||||
@PreAuthorize("@ss.hasPermission('system:user:query')")
|
||||
public CommonResult<UserRespVO> getInfo(@RequestParam("id") Long id) {
|
||||
public CommonResult<UserRespVO> getUser(@RequestParam("id") Long id) {
|
||||
return success(UserConvert.INSTANCE.convert(userService.getUser(id)));
|
||||
}
|
||||
|
||||
@ -130,10 +130,10 @@ public class UserController {
|
||||
@ApiOperation("导出用户")
|
||||
@PreAuthorize("@ss.hasPermission('system:user:export')")
|
||||
@OperateLog(type = EXPORT)
|
||||
public void exportUsers(@Validated UserExportReqVO reqVO,
|
||||
HttpServletResponse response) throws IOException {
|
||||
public void exportUserList(@Validated UserExportReqVO reqVO,
|
||||
HttpServletResponse response) throws IOException {
|
||||
// 获得用户列表
|
||||
List<AdminUserDO> users = userService.getUsers(reqVO);
|
||||
List<AdminUserDO> users = userService.getUserList(reqVO);
|
||||
|
||||
// 获得拼接需要的数据
|
||||
Collection<Long> deptIds = convertList(users, AdminUserDO::getDeptId);
|
||||
@ -183,7 +183,7 @@ public class UserController {
|
||||
public CommonResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
|
||||
List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class);
|
||||
return success(userService.importUsers(list, updateSupport));
|
||||
return success(userService.importUserList(list, updateSupport));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class UserProfileController {
|
||||
AdminUserDO user = userService.getUser(getLoginUserId());
|
||||
UserProfileRespVO resp = UserConvert.INSTANCE.convert03(user);
|
||||
// 获得用户角色
|
||||
List<RoleDO> userRoles = roleService.getRolesFromCache(permissionService.getUserRoleIdListByUserId(user.getId()));
|
||||
List<RoleDO> userRoles = roleService.getRoleListFromCache(permissionService.getUserRoleIdListByUserId(user.getId()));
|
||||
resp.setRoles(UserConvert.INSTANCE.convertList(userRoles));
|
||||
// 获得部门信息
|
||||
if (user.getDeptId() != null) {
|
||||
@ -72,7 +72,7 @@ public class UserProfileController {
|
||||
}
|
||||
// 获得岗位信息
|
||||
if (CollUtil.isNotEmpty(user.getPostIds())) {
|
||||
List<PostDO> posts = postService.getPosts(user.getPostIds());
|
||||
List<PostDO> posts = postService.getPostList(user.getPostIds());
|
||||
resp.setPosts(UserConvert.INSTANCE.convertList02(posts));
|
||||
}
|
||||
// 获得社交用户信息
|
||||
|
@ -9,13 +9,15 @@ import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
|
||||
import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT;
|
||||
|
||||
@Mapper
|
||||
public interface AuthConvert {
|
||||
|
||||
@ -47,7 +49,7 @@ public interface AuthConvert {
|
||||
Map<Long, AuthMenuRespVO> treeNodeMap = new LinkedHashMap<>();
|
||||
menuList.forEach(menu -> treeNodeMap.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu)));
|
||||
// 处理父子关系
|
||||
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(MenuIdEnum.ROOT.getId())).forEach(childNode -> {
|
||||
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {
|
||||
// 获得父节点
|
||||
AuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId());
|
||||
if (parentNode == null) {
|
||||
@ -62,7 +64,7 @@ public interface AuthConvert {
|
||||
parentNode.getChildren().add(childNode);
|
||||
});
|
||||
// 获得到所有的根节点
|
||||
return CollectionUtils.filterList(treeNodeMap.values(), node -> MenuIdEnum.ROOT.getId().equals(node.getParentId()));
|
||||
return filterList(treeNodeMap.values(), node -> ID_ROOT.equals(node.getParentId()));
|
||||
}
|
||||
|
||||
SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialLoginReqVO reqVO);
|
||||
|
@ -21,7 +21,12 @@ import lombok.EqualsAndHashCode;
|
||||
public class MenuDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 菜单ID
|
||||
* 菜单编号 - 根节点
|
||||
*/
|
||||
public static final Long ID_ROOT = 0L;
|
||||
|
||||
/**
|
||||
* 菜单编号
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
|
@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
@ -19,9 +18,7 @@ public interface DeptMapper extends BaseMapperX<DeptDO> {
|
||||
}
|
||||
|
||||
default DeptDO selectByParentIdAndName(Long parentId, String name) {
|
||||
return selectOne(new LambdaQueryWrapper<DeptDO>()
|
||||
.eq(DeptDO::getParentId, parentId)
|
||||
.eq(DeptDO::getName, name));
|
||||
return selectOne(DeptDO::getParentId, parentId, DeptDO::getName, name);
|
||||
}
|
||||
|
||||
default Long selectCountByParentId(Long parentId) {
|
||||
|
@ -2,11 +2,10 @@ package cn.iocoder.yudao.module.system.dal.mysql.dept;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -16,31 +15,32 @@ import java.util.List;
|
||||
public interface PostMapper extends BaseMapperX<PostDO> {
|
||||
|
||||
default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses) {
|
||||
return selectList(new QueryWrapperX<PostDO>().inIfPresent("id", ids)
|
||||
.inIfPresent("status", statuses));
|
||||
return selectList(new LambdaQueryWrapperX<PostDO>()
|
||||
.inIfPresent(PostDO::getId, ids)
|
||||
.inIfPresent(PostDO::getStatus, statuses));
|
||||
}
|
||||
|
||||
default PageResult<PostDO> selectPage(PostPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new QueryWrapperX<PostDO>()
|
||||
.likeIfPresent("code", reqVO.getCode())
|
||||
.likeIfPresent("name", reqVO.getName())
|
||||
.eqIfPresent("status", reqVO.getStatus())
|
||||
.orderByDesc("id"));
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<PostDO>()
|
||||
.likeIfPresent(PostDO::getCode, reqVO.getCode())
|
||||
.likeIfPresent(PostDO::getName, reqVO.getName())
|
||||
.eqIfPresent(PostDO::getStatus, reqVO.getStatus())
|
||||
.orderByDesc(PostDO::getId));
|
||||
}
|
||||
|
||||
default List<PostDO> selectList(PostExportReqVO reqVO) {
|
||||
return selectList(new QueryWrapperX<PostDO>()
|
||||
.likeIfPresent("code", reqVO.getCode())
|
||||
.likeIfPresent("name", reqVO.getName())
|
||||
.eqIfPresent("status", reqVO.getStatus()));
|
||||
return selectList(new LambdaQueryWrapperX<PostDO>()
|
||||
.likeIfPresent(PostDO::getCode, reqVO.getCode())
|
||||
.likeIfPresent(PostDO::getName, reqVO.getName())
|
||||
.eqIfPresent(PostDO::getStatus, reqVO.getStatus()));
|
||||
}
|
||||
|
||||
default PostDO selectByName(String name) {
|
||||
return selectOne(new QueryWrapper<PostDO>().eq("name", name));
|
||||
return selectOne(PostDO::getName, name);
|
||||
}
|
||||
|
||||
default PostDO selectByCode(String code) {
|
||||
return selectOne(new QueryWrapper<PostDO>().eq("code", code));
|
||||
return selectOne(PostDO::getCode, code);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,8 +13,7 @@ import java.util.List;
|
||||
public interface UserPostMapper extends BaseMapperX<UserPostDO> {
|
||||
|
||||
default List<UserPostDO> selectListByUserId(Long userId) {
|
||||
return selectList(new LambdaQueryWrapperX<UserPostDO>()
|
||||
.eq(UserPostDO::getUserId, userId));
|
||||
return selectList(UserPostDO::getUserId, userId);
|
||||
}
|
||||
|
||||
default void deleteByUserIdAndPostId(Long userId, Collection<Long> postIds) {
|
||||
@ -24,11 +23,10 @@ public interface UserPostMapper extends BaseMapperX<UserPostDO> {
|
||||
}
|
||||
|
||||
default List<UserPostDO> selectListByPostIds(Collection<Long> postIds) {
|
||||
return selectList(new LambdaQueryWrapperX<UserPostDO>()
|
||||
.in(UserPostDO::getPostId, postIds));
|
||||
return selectList(UserPostDO::getPostId, postIds);
|
||||
}
|
||||
|
||||
default void deleteByUserId(Long userId){
|
||||
default void deleteByUserId(Long userId) {
|
||||
delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId));
|
||||
}
|
||||
}
|
||||
|
@ -17,13 +17,11 @@ import java.util.List;
|
||||
public interface DictDataMapper extends BaseMapperX<DictDataDO> {
|
||||
|
||||
default DictDataDO selectByDictTypeAndValue(String dictType, String value) {
|
||||
return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType)
|
||||
.eq(DictDataDO::getValue, value));
|
||||
return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getValue, value);
|
||||
}
|
||||
|
||||
default DictDataDO selectByDictTypeAndLabel(String dictType, String label) {
|
||||
return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType)
|
||||
.eq(DictDataDO::getLabel, label));
|
||||
return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getLabel, label);
|
||||
}
|
||||
|
||||
default List<DictDataDO> selectByDictTypeAndValues(String dictType, Collection<String> values) {
|
||||
|
@ -36,11 +36,11 @@ public interface ErrorCodeMapper extends BaseMapperX<ErrorCodeDO> {
|
||||
}
|
||||
|
||||
default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) {
|
||||
return selectList(new LambdaQueryWrapperX<ErrorCodeDO>().in(ErrorCodeDO::getCode, codes));
|
||||
return selectList(ErrorCodeDO::getCode, codes);
|
||||
}
|
||||
|
||||
default ErrorCodeDO selectByCode(Integer code) {
|
||||
return selectOne(new LambdaQueryWrapperX<ErrorCodeDO>().eq(ErrorCodeDO::getCode, code));
|
||||
return selectOne(ErrorCodeDO::getCode, code);
|
||||
}
|
||||
|
||||
default List<ErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) {
|
||||
|
@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
@ -13,8 +12,7 @@ import java.util.List;
|
||||
public interface MenuMapper extends BaseMapperX<MenuDO> {
|
||||
|
||||
default MenuDO selectByParentIdAndName(Long parentId, String name) {
|
||||
return selectOne(new LambdaQueryWrapper<MenuDO>().eq(MenuDO::getParentId, parentId)
|
||||
.eq(MenuDO::getName, name));
|
||||
return selectOne(MenuDO::getParentId, parentId, MenuDO::getName, name);
|
||||
}
|
||||
|
||||
default Long selectCountByParentId(Long parentId) {
|
||||
@ -22,7 +20,8 @@ public interface MenuMapper extends BaseMapperX<MenuDO> {
|
||||
}
|
||||
|
||||
default List<MenuDO> selectList(MenuListReqVO reqVO) {
|
||||
return selectList(new LambdaQueryWrapperX<MenuDO>().likeIfPresent(MenuDO::getName, reqVO.getName())
|
||||
return selectList(new LambdaQueryWrapperX<MenuDO>()
|
||||
.likeIfPresent(MenuDO::getName, reqVO.getName())
|
||||
.eqIfPresent(MenuDO::getStatus, reqVO.getStatus()));
|
||||
}
|
||||
|
||||
|
@ -24,9 +24,8 @@ public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> {
|
||||
}
|
||||
|
||||
default SocialUserBindDO selectByUserTypeAndSocialUserId(Integer userType, Long socialUserId) {
|
||||
return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>()
|
||||
.eq(SocialUserBindDO::getUserType, userType)
|
||||
.eq(SocialUserBindDO::getSocialUserId, socialUserId));
|
||||
return selectOne(SocialUserBindDO::getUserType, userType,
|
||||
SocialUserBindDO::getSocialUserId, socialUserId);
|
||||
}
|
||||
|
||||
default List<SocialUserBindDO> selectListByUserIdAndUserType(Long userId, Integer userType) {
|
||||
|
@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserExportReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -16,15 +15,15 @@ import java.util.List;
|
||||
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {
|
||||
|
||||
default AdminUserDO selectByUsername(String username) {
|
||||
return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getUsername, username));
|
||||
return selectOne(AdminUserDO::getUsername, username);
|
||||
}
|
||||
|
||||
default AdminUserDO selectByEmail(String email) {
|
||||
return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getEmail, email));
|
||||
return selectOne(AdminUserDO::getEmail, email);
|
||||
}
|
||||
|
||||
default AdminUserDO selectByMobile(String mobile) {
|
||||
return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getMobile, mobile));
|
||||
return selectOne(AdminUserDO::getMobile, mobile);
|
||||
}
|
||||
|
||||
default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds) {
|
||||
@ -50,10 +49,6 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {
|
||||
return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getNickname, nickname));
|
||||
}
|
||||
|
||||
default List<AdminUserDO> selectListByUsername(String username) {
|
||||
return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getUsername, username));
|
||||
}
|
||||
|
||||
default List<AdminUserDO> selectListByStatus(Integer status) {
|
||||
return selectList(AdminUserDO::getStatus, status);
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Override
|
||||
public AuthLoginRespVO login(AuthLoginReqVO reqVO) {
|
||||
// 校验验证码
|
||||
verifyCaptcha(reqVO);
|
||||
validateCaptcha(reqVO);
|
||||
|
||||
// 使用账号密码,进行登录
|
||||
AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword());
|
||||
@ -171,14 +171,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO refreshToken(String refreshToken) {
|
||||
OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, OAuth2ClientConstants.CLIENT_ID_DEFAULT);
|
||||
return AuthConvert.INSTANCE.convert(accessTokenDO);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void verifyCaptcha(AuthLoginReqVO reqVO) {
|
||||
void validateCaptcha(AuthLoginReqVO reqVO) {
|
||||
// 如果验证码关闭,则不进行校验
|
||||
if (!captchaEnable) {
|
||||
return;
|
||||
@ -206,6 +200,12 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return AuthConvert.INSTANCE.convert(accessTokenDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO refreshToken(String refreshToken) {
|
||||
OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, OAuth2ClientConstants.CLIENT_ID_DEFAULT);
|
||||
return AuthConvert.INSTANCE.convert(accessTokenDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout(String token, Integer logType) {
|
||||
// 删除访问令牌
|
||||
|
@ -52,7 +52,7 @@ public interface DeptService {
|
||||
* @param reqVO 筛选条件请求 VO
|
||||
* @return 部门列表
|
||||
*/
|
||||
List<DeptDO> getSimpleDepts(DeptListReqVO reqVO);
|
||||
List<DeptDO> getDeptList(DeptListReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得所有子部门,从缓存中
|
||||
@ -61,7 +61,7 @@ public interface DeptService {
|
||||
* @param recursive 是否递归获取所有
|
||||
* @return 子部门列表
|
||||
*/
|
||||
List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive);
|
||||
List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive);
|
||||
|
||||
/**
|
||||
* 获得部门信息数组
|
||||
@ -69,7 +69,21 @@ public interface DeptService {
|
||||
* @param ids 部门编号数组
|
||||
* @return 部门信息数组
|
||||
*/
|
||||
List<DeptDO> getDepts(Collection<Long> ids);
|
||||
List<DeptDO> getDeptList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得指定编号的部门 Map
|
||||
*
|
||||
* @param ids 部门编号数组
|
||||
* @return 部门 Map
|
||||
*/
|
||||
default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
List<DeptDO> list = getDeptList(ids);
|
||||
return CollectionUtils.convertMap(list, DeptDO::getId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得部门信息
|
||||
@ -86,27 +100,6 @@ public interface DeptService {
|
||||
*
|
||||
* @param ids 角色编号数组
|
||||
*/
|
||||
void validDepts(Collection<Long> ids);
|
||||
void validateDeptList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得指定编号的部门列表
|
||||
*
|
||||
* @param ids 部门编号数组
|
||||
* @return 部门列表
|
||||
*/
|
||||
List<DeptDO> getSimpleDepts(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得指定编号的部门 Map
|
||||
*
|
||||
* @param ids 部门编号数组
|
||||
* @return 部门 Map
|
||||
*/
|
||||
default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
List<DeptDO> list = getSimpleDepts(ids);
|
||||
return CollectionUtils.convertMap(list, DeptDO::getId);
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.system.service.dept;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptCreateReqVO;
|
||||
@ -19,13 +17,11 @@ import com.google.common.collect.ImmutableMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
@ -95,7 +91,7 @@ public class DeptServiceImpl implements DeptService {
|
||||
if (reqVO.getParentId() == null) {
|
||||
reqVO.setParentId(DeptIdEnum.ROOT.getId());
|
||||
}
|
||||
checkCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName());
|
||||
validateForCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName());
|
||||
// 插入部门
|
||||
DeptDO dept = DeptConvert.INSTANCE.convert(reqVO);
|
||||
deptMapper.insert(dept);
|
||||
@ -110,7 +106,7 @@ public class DeptServiceImpl implements DeptService {
|
||||
if (reqVO.getParentId() == null) {
|
||||
reqVO.setParentId(DeptIdEnum.ROOT.getId());
|
||||
}
|
||||
checkCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
|
||||
validateForCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
|
||||
// 更新部门
|
||||
DeptDO updateObj = DeptConvert.INSTANCE.convert(reqVO);
|
||||
deptMapper.updateById(updateObj);
|
||||
@ -121,10 +117,10 @@ public class DeptServiceImpl implements DeptService {
|
||||
@Override
|
||||
public void deleteDept(Long id) {
|
||||
// 校验是否存在
|
||||
checkDeptExists(id);
|
||||
validateDeptExists(id);
|
||||
// 校验是否有子部门
|
||||
if (deptMapper.selectCountByParentId(id) > 0) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_EXITS_CHILDREN);
|
||||
throw exception(DEPT_EXITS_CHILDREN);
|
||||
}
|
||||
// 删除部门
|
||||
deptMapper.deleteById(id);
|
||||
@ -133,20 +129,20 @@ public class DeptServiceImpl implements DeptService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptDO> getSimpleDepts(DeptListReqVO reqVO) {
|
||||
public List<DeptDO> getDeptList(DeptListReqVO reqVO) {
|
||||
return deptMapper.selectList(reqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive) {
|
||||
public List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive) {
|
||||
if (parentId == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<DeptDO> result = new ArrayList<>(); // TODO 芋艿:待优化,新增缓存,避免每次遍历的计算
|
||||
List<DeptDO> result = new ArrayList<>();
|
||||
// 递归,简单粗暴
|
||||
this.getDeptsByParentIdFromCache(result, parentId,
|
||||
recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次
|
||||
parentDeptCache);
|
||||
getDeptsByParentIdFromCache(result, parentId,
|
||||
recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次
|
||||
parentDeptCache);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -182,65 +178,65 @@ public class DeptServiceImpl implements DeptService {
|
||||
recursiveCount - 1, parentDeptMap));
|
||||
}
|
||||
|
||||
private void checkCreateOrUpdate(Long id, Long parentId, String name) {
|
||||
private void validateForCreateOrUpdate(Long id, Long parentId, String name) {
|
||||
// 校验自己存在
|
||||
checkDeptExists(id);
|
||||
validateDeptExists(id);
|
||||
// 校验父部门的有效性
|
||||
checkParentDeptEnable(id, parentId);
|
||||
validateParentDeptEnable(id, parentId);
|
||||
// 校验部门名的唯一性
|
||||
checkDeptNameUnique(id, parentId, name);
|
||||
validateDeptNameUnique(id, parentId, name);
|
||||
}
|
||||
|
||||
private void checkParentDeptEnable(Long id, Long parentId) {
|
||||
private void validateParentDeptEnable(Long id, Long parentId) {
|
||||
if (parentId == null || DeptIdEnum.ROOT.getId().equals(parentId)) {
|
||||
return;
|
||||
}
|
||||
// 不能设置自己为父部门
|
||||
if (parentId.equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_PARENT_ERROR);
|
||||
throw exception(DEPT_PARENT_ERROR);
|
||||
}
|
||||
// 父岗位不存在
|
||||
DeptDO dept = deptMapper.selectById(parentId);
|
||||
if (dept == null) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_PARENT_NOT_EXITS);
|
||||
throw exception(DEPT_PARENT_NOT_EXITS);
|
||||
}
|
||||
// 父部门被禁用
|
||||
if (!CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_NOT_ENABLE);
|
||||
throw exception(DEPT_NOT_ENABLE);
|
||||
}
|
||||
// 父部门不能是原来的子部门
|
||||
List<DeptDO> children = this.getDeptsByParentIdFromCache(id, true);
|
||||
List<DeptDO> children = getDeptListByParentIdFromCache(id, true);
|
||||
if (children.stream().anyMatch(dept1 -> dept1.getId().equals(parentId))) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_PARENT_IS_CHILD);
|
||||
throw exception(DEPT_PARENT_IS_CHILD);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDeptExists(Long id) {
|
||||
private void validateDeptExists(Long id) {
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
DeptDO dept = deptMapper.selectById(id);
|
||||
if (dept == null) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_NOT_FOUND);
|
||||
throw exception(DEPT_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDeptNameUnique(Long id, Long parentId, String name) {
|
||||
private void validateDeptNameUnique(Long id, Long parentId, String name) {
|
||||
DeptDO menu = deptMapper.selectByParentIdAndName(parentId, name);
|
||||
if (menu == null) {
|
||||
return;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
|
||||
if (id == null) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE);
|
||||
throw exception(DEPT_NAME_DUPLICATE);
|
||||
}
|
||||
if (!menu.getId().equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE);
|
||||
throw exception(DEPT_NAME_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptDO> getDepts(Collection<Long> ids) {
|
||||
public List<DeptDO> getDeptList(Collection<Long> ids) {
|
||||
return deptMapper.selectBatchIds(ids);
|
||||
}
|
||||
|
||||
@ -250,13 +246,12 @@ public class DeptServiceImpl implements DeptService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validDepts(Collection<Long> ids) {
|
||||
public void validateDeptList(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return;
|
||||
}
|
||||
// 获得科室信息
|
||||
List<DeptDO> depts = deptMapper.selectBatchIds(ids);
|
||||
Map<Long, DeptDO> deptMap = CollectionUtils.convertMap(depts, DeptDO::getId);
|
||||
Map<Long, DeptDO> deptMap = getDeptMap(ids);
|
||||
// 校验
|
||||
ids.forEach(id -> {
|
||||
DeptDO dept = deptMap.get(id);
|
||||
@ -269,9 +264,4 @@ public class DeptServiceImpl implements DeptService {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptDO> getSimpleDepts(Collection<Long> ids) {
|
||||
return deptMapper.selectBatchIds(ids);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -49,8 +49,8 @@ public interface PostService {
|
||||
* @param ids 岗位编号数组。如果为空,不进行筛选
|
||||
* @return 部门列表
|
||||
*/
|
||||
default List<PostDO> getPosts(@Nullable Collection<Long> ids) {
|
||||
return getPosts(ids, asSet(CommonStatusEnum.ENABLE.getStatus(), CommonStatusEnum.DISABLE.getStatus()));
|
||||
default List<PostDO> getPostList(@Nullable Collection<Long> ids) {
|
||||
return getPostList(ids, asSet(CommonStatusEnum.ENABLE.getStatus(), CommonStatusEnum.DISABLE.getStatus()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,7 +60,7 @@ public interface PostService {
|
||||
* @param statuses 状态数组。如果为空,不进行筛选
|
||||
* @return 部门列表
|
||||
*/
|
||||
List<PostDO> getPosts(@Nullable Collection<Long> ids, @Nullable Collection<Integer> statuses);
|
||||
List<PostDO> getPostList(@Nullable Collection<Long> ids, @Nullable Collection<Integer> statuses);
|
||||
|
||||
/**
|
||||
* 获得岗位分页列表
|
||||
@ -76,7 +76,7 @@ public interface PostService {
|
||||
* @param reqVO 查询条件
|
||||
* @return 部门列表
|
||||
*/
|
||||
List<PostDO> getPosts(PostExportReqVO reqVO);
|
||||
List<PostDO> getPostList(PostExportReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得岗位信息
|
||||
@ -93,6 +93,6 @@ public interface PostService {
|
||||
*
|
||||
* @param ids 岗位编号数组
|
||||
*/
|
||||
void validPosts(Collection<Long> ids);
|
||||
void validatePostList(Collection<Long> ids);
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.service.dept;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
|
||||
@ -38,7 +37,8 @@ public class PostServiceImpl implements PostService {
|
||||
@Override
|
||||
public Long createPost(PostCreateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
this.checkCreateOrUpdate(null, reqVO.getName(), reqVO.getCode());
|
||||
validatePostForCreateOrUpdate(null, reqVO.getName(), reqVO.getCode());
|
||||
|
||||
// 插入岗位
|
||||
PostDO post = PostConvert.INSTANCE.convert(reqVO);
|
||||
postMapper.insert(post);
|
||||
@ -48,7 +48,8 @@ public class PostServiceImpl implements PostService {
|
||||
@Override
|
||||
public void updatePost(PostUpdateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
this.checkCreateOrUpdate(reqVO.getId(), reqVO.getName(), reqVO.getCode());
|
||||
validatePostForCreateOrUpdate(reqVO.getId(), reqVO.getName(), reqVO.getCode());
|
||||
|
||||
// 更新岗位
|
||||
PostDO updateObj = PostConvert.INSTANCE.convert(reqVO);
|
||||
postMapper.updateById(updateObj);
|
||||
@ -57,13 +58,59 @@ public class PostServiceImpl implements PostService {
|
||||
@Override
|
||||
public void deletePost(Long id) {
|
||||
// 校验是否存在
|
||||
this.checkPostExists(id);
|
||||
validatePostExists(id);
|
||||
// 删除部门
|
||||
postMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private void validatePostForCreateOrUpdate(Long id, String name, String code) {
|
||||
// 校验自己存在
|
||||
validatePostExists(id);
|
||||
// 校验岗位名的唯一性
|
||||
validatePostNameUnique(id, name);
|
||||
// 校验岗位编码的唯一性
|
||||
validatePostCodeUnique(id, code);
|
||||
}
|
||||
|
||||
private void validatePostNameUnique(Long id, String name) {
|
||||
PostDO post = postMapper.selectByName(name);
|
||||
if (post == null) {
|
||||
return;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
|
||||
if (id == null) {
|
||||
throw exception(POST_NAME_DUPLICATE);
|
||||
}
|
||||
if (!post.getId().equals(id)) {
|
||||
throw exception(POST_NAME_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
private void validatePostCodeUnique(Long id, String code) {
|
||||
PostDO post = postMapper.selectByCode(code);
|
||||
if (post == null) {
|
||||
return;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
|
||||
if (id == null) {
|
||||
throw exception(POST_CODE_DUPLICATE);
|
||||
}
|
||||
if (!post.getId().equals(id)) {
|
||||
throw exception(POST_CODE_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
private void validatePostExists(Long id) {
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
if (postMapper.selectById(id) == null) {
|
||||
throw exception(POST_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PostDO> getPosts(Collection<Long> ids, Collection<Integer> statuses) {
|
||||
public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses) {
|
||||
return postMapper.selectList(ids, statuses);
|
||||
}
|
||||
|
||||
@ -73,7 +120,7 @@ public class PostServiceImpl implements PostService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PostDO> getPosts(PostExportReqVO reqVO) {
|
||||
public List<PostDO> getPostList(PostExportReqVO reqVO) {
|
||||
return postMapper.selectList(reqVO);
|
||||
}
|
||||
|
||||
@ -82,55 +129,8 @@ public class PostServiceImpl implements PostService {
|
||||
return postMapper.selectById(id);
|
||||
}
|
||||
|
||||
private void checkCreateOrUpdate(Long id, String name, String code) {
|
||||
// 校验自己存在
|
||||
checkPostExists(id);
|
||||
// 校验岗位名的唯一性
|
||||
checkPostNameUnique(id, name);
|
||||
// 校验岗位编码的唯一性
|
||||
checkPostCodeUnique(id, code);
|
||||
}
|
||||
|
||||
private void checkPostNameUnique(Long id, String name) {
|
||||
PostDO post = postMapper.selectByName(name);
|
||||
if (post == null) {
|
||||
return;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
|
||||
if (id == null) {
|
||||
throw ServiceExceptionUtil.exception(POST_NAME_DUPLICATE);
|
||||
}
|
||||
if (!post.getId().equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(POST_NAME_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkPostCodeUnique(Long id, String code) {
|
||||
PostDO post = postMapper.selectByCode(code);
|
||||
if (post == null) {
|
||||
return;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
|
||||
if (id == null) {
|
||||
throw ServiceExceptionUtil.exception(POST_CODE_DUPLICATE);
|
||||
}
|
||||
if (!post.getId().equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(POST_CODE_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkPostExists(Long id) {
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
PostDO post = postMapper.selectById(id);
|
||||
if (post == null) {
|
||||
throw ServiceExceptionUtil.exception(POST_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validPosts(Collection<Long> ids) {
|
||||
public void validatePostList(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return;
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ public interface DictDataService {
|
||||
*
|
||||
* @return 字典数据全列表
|
||||
*/
|
||||
List<DictDataDO> getDictDatas();
|
||||
List<DictDataDO> getDictDataList();
|
||||
|
||||
/**
|
||||
* 获得字典数据分页列表
|
||||
@ -60,7 +60,7 @@ public interface DictDataService {
|
||||
* @param reqVO 列表请求
|
||||
* @return 字典数据列表
|
||||
*/
|
||||
List<DictDataDO> getDictDatas(DictDataExportReqVO reqVO);
|
||||
List<DictDataDO> getDictDataList(DictDataExportReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得字典数据详情
|
||||
@ -86,7 +86,7 @@ public interface DictDataService {
|
||||
* @param dictType 字典类型
|
||||
* @param values 字典数据值的数组
|
||||
*/
|
||||
void validDictDatas(String dictType, Collection<String> values);
|
||||
void validateDictDataList(String dictType, Collection<String> values);
|
||||
|
||||
/**
|
||||
* 获得指定的字典数据
|
||||
|
@ -48,7 +48,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
private DictDataMapper dictDataMapper;
|
||||
|
||||
@Override
|
||||
public List<DictDataDO> getDictDatas() {
|
||||
public List<DictDataDO> getDictDataList() {
|
||||
List<DictDataDO> list = dictDataMapper.selectList();
|
||||
list.sort(COMPARATOR_TYPE_AND_SORT);
|
||||
return list;
|
||||
@ -60,7 +60,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DictDataDO> getDictDatas(DictDataExportReqVO reqVO) {
|
||||
public List<DictDataDO> getDictDataList(DictDataExportReqVO reqVO) {
|
||||
List<DictDataDO> list = dictDataMapper.selectList(reqVO);
|
||||
list.sort(COMPARATOR_TYPE_AND_SORT);
|
||||
return list;
|
||||
@ -74,7 +74,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
@Override
|
||||
public Long createDictData(DictDataCreateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
checkCreateOrUpdate(null, reqVO.getValue(), reqVO.getDictType());
|
||||
validateDictDataForCreateOrUpdate(null, reqVO.getValue(), reqVO.getDictType());
|
||||
|
||||
// 插入字典类型
|
||||
DictDataDO dictData = DictDataConvert.INSTANCE.convert(reqVO);
|
||||
@ -85,7 +85,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
@Override
|
||||
public void updateDictData(DictDataUpdateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
checkCreateOrUpdate(reqVO.getId(), reqVO.getValue(), reqVO.getDictType());
|
||||
validateDictDataForCreateOrUpdate(reqVO.getId(), reqVO.getValue(), reqVO.getDictType());
|
||||
|
||||
// 更新字典类型
|
||||
DictDataDO updateObj = DictDataConvert.INSTANCE.convert(reqVO);
|
||||
@ -95,7 +95,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
@Override
|
||||
public void deleteDictData(Long id) {
|
||||
// 校验是否存在
|
||||
checkDictDataExists(id);
|
||||
validateDictDataExists(id);
|
||||
|
||||
// 删除字典数据
|
||||
dictDataMapper.deleteById(id);
|
||||
@ -106,18 +106,17 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
return dictDataMapper.selectCountByDictType(dictType);
|
||||
}
|
||||
|
||||
|
||||
private void checkCreateOrUpdate(Long id, String value, String dictType) {
|
||||
private void validateDictDataForCreateOrUpdate(Long id, String value, String dictType) {
|
||||
// 校验自己存在
|
||||
checkDictDataExists(id);
|
||||
validateDictDataExists(id);
|
||||
// 校验字典类型有效
|
||||
checkDictTypeValid(dictType);
|
||||
validateDictTypeExists(dictType);
|
||||
// 校验字典数据的值的唯一性
|
||||
checkDictDataValueUnique(id, dictType, value);
|
||||
validateDictDataValueUnique(id, dictType, value);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkDictDataValueUnique(Long id, String dictType, String value) {
|
||||
public void validateDictDataValueUnique(Long id, String dictType, String value) {
|
||||
DictDataDO dictData = dictDataMapper.selectByDictTypeAndValue(dictType, value);
|
||||
if (dictData == null) {
|
||||
return;
|
||||
@ -132,7 +131,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkDictDataExists(Long id) {
|
||||
public void validateDictDataExists(Long id) {
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
@ -143,7 +142,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkDictTypeValid(String type) {
|
||||
public void validateDictTypeExists(String type) {
|
||||
DictTypeDO dictType = dictTypeService.getDictType(type);
|
||||
if (dictType == null) {
|
||||
throw exception(DICT_TYPE_NOT_EXISTS);
|
||||
@ -154,7 +153,7 @@ public class DictDataServiceImpl implements DictDataService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validDictDatas(String dictType, Collection<String> values) {
|
||||
public void validateDictDataList(String dictType, Collection<String> values) {
|
||||
if (CollUtil.isEmpty(values)) {
|
||||
return;
|
||||
}
|
||||
|
@ -57,7 +57,8 @@ public class DictTypeServiceImpl implements DictTypeService {
|
||||
@Override
|
||||
public Long createDictType(DictTypeCreateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
checkCreateOrUpdate(null, reqVO.getName(), reqVO.getType());
|
||||
validateDictTypeForCreateOrUpdate(null, reqVO.getName(), reqVO.getType());
|
||||
|
||||
// 插入字典类型
|
||||
DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO)
|
||||
.setDeletedTime(LocalDateTimeUtils.EMPTY); // 唯一索引,避免 null 值
|
||||
@ -68,7 +69,8 @@ public class DictTypeServiceImpl implements DictTypeService {
|
||||
@Override
|
||||
public void updateDictType(DictTypeUpdateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
checkCreateOrUpdate(reqVO.getId(), reqVO.getName(), null);
|
||||
validateDictTypeForCreateOrUpdate(reqVO.getId(), reqVO.getName(), null);
|
||||
|
||||
// 更新字典类型
|
||||
DictTypeDO updateObj = DictTypeConvert.INSTANCE.convert(reqVO);
|
||||
dictTypeMapper.updateById(updateObj);
|
||||
@ -77,7 +79,7 @@ public class DictTypeServiceImpl implements DictTypeService {
|
||||
@Override
|
||||
public void deleteDictType(Long id) {
|
||||
// 校验是否存在
|
||||
DictTypeDO dictType = checkDictTypeExists(id);
|
||||
DictTypeDO dictType = validateDictTypeExists(id);
|
||||
// 校验是否有字典数据
|
||||
if (dictDataService.countByDictType(dictType.getType()) > 0) {
|
||||
throw exception(DICT_TYPE_HAS_CHILDREN);
|
||||
@ -91,17 +93,17 @@ public class DictTypeServiceImpl implements DictTypeService {
|
||||
return dictTypeMapper.selectList();
|
||||
}
|
||||
|
||||
private void checkCreateOrUpdate(Long id, String name, String type) {
|
||||
private void validateDictTypeForCreateOrUpdate(Long id, String name, String type) {
|
||||
// 校验自己存在
|
||||
checkDictTypeExists(id);
|
||||
validateDictTypeExists(id);
|
||||
// 校验字典类型的名字的唯一性
|
||||
checkDictTypeNameUnique(id, name);
|
||||
validateDictTypeNameUnique(id, name);
|
||||
// 校验字典类型的类型的唯一性
|
||||
checkDictTypeUnique(id, type);
|
||||
validateDictTypeUnique(id, type);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkDictTypeNameUnique(Long id, String name) {
|
||||
void validateDictTypeNameUnique(Long id, String name) {
|
||||
DictTypeDO dictType = dictTypeMapper.selectByName(name);
|
||||
if (dictType == null) {
|
||||
return;
|
||||
@ -116,7 +118,7 @@ public class DictTypeServiceImpl implements DictTypeService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkDictTypeUnique(Long id, String type) {
|
||||
void validateDictTypeUnique(Long id, String type) {
|
||||
if (StrUtil.isEmpty(type)) {
|
||||
return;
|
||||
}
|
||||
@ -134,7 +136,7 @@ public class DictTypeServiceImpl implements DictTypeService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public DictTypeDO checkDictTypeExists(Long id) {
|
||||
DictTypeDO validateDictTypeExists(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ public class ErrorCodeServiceImpl implements ErrorCodeService {
|
||||
@Override
|
||||
public void updateErrorCode(ErrorCodeUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
this.validateErrorCodeExists(updateReqVO.getId());
|
||||
validateErrorCodeExists(updateReqVO.getId());
|
||||
// 校验 code 重复
|
||||
validateCodeDuplicate(updateReqVO.getCode(), updateReqVO.getId());
|
||||
|
||||
@ -71,7 +71,7 @@ public class ErrorCodeServiceImpl implements ErrorCodeService {
|
||||
@Override
|
||||
public void deleteErrorCode(Long id) {
|
||||
// 校验存在
|
||||
this.validateErrorCodeExists(id);
|
||||
validateErrorCodeExists(id);
|
||||
// 删除
|
||||
errorCodeMapper.deleteById(id);
|
||||
}
|
||||
@ -100,7 +100,7 @@ public class ErrorCodeServiceImpl implements ErrorCodeService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void validateErrorCodeExists(Long id) {
|
||||
void validateErrorCodeExists(Long id) {
|
||||
if (errorCodeMapper.selectById(id) == null) {
|
||||
throw exception(ERROR_CODE_NOT_EXISTS);
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package cn.iocoder.yudao.module.system.service.logger;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.service.OperateLog;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.service.OperateLogFrameworkService;
|
||||
import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO;
|
||||
@ -38,6 +36,6 @@ public interface OperateLogService {
|
||||
* @param reqVO 列表条件
|
||||
* @return 日志列表
|
||||
*/
|
||||
List<OperateLogDO> getOperateLogs(OperateLogExportReqVO reqVO);
|
||||
List<OperateLogDO> getOperateLogList(OperateLogExportReqVO reqVO);
|
||||
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public class OperateLogServiceImpl implements OperateLogService {
|
||||
// 处理基于用户昵称的查询
|
||||
Collection<Long> userIds = null;
|
||||
if (StrUtil.isNotEmpty(reqVO.getUserNickname())) {
|
||||
userIds = convertSet(userService.getUsersByNickname(reqVO.getUserNickname()), AdminUserDO::getId);
|
||||
userIds = convertSet(userService.getUserListByNickname(reqVO.getUserNickname()), AdminUserDO::getId);
|
||||
if (CollUtil.isEmpty(userIds)) {
|
||||
return PageResult.empty();
|
||||
}
|
||||
@ -59,11 +59,11 @@ public class OperateLogServiceImpl implements OperateLogService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OperateLogDO> getOperateLogs(OperateLogExportReqVO reqVO) {
|
||||
public List<OperateLogDO> getOperateLogList(OperateLogExportReqVO reqVO) {
|
||||
// 处理基于用户昵称的查询
|
||||
Collection<Long> userIds = null;
|
||||
if (StrUtil.isNotEmpty(reqVO.getUserNickname())) {
|
||||
userIds = convertSet(userService.getUsersByNickname(reqVO.getUserNickname()), AdminUserDO::getId);
|
||||
userIds = convertSet(userService.getUserListByNickname(reqVO.getUserNickname()), AdminUserDO::getId);
|
||||
if (CollUtil.isEmpty(userIds)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.system.service.mail;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.mail.MailAccount;
|
||||
import cn.hutool.extra.mail.MailUtil;
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
|
||||
@ -20,15 +19,13 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 邮箱模版 服务实现类
|
||||
* 邮箱发送 Service 实现类
|
||||
*
|
||||
* @author wangjingyi
|
||||
* @since 2022-03-21
|
||||
@ -82,13 +79,13 @@ public class MailSendServiceImpl implements MailSendService {
|
||||
public Long sendSingleMail(String mail, Long userId, Integer userType,
|
||||
String templateCode, Map<String, Object> templateParams) {
|
||||
// 校验邮箱模版是否合法
|
||||
MailTemplateDO template = checkMailTemplateValid(templateCode);
|
||||
MailTemplateDO template = validateMailTemplate(templateCode);
|
||||
// 校验邮箱账号是否合法
|
||||
MailAccountDO account = checkMailAccountValid(template.getAccountId());
|
||||
MailAccountDO account = validateMailAccount(template.getAccountId());
|
||||
|
||||
// 校验邮箱是否存在
|
||||
mail = checkMail(mail);
|
||||
checkTemplateParams(template, templateParams);
|
||||
mail = validateMail(mail);
|
||||
validateTemplateParams(template, templateParams);
|
||||
|
||||
// 创建发送日志。如果模板被禁用,则不发送短信,只记录日志
|
||||
Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus());
|
||||
@ -106,7 +103,7 @@ public class MailSendServiceImpl implements MailSendService {
|
||||
@Override
|
||||
public void doSendMail(MailSendMessage message) {
|
||||
// 1. 创建发送账号
|
||||
MailAccountDO account = checkMailAccountValid(message.getAccountId());
|
||||
MailAccountDO account = validateMailAccount(message.getAccountId());
|
||||
MailAccount mailAccount = MailAccountConvert.INSTANCE.convert(account, message.getNickname());
|
||||
// 2. 发送邮件
|
||||
try {
|
||||
@ -121,7 +118,7 @@ public class MailSendServiceImpl implements MailSendService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public MailTemplateDO checkMailTemplateValid(String templateCode) {
|
||||
MailTemplateDO validateMailTemplate(String templateCode) {
|
||||
// 获得邮件模板。考虑到效率,从缓存中获取
|
||||
MailTemplateDO template = mailTemplateService.getMailTemplateByCodeFromCache(templateCode);
|
||||
// 邮件模板不存在
|
||||
@ -132,7 +129,7 @@ public class MailSendServiceImpl implements MailSendService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public MailAccountDO checkMailAccountValid(Long accountId) {
|
||||
MailAccountDO validateMailAccount(Long accountId) {
|
||||
// 获得邮箱账号。考虑到效率,从缓存中获取
|
||||
MailAccountDO account = mailAccountService.getMailAccountFromCache(accountId);
|
||||
// 邮箱账号不存在
|
||||
@ -143,7 +140,7 @@ public class MailSendServiceImpl implements MailSendService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public String checkMail(String mail) {
|
||||
String validateMail(String mail) {
|
||||
if (StrUtil.isEmpty(mail)) {
|
||||
throw exception(MAIL_SEND_MAIL_NOT_EXISTS);
|
||||
}
|
||||
@ -157,7 +154,7 @@ public class MailSendServiceImpl implements MailSendService {
|
||||
* @param templateParams 参数列表
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) {
|
||||
void validateTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) {
|
||||
template.getParams().forEach(key -> {
|
||||
Object value = templateParams.get(key);
|
||||
if (value == null) {
|
||||
|
@ -39,7 +39,7 @@ public interface NoticeService {
|
||||
* @param reqVO 分页条件
|
||||
* @return 部门分页列表
|
||||
*/
|
||||
PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO);
|
||||
PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得岗位公告公告信息
|
||||
|
@ -1,18 +1,18 @@
|
||||
package cn.iocoder.yudao.module.system.service.notice;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.convert.notice.NoticeConvert;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
|
||||
|
||||
/**
|
||||
@ -36,7 +36,7 @@ public class NoticeServiceImpl implements NoticeService {
|
||||
@Override
|
||||
public void updateNotice(NoticeUpdateReqVO reqVO) {
|
||||
// 校验是否存在
|
||||
this.checkNoticeExists(reqVO.getId());
|
||||
validateNoticeExists(reqVO.getId());
|
||||
// 更新通知公告
|
||||
NoticeDO updateObj = NoticeConvert.INSTANCE.convert(reqVO);
|
||||
noticeMapper.updateById(updateObj);
|
||||
@ -45,13 +45,13 @@ public class NoticeServiceImpl implements NoticeService {
|
||||
@Override
|
||||
public void deleteNotice(Long id) {
|
||||
// 校验是否存在
|
||||
this.checkNoticeExists(id);
|
||||
validateNoticeExists(id);
|
||||
// 删除通知公告
|
||||
noticeMapper.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO) {
|
||||
public PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO) {
|
||||
return noticeMapper.selectPage(reqVO);
|
||||
}
|
||||
|
||||
@ -61,13 +61,13 @@ public class NoticeServiceImpl implements NoticeService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkNoticeExists(Long id) {
|
||||
public void validateNoticeExists(Long id) {
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
NoticeDO notice = noticeMapper.selectById(id);
|
||||
if (notice == null) {
|
||||
throw ServiceExceptionUtil.exception(NOTICE_NOT_FOUND);
|
||||
throw exception(NOTICE_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,13 +44,13 @@ public class NotifySendServiceImpl implements NotifySendService {
|
||||
@Override
|
||||
public Long sendSingleNotify(Long userId, Integer userType, String templateCode, Map<String, Object> templateParams) {
|
||||
// 校验模版
|
||||
NotifyTemplateDO template = checkNotifyTemplateValid(templateCode);
|
||||
NotifyTemplateDO template = validateNotifyTemplate(templateCode);
|
||||
if (Objects.equals(template.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
|
||||
log.info("[sendSingleNotify][模版({})已经关闭,无法给用户({}/{})发送]", templateCode, userId, userType);
|
||||
return null;
|
||||
}
|
||||
// 校验参数
|
||||
checkTemplateParams(template, templateParams);
|
||||
validateTemplateParams(template, templateParams);
|
||||
|
||||
// 发送站内信
|
||||
String content = notifyTemplateService.formatNotifyTemplateContent(template.getContent(), templateParams);
|
||||
@ -58,7 +58,7 @@ public class NotifySendServiceImpl implements NotifySendService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public NotifyTemplateDO checkNotifyTemplateValid(String templateCode) {
|
||||
public NotifyTemplateDO validateNotifyTemplate(String templateCode) {
|
||||
// 获得站内信模板。考虑到效率,从缓存中获取
|
||||
NotifyTemplateDO template = notifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode);
|
||||
// 站内信模板不存在
|
||||
@ -75,7 +75,7 @@ public class NotifySendServiceImpl implements NotifySendService {
|
||||
* @param templateParams 参数列表
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) {
|
||||
public void validateTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) {
|
||||
template.getParams().forEach(key -> {
|
||||
Object value = templateParams.get(key);
|
||||
if (value == null) {
|
||||
|
@ -76,7 +76,7 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
||||
@Override
|
||||
public Long createNotifyTemplate(NotifyTemplateCreateReqVO createReqVO) {
|
||||
// 校验站内信编码是否重复
|
||||
checkNotifyTemplateCodeDuplicate(null, createReqVO.getCode());
|
||||
validateNotifyTemplateCodeDuplicate(null, createReqVO.getCode());
|
||||
|
||||
// 插入
|
||||
NotifyTemplateDO notifyTemplate = NotifyTemplateConvert.INSTANCE.convert(createReqVO);
|
||||
@ -93,7 +93,7 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
||||
// 校验存在
|
||||
validateNotifyTemplateExists(updateReqVO.getId());
|
||||
// 校验站内信编码是否重复
|
||||
checkNotifyTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode());
|
||||
validateNotifyTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode());
|
||||
|
||||
// 更新
|
||||
NotifyTemplateDO updateObj = NotifyTemplateConvert.INSTANCE.convert(updateReqVO);
|
||||
@ -136,7 +136,7 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkNotifyTemplateCodeDuplicate(Long id, String code) {
|
||||
public void validateNotifyTemplateCodeDuplicate(Long id, String code) {
|
||||
NotifyTemplateDO template = notifyTemplateMapper.selectByCode(code);
|
||||
if (template == null) {
|
||||
return;
|
||||
|
@ -47,7 +47,7 @@ public interface MenuService {
|
||||
*
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<MenuDO> getMenus();
|
||||
List<MenuDO> getMenuList();
|
||||
|
||||
/**
|
||||
* 基于租户,筛选菜单列表
|
||||
@ -56,7 +56,7 @@ public interface MenuService {
|
||||
* @param reqVO 筛选条件请求 VO
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<MenuDO> getTenantMenus(MenuListReqVO reqVO);
|
||||
List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 筛选菜单列表
|
||||
@ -64,7 +64,7 @@ public interface MenuService {
|
||||
* @param reqVO 筛选条件请求 VO
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<MenuDO> getMenus(MenuListReqVO reqVO);
|
||||
List<MenuDO> getMenuList(MenuListReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得所有菜单,从缓存中
|
||||
|
@ -10,7 +10,6 @@ import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuUp
|
||||
import cn.iocoder.yudao.module.system.convert.permission.MenuConvert;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper;
|
||||
import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum;
|
||||
import cn.iocoder.yudao.module.system.enums.permission.MenuTypeEnum;
|
||||
import cn.iocoder.yudao.module.system.mq.producer.permission.MenuProducer;
|
||||
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
|
||||
@ -19,6 +18,7 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -31,6 +31,7 @@ import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
@ -49,6 +50,7 @@ public class MenuServiceImpl implements MenuService {
|
||||
* 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private volatile Map<Long, MenuDO> menuCache;
|
||||
/**
|
||||
* 权限与菜单缓存
|
||||
@ -58,6 +60,7 @@ public class MenuServiceImpl implements MenuService {
|
||||
* 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private volatile Multimap<String, MenuDO> permissionMenuCache;
|
||||
|
||||
@Resource
|
||||
@ -97,9 +100,10 @@ public class MenuServiceImpl implements MenuService {
|
||||
@Override
|
||||
public Long createMenu(MenuCreateReqVO reqVO) {
|
||||
// 校验父菜单存在
|
||||
checkParentResource(reqVO.getParentId(), null);
|
||||
validateParentMenu(reqVO.getParentId(), null);
|
||||
// 校验菜单(自己)
|
||||
checkResource(reqVO.getParentId(), reqVO.getName(), null);
|
||||
validateMenu(reqVO.getParentId(), reqVO.getName(), null);
|
||||
|
||||
// 插入数据库
|
||||
MenuDO menu = MenuConvert.INSTANCE.convert(reqVO);
|
||||
initMenuProperty(menu);
|
||||
@ -117,9 +121,10 @@ public class MenuServiceImpl implements MenuService {
|
||||
throw ServiceExceptionUtil.exception(MENU_NOT_EXISTS);
|
||||
}
|
||||
// 校验父菜单存在
|
||||
checkParentResource(reqVO.getParentId(), reqVO.getId());
|
||||
validateParentMenu(reqVO.getParentId(), reqVO.getId());
|
||||
// 校验菜单(自己)
|
||||
checkResource(reqVO.getParentId(), reqVO.getName(), reqVO.getId());
|
||||
validateMenu(reqVO.getParentId(), reqVO.getName(), reqVO.getId());
|
||||
|
||||
// 更新到数据库
|
||||
MenuDO updateObject = MenuConvert.INSTANCE.convert(reqVO);
|
||||
initMenuProperty(updateObject);
|
||||
@ -128,13 +133,8 @@ public class MenuServiceImpl implements MenuService {
|
||||
menuProducer.sendMenuRefreshMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
*
|
||||
* @param menuId 菜单编号
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteMenu(Long menuId) {
|
||||
// 校验是否还有子菜单
|
||||
if (menuMapper.selectCountByParentId(menuId) > 0) {
|
||||
@ -160,20 +160,20 @@ public class MenuServiceImpl implements MenuService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuDO> getMenus() {
|
||||
public List<MenuDO> getMenuList() {
|
||||
return menuMapper.selectList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuDO> getTenantMenus(MenuListReqVO reqVO) {
|
||||
List<MenuDO> menus = getMenus(reqVO);
|
||||
public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) {
|
||||
List<MenuDO> menus = getMenuList(reqVO);
|
||||
// 开启多租户的情况下,需要过滤掉未开通的菜单
|
||||
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId())));
|
||||
return menus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuDO> getMenus(MenuListReqVO reqVO) {
|
||||
public List<MenuDO> getMenuList(MenuListReqVO reqVO) {
|
||||
return menuMapper.selectList(reqVO);
|
||||
}
|
||||
|
||||
@ -223,8 +223,8 @@ public class MenuServiceImpl implements MenuService {
|
||||
* @param childId 当前菜单编号
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkParentResource(Long parentId, Long childId) {
|
||||
if (parentId == null || MenuIdEnum.ROOT.getId().equals(parentId)) {
|
||||
void validateParentMenu(Long parentId, Long childId) {
|
||||
if (parentId == null || ID_ROOT.equals(parentId)) {
|
||||
return;
|
||||
}
|
||||
// 不能设置自己为父菜单
|
||||
@ -253,7 +253,7 @@ public class MenuServiceImpl implements MenuService {
|
||||
* @param id 菜单编号
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkResource(Long parentId, String name, Long id) {
|
||||
void validateMenu(Long parentId, String name, Long id) {
|
||||
MenuDO menu = menuMapper.selectByParentIdAndName(parentId, name);
|
||||
if (menu == null) {
|
||||
return;
|
||||
|
@ -158,7 +158,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||
}
|
||||
|
||||
// 判断角色是否包含超级管理员。如果是超级管理员,获取到全部
|
||||
List<RoleDO> roleList = roleService.getRolesFromCache(roleIds);
|
||||
List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds);
|
||||
if (roleService.hasAnySuperAdmin(roleList)) {
|
||||
return menuService.getMenuListFromCache(menuTypes, menusStatuses);
|
||||
}
|
||||
@ -190,7 +190,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||
public Set<Long> getRoleMenuIds(Long roleId) {
|
||||
// 如果是管理员的情况下,获取全部菜单编号
|
||||
if (roleService.hasAnySuperAdmin(Collections.singleton(roleId))) {
|
||||
return convertSet(menuService.getMenus(), MenuDO::getId);
|
||||
return convertSet(menuService.getMenuList(), MenuDO::getId);
|
||||
}
|
||||
// 如果是非管理员的情况下,获得拥有的菜单编号
|
||||
return convertSet(roleMenuMapper.selectListByRoleId(roleId), RoleMenuDO::getMenuId);
|
||||
@ -371,7 +371,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||
if (roleService.hasAnySuperAdmin(roleIds)) {
|
||||
return true;
|
||||
}
|
||||
Set<String> userRoles = convertSet(roleService.getRolesFromCache(roleIds),
|
||||
Set<String> userRoles = convertSet(roleService.getRoleListFromCache(roleIds),
|
||||
RoleDO::getCode);
|
||||
return CollUtil.containsAny(userRoles, Sets.newHashSet(roles));
|
||||
}
|
||||
@ -388,7 +388,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||
result.setSelf(true);
|
||||
return result;
|
||||
}
|
||||
List<RoleDO> roles = roleService.getRolesFromCache(roleIds);
|
||||
List<RoleDO> roles = roleService.getRoleListFromCache(roleIds);
|
||||
|
||||
// 获得用户的部门编号的缓存,通过 Guava 的 Suppliers 惰性求值,即有且仅有第一次发起 DB 的查询
|
||||
Supplier<Long> userDeptIdCache = Suppliers.memoize(() -> userService.getUser(userId).getDeptId());
|
||||
@ -418,7 +418,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||
}
|
||||
// 情况四,DEPT_DEPT_AND_CHILD
|
||||
if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_AND_CHILD.getScope())) {
|
||||
List<DeptDO> depts = deptService.getDeptsByParentIdFromCache(userDeptIdCache.get(), true);
|
||||
List<DeptDO> depts = deptService.getDeptListByParentIdFromCache(userDeptIdCache.get(), true);
|
||||
CollUtil.addAll(result.getDeptIds(), CollectionUtils.convertList(depts, DeptDO::getId));
|
||||
// 添加本身部门编号
|
||||
CollUtil.addAll(result.getDeptIds(), userDeptIdCache.get());
|
||||
|
@ -79,7 +79,7 @@ public interface RoleService {
|
||||
* @param statuses 筛选的状态。允许空,空时不筛选
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<RoleDO> getRoles(@Nullable Collection<Integer> statuses);
|
||||
List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses);
|
||||
|
||||
/**
|
||||
* 获得角色数组,从缓存中
|
||||
@ -87,7 +87,7 @@ public interface RoleService {
|
||||
* @param ids 角色编号数组
|
||||
* @return 角色数组
|
||||
*/
|
||||
List<RoleDO> getRolesFromCache(Collection<Long> ids);
|
||||
List<RoleDO> getRoleListFromCache(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 判断角色数组中,是否有超级管理员
|
||||
@ -104,7 +104,7 @@ public interface RoleService {
|
||||
* @return 是否有管理员
|
||||
*/
|
||||
default boolean hasAnySuperAdmin(Set<Long> ids) {
|
||||
return hasAnySuperAdmin(getRolesFromCache(ids));
|
||||
return hasAnySuperAdmin(getRoleListFromCache(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,6 +138,6 @@ public interface RoleService {
|
||||
*
|
||||
* @param ids 角色编号数组
|
||||
*/
|
||||
void validRoles(Collection<Long> ids);
|
||||
void validateRoleList(Collection<Long> ids);
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleExportReqVO;
|
||||
@ -34,6 +33,7 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
@ -76,7 +76,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
log.info("[initLocalCache][缓存角色,数量为:{}]", roleList.size());
|
||||
|
||||
// 第二步:构建缓存
|
||||
roleCache = CollectionUtils.convertMap(roleList, RoleDO::getId);
|
||||
roleCache = convertMap(roleList, RoleDO::getId);
|
||||
});
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
@Transactional
|
||||
public Long createRole(RoleCreateReqVO reqVO, Integer type) {
|
||||
// 校验角色
|
||||
checkDuplicateRole(reqVO.getName(), reqVO.getCode(), null);
|
||||
validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), null);
|
||||
// 插入到数据库
|
||||
RoleDO role = RoleConvert.INSTANCE.convert(reqVO);
|
||||
role.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType()));
|
||||
@ -105,13 +105,13 @@ public class RoleServiceImpl implements RoleService {
|
||||
@Override
|
||||
public void updateRole(RoleUpdateReqVO reqVO) {
|
||||
// 校验是否可以更新
|
||||
checkUpdateRole(reqVO.getId());
|
||||
validateRoleForUpdate(reqVO.getId());
|
||||
// 校验角色的唯一字段是否重复
|
||||
checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId());
|
||||
validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), reqVO.getId());
|
||||
|
||||
// 更新到数据库
|
||||
RoleDO updateObject = RoleConvert.INSTANCE.convert(reqVO);
|
||||
roleMapper.updateById(updateObject);
|
||||
RoleDO updateObj = RoleConvert.INSTANCE.convert(reqVO);
|
||||
roleMapper.updateById(updateObj);
|
||||
// 发送刷新消息
|
||||
roleProducer.sendRoleRefreshMessage();
|
||||
}
|
||||
@ -119,12 +119,11 @@ public class RoleServiceImpl implements RoleService {
|
||||
@Override
|
||||
public void updateRoleStatus(Long id, Integer status) {
|
||||
// 校验是否可以更新
|
||||
checkUpdateRole(id);
|
||||
validateRoleForUpdate(id);
|
||||
|
||||
// 更新状态
|
||||
RoleDO updateObject = new RoleDO();
|
||||
updateObject.setId(id);
|
||||
updateObject.setStatus(status);
|
||||
roleMapper.updateById(updateObject);
|
||||
RoleDO updateObj = new RoleDO().setId(id).setStatus(status);
|
||||
roleMapper.updateById(updateObj);
|
||||
// 发送刷新消息
|
||||
roleProducer.sendRoleRefreshMessage();
|
||||
}
|
||||
@ -132,7 +131,8 @@ public class RoleServiceImpl implements RoleService {
|
||||
@Override
|
||||
public void updateRoleDataScope(Long id, Integer dataScope, Set<Long> dataScopeDeptIds) {
|
||||
// 校验是否可以更新
|
||||
checkUpdateRole(id);
|
||||
validateRoleForUpdate(id);
|
||||
|
||||
// 更新数据范围
|
||||
RoleDO updateObject = new RoleDO();
|
||||
updateObject.setId(id);
|
||||
@ -147,7 +147,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteRole(Long id) {
|
||||
// 校验是否可以更新
|
||||
this.checkUpdateRole(id);
|
||||
validateRoleForUpdate(id);
|
||||
// 标记删除
|
||||
roleMapper.deleteById(id);
|
||||
// 删除相关数据
|
||||
@ -169,7 +169,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RoleDO> getRoles(@Nullable Collection<Integer> statuses) {
|
||||
public List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses) {
|
||||
if (CollUtil.isEmpty(statuses)) {
|
||||
return roleMapper.selectList();
|
||||
}
|
||||
@ -177,7 +177,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RoleDO> getRolesFromCache(Collection<Long> ids) {
|
||||
public List<RoleDO> getRoleListFromCache(Collection<Long> ids) {
|
||||
if (CollectionUtil.isEmpty(ids)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@ -219,7 +219,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
* @param id 角色编号
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkDuplicateRole(String name, String code, Long id) {
|
||||
void validateRoleDuplicate(String name, String code, Long id) {
|
||||
// 0. 超级管理员,不允许创建
|
||||
if (RoleCodeEnum.isSuperAdmin(code)) {
|
||||
throw exception(ROLE_ADMIN_CODE_ERROR, code);
|
||||
@ -246,7 +246,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
* @param id 角色编号
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkUpdateRole(Long id) {
|
||||
void validateRoleForUpdate(Long id) {
|
||||
RoleDO roleDO = roleMapper.selectById(id);
|
||||
if (roleDO == null) {
|
||||
throw exception(ROLE_NOT_EXISTS);
|
||||
@ -258,13 +258,13 @@ public class RoleServiceImpl implements RoleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validRoles(Collection<Long> ids) {
|
||||
public void validateRoleList(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return;
|
||||
}
|
||||
// 获得角色信息
|
||||
List<RoleDO> roles = roleMapper.selectBatchIds(ids);
|
||||
Map<Long, RoleDO> roleMap = CollectionUtils.convertMap(roles, RoleDO::getId);
|
||||
Map<Long, RoleDO> roleMap = convertMap(roles, RoleDO::getId);
|
||||
// 校验
|
||||
ids.forEach(id -> {
|
||||
RoleDO role = roleMap.get(id);
|
||||
|
@ -81,7 +81,7 @@ public interface SensitiveWordService {
|
||||
*
|
||||
* @return 标签数组
|
||||
*/
|
||||
Set<String> getSensitiveWordTags();
|
||||
Set<String> getSensitiveWordTagSet();
|
||||
|
||||
/**
|
||||
* 获得文本所包含的不合法的敏感词数组
|
||||
|
@ -25,6 +25,7 @@ import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS;
|
||||
|
||||
@ -85,7 +86,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
|
||||
|
||||
private void initSensitiveWordTrie(List<SensitiveWordDO> wordDOs) {
|
||||
// 过滤禁用的敏感词
|
||||
wordDOs = CollectionUtils.filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus()));
|
||||
wordDOs = filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus()));
|
||||
|
||||
// 初始化默认的 defaultSensitiveWordTrie
|
||||
this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName));
|
||||
@ -107,7 +108,8 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
|
||||
@Override
|
||||
public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) {
|
||||
// 校验唯一性
|
||||
checkSensitiveWordNameUnique(null, createReqVO.getName());
|
||||
validateSensitiveWordNameUnique(null, createReqVO.getName());
|
||||
|
||||
// 插入
|
||||
SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO);
|
||||
sensitiveWordMapper.insert(sensitiveWord);
|
||||
@ -119,8 +121,9 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
|
||||
@Override
|
||||
public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) {
|
||||
// 校验唯一性
|
||||
checkSensitiveWordExists(updateReqVO.getId());
|
||||
checkSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName());
|
||||
validateSensitiveWordExists(updateReqVO.getId());
|
||||
validateSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName());
|
||||
|
||||
// 更新
|
||||
SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO);
|
||||
sensitiveWordMapper.updateById(updateObj);
|
||||
@ -131,14 +134,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
|
||||
@Override
|
||||
public void deleteSensitiveWord(Long id) {
|
||||
// 校验存在
|
||||
checkSensitiveWordExists(id);
|
||||
validateSensitiveWordExists(id);
|
||||
// 删除
|
||||
sensitiveWordMapper.deleteById(id);
|
||||
// 发送消息,刷新缓存
|
||||
sensitiveWordProducer.sendSensitiveWordRefreshMessage();
|
||||
}
|
||||
|
||||
private void checkSensitiveWordNameUnique(Long id, String name) {
|
||||
private void validateSensitiveWordNameUnique(Long id, String name) {
|
||||
SensitiveWordDO word = sensitiveWordMapper.selectByName(name);
|
||||
if (word == null) {
|
||||
return;
|
||||
@ -152,7 +155,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkSensitiveWordExists(Long id) {
|
||||
private void validateSensitiveWordExists(Long id) {
|
||||
if (sensitiveWordMapper.selectById(id) == null) {
|
||||
throw exception(SENSITIVE_WORD_NOT_EXISTS);
|
||||
}
|
||||
@ -179,7 +182,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSensitiveWordTags() {
|
||||
public Set<String> getSensitiveWordTagSet() {
|
||||
return sensitiveWordTagsCache;
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,12 @@
|
||||
package cn.iocoder.yudao.module.system.service.sms;
|
||||
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -53,14 +52,6 @@ public interface SmsChannelService {
|
||||
*/
|
||||
SmsChannelDO getSmsChannel(Long id);
|
||||
|
||||
/**
|
||||
* 获得短信渠道列表
|
||||
*
|
||||
* @param ids 编号
|
||||
* @return 短信渠道列表
|
||||
*/
|
||||
List<SmsChannelDO> getSmsChannelList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得所有短信渠道列表
|
||||
*
|
||||
|
@ -15,7 +15,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
@ -69,7 +68,7 @@ public class SmsChannelServiceImpl implements SmsChannelService {
|
||||
@Override
|
||||
public void updateSmsChannel(SmsChannelUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
this.validateSmsChannelExists(updateReqVO.getId());
|
||||
validateSmsChannelExists(updateReqVO.getId());
|
||||
// 更新
|
||||
SmsChannelDO updateObj = SmsChannelConvert.INSTANCE.convert(updateReqVO);
|
||||
smsChannelMapper.updateById(updateObj);
|
||||
@ -80,7 +79,7 @@ public class SmsChannelServiceImpl implements SmsChannelService {
|
||||
@Override
|
||||
public void deleteSmsChannel(Long id) {
|
||||
// 校验存在
|
||||
this.validateSmsChannelExists(id);
|
||||
validateSmsChannelExists(id);
|
||||
// 校验是否有在使用该账号的模版
|
||||
if (smsTemplateService.countByChannelId(id) > 0) {
|
||||
throw exception(SMS_CHANNEL_HAS_CHILDREN);
|
||||
@ -102,11 +101,6 @@ public class SmsChannelServiceImpl implements SmsChannelService {
|
||||
return smsChannelMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SmsChannelDO> getSmsChannelList(Collection<Long> ids) {
|
||||
return smsChannelMapper.selectBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SmsChannelDO> getSmsChannelList() {
|
||||
return smsChannelMapper.selectList();
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cn.iocoder.yudao.module.system.service.sms;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
||||
|
||||
@ -35,6 +35,6 @@ public interface SmsCodeService {
|
||||
*
|
||||
* @param reqDTO 校验请求
|
||||
*/
|
||||
void checkSmsCode(@Valid SmsCodeCheckReqDTO reqDTO);
|
||||
void validateSmsCode(@Valid SmsCodeValidateReqDTO reqDTO);
|
||||
|
||||
}
|
||||
|
@ -3,11 +3,9 @@ package cn.iocoder.yudao.module.system.service.sms;
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeValidateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsCodeDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsCodeMapper;
|
||||
import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum;
|
||||
@ -16,11 +14,11 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomInt;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.isToday;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
@ -58,11 +56,11 @@ public class SmsCodeServiceImpl implements SmsCodeService {
|
||||
if (lastSmsCode != null) {
|
||||
if (LocalDateTimeUtil.between(lastSmsCode.getCreateTime(), LocalDateTime.now()).toMillis()
|
||||
< smsCodeProperties.getSendFrequency().toMillis()) { // 发送过于频繁
|
||||
throw ServiceExceptionUtil.exception(SMS_CODE_SEND_TOO_FAST);
|
||||
throw exception(SMS_CODE_SEND_TOO_FAST);
|
||||
}
|
||||
if (DateUtils.isToday(lastSmsCode.getCreateTime()) && // 必须是今天,才能计算超过当天的上限
|
||||
if (isToday(lastSmsCode.getCreateTime()) && // 必须是今天,才能计算超过当天的上限
|
||||
lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。
|
||||
throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY);
|
||||
throw exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY);
|
||||
}
|
||||
// TODO 芋艿:提升,每个 IP 每天可发送数量
|
||||
// TODO 芋艿:提升,每个 IP 每小时可发送数量
|
||||
@ -71,7 +69,7 @@ public class SmsCodeServiceImpl implements SmsCodeService {
|
||||
// 创建验证码记录
|
||||
String code = String.valueOf(randomInt(smsCodeProperties.getBeginCode(), smsCodeProperties.getEndCode() + 1));
|
||||
SmsCodeDO newSmsCode = SmsCodeDO.builder().mobile(mobile).code(code).scene(scene)
|
||||
.todayIndex(lastSmsCode != null && DateUtils.isToday(lastSmsCode.getCreateTime()) ? lastSmsCode.getTodayIndex() + 1 : 1)
|
||||
.todayIndex(lastSmsCode != null && isToday(lastSmsCode.getCreateTime()) ? lastSmsCode.getTodayIndex() + 1 : 1)
|
||||
.createIp(ip).used(false).build();
|
||||
smsCodeMapper.insert(newSmsCode);
|
||||
return code;
|
||||
@ -80,32 +78,32 @@ public class SmsCodeServiceImpl implements SmsCodeService {
|
||||
@Override
|
||||
public void useSmsCode(SmsCodeUseReqDTO reqDTO) {
|
||||
// 检测验证码是否有效
|
||||
SmsCodeDO lastSmsCode = this.checkSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene());
|
||||
SmsCodeDO lastSmsCode = validateSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene());
|
||||
// 使用验证码
|
||||
smsCodeMapper.updateById(SmsCodeDO.builder().id(lastSmsCode.getId())
|
||||
.used(true).usedTime(LocalDateTime.now()).usedIp(reqDTO.getUsedIp()).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkSmsCode(SmsCodeCheckReqDTO reqDTO) {
|
||||
checkSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene());
|
||||
public void validateSmsCode(SmsCodeValidateReqDTO reqDTO) {
|
||||
validateSmsCode0(reqDTO.getMobile(), reqDTO.getCode(), reqDTO.getScene());
|
||||
}
|
||||
|
||||
public SmsCodeDO checkSmsCode0(String mobile, String code, Integer scene) {
|
||||
private SmsCodeDO validateSmsCode0(String mobile, String code, Integer scene) {
|
||||
// 校验验证码
|
||||
SmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile, code, scene);
|
||||
// 若验证码不存在,抛出异常
|
||||
if (lastSmsCode == null) {
|
||||
throw ServiceExceptionUtil.exception(SMS_CODE_NOT_FOUND);
|
||||
throw exception(SMS_CODE_NOT_FOUND);
|
||||
}
|
||||
// 超过时间
|
||||
if (LocalDateTimeUtil.between(lastSmsCode.getCreateTime(), LocalDateTime.now()).toMillis()
|
||||
>= smsCodeProperties.getExpireTimes().toMillis()) { // 验证码已过期
|
||||
throw ServiceExceptionUtil.exception(SMS_CODE_EXPIRED);
|
||||
throw exception(SMS_CODE_EXPIRED);
|
||||
}
|
||||
// 判断验证码是否已被使用
|
||||
if (Boolean.TRUE.equals(lastSmsCode.getUsed())) {
|
||||
throw ServiceExceptionUtil.exception(SMS_CODE_USED);
|
||||
throw exception(SMS_CODE_USED);
|
||||
}
|
||||
return lastSmsCode;
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ public class SmsSendServiceImpl implements SmsSendService {
|
||||
}
|
||||
}
|
||||
// 执行发送
|
||||
return this.sendSingleSms(mobile, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams);
|
||||
return sendSingleSms(mobile, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -76,21 +76,21 @@ public class SmsSendServiceImpl implements SmsSendService {
|
||||
mobile = memberService.getMemberUserMobile(userId);
|
||||
}
|
||||
// 执行发送
|
||||
return this.sendSingleSms(mobile, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams);
|
||||
return sendSingleSms(mobile, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long sendSingleSms(String mobile, Long userId, Integer userType,
|
||||
String templateCode, Map<String, Object> templateParams) {
|
||||
// 校验短信模板是否合法
|
||||
SmsTemplateDO template = this.checkSmsTemplateValid(templateCode);
|
||||
SmsTemplateDO template = validateSmsTemplate(templateCode);
|
||||
// 校验短信渠道是否合法
|
||||
SmsChannelDO smsChannel = this.checkSmsChannelValid(template.getChannelId());
|
||||
SmsChannelDO smsChannel = validateSmsChannel(template.getChannelId());
|
||||
|
||||
// 校验手机号码是否存在
|
||||
mobile = this.checkMobile(mobile);
|
||||
mobile = validateMobile(mobile);
|
||||
// 构建有序的模板参数。为什么放在这个位置,是提前保证模板参数的正确性,而不是到了插入发送日志
|
||||
List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams);
|
||||
List<KeyValue<String, Object>> newTemplateParams = buildTemplateParams(template, templateParams);
|
||||
|
||||
// 创建发送日志。如果模板被禁用,则不发送短信,只记录日志
|
||||
Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus())
|
||||
@ -108,7 +108,7 @@ public class SmsSendServiceImpl implements SmsSendService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public SmsChannelDO checkSmsChannelValid(Long channelId) {
|
||||
SmsChannelDO validateSmsChannel(Long channelId) {
|
||||
// 获得短信模板。考虑到效率,从缓存中获取
|
||||
SmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId);
|
||||
// 短信模板不存在
|
||||
@ -119,7 +119,7 @@ public class SmsSendServiceImpl implements SmsSendService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public SmsTemplateDO checkSmsTemplateValid(String templateCode) {
|
||||
SmsTemplateDO validateSmsTemplate(String templateCode) {
|
||||
// 获得短信模板。考虑到效率,从缓存中获取
|
||||
SmsTemplateDO template = smsTemplateService.getSmsTemplateByCodeFromCache(templateCode);
|
||||
// 短信模板不存在
|
||||
@ -139,7 +139,7 @@ public class SmsSendServiceImpl implements SmsSendService {
|
||||
* @return 处理后的参数
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public List<KeyValue<String, Object>> buildTemplateParams(SmsTemplateDO template, Map<String, Object> templateParams) {
|
||||
List<KeyValue<String, Object>> buildTemplateParams(SmsTemplateDO template, Map<String, Object> templateParams) {
|
||||
return template.getParams().stream().map(key -> {
|
||||
Object value = templateParams.get(key);
|
||||
if (value == null) {
|
||||
@ -150,7 +150,7 @@ public class SmsSendServiceImpl implements SmsSendService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public String checkMobile(String mobile) {
|
||||
public String validateMobile(String mobile) {
|
||||
if (StrUtil.isEmpty(mobile)) {
|
||||
throw exception(SMS_SEND_MOBILE_NOT_EXISTS);
|
||||
}
|
||||
|
@ -105,11 +105,11 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
|
||||
@Override
|
||||
public Long createSmsTemplate(SmsTemplateCreateReqVO createReqVO) {
|
||||
// 校验短信渠道
|
||||
SmsChannelDO channelDO = checkSmsChannel(createReqVO.getChannelId());
|
||||
SmsChannelDO channelDO = validateSmsChannel(createReqVO.getChannelId());
|
||||
// 校验短信编码是否重复
|
||||
checkSmsTemplateCodeDuplicate(null, createReqVO.getCode());
|
||||
validateSmsTemplateCodeDuplicate(null, createReqVO.getCode());
|
||||
// 校验短信模板
|
||||
checkApiTemplate(createReqVO.getChannelId(), createReqVO.getApiTemplateId());
|
||||
validateApiTemplate(createReqVO.getChannelId(), createReqVO.getApiTemplateId());
|
||||
|
||||
// 插入
|
||||
SmsTemplateDO template = SmsTemplateConvert.INSTANCE.convert(createReqVO);
|
||||
@ -125,13 +125,13 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
|
||||
@Override
|
||||
public void updateSmsTemplate(SmsTemplateUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
this.validateSmsTemplateExists(updateReqVO.getId());
|
||||
validateSmsTemplateExists(updateReqVO.getId());
|
||||
// 校验短信渠道
|
||||
SmsChannelDO channelDO = checkSmsChannel(updateReqVO.getChannelId());
|
||||
SmsChannelDO channelDO = validateSmsChannel(updateReqVO.getChannelId());
|
||||
// 校验短信编码是否重复
|
||||
checkSmsTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode());
|
||||
validateSmsTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode());
|
||||
// 校验短信模板
|
||||
checkApiTemplate(updateReqVO.getChannelId(), updateReqVO.getApiTemplateId());
|
||||
validateApiTemplate(updateReqVO.getChannelId(), updateReqVO.getApiTemplateId());
|
||||
|
||||
// 更新
|
||||
SmsTemplateDO updateObj = SmsTemplateConvert.INSTANCE.convert(updateReqVO);
|
||||
@ -145,7 +145,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
|
||||
@Override
|
||||
public void deleteSmsTemplate(Long id) {
|
||||
// 校验存在
|
||||
this.validateSmsTemplateExists(id);
|
||||
validateSmsTemplateExists(id);
|
||||
// 更新
|
||||
smsTemplateMapper.deleteById(id);
|
||||
// 发送刷新消息
|
||||
@ -184,7 +184,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public SmsChannelDO checkSmsChannel(Long channelId) {
|
||||
public SmsChannelDO validateSmsChannel(Long channelId) {
|
||||
SmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId);
|
||||
if (channelDO == null) {
|
||||
throw exception(SMS_CHANNEL_NOT_EXISTS);
|
||||
@ -196,7 +196,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkSmsTemplateCodeDuplicate(Long id, String code) {
|
||||
public void validateSmsTemplateCodeDuplicate(Long id, String code) {
|
||||
SmsTemplateDO template = smsTemplateMapper.selectByCode(code);
|
||||
if (template == null) {
|
||||
return;
|
||||
@ -217,7 +217,7 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
|
||||
* @param apiTemplateId API 模板编号
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkApiTemplate(Long channelId, String apiTemplateId) {
|
||||
public void validateApiTemplate(Long channelId, String apiTemplateId) {
|
||||
// 获得短信模板
|
||||
SmsClient smsClient = smsClientFactory.getSmsClient(channelId);
|
||||
Assert.notNull(smsClient, String.format("短信客户端(%d) 不存在", channelId));
|
||||
|
@ -64,9 +64,9 @@ public class TenantPackageServiceImpl implements TenantPackageService {
|
||||
@Override
|
||||
public void deleteTenantPackage(Long id) {
|
||||
// 校验存在
|
||||
this.validateTenantPackageExists(id);
|
||||
validateTenantPackageExists(id);
|
||||
// 校验正在使用
|
||||
this.validateTenantUsed(id);
|
||||
validateTenantUsed(id);
|
||||
// 删除
|
||||
tenantPackageMapper.deleteById(id);
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ public interface TenantService {
|
||||
*
|
||||
* @return 租户编号数组
|
||||
*/
|
||||
List<Long> getTenantIds();
|
||||
List<Long> getTenantIdList();
|
||||
|
||||
/**
|
||||
* 校验租户是否合法
|
||||
|
@ -75,7 +75,7 @@ public class TenantServiceImpl implements TenantService {
|
||||
private PermissionService permissionService;
|
||||
|
||||
@Override
|
||||
public List<Long> getTenantIds() {
|
||||
public List<Long> getTenantIdList() {
|
||||
List<TenantDO> tenants = tenantMapper.selectList();
|
||||
return CollectionUtils.convertList(tenants, TenantDO::getId);
|
||||
}
|
||||
@ -138,7 +138,7 @@ public class TenantServiceImpl implements TenantService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateTenant(TenantUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
TenantDO tenant = checkUpdateTenant(updateReqVO.getId());
|
||||
TenantDO tenant = validateUpdateTenant(updateReqVO.getId());
|
||||
// 校验套餐被禁用
|
||||
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId());
|
||||
|
||||
@ -156,7 +156,7 @@ public class TenantServiceImpl implements TenantService {
|
||||
public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {
|
||||
TenantUtils.execute(tenantId, () -> {
|
||||
// 获得所有角色
|
||||
List<RoleDO> roles = roleService.getRoles(null);
|
||||
List<RoleDO> roles = roleService.getRoleListByStatus(null);
|
||||
roles.forEach(role -> Assert.isTrue(tenantId.equals(role.getTenantId()), "角色({}/{}) 租户不匹配",
|
||||
role.getId(), role.getTenantId(), tenantId)); // 兜底校验
|
||||
// 重新分配每个角色的权限
|
||||
@ -179,12 +179,12 @@ public class TenantServiceImpl implements TenantService {
|
||||
@Override
|
||||
public void deleteTenant(Long id) {
|
||||
// 校验存在
|
||||
checkUpdateTenant(id);
|
||||
validateUpdateTenant(id);
|
||||
// 删除
|
||||
tenantMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private TenantDO checkUpdateTenant(Long id) {
|
||||
private TenantDO validateUpdateTenant(Long id) {
|
||||
TenantDO tenant = tenantMapper.selectById(id);
|
||||
if (tenant == null) {
|
||||
throw exception(TENANT_NOT_EXISTS);
|
||||
@ -248,7 +248,7 @@ public class TenantServiceImpl implements TenantService {
|
||||
TenantDO tenant = getTenant(TenantContextHolder.getRequiredTenantId());
|
||||
Set<Long> menuIds;
|
||||
if (isSystemTenant(tenant)) { // 系统租户,菜单是全量的
|
||||
menuIds = CollectionUtils.convertSet(menuService.getMenus(), MenuDO::getId);
|
||||
menuIds = CollectionUtils.convertSet(menuService.getMenuList(), MenuDO::getId);
|
||||
} else {
|
||||
menuIds = tenantPackageService.getTenantPackage(tenant.getPackageId()).getMenuIds();
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ public interface AdminUserService {
|
||||
* @param deptIds 部门数组
|
||||
* @return 用户数组
|
||||
*/
|
||||
List<AdminUserDO> getUsersByDeptIds(Collection<Long> deptIds);
|
||||
List<AdminUserDO> getUserListByDeptIds(Collection<Long> deptIds);
|
||||
|
||||
/**
|
||||
* 获得指定岗位的用户数组
|
||||
@ -135,7 +135,7 @@ public interface AdminUserService {
|
||||
* @param postIds 岗位数组
|
||||
* @return 用户数组
|
||||
*/
|
||||
List<AdminUserDO> getUsersByPostIds(Collection<Long> postIds);
|
||||
List<AdminUserDO> getUserListByPostIds(Collection<Long> postIds);
|
||||
|
||||
/**
|
||||
* 获得用户列表
|
||||
@ -143,7 +143,7 @@ public interface AdminUserService {
|
||||
* @param ids 用户编号数组
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<AdminUserDO> getUsers(Collection<Long> ids);
|
||||
List<AdminUserDO> getUserList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 校验用户们是否有效。如下情况,视为无效:
|
||||
@ -152,7 +152,7 @@ public interface AdminUserService {
|
||||
*
|
||||
* @param ids 用户编号数组
|
||||
*/
|
||||
void validUsers(Set<Long> ids);
|
||||
void validateUserList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得用户 Map
|
||||
@ -164,7 +164,7 @@ public interface AdminUserService {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
return CollectionUtils.convertMap(getUsers(ids), AdminUserDO::getId);
|
||||
return CollectionUtils.convertMap(getUserList(ids), AdminUserDO::getId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -173,7 +173,7 @@ public interface AdminUserService {
|
||||
* @param reqVO 列表请求
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<AdminUserDO> getUsers(UserExportReqVO reqVO);
|
||||
List<AdminUserDO> getUserList(UserExportReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得用户列表,基于昵称模糊匹配
|
||||
@ -181,15 +181,7 @@ public interface AdminUserService {
|
||||
* @param nickname 昵称
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<AdminUserDO> getUsersByNickname(String nickname);
|
||||
|
||||
/**
|
||||
* 获得用户列表,基于用户账号模糊匹配
|
||||
*
|
||||
* @param username 用户账号
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<AdminUserDO> getUsersByUsername(String username);
|
||||
List<AdminUserDO> getUserListByNickname(String nickname);
|
||||
|
||||
/**
|
||||
* 批量导入用户
|
||||
@ -198,7 +190,7 @@ public interface AdminUserService {
|
||||
* @param isUpdateSupport 是否支持更新
|
||||
* @return 导入结果
|
||||
*/
|
||||
UserImportRespVO importUsers(List<UserImportExcelVO> importUsers, boolean isUpdateSupport);
|
||||
UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport);
|
||||
|
||||
/**
|
||||
* 获得指定状态的用户们
|
||||
@ -206,7 +198,7 @@ public interface AdminUserService {
|
||||
* @param status 状态
|
||||
* @return 用户们
|
||||
*/
|
||||
List<AdminUserDO> getUsersByStatus(Integer status);
|
||||
List<AdminUserDO> getUserListByStatus(Integer status);
|
||||
|
||||
/**
|
||||
* 判断密码是否匹配
|
||||
|
@ -8,7 +8,7 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
|
||||
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
|
||||
import cn.iocoder.yudao.module.infra.api.file.FileApi;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO;
|
||||
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.*;
|
||||
|
||||
/**
|
||||
* 后台用户 Service 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service("adminUserService")
|
||||
@ -73,10 +74,6 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Resource
|
||||
private FileApi fileApi;
|
||||
|
||||
@Resource
|
||||
@Lazy // 循环依赖(自己依赖自己),避免报错
|
||||
private AdminUserServiceImpl self;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long createUser(UserCreateReqVO reqVO) {
|
||||
@ -88,7 +85,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
});
|
||||
// 校验正确性
|
||||
self.checkCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
|
||||
validateUserForCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
|
||||
reqVO.getDeptId(), reqVO.getPostIds());
|
||||
// 插入用户
|
||||
AdminUserDO user = UserConvert.INSTANCE.convert(reqVO);
|
||||
@ -107,7 +104,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateUser(UserUpdateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
self.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
|
||||
validateUserForCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
|
||||
reqVO.getDeptId(), reqVO.getPostIds());
|
||||
// 更新用户
|
||||
AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO);
|
||||
@ -141,9 +138,9 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Override
|
||||
public void updateUserProfile(Long id, UserProfileUpdateReqVO reqVO) {
|
||||
// 校验正确性
|
||||
checkUserExists(id);
|
||||
checkEmailUnique(id, reqVO.getEmail());
|
||||
checkMobileUnique(id, reqVO.getMobile());
|
||||
validateUserExists(id);
|
||||
validateEmailUnique(id, reqVO.getEmail());
|
||||
validateMobileUnique(id, reqVO.getMobile());
|
||||
// 执行更新
|
||||
userMapper.updateById(UserConvert.INSTANCE.convert(reqVO).setId(id));
|
||||
}
|
||||
@ -151,7 +148,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Override
|
||||
public void updateUserPassword(Long id, UserProfileUpdatePasswordReqVO reqVO) {
|
||||
// 校验旧密码密码
|
||||
checkOldPassword(id, reqVO.getOldPassword());
|
||||
validateOldPassword(id, reqVO.getOldPassword());
|
||||
// 执行更新
|
||||
AdminUserDO updateObj = new AdminUserDO().setId(id);
|
||||
updateObj.setPassword(encodePassword(reqVO.getNewPassword())); // 加密密码
|
||||
@ -160,7 +157,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
|
||||
@Override
|
||||
public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception {
|
||||
checkUserExists(id);
|
||||
validateUserExists(id);
|
||||
// 存储文件
|
||||
String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile));
|
||||
// 更新路径
|
||||
@ -174,7 +171,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Override
|
||||
public void updateUserPassword(Long id, String password) {
|
||||
// 校验用户存在
|
||||
checkUserExists(id);
|
||||
validateUserExists(id);
|
||||
// 更新密码
|
||||
AdminUserDO updateObj = new AdminUserDO();
|
||||
updateObj.setId(id);
|
||||
@ -185,7 +182,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Override
|
||||
public void updateUserStatus(Long id, Integer status) {
|
||||
// 校验用户存在
|
||||
checkUserExists(id);
|
||||
validateUserExists(id);
|
||||
// 更新状态
|
||||
AdminUserDO updateObj = new AdminUserDO();
|
||||
updateObj.setId(id);
|
||||
@ -197,7 +194,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteUser(Long id) {
|
||||
// 校验用户存在
|
||||
checkUserExists(id);
|
||||
validateUserExists(id);
|
||||
// 删除用户
|
||||
userMapper.deleteById(id);
|
||||
// 删除用户关联数据
|
||||
@ -227,7 +224,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUsersByDeptIds(Collection<Long> deptIds) {
|
||||
public List<AdminUserDO> getUserListByDeptIds(Collection<Long> deptIds) {
|
||||
if (CollUtil.isEmpty(deptIds)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@ -235,7 +232,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUsersByPostIds(Collection<Long> postIds) {
|
||||
public List<AdminUserDO> getUserListByPostIds(Collection<Long> postIds) {
|
||||
if (CollUtil.isEmpty(postIds)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@ -247,7 +244,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUsers(Collection<Long> ids) {
|
||||
public List<AdminUserDO> getUserList(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@ -255,7 +252,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validUsers(Set<Long> ids) {
|
||||
public void validateUserList(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return;
|
||||
}
|
||||
@ -275,20 +272,15 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUsers(UserExportReqVO reqVO) {
|
||||
public List<AdminUserDO> getUserList(UserExportReqVO reqVO) {
|
||||
return userMapper.selectList(reqVO, getDeptCondition(reqVO.getDeptId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUsersByNickname(String nickname) {
|
||||
public List<AdminUserDO> getUserListByNickname(String nickname) {
|
||||
return userMapper.selectListByNickname(nickname);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUsersByUsername(String username) {
|
||||
return userMapper.selectListByUsername(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得部门条件:查询指定部门的子部门编号们,包括自身
|
||||
* @param deptId 部门编号
|
||||
@ -298,31 +290,33 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
if (deptId == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
Set<Long> deptIds = convertSet(deptService.getDeptsByParentIdFromCache(
|
||||
Set<Long> deptIds = convertSet(deptService.getDeptListByParentIdFromCache(
|
||||
deptId, true), DeptDO::getId);
|
||||
deptIds.add(deptId); // 包括自身
|
||||
return deptIds;
|
||||
}
|
||||
|
||||
@DataPermission(enable = false) // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确
|
||||
public void checkCreateOrUpdate(Long id, String username, String mobile, String email,
|
||||
Long deptId, Set<Long> postIds) {
|
||||
// 校验用户存在
|
||||
checkUserExists(id);
|
||||
// 校验用户名唯一
|
||||
checkUsernameUnique(id, username);
|
||||
// 校验手机号唯一
|
||||
checkMobileUnique(id, mobile);
|
||||
// 校验邮箱唯一
|
||||
checkEmailUnique(id, email);
|
||||
// 校验部门处于开启状态
|
||||
deptService.validDepts(CollectionUtils.singleton(deptId));
|
||||
// 校验岗位处于开启状态
|
||||
postService.validPosts(postIds);
|
||||
private void validateUserForCreateOrUpdate(Long id, String username, String mobile, String email,
|
||||
Long deptId, Set<Long> postIds) {
|
||||
// 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确
|
||||
DataPermissionUtils.executeIgnore(() -> {
|
||||
// 校验用户存在
|
||||
validateUserExists(id);
|
||||
// 校验用户名唯一
|
||||
validateUsernameUnique(id, username);
|
||||
// 校验手机号唯一
|
||||
validateMobileUnique(id, mobile);
|
||||
// 校验邮箱唯一
|
||||
validateEmailUnique(id, email);
|
||||
// 校验部门处于开启状态
|
||||
deptService.validateDeptList(CollectionUtils.singleton(deptId));
|
||||
// 校验岗位处于开启状态
|
||||
postService.validatePostList(postIds);
|
||||
});
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkUserExists(Long id) {
|
||||
void validateUserExists(Long id) {
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
@ -333,7 +327,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkUsernameUnique(Long id, String username) {
|
||||
void validateUsernameUnique(Long id, String username) {
|
||||
if (StrUtil.isBlank(username)) {
|
||||
return;
|
||||
}
|
||||
@ -351,7 +345,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkEmailUnique(Long id, String email) {
|
||||
void validateEmailUnique(Long id, String email) {
|
||||
if (StrUtil.isBlank(email)) {
|
||||
return;
|
||||
}
|
||||
@ -369,7 +363,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkMobileUnique(Long id, String mobile) {
|
||||
void validateMobileUnique(Long id, String mobile) {
|
||||
if (StrUtil.isBlank(mobile)) {
|
||||
return;
|
||||
}
|
||||
@ -392,7 +386,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
* @param oldPassword 旧密码
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void checkOldPassword(Long id, String oldPassword) {
|
||||
void validateOldPassword(Long id, String oldPassword) {
|
||||
AdminUserDO user = userMapper.selectById(id);
|
||||
if (user == null) {
|
||||
throw exception(USER_NOT_EXISTS);
|
||||
@ -404,7 +398,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
||||
public UserImportRespVO importUsers(List<UserImportExcelVO> importUsers, boolean isUpdateSupport) {
|
||||
public UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport) {
|
||||
if (CollUtil.isEmpty(importUsers)) {
|
||||
throw exception(USER_IMPORT_LIST_IS_EMPTY);
|
||||
}
|
||||
@ -413,7 +407,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
importUsers.forEach(importUser -> {
|
||||
// 校验,判断是否有不符合的原因
|
||||
try {
|
||||
checkCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(),
|
||||
validateUserForCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(),
|
||||
importUser.getDeptId(), null);
|
||||
} catch (ServiceException ex) {
|
||||
respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage());
|
||||
@ -441,7 +435,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUsersByStatus(Integer status) {
|
||||
public List<AdminUserDO> getUserListByStatus(Integer status) {
|
||||
return userMapper.selectListByStatus(status);
|
||||
}
|
||||
|
||||
|
@ -1,31 +1,43 @@
|
||||
package cn.iocoder.yudao.module.system.service.auth;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
|
||||
import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi;
|
||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
|
||||
import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
|
||||
import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum;
|
||||
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
|
||||
import cn.iocoder.yudao.module.system.service.logger.LoginLogService;
|
||||
import cn.iocoder.yudao.module.system.service.member.MemberService;
|
||||
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
|
||||
import cn.iocoder.yudao.module.system.service.social.SocialUserService;
|
||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
||||
import com.xingyuv.captcha.model.common.ResponseModel;
|
||||
import com.xingyuv.captcha.service.CaptchaService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ -42,17 +54,24 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
@MockBean
|
||||
private LoginLogService loginLogService;
|
||||
@MockBean
|
||||
private SocialUserService socialService;
|
||||
private SocialUserService socialUserService;
|
||||
@MockBean
|
||||
private SmsCodeApi smsCodeApi;
|
||||
@MockBean
|
||||
private OAuth2TokenService oauth2TokenService;
|
||||
@MockBean
|
||||
private MemberService memberService;
|
||||
|
||||
@MockBean
|
||||
private Validator validator;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
// 注入一个 Validator 对象
|
||||
ReflectUtil.setFieldValue(authService, "validator",
|
||||
Validation.buildDefaultValidatorFactory().getValidator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticate_success() {
|
||||
// 准备参数
|
||||
@ -78,7 +97,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
String password = randomString();
|
||||
|
||||
// 调用, 并断言异常
|
||||
AssertUtils.assertServiceException(() -> authService.authenticate(username, password),
|
||||
assertServiceException(() -> authService.authenticate(username, password),
|
||||
AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
@ -98,7 +117,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
when(userService.getUserByUsername(eq(username))).thenReturn(user);
|
||||
|
||||
// 调用, 并断言异常
|
||||
AssertUtils.assertServiceException(() -> authService.authenticate(username, password),
|
||||
assertServiceException(() -> authService.authenticate(username, password),
|
||||
AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
@ -120,7 +139,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
when(userService.isPasswordMatch(eq(password), eq(user.getPassword()))).thenReturn(true);
|
||||
|
||||
// 调用, 并断言异常
|
||||
AssertUtils.assertServiceException(() -> authService.authenticate(username, password),
|
||||
assertServiceException(() -> authService.authenticate(username, password),
|
||||
AUTH_LOGIN_USER_DISABLED);
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
@ -129,82 +148,194 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testCaptcha_success() {
|
||||
// // 准备参数
|
||||
// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
//
|
||||
// // mock 验证码正确
|
||||
// when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode());
|
||||
//
|
||||
// // 调用
|
||||
// authService.verifyCaptcha(reqVO);
|
||||
// // 断言
|
||||
// verify(captchaService).deleteCaptchaCode(reqVO.getUuid());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testCaptcha_notFound() {
|
||||
// // 准备参数
|
||||
// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
//
|
||||
// // 调用, 并断言异常
|
||||
// assertServiceException(() -> authService.verifyCaptcha(reqVO), AUTH_LOGIN_CAPTCHA_NOT_FOUND);
|
||||
// // 校验调用参数
|
||||
// verify(loginLogService, times(1)).createLoginLog(
|
||||
// argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
// && o.getResult().equals(LoginResultEnum.CAPTCHA_NOT_FOUND.getResult()))
|
||||
// );
|
||||
// }
|
||||
@Test
|
||||
public void testLogin_success() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class, o ->
|
||||
o.setUsername("test_username").setPassword("test_password")
|
||||
.setSocialType(randomEle(SocialTypeEnum.values()).getType()));
|
||||
|
||||
// @Test
|
||||
// public void testCaptcha_codeError() {
|
||||
// // 准备参数
|
||||
// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
//
|
||||
// // mock 验证码不正确
|
||||
// String code = randomString();
|
||||
// when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(code);
|
||||
//
|
||||
// // 调用, 并断言异常
|
||||
// assertServiceException(() -> authService.verifyCaptcha(reqVO), AUTH_LOGIN_CAPTCHA_CODE_ERROR);
|
||||
// // 校验调用参数
|
||||
// verify(loginLogService).createLoginLog(
|
||||
// argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
// && o.getResult().equals(LoginResultEnum.CAPTCHA_CODE_ERROR.getResult()))
|
||||
// );
|
||||
// }
|
||||
// mock 验证码正确
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", false);
|
||||
// mock user 数据
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username")
|
||||
.setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
when(userService.getUserByUsername(eq("test_username"))).thenReturn(user);
|
||||
// mock password 匹配
|
||||
when(userService.isPasswordMatch(eq("test_password"), eq(user.getPassword()))).thenReturn(true);
|
||||
// mock 缓存登录用户到 Redis
|
||||
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L)
|
||||
.setUserType(UserTypeEnum.ADMIN.getValue()));
|
||||
when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull()))
|
||||
.thenReturn(accessTokenDO);
|
||||
|
||||
// @Test
|
||||
// public void testLogin_success() {
|
||||
// // 准备参数
|
||||
// AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class, o ->
|
||||
// o.setUsername("test_username").setPassword("test_password"));
|
||||
//
|
||||
// // mock 验证码正确
|
||||
// when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode());
|
||||
// // mock user 数据
|
||||
// AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username")
|
||||
// .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
// when(userService.getUserByUsername(eq("test_username"))).thenReturn(user);
|
||||
// // mock password 匹配
|
||||
// when(userService.isPasswordMatch(eq("test_password"), eq(user.getPassword()))).thenReturn(true);
|
||||
// // mock 缓存登录用户到 Redis
|
||||
// OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L)
|
||||
// .setUserType(UserTypeEnum.ADMIN.getValue()));
|
||||
// when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull()))
|
||||
// .thenReturn(accessTokenDO);
|
||||
//
|
||||
// // 调用, 并断言异常
|
||||
// AuthLoginRespVO loginRespVO = authService.login(reqVO);
|
||||
// assertPojoEquals(accessTokenDO, loginRespVO);
|
||||
// // 校验调用参数
|
||||
// verify(loginLogService).createLoginLog(
|
||||
// argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
// && o.getResult().equals(LoginResultEnum.SUCCESS.getResult())
|
||||
// && o.getUserId().equals(user.getId()))
|
||||
// );
|
||||
// }
|
||||
// 调用,并校验
|
||||
AuthLoginRespVO loginRespVO = authService.login(reqVO);
|
||||
assertPojoEquals(accessTokenDO, loginRespVO);
|
||||
// 校验调用参数
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.SUCCESS.getResult())
|
||||
&& o.getUserId().equals(user.getId()))
|
||||
);
|
||||
verify(socialUserService).bindSocialUser(eq(new SocialUserBindReqDTO(
|
||||
user.getId(), UserTypeEnum.ADMIN.getValue(),
|
||||
reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendSmsCode() {
|
||||
// 准备参数
|
||||
String mobile = randomString();
|
||||
Integer scene = randomEle(SmsSceneEnum.values()).getScene();
|
||||
AuthSmsSendReqVO reqVO = new AuthSmsSendReqVO(mobile, scene);
|
||||
// mock 方法(用户信息)
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class);
|
||||
when(userService.getUserByMobile(eq(mobile))).thenReturn(user);
|
||||
|
||||
// 调用
|
||||
authService.sendSmsCode(reqVO);
|
||||
// 断言
|
||||
verify(smsCodeApi).sendSmsCode(argThat(sendReqDTO -> {
|
||||
assertEquals(mobile, sendReqDTO.getMobile());
|
||||
assertEquals(scene, sendReqDTO.getScene());
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSmsLogin_success() {
|
||||
// 准备参数
|
||||
String mobile = randomString();
|
||||
String scene = randomString();
|
||||
AuthSmsLoginReqVO reqVO = new AuthSmsLoginReqVO(mobile, scene);
|
||||
// mock 方法(用户信息)
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L));
|
||||
when(userService.getUserByMobile(eq(mobile))).thenReturn(user);
|
||||
// mock 缓存登录用户到 Redis
|
||||
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L)
|
||||
.setUserType(UserTypeEnum.ADMIN.getValue()));
|
||||
when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull()))
|
||||
.thenReturn(accessTokenDO);
|
||||
|
||||
// 调用,并断言
|
||||
AuthLoginRespVO loginRespVO = authService.smsLogin(reqVO);
|
||||
assertPojoEquals(accessTokenDO, loginRespVO);
|
||||
// 断言调用
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_MOBILE.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.SUCCESS.getResult())
|
||||
&& o.getUserId().equals(user.getId()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSocialLogin_success() {
|
||||
// 准备参数
|
||||
AuthSocialLoginReqVO reqVO = randomPojo(AuthSocialLoginReqVO.class);
|
||||
// mock 方法(绑定的用户编号)
|
||||
Long userId = 1L;
|
||||
when(socialUserService.getBindUserId(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()),
|
||||
eq(reqVO.getCode()), eq(reqVO.getState()))).thenReturn(userId);
|
||||
// mock(用户)
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(userId));
|
||||
when(userService.getUser(eq(userId))).thenReturn(user);
|
||||
// mock 缓存登录用户到 Redis
|
||||
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L)
|
||||
.setUserType(UserTypeEnum.ADMIN.getValue()));
|
||||
when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq("default"), isNull()))
|
||||
.thenReturn(accessTokenDO);
|
||||
|
||||
// 调用,并断言
|
||||
AuthLoginRespVO loginRespVO = authService.socialLogin(reqVO);
|
||||
assertPojoEquals(accessTokenDO, loginRespVO);
|
||||
// 断言调用
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_SOCIAL.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.SUCCESS.getResult())
|
||||
&& o.getUserId().equals(user.getId()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCaptcha_successWithEnable() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
|
||||
// mock 验证码打开
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
// mock 验证通过
|
||||
when(captchaService.verification(argThat(captchaVO -> {
|
||||
assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());
|
||||
return true;
|
||||
}))).thenReturn(ResponseModel.success());
|
||||
|
||||
// 调用,无需断言
|
||||
authService.validateCaptcha(reqVO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCaptcha_successWithDisable() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
|
||||
// mock 验证码关闭
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", false);
|
||||
|
||||
// 调用,无需断言
|
||||
authService.validateCaptcha(reqVO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCaptcha_constraintViolationException() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class).setCaptchaVerification(null);
|
||||
|
||||
// mock 验证码打开
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
|
||||
// 调用,并断言异常
|
||||
assertThrows(ConstraintViolationException.class, () -> authService.validateCaptcha(reqVO),
|
||||
"验证码不能为空");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCaptcha_fail() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
|
||||
// mock 验证码打开
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
// mock 验证通过
|
||||
when(captchaService.verification(argThat(captchaVO -> {
|
||||
assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());
|
||||
return true;
|
||||
}))).thenReturn(ResponseModel.errorMsg("就是不对"));
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> authService.validateCaptcha(reqVO), AUTH_LOGIN_CAPTCHA_CODE_ERROR, "就是不对");
|
||||
// 校验调用参数
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.CAPTCHA_CODE_ERROR.getResult()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshToken() {
|
||||
// 准备参数
|
||||
String refreshToken = randomString();
|
||||
// mock 方法
|
||||
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class);
|
||||
when(oauth2TokenService.refreshAccessToken(eq(refreshToken), eq("default")))
|
||||
.thenReturn(accessTokenDO);
|
||||
|
||||
// 调用
|
||||
AuthLoginRespVO loginRespVO = authService.refreshToken(refreshToken);
|
||||
// 断言
|
||||
assertPojoEquals(accessTokenDO, loginRespVO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogout_success() {
|
||||
@ -221,6 +352,8 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
verify(loginLogService).createLoginLog(argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGOUT_SELF.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.SUCCESS.getResult()))
|
||||
);
|
||||
// 调用,并校验
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -19,6 +19,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
@ -28,8 +29,8 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
@ -38,7 +39,7 @@ import static org.mockito.Mockito.verify;
|
||||
* @author niudehua
|
||||
*/
|
||||
@Import(DeptServiceImpl.class)
|
||||
public class DeptServiceTest extends BaseDbUnitTest {
|
||||
public class DeptServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private DeptServiceImpl deptService;
|
||||
@ -76,7 +77,7 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testListDepts() {
|
||||
public void testListDepts() {
|
||||
// mock 数据
|
||||
DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到
|
||||
o.setName("开发部");
|
||||
@ -91,21 +92,22 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
||||
DeptListReqVO reqVO = new DeptListReqVO();
|
||||
reqVO.setName("开");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
|
||||
// 调用
|
||||
List<DeptDO> sysDeptDOS = deptService.getSimpleDepts(reqVO);
|
||||
List<DeptDO> sysDeptDOS = deptService.getDeptList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, sysDeptDOS.size());
|
||||
assertPojoEquals(dept, sysDeptDOS.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateDept_success() {
|
||||
public void testCreateDept_success() {
|
||||
// 准备参数
|
||||
DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
|
||||
o -> {
|
||||
o.setParentId(DeptIdEnum.ROOT.getId());
|
||||
o.setStatus(randomCommonStatus());
|
||||
});
|
||||
DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> {
|
||||
o.setParentId(DeptIdEnum.ROOT.getId());
|
||||
o.setStatus(randomCommonStatus());
|
||||
});
|
||||
|
||||
// 调用
|
||||
Long deptId = deptService.createDept(reqVO);
|
||||
// 断言
|
||||
@ -114,11 +116,11 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
||||
DeptDO deptDO = deptMapper.selectById(deptId);
|
||||
assertPojoEquals(reqVO, deptDO);
|
||||
// 校验调用
|
||||
verify(deptProducer, times(1)).sendDeptRefreshMessage();
|
||||
verify(deptProducer).sendDeptRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateDept_success() {
|
||||
public void testUpdateDept_success() {
|
||||
// mock 数据
|
||||
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
||||
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
|
||||
@ -129,28 +131,34 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
||||
o.setId(dbDeptDO.getId());
|
||||
o.setStatus(randomCommonStatus());
|
||||
});
|
||||
|
||||
// 调用
|
||||
deptService.updateDept(reqVO);
|
||||
// 校验是否更新正确
|
||||
DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, deptDO);
|
||||
// 校验调用
|
||||
verify(deptProducer).sendDeptRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteDept_success() {
|
||||
public void testDeleteDept_success() {
|
||||
// mock 数据
|
||||
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
||||
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbDeptDO.getId();
|
||||
|
||||
// 调用
|
||||
deptService.deleteDept(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(deptMapper.selectById(id));
|
||||
// 校验调用
|
||||
verify(deptProducer).sendDeptRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDept_nameDuplicateForUpdate() {
|
||||
public void testValidateDept_nameDuplicateForUpdate() {
|
||||
// mock 数据
|
||||
DeptDO deptDO = randomDeptDO();
|
||||
// 设置根节点部门
|
||||
@ -162,37 +170,40 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
||||
nameDeptDO.setParentId(DeptIdEnum.ROOT.getId());
|
||||
deptMapper.insert(nameDeptDO);
|
||||
// 准备参数
|
||||
DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
|
||||
o -> {
|
||||
// 设置根节点部门
|
||||
o.setParentId(DeptIdEnum.ROOT.getId());
|
||||
// 设置更新的 ID
|
||||
o.setId(deptDO.getId());
|
||||
// 模拟 name 重复
|
||||
o.setName(nameDeptDO.getName());
|
||||
});
|
||||
DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
|
||||
// 设置根节点部门
|
||||
o.setParentId(DeptIdEnum.ROOT.getId());
|
||||
// 设置更新的 ID
|
||||
o.setId(deptDO.getId());
|
||||
// 模拟 name 重复
|
||||
o.setName(nameDeptDO.getName());
|
||||
});
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deptService.updateDept(reqVO), DEPT_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDept_parentNotExitsForCreate() {
|
||||
public void testValidateDept_parentNotExitsForCreate() {
|
||||
// 准备参数
|
||||
DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
|
||||
o -> o.setStatus(randomCommonStatus()));
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> deptService.createDept(reqVO), DEPT_PARENT_NOT_EXITS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDept_notFoundForDelete() {
|
||||
public void testValidateDept_notFoundForDelete() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deptService.deleteDept(id), DEPT_NOT_FOUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDept_exitsChildrenForDelete() {
|
||||
public void testValidateDept_exitsChildrenForDelete() {
|
||||
// mock 数据
|
||||
DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
||||
deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据
|
||||
@ -208,39 +219,39 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDept_parentErrorForUpdate() {
|
||||
public void testValidateDept_parentErrorForUpdate() {
|
||||
// mock 数据
|
||||
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
||||
deptMapper.insert(dbDeptDO);
|
||||
// 准备参数
|
||||
DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
|
||||
o -> {
|
||||
// 设置自己为父部门
|
||||
o.setParentId(dbDeptDO.getId());
|
||||
// 设置更新的 ID
|
||||
o.setId(dbDeptDO.getId());
|
||||
});
|
||||
DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
|
||||
// 设置自己为父部门
|
||||
o.setParentId(dbDeptDO.getId());
|
||||
// 设置更新的 ID
|
||||
o.setId(dbDeptDO.getId());
|
||||
});
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDept_notEnableForCreate() {
|
||||
public void testValidateDept_notEnableForCreate() {
|
||||
// mock 数据
|
||||
DeptDO deptDO = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
|
||||
deptMapper.insert(deptDO);
|
||||
// 准备参数
|
||||
DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
|
||||
o -> {
|
||||
// 设置未启用的部门为副部门
|
||||
o.setParentId(deptDO.getId());
|
||||
});
|
||||
DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> {
|
||||
// 设置未启用的部门为父部门
|
||||
o.setParentId(deptDO.getId());
|
||||
});
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deptService.createDept(reqVO), DEPT_NOT_ENABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDept_parentIsChildForUpdate() {
|
||||
public void testCheckDept_parentIsChildForUpdate() {
|
||||
// mock 数据
|
||||
DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
deptMapper.insert(parentDept);
|
||||
@ -251,18 +262,84 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
||||
deptMapper.insert(childDept);
|
||||
// 初始化本地缓存
|
||||
deptService.initLocalCache();
|
||||
|
||||
// 准备参数
|
||||
DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
|
||||
o -> {
|
||||
// 设置自己的子部门为父部门
|
||||
o.setParentId(childDept.getId());
|
||||
// 设置更新的 ID
|
||||
o.setId(parentDept.getId());
|
||||
});
|
||||
DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
|
||||
// 设置自己的子部门为父部门
|
||||
o.setParentId(childDept.getId());
|
||||
// 设置更新的 ID
|
||||
o.setId(parentDept.getId());
|
||||
});
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_IS_CHILD);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDeptList() {
|
||||
// mock 数据
|
||||
DeptDO deptDO01 = randomDeptDO();
|
||||
deptMapper.insert(deptDO01);
|
||||
DeptDO deptDO02 = randomDeptDO();
|
||||
deptMapper.insert(deptDO02);
|
||||
// 准备参数
|
||||
List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId());
|
||||
|
||||
// 调用
|
||||
List<DeptDO> deptDOList = deptService.getDeptList(ids);
|
||||
// 断言
|
||||
assertEquals(2, deptDOList.size());
|
||||
assertEquals(deptDO01, deptDOList.get(0));
|
||||
assertEquals(deptDO02, deptDOList.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDept() {
|
||||
// mock 数据
|
||||
DeptDO deptDO = randomDeptDO();
|
||||
deptMapper.insert(deptDO);
|
||||
// 准备参数
|
||||
Long id = deptDO.getId();
|
||||
|
||||
// 调用
|
||||
DeptDO dbDept = deptService.getDept(id);
|
||||
// 断言
|
||||
assertEquals(deptDO, dbDept);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateDeptList_success() {
|
||||
// mock 数据
|
||||
DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
deptMapper.insert(deptDO);
|
||||
// 准备参数
|
||||
List<Long> ids = singletonList(deptDO.getId());
|
||||
|
||||
// 调用,无需断言
|
||||
deptService.validateDeptList(ids);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateDeptList_notFound() {
|
||||
// 准备参数
|
||||
List<Long> ids = singletonList(randomLongId());
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_FOUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateDeptList_notEnable() {
|
||||
// mock 数据
|
||||
DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
|
||||
deptMapper.insert(deptDO);
|
||||
// 准备参数
|
||||
List<Long> ids = singletonList(deptDO.getId());
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE, deptDO.getName());
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static DeptDO randomDeptDO(Consumer<DeptDO>... consumers) {
|
||||
Consumer<DeptDO> consumer = (o) -> {
|
@ -1,41 +1,136 @@
|
||||
package cn.iocoder.yudao.module.system.service.dept;
|
||||
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* {@link PostServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author niudehua
|
||||
*/
|
||||
@Import(PostServiceImpl.class)
|
||||
public class PostServiceTest extends BaseDbUnitTest {
|
||||
public class PostServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private PostServiceImpl postService;
|
||||
|
||||
@Resource
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Test
|
||||
void testPagePosts() {
|
||||
public void testCreatePost_success() {
|
||||
// 准备参数
|
||||
PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,
|
||||
o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()));
|
||||
// 调用
|
||||
Long postId = postService.createPost(reqVO);
|
||||
|
||||
// 断言
|
||||
assertNotNull(postId);
|
||||
// 校验记录的属性是否正确
|
||||
PostDO post = postMapper.selectById(postId);
|
||||
assertPojoEquals(reqVO, post);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePost_success() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> {
|
||||
// 设置更新的 ID
|
||||
o.setId(postDO.getId());
|
||||
o.setStatus(randomEle(CommonStatusEnum.values()).getStatus());
|
||||
});
|
||||
|
||||
// 调用
|
||||
postService.updatePost(reqVO);
|
||||
// 校验是否更新正确
|
||||
PostDO post = postMapper.selectById(reqVO.getId());
|
||||
assertPojoEquals(reqVO, post);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePost_success() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
postMapper.insert(postDO);
|
||||
// 准备参数
|
||||
Long id = postDO.getId();
|
||||
|
||||
// 调用
|
||||
postService.deletePost(id);
|
||||
assertNull(postMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidatePost_notFoundForDelete() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> postService.deletePost(id), POST_NOT_FOUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidatePost_nameDuplicateForCreate() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,
|
||||
// 模拟 name 重复
|
||||
o -> o.setName(postDO.getName()));
|
||||
assertServiceException(() -> postService.createPost(reqVO), POST_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidatePost_codeDuplicateForUpdate() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
postMapper.insert(postDO);
|
||||
// mock 数据:稍后模拟重复它的 code
|
||||
PostDO codePostDO = randomPostDO();
|
||||
postMapper.insert(codePostDO);
|
||||
// 准备参数
|
||||
PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> {
|
||||
// 设置更新的 ID
|
||||
o.setId(postDO.getId());
|
||||
// 模拟 code 重复
|
||||
o.setCode(codePostDO.getCode());
|
||||
});
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> postService.updatePost(reqVO), POST_CODE_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPostPage() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPojo(PostDO.class, o -> {
|
||||
o.setName("码仔");
|
||||
@ -43,10 +138,9 @@ public class PostServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
postMapper.insert(postDO);
|
||||
// 测试 name 不匹配
|
||||
postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员")));
|
||||
postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员")));
|
||||
// 测试 status 不匹配
|
||||
postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
|
||||
postMapper.insert(cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 准备参数
|
||||
PostPageReqVO reqVO = new PostPageReqVO();
|
||||
reqVO.setName("码");
|
||||
@ -54,7 +148,6 @@ public class PostServiceTest extends BaseDbUnitTest {
|
||||
|
||||
// 调用
|
||||
PageResult<PostDO> pageResult = postService.getPostPage(reqVO);
|
||||
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
@ -62,7 +155,7 @@ public class PostServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testListPosts() {
|
||||
public void testGetPostList_export() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPojo(PostDO.class, o -> {
|
||||
o.setName("码仔");
|
||||
@ -70,23 +163,41 @@ public class PostServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
postMapper.insert(postDO);
|
||||
// 测试 name 不匹配
|
||||
postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员")));
|
||||
postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员")));
|
||||
// 测试 status 不匹配
|
||||
postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
postMapper.insert(cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 准备参数
|
||||
PostExportReqVO reqVO = new PostExportReqVO();
|
||||
reqVO.setName("码");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
|
||||
// 调用
|
||||
List<PostDO> list = postService.getPosts(reqVO);
|
||||
List<PostDO> list = postService.getPostList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(postDO, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPost() {
|
||||
public void testGetPostList() {
|
||||
// mock 数据
|
||||
PostDO postDO01 = randomPojo(PostDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
postMapper.insert(postDO01);
|
||||
// 测试 status 不匹配
|
||||
PostDO postDO02 = randomPojo(PostDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
|
||||
postMapper.insert(postDO02);
|
||||
// 准备参数
|
||||
List<Long> ids = Arrays.asList(postDO01.getId(), postDO02.getId());
|
||||
|
||||
// 调用
|
||||
List<PostDO> list = postService.getPostList(ids, singletonList(CommonStatusEnum.ENABLE.getStatus()));
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(postDO01, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPost() {
|
||||
// mock 数据
|
||||
PostDO dbPostDO = randomPostDO();
|
||||
postMapper.insert(dbPostDO);
|
||||
@ -100,94 +211,43 @@ public class PostServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreatePost_success() {
|
||||
// 准备参数
|
||||
PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,
|
||||
o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()));
|
||||
// 调用
|
||||
Long postId = postService.createPost(reqVO);
|
||||
// 断言
|
||||
assertNotNull(postId);
|
||||
// 校验记录的属性是否正确
|
||||
PostDO post = postMapper.selectById(postId);
|
||||
assertPojoEquals(reqVO, post);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdatePost_success() {
|
||||
public void testValidatePostList_success() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class,
|
||||
o -> {
|
||||
// 设置更新的 ID
|
||||
o.setId(postDO.getId());
|
||||
o.setStatus(randomEle(CommonStatusEnum.values()).getStatus());
|
||||
});
|
||||
// 调用
|
||||
postService.updatePost(reqVO);
|
||||
// 校验是否更新正确
|
||||
PostDO post = postMapper.selectById(reqVO.getId());// 获取最新的
|
||||
assertPojoEquals(reqVO, post);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeletePost_success() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
postMapper.insert(postDO);
|
||||
// 准备参数
|
||||
Long id = postDO.getId();
|
||||
// 调用
|
||||
postService.deletePost(id);
|
||||
assertNull(postMapper.selectById(id));
|
||||
List<Long> ids = singletonList(postDO.getId());
|
||||
|
||||
// 调用,无需断言
|
||||
postService.validatePostList(ids);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckPost_notFoundForDelete() {
|
||||
public void testValidatePostList_notFound() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
List<Long> ids = singletonList(randomLongId());
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> postService.deletePost(id), POST_NOT_FOUND);
|
||||
assertServiceException(() -> postService.validatePostList(ids), POST_NOT_FOUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckPost_nameDuplicateForCreate() {
|
||||
public void testValidatePostList_notEnable() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
PostCreateReqVO reqVO = randomPojo(PostCreateReqVO.class,
|
||||
// 模拟 name 重复
|
||||
o -> o.setName(postDO.getName()));
|
||||
assertServiceException(() -> postService.createPost(reqVO), POST_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckPost_codeDuplicateForUpdate() {
|
||||
// mock 数据
|
||||
PostDO postDO = randomPostDO();
|
||||
PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
|
||||
postMapper.insert(postDO);
|
||||
// mock 数据 稍后模拟重复它的 code
|
||||
PostDO codePostDO = randomPostDO();
|
||||
postMapper.insert(codePostDO);
|
||||
// 准备参数
|
||||
PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class,
|
||||
o -> {
|
||||
// 设置更新的 ID
|
||||
o.setId(postDO.getId());
|
||||
// 模拟 code 重复
|
||||
o.setCode(codePostDO.getCode());
|
||||
});
|
||||
List<Long> ids = singletonList(postDO.getId());
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> postService.updatePost(reqVO), POST_CODE_DUPLICATE);
|
||||
assertServiceException(() -> postService.validatePostList(ids), POST_NOT_ENABLE,
|
||||
postDO.getName());
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static PostDO randomPostDO(Consumer<PostDO>... consumers) {
|
||||
Consumer<PostDO> consumer = (o) -> {
|
||||
o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围
|
||||
o.setStatus(randomCommonStatus()); // 保证 status 的范围
|
||||
};
|
||||
return randomPojo(PostDO.class, ArrayUtils.append(consumer, consumers));
|
||||
}
|
@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.system.service.dict;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExportReqVO;
|
||||
@ -20,16 +19,18 @@ import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@Import(DictDataServiceImpl.class)
|
||||
public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
public class DictDataServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private DictDataServiceImpl dictDataService;
|
||||
@ -39,6 +40,23 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
@MockBean
|
||||
private DictTypeService dictTypeService;
|
||||
|
||||
@Test
|
||||
public void testGetDictDataList() {
|
||||
// mock 数据
|
||||
DictDataDO dictDataDO01 = randomDictDataDO().setDictType("yunai").setSort(2);
|
||||
dictDataMapper.insert(dictDataDO01);
|
||||
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setSort(1);
|
||||
dictDataMapper.insert(dictDataDO02);
|
||||
// 准备参数
|
||||
|
||||
// 调用
|
||||
List<DictDataDO> dictDataDOList = dictDataService.getDictDataList();
|
||||
// 断言
|
||||
assertEquals(2, dictDataDOList.size());
|
||||
assertPojoEquals(dictDataDO02, dictDataDOList.get(0));
|
||||
assertPojoEquals(dictDataDO01, dictDataDOList.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDictDataPage() {
|
||||
// mock 数据
|
||||
@ -49,11 +67,11 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
dictDataMapper.insert(dbDictData);
|
||||
// 测试 label 不匹配
|
||||
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("艿")));
|
||||
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setLabel("艿")));
|
||||
// 测试 dictType 不匹配
|
||||
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
|
||||
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
|
||||
// 测试 status 不匹配
|
||||
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 准备参数
|
||||
DictDataPageReqVO reqVO = new DictDataPageReqVO();
|
||||
reqVO.setLabel("芋");
|
||||
@ -69,7 +87,7 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDictDataList() {
|
||||
public void testGetDictDataList_export() {
|
||||
// mock 数据
|
||||
DictDataDO dbDictData = randomPojo(DictDataDO.class, o -> { // 等会查询到
|
||||
o.setLabel("芋艿");
|
||||
@ -78,11 +96,11 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
dictDataMapper.insert(dbDictData);
|
||||
// 测试 label 不匹配
|
||||
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("艿")));
|
||||
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setLabel("艿")));
|
||||
// 测试 dictType 不匹配
|
||||
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
|
||||
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
|
||||
// 测试 status 不匹配
|
||||
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 准备参数
|
||||
DictDataExportReqVO reqVO = new DictDataExportReqVO();
|
||||
reqVO.setLabel("芋");
|
||||
@ -90,12 +108,26 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
|
||||
// 调用
|
||||
List<DictDataDO> list = dictDataService.getDictDatas(reqVO);
|
||||
List<DictDataDO> list = dictDataService.getDictDataList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbDictData, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDictData() {
|
||||
// mock 数据
|
||||
DictDataDO dbDictData = randomDictDataDO();
|
||||
dictDataMapper.insert(dbDictData);
|
||||
// 准备参数
|
||||
Long id = dbDictData.getId();
|
||||
|
||||
// 调用
|
||||
DictDataDO dictData = dictDataService.getDictData(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbDictData, dictData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateDictData_success() {
|
||||
// 准备参数
|
||||
@ -148,54 +180,54 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataExists_success() {
|
||||
public void testValidateDictDataExists_success() {
|
||||
// mock 数据
|
||||
DictDataDO dbDictData = randomDictDataDO();
|
||||
dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据
|
||||
|
||||
// 调用成功
|
||||
dictDataService.checkDictDataExists(dbDictData.getId());
|
||||
dictDataService.validateDictDataExists(dbDictData.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataExists_notExists() {
|
||||
assertServiceException(() -> dictDataService.checkDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS);
|
||||
public void testValidateDictDataExists_notExists() {
|
||||
assertServiceException(() -> dictDataService.validateDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeValid_success() {
|
||||
public void testValidateDictTypeExists_success() {
|
||||
// mock 方法,数据类型被禁用
|
||||
String type = randomString();
|
||||
when(dictTypeService.getDictType(eq(type))).thenReturn(randomDictTypeDO(type));
|
||||
|
||||
// 调用, 成功
|
||||
dictDataService.checkDictTypeValid(type);
|
||||
dictDataService.validateDictTypeExists(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeValid_notExists() {
|
||||
assertServiceException(() -> dictDataService.checkDictTypeValid(randomString()), DICT_TYPE_NOT_EXISTS);
|
||||
public void testValidateDictTypeExists_notExists() {
|
||||
assertServiceException(() -> dictDataService.validateDictTypeExists(randomString()), DICT_TYPE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeValid_notEnable() {
|
||||
public void testValidateDictTypeExists_notEnable() {
|
||||
// mock 方法,数据类型被禁用
|
||||
String dictType = randomString();
|
||||
when(dictTypeService.getDictType(eq(dictType))).thenReturn(
|
||||
randomPojo(DictTypeDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> dictDataService.checkDictTypeValid(dictType), DICT_TYPE_NOT_ENABLE);
|
||||
assertServiceException(() -> dictDataService.validateDictTypeExists(dictType), DICT_TYPE_NOT_ENABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataValueUnique_success() {
|
||||
public void testValidateDictDataValueUnique_success() {
|
||||
// 调用,成功
|
||||
dictDataService.checkDictDataValueUnique(randomLongId(), randomString(), randomString());
|
||||
dictDataService.validateDictDataValueUnique(randomLongId(), randomString(), randomString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataValueUnique_valueDuplicateForCreate() {
|
||||
public void testValidateDictDataValueUnique_valueDuplicateForCreate() {
|
||||
// 准备参数
|
||||
String dictType = randomString();
|
||||
String value = randomString();
|
||||
@ -206,12 +238,12 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
}));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> dictDataService.checkDictDataValueUnique(null, dictType, value),
|
||||
assertServiceException(() -> dictDataService.validateDictDataValueUnique(null, dictType, value),
|
||||
DICT_DATA_VALUE_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataValueUnique_valueDuplicateForUpdate() {
|
||||
public void testValidateDictDataValueUnique_valueDuplicateForUpdate() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
String dictType = randomString();
|
||||
@ -223,10 +255,96 @@ public class DictDataServiceTest extends BaseDbUnitTest {
|
||||
}));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> dictDataService.checkDictDataValueUnique(id, dictType, value),
|
||||
assertServiceException(() -> dictDataService.validateDictDataValueUnique(id, dictType, value),
|
||||
DICT_DATA_VALUE_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountByDictType() {
|
||||
// mock 数据
|
||||
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai")));
|
||||
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("tudou")));
|
||||
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai")));
|
||||
// 准备参数
|
||||
String dictType = "yunai";
|
||||
|
||||
// 调用
|
||||
long count = dictDataService.countByDictType(dictType);
|
||||
// 校验
|
||||
assertEquals(2L, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateDictDataList_success() {
|
||||
// mock 数据
|
||||
DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
dictDataMapper.insert(dictDataDO);
|
||||
// 准备参数
|
||||
String dictType = dictDataDO.getDictType();
|
||||
List<String> values = singletonList(dictDataDO.getValue());
|
||||
|
||||
// 调用,无需断言
|
||||
dictDataService.validateDictDataList(dictType, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateDictDataList_notFound() {
|
||||
// 准备参数
|
||||
String dictType = randomString();
|
||||
List<String> values = singletonList(randomString());
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> dictDataService.validateDictDataList(dictType, values), DICT_DATA_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateDictDataList_notEnable() {
|
||||
// mock 数据
|
||||
DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
|
||||
dictDataMapper.insert(dictDataDO);
|
||||
// 准备参数
|
||||
String dictType = dictDataDO.getDictType();
|
||||
List<String> values = singletonList(dictDataDO.getValue());
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> dictDataService.validateDictDataList(dictType, values),
|
||||
DICT_DATA_NOT_ENABLE, dictDataDO.getLabel());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDictData_dictType() {
|
||||
// mock 数据
|
||||
DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setValue("1");
|
||||
dictDataMapper.insert(dictDataDO);
|
||||
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setValue("2");
|
||||
dictDataMapper.insert(dictDataDO02);
|
||||
// 准备参数
|
||||
String dictType = "yunai";
|
||||
String value = "1";
|
||||
|
||||
// 调用
|
||||
DictDataDO dbDictData = dictDataService.getDictData(dictType, value);
|
||||
// 断言
|
||||
assertEquals(dictDataDO, dbDictData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseDictData() {
|
||||
// mock 数据
|
||||
DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setLabel("1");
|
||||
dictDataMapper.insert(dictDataDO);
|
||||
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setLabel("2");
|
||||
dictDataMapper.insert(dictDataDO02);
|
||||
// 准备参数
|
||||
String dictType = "yunai";
|
||||
String label = "1";
|
||||
|
||||
// 调用
|
||||
DictDataDO dbDictData = dictDataService.parseDictData(dictType, label);
|
||||
// 断言
|
||||
assertEquals(dictDataDO, dbDictData);
|
||||
}
|
||||
|
||||
// ========== 随机对象 ==========
|
||||
|
||||
@SafeVarargs
|
@ -2,36 +2,36 @@ package cn.iocoder.yudao.module.system.service.dict;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.dict.DictTypeMapper;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@Import(DictTypeServiceImpl.class)
|
||||
public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
public class DictTypeServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private DictTypeServiceImpl dictTypeService;
|
||||
@ -52,19 +52,19 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
dictTypeMapper.insert(dbDictType);
|
||||
// 测试 name 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou")));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setName("tudou")));
|
||||
// 测试 type 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆")));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setType("土豆")));
|
||||
// 测试 status 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 createTime 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
// 准备参数
|
||||
DictTypePageReqVO reqVO = new DictTypePageReqVO();
|
||||
reqVO.setName("nai");
|
||||
reqVO.setType("艿");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 10),buildTime(2021, 1, 20)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20));
|
||||
|
||||
// 调用
|
||||
PageResult<DictTypeDO> pageResult = dictTypeService.getDictTypePage(reqVO);
|
||||
@ -75,7 +75,7 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDictTypeList() {
|
||||
public void testGetDictTypeList_export() {
|
||||
// mock 数据
|
||||
DictTypeDO dbDictType = randomPojo(DictTypeDO.class, o -> { // 等会查询到
|
||||
o.setName("yunai");
|
||||
@ -85,19 +85,19 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
dictTypeMapper.insert(dbDictType);
|
||||
// 测试 name 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou")));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setName("tudou")));
|
||||
// 测试 type 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆")));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setType("土豆")));
|
||||
// 测试 status 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 createTime 不匹配
|
||||
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
||||
// 准备参数
|
||||
DictTypeExportReqVO reqVO = new DictTypeExportReqVO();
|
||||
reqVO.setName("nai");
|
||||
reqVO.setType("艿");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 10),buildTime(2021, 1, 20)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20));
|
||||
|
||||
// 调用
|
||||
List<DictTypeDO> list = dictTypeService.getDictTypeList(reqVO);
|
||||
@ -107,7 +107,22 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDictType() {
|
||||
public void testGetDictType_id() {
|
||||
// mock 数据
|
||||
DictTypeDO dbDictType = randomDictTypeDO();
|
||||
dictTypeMapper.insert(dbDictType);
|
||||
// 准备参数
|
||||
Long id = dbDictType.getId();
|
||||
|
||||
// 调用
|
||||
DictTypeDO dictType = dictTypeService.getDictType(id);
|
||||
// 断言
|
||||
assertNotNull(dictType);
|
||||
assertPojoEquals(dbDictType, dictType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDictType_type() {
|
||||
// mock 数据
|
||||
DictTypeDO dbDictType = randomDictTypeDO();
|
||||
dictTypeMapper.insert(dbDictType);
|
||||
@ -183,40 +198,57 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataExists_success() {
|
||||
public void testGetDictTypeList() {
|
||||
// 准备参数
|
||||
DictTypeDO dictTypeDO01 = randomDictTypeDO();
|
||||
dictTypeMapper.insert(dictTypeDO01);
|
||||
DictTypeDO dictTypeDO02 = randomDictTypeDO();
|
||||
dictTypeMapper.insert(dictTypeDO02);
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
List<DictTypeDO> dictTypeDOList = dictTypeService.getDictTypeList();
|
||||
// 断言
|
||||
assertEquals(2, dictTypeDOList.size());
|
||||
assertPojoEquals(dictTypeDO01, dictTypeDOList.get(0));
|
||||
assertPojoEquals(dictTypeDO02, dictTypeDOList.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateDictDataExists_success() {
|
||||
// mock 数据
|
||||
DictTypeDO dbDictType = randomDictTypeDO();
|
||||
dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据
|
||||
|
||||
// 调用成功
|
||||
dictTypeService.checkDictTypeExists(dbDictType.getId());
|
||||
dictTypeService.validateDictTypeExists(dbDictType.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataExists_notExists() {
|
||||
assertServiceException(() -> dictTypeService.checkDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS);
|
||||
public void testValidateDictDataExists_notExists() {
|
||||
assertServiceException(() -> dictTypeService.validateDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeUnique_success() {
|
||||
public void testValidateDictTypeUnique_success() {
|
||||
// 调用,成功
|
||||
dictTypeService.checkDictTypeUnique(randomLongId(), randomString());
|
||||
dictTypeService.validateDictTypeUnique(randomLongId(), randomString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeUnique_valueDuplicateForCreate() {
|
||||
public void testValidateDictTypeUnique_valueDuplicateForCreate() {
|
||||
// 准备参数
|
||||
String type = randomString();
|
||||
// mock 数据
|
||||
dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> dictTypeService.checkDictTypeUnique(null, type),
|
||||
assertServiceException(() -> dictTypeService.validateDictTypeUnique(null, type),
|
||||
DICT_TYPE_TYPE_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeUnique_valueDuplicateForUpdate() {
|
||||
public void testValidateDictTypeUnique_valueDuplicateForUpdate() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
String type = randomString();
|
||||
@ -224,30 +256,30 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> dictTypeService.checkDictTypeUnique(id, type),
|
||||
assertServiceException(() -> dictTypeService.validateDictTypeUnique(id, type),
|
||||
DICT_TYPE_TYPE_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypNameUnique_success() {
|
||||
public void testValidateDictTypNameUnique_success() {
|
||||
// 调用,成功
|
||||
dictTypeService.checkDictTypeNameUnique(randomLongId(), randomString());
|
||||
dictTypeService.validateDictTypeNameUnique(randomLongId(), randomString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeNameUnique_nameDuplicateForCreate() {
|
||||
public void testValidateDictTypeNameUnique_nameDuplicateForCreate() {
|
||||
// 准备参数
|
||||
String name = randomString();
|
||||
// mock 数据
|
||||
dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(null, name),
|
||||
assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(null, name),
|
||||
DICT_TYPE_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictTypeNameUnique_nameDuplicateForUpdate() {
|
||||
public void testValidateDictTypeNameUnique_nameDuplicateForUpdate() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
String name = randomString();
|
||||
@ -255,7 +287,7 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
|
||||
dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(id, name),
|
||||
assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(id, name),
|
||||
DICT_TYPE_NAME_DUPLICATE);
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ package cn.iocoder.yudao.module.system.service.errorcode;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO;
|
||||
@ -22,7 +22,9 @@ import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
@ -57,7 +59,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testUpdateErrorCode_success() {
|
||||
// mock 数据
|
||||
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO();
|
||||
ErrorCodeDO dbErrorCode = randomErrorCodeDO();
|
||||
errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
ErrorCodeUpdateReqVO reqVO = randomPojo(ErrorCodeUpdateReqVO.class, o -> {
|
||||
@ -75,7 +77,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testDeleteErrorCode_success() {
|
||||
// mock 数据
|
||||
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO();
|
||||
ErrorCodeDO dbErrorCode = randomErrorCodeDO();
|
||||
errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbErrorCode.getId();
|
||||
@ -96,7 +98,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
reqVO.setApplicationName("tu");
|
||||
reqVO.setCode(1);
|
||||
reqVO.setMessage("ma");
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 11, 1),buildTime(2020, 11, 30)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30));
|
||||
|
||||
// 调用
|
||||
PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(reqVO);
|
||||
@ -110,7 +112,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
* 初始化 getErrorCodePage 方法的测试数据
|
||||
*/
|
||||
private ErrorCodeDO initGetErrorCodePage() {
|
||||
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> { // 等会查询到
|
||||
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> { // 等会查询到
|
||||
o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
|
||||
o.setApplicationName("tudou");
|
||||
o.setCode(1);
|
||||
@ -119,20 +121,20 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
});
|
||||
errorCodeMapper.insert(dbErrorCode);
|
||||
// 测试 type 不匹配
|
||||
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())));
|
||||
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())));
|
||||
// 测试 applicationName 不匹配
|
||||
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan")));
|
||||
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan")));
|
||||
// 测试 code 不匹配
|
||||
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCode(2)));
|
||||
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCode(2)));
|
||||
// 测试 message 不匹配
|
||||
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai")));
|
||||
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai")));
|
||||
// 测试 createTime 不匹配
|
||||
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12))));
|
||||
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12))));
|
||||
return dbErrorCode;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetErrorCodeList() {
|
||||
public void testGetErrorCodeList_export() {
|
||||
// mock 数据
|
||||
ErrorCodeDO dbErrorCode = initGetErrorCodePage();
|
||||
// 准备参数
|
||||
@ -141,7 +143,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
reqVO.setApplicationName("tu");
|
||||
reqVO.setCode(1);
|
||||
reqVO.setMessage("ma");
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 11, 1),buildTime(2020, 11, 30)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30));
|
||||
|
||||
// 调用
|
||||
List<ErrorCodeDO> list = errorCodeService.getErrorCodeList(reqVO);
|
||||
@ -155,7 +157,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
// 准备参数
|
||||
Integer code = randomInteger();
|
||||
// mock 数据
|
||||
errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code)));
|
||||
errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null),
|
||||
@ -168,7 +170,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
Long id = randomLongId();
|
||||
Integer code = randomInteger();
|
||||
// mock 数据
|
||||
errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code)));
|
||||
errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code)));
|
||||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id),
|
||||
@ -204,7 +206,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testAutoGenerateErrorCodes_021() {
|
||||
// mock 数据
|
||||
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()));
|
||||
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()));
|
||||
errorCodeMapper.insert(dbErrorCode);
|
||||
// 准备参数
|
||||
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
|
||||
@ -224,7 +226,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testAutoGenerateErrorCodes_022() {
|
||||
// mock 数据
|
||||
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
|
||||
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
|
||||
errorCodeMapper.insert(dbErrorCode);
|
||||
// 准备参数
|
||||
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
|
||||
@ -244,7 +246,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testAutoGenerateErrorCodes_023() {
|
||||
// mock 数据
|
||||
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
|
||||
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
|
||||
errorCodeMapper.insert(dbErrorCode);
|
||||
// 准备参数
|
||||
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
|
||||
@ -265,7 +267,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testAutoGenerateErrorCodes_024() {
|
||||
// mock 数据
|
||||
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
|
||||
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
|
||||
errorCodeMapper.insert(dbErrorCode);
|
||||
// 准备参数
|
||||
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
|
||||
@ -279,10 +281,44 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
|
||||
assertPojoEquals(generateReqDTO, errorCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetErrorCode() {
|
||||
// 准备参数
|
||||
ErrorCodeDO errorCodeDO = randomErrorCodeDO();
|
||||
errorCodeMapper.insert(errorCodeDO);
|
||||
// mock 方法
|
||||
Long id = errorCodeDO.getId();
|
||||
|
||||
// 调用
|
||||
ErrorCodeDO dbErrorCode = errorCodeService.getErrorCode(id);
|
||||
// 断言
|
||||
assertPojoEquals(errorCodeDO, dbErrorCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetErrorCodeList() {
|
||||
// 准备参数
|
||||
ErrorCodeDO errorCodeDO01 = randomErrorCodeDO(
|
||||
o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 10)));
|
||||
errorCodeMapper.insert(errorCodeDO01);
|
||||
ErrorCodeDO errorCodeDO02 = randomErrorCodeDO(
|
||||
o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 12)));
|
||||
errorCodeMapper.insert(errorCodeDO02);
|
||||
// mock 方法
|
||||
String applicationName = "yunai_server";
|
||||
LocalDateTime minUpdateTime = buildTime(2022, 1, 11);
|
||||
|
||||
// 调用
|
||||
List<ErrorCodeRespDTO> errorCodeList = errorCodeService.getErrorCodeList(applicationName, minUpdateTime);
|
||||
// 断言
|
||||
assertEquals(1, errorCodeList.size());
|
||||
assertPojoEquals(errorCodeDO02, errorCodeList.get(0));
|
||||
}
|
||||
|
||||
// ========== 随机对象 ==========
|
||||
|
||||
@SafeVarargs
|
||||
private static ErrorCodeDO randomInfErrorCodeDO(Consumer<ErrorCodeDO>... consumers) {
|
||||
private static ErrorCodeDO randomErrorCodeDO(Consumer<ErrorCodeDO>... consumers) {
|
||||
Consumer<ErrorCodeDO> consumer = (o) -> {
|
||||
o.setType(randomEle(ErrorCodeTypeEnum.values()).getType()); // 保证 key 的范围
|
||||
};
|
||||
|
@ -1,29 +1,25 @@
|
||||
package cn.iocoder.yudao.module.system.service.logger;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogExportReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.loginlog.LoginLogPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.logger.LoginLogDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.logger.LoginLogMapper;
|
||||
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.CAPTCHA_CODE_ERROR;
|
||||
import static cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum.SUCCESS;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@Import(LoginLogServiceImpl.class)
|
||||
@ -37,42 +33,31 @@ public class LoginLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Test
|
||||
public void testGetLoginLogPage() {
|
||||
// 构造测试数据
|
||||
// 登录成功的
|
||||
LoginLogDO loginLogDO = RandomUtils.randomPojo(LoginLogDO.class, logDO -> {
|
||||
logDO.setLogType(RandomUtil.randomEle(cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum.values()).getType());
|
||||
logDO.setTraceId(TracerUtils.getTraceId());
|
||||
logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue());
|
||||
|
||||
logDO.setUserIp("192.168.199.16");
|
||||
logDO.setUsername("wangkai");
|
||||
logDO.setCreateTime(buildTime(2021, 3, 6));
|
||||
logDO.setResult(SUCCESS.getResult());
|
||||
// mock 数据
|
||||
LoginLogDO loginLogDO = randomPojo(LoginLogDO.class, o -> {
|
||||
o.setUserIp("192.168.199.16");
|
||||
o.setUsername("wang");
|
||||
o.setResult(SUCCESS.getResult());
|
||||
o.setCreateTime(buildTime(2021, 3, 6));
|
||||
});
|
||||
loginLogMapper.insert(loginLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// 登录失败的
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(CAPTCHA_CODE_ERROR.getResult())));
|
||||
// 不同ip段的
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18")));
|
||||
// 不同username
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6))));
|
||||
|
||||
|
||||
// 测试 status 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setResult(CAPTCHA_CODE_ERROR.getResult())));
|
||||
// 测试 ip 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUserIp("192.168.128.18")));
|
||||
// 测试 username 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUsername("yunai")));
|
||||
// 测试 createTime 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setCreateTime(buildTime(2021, 2, 6))));
|
||||
// 构造调用参数
|
||||
LoginLogPageReqVO reqVO = new LoginLogPageReqVO();
|
||||
reqVO.setUsername("wangkai");
|
||||
reqVO.setUsername("wang");
|
||||
reqVO.setUserIp("192.168.199");
|
||||
reqVO.setStatus(true);
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 3, 5),
|
||||
buildTime(2021, 3, 7)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
|
||||
|
||||
// 调用service方法
|
||||
// 调用
|
||||
PageResult<LoginLogDO> pageResult = loginLogService.getLoginLogPage(reqVO);
|
||||
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
@ -81,62 +66,45 @@ public class LoginLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Test
|
||||
public void testGetLoginLogList() {
|
||||
// 构造测试数据
|
||||
|
||||
// 登录成功的
|
||||
LoginLogDO loginLogDO = RandomUtils.randomPojo(LoginLogDO.class, logDO -> {
|
||||
logDO.setLogType(RandomUtil.randomEle(cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum.values()).getType());
|
||||
logDO.setTraceId(TracerUtils.getTraceId());
|
||||
logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue());
|
||||
|
||||
logDO.setUserIp("192.168.111.16");
|
||||
logDO.setUsername("wangxiaokai");
|
||||
logDO.setCreateTime(buildTime(2021, 3, 6));
|
||||
logDO.setResult(SUCCESS.getResult());
|
||||
// mock 数据
|
||||
LoginLogDO loginLogDO = randomPojo(LoginLogDO.class, o -> {
|
||||
o.setUserIp("192.168.199.16");
|
||||
o.setUsername("wang");
|
||||
o.setResult(SUCCESS.getResult());
|
||||
o.setCreateTime(buildTime(2021, 3, 6));
|
||||
});
|
||||
loginLogMapper.insert(loginLogDO);
|
||||
|
||||
// 下面几个都是不匹配的数据
|
||||
// 登录失败的
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(CAPTCHA_CODE_ERROR.getResult())));
|
||||
// 不同ip段的
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18")));
|
||||
// 不同username
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai")));
|
||||
// 构造一个早期时间 2021-02-06 00:00:00
|
||||
loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6))));
|
||||
|
||||
// 测试 status 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setResult(CAPTCHA_CODE_ERROR.getResult())));
|
||||
// 测试 ip 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUserIp("192.168.128.18")));
|
||||
// 测试 username 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setUsername("yunai")));
|
||||
// 测试 createTime 不匹配
|
||||
loginLogMapper.insert(cloneIgnoreId(loginLogDO, o -> o.setCreateTime(buildTime(2021, 2, 6))));
|
||||
// 构造调用参数
|
||||
LoginLogExportReqVO reqVO = new LoginLogExportReqVO();
|
||||
reqVO.setUsername("wangxiaokai");
|
||||
reqVO.setUserIp("192.168.111");
|
||||
reqVO.setUsername("wang");
|
||||
reqVO.setUserIp("192.168.199");
|
||||
reqVO.setStatus(true);
|
||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 3, 5),
|
||||
buildTime(2021, 3, 7)}));
|
||||
reqVO.setCreateTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
|
||||
|
||||
// 调用service方法
|
||||
List<LoginLogDO> loginLogList = loginLogService.getLoginLogList(reqVO);
|
||||
|
||||
List<LoginLogDO> list = loginLogService.getLoginLogList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, loginLogList.size());
|
||||
assertPojoEquals(loginLogDO, loginLogList.get(0));
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(loginLogDO, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateLoginLog() {
|
||||
LoginLogCreateReqDTO reqDTO = RandomUtils.randomPojo(LoginLogCreateReqDTO.class, vo -> {
|
||||
// 指定随机的范围,避免超出范围入库失败
|
||||
vo.setUserType(randomEle(UserTypeEnum.values()).getValue());
|
||||
vo.setLogType(randomEle(LoginLogTypeEnum.values()).getType());
|
||||
vo.setResult(randomEle(values()).getResult());
|
||||
vo.setTraceId(TracerUtils.getTraceId());
|
||||
});
|
||||
LoginLogCreateReqDTO reqDTO = randomPojo(LoginLogCreateReqDTO.class);
|
||||
|
||||
// 调用
|
||||
loginLogService.createLoginLog(reqDTO);
|
||||
// 断言,忽略基本字段
|
||||
LoginLogDO sysLoginLogDO = loginLogMapper.selectOne(null);
|
||||
assertPojoEquals(reqDTO, sysLoginLogDO);
|
||||
// 断言
|
||||
LoginLogDO loginLogDO = loginLogMapper.selectOne(null);
|
||||
assertPojoEquals(reqDTO, loginLogDO);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,8 +5,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
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
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.logger.OperateLogDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.logger.OperateLogMapper;
|
||||
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
|
||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@ -49,34 +47,28 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Test
|
||||
public void testCreateOperateLogAsync() {
|
||||
String traceId = TracerUtils.getTraceId();
|
||||
OperateLogCreateReqDTO reqVO = RandomUtils.randomPojo(OperateLogCreateReqDTO.class, o -> {
|
||||
o.setTraceId(traceId);
|
||||
o.setUserId(randomLongId());
|
||||
o.setUserType(randomEle(UserTypeEnum.values()).getValue());
|
||||
o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build());
|
||||
});
|
||||
OperateLogCreateReqDTO reqVO = RandomUtils.randomPojo(OperateLogCreateReqDTO.class,
|
||||
o -> o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build()));
|
||||
|
||||
// 执行service方法
|
||||
// 调研
|
||||
operateLogServiceImpl.createOperateLog(reqVO);
|
||||
// 断言插入是否正确
|
||||
OperateLogDO sysOperateLogDO = operateLogMapper.selectOne("trace_id", traceId);
|
||||
assertPojoEquals(reqVO, sysOperateLogDO);
|
||||
// 断言
|
||||
OperateLogDO operateLogDO = operateLogMapper.selectOne(null);
|
||||
assertPojoEquals(reqVO, operateLogDO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOperateLogPage() {
|
||||
// 构造测试数据
|
||||
// 先构造用户
|
||||
// mock(用户信息)
|
||||
AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> {
|
||||
o.setNickname("wangkai");
|
||||
o.setSex(SexEnum.MALE.getSex());
|
||||
o.setNickname("wang");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
});
|
||||
when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user));
|
||||
when(userService.getUserListByNickname("wang")).thenReturn(Collections.singletonList(user));
|
||||
Long userId = user.getId();
|
||||
|
||||
// 构造操作日志
|
||||
OperateLogDO sysOperateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> {
|
||||
OperateLogDO operateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> {
|
||||
o.setUserId(userId);
|
||||
o.setUserType(randomEle(UserTypeEnum.values()).getValue());
|
||||
o.setModule("order");
|
||||
@ -85,50 +77,46 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest {
|
||||
o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
||||
o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build());
|
||||
});
|
||||
operateLogMapper.insert(sysOperateLogDO);
|
||||
|
||||
// 下面几个是不匹配的数据
|
||||
// 随机 userId
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1)));
|
||||
// module 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user")));
|
||||
// type 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType())));
|
||||
// createTime 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6))));
|
||||
// resultCode 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(BAD_REQUEST.getCode())));
|
||||
operateLogMapper.insert(operateLogDO);
|
||||
// 测试 userId 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setUserId(userId + 1)));
|
||||
// 测试 module 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setModule("user")));
|
||||
// 测试 type 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setType(OperateTypeEnum.IMPORT.getType())));
|
||||
// 测试 createTime 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setStartTime(buildTime(2021, 2, 6))));
|
||||
// 测试 resultCode 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setResultCode(BAD_REQUEST.getCode())));
|
||||
|
||||
// 构造调用参数
|
||||
OperateLogPageReqVO reqVO = new OperateLogPageReqVO();
|
||||
reqVO.setUserNickname("wangkai");
|
||||
reqVO.setUserNickname("wang");
|
||||
reqVO.setModule("order");
|
||||
reqVO.setType(OperateTypeEnum.CREATE.getType());
|
||||
reqVO.setStartTime((new LocalDateTime[]{buildTime(2021, 3, 5),
|
||||
buildTime(2021, 3, 7)}));
|
||||
reqVO.setStartTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
|
||||
reqVO.setSuccess(true);
|
||||
|
||||
// 调用service方法
|
||||
// 调用
|
||||
PageResult<OperateLogDO> pageResult = operateLogServiceImpl.getOperateLogPage(reqVO);
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(sysOperateLogDO, pageResult.getList().get(0));
|
||||
assertPojoEquals(operateLogDO, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOperateLogs() {
|
||||
// 构造测试数据
|
||||
// 先构造用户
|
||||
// mock(用户信息)
|
||||
AdminUserDO user = RandomUtils.randomPojo(AdminUserDO.class, o -> {
|
||||
o.setNickname("wangkai");
|
||||
o.setSex(SexEnum.MALE.getSex());
|
||||
o.setNickname("wang");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
});
|
||||
when(userService.getUsersByNickname("wangkai")).thenReturn(Collections.singletonList(user));
|
||||
when(userService.getUserListByNickname("wang")).thenReturn(Collections.singletonList(user));
|
||||
Long userId = user.getId();
|
||||
|
||||
// 构造操作日志
|
||||
OperateLogDO sysOperateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> {
|
||||
OperateLogDO operateLogDO = RandomUtils.randomPojo(OperateLogDO.class, o -> {
|
||||
o.setUserId(userId);
|
||||
o.setUserType(randomEle(UserTypeEnum.values()).getValue());
|
||||
o.setModule("order");
|
||||
@ -137,33 +125,31 @@ public class OperateLogServiceImplTest extends BaseDbUnitTest {
|
||||
o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
||||
o.setExts(MapUtil.<String, Object>builder("orderId", randomLongId()).build());
|
||||
});
|
||||
operateLogMapper.insert(sysOperateLogDO);
|
||||
|
||||
// 下面几个是不匹配的数据
|
||||
// 随机 userId
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1)));
|
||||
// module 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user")));
|
||||
// type 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType())));
|
||||
// createTime 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6))));
|
||||
// resultCode 不同
|
||||
operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(BAD_REQUEST.getCode())));
|
||||
operateLogMapper.insert(operateLogDO);
|
||||
// 测试 userId 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setUserId(userId + 1)));
|
||||
// 测试 module 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setModule("user")));
|
||||
// 测试 type 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setType(OperateTypeEnum.IMPORT.getType())));
|
||||
// 测试 createTime 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setStartTime(buildTime(2021, 2, 6))));
|
||||
// 测试 resultCode 不匹配
|
||||
operateLogMapper.insert(cloneIgnoreId(operateLogDO, o -> o.setResultCode(BAD_REQUEST.getCode())));
|
||||
|
||||
// 构造调用参数
|
||||
OperateLogExportReqVO reqVO = new OperateLogExportReqVO();
|
||||
reqVO.setUserNickname("wangkai");
|
||||
reqVO.setUserNickname("wang");
|
||||
reqVO.setModule("order");
|
||||
reqVO.setType(OperateTypeEnum.CREATE.getType());
|
||||
reqVO.setStartTime((new LocalDateTime[]{buildTime(2021, 3, 5),buildTime(2021, 3, 7)}));
|
||||
reqVO.setStartTime(buildBetweenTime(2021, 3, 5, 2021, 3, 7));
|
||||
reqVO.setSuccess(true);
|
||||
|
||||
// 调用 service 方法
|
||||
List<OperateLogDO> list = operateLogServiceImpl.getOperateLogs(reqVO);
|
||||
List<OperateLogDO> list = operateLogServiceImpl.getOperateLogList(reqVO);
|
||||
// 断言,只查到了一条符合条件的
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(sysOperateLogDO, list.get(0));
|
||||
assertPojoEquals(operateLogDO, list.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper;
|
||||
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -14,7 +13,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@link MailAccountServiceImpl} 的单元测试类
|
||||
@ -109,6 +110,8 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest {
|
||||
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbMailAccount.getId();
|
||||
// mock 方法(无关联模版)
|
||||
when(mailTemplateService.countByAccountId(eq(id))).thenReturn(0L);
|
||||
|
||||
// 调用
|
||||
mailAccountService.deleteMailAccount(id);
|
||||
@ -117,6 +120,21 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest {
|
||||
verify(mailProducer).sendMailAccountRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailAccountFromCache() {
|
||||
// mock 数据
|
||||
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据
|
||||
mailAccountService.initLocalCache();
|
||||
// 准备参数
|
||||
Long id = dbMailAccount.getId();
|
||||
|
||||
// 调用
|
||||
MailAccountDO mailAccount = mailAccountService.getMailAccountFromCache(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbMailAccount, mailAccount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteMailAccount_notExists() {
|
||||
// 准备参数
|
||||
@ -151,4 +169,35 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest {
|
||||
assertPojoEquals(dbMailAccount, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailAccount() {
|
||||
// mock 数据
|
||||
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbMailAccount.getId();
|
||||
|
||||
// 调用
|
||||
MailAccountDO mailAccount = mailAccountService.getMailAccount(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbMailAccount, mailAccount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailAccountList() {
|
||||
// mock 数据
|
||||
MailAccountDO dbMailAccount01 = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(dbMailAccount01);
|
||||
MailAccountDO dbMailAccount02 = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(dbMailAccount02);
|
||||
// 准备参数
|
||||
|
||||
// 调用
|
||||
List<MailAccountDO> list = mailAccountService.getMailAccountList();
|
||||
// 断言
|
||||
assertEquals(2, list.size());
|
||||
assertPojoEquals(dbMailAccount01, list.get(0));
|
||||
assertPojoEquals(dbMailAccount02, list.get(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -116,6 +116,20 @@ public class MailLogServiceImplTest extends BaseDbUnitTest {
|
||||
assertEquals("NullPointerException: 测试异常", dbLog.getSendException());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailLog() {
|
||||
// mock 数据
|
||||
MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> o.setTemplateParams(randomTemplateParams()));
|
||||
mailLogMapper.insert(dbMailLog);
|
||||
// 准备参数
|
||||
Long id = dbMailLog.getId();
|
||||
|
||||
// 调用
|
||||
MailLogDO mailLog = mailLogService.getMailLog(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbMailLog, mailLog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailLogPage() {
|
||||
// mock 数据
|
||||
|
@ -6,14 +6,20 @@ import cn.hutool.extra.mail.MailUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
|
||||
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
|
||||
import cn.iocoder.yudao.module.system.service.member.MemberService;
|
||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -23,6 +29,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ -31,6 +38,10 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
@InjectMocks
|
||||
private MailSendServiceImpl mailSendService;
|
||||
|
||||
@Mock
|
||||
private AdminUserService adminUserService;
|
||||
@Mock
|
||||
private MemberService memberService;
|
||||
@Mock
|
||||
private MailAccountService mailAccountService;
|
||||
@Mock
|
||||
@ -55,6 +66,82 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
System.out.println("发送结果:" + messageId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendSingleMailToAdmin() {
|
||||
// 准备参数
|
||||
Long userId = randomLongId();
|
||||
String templateCode = RandomUtils.randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock adminUserService 的方法
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setMobile("15601691300"));
|
||||
when(adminUserService.getUser(eq(userId))).thenReturn(user);
|
||||
|
||||
// mock MailTemplateService 的方法
|
||||
MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> {
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setContent("验证码为{code}, 操作为{op}");
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
String content = RandomUtils.randomString();
|
||||
when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams)))
|
||||
.thenReturn(content);
|
||||
// mock MailAccountService 的方法
|
||||
MailAccountDO account = randomPojo(MailAccountDO.class);
|
||||
when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account);
|
||||
// mock MailLogService 的方法
|
||||
Long mailLogId = randomLongId();
|
||||
when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.ADMIN.getValue()), eq(user.getEmail()),
|
||||
eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId);
|
||||
|
||||
// 调用
|
||||
Long resultMailLogId = mailSendService.sendSingleMailToAdmin(null, userId, templateCode, templateParams);
|
||||
// 断言
|
||||
assertEquals(mailLogId, resultMailLogId);
|
||||
// 断言调用
|
||||
verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(user.getEmail()),
|
||||
eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendSingleMailToMember() {
|
||||
// 准备参数
|
||||
Long userId = randomLongId();
|
||||
String templateCode = RandomUtils.randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock memberService 的方法
|
||||
String mail = randomEmail();
|
||||
when(memberService.getMemberUserEmail(eq(userId))).thenReturn(mail);
|
||||
|
||||
// mock MailTemplateService 的方法
|
||||
MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> {
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setContent("验证码为{code}, 操作为{op}");
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
String content = RandomUtils.randomString();
|
||||
when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams)))
|
||||
.thenReturn(content);
|
||||
// mock MailAccountService 的方法
|
||||
MailAccountDO account = randomPojo(MailAccountDO.class);
|
||||
when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account);
|
||||
// mock MailLogService 的方法
|
||||
Long mailLogId = randomLongId();
|
||||
when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.MEMBER.getValue()), eq(mail),
|
||||
eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId);
|
||||
|
||||
// 调用
|
||||
Long resultMailLogId = mailSendService.sendSingleMailToMember(null, userId, templateCode, templateParams);
|
||||
// 断言
|
||||
assertEquals(mailLogId, resultMailLogId);
|
||||
// 断言调用
|
||||
verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(mail),
|
||||
eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送成功,当短信模板开启时
|
||||
*/
|
||||
@ -64,7 +151,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
String mail = randomEmail();
|
||||
Long userId = randomLongId();
|
||||
Integer userType = randomEle(UserTypeEnum.values()).getValue();
|
||||
String templateCode = randomString();
|
||||
String templateCode = RandomUtils.randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock MailTemplateService 的方法
|
||||
@ -74,7 +161,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
String content = randomString();
|
||||
String content = RandomUtils.randomString();
|
||||
when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams)))
|
||||
.thenReturn(content);
|
||||
// mock MailAccountService 的方法
|
||||
@ -103,7 +190,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
String mail = randomEmail();
|
||||
Long userId = randomLongId();
|
||||
Integer userType = randomEle(UserTypeEnum.values()).getValue();
|
||||
String templateCode = randomString();
|
||||
String templateCode = RandomUtils.randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock MailTemplateService 的方法
|
||||
@ -113,7 +200,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
String content = randomString();
|
||||
String content = RandomUtils.randomString();
|
||||
when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams)))
|
||||
.thenReturn(content);
|
||||
// mock MailAccountService 的方法
|
||||
@ -134,18 +221,18 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckMailTemplateValid_notExists() {
|
||||
public void testValidateMailTemplateValid_notExists() {
|
||||
// 准备参数
|
||||
String templateCode = randomString();
|
||||
String templateCode = RandomUtils.randomString();
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> mailSendService.checkMailTemplateValid(templateCode),
|
||||
assertServiceException(() -> mailSendService.validateMailTemplate(templateCode),
|
||||
MAIL_TEMPLATE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckTemplateParams_paramMiss() {
|
||||
public void testValidateTemplateParams_paramMiss() {
|
||||
// 准备参数
|
||||
MailTemplateDO template = randomPojo(MailTemplateDO.class,
|
||||
o -> o.setParams(Lists.newArrayList("code")));
|
||||
@ -153,18 +240,80 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> mailSendService.checkTemplateParams(template, templateParams),
|
||||
assertServiceException(() -> mailSendService.validateTemplateParams(template, templateParams),
|
||||
MAIL_SEND_TEMPLATE_PARAM_MISS, "code");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckMail_notExists() {
|
||||
public void testValidateMail_notExists() {
|
||||
// 准备参数
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> mailSendService.checkMail(null),
|
||||
assertServiceException(() -> mailSendService.validateMail(null),
|
||||
MAIL_SEND_MAIL_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSendMail_success() {
|
||||
try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) {
|
||||
// 准备参数
|
||||
MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿"));
|
||||
// mock 方法(获得邮箱账号)
|
||||
MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com"));
|
||||
when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId())))
|
||||
.thenReturn(account);
|
||||
|
||||
// mock 方法(发送邮件)
|
||||
String messageId = randomString();
|
||||
mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> {
|
||||
assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom());
|
||||
assertTrue(mailAccount.isAuth());
|
||||
assertEquals(account.getUsername(), mailAccount.getUser());
|
||||
assertEquals(account.getPassword(), mailAccount.getPass());
|
||||
assertEquals(account.getHost(), mailAccount.getHost());
|
||||
assertEquals(account.getPort(), mailAccount.getPort());
|
||||
assertEquals(account.getSslEnable(), mailAccount.isSslEnable());
|
||||
return true;
|
||||
}), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true)))
|
||||
.thenReturn(messageId);
|
||||
|
||||
// 调用
|
||||
mailSendService.doSendMail(message);
|
||||
// 断言
|
||||
verify(mailLogService).updateMailSendResult(eq(message.getLogId()), eq(messageId), isNull());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSendMail_exception() {
|
||||
try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) {
|
||||
// 准备参数
|
||||
MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿"));
|
||||
// mock 方法(获得邮箱账号)
|
||||
MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com"));
|
||||
when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId())))
|
||||
.thenReturn(account);
|
||||
|
||||
// mock 方法(发送邮件)
|
||||
Exception e = new NullPointerException("啦啦啦");
|
||||
mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> {
|
||||
assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom());
|
||||
assertTrue(mailAccount.isAuth());
|
||||
assertEquals(account.getUsername(), mailAccount.getUser());
|
||||
assertEquals(account.getPassword(), mailAccount.getPass());
|
||||
assertEquals(account.getHost(), mailAccount.getHost());
|
||||
assertEquals(account.getPort(), mailAccount.getPort());
|
||||
assertEquals(account.getSslEnable(), mailAccount.isSslEnable());
|
||||
return true;
|
||||
}), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true)))
|
||||
.thenThrow(e);
|
||||
|
||||
// 调用
|
||||
mailSendService.doSendMail(message);
|
||||
// 断言
|
||||
verify(mailLogService).updateMailSendResult(eq(message.getLogId()), isNull(), same(e));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper;
|
||||
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
|
||||
@ -15,7 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* {@link MailTemplateServiceImpl} 的单元测试类
|
||||
@ -72,6 +73,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest {
|
||||
// 校验记录的属性是否正确
|
||||
MailTemplateDO mailTemplate = mailTemplateMapper.selectById(mailTemplateId);
|
||||
assertPojoEquals(reqVO, mailTemplate);
|
||||
verify(mailProducer).sendMailTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -89,6 +91,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest {
|
||||
// 校验是否更新正确
|
||||
MailTemplateDO mailTemplate = mailTemplateMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, mailTemplate);
|
||||
verify(mailProducer).sendMailTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -110,8 +113,9 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
// 调用
|
||||
mailTemplateService.deleteMailTemplate(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(mailTemplateMapper.selectById(id));
|
||||
// 校验数据不存在了
|
||||
assertNull(mailTemplateMapper.selectById(id));
|
||||
verify(mailProducer).sendMailTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -160,4 +164,77 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest {
|
||||
assertPojoEquals(dbMailTemplate, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailTemplateList() {
|
||||
// mock 数据
|
||||
MailTemplateDO dbMailTemplate01 = randomPojo(MailTemplateDO.class);
|
||||
mailTemplateMapper.insert(dbMailTemplate01);
|
||||
MailTemplateDO dbMailTemplate02 = randomPojo(MailTemplateDO.class);
|
||||
mailTemplateMapper.insert(dbMailTemplate02);
|
||||
|
||||
// 调用
|
||||
List<MailTemplateDO> list = mailTemplateService.getMailTemplateList();
|
||||
// 断言
|
||||
assertEquals(2, list.size());
|
||||
assertEquals(dbMailTemplate01, list.get(0));
|
||||
assertEquals(dbMailTemplate02, list.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailTemplate() {
|
||||
// mock 数据
|
||||
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class);
|
||||
mailTemplateMapper.insert(dbMailTemplate);
|
||||
// 准备参数
|
||||
Long id = dbMailTemplate.getId();
|
||||
|
||||
// 调用
|
||||
MailTemplateDO mailTemplate = mailTemplateService.getMailTemplate(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbMailTemplate, mailTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailTemplateByCodeFromCache() {
|
||||
// mock 数据
|
||||
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class);
|
||||
mailTemplateMapper.insert(dbMailTemplate);
|
||||
mailTemplateService.initLocalCache();
|
||||
// 准备参数
|
||||
String code = dbMailTemplate.getCode();
|
||||
|
||||
// 调用
|
||||
MailTemplateDO mailTemplate = mailTemplateService.getMailTemplateByCodeFromCache(code);
|
||||
// 断言
|
||||
assertPojoEquals(dbMailTemplate, mailTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatMailTemplateContent() {
|
||||
// 准备参数
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("name", "小红");
|
||||
params.put("what", "饭");
|
||||
|
||||
// 调用,并断言
|
||||
assertEquals("小红,你好,饭吃了吗?",
|
||||
mailTemplateService.formatMailTemplateContent("{name},你好,{what}吃了吗?", params));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountByAccountId() {
|
||||
// mock 数据
|
||||
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class);
|
||||
mailTemplateMapper.insert(dbMailTemplate);
|
||||
// 测试 accountId 不匹配
|
||||
mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L)));
|
||||
// 准备参数
|
||||
Long accountId = dbMailTemplate.getAccountId();
|
||||
|
||||
// 调用
|
||||
long count = mailTemplateService.countByAccountId(accountId);
|
||||
// 断言
|
||||
assertEquals(1, count);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,75 +2,67 @@ package cn.iocoder.yudao.module.system.service.notice;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper;
|
||||
import cn.iocoder.yudao.module.system.enums.notice.NoticeTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@Import(NoticeServiceImpl.class)
|
||||
class NoticeServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private NoticeServiceImpl sysNoticeService;
|
||||
private NoticeServiceImpl noticeService;
|
||||
|
||||
@Resource
|
||||
private NoticeMapper sysNoticeMapper;
|
||||
private NoticeMapper noticeMapper;
|
||||
|
||||
@Test
|
||||
public void testPageNotices_success() {
|
||||
public void testGetNoticePage_success() {
|
||||
// 插入前置数据
|
||||
NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> {
|
||||
o.setTitle("尼古拉斯赵四来啦!");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setType(randomEle(NoticeTypeEnum.values()).getType());
|
||||
});
|
||||
sysNoticeMapper.insert(dbNotice);
|
||||
|
||||
noticeMapper.insert(dbNotice);
|
||||
// 测试 title 不匹配
|
||||
sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!")));
|
||||
noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!")));
|
||||
// 测试 status 不匹配
|
||||
sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
|
||||
|
||||
// 查询
|
||||
noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 准备参数
|
||||
NoticePageReqVO reqVO = new NoticePageReqVO();
|
||||
reqVO.setTitle("尼古拉斯赵四来啦!");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
PageResult<NoticeDO> pageResult = sysNoticeService.pageNotices(reqVO);
|
||||
|
||||
// 调用
|
||||
PageResult<NoticeDO> pageResult = noticeService.getNoticePage(reqVO);
|
||||
// 验证查询结果经过筛选
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbNotice, pageResult.getList().get(0));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNotice_success() {
|
||||
// 插入前置数据
|
||||
NoticeDO dbNotice = randomNoticeDO();
|
||||
sysNoticeMapper.insert(dbNotice);
|
||||
NoticeDO dbNotice = randomPojo(NoticeDO.class);
|
||||
noticeMapper.insert(dbNotice);
|
||||
|
||||
// 查询
|
||||
NoticeDO notice = sysNoticeService.getNotice(dbNotice.getId());
|
||||
NoticeDO notice = noticeService.getNotice(dbNotice.getId());
|
||||
|
||||
// 验证插入与读取对象是否一致
|
||||
assertNotNull(notice);
|
||||
@ -80,84 +72,59 @@ class NoticeServiceImplTest extends BaseDbUnitTest {
|
||||
@Test
|
||||
public void testCreateNotice_success() {
|
||||
// 准备参数
|
||||
NoticeCreateReqVO reqVO = randomNoticeCreateReqVO();
|
||||
|
||||
// 校验插入是否成功
|
||||
Long noticeId = sysNoticeService.createNotice(reqVO);
|
||||
assertNotNull(noticeId);
|
||||
NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class);
|
||||
|
||||
// 调用
|
||||
Long noticeId = noticeService.createNotice(reqVO);
|
||||
// 校验插入属性是否正确
|
||||
NoticeDO notice = sysNoticeMapper.selectById(noticeId);
|
||||
assertNotNull(noticeId);
|
||||
NoticeDO notice = noticeMapper.selectById(noticeId);
|
||||
assertPojoEquals(reqVO, notice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateNotice_success() {
|
||||
// 插入前置数据
|
||||
NoticeDO dbNoticeDO = randomNoticeDO();
|
||||
sysNoticeMapper.insert(dbNoticeDO);
|
||||
NoticeDO dbNoticeDO = randomPojo(NoticeDO.class);
|
||||
noticeMapper.insert(dbNoticeDO);
|
||||
|
||||
// 准备更新参数
|
||||
NoticeUpdateReqVO reqVO = randomNoticeUpdateReqVO(o -> o.setId(dbNoticeDO.getId()));
|
||||
NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, o -> o.setId(dbNoticeDO.getId()));
|
||||
|
||||
// 更新
|
||||
sysNoticeService.updateNotice(reqVO);
|
||||
|
||||
noticeService.updateNotice(reqVO);
|
||||
// 检验是否更新成功
|
||||
NoticeDO notice = sysNoticeMapper.selectById(reqVO.getId());
|
||||
NoticeDO notice = noticeMapper.selectById(reqVO.getId());
|
||||
assertPojoEquals(reqVO, notice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNotice_success() {
|
||||
// 插入前置数据
|
||||
NoticeDO dbNotice = randomNoticeDO();
|
||||
sysNoticeMapper.insert(dbNotice);
|
||||
NoticeDO dbNotice = randomPojo(NoticeDO.class);
|
||||
noticeMapper.insert(dbNotice);
|
||||
|
||||
// 删除
|
||||
sysNoticeService.deleteNotice(dbNotice.getId());
|
||||
noticeService.deleteNotice(dbNotice.getId());
|
||||
|
||||
// 检查是否删除成功
|
||||
assertNull(sysNoticeMapper.selectById(dbNotice.getId()));
|
||||
assertNull(noticeMapper.selectById(dbNotice.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNoticeExists_success() {
|
||||
public void testValidateNoticeExists_success() {
|
||||
// 插入前置数据
|
||||
NoticeDO dbNotice = randomNoticeDO();
|
||||
sysNoticeMapper.insert(dbNotice);
|
||||
NoticeDO dbNotice = randomPojo(NoticeDO.class);
|
||||
noticeMapper.insert(dbNotice);
|
||||
|
||||
// 成功调用
|
||||
sysNoticeService.checkNoticeExists(dbNotice.getId());
|
||||
noticeService.validateNoticeExists(dbNotice.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNoticeExists_noExists() {
|
||||
assertServiceException(() -> sysNoticeService.checkNoticeExists(randomLongId()), NOTICE_NOT_FOUND);
|
||||
public void testValidateNoticeExists_noExists() {
|
||||
assertServiceException(() ->
|
||||
noticeService.validateNoticeExists(randomLongId()), NOTICE_NOT_FOUND);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static NoticeDO randomNoticeDO(Consumer<NoticeDO>... consumers) {
|
||||
NoticeDO notice = randomPojo(NoticeDO.class, consumers);
|
||||
notice.setType(randomEle(NoticeTypeEnum.values()).getType());
|
||||
notice.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
return notice;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static NoticeUpdateReqVO randomNoticeUpdateReqVO(Consumer<NoticeUpdateReqVO>... consumers) {
|
||||
NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, consumers);
|
||||
reqVO.setType(randomEle(NoticeTypeEnum.values()).getType());
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
return reqVO;
|
||||
}
|
||||
|
||||
private static NoticeCreateReqVO randomNoticeCreateReqVO() {
|
||||
NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class);
|
||||
reqVO.setType(randomEle(NoticeTypeEnum.values()).getType());
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
return reqVO;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -108,6 +108,20 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest {
|
||||
assertPojoEquals(dbNotifyMessage, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNotifyMessage() {
|
||||
// mock 数据
|
||||
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class,
|
||||
o -> o.setTemplateParams(randomTemplateParams()));
|
||||
notifyMessageMapper.insert(dbNotifyMessage);
|
||||
// 准备参数
|
||||
Long id = dbNotifyMessage.getId();
|
||||
|
||||
// 调用
|
||||
NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id);
|
||||
assertPojoEquals(dbNotifyMessage, notifyMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMyNotifyMessagePage() {
|
||||
// mock 数据
|
||||
|
@ -4,8 +4,6 @@ import cn.hutool.core.map.MapUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -18,7 +16,8 @@ import java.util.Map;
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_SEND_TEMPLATE_PARAM_MISS;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@ -34,6 +33,62 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest {
|
||||
@Mock
|
||||
private NotifyMessageService notifyMessageService;
|
||||
|
||||
@Test
|
||||
public void testSendSingleNotifyToAdmin() {
|
||||
// 准备参数
|
||||
Long userId = randomLongId();
|
||||
String templateCode = randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock NotifyTemplateService 的方法
|
||||
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> {
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setContent("验证码为{code}, 操作为{op}");
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
String content = randomString();
|
||||
when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams)))
|
||||
.thenReturn(content);
|
||||
// mock NotifyMessageService 的方法
|
||||
Long messageId = randomLongId();
|
||||
when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.ADMIN.getValue()),
|
||||
eq(template), eq(content), eq(templateParams))).thenReturn(messageId);
|
||||
|
||||
// 调用
|
||||
Long resultMessageId = notifySendService.sendSingleNotifyToAdmin(userId, templateCode, templateParams);
|
||||
// 断言
|
||||
assertEquals(messageId, resultMessageId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendSingleNotifyToMember() {
|
||||
// 准备参数
|
||||
Long userId = randomLongId();
|
||||
String templateCode = randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock NotifyTemplateService 的方法
|
||||
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> {
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setContent("验证码为{code}, 操作为{op}");
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
String content = randomString();
|
||||
when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams)))
|
||||
.thenReturn(content);
|
||||
// mock NotifyMessageService 的方法
|
||||
Long messageId = randomLongId();
|
||||
when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.MEMBER.getValue()),
|
||||
eq(template), eq(content), eq(templateParams))).thenReturn(messageId);
|
||||
|
||||
// 调用
|
||||
Long resultMessageId = notifySendService.sendSingleNotifyToMember(userId, templateCode, templateParams);
|
||||
// 断言
|
||||
assertEquals(messageId, resultMessageId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送成功,当短信模板开启时
|
||||
*/
|
||||
@ -100,7 +155,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> notifySendService.checkNotifyTemplateValid(templateCode),
|
||||
assertServiceException(() -> notifySendService.validateNotifyTemplate(templateCode),
|
||||
NOTICE_NOT_FOUND);
|
||||
}
|
||||
|
||||
@ -113,7 +168,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> notifySendService.checkTemplateParams(template, templateParams),
|
||||
assertServiceException(() -> notifySendService.validateTemplateParams(template, templateParams),
|
||||
NOTIFY_SEND_TEMPLATE_PARAM_MISS, "code");
|
||||
}
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user