refactor: 新增domain包的测试,并fix隐藏Bug
This commit is contained in:
parent
5bae6ea2ad
commit
7636244c49
@ -4,8 +4,6 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import cn.hutool.core.lang.tree.TreeNodeConfig;
|
||||
import cn.hutool.core.lang.tree.TreeUtil;
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode;
|
||||
import com.agileboot.domain.common.dto.TreeSelectedDTO;
|
||||
import com.agileboot.domain.system.menu.command.AddMenuCommand;
|
||||
import com.agileboot.domain.system.menu.command.UpdateMenuCommand;
|
||||
@ -82,7 +80,7 @@ public class MenuApplicationService {
|
||||
MenuModel model = updateCommand.toModel();
|
||||
model.checkMenuNameUnique(menuService);
|
||||
model.checkExternalLink();
|
||||
model.checkParentId();
|
||||
model.checkParentIdConflict();
|
||||
|
||||
model.logUpdater(loginUser);
|
||||
|
||||
@ -121,7 +119,7 @@ public class MenuApplicationService {
|
||||
|
||||
public List<Tree<Long>> buildMenuEntityTree(Long userId) {
|
||||
List<SysMenuEntity> allMenus;
|
||||
if (AuthenticationUtils.isAdmin(userId)) {
|
||||
if (LoginUser.isAdmin(userId)) {
|
||||
allMenus = menuService.list();
|
||||
} else {
|
||||
allMenus = menuService.getMenuListByUserId(userId);
|
||||
|
||||
@ -24,7 +24,6 @@ public class MenuModel extends SysMenuEntity {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void checkExternalLink() {
|
||||
if (getIsExternal() && !HttpUtil.isHttp(getPath()) && !HttpUtil.isHttps(getPath())) {
|
||||
throw new ApiException(ErrorCode.Business.MENU_EXTERNAL_LINK_MUST_BE_HTTP);
|
||||
@ -32,7 +31,7 @@ public class MenuModel extends SysMenuEntity {
|
||||
}
|
||||
|
||||
|
||||
public void checkParentId() {
|
||||
public void checkParentIdConflict() {
|
||||
if (getMenuId().equals(getParentId())) {
|
||||
throw new ApiException(ErrorCode.Business.MENU_PARENT_ID_NOT_ALLOW_SELF);
|
||||
}
|
||||
|
||||
@ -9,13 +9,9 @@ import lombok.Data;
|
||||
@Data
|
||||
public class NoticeModel extends SysNoticeEntity {
|
||||
|
||||
|
||||
public void checkFields() {
|
||||
Integer noticeType = this.getNoticeType();
|
||||
BasicEnumUtil.fromValue(NoticeTypeEnum.class, noticeType);
|
||||
|
||||
Integer status = this.getStatus();
|
||||
BasicEnumUtil.fromValue(CommonStatusEnum.class, status);
|
||||
BasicEnumUtil.fromValue(NoticeTypeEnum.class, getNoticeType());
|
||||
BasicEnumUtil.fromValue(CommonStatusEnum.class, getStatus());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.agileboot.domain.system.role.model;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode;
|
||||
import com.agileboot.common.exception.error.ErrorCode.Business;
|
||||
import com.agileboot.orm.entity.SysRoleEntity;
|
||||
import com.agileboot.orm.entity.SysRoleMenuEntity;
|
||||
import com.agileboot.orm.service.ISysRoleMenuService;
|
||||
@ -36,9 +37,9 @@ public class RoleModel extends SysRoleEntity {
|
||||
}
|
||||
}
|
||||
|
||||
public void checkRoleCanBeDelete(ISysRoleService userService) {
|
||||
if (userService.isAssignedToUsers(getRoleId())) {
|
||||
throw new ApiException(ErrorCode.Business.ROLE_NAME_IS_NOT_UNIQUE, getRoleName());
|
||||
public void checkRoleCanBeDelete(ISysRoleService roleService) {
|
||||
if (roleService.isAssignedToUsers(getRoleId())) {
|
||||
throw new ApiException(Business.ROLE_ALREADY_ASSIGN_TO_USER, getRoleName());
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +52,7 @@ public class RoleModel extends SysRoleEntity {
|
||||
public void generateDeptIdSet() {
|
||||
if (deptIds == null) {
|
||||
setDeptIdSet("");
|
||||
return;
|
||||
}
|
||||
|
||||
if (deptIds.size() > new HashSet<>(deptIds).size()) {
|
||||
@ -71,17 +73,21 @@ public class RoleModel extends SysRoleEntity {
|
||||
public void updateById(ISysRoleMenuService roleMenuService) {
|
||||
this.updateById();
|
||||
// 清空之前的角色菜单关联
|
||||
roleMenuService.getBaseMapper().deleteByMap(Collections.singletonMap("role_id", getRoleId()));
|
||||
cleanOldMenus(roleMenuService);
|
||||
saveMenus(roleMenuService);
|
||||
}
|
||||
|
||||
public void deleteById(ISysRoleMenuService roleMenuService) {
|
||||
this.deleteById();
|
||||
// 清空之前的角色菜单关联
|
||||
roleMenuService.getBaseMapper().deleteByMap(Collections.singletonMap("role_id", getRoleId()));
|
||||
cleanOldMenus(roleMenuService);
|
||||
}
|
||||
|
||||
|
||||
private void cleanOldMenus(ISysRoleMenuService roleMenuService) {
|
||||
roleMenuService.getBaseMapper().deleteByMap(Collections.singletonMap("role_id", getRoleId()));
|
||||
}
|
||||
|
||||
private void saveMenus(ISysRoleMenuService roleMenuService) {
|
||||
List<SysRoleMenuEntity> list = new ArrayList<>();
|
||||
if (getMenuIds() != null) {
|
||||
|
||||
@ -37,13 +37,15 @@ public class UserModel extends SysUserEntity {
|
||||
}
|
||||
|
||||
public void checkCanBeDelete(LoginUser loginUser) {
|
||||
if (Objects.equals(getUserId(), loginUser.getUserId())) {
|
||||
if (Objects.equals(getUserId(), loginUser.getUserId())
|
||||
|| LoginUser.isAdmin(getUserId())) {
|
||||
throw new ApiException(ErrorCode.Business.USER_CURRENT_USER_CAN_NOT_BE_DELETE);
|
||||
}
|
||||
}
|
||||
|
||||
public void checkCanBeModify(LoginUser loginUser) {
|
||||
if (LoginUser.isAdmin(this.getUserId()) && !loginUser.isAdmin()) {
|
||||
// TODO 这个异常需要更明确
|
||||
throw new ApiException(Business.UNSUPPORTED_OPERATION);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,29 +1,96 @@
|
||||
package com.agileboot.domain.system.menu.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode.Business;
|
||||
import com.agileboot.orm.service.ISysMenuService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
|
||||
class MenuModelTest {
|
||||
|
||||
private static final long MENU_ID = 1L;
|
||||
|
||||
private final ISysMenuService menuService = mock(ISysMenuService.class);
|
||||
|
||||
@Test
|
||||
void testCheckMenuNameUnique() {
|
||||
MenuModel menuModel = new MenuModel();
|
||||
menuModel.setMenuName("menu 1");
|
||||
when(
|
||||
menuService.isMenuNameDuplicated(ArgumentMatchers.any(), ArgumentMatchers.any(),
|
||||
ArgumentMatchers.any())).thenReturn(true);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () -> menuModel.checkMenuNameUnique(menuService));
|
||||
Assertions.assertEquals(Business.MENU_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckExternalLink() {
|
||||
void testCheckExternalLinkWhenSuccessful() {
|
||||
MenuModel notExternalButWithoutHttpPrefix = new MenuModel();
|
||||
notExternalButWithoutHttpPrefix.setIsExternal(false);
|
||||
notExternalButWithoutHttpPrefix.setPath("www.baidu.com");
|
||||
|
||||
MenuModel isExternalWithHttpPrefix = new MenuModel();
|
||||
isExternalWithHttpPrefix.setIsExternal(true);
|
||||
isExternalWithHttpPrefix.setPath("http://www.baidu.com");
|
||||
|
||||
Assertions.assertDoesNotThrow(()->{
|
||||
notExternalButWithoutHttpPrefix.checkExternalLink();
|
||||
isExternalWithHttpPrefix.checkExternalLink();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckParentId() {
|
||||
void testCheckExternalLinkWhenFailed() {
|
||||
MenuModel externalWithoutHttpPrefix = new MenuModel();
|
||||
externalWithoutHttpPrefix.setIsExternal(true);
|
||||
externalWithoutHttpPrefix.setPath("www.baidu.com");
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, externalWithoutHttpPrefix::checkExternalLink);
|
||||
|
||||
Assertions.assertEquals(Business.MENU_EXTERNAL_LINK_MUST_BE_HTTP, exception.getErrorCode());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testCheckParentIdConflict() {
|
||||
MenuModel menuModel = new MenuModel();
|
||||
menuModel.setMenuId(MENU_ID);
|
||||
menuModel.setParentId(MENU_ID);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, menuModel::checkParentIdConflict);
|
||||
|
||||
Assertions.assertEquals(Business.MENU_PARENT_ID_NOT_ALLOW_SELF, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckHasChildMenus() {
|
||||
MenuModel menuModel = new MenuModel();
|
||||
menuModel.setMenuId(MENU_ID);
|
||||
when(menuService.hasChildrenMenu(eq(MENU_ID))).thenReturn(true);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () -> menuModel.checkHasChildMenus(menuService));
|
||||
|
||||
Assertions.assertEquals(Business.MENU_EXIST_CHILD_MENU_NOT_ALLOW_DELETE, exception.getErrorCode());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckMenuAlreadyAssignToRole() {
|
||||
MenuModel menuModel = new MenuModel();
|
||||
menuModel.setMenuId(MENU_ID);
|
||||
when(menuService.isMenuAssignToRoles(eq(MENU_ID))).thenReturn(true);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> menuModel.checkMenuAlreadyAssignToRole(menuService));
|
||||
|
||||
Assertions.assertEquals(Business.MENU_ALREADY_ASSIGN_TO_ROLE_NOT_ALLOW_DELETE, exception.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,11 +2,43 @@ package com.agileboot.domain.system.notice.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class NoticeModelTest {
|
||||
|
||||
@Test
|
||||
void testCheckFields() {
|
||||
void testCheckFieldsWhenTypeFailed() {
|
||||
NoticeModel noticeModel = new NoticeModel();
|
||||
noticeModel.setNoticeType(3);
|
||||
noticeModel.setStatus(1);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, noticeModel::checkFields);
|
||||
|
||||
Assertions.assertEquals(ErrorCode.Internal.GET_ENUM_FAILED, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckFieldsWhenTypeCorrect() {
|
||||
NoticeModel noticeModel = new NoticeModel();
|
||||
noticeModel.setNoticeType(1);
|
||||
noticeModel.setStatus(1);
|
||||
|
||||
Assertions.assertDoesNotThrow(noticeModel::checkFields);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testCheckFieldsWhenStatusFailed() {
|
||||
NoticeModel noticeModel = new NoticeModel();
|
||||
noticeModel.setNoticeType(1);
|
||||
noticeModel.setStatus(3);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, noticeModel::checkFields);
|
||||
|
||||
Assertions.assertEquals(ErrorCode.Internal.GET_ENUM_FAILED, exception.getErrorCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,20 +1,86 @@
|
||||
package com.agileboot.domain.system.post.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode.Business;
|
||||
import com.agileboot.orm.service.ISysPostService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
class PostModelTest {
|
||||
|
||||
private final ISysPostService postService = mock(ISysPostService.class);
|
||||
private static final long POST_ID = 1L;
|
||||
|
||||
@AfterEach
|
||||
public void clean() {
|
||||
Mockito.reset(postService);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testCheckCanBeDelete() {
|
||||
void testCheckCanBeDeleteWhenFailed() {
|
||||
PostModel postModel = new PostModel();
|
||||
postModel.setPostId(POST_ID);
|
||||
|
||||
when(postService.isAssignedToUsers(eq(POST_ID))).thenReturn(true);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () -> postModel.checkCanBeDelete(postService));
|
||||
Assertions.assertEquals(Business.POST_ALREADY_ASSIGNED_TO_USER_CAN_NOT_BE_DELETED, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckCanBeDeleteWhenSuccessful() {
|
||||
PostModel postModel = new PostModel();
|
||||
postModel.setPostId(POST_ID);
|
||||
|
||||
when(postService.isAssignedToUsers(eq(POST_ID))).thenReturn(true);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () -> postModel.checkCanBeDelete(postService));
|
||||
Assertions.assertEquals(Business.POST_ALREADY_ASSIGNED_TO_USER_CAN_NOT_BE_DELETED, exception.getErrorCode());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testCheckPostNameUnique() {
|
||||
PostModel postWithSameName = new PostModel();
|
||||
postWithSameName.setPostId(POST_ID);
|
||||
postWithSameName.setPostName("post 1");
|
||||
PostModel postWithNewName = new PostModel();
|
||||
postWithNewName.setPostName("post 2");
|
||||
postWithNewName.setPostId(POST_ID);
|
||||
|
||||
when(postService.isPostNameDuplicated(eq(POST_ID), eq("post 1"))).thenReturn(true);
|
||||
when(postService.isPostNameDuplicated(eq(POST_ID), eq("post 2"))).thenReturn(false);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> postWithSameName.checkPostNameUnique(postService));
|
||||
Assertions.assertEquals(Business.POST_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
Assertions.assertDoesNotThrow(() -> postWithNewName.checkPostNameUnique(postService));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckPostCodeUnique() {
|
||||
PostModel postWithSameCode = new PostModel();
|
||||
postWithSameCode.setPostId(POST_ID);
|
||||
postWithSameCode.setPostCode("code 1");
|
||||
PostModel postWithNewCode = new PostModel();
|
||||
postWithNewCode.setPostId(POST_ID);
|
||||
postWithNewCode.setPostCode("code 2");
|
||||
|
||||
when(postService.isPostCodeDuplicated(eq(POST_ID), eq("code 1"))).thenReturn(true);
|
||||
when(postService.isPostCodeDuplicated(eq(POST_ID), eq("code 2"))).thenReturn(false);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> postWithSameCode.checkPostCodeUnique(postService));
|
||||
Assertions.assertEquals(Business.POST_CODE_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
Assertions.assertDoesNotThrow(() -> postWithNewCode.checkPostCodeUnique(postService));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,25 +1,103 @@
|
||||
package com.agileboot.domain.system.role.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode.Business;
|
||||
import com.agileboot.orm.service.ISysRoleService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RoleModelTest {
|
||||
|
||||
private final ISysRoleService roleService = mock(ISysRoleService.class);
|
||||
|
||||
private static final long ROLE_ID = 1L;
|
||||
|
||||
|
||||
@Test
|
||||
void testCheckRoleNameUnique() {
|
||||
RoleModel roleWithSameName = new RoleModel();
|
||||
roleWithSameName.setRoleId(ROLE_ID);
|
||||
roleWithSameName.setRoleName("role 1");
|
||||
RoleModel roleWithNewName = new RoleModel();
|
||||
roleWithNewName.setRoleId(ROLE_ID);
|
||||
roleWithNewName.setRoleName("role 2");
|
||||
|
||||
when(roleService.isRoleNameDuplicated(eq(ROLE_ID), eq("role 1"))).thenReturn(true);
|
||||
when(roleService.isRoleNameDuplicated(eq(ROLE_ID), eq("role 2"))).thenReturn(false);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> roleWithSameName.checkRoleNameUnique(roleService));
|
||||
Assertions.assertEquals(Business.ROLE_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
Assertions.assertDoesNotThrow(() -> roleWithNewName.checkRoleNameUnique(roleService));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckRoleCanBeDelete() {
|
||||
RoleModel roleModel = new RoleModel();
|
||||
roleModel.setRoleId(ROLE_ID);
|
||||
roleModel.checkRoleCanBeDelete(roleService);
|
||||
|
||||
when(roleService.isAssignedToUsers(eq(ROLE_ID))).thenReturn(true);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () -> roleModel.checkRoleCanBeDelete(roleService));
|
||||
Assertions.assertEquals(Business.ROLE_ALREADY_ASSIGN_TO_USER, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckRoleKeyUnique() {
|
||||
RoleModel roleWithSameKey = new RoleModel();
|
||||
roleWithSameKey.setRoleId(ROLE_ID);
|
||||
roleWithSameKey.setRoleKey("key 1");
|
||||
RoleModel roleWithNewKey = new RoleModel();
|
||||
roleWithNewKey.setRoleId(ROLE_ID);
|
||||
roleWithNewKey.setRoleKey("key 2");
|
||||
|
||||
when(roleService.isRoleKeyDuplicated(eq(ROLE_ID), eq("key 1"))).thenReturn(true);
|
||||
when(roleService.isRoleKeyDuplicated(eq(ROLE_ID), eq("key 2"))).thenReturn(false);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> roleWithSameKey.checkRoleKeyUnique(roleService));
|
||||
Assertions.assertEquals(Business.ROLE_KEY_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
Assertions.assertDoesNotThrow(() -> roleWithNewKey.checkRoleKeyUnique(roleService));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenerateDeptIdSet() {
|
||||
void testGenerateDeptIdSetWhenNull() {
|
||||
RoleModel roleModel = new RoleModel();
|
||||
roleModel.setDeptIds(null);
|
||||
roleModel.generateDeptIdSet();
|
||||
|
||||
Assertions.assertEquals("", roleModel.getDeptIdSet());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testGenerateDeptIdSetWhenDuplicated() {
|
||||
RoleModel roleModel = new RoleModel();
|
||||
roleModel.setDeptIds(ListUtil.of(1L,1L,2L,3L));
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, roleModel::generateDeptIdSet);
|
||||
|
||||
Assertions.assertEquals(Business.ROLE_DATA_SCOPE_DUPLICATED_DEPT, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenerateDeptIdSetWhenSuccessful() {
|
||||
RoleModel roleModel = new RoleModel();
|
||||
roleModel.setDeptIds(ListUtil.of(1L,2L,3L));
|
||||
roleModel.generateDeptIdSet();
|
||||
|
||||
Assertions.assertEquals("1,2,3", roleModel.getDeptIdSet());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,36 +1,142 @@
|
||||
package com.agileboot.domain.system.user.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode.Business;
|
||||
import com.agileboot.infrastructure.web.domain.login.LoginUser;
|
||||
import com.agileboot.orm.service.ISysUserService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UserModelTest {
|
||||
|
||||
private final ISysUserService userService = mock(ISysUserService.class);
|
||||
|
||||
private static final long USER_ID = 1L;
|
||||
|
||||
@Test
|
||||
void checkUsernameIsUnique() {
|
||||
void testCheckUsernameIsUnique() {
|
||||
UserModel userWithSameName = new UserModel();
|
||||
userWithSameName.setUserId(USER_ID);
|
||||
userWithSameName.setUsername("user 1");
|
||||
UserModel userWithNewName = new UserModel();
|
||||
userWithNewName.setUserId(USER_ID);
|
||||
userWithNewName.setUsername("user 2");
|
||||
|
||||
when(userService.isUserNameDuplicated(eq("user 1"))).thenReturn(true);
|
||||
when(userService.isUserNameDuplicated(eq("user 2"))).thenReturn(false);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> userWithSameName.checkUsernameIsUnique(userService));
|
||||
Assertions.assertEquals(Business.USER_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
Assertions.assertDoesNotThrow(() -> userWithNewName.checkUsernameIsUnique(userService));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkPhoneNumberIsUnique() {
|
||||
void testCheckPhoneNumberIsUnique() {
|
||||
UserModel userWithSameNumber = new UserModel();
|
||||
userWithSameNumber.setUserId(USER_ID);
|
||||
userWithSameNumber.setPhoneNumber("111111");
|
||||
UserModel userWithNewNumber = new UserModel();
|
||||
userWithNewNumber.setUserId(USER_ID);
|
||||
userWithNewNumber.setPhoneNumber("222222");
|
||||
|
||||
when(userService.isPhoneDuplicated(eq("111111"), eq(USER_ID))).thenReturn(true);
|
||||
when(userService.isPhoneDuplicated(eq("222222"), eq(USER_ID))).thenReturn(false);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> userWithSameNumber.checkPhoneNumberIsUnique(userService));
|
||||
Assertions.assertEquals(Business.USER_PHONE_NUMBER_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
Assertions.assertDoesNotThrow(() -> userWithNewNumber.checkPhoneNumberIsUnique(userService));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkEmailIsUnique() {
|
||||
void testCheckEmailIsUnique() {
|
||||
UserModel userWithSameEmail = new UserModel();
|
||||
userWithSameEmail.setUserId(USER_ID);
|
||||
userWithSameEmail.setEmail("1@1.com");
|
||||
UserModel userWithNewNumber = new UserModel();
|
||||
userWithNewNumber.setUserId(USER_ID);
|
||||
userWithNewNumber.setEmail("2@2.com");
|
||||
|
||||
when(userService.isEmailDuplicated(eq("1@1.com"), eq(USER_ID))).thenReturn(true);
|
||||
when(userService.isEmailDuplicated(eq("2@2.com"), eq(USER_ID))).thenReturn(false);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class,
|
||||
() -> userWithSameEmail.checkEmailIsUnique(userService));
|
||||
Assertions.assertEquals(Business.USER_EMAIL_IS_NOT_UNIQUE, exception.getErrorCode());
|
||||
Assertions.assertDoesNotThrow(() -> userWithNewNumber.checkPhoneNumberIsUnique(userService));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkCanBeDelete() {
|
||||
void testCheckCanBeDeleteWhenDeleteItself() {
|
||||
UserModel userModel = new UserModel();
|
||||
userModel.setUserId(USER_ID);
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setUserId(USER_ID);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () ->userModel.checkCanBeDelete(loginUser));
|
||||
|
||||
Assertions.assertEquals(Business.USER_CURRENT_USER_CAN_NOT_BE_DELETE, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkCanBeModify() {
|
||||
void testCheckCanBeDeleteWhenDeleteAdmin() {
|
||||
UserModel userModel = new UserModel();
|
||||
long adminId = 1L;
|
||||
userModel.setUserId(adminId);
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setUserId(2L);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () ->userModel.checkCanBeDelete(loginUser));
|
||||
|
||||
Assertions.assertEquals(Business.USER_CURRENT_USER_CAN_NOT_BE_DELETE, exception.getErrorCode());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testCheckCanBeDeleteWhenSuccessful() {
|
||||
UserModel userModel = new UserModel();
|
||||
userModel.setUserId(2L);
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setUserId(USER_ID);
|
||||
|
||||
Assertions.assertDoesNotThrow(()-> userModel.checkCanBeDelete(loginUser));
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyPassword() {
|
||||
void testCheckCanBeModifyWhenFailed() {
|
||||
UserModel userModel = new UserModel();
|
||||
userModel.setUserId(USER_ID);
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setUserId(2L);
|
||||
|
||||
ApiException exception = assertThrows(ApiException.class, () -> userModel.checkCanBeModify(loginUser));
|
||||
Assertions.assertEquals(Business.UNSUPPORTED_OPERATION, exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resetPassword() {
|
||||
void testCheckCanBeModifyWhenSuccessful() {
|
||||
UserModel adminUser = new UserModel();
|
||||
adminUser.setUserId(USER_ID);
|
||||
UserModel normalUser = new UserModel();
|
||||
normalUser.setUserId(2L);
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setUserId(USER_ID);
|
||||
|
||||
Assertions.assertDoesNotThrow(()->adminUser.checkCanBeModify(loginUser));
|
||||
Assertions.assertDoesNotThrow(()->normalUser.checkCanBeModify(loginUser));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testModifyPassword() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testResetPassword() {
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,21 +89,5 @@ public class AuthenticationUtils {
|
||||
return passwordEncoder.matches(rawPassword, encodedPassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为管理员
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 结果
|
||||
*/
|
||||
public static boolean isAdmin(Long userId) {
|
||||
return userId != null && 1L == userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为管理员角色
|
||||
* @return 结果
|
||||
*/
|
||||
public static boolean isAdminRole(Long roleId) {
|
||||
return roleId != null && 1L == roleId;
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,7 +27,11 @@ public interface ISysRoleService extends IService<SysRoleEntity> {
|
||||
boolean isRoleKeyDuplicated(Long roleId, String roleKey);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 检测角色是否分配给用户
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
boolean isAssignedToUsers(Long roleId);
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user