diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java new file mode 100644 index 000000000..ddb8832e3 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImplTest.java @@ -0,0 +1,170 @@ +package cn.iocoder.dashboard.modules.system.service.user; + +import cn.hutool.core.util.RandomUtil; +import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.common.enums.CommonStatusEnum; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportExcelVO; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserImportRespVO; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; +import cn.iocoder.dashboard.modules.system.dal.dataobject.dept.SysDeptDO; +import cn.iocoder.dashboard.modules.system.dal.mysql.dept.SysDeptMapper; +import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService; +import cn.iocoder.dashboard.util.RandomUtils; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Import; +import org.springframework.security.crypto.password.PasswordEncoder; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; +import static cn.iocoder.dashboard.util.RandomUtils.randomString; +import static org.junit.jupiter.api.Assertions.*; + +/** + * @Author zxl + * @Date 2021/3/6 14:28 + * @Desc + */ +@Import(PasswordEncoder.class) +public class SysUserServiceImplTest extends BaseSpringBootUnitTest { + + @Autowired + private SysUserService userService; + @Autowired + private PasswordEncoder passwordEncoder; + @Autowired + private SysDeptService deptService; + @Resource + private SysDeptMapper deptMapper; + + + @Test + public void test_creatUser(){ + // 准备参数 + SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{ + o.setDeptId(null); + o.setPostIds(Collections.emptySet()); + o.setSex(1); + o.setPassword(randomString()); + }); + // 调用方法 + Long userId = userService.createUser(reqVO); + // 校验数据是否存在 + assertNotNull(userService.getUser(userId)); + } + + @Test + public void test_updateUser(){ + // 准备参数 + SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{ + o.setDeptId(null); + o.setPostIds(Collections.emptySet()); + o.setSex(1); + o.setPassword(randomString()); + }); + // 先插入一条 + Long userId = userService.createUser(reqVO); + // 准备更新参数:更新手机号 + SysUserUpdateReqVO updateVo = new SysUserUpdateReqVO(); + updateVo.setId(userId); + updateVo.setMobile(RandomUtil.randomNumbers(11)); + // 调用方法、 + userService.updateUser(updateVo); + // 校验结果 + assertEquals(userService.getUser(userId).getMobile(),updateVo.getMobile()); + + } + + @Test + public void test_deleteUser(){ + // 准备参数 + SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{ + o.setDeptId(null); + o.setPostIds(Collections.emptySet()); + o.setSex(1); + o.setPassword(randomString()); + }); + // 先插入一条 + Long userId = userService.createUser(reqVO); + // 调用数据 + userService.deleteUser(userId); + // 校验结果 + assertNull(userService.getUser(userId)); + } + + @Test + public void test_updateUserPassword(){ + // 准备参数 + SysUserCreateReqVO reqVO = randomPojo(SysUserCreateReqVO.class,o->{ + o.setDeptId(null); + o.setPostIds(Collections.emptySet()); + o.setSex(1); + o.setPassword("123"); + }); + // 先插入一条 + Long userId = userService.createUser(reqVO); + String newPassword = RandomUtils.randomString(); + // 调用 + userService.updateUserPassword(userId,newPassword); + // 校验结果 + assertNotEquals(passwordEncoder.encode(newPassword),userService.getUser(userId).getPassword()); + } + + @Test + public void test_importUsers(){ + SysDeptDO dept = randomPojo(SysDeptDO.class, o -> { // 等会查询到 + o.setName("开发部"); + o.setSort("1"); + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); + }); + int depId = deptMapper.insert(dept); + // 准备参数 + List list = new ArrayList<>(); + list.add(randomPojo(SysUserImportExcelVO.class, o->{ + o.setDeptId(dept.getId()); + o.setSex(1); + o.setStatus(1); + })); + list.add(randomPojo(SysUserImportExcelVO.class, o->{ + o.setDeptId(dept.getId()); + o.setSex(1); + o.setStatus(1); + })); + list.add(randomPojo(SysUserImportExcelVO.class, o->{ + o.setDeptId(dept.getId()); + o.setSex(1); + o.setStatus(1); + })); + // 批量插入 + SysUserImportRespVO respVO = userService.importUsers(list,false); + System.out.println(respVO.getCreateUsernames().size()); + // 校验结果 + assertEquals(respVO.getCreateUsernames().size(),3); + // 批量更新 + list.get(0).setSex(0); + SysUserImportRespVO respVOUpdate = userService.importUsers(list,true); + System.out.println(respVOUpdate.getUpdateUsernames().size()); + // 校验结果 + assertEquals(respVOUpdate.getUpdateUsernames().size(),3); + } + + public SysUserCreateReqVO randomUserVO(){ + + SysUserCreateReqVO userVO = new SysUserCreateReqVO(); + userVO.setUsername(RandomUtils.randomString()); + userVO.setNickname(RandomUtils.randomString()); + userVO.setMobile(RandomUtil.randomNumbers(11)); + userVO.setEmail(RandomUtils.randomString()+"@ruoyi.com"); + userVO.setDeptId(null); + userVO.setPostIds(Collections.emptySet()); + + userVO.setPassword(RandomUtils.randomString()); + return userVO; + + } +} diff --git a/src/test/resources/sql/clean.sql b/src/test/resources/sql/clean.sql index 2887b4e9d..9319845ff 100644 --- a/src/test/resources/sql/clean.sql +++ b/src/test/resources/sql/clean.sql @@ -7,3 +7,4 @@ DELETE FROM "sys_dict_data"; DELETE FROM "sys_role"; DELETE FROM "sys_role_menu"; DELETE FROM "sys_menu"; +delete from "sys_user" \ No newline at end of file diff --git a/src/test/resources/sql/create_tables.sql b/src/test/resources/sql/create_tables.sql index 183554519..dae945874 100644 --- a/src/test/resources/sql/create_tables.sql +++ b/src/test/resources/sql/create_tables.sql @@ -100,3 +100,26 @@ CREATE TABLE IF NOT EXISTS "sys_menu" ( "deleted" bit NOT NULL DEFAULT FALSE, PRIMARY KEY ("id") ) COMMENT '菜单权限表'; + +CREATE TABLE IF NOT EXISTS `sys_user` ( +`id` bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '用户ID', +`username` varchar(30) NOT NULL COMMENT '用户账号', +`password` varchar(100) NOT NULL DEFAULT '' COMMENT '密码', +`nickname` varchar(30) NOT NULL COMMENT '用户昵称', +`remark` varchar(500) DEFAULT NULL COMMENT '备注', +`dept_id` bigint(20) DEFAULT NULL COMMENT '部门ID', +`post_ids` varchar(255) DEFAULT NULL COMMENT '岗位编号数组', +`email` varchar(50) DEFAULT '' COMMENT '用户邮箱', +`mobile` varchar(11) DEFAULT '' COMMENT '手机号码', +`sex` tinyint(4) DEFAULT '0' COMMENT '用户性别', +`avatar` varchar(100) DEFAULT '' COMMENT '头像地址', +`status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '帐号状态(0正常 1停用)', +`login_ip` varchar(50) DEFAULT '' COMMENT '最后登录IP', +`login_date` datetime DEFAULT NULL COMMENT '最后登录时间', +`create_by` varchar(64) DEFAULT '' COMMENT '创建者', +`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', +`update_by` varchar(64) DEFAULT '' COMMENT '更新者', +`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', +`deleted` bit NOT NULL DEFAULT FALSE, +PRIMARY KEY (`id`) +) COMMENT='用户信息表'; \ No newline at end of file