|
@@ -3,11 +3,11 @@ package cn.iocoder.yudao.module.infra.service.file;
|
|
|
import cn.hutool.core.io.resource.ResourceUtil;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
|
|
+import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
|
|
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
|
|
|
-import cn.iocoder.yudao.module.infra.controller.admin.file.vo.FilePageReqVO;
|
|
|
+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 cn.iocoder.yudao.module.infra.framework.file.config.FileProperties;
|
|
|
import cn.iocoder.yudao.module.infra.test.BaseDbUnitTest;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
@@ -17,47 +17,46 @@ import javax.annotation.Resource;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
|
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
|
-import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
|
|
-import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
|
|
|
+import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
|
|
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.same;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
|
-@Import({FileServiceImpl.class, FileProperties.class})
|
|
|
+@Import({FileServiceImpl.class})
|
|
|
public class FileServiceTest extends BaseDbUnitTest {
|
|
|
|
|
|
@Resource
|
|
|
private FileService fileService;
|
|
|
|
|
|
- @MockBean
|
|
|
- private FileProperties fileProperties;
|
|
|
-
|
|
|
@Resource
|
|
|
private FileMapper fileMapper;
|
|
|
|
|
|
+ @MockBean
|
|
|
+ private FileConfigService fileConfigService;
|
|
|
+
|
|
|
@Test
|
|
|
public void testGetFilePage() {
|
|
|
// mock 数据
|
|
|
FileDO dbFile = randomPojo(FileDO.class, o -> { // 等会查询到
|
|
|
- o.setId("yunai");
|
|
|
+ o.setPath("yunai");
|
|
|
o.setType("jpg");
|
|
|
o.setCreateTime(buildTime(2021, 1, 15));
|
|
|
});
|
|
|
fileMapper.insert(dbFile);
|
|
|
- // 测试 id 不匹配
|
|
|
- fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setId("tudou")));
|
|
|
+ // 测试 path 不匹配
|
|
|
+ fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setPath("tudou")));
|
|
|
// 测试 type 不匹配
|
|
|
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
|
|
|
- o.setId("yunai02");
|
|
|
o.setType("png");
|
|
|
}));
|
|
|
// 测试 createTime 不匹配
|
|
|
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
|
|
|
- o.setId("yunai03");
|
|
|
o.setCreateTime(buildTime(2020, 1, 15));
|
|
|
}));
|
|
|
// 准备参数
|
|
|
FilePageReqVO reqVO = new FilePageReqVO();
|
|
|
- reqVO.setId("yunai");
|
|
|
+ reqVO.setPath("yunai");
|
|
|
reqVO.setType("jp");
|
|
|
reqVO.setBeginCreateTime(buildTime(2021, 1, 10));
|
|
|
reqVO.setEndCreateTime(buildTime(2021, 1, 20));
|
|
@@ -67,7 +66,7 @@ public class FileServiceTest extends BaseDbUnitTest {
|
|
|
// 断言
|
|
|
assertEquals(1, pageResult.getTotal());
|
|
|
assertEquals(1, pageResult.getList().size());
|
|
|
- AssertUtils.assertPojoEquals(dbFile, pageResult.getList().get(0), "content");
|
|
|
+ AssertUtils.assertPojoEquals(dbFile, pageResult.getList().get(0));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -75,52 +74,68 @@ public class FileServiceTest extends BaseDbUnitTest {
|
|
|
// 准备参数
|
|
|
String path = randomString();
|
|
|
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
|
|
+ // mock Master 文件客户端
|
|
|
+ FileClient client = mock(FileClient.class);
|
|
|
+ when(fileConfigService.getMasterFileClient()).thenReturn(client);
|
|
|
+ String url = randomString();
|
|
|
+ when(client.upload(same(content), same(path))).thenReturn(url);
|
|
|
+ when(client.getId()).thenReturn(10L);
|
|
|
|
|
|
// 调用
|
|
|
- String url = fileService.createFile(path, content);
|
|
|
+ String result = fileService.createFile(path, content);
|
|
|
// 断言
|
|
|
- assertEquals(fileProperties.getBasePath() + path, url);
|
|
|
+ assertEquals(result, url);
|
|
|
// 校验数据
|
|
|
- FileDO file = fileMapper.selectById(path);
|
|
|
- assertEquals(path, file.getId());
|
|
|
+ FileDO file = fileMapper.selectOne(FileDO::getPath, path);
|
|
|
+ assertEquals(10L, file.getConfigId());
|
|
|
+ assertEquals(path, file.getPath());
|
|
|
+ assertEquals(url, file.getUrl());
|
|
|
assertEquals("jpg", file.getType());
|
|
|
- assertArrayEquals(content, file.getContent());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void testCreateFile_exists() {
|
|
|
- // mock 数据
|
|
|
- FileDO dbFile = randomPojo(FileDO.class);
|
|
|
- fileMapper.insert(dbFile);
|
|
|
- // 准备参数
|
|
|
- String path = dbFile.getId(); // 模拟已存在
|
|
|
- byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
|
|
-
|
|
|
- // 调用,并断言异常
|
|
|
- assertServiceException(() -> fileService.createFile(path, content), FILE_PATH_EXISTS);
|
|
|
+ assertEquals(content.length, file.getSize());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testDeleteFile_success() {
|
|
|
// mock 数据
|
|
|
- FileDO dbFile = randomPojo(FileDO.class);
|
|
|
+ FileDO dbFile = randomPojo(FileDO.class, o -> o.setConfigId(10L).setPath("tudou.jpg"));
|
|
|
fileMapper.insert(dbFile);// @Sql: 先插入出一条存在的数据
|
|
|
+ // mock Master 文件客户端
|
|
|
+ FileClient client = mock(FileClient.class);
|
|
|
+ when(fileConfigService.getFileClient(eq(10L))).thenReturn(client);
|
|
|
// 准备参数
|
|
|
- String id = dbFile.getId();
|
|
|
+ Long id = dbFile.getId();
|
|
|
|
|
|
// 调用
|
|
|
fileService.deleteFile(id);
|
|
|
// 校验数据不存在了
|
|
|
assertNull(fileMapper.selectById(id));
|
|
|
+ // 校验调用
|
|
|
+ verify(client).delete(eq("tudou.jpg"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testDeleteFile_notExists() {
|
|
|
// 准备参数
|
|
|
- String id = randomString();
|
|
|
+ Long id = randomLongId();
|
|
|
|
|
|
// 调用, 并断言异常
|
|
|
assertServiceException(() -> fileService.deleteFile(id), FILE_NOT_EXISTS);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testGetFileContent() {
|
|
|
+ // 准备参数
|
|
|
+ Long configId = 10L;
|
|
|
+ String path = "tudou.jpg";
|
|
|
+ // mock 方法
|
|
|
+ FileClient client = mock(FileClient.class);
|
|
|
+ when(fileConfigService.getFileClient(eq(10L))).thenReturn(client);
|
|
|
+ byte[] content = new byte[]{};
|
|
|
+ when(client.getContent(eq("tudou.jpg"))).thenReturn(content);
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ byte[] result = fileService.getFileContent(configId, path);
|
|
|
+ // 断言
|
|
|
+ assertSame(result, content);
|
|
|
+ }
|
|
|
}
|