1
0

feat: initial commit

This commit is contained in:
gin
2026-05-07 18:39:00 +08:00
commit cdee21ee8e
653 changed files with 63946 additions and 0 deletions
@@ -0,0 +1,96 @@
package com.agileboot.domain.system.config.model;
import static org.mockito.ArgumentMatchers.any;
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.domain.system.config.db.SysConfigEntity;
import com.agileboot.domain.system.config.db.SysConfigService;
import java.util.Date;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class ConfigModelTest {
private final SysConfigService configService = mock(SysConfigService.class);
private final ConfigModelFactory configModelFactory = new ConfigModelFactory(configService);
private final Long CONFIG_ID = 1L;
@Test
void testBeanUtilsCopyFunction() {
SysConfigEntity entity = getConfigEntity();
when(configService.getById(any())).thenReturn(entity);
ConfigModel configModel = configModelFactory.loadById(CONFIG_ID);
Assertions.assertEquals(entity.getConfigId(), configModel.getConfigId());
Assertions.assertEquals(entity.getConfigKey(), configModel.getConfigKey());
Assertions.assertEquals(entity.getConfigValue(), configModel.getConfigValue());
Assertions.assertEquals(entity.getConfigName(), configModel.getConfigName());
Assertions.assertEquals(entity.getConfigOptions(), configModel.getConfigOptions());
Assertions.assertEquals(entity.getIsAllowChange(), configModel.getIsAllowChange());
Assertions.assertEquals(entity.getRemark(), configModel.getRemark());
Assertions.assertEquals(entity.getCreateTime(), configModel.getCreateTime());
Assertions.assertEquals(entity.getCreatorId(), configModel.getCreatorId());
Assertions.assertEquals(2, configModel.getConfigOptionSet().size());
}
@Test
void testConfigModelConstructor() {
SysConfigEntity entity = getConfigEntity();
when(configService.getById(any())).thenReturn(entity);
ConfigModel configModel = configModelFactory.loadById(CONFIG_ID);
Assertions.assertTrue(configModel.getConfigOptionSet().contains("true"));
}
@Test
void testConfigModelConstructorWhenInvalidJSon() {
SysConfigEntity entity = getConfigEntity();
entity.setConfigOptions("{\"true\",\"false\"}");
ConfigModel invalid1 = new ConfigModel(entity, configService);
entity.setConfigOptions("\"[]\"");
ConfigModel invalid2 = new ConfigModel(entity, configService);
entity.setConfigOptions("\"xxxx\"");
ConfigModel invalid3 = new ConfigModel(entity, configService);
Assertions.assertEquals(0, invalid1.getConfigOptionSet().size());
Assertions.assertEquals(0, invalid2.getConfigOptionSet().size());
Assertions.assertEquals(0, invalid3.getConfigOptionSet().size());
}
@Test
void checkCanBeModifyWhenValueEmpty() {
ConfigModel configModel = configModelFactory.create();
configModel.setConfigValue(null);
ApiException exception1 = Assertions.assertThrows(ApiException.class, configModel::checkCanBeModify);
configModel.setConfigValue("");
ApiException exception2 = Assertions.assertThrows(ApiException.class, configModel::checkCanBeModify);
Assertions.assertEquals(Business.CONFIG_VALUE_IS_NOT_ALLOW_TO_EMPTY, exception1.getErrorCode());
Assertions.assertEquals(Business.CONFIG_VALUE_IS_NOT_ALLOW_TO_EMPTY, exception2.getErrorCode());
}
private SysConfigEntity getConfigEntity() {
SysConfigEntity entity = new SysConfigEntity();
entity.setConfigId(1);
entity.setConfigKey("testKey");
entity.setConfigName("testName");
entity.setConfigValue("testValue");
entity.setConfigOptions("[\"true\",\"false\"]");
entity.setIsAllowChange(false);
entity.setRemark("备注");
entity.setCreatorId(1L);
entity.setCreateTime(new Date());
return entity;
}
}
@@ -0,0 +1,157 @@
package com.agileboot.domain.system.dept.model;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.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.domain.system.dept.db.SysDeptEntity;
import com.agileboot.domain.system.dept.db.SysDeptService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
class DeptModelTest {
private static final Long DEPT_ID = 1L;
private static final Long PARENT_ID = 2L;
private final SysDeptService deptService = mock(SysDeptService.class);
private final DeptModelFactory deptModelFactory = new DeptModelFactory(deptService);
@Test
void testCheckDeptNameUnique() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setDeptName("dept 1");
when(
deptService.isDeptNameDuplicated(ArgumentMatchers.any(), ArgumentMatchers.any(),
ArgumentMatchers.any())).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, deptModel::checkDeptNameUnique);
Assertions.assertEquals(Business.DEPT_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
}
@Test
void testCheckParentIdConflict() {
DeptModel deptModel = deptModelFactory.create();
Long sameId = 1L;
deptModel.setDeptId(sameId);
deptModel.setParentId(sameId);
ApiException exception = assertThrows(ApiException.class, deptModel::checkParentIdConflict);
Assertions.assertEquals(Business.DEPT_PARENT_ID_IS_NOT_ALLOWED_SELF, exception.getErrorCode());
}
@Test
void testCheckHasChildDept() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setDeptId(DEPT_ID);
when(deptService.hasChildrenDept((DEPT_ID), eq(null))).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, deptModel::checkHasChildDept);
Assertions.assertEquals(Business.DEPT_EXIST_CHILD_DEPT_NOT_ALLOW_DELETE, exception.getErrorCode());
}
@Test
void testCheckDeptAssignedToUsers() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setDeptId(DEPT_ID);
when(deptService.isDeptAssignedToUsers(DEPT_ID)).thenReturn(true);
ApiException exception = assertThrows(ApiException.class,
deptModel::checkDeptAssignedToUsers);
Assertions.assertEquals(Business.DEPT_EXIST_LINK_USER_NOT_ALLOW_DELETE, exception.getErrorCode());
}
@Test
void testGenerateAncestorsWhenParentIdZero() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setParentId(0L);
deptModel.generateAncestors();
Assertions.assertEquals(deptModel.getAncestors(), deptModel.getParentId().toString());
}
@Test
void testGenerateAncestorsWhenParentDeptNotExist() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setParentId(PARENT_ID);
when(deptService.getById(PARENT_ID)).thenReturn(null);
ApiException exception = assertThrows(ApiException.class, deptModel::generateAncestors);
Assertions.assertEquals(Business.DEPT_PARENT_DEPT_NO_EXIST_OR_DISABLED, exception.getErrorCode());
}
@Test
void testGenerateAncestorsWhenParentDeptDisabled() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setParentId(PARENT_ID);
SysDeptEntity parentDept = new SysDeptEntity();
parentDept.setStatus(0);
when(deptService.getById(PARENT_ID)).thenReturn(parentDept);
ApiException exception = assertThrows(ApiException.class, deptModel::generateAncestors);
Assertions.assertEquals(Business.DEPT_PARENT_DEPT_NO_EXIST_OR_DISABLED, exception.getErrorCode());
}
@Test
void testGenerateAncestorsSuccessful() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setParentId(PARENT_ID);
SysDeptEntity parentDept = new SysDeptEntity();
parentDept.setStatus(1);
parentDept.setAncestors("1,100");
when(deptService.getById(PARENT_ID)).thenReturn(parentDept);
deptModel.generateAncestors();
Assertions.assertEquals("1,100,2", deptModel.getAncestors());
}
@Test
void testCheckStatusAllowChangeWhenDisableButHasChildDept() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setDeptId(DEPT_ID);
deptModel.setStatus(0);
when(deptService.hasChildrenDept(DEPT_ID, true)).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, deptModel::checkStatusAllowChange);
Assertions.assertEquals(Business.DEPT_STATUS_ID_IS_NOT_ALLOWED_CHANGE, exception.getErrorCode());
}
@Test
void testCheckStatusAllowChangeWhenDisableButNoChildDept() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setDeptId(DEPT_ID);
deptModel.setStatus(0);
when(deptService.hasChildrenDept(DEPT_ID, true)).thenReturn(false);
Assertions.assertDoesNotThrow(deptModel::checkStatusAllowChange);
}
@Test
void testCheckStatusAllowChangeWhenEnableAndHasChildDept() {
DeptModel deptModel = deptModelFactory.create();
deptModel.setDeptId(DEPT_ID);
deptModel.setStatus(1);
when(deptService.hasChildrenDept(DEPT_ID, true)).thenReturn(true);
Assertions.assertDoesNotThrow(deptModel::checkStatusAllowChange);
}
}
@@ -0,0 +1,96 @@
package com.agileboot.domain.system.menu.model;
import static org.junit.jupiter.api.Assertions.assertThrows;
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.domain.system.menu.db.SysMenuService;
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 SysMenuService menuService = mock(SysMenuService.class);
private final MenuModelFactory menuModelFactory = new MenuModelFactory(menuService);
@Test
void testCheckMenuNameUnique() {
MenuModel menuModel = menuModelFactory.create();
menuModel.setMenuName("menu 1");
when(
menuService.isMenuNameDuplicated(ArgumentMatchers.any(), ArgumentMatchers.any(),
ArgumentMatchers.any())).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, menuModel::checkMenuNameUnique);
Assertions.assertEquals(Business.MENU_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
}
@Test
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 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 = menuModelFactory.create();
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 = menuModelFactory.create();
menuModel.setMenuId(MENU_ID);
when(menuService.hasChildrenMenu(MENU_ID)).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, menuModel::checkHasChildMenus);
Assertions.assertEquals(Business.MENU_EXIST_CHILD_MENU_NOT_ALLOW_DELETE, exception.getErrorCode());
}
@Test
void testCheckMenuAlreadyAssignToRole() {
MenuModel menuModel = menuModelFactory.create();
menuModel.setMenuId(MENU_ID);
when(menuService.isMenuAssignToRoles(MENU_ID)).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, menuModel::checkMenuAlreadyAssignToRole);
Assertions.assertEquals(Business.MENU_ALREADY_ASSIGN_TO_ROLE_NOT_ALLOW_DELETE, exception.getErrorCode());
}
}
@@ -0,0 +1,17 @@
package com.agileboot.domain.system.monitor.dto;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class ServerInfoTest {
@Test
void testConvertFileSize() {
ServerInfo serverInfo = new ServerInfo();
Assertions.assertEquals("0.5 KB", serverInfo.convertFileSize(512L));
Assertions.assertEquals("1.5 KB", serverInfo.convertFileSize(1536L));
Assertions.assertEquals("1.5 MB", serverInfo.convertFileSize(1572864));
Assertions.assertEquals("1.5 GB", serverInfo.convertFileSize(1610612736L));
}
}
@@ -0,0 +1,44 @@
package com.agileboot.domain.system.notice.model;
import static org.junit.jupiter.api.Assertions.assertThrows;
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 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());
}
}
@@ -0,0 +1,86 @@
package com.agileboot.domain.system.post.model;
import static org.junit.jupiter.api.Assertions.assertThrows;
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.domain.system.post.db.SysPostService;
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 SysPostService postService = mock(SysPostService.class);
private final PostModelFactory postModelFactory = new PostModelFactory(postService);
private static final long POST_ID = 1L;
@AfterEach
public void clean() {
Mockito.reset(postService);
}
@Test
void testCheckCanBeDeleteWhenFailed() {
PostModel postModel = postModelFactory.create();
postModel.setPostId(POST_ID);
when(postService.isAssignedToUsers(POST_ID)).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, postModel::checkCanBeDelete);
Assertions.assertEquals(Business.POST_ALREADY_ASSIGNED_TO_USER_CAN_NOT_BE_DELETED, exception.getErrorCode());
}
@Test
void testCheckCanBeDeleteWhenSuccessful() {
PostModel postModel = postModelFactory.create();
postModel.setPostId(POST_ID);
when(postService.isAssignedToUsers(POST_ID)).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, postModel::checkCanBeDelete);
Assertions.assertEquals(Business.POST_ALREADY_ASSIGNED_TO_USER_CAN_NOT_BE_DELETED, exception.getErrorCode());
}
@Test
void testCheckPostNameUnique() {
PostModel postWithSameName = postModelFactory.create();
postWithSameName.setPostId(POST_ID);
postWithSameName.setPostName("post 1");
PostModel postWithNewName = postModelFactory.create();
postWithNewName.setPostName("post 2");
postWithNewName.setPostId(POST_ID);
when(postService.isPostNameDuplicated(POST_ID, eq("post 1"))).thenReturn(true);
when(postService.isPostNameDuplicated(POST_ID, eq("post 2"))).thenReturn(false);
ApiException exception = assertThrows(ApiException.class, postWithSameName::checkPostNameUnique);
Assertions.assertEquals(Business.POST_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
Assertions.assertDoesNotThrow(postWithNewName::checkPostNameUnique);
}
@Test
void testCheckPostCodeUnique() {
PostModel postWithSameCode = postModelFactory.create();
postWithSameCode.setPostId(POST_ID);
postWithSameCode.setPostCode("code 1");
PostModel postWithNewCode = postModelFactory.create();
postWithNewCode.setPostId(POST_ID);
postWithNewCode.setPostCode("code 2");
when(postService.isPostCodeDuplicated(POST_ID, "code 1")).thenReturn(true);
when(postService.isPostCodeDuplicated(POST_ID, "code 2")).thenReturn(false);
ApiException exception = assertThrows(ApiException.class, postWithSameCode::checkPostCodeUnique);
Assertions.assertEquals(Business.POST_CODE_IS_NOT_UNIQUE, exception.getErrorCode());
Assertions.assertDoesNotThrow(postWithNewCode::checkPostCodeUnique);
}
}
@@ -0,0 +1,104 @@
package com.agileboot.domain.system.role.model;
import static org.junit.jupiter.api.Assertions.assertThrows;
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.domain.system.role.db.SysRoleMenuService;
import com.agileboot.domain.system.role.db.SysRoleService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class RoleModelTest {
private final SysRoleService roleService = mock(SysRoleService.class);
private final SysRoleMenuService roleMenuService = mock(SysRoleMenuService.class);
private final RoleModelFactory roleModelFactory = new RoleModelFactory(roleService, roleMenuService);
private static final long ROLE_ID = 1L;
@Test
void testCheckRoleNameUnique() {
RoleModel roleWithSameName = roleModelFactory.create();
roleWithSameName.setRoleId(ROLE_ID);
roleWithSameName.setRoleName("role 1");
RoleModel roleWithNewName = roleModelFactory.create();
roleWithNewName.setRoleId(ROLE_ID);
roleWithNewName.setRoleName("role 2");
when(roleService.isRoleNameDuplicated(ROLE_ID, "role 1")).thenReturn(true);
when(roleService.isRoleNameDuplicated(ROLE_ID, "role 2")).thenReturn(false);
ApiException exception = assertThrows(ApiException.class, roleWithSameName::checkRoleNameUnique);
Assertions.assertEquals(Business.ROLE_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
Assertions.assertDoesNotThrow(roleWithNewName::checkRoleNameUnique);
}
@Test
void testCheckRoleCanBeDelete() {
RoleModel roleModel = roleModelFactory.create();
roleModel.setRoleId(ROLE_ID);
when(roleService.isAssignedToUsers(ROLE_ID)).thenReturn(true);
ApiException exception = assertThrows(ApiException.class, roleModel::checkRoleCanBeDelete);
Assertions.assertEquals(Business.ROLE_ALREADY_ASSIGN_TO_USER, exception.getErrorCode());
}
@Test
void testCheckRoleKeyUnique() {
RoleModel roleWithSameKey = roleModelFactory.create();
roleWithSameKey.setRoleId(ROLE_ID);
roleWithSameKey.setRoleKey("key 1");
RoleModel roleWithNewKey = roleModelFactory.create();
roleWithNewKey.setRoleId(ROLE_ID);
roleWithNewKey.setRoleKey("key 2");
when(roleService.isRoleKeyDuplicated(ROLE_ID, "key 1")).thenReturn(true);
when(roleService.isRoleKeyDuplicated(ROLE_ID, "key 2")).thenReturn(false);
ApiException exception = assertThrows(ApiException.class, roleWithSameKey::checkRoleKeyUnique);
Assertions.assertEquals(Business.ROLE_KEY_IS_NOT_UNIQUE, exception.getErrorCode());
Assertions.assertDoesNotThrow(roleWithNewKey::checkRoleKeyUnique);
}
@Test
void testGenerateDeptIdSetWhenNull() {
RoleModel roleModel = roleModelFactory.create();
roleModel.setDeptIds(null);
roleModel.generateDeptIdSet();
Assertions.assertEquals("", roleModel.getDeptIdSet());
}
@Test
void testGenerateDeptIdSetWhenDuplicated() {
RoleModel roleModel = roleModelFactory.create();
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 = roleModelFactory.create();
roleModel.setDeptIds(ListUtil.of(1L,2L,3L));
roleModel.generateDeptIdSet();
Assertions.assertEquals("1,2,3", roleModel.getDeptIdSet());
}
}
@@ -0,0 +1,166 @@
package com.agileboot.domain.system.user.model;
import static org.junit.jupiter.api.Assertions.assertThrows;
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.domain.system.dept.model.DeptModelFactory;
import com.agileboot.domain.system.post.model.PostModelFactory;
import com.agileboot.domain.system.role.model.RoleModelFactory;
import com.agileboot.domain.system.user.command.UpdateUserPasswordCommand;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import com.agileboot.domain.system.user.db.SysUserService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class UserModelTest {
private final SysUserService userService = mock(SysUserService.class);
private final PostModelFactory postModelFactory = mock(PostModelFactory.class);
private final DeptModelFactory deptModelFactory = mock(DeptModelFactory.class);
private final RoleModelFactory roleModelFactory = mock(RoleModelFactory.class);
private final UserModelFactory userModelFactory = new UserModelFactory(userService, postModelFactory,
deptModelFactory, roleModelFactory);
private static final long USER_ID = 1L;
private static final long ADMIN_USER_ID = 1L;
@Test
void testCheckUsernameIsUnique() {
UserModel userWithSameName = userModelFactory.create();
userWithSameName.setUserId(USER_ID);
userWithSameName.setUsername("user 1");
UserModel userWithNewName = userModelFactory.create();
userWithNewName.setUserId(USER_ID);
userWithNewName.setUsername("user 2");
when(userService.isUserNameDuplicated("user 1")).thenReturn(true);
when(userService.isUserNameDuplicated("user 2")).thenReturn(false);
ApiException exception = assertThrows(ApiException.class, userWithSameName::checkUsernameIsUnique);
Assertions.assertEquals(Business.USER_NAME_IS_NOT_UNIQUE, exception.getErrorCode());
Assertions.assertDoesNotThrow(userWithNewName::checkUsernameIsUnique);
}
@Test
void testCheckPhoneNumberIsUnique() {
UserModel userWithSameNumber = userModelFactory.create();
userWithSameNumber.setUserId(USER_ID);
userWithSameNumber.setPhoneNumber("111111");
UserModel userWithNewNumber = userModelFactory.create();
userWithNewNumber.setUserId(USER_ID);
userWithNewNumber.setPhoneNumber("222222");
when(userService.isPhoneDuplicated("111111", USER_ID)).thenReturn(true);
when(userService.isPhoneDuplicated("222222", USER_ID)).thenReturn(false);
ApiException exception = assertThrows(ApiException.class, userWithSameNumber::checkPhoneNumberIsUnique);
Assertions.assertEquals(Business.USER_PHONE_NUMBER_IS_NOT_UNIQUE, exception.getErrorCode());
Assertions.assertDoesNotThrow(userWithNewNumber::checkPhoneNumberIsUnique);
}
@Test
void testCheckEmailIsUnique() {
UserModel userWithSameEmail = userModelFactory.create();
userWithSameEmail.setUserId(USER_ID);
userWithSameEmail.setEmail("1@1.com");
UserModel userWithNewNumber = userModelFactory.create();
userWithNewNumber.setUserId(USER_ID);
userWithNewNumber.setEmail("2@2.com");
when(userService.isEmailDuplicated("1@1.com", USER_ID)).thenReturn(true);
when(userService.isEmailDuplicated("2@2.com", USER_ID)).thenReturn(false);
ApiException exception = assertThrows(ApiException.class, userWithSameEmail::checkEmailIsUnique);
Assertions.assertEquals(Business.USER_EMAIL_IS_NOT_UNIQUE, exception.getErrorCode());
Assertions.assertDoesNotThrow(userWithNewNumber::checkPhoneNumberIsUnique);
}
@Test
void testCheckCanBeDeleteWhenDeleteItself() {
UserModel userModel = userModelFactory.create();
userModel.setUserId(USER_ID);
SystemLoginUser loginUser = new SystemLoginUser();
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 testCheckCanBeDeleteWhenDeleteAdmin() {
UserModel userModel = userModelFactory.create();
long adminId = 1L;
userModel.setUserId(adminId);
SystemLoginUser loginUser = new SystemLoginUser();
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 = userModelFactory.create();
userModel.setUserId(2L);
SystemLoginUser loginUser = new SystemLoginUser();
loginUser.setUserId(ADMIN_USER_ID);
Assertions.assertDoesNotThrow(() -> userModel.checkCanBeDelete(loginUser));
}
@Test
void testModifyPasswordWhenSuccessful() {
UserModel userModel = userModelFactory.create();
userModel.setPassword("$2a$10$rb1wRoEIkLbIknREEN1LH.FGs4g0oOS5t6l5LQ793nRaFO.SPHDHy");
UpdateUserPasswordCommand passwordCommand = new UpdateUserPasswordCommand();
passwordCommand.setOldPassword("admin123");
String newPassword = "admin456";
passwordCommand.setNewPassword(newPassword);
userModel.modifyPassword(passwordCommand);
Assertions.assertTrue(AuthenticationUtils.matchesPassword(newPassword, userModel.getPassword()));
}
@Test
void testModifyPasswordWhenPasswordWrong() {
UserModel userModel = userModelFactory.create();
userModel.setPassword("$2a$10$rb1wRoEIkLbIknREEN1LH.FGs4g0oOS5t6l5LQ793nRaFO.SPHDHy");
UpdateUserPasswordCommand passwordCommand = new UpdateUserPasswordCommand();
passwordCommand.setOldPassword("admin999");
String newPassword = "admin456";
passwordCommand.setNewPassword(newPassword);
ApiException exception = assertThrows(ApiException.class, () -> userModel.modifyPassword(passwordCommand));
Assertions.assertEquals(Business.USER_PASSWORD_IS_NOT_CORRECT, exception.getErrorCode());
}
@Test
void testModifyPasswordWhenOldNewPasswordSame() {
UserModel userModel = userModelFactory.create();
userModel.setPassword("$2a$10$rb1wRoEIkLbIknREEN1LH.FGs4g0oOS5t6l5LQ793nRaFO.SPHDHy");
UpdateUserPasswordCommand passwordCommand = new UpdateUserPasswordCommand();
passwordCommand.setOldPassword("admin123");
String newPassword = "admin123";
passwordCommand.setNewPassword(newPassword);
ApiException exception = assertThrows(ApiException.class, () -> userModel.modifyPassword(passwordCommand));
Assertions.assertEquals(Business.USER_NEW_PASSWORD_IS_THE_SAME_AS_OLD, exception.getErrorCode());
}
@Test
void testResetPassword() {
UserModel userModel = userModelFactory.create();
userModel.resetPassword("admin456");
Assertions.assertTrue(AuthenticationUtils.matchesPassword("admin456", userModel.getPassword()));
}
}
@@ -0,0 +1,18 @@
package com.agileboot.integrationTest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* 集成测试配置类
* @author valarchie
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = "com.agileboot.*")
public class IntegrationTestApplication {
public static void main(String[] args) {
SpringApplication.run(IntegrationTestApplication.class, args);
}
}
@@ -0,0 +1,27 @@
package com.agileboot.integrationTest.db;
import com.agileboot.domain.system.config.db.SysConfigService;
import com.agileboot.integrationTest.IntegrationTestApplication;
import com.agileboot.common.enums.common.ConfigKeyEnum;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = IntegrationTestApplication.class)
@RunWith(SpringRunner.class)
class SysConfigServiceImplTest {
@Resource
SysConfigService configService;
@Test
void testGetConfigValueByKey() {
String configValue = configService.getConfigValueByKey(ConfigKeyEnum.CAPTCHA.getValue());
Assertions.assertFalse(Boolean.parseBoolean(configValue));
}
}
@@ -0,0 +1,68 @@
package com.agileboot.integrationTest.db;
import com.agileboot.integrationTest.IntegrationTestApplication;
import com.agileboot.domain.system.dept.db.SysDeptService;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = IntegrationTestApplication.class)
@RunWith(SpringRunner.class)
class SysDeptServiceImplTest {
@Resource
SysDeptService deptService;
@Test
@Rollback
void testIsDeptNameDuplicated() {
boolean addWithSame = deptService.isDeptNameDuplicated("AgileBoot科技", null, null);
boolean updateWithSame = deptService.isDeptNameDuplicated("AgileBoot科技", 1L, null);
boolean addSameInParent = deptService.isDeptNameDuplicated("深圳总公司", null, 1L);
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertTrue(addSameInParent);
}
@Test
@Rollback
void testHasChildDept() {
boolean hasChild = deptService.hasChildrenDept(3L, null);
boolean hasDisableChild = deptService.hasChildrenDept(3L, false);
Assertions.assertTrue(hasChild);
Assertions.assertTrue(hasDisableChild);
}
@Test
@Rollback
void testIsChildOfTheDept() {
boolean isIndirectChild = deptService.isChildOfTheDept(1L, 10L);
boolean isDirectChild = deptService.isChildOfTheDept(3L, 10L);
boolean isNotChild = deptService.isChildOfTheDept(5L, 10L);
Assertions.assertTrue(isIndirectChild);
Assertions.assertTrue(isDirectChild);
Assertions.assertFalse(isNotChild);
}
@Test
@Rollback
void testIsDeptAssignedToUsers() {
boolean notAssigned = deptService.isDeptAssignedToUsers(1L);
boolean isAssigned = deptService.isDeptAssignedToUsers(4L);
Assertions.assertFalse(notAssigned);
Assertions.assertTrue(isAssigned);
}
}
@@ -0,0 +1,78 @@
package com.agileboot.integrationTest.db;
import cn.hutool.core.collection.CollUtil;
import com.agileboot.integrationTest.IntegrationTestApplication;
import com.agileboot.domain.system.menu.db.SysMenuEntity;
import com.agileboot.domain.system.menu.db.SysMenuService;
import java.util.List;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = IntegrationTestApplication.class)
@RunWith(SpringRunner.class)
class SysMenuServiceImplTest {
@Resource
SysMenuService menuService;
@Test
@Rollback
void testGetMenuListByUserId() {
List<SysMenuEntity> menusMissingLastMenu = menuService.getMenuListByUserId(2L);
List<SysMenuEntity> allMenus = menuService.list();
Assertions.assertEquals(allMenus.size(), menusMissingLastMenu.size() + 1);
}
@Test
@Rollback
void testGetMenuIdsByRoleId() {
List<Long> menusMissingLastMenu = menuService.getMenuIdsByRoleId(2L);
List<SysMenuEntity> allMenus = menuService.list();
Assertions.assertEquals(allMenus.size(), menusMissingLastMenu.size() + 1);
}
@Test
@Rollback
void testIsMenuNameDuplicated() {
boolean addWithSame = menuService.isMenuNameDuplicated("用户管理", null, 1L);
boolean updateWithSame = menuService.isMenuNameDuplicated("用户管理", 5L, 1L);
boolean addWithoutSame = menuService.isMenuNameDuplicated("用户管理", null, 2L);
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertFalse(addWithoutSame);
}
@Test
@Rollback
void testHasChildrenMenus() {
boolean hasChildrenMenu = menuService.hasChildrenMenu(5L);
boolean hasNotChildrenMenu = menuService.hasChildrenMenu(20L);
Assertions.assertTrue(hasChildrenMenu);
Assertions.assertFalse(hasNotChildrenMenu);
}
@Test
@Rollback
void testIsMenuAssignToRole() {
List<SysMenuEntity> allMenus = menuService.list();
boolean isAssignToRole = menuService.isMenuAssignToRoles(CollUtil.getFirst(allMenus).getMenuId());
// role2 默认不给最后一个权限 所以最后一个菜单无权限
boolean isNotAssignToRole = menuService.isMenuAssignToRoles(CollUtil.getLast(allMenus).getMenuId());
Assertions.assertFalse(isNotAssignToRole);
Assertions.assertTrue(isAssignToRole);
}
}
@@ -0,0 +1,57 @@
package com.agileboot.integrationTest.db;
import com.agileboot.integrationTest.IntegrationTestApplication;
import com.agileboot.domain.system.post.db.SysPostService;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = IntegrationTestApplication.class)
@RunWith(SpringRunner.class)
class SysPostServiceImplTest {
@Resource
SysPostService postService;
@Test
@Rollback
void testIsPostNameDuplicated() {
boolean addWithSame = postService.isPostNameDuplicated(null, "董事长");
boolean updateWithSame = postService.isPostNameDuplicated(1L, "董事长");
boolean addWithoutSame = postService.isPostNameDuplicated(null, "董事长1");
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertFalse(addWithoutSame);
}
@Test
@Rollback
void testIsPostCodeDuplicated() {
boolean addWithSame = postService.isPostCodeDuplicated(null, "ceo");
boolean updateWithSame = postService.isPostCodeDuplicated(1L, "ceo");
boolean addWithoutSame = postService.isPostCodeDuplicated(null, "ceo1");
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertFalse(addWithoutSame);
}
@Test
@Rollback
void testIsAssignedToUsers() {
boolean assignedPost = postService.isAssignedToUsers(1L);
boolean unassignedPost = postService.isAssignedToUsers(3L);
Assertions.assertTrue(assignedPost);
Assertions.assertFalse(unassignedPost);
}
}
@@ -0,0 +1,58 @@
package com.agileboot.integrationTest.db;
import com.agileboot.integrationTest.IntegrationTestApplication;
import com.agileboot.domain.system.role.db.SysRoleService;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = IntegrationTestApplication.class)
@RunWith(SpringRunner.class)
class SysRoleServiceImplTest {
@Resource
SysRoleService roleService;
@Test
@Rollback
void testIsRoleNameDuplicated() {
boolean addWithSame = roleService.isRoleNameDuplicated(null, "超级管理员");
boolean updateWithSame = roleService.isRoleNameDuplicated(1L, "超级管理员");
boolean addWithoutSame = roleService.isRoleNameDuplicated(null, "超级管理员1");
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertFalse(addWithoutSame);
}
@Test
@Rollback
void testIsRoleKeyDuplicated() {
boolean addWithSame = roleService.isRoleKeyDuplicated(null, "admin");
boolean updateWithSame = roleService.isRoleKeyDuplicated(1L, "admin");
boolean addWithoutSame = roleService.isRoleKeyDuplicated(null, "admin1");
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertFalse(addWithoutSame);
}
@Test
@Rollback
void testIsAssignedToUsers() {
boolean assignedRole = roleService.isAssignedToUsers(1L);
boolean unassignedRole = roleService.isAssignedToUsers(3L);
Assertions.assertTrue(assignedRole);
Assertions.assertFalse(unassignedRole);
}
}
@@ -0,0 +1,148 @@
package com.agileboot.integrationTest.db;
import com.agileboot.domain.system.role.query.AllocatedRoleQuery;
import com.agileboot.domain.system.role.query.UnallocatedRoleQuery;
import com.agileboot.domain.system.user.query.SearchUserQuery;
import com.agileboot.integrationTest.IntegrationTestApplication;
import com.agileboot.domain.system.menu.db.SysMenuEntity;
import com.agileboot.domain.system.post.db.SysPostEntity;
import com.agileboot.domain.system.role.db.SysRoleEntity;
import com.agileboot.domain.system.user.db.SysUserEntity;
import com.agileboot.domain.system.user.db.SearchUserDO;
import com.agileboot.domain.system.menu.db.SysMenuService;
import com.agileboot.domain.system.user.db.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = IntegrationTestApplication.class)
@RunWith(SpringRunner.class)
class SysUserServiceImplTest {
@Resource
SysUserService userService;
@Resource
SysMenuService menuService;
@Test
@Rollback
void testIsUserNameDuplicated() {
boolean isDuplicated = userService.isUserNameDuplicated("admin");
Assertions.assertTrue(isDuplicated);
}
@Test
@Rollback
void testIsPhoneDuplicated() {
boolean addWithSame = userService.isPhoneDuplicated("15888888889", null);
boolean updateWithSame = userService.isPhoneDuplicated("15888888889", 1L);
boolean addWithoutSame = userService.isPhoneDuplicated("15888888899", null);
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertFalse(addWithoutSame);
}
@Test
@Rollback
void testIsEmailDuplicated() {
boolean addWithSame = userService.isEmailDuplicated("agileboot@163.com", null);
boolean updateWithSame = userService.isEmailDuplicated("agileboot@163.com", 1L);
boolean addWithoutSame = userService.isEmailDuplicated("agileboot@164.com", null);
Assertions.assertTrue(addWithSame);
Assertions.assertFalse(updateWithSame);
Assertions.assertFalse(addWithoutSame);
}
@Test
@Rollback
void testGetRoleOfUser() {
SysRoleEntity roleOfUser = userService.getRoleOfUser(1L);
Assertions.assertEquals(1L, roleOfUser.getRoleId());
Assertions.assertEquals("超级管理员", roleOfUser.getRoleName());
Assertions.assertEquals("admin", roleOfUser.getRoleKey());
}
@Test
@Rollback
void testGetPostOfUser() {
SysPostEntity postOfUser = userService.getPostOfUser(1L);
Assertions.assertEquals(1L, postOfUser.getPostId());
Assertions.assertEquals("董事长", postOfUser.getPostName());
Assertions.assertEquals("ceo", postOfUser.getPostCode());
}
@Test
@Rollback
void testGetMenuPermissionsForUsers() {
Set<String> permissionByUser = userService.getMenuPermissionsForUser(2L);
List<SysMenuEntity> allMenus = menuService.list();
Set<String> allPermissions = allMenus.stream().map(SysMenuEntity::getPermission).collect(Collectors.toSet());
Assertions.assertEquals(allPermissions.size() - 1, permissionByUser.size());
}
@Test
@Rollback
void testGetUserByUserName() {
SysUserEntity admin = userService.getUserByUserName("admin");
Assertions.assertEquals(1L, admin.getUserId());
Assertions.assertEquals(1L, admin.getRoleId());
Assertions.assertEquals(1L, admin.getPostId());
}
@Test
@Rollback
void testGetRoleAssignedUserList() {
AllocatedRoleQuery allocatedRoleQuery = new AllocatedRoleQuery();
allocatedRoleQuery.setRoleId(1L);
allocatedRoleQuery.setUsername("admin");
Page<SysUserEntity> roleAssignedPage = userService.getUserListByRole(allocatedRoleQuery);
Assertions.assertEquals(1, roleAssignedPage.getTotal());
Assertions.assertEquals("admin", roleAssignedPage.getRecords().get(0).getUsername());
}
@Test
@Rollback
void testGetRoleUnassignedUserList() {
UnallocatedRoleQuery unallocatedRoleQuery = new UnallocatedRoleQuery();
unallocatedRoleQuery.setRoleId(3L);
unallocatedRoleQuery.setUsername("ag2");
Page<SysUserEntity> roleAssignedPage = userService.getUserListByRole(unallocatedRoleQuery);
Assertions.assertEquals(1, roleAssignedPage.getTotal());
Assertions.assertEquals("ag2", roleAssignedPage.getRecords().get(0).getUsername());
}
@Test
@Rollback
void testGetUserList() {
SearchUserQuery<SearchUserDO> searchUserQuery = new SearchUserQuery<>();
searchUserQuery.setUsername("a");
Page<SearchUserDO> userList = userService.getUserList(searchUserQuery);
Assertions.assertEquals(3, userList.getTotal());
Assertions.assertEquals("admin", userList.getRecords().get(0).getUsername());
}
}
@@ -0,0 +1,53 @@
# 数据源配置
spring:
datasource:
# 驱动
driver-class-name: org.h2.Driver
dynamic:
primary: master
strict: false
datasource:
master:
# h2 内存数据库 内存模式连接配置 库名: agileboot
url: jdbc:h2:mem:agileboot;DB_CLOSE_DELAY=-1
h2:
# 开启console 访问 默认false
console:
enabled: true
settings:
# 开启h2 console 跟踪 方便调试 默认 false
trace: true
# 允许console 远程访问 默认false
web-allow-others: true
# h2 访问路径上下文
path: /h2-console
sql:
init:
platform: mysql
# 初始化数据
schema-locations: classpath:h2sql/agileboot_schema.sql
data-locations: classpath:h2sql/agileboot_data.sql
# redis 配置
redis:
# 地址
host: localhost
# 端口,默认为6379
port: 36379
# 数据库索引
database: 0
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
@@ -0,0 +1,18 @@
# Spring配置
spring:
profiles:
active: basic,test
# 如果需要无Mysql 无Redis直接启动的话 可以将这个参数置为true, 并且spring.profile.active: dev换成test
agileboot:
embedded:
mysql: true
redis: false