提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.service.file; |
H |
2 |
|
|
3 |
import cn.hutool.core.io.resource.ResourceUtil; |
|
4 |
import com.iailab.framework.common.pojo.PageResult; |
|
5 |
import com.iailab.framework.common.util.object.ObjectUtils; |
|
6 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
7 |
import com.iailab.framework.test.core.util.AssertUtils; |
|
8 |
import com.iailab.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
|
9 |
import com.iailab.module.infra.dal.dataobject.file.FileDO; |
|
10 |
import com.iailab.module.infra.dal.mysql.file.FileMapper; |
|
11 |
import com.iailab.module.infra.framework.file.core.client.FileClient; |
|
12 |
import org.junit.jupiter.api.Test; |
|
13 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
14 |
import org.springframework.context.annotation.Import; |
|
15 |
|
|
16 |
import javax.annotation.Resource; |
|
17 |
|
|
18 |
import java.time.LocalDateTime; |
|
19 |
|
|
20 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
21 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
22 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
23 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; |
|
24 |
import static org.junit.jupiter.api.Assertions.*; |
|
25 |
import static org.mockito.ArgumentMatchers.same; |
|
26 |
import static org.mockito.Mockito.*; |
|
27 |
|
|
28 |
@Import({FileServiceImpl.class}) |
|
29 |
public class FileServiceImplTest extends BaseDbUnitTest { |
|
30 |
|
|
31 |
@Resource |
|
32 |
private FileService fileService; |
|
33 |
|
|
34 |
@Resource |
|
35 |
private FileMapper fileMapper; |
|
36 |
|
|
37 |
@MockBean |
|
38 |
private FileConfigService fileConfigService; |
|
39 |
|
|
40 |
@Test |
|
41 |
public void testGetFilePage() { |
|
42 |
// mock 数据 |
|
43 |
FileDO dbFile = randomPojo(FileDO.class, o -> { // 等会查询到 |
|
44 |
o.setPath("yunai"); |
|
45 |
o.setType("image/jpg"); |
|
46 |
o.setCreateTime(buildTime(2021, 1, 15)); |
|
47 |
}); |
|
48 |
fileMapper.insert(dbFile); |
|
49 |
// 测试 path 不匹配 |
|
50 |
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setPath("tudou"))); |
|
51 |
// 测试 type 不匹配 |
|
52 |
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> { |
|
53 |
o.setType("image/png"); |
|
54 |
})); |
|
55 |
// 测试 createTime 不匹配 |
|
56 |
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> { |
|
57 |
o.setCreateTime(buildTime(2020, 1, 15)); |
|
58 |
})); |
|
59 |
// 准备参数 |
|
60 |
FilePageReqVO reqVO = new FilePageReqVO(); |
|
61 |
reqVO.setPath("yunai"); |
|
62 |
reqVO.setType("jp"); |
|
63 |
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 10), buildTime(2021, 1, 20)})); |
|
64 |
|
|
65 |
// 调用 |
|
66 |
PageResult<FileDO> pageResult = fileService.getFilePage(reqVO); |
|
67 |
// 断言 |
|
68 |
assertEquals(1, pageResult.getTotal()); |
|
69 |
assertEquals(1, pageResult.getList().size()); |
|
70 |
AssertUtils.assertPojoEquals(dbFile, pageResult.getList().get(0)); |
|
71 |
} |
|
72 |
|
|
73 |
@Test |
|
74 |
public void testCreateFile_success() throws Exception { |
|
75 |
// 准备参数 |
|
76 |
String path = randomString(); |
|
77 |
byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); |
|
78 |
// mock Master 文件客户端 |
|
79 |
FileClient client = mock(FileClient.class); |
|
80 |
when(fileConfigService.getMasterFileClient()).thenReturn(client); |
|
81 |
String url = randomString(); |
|
82 |
when(client.upload(same(content), same(path), eq("image/jpeg"))).thenReturn(url); |
|
83 |
when(client.getId()).thenReturn(10L); |
|
84 |
String name = "单测文件名"; |
|
85 |
// 调用 |
|
86 |
String result = fileService.createFile(name, path, content); |
|
87 |
// 断言 |
|
88 |
assertEquals(result, url); |
|
89 |
// 校验数据 |
|
90 |
FileDO file = fileMapper.selectOne(FileDO::getPath, path); |
|
91 |
assertEquals(10L, file.getConfigId()); |
|
92 |
assertEquals(path, file.getPath()); |
|
93 |
assertEquals(url, file.getUrl()); |
|
94 |
assertEquals("image/jpeg", file.getType()); |
|
95 |
assertEquals(content.length, file.getSize()); |
|
96 |
} |
|
97 |
|
|
98 |
@Test |
|
99 |
public void testDeleteFile_success() throws Exception { |
|
100 |
// mock 数据 |
|
101 |
FileDO dbFile = randomPojo(FileDO.class, o -> o.setConfigId(10L).setPath("tudou.jpg")); |
|
102 |
fileMapper.insert(dbFile);// @Sql: 先插入出一条存在的数据 |
|
103 |
// mock Master 文件客户端 |
|
104 |
FileClient client = mock(FileClient.class); |
|
105 |
when(fileConfigService.getFileClient(eq(10L))).thenReturn(client); |
|
106 |
// 准备参数 |
|
107 |
Long id = dbFile.getId(); |
|
108 |
|
|
109 |
// 调用 |
|
110 |
fileService.deleteFile(id); |
|
111 |
// 校验数据不存在了 |
|
112 |
assertNull(fileMapper.selectById(id)); |
|
113 |
// 校验调用 |
|
114 |
verify(client).delete(eq("tudou.jpg")); |
|
115 |
} |
|
116 |
|
|
117 |
@Test |
|
118 |
public void testDeleteFile_notExists() { |
|
119 |
// 准备参数 |
|
120 |
Long id = randomLongId(); |
|
121 |
|
|
122 |
// 调用, 并断言异常 |
|
123 |
assertServiceException(() -> fileService.deleteFile(id), FILE_NOT_EXISTS); |
|
124 |
} |
|
125 |
|
|
126 |
@Test |
|
127 |
public void testGetFileContent() throws Exception { |
|
128 |
// 准备参数 |
|
129 |
Long configId = 10L; |
|
130 |
String path = "tudou.jpg"; |
|
131 |
// mock 方法 |
|
132 |
FileClient client = mock(FileClient.class); |
|
133 |
when(fileConfigService.getFileClient(eq(10L))).thenReturn(client); |
|
134 |
byte[] content = new byte[]{}; |
|
135 |
when(client.getContent(eq("tudou.jpg"))).thenReturn(content); |
|
136 |
|
|
137 |
// 调用 |
|
138 |
byte[] result = fileService.getFileContent(configId, path); |
|
139 |
// 断言 |
|
140 |
assertSame(result, content); |
|
141 |
} |
|
142 |
|
|
143 |
} |