Commit f4a5a139b5719dbcde750fbb2f97de15bbda10cf
1 parent
5193de7c
[update]user server模块新增测试,修改部分接口规范问题
Showing
21 changed files
with
279 additions
and
45 deletions
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/controller/file/InfFileController.java
| ... | ... | @@ -2,7 +2,7 @@ package cn.iocoder.yudao.adminserver.modules.infra.controller.file; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.io.IoUtil; |
| 4 | 4 | import cn.iocoder.yudao.adminserver.modules.infra.service.file.InfFileService; |
| 5 | -import cn.iocoder.yudao.coreservice.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 5 | +import cn.iocoder.yudao.adminserver.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 6 | 6 | import cn.iocoder.yudao.coreservice.modules.infra.controller.file.vo.InfFileRespVO; |
| 7 | 7 | import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/infra/controller/file/vo/InfFilePageReqVO.java renamed to yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/controller/file/vo/InfFilePageReqVO.java
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/dal/mysql/file/InfFileMapper.java
0 → 100644
| 1 | +package cn.iocoder.yudao.adminserver.modules.infra.dal.mysql.file; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.adminserver.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 4 | +import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO; | |
| 5 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 6 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 7 | +import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; | |
| 8 | +import org.apache.ibatis.annotations.Mapper; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * admin 文件操作 Mapper | |
| 12 | + * | |
| 13 | + * @author 芋道源码 | |
| 14 | + */ | |
| 15 | +@Mapper | |
| 16 | +public interface InfFileMapper extends BaseMapperX<InfFileDO> { | |
| 17 | + default PageResult<InfFileDO> selectPage(InfFilePageReqVO reqVO) { | |
| 18 | + return selectPage(reqVO, new QueryWrapperX<InfFileDO>() | |
| 19 | + .likeIfPresent("id", reqVO.getId()) | |
| 20 | + .likeIfPresent("type", reqVO.getType()) | |
| 21 | + .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) | |
| 22 | + .orderByDesc("create_time")); | |
| 23 | + } | |
| 24 | +} | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileService.java
| 1 | 1 | package cn.iocoder.yudao.adminserver.modules.infra.service.file; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.coreservice.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 3 | +import cn.iocoder.yudao.adminserver.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 4 | 4 | import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 6 | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/impl/InfFileServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.adminserver.modules.infra.service.file.impl; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.adminserver.modules.infra.dal.mysql.file.InfFileMapper; | |
| 3 | 4 | import cn.iocoder.yudao.adminserver.modules.infra.service.file.InfFileService; |
| 4 | -import cn.iocoder.yudao.coreservice.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 5 | +import cn.iocoder.yudao.adminserver.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 5 | 6 | import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO; |
| 6 | -import cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.file.InfFileCoreMapper; | |
| 7 | 7 | import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 9 | 9 | import org.springframework.stereotype.Service; |
| ... | ... | @@ -17,8 +17,9 @@ import javax.annotation.Resource; |
| 17 | 17 | */ |
| 18 | 18 | @Service |
| 19 | 19 | public class InfFileServiceImpl implements InfFileService { |
| 20 | + | |
| 20 | 21 | @Resource |
| 21 | - private InfFileCoreMapper fileCoreMapper; | |
| 22 | + private InfFileMapper fileMapper; | |
| 22 | 23 | |
| 23 | 24 | @Resource |
| 24 | 25 | private InfFileCoreService fileCoreService; |
| ... | ... | @@ -40,7 +41,7 @@ public class InfFileServiceImpl implements InfFileService { |
| 40 | 41 | |
| 41 | 42 | @Override |
| 42 | 43 | public PageResult<InfFileDO> getFilePage(InfFilePageReqVO pageReqVO) { |
| 43 | - return fileCoreMapper.selectPage(pageReqVO); | |
| 44 | + return fileMapper.selectPage(pageReqVO); | |
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | } | ... | ... |
yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java
| ... | ... | @@ -2,7 +2,7 @@ package cn.iocoder.yudao.adminserver.modules.infra.service.file; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.io.resource.ResourceUtil; |
| 4 | 4 | import cn.iocoder.yudao.adminserver.BaseDbUnitTest; |
| 5 | -import cn.iocoder.yudao.coreservice.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 5 | +import cn.iocoder.yudao.adminserver.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 6 | 6 | import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO; |
| 7 | 7 | import cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.file.InfFileCoreMapper; |
| 8 | 8 | import cn.iocoder.yudao.coreservice.modules.infra.service.file.impl.InfFileCoreServiceImpl; | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/infra/dal/mysql/file/InfFileCoreMapper.java
| 1 | 1 | package cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.file; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.coreservice.modules.infra.controller.file.vo.InfFilePageReqVO; | |
| 4 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 5 | 3 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 6 | 4 | import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO; |
| 7 | -import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; | |
| 8 | 5 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 6 | |
| 10 | 7 | @Mapper |
| ... | ... | @@ -12,13 +9,4 @@ public interface InfFileCoreMapper extends BaseMapperX<InfFileDO> { |
| 12 | 9 | default Integer selectCountById(String id) { |
| 13 | 10 | return selectCount("id", id); |
| 14 | 11 | } |
| 15 | - | |
| 16 | - default PageResult<InfFileDO> selectPage(InfFilePageReqVO reqVO) { | |
| 17 | - return selectPage(reqVO, new QueryWrapperX<InfFileDO>() | |
| 18 | - .likeIfPresent("id", reqVO.getId()) | |
| 19 | - .likeIfPresent("type", reqVO.getType()) | |
| 20 | - .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) | |
| 21 | - .orderByDesc("create_time")); | |
| 22 | - } | |
| 23 | - | |
| 24 | 12 | } | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -14,7 +14,7 @@ public interface SysErrorCodeConstants { |
| 14 | 14 | ErrorCode SMS_SEND_MOBILE_TEMPLATE_PARAM_MISS = new ErrorCode(1006000001, "模板参数({})缺失"); |
| 15 | 15 | ErrorCode SMS_SEND_TEMPLATE_NOT_EXISTS = new ErrorCode(1006000000, "短信模板不存在"); |
| 16 | 16 | |
| 17 | - // 文件相关 | |
| 17 | + // ========= 文件相关 1006001000================= | |
| 18 | 18 | ErrorCode FILE_PATH_EXISTS = new ErrorCode(1006001000, "文件路径已存在"); |
| 19 | 19 | ErrorCode FILE_NOT_EXISTS = new ErrorCode(1006001002, "文件不存在"); |
| 20 | 20 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/controller/user/SysUserProfileController.http
| ... | ... | @@ -3,9 +3,9 @@ GET {{userServerUrl}}/system/user/profile/get |
| 3 | 3 | Authorization: Bearer test245 |
| 4 | 4 | |
| 5 | 5 | ### 请求 /system/user/profile/revise-nickname 接口 成功 |
| 6 | -PUT http://localhost:28080/api/system/user/profile/update-nickname?nickName=yunai222 | |
| 6 | +PUT {{userServerUrl}}/system/user/profile/update-nickname?nickName=yunai222 | |
| 7 | 7 | Authorization: Bearer test245 |
| 8 | 8 | |
| 9 | 9 | ### 请求 /system/user/profile/get-user-info 接口 成功 |
| 10 | -GET http://localhost:28080/api/system/user/profile/get-user-info?id=245 | |
| 10 | +GET {{userServerUrl}}/system/user/profile/get-user-info?id=245 | |
| 11 | 11 | Authorization: Bearer test245 |
| 12 | 12 | \ No newline at end of file | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/controller/user/SysUserProfileController.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.member.controller.user; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.SysUserInfoRespVO; | |
| 3 | +import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfoRespVO; | |
| 4 | 4 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 6 | 6 | import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; |
| ... | ... | @@ -30,13 +30,6 @@ public class SysUserProfileController { |
| 30 | 30 | @Resource |
| 31 | 31 | private MbrUserService userService; |
| 32 | 32 | |
| 33 | - @GetMapping("/get") | |
| 34 | - @ApiOperation("获得登录用户信息") | |
| 35 | - @PreAuthenticated | |
| 36 | - public CommonResult<Boolean> profile() { | |
| 37 | - return null; | |
| 38 | - } | |
| 39 | - | |
| 40 | 33 | @PutMapping("/update-nickname") |
| 41 | 34 | @ApiOperation("修改用户昵称") |
| 42 | 35 | @PreAuthenticated |
| ... | ... | @@ -45,21 +38,21 @@ public class SysUserProfileController { |
| 45 | 38 | return success(true); |
| 46 | 39 | } |
| 47 | 40 | |
| 48 | - @PutMapping("/revise-avatar") | |
| 41 | + @PutMapping("/update-avatar") | |
| 49 | 42 | @ApiOperation("修改用户头像") |
| 50 | 43 | @PreAuthenticated |
| 51 | - public CommonResult<String> reviseAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException { | |
| 44 | + public CommonResult<String> updateAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException { | |
| 52 | 45 | if (file.isEmpty()) { |
| 53 | 46 | throw ServiceExceptionUtil.exception(FILE_IS_EMPTY); |
| 54 | 47 | } |
| 55 | - String avatar = userService.reviseAvatar(getLoginUserId(), file.getInputStream()); | |
| 48 | + String avatar = userService.updateAvatar(getLoginUserId(), file.getInputStream()); | |
| 56 | 49 | return success(avatar); |
| 57 | 50 | } |
| 58 | 51 | |
| 59 | 52 | @GetMapping("/get-user-info") |
| 60 | 53 | @ApiOperation("获取用户头像与昵称") |
| 61 | 54 | @PreAuthenticated |
| 62 | - public CommonResult<SysUserInfoRespVO> getUserInfo() { | |
| 55 | + public CommonResult<MbrUserInfoRespVO> getUserInfo() { | |
| 63 | 56 | return success(userService.getUserInfo(getLoginUserId())); |
| 64 | 57 | } |
| 65 | 58 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/controller/user/vo/SysUserInfoRespVO.java renamed to yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/controller/user/vo/MbrUserInfoRespVO.java
| ... | ... | @@ -10,7 +10,7 @@ import lombok.NoArgsConstructor; |
| 10 | 10 | @Data |
| 11 | 11 | @NoArgsConstructor |
| 12 | 12 | @AllArgsConstructor |
| 13 | -public class SysUserInfoRespVO { | |
| 13 | +public class MbrUserInfoRespVO { | |
| 14 | 14 | |
| 15 | 15 | @ApiModelProperty(value = "用户昵称", required = true, example = "芋艿") |
| 16 | 16 | private String nickName; | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/enums/MbrErrorCodeConstants.java
| ... | ... | @@ -9,9 +9,9 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode; |
| 9 | 9 | */ |
| 10 | 10 | public interface MbrErrorCodeConstants { |
| 11 | 11 | |
| 12 | - // ==========用户相关============ | |
| 12 | + // ==========用户相关 1004001000============ | |
| 13 | 13 | ErrorCode USER_NOT_EXISTS = new ErrorCode(1004001000, "用户不存在"); |
| 14 | 14 | |
| 15 | - // ==========文件相关 =========== | |
| 15 | + // ==========文件相关 1004002000 =========== | |
| 16 | 16 | ErrorCode FILE_IS_EMPTY = new ErrorCode(1004002000, "文件为空"); |
| 17 | 17 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/user/MbrUserService.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.member.service.user; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; |
| 4 | -import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.SysUserInfoRespVO; | |
| 4 | +import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfoRespVO; | |
| 5 | 5 | import cn.iocoder.yudao.framework.common.validation.Mobile; |
| 6 | 6 | |
| 7 | 7 | import java.io.InputStream; |
| ... | ... | @@ -60,13 +60,13 @@ public interface MbrUserService { |
| 60 | 60 | * @param inputStream 头像文件 |
| 61 | 61 | * @return 头像url |
| 62 | 62 | */ |
| 63 | - String reviseAvatar(Long userId, InputStream inputStream); | |
| 63 | + String updateAvatar(Long userId, InputStream inputStream); | |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * 根据用户id,获取用户头像与昵称 |
| 67 | 67 | * @param userId 用户id |
| 68 | 68 | * @return 用户响应实体类 |
| 69 | 69 | */ |
| 70 | - SysUserInfoRespVO getUserInfo(Long userId); | |
| 70 | + MbrUserInfoRespVO getUserInfo(Long userId); | |
| 71 | 71 | |
| 72 | 72 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/user/impl/MbrUserServiceImpl.java
| ... | ... | @@ -4,7 +4,7 @@ import cn.hutool.core.io.IoUtil; |
| 4 | 4 | import cn.hutool.core.util.IdUtil; |
| 5 | 5 | import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService; |
| 6 | 6 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; |
| 7 | -import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.SysUserInfoRespVO; | |
| 7 | +import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfoRespVO; | |
| 8 | 8 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 9 | 9 | import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; |
| 10 | 10 | import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; |
| ... | ... | @@ -93,7 +93,7 @@ public class MbrUserServiceImpl implements MbrUserService { |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | @Override |
| 96 | - public String reviseAvatar(Long userId, InputStream avatarFile) { | |
| 96 | + public String updateAvatar(Long userId, InputStream avatarFile) { | |
| 97 | 97 | this.checkUserExists(userId); |
| 98 | 98 | // 创建文件 |
| 99 | 99 | String avatar = fileCoreService.createFile(IdUtil.fastUUID(), IoUtil.readBytes(avatarFile)); |
| ... | ... | @@ -107,9 +107,9 @@ public class MbrUserServiceImpl implements MbrUserService { |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | @Override |
| 110 | - public SysUserInfoRespVO getUserInfo(Long userId) { | |
| 110 | + public MbrUserInfoRespVO getUserInfo(Long userId) { | |
| 111 | 111 | MbrUserDO user = this.checkUserExists(userId); |
| 112 | - SysUserInfoRespVO userResp = new SysUserInfoRespVO(); | |
| 112 | + MbrUserInfoRespVO userResp = new MbrUserInfoRespVO(); | |
| 113 | 113 | userResp.setNickName(user.getNickname()); |
| 114 | 114 | userResp.setAvatar(user.getAvatar()); |
| 115 | 115 | return userResp; | ... | ... |
yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/BaseDbUnitTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.userserver; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; | |
| 4 | +import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; | |
| 5 | +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; | |
| 6 | +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; | |
| 7 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | |
| 8 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; | |
| 9 | +import org.springframework.boot.test.context.SpringBootTest; | |
| 10 | +import org.springframework.context.annotation.Import; | |
| 11 | +import org.springframework.test.context.ActiveProfiles; | |
| 12 | +import org.springframework.test.context.jdbc.Sql; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * 依赖内存 DB 的单元测试 | |
| 16 | + * | |
| 17 | + * 注意,Service 层同样适用。对于 Service 层的单元测试,我们针对自己模块的 Mapper 走的是 H2 内存数据库,针对别的模块的 Service 走的是 Mock 方法 | |
| 18 | + * | |
| 19 | + * @author 芋道源码 | |
| 20 | + */ | |
| 21 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) | |
| 22 | +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 | |
| 23 | +@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB | |
| 24 | +public class BaseDbUnitTest { | |
| 25 | + | |
| 26 | + @Import({ | |
| 27 | + // DB 配置类 | |
| 28 | + YudaoDataSourceAutoConfiguration.class, // 自己的 DB 配置类 | |
| 29 | + DataSourceAutoConfiguration.class, // Spring DB 自动配置类 | |
| 30 | + DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 | |
| 31 | + DruidDataSourceAutoConfigure.class, // Druid 自动配置类 | |
| 32 | + // MyBatis 配置类 | |
| 33 | + YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 | |
| 34 | + MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | |
| 35 | + }) | |
| 36 | + public static class Application { | |
| 37 | + } | |
| 38 | + | |
| 39 | +} | ... | ... |
yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/service/MbrUserServiceImplTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.userserver.modules.member.service; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService; | |
| 4 | +import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; | |
| 5 | +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; | |
| 6 | +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | |
| 7 | +import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | |
| 8 | +import cn.iocoder.yudao.userserver.BaseDbUnitTest; | |
| 9 | +import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfoRespVO; | |
| 10 | +import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; | |
| 11 | +import cn.iocoder.yudao.userserver.modules.member.service.user.impl.MbrUserServiceImpl; | |
| 12 | +import org.junit.jupiter.api.Test; | |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; | |
| 14 | +import org.springframework.context.annotation.Import; | |
| 15 | +import org.springframework.http.MediaType; | |
| 16 | +import org.springframework.mock.web.MockMultipartFile; | |
| 17 | +import org.springframework.security.crypto.password.PasswordEncoder; | |
| 18 | +import org.springframework.util.Assert; | |
| 19 | + | |
| 20 | +import javax.annotation.Resource; | |
| 21 | +import java.io.*; | |
| 22 | +import java.util.function.Consumer; | |
| 23 | + | |
| 24 | +import static cn.hutool.core.util.RandomUtil.randomBytes; | |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 26 | +import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 27 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 28 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; | |
| 29 | +import static org.mockito.Mockito.*; | |
| 30 | +/** | |
| 31 | + * {@link MbrUserServiceImpl} 的单元测试类 | |
| 32 | + * | |
| 33 | + * @author 宋天 | |
| 34 | + */ | |
| 35 | +@Import(MbrUserServiceImpl.class) | |
| 36 | +public class MbrUserServiceImplTest extends BaseDbUnitTest { | |
| 37 | + | |
| 38 | + @Resource | |
| 39 | + private MbrUserServiceImpl mbrUserService; | |
| 40 | + | |
| 41 | + @Resource | |
| 42 | + private MbrUserMapper userMapper; | |
| 43 | + | |
| 44 | + @MockBean | |
| 45 | + private InfFileCoreService fileCoreService; | |
| 46 | + | |
| 47 | + @MockBean | |
| 48 | + private PasswordEncoder passwordEncoder; | |
| 49 | + | |
| 50 | + @Test | |
| 51 | + public void testUpdateNickName_success(){ | |
| 52 | + // mock 数据 | |
| 53 | + MbrUserDO userDO = randomMbrUserDO(); | |
| 54 | + userMapper.insert(userDO); | |
| 55 | + | |
| 56 | + // 随机昵称 | |
| 57 | + String newNickName = randomString(); | |
| 58 | + | |
| 59 | + // 调用接口修改昵称 | |
| 60 | + mbrUserService.updateNickname(userDO.getId(),newNickName); | |
| 61 | + // 查询新修改后的昵称 | |
| 62 | + String nickname = mbrUserService.getUser(userDO.getId()).getNickname(); | |
| 63 | + // 断言 | |
| 64 | + assertEquals(newNickName,nickname); | |
| 65 | + } | |
| 66 | + | |
| 67 | + @Test | |
| 68 | + public void testGetUserInfo_success(){ | |
| 69 | + // mock 数据 | |
| 70 | + MbrUserDO userDO = randomMbrUserDO(); | |
| 71 | + userMapper.insert(userDO); | |
| 72 | + | |
| 73 | + // 查询用户昵称与头像 | |
| 74 | + MbrUserInfoRespVO userInfo = mbrUserService.getUserInfo(userDO.getId()); | |
| 75 | + System.out.println(userInfo); | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Test | |
| 79 | + public void testUpdateAvatar_success(){ | |
| 80 | + // mock 数据 | |
| 81 | + MbrUserDO dbUser = randomMbrUserDO(); | |
| 82 | + userMapper.insert(dbUser); | |
| 83 | + | |
| 84 | + // 准备参数 | |
| 85 | + Long userId = dbUser.getId(); | |
| 86 | + byte[] avatarFileBytes = randomBytes(10); | |
| 87 | + ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); | |
| 88 | + // mock 方法 | |
| 89 | + String avatar = randomString(); | |
| 90 | + when(fileCoreService.createFile(anyString(), eq(avatarFileBytes))).thenReturn(avatar); | |
| 91 | + // 调用 | |
| 92 | + String str = mbrUserService.updateAvatar(userId, avatarFile); | |
| 93 | + // 断言 | |
| 94 | + assertEquals(avatar, str); | |
| 95 | + } | |
| 96 | + | |
| 97 | + // ========== 随机对象 ========== | |
| 98 | + | |
| 99 | + @SafeVarargs | |
| 100 | + private static MbrUserDO randomMbrUserDO(Consumer<MbrUserDO>... consumers) { | |
| 101 | + Consumer<MbrUserDO> consumer = (o) -> { | |
| 102 | + o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 | |
| 103 | + }; | |
| 104 | + return randomPojo(MbrUserDO.class, ArrayUtils.append(consumer, consumers)); | |
| 105 | + } | |
| 106 | + | |
| 107 | +} | ... | ... |
yudao-user-server/src/test/resources/application-unit-test.yaml
0 → 100644
| 1 | +spring: | |
| 2 | + main: | |
| 3 | + lazy-initialization: true # 开启懒加载,加快速度 | |
| 4 | + banner-mode: off # 单元测试,禁用 Banner | |
| 5 | + | |
| 6 | +--- #################### 数据库相关配置 #################### | |
| 7 | + | |
| 8 | +spring: | |
| 9 | + # 数据源配置项 | |
| 10 | + datasource: | |
| 11 | + name: ruoyi-vue-pro | |
| 12 | + url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写 | |
| 13 | + driver-class-name: org.h2.Driver | |
| 14 | + username: sa | |
| 15 | + password: | |
| 16 | + schema: classpath:sql/create_tables.sql # MySQL 转 H2 的语句,使用 https://www.jooq.org/translate/ 工具 | |
| 17 | + druid: | |
| 18 | + async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度 | |
| 19 | + initial-size: 1 # 单元测试,配置为 1,提升启动速度 | |
| 20 | + | |
| 21 | + # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 | |
| 22 | + redis: | |
| 23 | + host: 127.0.0.1 # 地址 | |
| 24 | + port: 16379 # 端口(单元测试,使用 16379 端口) | |
| 25 | + database: 0 # 数据库索引 | |
| 26 | + | |
| 27 | +mybatis: | |
| 28 | + lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试 | |
| 29 | + | |
| 30 | +--- #################### 定时任务相关配置 #################### | |
| 31 | + | |
| 32 | +--- #################### 配置中心相关配置 #################### | |
| 33 | + | |
| 34 | +--- #################### 服务保障相关配置 #################### | |
| 35 | + | |
| 36 | +# Lock4j 配置项(单元测试,禁用 Lock4j) | |
| 37 | + | |
| 38 | +# Resilience4j 配置项 | |
| 39 | + | |
| 40 | +--- #################### 监控相关配置 #################### | |
| 41 | + | |
| 42 | +--- #################### 芋道相关配置 #################### | |
| 43 | + | |
| 44 | +# 芋道配置项,设置当前项目所有自定义的配置 | ... | ... |
yudao-user-server/src/test/resources/file/erweima.jpg
0 → 100644
18 KB
yudao-user-server/src/test/resources/logback-spring.xml
0 → 100644
yudao-user-server/src/test/resources/sql/clean.sql
0 → 100644
yudao-user-server/src/test/resources/sql/create_tables.sql
0 → 100644
| 1 | +-- mbr 开头的 DB | |
| 2 | +CREATE TABLE IF NOT EXISTS "mbr_user" ( | |
| 3 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号', | |
| 4 | + "nickname" varchar(30) NOT NULL DEFAULT '' COMMENT '用户昵称', | |
| 5 | + "avatar" varchar(255) NOT NULL DEFAULT '' COMMENT '头像', | |
| 6 | + "status" tinyint NOT NULL COMMENT '状态', | |
| 7 | + "mobile" varchar(11) NOT NULL COMMENT '手机号', | |
| 8 | + "password" varchar(100) NOT NULL DEFAULT '' COMMENT '密码', | |
| 9 | + "register_ip" varchar(32) NOT NULL COMMENT '注册 IP', | |
| 10 | + "login_ip" varchar(50) NULL DEFAULT '' COMMENT '最后登录IP', | |
| 11 | + "login_date" datetime NULL DEFAULT NULL COMMENT '最后登录时间', | |
| 12 | + "creator" varchar(64) NULL DEFAULT '' COMMENT '创建者', | |
| 13 | + "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | |
| 14 | + "updater" varchar(64) NULL DEFAULT '' COMMENT '更新者', | |
| 15 | + "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', | |
| 16 | + "deleted" bit(1) NOT NULL DEFAULT '0' COMMENT '是否删除', | |
| 17 | + PRIMARY KEY ("id") | |
| 18 | +) COMMENT '会员表'; | |
| 19 | + | |
| 20 | +-- inf 开头的 DB | |
| 21 | +CREATE TABLE IF NOT EXISTS "inf_file" ( | |
| 22 | + "id" varchar(188) NOT NULL, | |
| 23 | + "type" varchar(63) DEFAULT NULL, | |
| 24 | + "content" blob NOT NULL, | |
| 25 | + "creator" varchar(64) DEFAULT '', | |
| 26 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 27 | + "updater" varchar(64) DEFAULT '', | |
| 28 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 29 | + "deleted" bit NOT NULL DEFAULT FALSE, | |
| 30 | + PRIMARY KEY ("id") | |
| 31 | +) COMMENT '文件表'; | |
| 32 | + | ... | ... |