Commit 3070392ecd5548632d774287b19ba9df1d6b3b8c
1 parent
cc1fa54e
infra:完善 file 的单元测试
Showing
7 changed files
with
36 additions
and
27 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigService.java
| @@ -8,8 +8,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigU | @@ -8,8 +8,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigU | ||
| 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO; | 8 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO; |
| 9 | 9 | ||
| 10 | import javax.validation.Valid; | 10 | import javax.validation.Valid; |
| 11 | -import java.util.Collection; | ||
| 12 | -import java.util.List; | ||
| 13 | 11 | ||
| 14 | /** | 12 | /** |
| 15 | * 文件配置 Service 接口 | 13 | * 文件配置 Service 接口 |
| @@ -61,14 +59,6 @@ public interface FileConfigService { | @@ -61,14 +59,6 @@ public interface FileConfigService { | ||
| 61 | FileConfigDO getFileConfig(Long id); | 59 | FileConfigDO getFileConfig(Long id); |
| 62 | 60 | ||
| 63 | /** | 61 | /** |
| 64 | - * 获得文件配置列表 | ||
| 65 | - * | ||
| 66 | - * @param ids 编号 | ||
| 67 | - * @return 文件配置列表 | ||
| 68 | - */ | ||
| 69 | - List<FileConfigDO> getFileConfigList(Collection<Long> ids); | ||
| 70 | - | ||
| 71 | - /** | ||
| 72 | * 获得文件配置分页 | 62 | * 获得文件配置分页 |
| 73 | * | 63 | * |
| 74 | * @param pageReqVO 分页查询 | 64 | * @param pageReqVO 分页查询 |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImpl.java
| @@ -27,7 +27,6 @@ import org.springframework.validation.annotation.Validated; | @@ -27,7 +27,6 @@ import org.springframework.validation.annotation.Validated; | ||
| 27 | import javax.annotation.PostConstruct; | 27 | import javax.annotation.PostConstruct; |
| 28 | import javax.annotation.Resource; | 28 | import javax.annotation.Resource; |
| 29 | import javax.validation.Validator; | 29 | import javax.validation.Validator; |
| 30 | -import java.util.Collection; | ||
| 31 | import java.util.List; | 30 | import java.util.List; |
| 32 | import java.util.Map; | 31 | import java.util.Map; |
| 33 | 32 | ||
| @@ -95,7 +94,7 @@ public class FileConfigServiceImpl implements FileConfigService { | @@ -95,7 +94,7 @@ public class FileConfigServiceImpl implements FileConfigService { | ||
| 95 | @Override | 94 | @Override |
| 96 | public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) { | 95 | public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) { |
| 97 | // 校验存在 | 96 | // 校验存在 |
| 98 | - FileConfigDO config = this.validateFileConfigExists(updateReqVO.getId()); | 97 | + FileConfigDO config = validateFileConfigExists(updateReqVO.getId()); |
| 99 | // 更新 | 98 | // 更新 |
| 100 | FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO) | 99 | FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO) |
| 101 | .setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig())); | 100 | .setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig())); |
| @@ -108,7 +107,7 @@ public class FileConfigServiceImpl implements FileConfigService { | @@ -108,7 +107,7 @@ public class FileConfigServiceImpl implements FileConfigService { | ||
| 108 | @Transactional(rollbackFor = Exception.class) | 107 | @Transactional(rollbackFor = Exception.class) |
| 109 | public void updateFileConfigMaster(Long id) { | 108 | public void updateFileConfigMaster(Long id) { |
| 110 | // 校验存在 | 109 | // 校验存在 |
| 111 | - this.validateFileConfigExists(id); | 110 | + validateFileConfigExists(id); |
| 112 | // 更新其它为非 master | 111 | // 更新其它为非 master |
| 113 | fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false)); | 112 | fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false)); |
| 114 | // 更新 | 113 | // 更新 |
| @@ -138,7 +137,7 @@ public class FileConfigServiceImpl implements FileConfigService { | @@ -138,7 +137,7 @@ public class FileConfigServiceImpl implements FileConfigService { | ||
| 138 | @Override | 137 | @Override |
| 139 | public void deleteFileConfig(Long id) { | 138 | public void deleteFileConfig(Long id) { |
| 140 | // 校验存在 | 139 | // 校验存在 |
| 141 | - FileConfigDO config = this.validateFileConfigExists(id); | 140 | + FileConfigDO config = validateFileConfigExists(id); |
| 142 | if (Boolean.TRUE.equals(config.getMaster())) { | 141 | if (Boolean.TRUE.equals(config.getMaster())) { |
| 143 | throw exception(FILE_CONFIG_DELETE_FAIL_MASTER); | 142 | throw exception(FILE_CONFIG_DELETE_FAIL_MASTER); |
| 144 | } | 143 | } |
| @@ -162,11 +161,6 @@ public class FileConfigServiceImpl implements FileConfigService { | @@ -162,11 +161,6 @@ public class FileConfigServiceImpl implements FileConfigService { | ||
| 162 | } | 161 | } |
| 163 | 162 | ||
| 164 | @Override | 163 | @Override |
| 165 | - public List<FileConfigDO> getFileConfigList(Collection<Long> ids) { | ||
| 166 | - return fileConfigMapper.selectBatchIds(ids); | ||
| 167 | - } | ||
| 168 | - | ||
| 169 | - @Override | ||
| 170 | public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) { | 164 | public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) { |
| 171 | return fileConfigMapper.selectPage(pageReqVO); | 165 | return fileConfigMapper.selectPage(pageReqVO); |
| 172 | } | 166 | } |
| @@ -174,7 +168,7 @@ public class FileConfigServiceImpl implements FileConfigService { | @@ -174,7 +168,7 @@ public class FileConfigServiceImpl implements FileConfigService { | ||
| 174 | @Override | 168 | @Override |
| 175 | public String testFileConfig(Long id) throws Exception { | 169 | public String testFileConfig(Long id) throws Exception { |
| 176 | // 校验存在 | 170 | // 校验存在 |
| 177 | - this.validateFileConfigExists(id); | 171 | + validateFileConfigExists(id); |
| 178 | // 上传文件 | 172 | // 上传文件 |
| 179 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); | 173 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); |
| 180 | return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg"); | 174 | return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg"); |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
| @@ -69,7 +69,7 @@ public class FileServiceImpl implements FileService { | @@ -69,7 +69,7 @@ public class FileServiceImpl implements FileService { | ||
| 69 | @Override | 69 | @Override |
| 70 | public void deleteFile(Long id) throws Exception { | 70 | public void deleteFile(Long id) throws Exception { |
| 71 | // 校验存在 | 71 | // 校验存在 |
| 72 | - FileDO file = this.validateFileExists(id); | 72 | + FileDO file = validateFileExists(id); |
| 73 | 73 | ||
| 74 | // 从文件存储器中删除 | 74 | // 从文件存储器中删除 |
| 75 | FileClient client = fileConfigService.getFileClient(file.getConfigId()); | 75 | FileClient client = fileConfigService.getFileClient(file.getConfigId()); |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java
| @@ -69,7 +69,7 @@ public class JobServiceImpl implements JobService { | @@ -69,7 +69,7 @@ public class JobServiceImpl implements JobService { | ||
| 69 | public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException { | 69 | public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException { |
| 70 | validateCronExpression(updateReqVO.getCronExpression()); | 70 | validateCronExpression(updateReqVO.getCronExpression()); |
| 71 | // 校验存在 | 71 | // 校验存在 |
| 72 | - JobDO job = this.validateJobExists(updateReqVO.getId()); | 72 | + JobDO job = validateJobExists(updateReqVO.getId()); |
| 73 | // 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行 | 73 | // 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行 |
| 74 | if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) { | 74 | if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) { |
| 75 | throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS); | 75 | throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS); |
| @@ -92,7 +92,7 @@ public class JobServiceImpl implements JobService { | @@ -92,7 +92,7 @@ public class JobServiceImpl implements JobService { | ||
| 92 | throw exception(JOB_CHANGE_STATUS_INVALID); | 92 | throw exception(JOB_CHANGE_STATUS_INVALID); |
| 93 | } | 93 | } |
| 94 | // 校验存在 | 94 | // 校验存在 |
| 95 | - JobDO job = this.validateJobExists(id); | 95 | + JobDO job = validateJobExists(id); |
| 96 | // 校验是否已经为当前状态 | 96 | // 校验是否已经为当前状态 |
| 97 | if (job.getStatus().equals(status)) { | 97 | if (job.getStatus().equals(status)) { |
| 98 | throw exception(JOB_CHANGE_STATUS_EQUALS); | 98 | throw exception(JOB_CHANGE_STATUS_EQUALS); |
| @@ -112,7 +112,7 @@ public class JobServiceImpl implements JobService { | @@ -112,7 +112,7 @@ public class JobServiceImpl implements JobService { | ||
| 112 | @Override | 112 | @Override |
| 113 | public void triggerJob(Long id) throws SchedulerException { | 113 | public void triggerJob(Long id) throws SchedulerException { |
| 114 | // 校验存在 | 114 | // 校验存在 |
| 115 | - JobDO job = this.validateJobExists(id); | 115 | + JobDO job = validateJobExists(id); |
| 116 | 116 | ||
| 117 | // 触发 Quartz 中的 Job | 117 | // 触发 Quartz 中的 Job |
| 118 | schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam()); | 118 | schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam()); |
| @@ -122,7 +122,7 @@ public class JobServiceImpl implements JobService { | @@ -122,7 +122,7 @@ public class JobServiceImpl implements JobService { | ||
| 122 | @Transactional(rollbackFor = Exception.class) | 122 | @Transactional(rollbackFor = Exception.class) |
| 123 | public void deleteJob(Long id) throws SchedulerException { | 123 | public void deleteJob(Long id) throws SchedulerException { |
| 124 | // 校验存在 | 124 | // 校验存在 |
| 125 | - JobDO job = this.validateJobExists(id); | 125 | + JobDO job = validateJobExists(id); |
| 126 | // 更新 | 126 | // 更新 |
| 127 | jobMapper.deleteById(id); | 127 | jobMapper.deleteById(id); |
| 128 | 128 |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/config/ConfigServiceTest.java renamed to yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/config/ConfigServiceImplTest.java
| @@ -29,7 +29,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; | @@ -29,7 +29,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; | ||
| 29 | import static org.junit.jupiter.api.Assertions.*; | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | 30 | ||
| 31 | @Import(ConfigServiceImpl.class) | 31 | @Import(ConfigServiceImpl.class) |
| 32 | -public class ConfigServiceTest extends BaseDbUnitTest { | 32 | +public class ConfigServiceImplTest extends BaseDbUnitTest { |
| 33 | 33 | ||
| 34 | @Resource | 34 | @Resource |
| 35 | private ConfigServiceImpl configService; | 35 | private ConfigServiceImpl configService; |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImplTest.java
| @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 7 | import cn.iocoder.yudao.framework.file.core.client.FileClient; | 7 | import cn.iocoder.yudao.framework.file.core.client.FileClient; |
| 8 | import cn.iocoder.yudao.framework.file.core.client.FileClientConfig; | 8 | import cn.iocoder.yudao.framework.file.core.client.FileClientConfig; |
| 9 | import cn.iocoder.yudao.framework.file.core.client.FileClientFactory; | 9 | import cn.iocoder.yudao.framework.file.core.client.FileClientFactory; |
| 10 | +import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClient; | ||
| 10 | import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig; | 11 | import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig; |
| 11 | import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum; | 12 | import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum; |
| 12 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | 13 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| @@ -242,6 +243,30 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest { | @@ -242,6 +243,30 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest { | ||
| 242 | assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id)); | 243 | assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id)); |
| 243 | } | 244 | } |
| 244 | 245 | ||
| 246 | + @Test | ||
| 247 | + public void testGetFileConfig() { | ||
| 248 | + // mock 数据 | ||
| 249 | + FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false); | ||
| 250 | + fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据 | ||
| 251 | + // 准备参数 | ||
| 252 | + Long id = dbFileConfig.getId(); | ||
| 253 | + | ||
| 254 | + // 调用,并断言 | ||
| 255 | + assertPojoEquals(dbFileConfig, fileConfigService.getFileConfig(id)); | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + @Test | ||
| 259 | + public void testGetFileClient() { | ||
| 260 | + // 准备参数 | ||
| 261 | + Long id = randomLongId(); | ||
| 262 | + // mock 获得 Client | ||
| 263 | + FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig()); | ||
| 264 | + when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient); | ||
| 265 | + | ||
| 266 | + // 调用,并断言 | ||
| 267 | + assertSame(fileClient, fileConfigService.getFileClient(id)); | ||
| 268 | + } | ||
| 269 | + | ||
| 245 | private FileConfigDO randomFileConfigDO() { | 270 | private FileConfigDO randomFileConfigDO() { |
| 246 | return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage()) | 271 | return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage()) |
| 247 | .setConfig(new EmptyFileClientConfig()); | 272 | .setConfig(new EmptyFileClientConfig()); |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java renamed to yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImplTest.java
| @@ -26,7 +26,7 @@ import static org.mockito.ArgumentMatchers.same; | @@ -26,7 +26,7 @@ import static org.mockito.ArgumentMatchers.same; | ||
| 26 | import static org.mockito.Mockito.*; | 26 | import static org.mockito.Mockito.*; |
| 27 | 27 | ||
| 28 | @Import({FileServiceImpl.class}) | 28 | @Import({FileServiceImpl.class}) |
| 29 | -public class FileServiceTest extends BaseDbUnitTest { | 29 | +public class FileServiceImplTest extends BaseDbUnitTest { |
| 30 | 30 | ||
| 31 | @Resource | 31 | @Resource |
| 32 | private FileService fileService; | 32 | private FileService fileService; |