feat: extend operation log service and repository with pagination support

This commit is contained in:
张翔
2026-03-18 22:34:43 +08:00
parent 157aee2ffc
commit 8a0cd64829
81 changed files with 8842 additions and 509 deletions
@@ -0,0 +1,90 @@
package cn.novalon.manage.file.core.service.impl;
import cn.novalon.manage.file.core.domain.SysFile;
import cn.novalon.manage.file.core.repository.ISysFileRepository;
import cn.novalon.manage.file.core.service.ISysFileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class SysFileServiceTest {
@Mock
private ISysFileRepository fileRepository;
private ISysFileService fileService;
private SysFile testFile;
@BeforeEach
void setUp() {
fileService = new SysFileServiceImpl(fileRepository);
testFile = new SysFile();
testFile.setId(1L);
testFile.setFileName("test.txt");
testFile.setFilePath("/app/uploads/test.txt");
testFile.setFileType("text/plain");
testFile.setFileSize("1024");
testFile.setCreateBy("testuser");
testFile.setStorageType("LOCAL");
}
@Test
void testGetAllFiles_Success() {
when(fileRepository.findByDeletedAtIsNullOrderByCreatedAtDesc()).thenReturn(Flux.just(testFile));
Flux<SysFile> result = fileService.getAllFiles();
StepVerifier.create(result)
.expectNext(testFile)
.verifyComplete();
verify(fileRepository).findByDeletedAtIsNullOrderByCreatedAtDesc();
}
@Test
void testGetFileById_Success() {
when(fileRepository.findById(1L)).thenReturn(Mono.just(testFile));
Mono<SysFile> result = fileService.getFileById(1L);
StepVerifier.create(result)
.expectNext(testFile)
.verifyComplete();
verify(fileRepository).findById(1L);
}
@Test
void testGetFileById_NotFound() {
when(fileRepository.findById(999L)).thenReturn(Mono.empty());
Mono<SysFile> result = fileService.getFileById(999L);
StepVerifier.create(result)
.verifyComplete();
verify(fileRepository).findById(999L);
}
@Test
void testDeleteFile_NotFound() {
when(fileRepository.findById(999L)).thenReturn(Mono.empty());
Mono<Void> result = fileService.deleteFile(999L);
StepVerifier.create(result)
.verifyComplete();
verify(fileRepository).findById(999L);
verify(fileRepository, never()).deleteByIdAndDeletedAtIsNull(any());
}
}
@@ -0,0 +1,260 @@
package cn.novalon.manage.file.handler;
import cn.novalon.manage.file.core.domain.SysFile;
import cn.novalon.manage.file.core.service.ISysFileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.reactive.function.server.MockServerRequest;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class SysFileHandlerTest {
@Mock
private ISysFileService fileService;
private SysFileHandler fileHandler;
private SysFile testFile;
@BeforeEach
void setUp() {
fileHandler = new SysFileHandler(fileService);
testFile = new SysFile();
testFile.setId(1L);
testFile.setFileName("test.txt");
testFile.setFilePath("/app/uploads/test.txt");
testFile.setFileType("text/plain");
testFile.setFileSize("1024");
testFile.setCreateBy("testuser");
}
@Test
void testGetAllFiles_Success() {
when(fileService.getAllFiles()).thenReturn(Flux.just(testFile));
ServerRequest request = MockServerRequest.builder().build();
Mono<ServerResponse> response = fileHandler.getAllFiles(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.OK)
.verifyComplete();
verify(fileService).getAllFiles();
}
@Test
void testGetFileById_Success() {
when(fileService.getFileById(1L)).thenReturn(Mono.just(testFile));
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "1")
.build();
Mono<ServerResponse> response = fileHandler.getFileById(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.OK)
.verifyComplete();
verify(fileService).getFileById(1L);
}
@Test
void testGetFileById_NotFound() {
when(fileService.getFileById(999L)).thenReturn(Mono.empty());
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "999")
.build();
Mono<ServerResponse> response = fileHandler.getFileById(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getFileById(999L);
}
@Test
void testDeleteFile_Success() {
when(fileService.deleteFile(1L)).thenReturn(Mono.empty());
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "1")
.build();
Mono<ServerResponse> response = fileHandler.deleteFile(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.OK)
.verifyComplete();
verify(fileService).deleteFile(1L);
}
@Test
void testDeleteFile_NotFound() {
when(fileService.deleteFile(999L)).thenReturn(Mono.empty());
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "999")
.build();
Mono<ServerResponse> response = fileHandler.deleteFile(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.OK)
.verifyComplete();
verify(fileService).deleteFile(999L);
}
@Test
void testDownloadFile_Success() {
when(fileService.getFileById(1L)).thenReturn(Mono.just(testFile));
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "1")
.build();
Mono<ServerResponse> response = fileHandler.downloadFile(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getFileById(1L);
}
@Test
void testDownloadFile_NotFound() {
when(fileService.getFileById(999L)).thenReturn(Mono.empty());
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "999")
.build();
Mono<ServerResponse> response = fileHandler.downloadFile(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getFileById(999L);
}
@Test
void testDownloadFileByName_Success() {
when(fileService.getAllFiles()).thenReturn(Flux.just(testFile));
ServerRequest request = MockServerRequest.builder()
.pathVariable("fileName", "test.txt")
.build();
Mono<ServerResponse> response = fileHandler.downloadFileByName(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getAllFiles();
}
@Test
void testDownloadFileByName_NotFound() {
when(fileService.getAllFiles()).thenReturn(Flux.empty());
ServerRequest request = MockServerRequest.builder()
.pathVariable("fileName", "nonexistent.txt")
.build();
Mono<ServerResponse> response = fileHandler.downloadFileByName(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getAllFiles();
}
@Test
void testPreviewFile_Success() {
when(fileService.getFileById(1L)).thenReturn(Mono.just(testFile));
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "1")
.build();
Mono<ServerResponse> response = fileHandler.previewFile(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getFileById(1L);
}
@Test
void testPreviewFile_NotFound() {
when(fileService.getFileById(999L)).thenReturn(Mono.empty());
ServerRequest request = MockServerRequest.builder()
.pathVariable("id", "999")
.build();
Mono<ServerResponse> response = fileHandler.previewFile(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getFileById(999L);
}
@Test
void testPreviewFileByName_Success() {
when(fileService.getAllFiles()).thenReturn(Flux.just(testFile));
ServerRequest request = MockServerRequest.builder()
.pathVariable("fileName", "test.txt")
.build();
Mono<ServerResponse> response = fileHandler.previewFileByName(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getAllFiles();
}
@Test
void testPreviewFileByName_NotFound() {
when(fileService.getAllFiles()).thenReturn(Flux.empty());
ServerRequest request = MockServerRequest.builder()
.pathVariable("fileName", "nonexistent.txt")
.build();
Mono<ServerResponse> response = fileHandler.previewFileByName(request);
StepVerifier.create(response)
.expectNextMatches(serverResponse ->
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
.verifyComplete();
verify(fileService).getAllFiles();
}
}