infra:完善 file 的单元测试

This commit is contained in:
YunaiV 2023-02-04 07:50:36 +08:00
parent cc1fa54ea5
commit 3070392ecd
7 changed files with 36 additions and 27 deletions

View File

@ -8,8 +8,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigU
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
/** /**
* 文件配置 Service 接口 * 文件配置 Service 接口
@ -60,14 +58,6 @@ public interface FileConfigService {
*/ */
FileConfigDO getFileConfig(Long id); FileConfigDO getFileConfig(Long id);
/**
* 获得文件配置列表
*
* @param ids 编号
* @return 文件配置列表
*/
List<FileConfigDO> getFileConfigList(Collection<Long> ids);
/** /**
* 获得文件配置分页 * 获得文件配置分页
* *

View File

@ -27,7 +27,6 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Validator; import javax.validation.Validator;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -95,7 +94,7 @@ public class FileConfigServiceImpl implements FileConfigService {
@Override @Override
public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) { public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) {
// 校验存在 // 校验存在
FileConfigDO config = this.validateFileConfigExists(updateReqVO.getId()); FileConfigDO config = validateFileConfigExists(updateReqVO.getId());
// 更新 // 更新
FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO) FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO)
.setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig())); .setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig()));
@ -108,7 +107,7 @@ public class FileConfigServiceImpl implements FileConfigService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateFileConfigMaster(Long id) { public void updateFileConfigMaster(Long id) {
// 校验存在 // 校验存在
this.validateFileConfigExists(id); validateFileConfigExists(id);
// 更新其它为非 master // 更新其它为非 master
fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false)); fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false));
// 更新 // 更新
@ -138,7 +137,7 @@ public class FileConfigServiceImpl implements FileConfigService {
@Override @Override
public void deleteFileConfig(Long id) { public void deleteFileConfig(Long id) {
// 校验存在 // 校验存在
FileConfigDO config = this.validateFileConfigExists(id); FileConfigDO config = validateFileConfigExists(id);
if (Boolean.TRUE.equals(config.getMaster())) { if (Boolean.TRUE.equals(config.getMaster())) {
throw exception(FILE_CONFIG_DELETE_FAIL_MASTER); throw exception(FILE_CONFIG_DELETE_FAIL_MASTER);
} }
@ -161,11 +160,6 @@ public class FileConfigServiceImpl implements FileConfigService {
return fileConfigMapper.selectById(id); return fileConfigMapper.selectById(id);
} }
@Override
public List<FileConfigDO> getFileConfigList(Collection<Long> ids) {
return fileConfigMapper.selectBatchIds(ids);
}
@Override @Override
public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) { public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) {
return fileConfigMapper.selectPage(pageReqVO); return fileConfigMapper.selectPage(pageReqVO);
@ -174,7 +168,7 @@ public class FileConfigServiceImpl implements FileConfigService {
@Override @Override
public String testFileConfig(Long id) throws Exception { public String testFileConfig(Long id) throws Exception {
// 校验存在 // 校验存在
this.validateFileConfigExists(id); validateFileConfigExists(id);
// 上传文件 // 上传文件
byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg"); return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");

View File

@ -69,7 +69,7 @@ public class FileServiceImpl implements FileService {
@Override @Override
public void deleteFile(Long id) throws Exception { public void deleteFile(Long id) throws Exception {
// 校验存在 // 校验存在
FileDO file = this.validateFileExists(id); FileDO file = validateFileExists(id);
// 从文件存储器中删除 // 从文件存储器中删除
FileClient client = fileConfigService.getFileClient(file.getConfigId()); FileClient client = fileConfigService.getFileClient(file.getConfigId());

View File

@ -69,7 +69,7 @@ public class JobServiceImpl implements JobService {
public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException { public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException {
validateCronExpression(updateReqVO.getCronExpression()); validateCronExpression(updateReqVO.getCronExpression());
// 校验存在 // 校验存在
JobDO job = this.validateJobExists(updateReqVO.getId()); JobDO job = validateJobExists(updateReqVO.getId());
// 只有开启状态才可以修改.原因是如果出暂停状态修改 Quartz Job 会导致任务又开始执行 // 只有开启状态才可以修改.原因是如果出暂停状态修改 Quartz Job 会导致任务又开始执行
if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) { if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) {
throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS); throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS);
@ -92,7 +92,7 @@ public class JobServiceImpl implements JobService {
throw exception(JOB_CHANGE_STATUS_INVALID); throw exception(JOB_CHANGE_STATUS_INVALID);
} }
// 校验存在 // 校验存在
JobDO job = this.validateJobExists(id); JobDO job = validateJobExists(id);
// 校验是否已经为当前状态 // 校验是否已经为当前状态
if (job.getStatus().equals(status)) { if (job.getStatus().equals(status)) {
throw exception(JOB_CHANGE_STATUS_EQUALS); throw exception(JOB_CHANGE_STATUS_EQUALS);
@ -112,7 +112,7 @@ public class JobServiceImpl implements JobService {
@Override @Override
public void triggerJob(Long id) throws SchedulerException { public void triggerJob(Long id) throws SchedulerException {
// 校验存在 // 校验存在
JobDO job = this.validateJobExists(id); JobDO job = validateJobExists(id);
// 触发 Quartz 中的 Job // 触发 Quartz 中的 Job
schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam()); schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam());
@ -122,7 +122,7 @@ public class JobServiceImpl implements JobService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void deleteJob(Long id) throws SchedulerException { public void deleteJob(Long id) throws SchedulerException {
// 校验存在 // 校验存在
JobDO job = this.validateJobExists(id); JobDO job = validateJobExists(id);
// 更新 // 更新
jobMapper.deleteById(id); jobMapper.deleteById(id);

View File

@ -29,7 +29,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@Import(ConfigServiceImpl.class) @Import(ConfigServiceImpl.class)
public class ConfigServiceTest extends BaseDbUnitTest { public class ConfigServiceImplTest extends BaseDbUnitTest {
@Resource @Resource
private ConfigServiceImpl configService; private ConfigServiceImpl configService;

View File

@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.file.core.client.FileClient; import cn.iocoder.yudao.framework.file.core.client.FileClient;
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig; import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
import cn.iocoder.yudao.framework.file.core.client.FileClientFactory; import cn.iocoder.yudao.framework.file.core.client.FileClientFactory;
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClient;
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig; import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig;
import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum; import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
@ -242,6 +243,30 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id)); assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id));
} }
@Test
public void testGetFileConfig() {
// mock 数据
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbFileConfig.getId();
// 调用并断言
assertPojoEquals(dbFileConfig, fileConfigService.getFileConfig(id));
}
@Test
public void testGetFileClient() {
// 准备参数
Long id = randomLongId();
// mock 获得 Client
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
// 调用并断言
assertSame(fileClient, fileConfigService.getFileClient(id));
}
private FileConfigDO randomFileConfigDO() { private FileConfigDO randomFileConfigDO() {
return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage()) return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage())
.setConfig(new EmptyFileClientConfig()); .setConfig(new EmptyFileClientConfig());

View File

@ -26,7 +26,7 @@ import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@Import({FileServiceImpl.class}) @Import({FileServiceImpl.class})
public class FileServiceTest extends BaseDbUnitTest { public class FileServiceImplTest extends BaseDbUnitTest {
@Resource @Resource
private FileService fileService; private FileService fileService;