|
@@ -3,19 +3,20 @@ package cn.iocoder.yudao.module.infra.service.file;
|
|
|
import cn.hutool.core.io.resource.ResourceUtil;
|
|
import cn.hutool.core.io.resource.ResourceUtil;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
|
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
|
|
-import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClient;
|
|
|
|
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
|
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
|
|
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
|
|
|
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.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.dataobject.file.FileDO;
|
|
|
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
|
|
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
|
|
|
|
|
+import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClient;
|
|
|
|
|
+import jakarta.annotation.Resource;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
|
import org.springframework.context.annotation.Import;
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
|
|
|
-import jakarta.annotation.Resource;
|
|
|
|
|
-
|
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
|
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
|
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
@@ -29,7 +30,7 @@ import static org.mockito.Mockito.*;
|
|
|
public class FileServiceImplTest extends BaseDbUnitTest {
|
|
public class FileServiceImplTest extends BaseDbUnitTest {
|
|
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
|
- private FileService fileService;
|
|
|
|
|
|
|
+ private FileServiceImpl fileService;
|
|
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
|
private FileMapper fileMapper;
|
|
private FileMapper fileMapper;
|
|
@@ -37,6 +38,12 @@ public class FileServiceImplTest extends BaseDbUnitTest {
|
|
|
@MockBean
|
|
@MockBean
|
|
|
private FileConfigService fileConfigService;
|
|
private FileConfigService fileConfigService;
|
|
|
|
|
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ public void setUp() {
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Test
|
|
@Test
|
|
|
public void testGetFilePage() {
|
|
public void testGetFilePage() {
|
|
|
// mock 数据
|
|
// mock 数据
|
|
@@ -70,28 +77,69 @@ public class FileServiceImplTest extends BaseDbUnitTest {
|
|
|
AssertUtils.assertPojoEquals(dbFile, pageResult.getList().get(0));
|
|
AssertUtils.assertPojoEquals(dbFile, pageResult.getList().get(0));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * content、name、directory、type 都非空
|
|
|
|
|
+ */
|
|
|
@Test
|
|
@Test
|
|
|
- public void testCreateFile_success() throws Exception {
|
|
|
|
|
|
|
+ public void testCreateFile_success_01() throws Exception {
|
|
|
// 准备参数
|
|
// 准备参数
|
|
|
- String path = randomString();
|
|
|
|
|
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
|
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
|
|
|
|
+ String name = "单测文件名";
|
|
|
|
|
+ String directory = randomString();
|
|
|
|
|
+ String type = "image/jpeg";
|
|
|
// mock Master 文件客户端
|
|
// mock Master 文件客户端
|
|
|
FileClient client = mock(FileClient.class);
|
|
FileClient client = mock(FileClient.class);
|
|
|
when(fileConfigService.getMasterFileClient()).thenReturn(client);
|
|
when(fileConfigService.getMasterFileClient()).thenReturn(client);
|
|
|
String url = randomString();
|
|
String url = randomString();
|
|
|
- when(client.upload(same(content), same(path), eq("image/jpeg"))).thenReturn(url);
|
|
|
|
|
|
|
+ AtomicReference<String> pathRef = new AtomicReference<>();
|
|
|
|
|
+ when(client.upload(same(content), argThat(path -> {
|
|
|
|
|
+ assertTrue(path.matches(directory + "/\\d{8}/" + name + "_\\d+.jpg"));
|
|
|
|
|
+ pathRef.set(path);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }), eq(type))).thenReturn(url);
|
|
|
when(client.getId()).thenReturn(10L);
|
|
when(client.getId()).thenReturn(10L);
|
|
|
- String name = "单测文件名";
|
|
|
|
|
// 调用
|
|
// 调用
|
|
|
- String result = fileService.createFile(name, path, content);
|
|
|
|
|
|
|
+ String result = fileService.createFile(content, name, directory, type);
|
|
|
// 断言
|
|
// 断言
|
|
|
assertEquals(result, url);
|
|
assertEquals(result, url);
|
|
|
// 校验数据
|
|
// 校验数据
|
|
|
- FileDO file = fileMapper.selectOne(FileDO::getPath, path);
|
|
|
|
|
|
|
+ FileDO file = fileMapper.selectOne(FileDO::getUrl, url);
|
|
|
assertEquals(10L, file.getConfigId());
|
|
assertEquals(10L, file.getConfigId());
|
|
|
- assertEquals(path, file.getPath());
|
|
|
|
|
|
|
+ assertEquals(pathRef.get(), file.getPath());
|
|
|
assertEquals(url, file.getUrl());
|
|
assertEquals(url, file.getUrl());
|
|
|
- assertEquals("image/jpeg", file.getType());
|
|
|
|
|
|
|
+ assertEquals(type, file.getType());
|
|
|
|
|
+ assertEquals(content.length, file.getSize());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * content 非空,其它都空
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testCreateFile_success_02() throws Exception {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
|
|
|
|
+ // mock Master 文件客户端
|
|
|
|
|
+ String type = "image/jpeg";
|
|
|
|
|
+ FileClient client = mock(FileClient.class);
|
|
|
|
|
+ when(fileConfigService.getMasterFileClient()).thenReturn(client);
|
|
|
|
|
+ String url = randomString();
|
|
|
|
|
+ AtomicReference<String> pathRef = new AtomicReference<>();
|
|
|
|
|
+ when(client.upload(same(content), argThat(path -> {
|
|
|
|
|
+ assertTrue(path.matches("\\d{8}/6318848e882d8a7e7e82789d87608f684ee52d41966bfc8cad3ce15aad2b970e_\\d+\\.jpg"));
|
|
|
|
|
+ pathRef.set(path);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }), eq(type))).thenReturn(url);
|
|
|
|
|
+ when(client.getId()).thenReturn(10L);
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String result = fileService.createFile(content, null, null, null);
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ assertEquals(result, url);
|
|
|
|
|
+ // 校验数据
|
|
|
|
|
+ FileDO file = fileMapper.selectOne(FileDO::getUrl, url);
|
|
|
|
|
+ assertEquals(10L, file.getConfigId());
|
|
|
|
|
+ assertEquals(pathRef.get(), file.getPath());
|
|
|
|
|
+ assertEquals(url, file.getUrl());
|
|
|
|
|
+ assertEquals(type, file.getType());
|
|
|
assertEquals(content.length, file.getSize());
|
|
assertEquals(content.length, file.getSize());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -140,4 +188,122 @@ public class FileServiceImplTest extends BaseDbUnitTest {
|
|
|
assertSame(result, content);
|
|
assertSame(result, content);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testGenerateUploadPath_AllEnabled() {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ String name = "test.jpg";
|
|
|
|
|
+ String directory = "avatar";
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true;
|
|
|
|
|
+
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String path = fileService.generateUploadPath(name, directory);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ // 格式为:avatar/yyyyMMdd/test_timestamp.jpg
|
|
|
|
|
+ assertTrue(path.startsWith(directory + "/"));
|
|
|
|
|
+ // 包含日期格式:8 位数字,如 20240517
|
|
|
|
|
+ assertTrue(path.matches(directory + "/\\d{8}/test_\\d+\\.jpg"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testGenerateUploadPath_PrefixEnabled_SuffixDisabled() {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ String name = "test.jpg";
|
|
|
|
|
+ String directory = "avatar";
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = false;
|
|
|
|
|
+
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String path = fileService.generateUploadPath(name, directory);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ // 格式为:avatar/yyyyMMdd/test.jpg
|
|
|
|
|
+ assertTrue(path.startsWith(directory + "/"));
|
|
|
|
|
+ // 包含日期格式:8 位数字,如 20240517
|
|
|
|
|
+ assertTrue(path.matches(directory + "/\\d{8}/test\\.jpg"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testGenerateUploadPath_PrefixDisabled_SuffixEnabled() {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ String name = "test.jpg";
|
|
|
|
|
+ String directory = "avatar";
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = false;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true;
|
|
|
|
|
+
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String path = fileService.generateUploadPath(name, directory);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ // 格式为:avatar/test_timestamp.jpg
|
|
|
|
|
+ assertTrue(path.startsWith(directory + "/"));
|
|
|
|
|
+ assertTrue(path.matches(directory + "/test_\\d+\\.jpg"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testGenerateUploadPath_AllDisabled() {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ String name = "test.jpg";
|
|
|
|
|
+ String directory = "avatar";
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = false;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = false;
|
|
|
|
|
+
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String path = fileService.generateUploadPath(name, directory);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ // 格式为:avatar/test.jpg
|
|
|
|
|
+ assertEquals(directory + "/" + name, path);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testGenerateUploadPath_NoExtension() {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ String name = "test";
|
|
|
|
|
+ String directory = "avatar";
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true;
|
|
|
|
|
+
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String path = fileService.generateUploadPath(name, directory);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ // 格式为:avatar/yyyyMMdd/test_timestamp
|
|
|
|
|
+ assertTrue(path.startsWith(directory + "/"));
|
|
|
|
|
+ assertTrue(path.matches(directory + "/\\d{8}/test_\\d+"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testGenerateUploadPath_DirectoryNull() {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ String name = "test.jpg";
|
|
|
|
|
+ String directory = null;
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true;
|
|
|
|
|
+
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String path = fileService.generateUploadPath(name, directory);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ // 格式为:yyyyMMdd/test_timestamp.jpg
|
|
|
|
|
+ assertTrue(path.matches("\\d{8}/test_\\d+\\.jpg"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testGenerateUploadPath_DirectoryEmpty() {
|
|
|
|
|
+ // 准备参数
|
|
|
|
|
+ String name = "test.jpg";
|
|
|
|
|
+ String directory = "";
|
|
|
|
|
+ FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true;
|
|
|
|
|
+ FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true;
|
|
|
|
|
+
|
|
|
|
|
+ // 调用
|
|
|
|
|
+ String path = fileService.generateUploadPath(name, directory);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言
|
|
|
|
|
+ // 格式为:yyyyMMdd/test_timestamp.jpg
|
|
|
|
|
+ assertTrue(path.matches("\\d{8}/test_\\d+\\.jpg"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|