infra:完善 file 的单元测试
This commit is contained in:
parent
cc1fa54ea5
commit
3070392ecd
@ -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 javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件配置 Service 接口
|
||||
@ -60,14 +58,6 @@ public interface FileConfigService {
|
||||
*/
|
||||
FileConfigDO getFileConfig(Long id);
|
||||
|
||||
/**
|
||||
* 获得文件配置列表
|
||||
*
|
||||
* @param ids 编号
|
||||
* @return 文件配置列表
|
||||
*/
|
||||
List<FileConfigDO> getFileConfigList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得文件配置分页
|
||||
*
|
||||
|
@ -27,7 +27,6 @@ import org.springframework.validation.annotation.Validated;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Validator;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -95,7 +94,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Override
|
||||
public void updateFileConfig(FileConfigUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
FileConfigDO config = this.validateFileConfigExists(updateReqVO.getId());
|
||||
FileConfigDO config = validateFileConfigExists(updateReqVO.getId());
|
||||
// 更新
|
||||
FileConfigDO updateObj = FileConfigConvert.INSTANCE.convert(updateReqVO)
|
||||
.setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig()));
|
||||
@ -108,7 +107,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateFileConfigMaster(Long id) {
|
||||
// 校验存在
|
||||
this.validateFileConfigExists(id);
|
||||
validateFileConfigExists(id);
|
||||
// 更新其它为非 master
|
||||
fileConfigMapper.updateBatch(new FileConfigDO().setMaster(false));
|
||||
// 更新
|
||||
@ -138,7 +137,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Override
|
||||
public void deleteFileConfig(Long id) {
|
||||
// 校验存在
|
||||
FileConfigDO config = this.validateFileConfigExists(id);
|
||||
FileConfigDO config = validateFileConfigExists(id);
|
||||
if (Boolean.TRUE.equals(config.getMaster())) {
|
||||
throw exception(FILE_CONFIG_DELETE_FAIL_MASTER);
|
||||
}
|
||||
@ -161,11 +160,6 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
return fileConfigMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileConfigDO> getFileConfigList(Collection<Long> ids) {
|
||||
return fileConfigMapper.selectBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<FileConfigDO> getFileConfigPage(FileConfigPageReqVO pageReqVO) {
|
||||
return fileConfigMapper.selectPage(pageReqVO);
|
||||
@ -174,7 +168,7 @@ public class FileConfigServiceImpl implements FileConfigService {
|
||||
@Override
|
||||
public String testFileConfig(Long id) throws Exception {
|
||||
// 校验存在
|
||||
this.validateFileConfigExists(id);
|
||||
validateFileConfigExists(id);
|
||||
// 上传文件
|
||||
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
||||
return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");
|
||||
|
@ -69,7 +69,7 @@ public class FileServiceImpl implements FileService {
|
||||
@Override
|
||||
public void deleteFile(Long id) throws Exception {
|
||||
// 校验存在
|
||||
FileDO file = this.validateFileExists(id);
|
||||
FileDO file = validateFileExists(id);
|
||||
|
||||
// 从文件存储器中删除
|
||||
FileClient client = fileConfigService.getFileClient(file.getConfigId());
|
||||
|
@ -69,7 +69,7 @@ public class JobServiceImpl implements JobService {
|
||||
public void updateJob(JobUpdateReqVO updateReqVO) throws SchedulerException {
|
||||
validateCronExpression(updateReqVO.getCronExpression());
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(updateReqVO.getId());
|
||||
JobDO job = validateJobExists(updateReqVO.getId());
|
||||
// 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行
|
||||
if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) {
|
||||
throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS);
|
||||
@ -92,7 +92,7 @@ public class JobServiceImpl implements JobService {
|
||||
throw exception(JOB_CHANGE_STATUS_INVALID);
|
||||
}
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(id);
|
||||
JobDO job = validateJobExists(id);
|
||||
// 校验是否已经为当前状态
|
||||
if (job.getStatus().equals(status)) {
|
||||
throw exception(JOB_CHANGE_STATUS_EQUALS);
|
||||
@ -112,7 +112,7 @@ public class JobServiceImpl implements JobService {
|
||||
@Override
|
||||
public void triggerJob(Long id) throws SchedulerException {
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(id);
|
||||
JobDO job = validateJobExists(id);
|
||||
|
||||
// 触发 Quartz 中的 Job
|
||||
schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam());
|
||||
@ -122,7 +122,7 @@ public class JobServiceImpl implements JobService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteJob(Long id) throws SchedulerException {
|
||||
// 校验存在
|
||||
JobDO job = this.validateJobExists(id);
|
||||
JobDO job = validateJobExists(id);
|
||||
// 更新
|
||||
jobMapper.deleteById(id);
|
||||
|
||||
|
@ -29,7 +29,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@Import(ConfigServiceImpl.class)
|
||||
public class ConfigServiceTest extends BaseDbUnitTest {
|
||||
public class ConfigServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ConfigServiceImpl configService;
|
@ -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.FileClientConfig;
|
||||
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.enums.FileStorageEnum;
|
||||
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));
|
||||
}
|
||||
|
||||
@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() {
|
||||
return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage())
|
||||
.setConfig(new EmptyFileClientConfig());
|
||||
|
@ -26,7 +26,7 @@ import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@Import({FileServiceImpl.class})
|
||||
public class FileServiceTest extends BaseDbUnitTest {
|
||||
public class FileServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private FileService fileService;
|
Loading…
Reference in New Issue
Block a user