| | |
| | | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| | | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| | | import cn.iocoder.yudao.framework.test.core.util.AssertUtils; |
| | | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO; |
| | | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
| | | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; |
| | | import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; |
| | |
| | | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| | | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| | | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; |
| | | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_PATH_INVALID; |
| | | import static org.junit.jupiter.api.Assertions.*; |
| | | import static org.mockito.ArgumentMatchers.same; |
| | | import static org.mockito.Mockito.*; |
| | |
| | | } |
| | | |
| | | @Test |
| | | public void testDeleteFile_pathInvalid() { |
| | | // mock 数据 |
| | | FileDO dbFile = randomPojo(FileDO.class, o -> o.setConfigId(10L).setPath("../tudou.jpg")); |
| | | fileMapper.insert(dbFile); |
| | | |
| | | // 调用,并断言异常 |
| | | assertServiceException(() -> fileService.deleteFile(dbFile.getId()), FILE_PATH_INVALID); |
| | | } |
| | | |
| | | @Test |
| | | public void testGetFileContent() throws Exception { |
| | | // 准备参数 |
| | | Long configId = 10L; |
| | |
| | | byte[] result = fileService.getFileContent(configId, path); |
| | | // 断言 |
| | | assertSame(result, content); |
| | | } |
| | | |
| | | @Test |
| | | public void testGetFileContent_pathInvalid() { |
| | | // 准备参数 |
| | | Long configId = 10L; |
| | | String path = "../tudou.jpg"; |
| | | |
| | | // 调用,并断言异常 |
| | | assertServiceException(() -> fileService.getFileContent(configId, path), FILE_PATH_INVALID); |
| | | } |
| | | |
| | | @Test |
| | | public void testGetFileByConfigIdAndPath() { |
| | | // mock 数据 |
| | | FileDO dbFile = randomPojo(FileDO.class, o -> o.setConfigId(10L).setPath("avatar/中文 100%+文件.jpg")); |
| | | fileMapper.insert(dbFile); |
| | | FileDO latestFile = ObjectUtils.cloneIgnoreId(dbFile, o -> o.setName("最新文件名.jpg")); |
| | | fileMapper.insert(latestFile); |
| | | fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setPath("avatar/other.jpg"))); |
| | | fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setConfigId(20L))); |
| | | |
| | | // 调用 |
| | | FileDO result = fileService.getFileByConfigIdAndPath(10L, "avatar/中文 100%+文件.jpg"); |
| | | |
| | | // 断言 |
| | | AssertUtils.assertPojoEquals(latestFile, result); |
| | | } |
| | | |
| | | @Test |
| | | public void testCreateFileByPresignedPath_success() { |
| | | // 准备参数 |
| | | FileCreateReqVO reqVO = randomPojo(FileCreateReqVO.class, o -> { |
| | | o.setPath("avatar/test.jpg"); |
| | | o.setName("test.jpg"); |
| | | o.setUrl("https://www.iocoder.cn/test.jpg?token=123"); |
| | | }); |
| | | |
| | | // 调用 |
| | | Long fileId = fileService.createFile(reqVO); |
| | | |
| | | // 断言 |
| | | FileDO file = fileMapper.selectById(fileId); |
| | | assertEquals("avatar/test.jpg", file.getPath()); |
| | | assertEquals("test.jpg", file.getName()); |
| | | assertEquals("https://www.iocoder.cn/test.jpg", file.getUrl()); |
| | | } |
| | | |
| | | @Test |
| | | public void testCreateFileByPresignedPath_nameInvalid() { |
| | | // 准备参数 |
| | | FileCreateReqVO reqVO = randomPojo(FileCreateReqVO.class, o -> { |
| | | o.setPath("avatar/test.jpg"); |
| | | o.setName("../test.jpg"); |
| | | }); |
| | | |
| | | // 调用,并断言异常 |
| | | assertServiceException(() -> fileService.createFile(reqVO), FILE_PATH_INVALID); |
| | | } |
| | | |
| | | @Test |
| | | public void testCreateFileByPresignedPath_pathInvalid() { |
| | | // 准备参数 |
| | | FileCreateReqVO reqVO = randomPojo(FileCreateReqVO.class, o -> { |
| | | o.setPath("../test.jpg"); |
| | | o.setName("test.jpg"); |
| | | }); |
| | | |
| | | // 调用,并断言异常 |
| | | assertServiceException(() -> fileService.createFile(reqVO), FILE_PATH_INVALID); |
| | | } |
| | | |
| | | @Test |
| | |
| | | } |
| | | |
| | | @Test |
| | | public void testGenerateUploadPath_FileNameInvalid() { |
| | | // 准备参数 |
| | | String name = "../test.jpg"; |
| | | String directory = "avatar"; |
| | | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = false; |
| | | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = false; |
| | | |
| | | // 调用,并断言异常 |
| | | assertServiceException(() -> fileService.generateUploadPath(name, directory), FILE_PATH_INVALID); |
| | | } |
| | | |
| | | @Test |
| | | public void testGenerateUploadPath_DirectoryInvalid() { |
| | | // 准备参数 |
| | | String name = "test.jpg"; |
| | | String directory = "../avatar"; |
| | | |
| | | // 调用,并断言异常 |
| | | assertServiceException(() -> fileService.generateUploadPath(name, directory), FILE_PATH_INVALID); |
| | | } |
| | | |
| | | @Test |
| | | public void testGenerateUploadPath_DirectoryEmpty() { |
| | | // 准备参数 |
| | | String name = "test.jpg"; |