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 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO; |
| 9 | 9 | |
| 10 | 10 | import javax.validation.Valid; |
| 11 | -import java.util.Collection; | |
| 12 | -import java.util.List; | |
| 13 | 11 | |
| 14 | 12 | /** |
| 15 | 13 | * 文件配置 Service 接口 |
| ... | ... | @@ -61,14 +59,6 @@ public interface FileConfigService { |
| 61 | 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 | 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 | 27 | import javax.annotation.PostConstruct; |
| 28 | 28 | import javax.annotation.Resource; |
| 29 | 29 | import javax.validation.Validator; |
| 30 | -import java.util.Collection; | |
| 31 | 30 | import java.util.List; |
| 32 | 31 | import java.util.Map; |
| 33 | 32 | |
| ... | ... | @@ -95,7 +94,7 @@ public class FileConfigServiceImpl implements FileConfigService { |
| 95 | 94 | @Override |
| 96 | 95 | public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) { |
| 97 | 96 | // 校验存在 |
| 98 | - FileConfigDO config = this.validateFileConfigExists(updateReqVO.getId()); | |
| 97 | + FileConfigDO config = validateFileConfigExists(updateReqVO.getId()); | |
| 99 | 98 | // 更新 |
| 100 | 99 | FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO) |
| 101 | 100 | .setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig())); |
| ... | ... | @@ -108,7 +107,7 @@ public class FileConfigServiceImpl implements FileConfigService { |
| 108 | 107 | @Transactional(rollbackFor = Exception.class) |
| 109 | 108 | public void updateFileConfigMaster(Long id) { |
| 110 | 109 | // 校验存在 |
| 111 | - this.validateFileConfigExists(id); | |
| 110 | + validateFileConfigExists(id); | |
| 112 | 111 | // 更新其它为非 master |
| 113 | 112 | fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false)); |
| 114 | 113 | // 更新 |
| ... | ... | @@ -138,7 +137,7 @@ public class FileConfigServiceImpl implements FileConfigService { |
| 138 | 137 | @Override |
| 139 | 138 | public void deleteFileConfig(Long id) { |
| 140 | 139 | // 校验存在 |
| 141 | - FileConfigDO config = this.validateFileConfigExists(id); | |
| 140 | + FileConfigDO config = validateFileConfigExists(id); | |
| 142 | 141 | if (Boolean.TRUE.equals(config.getMaster())) { |
| 143 | 142 | throw exception(FILE_CONFIG_DELETE_FAIL_MASTER); |
| 144 | 143 | } |
| ... | ... | @@ -162,11 +161,6 @@ public class FileConfigServiceImpl implements FileConfigService { |
| 162 | 161 | } |
| 163 | 162 | |
| 164 | 163 | @Override |
| 165 | - public List<FileConfigDO> getFileConfigList(Collection<Long> ids) { | |
| 166 | - return fileConfigMapper.selectBatchIds(ids); | |
| 167 | - } | |
| 168 | - | |
| 169 | - @Override | |
| 170 | 164 | public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) { |
| 171 | 165 | return fileConfigMapper.selectPage(pageReqVO); |
| 172 | 166 | } |
| ... | ... | @@ -174,7 +168,7 @@ public class FileConfigServiceImpl implements FileConfigService { |
| 174 | 168 | @Override |
| 175 | 169 | public String testFileConfig(Long id) throws Exception { |
| 176 | 170 | // 校验存在 |
| 177 | - this.validateFileConfigExists(id); | |
| 171 | + validateFileConfigExists(id); | |
| 178 | 172 | // 上传文件 |
| 179 | 173 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); |
| 180 | 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 | 69 | @Override |
| 70 | 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 | 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 | 69 | public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException { |
| 70 | 70 | validateCronExpression(updateReqVO.getCronExpression()); |
| 71 | 71 | // 校验存在 |
| 72 | - JobDO job = this.validateJobExists(updateReqVO.getId()); | |
| 72 | + JobDO job = validateJobExists(updateReqVO.getId()); | |
| 73 | 73 | // 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行 |
| 74 | 74 | if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) { |
| 75 | 75 | throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS); |
| ... | ... | @@ -92,7 +92,7 @@ public class JobServiceImpl implements JobService { |
| 92 | 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 | 97 | if (job.getStatus().equals(status)) { |
| 98 | 98 | throw exception(JOB_CHANGE_STATUS_EQUALS); |
| ... | ... | @@ -112,7 +112,7 @@ public class JobServiceImpl implements JobService { |
| 112 | 112 | @Override |
| 113 | 113 | public void triggerJob(Long id) throws SchedulerException { |
| 114 | 114 | // 校验存在 |
| 115 | - JobDO job = this.validateJobExists(id); | |
| 115 | + JobDO job = validateJobExists(id); | |
| 116 | 116 | |
| 117 | 117 | // 触发 Quartz 中的 Job |
| 118 | 118 | schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam()); |
| ... | ... | @@ -122,7 +122,7 @@ public class JobServiceImpl implements JobService { |
| 122 | 122 | @Transactional(rollbackFor = Exception.class) |
| 123 | 123 | public void deleteJob(Long id) throws SchedulerException { |
| 124 | 124 | // 校验存在 |
| 125 | - JobDO job = this.validateJobExists(id); | |
| 125 | + JobDO job = validateJobExists(id); | |
| 126 | 126 | // 更新 |
| 127 | 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 | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | 30 | |
| 31 | 31 | @Import(ConfigServiceImpl.class) |
| 32 | -public class ConfigServiceTest extends BaseDbUnitTest { | |
| 32 | +public class ConfigServiceImplTest extends BaseDbUnitTest { | |
| 33 | 33 | |
| 34 | 34 | @Resource |
| 35 | 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 | 7 | import cn.iocoder.yudao.framework.file.core.client.FileClient; |
| 8 | 8 | import cn.iocoder.yudao.framework.file.core.client.FileClientConfig; |
| 9 | 9 | import cn.iocoder.yudao.framework.file.core.client.FileClientFactory; |
| 10 | +import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClient; | |
| 10 | 11 | import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig; |
| 11 | 12 | import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum; |
| 12 | 13 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| ... | ... | @@ -242,6 +243,30 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest { |
| 242 | 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 | 270 | private FileConfigDO randomFileConfigDO() { |
| 246 | 271 | return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage()) |
| 247 | 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 | 26 | import static org.mockito.Mockito.*; |
| 27 | 27 | |
| 28 | 28 | @Import({FileServiceImpl.class}) |
| 29 | -public class FileServiceTest extends BaseDbUnitTest { | |
| 29 | +public class FileServiceImplTest extends BaseDbUnitTest { | |
| 30 | 30 | |
| 31 | 31 | @Resource |
| 32 | 32 | private FileService fileService; | ... | ... |