feat: 添加异常日志功能并优化UI样式
refactor: 重构后端查询逻辑和API响应处理 fix: 修复用户角色更新和文件上传问题 test: 添加前端性能测试脚本和E2E测试用例 chore: 更新依赖版本和配置文件 docs: 添加环境检查脚本和测试文档 style: 统一表格标签样式和路由命名 perf: 优化前端页面加载速度和响应时间
This commit is contained in:
-3
@@ -6,10 +6,7 @@ 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.HttpMethod;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
+281
@@ -0,0 +1,281 @@
|
||||
package cn.novalon.manage.sys.core.command;
|
||||
|
||||
import cn.novalon.manage.common.util.StatusConstants;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CreateRoleCommandTest {
|
||||
|
||||
@Test
|
||||
void testConstructor() {
|
||||
CreateRoleCommand command = new CreateRoleCommand(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
1
|
||||
);
|
||||
|
||||
assertEquals("Admin", command.roleName());
|
||||
assertEquals("admin", command.roleKey());
|
||||
assertEquals(1, command.roleSort());
|
||||
assertEquals(1, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithValidStatus() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals("Admin", command.roleName());
|
||||
assertEquals("admin", command.roleKey());
|
||||
assertEquals(1, command.roleSort());
|
||||
assertEquals(StatusConstants.ENABLED, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithDisabledStatus() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
StatusConstants.DISABLED
|
||||
);
|
||||
|
||||
assertEquals("Admin", command.roleName());
|
||||
assertEquals("admin", command.roleKey());
|
||||
assertEquals(1, command.roleSort());
|
||||
assertEquals(StatusConstants.DISABLED, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNullStatus() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
null
|
||||
);
|
||||
|
||||
assertEquals("Admin", command.roleName());
|
||||
assertEquals("admin", command.roleKey());
|
||||
assertEquals(1, command.roleSort());
|
||||
assertNull(command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithInvalidStatus() {
|
||||
IllegalArgumentException exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
999
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals("Invalid status value. Status must be 0 (disabled) or 1 (enabled)", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithInvalidStatus_Negative() {
|
||||
IllegalArgumentException exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
-1
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals("Invalid status value. Status must be 0 (disabled) or 1 (enabled)", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithInvalidStatus_Two() {
|
||||
IllegalArgumentException exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
2
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals("Invalid status value. Status must be 0 (disabled) or 1 (enabled)", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNullValues() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
assertNull(command.roleName());
|
||||
assertNull(command.roleKey());
|
||||
assertNull(command.roleSort());
|
||||
assertNull(command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithEmptyStrings() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"",
|
||||
"",
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
assertEquals("", command.roleName());
|
||||
assertEquals("", command.roleKey());
|
||||
assertNull(command.roleSort());
|
||||
assertNull(command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithBoundaryValues() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"a",
|
||||
"a",
|
||||
Integer.MAX_VALUE,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals("a", command.roleName());
|
||||
assertEquals("a", command.roleKey());
|
||||
assertEquals(Integer.MAX_VALUE, command.roleSort());
|
||||
assertEquals(StatusConstants.ENABLED, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithZeroValues() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
0,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals(0, command.roleSort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNegativeSort() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
-1,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals(-1, command.roleSort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithSpecialCharacters() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"Admin@#$%",
|
||||
"admin@#$%",
|
||||
1,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals("Admin@#$%", command.roleName());
|
||||
assertEquals("admin@#$%", command.roleKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithLongStrings() {
|
||||
String longRoleName = "a".repeat(1000);
|
||||
String longRoleKey = "b".repeat(1000);
|
||||
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
longRoleName,
|
||||
longRoleKey,
|
||||
1,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals(longRoleName, command.roleName());
|
||||
assertEquals(longRoleKey, command.roleKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithUnicodeCharacters() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"管理员_测试",
|
||||
"admin_测试",
|
||||
1,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals("管理员_测试", command.roleName());
|
||||
assertEquals("admin_测试", command.roleKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithWhitespace() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
" Admin ",
|
||||
" admin ",
|
||||
1,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals(" Admin ", command.roleName());
|
||||
assertEquals(" admin ", command.roleKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNumericStrings() {
|
||||
CreateRoleCommand command = CreateRoleCommand.of(
|
||||
"12345",
|
||||
"67890",
|
||||
1,
|
||||
StatusConstants.ENABLED
|
||||
);
|
||||
|
||||
assertEquals("12345", command.roleName());
|
||||
assertEquals("67890", command.roleKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValidateStatus_EdgeCase_MaxInt() {
|
||||
IllegalArgumentException exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
Integer.MAX_VALUE
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals("Invalid status value. Status must be 0 (disabled) or 1 (enabled)", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValidateStatus_EdgeCase_MinInt() {
|
||||
IllegalArgumentException exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CreateRoleCommand.of(
|
||||
"Admin",
|
||||
"admin",
|
||||
1,
|
||||
Integer.MIN_VALUE
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals("Invalid status value. Status must be 0 (disabled) or 1 (enabled)", exception.getMessage());
|
||||
}
|
||||
}
|
||||
+246
@@ -0,0 +1,246 @@
|
||||
package cn.novalon.manage.sys.core.command;
|
||||
|
||||
import cn.novalon.manage.sys.primitive.Email;
|
||||
import cn.novalon.manage.sys.primitive.Password;
|
||||
import cn.novalon.manage.sys.primitive.Username;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CreateUserCommandTest {
|
||||
|
||||
@Test
|
||||
void testConstructor() {
|
||||
Username username = Username.of("testuser");
|
||||
Password password = Password.of("Password123!");
|
||||
Email email = Email.of("test@example.com");
|
||||
|
||||
CreateUserCommand command = new CreateUserCommand(
|
||||
username,
|
||||
password,
|
||||
email,
|
||||
"nickname",
|
||||
"1234567890",
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertEquals(username, command.username());
|
||||
assertEquals(password, command.password());
|
||||
assertEquals(email, command.email());
|
||||
assertEquals("nickname", command.nickname());
|
||||
assertEquals("1234567890", command.phone());
|
||||
assertEquals(1L, command.roleId());
|
||||
assertEquals(1, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"testuser",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
"nickname",
|
||||
"1234567890",
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertEquals("nickname", command.nickname());
|
||||
assertEquals("1234567890", command.phone());
|
||||
assertEquals(1L, command.roleId());
|
||||
assertEquals(1, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNullValues() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"testuser",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertNull(command.nickname());
|
||||
assertNull(command.phone());
|
||||
assertNull(command.roleId());
|
||||
assertNull(command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithEmptyStrings() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"testuser",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
"",
|
||||
"",
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertEquals("", command.nickname());
|
||||
assertEquals("", command.phone());
|
||||
assertNull(command.roleId());
|
||||
assertNull(command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithBoundaryValues() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"abc",
|
||||
"Abc123!@",
|
||||
"a@b.co",
|
||||
"n",
|
||||
"0",
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertEquals("n", command.nickname());
|
||||
assertEquals("0", command.phone());
|
||||
assertEquals(1L, command.roleId());
|
||||
assertEquals(1, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithZeroValues() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"testuser",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
"nickname",
|
||||
"1234567890",
|
||||
0L,
|
||||
0
|
||||
);
|
||||
|
||||
assertEquals(0L, command.roleId());
|
||||
assertEquals(0, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNegativeValues() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"testuser",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
"nickname",
|
||||
"1234567890",
|
||||
-1L,
|
||||
-1
|
||||
);
|
||||
|
||||
assertEquals(-1L, command.roleId());
|
||||
assertEquals(-1, command.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithSpecialCharacters() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"test_user",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
"nick@#$%",
|
||||
"123@#$%",
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertEquals("nick@#$%", command.nickname());
|
||||
assertEquals("123@#$%", command.phone());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithLongStrings() {
|
||||
String longNickname = "a".repeat(1000);
|
||||
String longPhone = "1".repeat(100);
|
||||
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"testuser",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
longNickname,
|
||||
longPhone,
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertEquals(longNickname, command.nickname());
|
||||
assertEquals(longPhone, command.phone());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithUnicodeCharacters() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"test_user",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
"昵称_测试",
|
||||
"1234567890",
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertEquals("昵称_测试", command.nickname());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithWhitespace() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"testuser",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
" nickname ",
|
||||
" 1234567890 ",
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertEquals(" nickname ", command.nickname());
|
||||
assertEquals(" 1234567890 ", command.phone());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNumericStrings() {
|
||||
CreateUserCommand command = CreateUserCommand.of(
|
||||
"test123",
|
||||
"Password123!",
|
||||
"test@example.com",
|
||||
"12345",
|
||||
"12345",
|
||||
1L,
|
||||
1
|
||||
);
|
||||
|
||||
assertNotNull(command.username());
|
||||
assertNotNull(command.password());
|
||||
assertNotNull(command.email());
|
||||
assertEquals("12345", command.nickname());
|
||||
assertEquals("12345", command.phone());
|
||||
}
|
||||
}
|
||||
+312
@@ -0,0 +1,312 @@
|
||||
package cn.novalon.manage.sys.core.command;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UpdateUserCommandTest {
|
||||
|
||||
@Test
|
||||
void testConstructor() {
|
||||
UpdateUserCommand command = new UpdateUserCommand(
|
||||
1L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
2L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals(1L, command.id());
|
||||
assertEquals("testuser", command.username());
|
||||
assertEquals("password123", command.password());
|
||||
assertEquals("test@example.com", command.email());
|
||||
assertEquals(2L, command.roleId());
|
||||
assertEquals(1, command.status());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithoutClearRole() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
2L,
|
||||
1
|
||||
);
|
||||
|
||||
assertEquals(1L, command.id());
|
||||
assertEquals("testuser", command.username());
|
||||
assertEquals("password123", command.password());
|
||||
assertEquals("test@example.com", command.email());
|
||||
assertEquals(2L, command.roleId());
|
||||
assertEquals(1, command.status());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithClearRoleFalse() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
2L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals(1L, command.id());
|
||||
assertEquals("testuser", command.username());
|
||||
assertEquals("password123", command.password());
|
||||
assertEquals("test@example.com", command.email());
|
||||
assertEquals(2L, command.roleId());
|
||||
assertEquals(1, command.status());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithClearRoleTrue() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
2L,
|
||||
1,
|
||||
true
|
||||
);
|
||||
|
||||
assertEquals(1L, command.id());
|
||||
assertEquals("testuser", command.username());
|
||||
assertEquals("password123", command.password());
|
||||
assertEquals("test@example.com", command.email());
|
||||
assertEquals(2L, command.roleId());
|
||||
assertEquals(1, command.status());
|
||||
assertTrue(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNullValues() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
assertNull(command.id());
|
||||
assertNull(command.username());
|
||||
assertNull(command.password());
|
||||
assertNull(command.email());
|
||||
assertNull(command.roleId());
|
||||
assertNull(command.status());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithEmptyStrings() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
assertEquals(1L, command.id());
|
||||
assertEquals("", command.username());
|
||||
assertEquals("", command.password());
|
||||
assertEquals("", command.email());
|
||||
assertNull(command.roleId());
|
||||
assertNull(command.status());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithBoundaryValues() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
Long.MAX_VALUE,
|
||||
"a",
|
||||
"1",
|
||||
"a@b.c",
|
||||
Long.MAX_VALUE,
|
||||
Integer.MAX_VALUE,
|
||||
true
|
||||
);
|
||||
|
||||
assertEquals(Long.MAX_VALUE, command.id());
|
||||
assertEquals("a", command.username());
|
||||
assertEquals("1", command.password());
|
||||
assertEquals("a@b.c", command.email());
|
||||
assertEquals(Long.MAX_VALUE, command.roleId());
|
||||
assertEquals(Integer.MAX_VALUE, command.status());
|
||||
assertTrue(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithZeroValues() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
0L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
0L,
|
||||
0,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals(0L, command.id());
|
||||
assertEquals(0L, command.roleId());
|
||||
assertEquals(0, command.status());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNegativeValues() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
-1L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
-1L,
|
||||
-1,
|
||||
true
|
||||
);
|
||||
|
||||
assertEquals(-1L, command.id());
|
||||
assertEquals(-1L, command.roleId());
|
||||
assertEquals(-1, command.status());
|
||||
assertTrue(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithSpecialCharacters() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
"user@#$%",
|
||||
"pass@#$%",
|
||||
"test@#$%.com",
|
||||
1L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals("user@#$%", command.username());
|
||||
assertEquals("pass@#$%", command.password());
|
||||
assertEquals("test@#$%.com", command.email());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithLongStrings() {
|
||||
String longUsername = "a".repeat(1000);
|
||||
String longPassword = "b".repeat(1000);
|
||||
String longEmail = "c".repeat(1000) + "@example.com";
|
||||
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
longUsername,
|
||||
longPassword,
|
||||
longEmail,
|
||||
1L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals(longUsername, command.username());
|
||||
assertEquals(longPassword, command.password());
|
||||
assertEquals(longEmail, command.email());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithUnicodeCharacters() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
"用户_测试",
|
||||
"密码_测试",
|
||||
"测试@example.com",
|
||||
1L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals("用户_测试", command.username());
|
||||
assertEquals("密码_测试", command.password());
|
||||
assertEquals("测试@example.com", command.email());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithWhitespace() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
" testuser ",
|
||||
" password123 ",
|
||||
" test@example.com ",
|
||||
1L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals(" testuser ", command.username());
|
||||
assertEquals(" password123 ", command.password());
|
||||
assertEquals(" test@example.com ", command.email());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNumericStrings() {
|
||||
UpdateUserCommand command = UpdateUserCommand.of(
|
||||
1L,
|
||||
"12345",
|
||||
"12345",
|
||||
"12345@example.com",
|
||||
1L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertEquals("12345", command.username());
|
||||
assertEquals("12345", command.password());
|
||||
assertEquals("12345@example.com", command.email());
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearRoleFlag_True() {
|
||||
UpdateUserCommand command = new UpdateUserCommand(
|
||||
1L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
2L,
|
||||
1,
|
||||
true
|
||||
);
|
||||
|
||||
assertTrue(command.clearRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearRoleFlag_False() {
|
||||
UpdateUserCommand command = new UpdateUserCommand(
|
||||
1L,
|
||||
"testuser",
|
||||
"password123",
|
||||
"test@example.com",
|
||||
2L,
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
assertFalse(command.clearRole());
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package cn.novalon.manage.sys.core.domain;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SysUserTest {
|
||||
|
||||
private SysUser user;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
user = new SysUser();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenerateId() {
|
||||
Long id = user.generateId();
|
||||
|
||||
assertNotNull(id);
|
||||
assertTrue(id > 0);
|
||||
assertEquals(id, user.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenerateId_GeneratesUniqueIds() {
|
||||
SysUser user1 = new SysUser();
|
||||
SysUser user2 = new SysUser();
|
||||
|
||||
Long id1 = user1.generateId();
|
||||
Long id2 = user2.generateId();
|
||||
|
||||
assertNotNull(id1);
|
||||
assertNotNull(id2);
|
||||
assertNotEquals(id1, id2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDelete() {
|
||||
assertNull(user.getDeletedAt());
|
||||
|
||||
user.delete();
|
||||
|
||||
assertNotNull(user.getDeletedAt());
|
||||
assertTrue(user.getDeletedAt().isBefore(LocalDateTime.now().plusSeconds(1)));
|
||||
assertTrue(user.getDeletedAt().isAfter(LocalDateTime.now().minusSeconds(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDelete_WhenAlreadyDeleted() {
|
||||
user.delete();
|
||||
LocalDateTime firstDeleteTime = user.getDeletedAt();
|
||||
|
||||
user.delete();
|
||||
LocalDateTime secondDeleteTime = user.getDeletedAt();
|
||||
|
||||
assertNotNull(firstDeleteTime);
|
||||
assertNotNull(secondDeleteTime);
|
||||
assertNotEquals(firstDeleteTime, secondDeleteTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUsername() {
|
||||
user.setUsername("testuser");
|
||||
assertEquals("testuser", user.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPassword() {
|
||||
user.setPassword("password123");
|
||||
assertEquals("password123", user.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNickname() {
|
||||
user.setNickname("测试用户");
|
||||
assertEquals("测试用户", user.getNickname());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmail() {
|
||||
user.setEmail("test@example.com");
|
||||
assertEquals("test@example.com", user.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPhone() {
|
||||
user.setPhone("13800138000");
|
||||
assertEquals("13800138000", user.getPhone());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRoleId() {
|
||||
user.setRoleId(1L);
|
||||
assertEquals(1L, user.getRoleId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStatus() {
|
||||
user.setStatus(1);
|
||||
assertEquals(1, user.getStatus());
|
||||
}
|
||||
}
|
||||
+77
-21
@@ -1,7 +1,10 @@
|
||||
package cn.novalon.manage.sys.core.service.impl;
|
||||
|
||||
import cn.novalon.manage.sys.core.domain.OperationLog;
|
||||
import cn.novalon.manage.sys.core.query.OperationLogQuery;
|
||||
import cn.novalon.manage.sys.core.repository.IOperationLogRepository;
|
||||
import cn.novalon.manage.common.dto.PageRequest;
|
||||
import cn.novalon.manage.common.dto.PageResponse;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -12,9 +15,9 @@ import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -30,7 +33,7 @@ class OperationLogServiceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
operationLogService = new OperationLogService(logRepository);
|
||||
|
||||
|
||||
testLog = new OperationLog();
|
||||
testLog.setId(1L);
|
||||
testLog.setUsername("testuser");
|
||||
@@ -45,68 +48,121 @@ class OperationLogServiceTest {
|
||||
@Test
|
||||
void testSave() {
|
||||
when(logRepository.save(any(OperationLog.class))).thenReturn(Mono.just(testLog));
|
||||
|
||||
|
||||
Mono<OperationLog> result = operationLogService.save(testLog);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(log ->
|
||||
log.getId().equals(1L) &&
|
||||
log.getUsername().equals("testuser") &&
|
||||
log.getCreatedAt() != null)
|
||||
.expectNextMatches(log -> log.getId().equals(1L) &&
|
||||
log.getUsername().equals("testuser") &&
|
||||
log.getCreatedAt() != null)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(logRepository).save(any(OperationLog.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAll() {
|
||||
when(logRepository.findAll()).thenReturn(Flux.just(testLog));
|
||||
|
||||
|
||||
Flux<OperationLog> result = operationLogService.findAll();
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(testLog)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(logRepository).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindByUsername() {
|
||||
when(logRepository.findByUsername("testuser")).thenReturn(Flux.just(testLog));
|
||||
|
||||
|
||||
Flux<OperationLog> result = operationLogService.findByUsername("testuser");
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(testLog)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(logRepository).findByUsername("testuser");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCount() {
|
||||
when(logRepository.count()).thenReturn(Mono.just(100L));
|
||||
|
||||
|
||||
Mono<Long> result = operationLogService.count();
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(100L)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(logRepository).count();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCountToday() {
|
||||
when(logRepository.countByCreatedAtAfter(any(LocalDateTime.class))).thenReturn(Mono.just(10L));
|
||||
|
||||
|
||||
Mono<Long> result = operationLogService.countToday();
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(10L)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(logRepository).countByCreatedAtAfter(any(LocalDateTime.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindById() {
|
||||
when(logRepository.findById(1L)).thenReturn(Mono.just(testLog));
|
||||
|
||||
Mono<OperationLog> result = operationLogService.findById(1L);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(testLog)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logRepository).findById(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindById_NotFound() {
|
||||
when(logRepository.findById(999L)).thenReturn(Mono.empty());
|
||||
|
||||
Mono<OperationLog> result = operationLogService.findById(999L);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logRepository).findById(999L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindByQueryWithPagination() {
|
||||
PageResponse<OperationLog> pageResponse = new PageResponse<>();
|
||||
pageResponse.setContent(Collections.singletonList(testLog));
|
||||
pageResponse.setTotalElements(1L);
|
||||
pageResponse.setTotalPages(1);
|
||||
pageResponse.setCurrentPage(0);
|
||||
pageResponse.setPageSize(10);
|
||||
|
||||
PageRequest pageRequest = new PageRequest();
|
||||
pageRequest.setPage(0);
|
||||
pageRequest.setSize(10);
|
||||
|
||||
OperationLogQuery query = new OperationLogQuery();
|
||||
|
||||
when(logRepository.findByQueryWithPagination(any(OperationLogQuery.class), any(PageRequest.class)))
|
||||
.thenReturn(Mono.just(pageResponse));
|
||||
|
||||
Mono<PageResponse<OperationLog>> result = operationLogService.findByQueryWithPagination(query, pageRequest);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(response -> response.getContent().size() == 1 &&
|
||||
response.getTotalElements() == 1L &&
|
||||
response.getTotalPages() == 1)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logRepository).findByQueryWithPagination(any(OperationLogQuery.class), any(PageRequest.class));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -11,8 +11,8 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 系统配置服务单元测试类
|
||||
|
||||
-2
@@ -14,8 +14,6 @@ import reactor.test.StepVerifier;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
-2
@@ -14,8 +14,6 @@ import reactor.test.StepVerifier;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
-2
@@ -14,10 +14,8 @@ import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
-2
@@ -14,10 +14,8 @@ import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
+98
-114
@@ -14,10 +14,8 @@ import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -33,7 +31,7 @@ class SysMenuServiceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
menuService = new SysMenuService(menuRepository);
|
||||
|
||||
|
||||
testMenu = new SysMenu();
|
||||
testMenu.setId(1L);
|
||||
testMenu.setMenuName("系统管理");
|
||||
@@ -49,64 +47,62 @@ class SysMenuServiceTest {
|
||||
@Test
|
||||
void testFindById() {
|
||||
when(menuRepository.findById(1L)).thenReturn(Mono.just(testMenu));
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.findById(1L);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(testMenu)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findById(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAll() {
|
||||
when(menuRepository.findAll()).thenReturn(Flux.just(testMenu));
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.findAll();
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(testMenu)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindByParentId() {
|
||||
when(menuRepository.findByParentId(0L)).thenReturn(Flux.just(testMenu));
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.findByParentId(0L);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(testMenu)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findByParentId(0L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateMenu() {
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(testMenu));
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.createMenu(testMenu);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getId().equals(1L) &&
|
||||
menu.getMenuName().equals("系统管理") &&
|
||||
menu.getCreatedAt() != null)
|
||||
.expectNextMatches(menu -> menu.getId().equals(1L) &&
|
||||
menu.getMenuName().equals("系统管理") &&
|
||||
menu.getCreatedAt() != null)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).save(any(SysMenu.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateMenuWithCommand() {
|
||||
CreateMenuCommand command = new CreateMenuCommand(
|
||||
0L, "用户管理", "M", 2, "user", "user:manage", 1
|
||||
);
|
||||
|
||||
0L, "用户管理", "M", 2, "user", "user:manage", 1);
|
||||
|
||||
SysMenu createdMenu = new SysMenu();
|
||||
createdMenu.setId(2L);
|
||||
createdMenu.setMenuName("用户管理");
|
||||
@@ -117,53 +113,49 @@ class SysMenuServiceTest {
|
||||
createdMenu.setComponent("user");
|
||||
createdMenu.setStatus(1);
|
||||
createdMenu.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(createdMenu));
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.createMenu(command);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getMenuName().equals("用户管理") &&
|
||||
menu.getParentId().equals(0L) &&
|
||||
menu.getCreatedAt() != null)
|
||||
.expectNextMatches(menu -> menu.getMenuName().equals("用户管理") &&
|
||||
menu.getParentId().equals(0L) &&
|
||||
menu.getCreatedAt() != null)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).save(any(SysMenu.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateMenu() {
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(testMenu));
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.updateMenu(testMenu);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getId().equals(1L) &&
|
||||
menu.getUpdatedAt() != null)
|
||||
.expectNextMatches(menu -> menu.getId().equals(1L) &&
|
||||
menu.getUpdatedAt() != null)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).save(any(SysMenu.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateMenuWithCommand() {
|
||||
UpdateMenuCommand command = new UpdateMenuCommand(
|
||||
1L, 0L, "系统管理(更新)", "M", 1, "system", "system:manage", 1
|
||||
);
|
||||
|
||||
1L, 0L, "系统管理(更新)", "M", 1, "system", "system:manage", 1);
|
||||
|
||||
when(menuRepository.findById(1L)).thenReturn(Mono.just(testMenu));
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(testMenu));
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.updateMenu(command);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getMenuName().equals("系统管理(更新)") &&
|
||||
menu.getUpdatedAt() != null)
|
||||
.expectNextMatches(menu -> menu.getMenuName().equals("系统管理(更新)") &&
|
||||
menu.getUpdatedAt() != null)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findById(1L);
|
||||
verify(menuRepository).save(any(SysMenu.class));
|
||||
}
|
||||
@@ -171,17 +163,16 @@ class SysMenuServiceTest {
|
||||
@Test
|
||||
void testUpdateMenuWithCommand_NotFound() {
|
||||
UpdateMenuCommand command = new UpdateMenuCommand(
|
||||
999L, 0L, "不存在的菜单", "M", 1, "system", "system:manage", 1
|
||||
);
|
||||
|
||||
999L, 0L, "不存在的菜单", "M", 1, "system", "system:manage", 1);
|
||||
|
||||
when(menuRepository.findById(999L)).thenReturn(Mono.empty());
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.updateMenu(command);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectErrorMatches(ex -> ex.getMessage().contains("Menu not found"))
|
||||
.verify();
|
||||
|
||||
|
||||
verify(menuRepository).findById(999L);
|
||||
}
|
||||
|
||||
@@ -212,8 +203,7 @@ class SysMenuServiceTest {
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(updatedMenu));
|
||||
|
||||
UpdateMenuCommand command = new UpdateMenuCommand(
|
||||
1L, null, null, null, null, null, null, null
|
||||
);
|
||||
1L, null, null, null, null, null, null, null);
|
||||
|
||||
StepVerifier.create(menuService.updateMenu(command))
|
||||
.expectNextMatches(menu -> menu.getUpdatedAt() != null)
|
||||
@@ -250,8 +240,7 @@ class SysMenuServiceTest {
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(updatedMenu));
|
||||
|
||||
UpdateMenuCommand command = new UpdateMenuCommand(
|
||||
1L, 2L, "系统管理(更新)", "C", 2, "system_updated", "system:manage_updated", 0
|
||||
);
|
||||
1L, 2L, "系统管理(更新)", "C", 2, "system_updated", "system:manage_updated", 0);
|
||||
|
||||
StepVerifier.create(menuService.updateMenu(command))
|
||||
.expectNextMatches(menu -> menu.getUpdatedAt() != null)
|
||||
@@ -264,12 +253,12 @@ class SysMenuServiceTest {
|
||||
@Test
|
||||
void testDeleteMenu() {
|
||||
when(menuRepository.deleteById(1L)).thenReturn(Mono.empty());
|
||||
|
||||
|
||||
Mono<Void> result = menuService.deleteMenu(1L);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).deleteById(1L);
|
||||
}
|
||||
|
||||
@@ -279,60 +268,59 @@ class SysMenuServiceTest {
|
||||
parentMenu.setId(1L);
|
||||
parentMenu.setMenuName("系统管理");
|
||||
parentMenu.setParentId(0L);
|
||||
|
||||
|
||||
SysMenu childMenu = new SysMenu();
|
||||
childMenu.setId(2L);
|
||||
childMenu.setMenuName("用户管理");
|
||||
childMenu.setParentId(1L);
|
||||
|
||||
|
||||
when(menuRepository.findAll()).thenReturn(Flux.just(parentMenu, childMenu));
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.buildMenuTree(menuService.findAll());
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getId().equals(1L) &&
|
||||
menu.getChildren() != null &&
|
||||
menu.getChildren().size() == 1)
|
||||
.expectNextMatches(menu -> menu.getId().equals(1L) &&
|
||||
menu.getChildren() != null &&
|
||||
menu.getChildren().size() == 1)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindById_WhenMenuNotFound() {
|
||||
when(menuRepository.findById(999L)).thenReturn(Mono.empty());
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.findById(999L);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextCount(0)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findById(999L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAll_WhenNoMenusExist() {
|
||||
when(menuRepository.findAll()).thenReturn(Flux.empty());
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.findAll();
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextCount(0)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindByParentId_WhenNoChildrenExist() {
|
||||
when(menuRepository.findByParentId(999L)).thenReturn(Flux.empty());
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.findByParentId(999L);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextCount(0)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findByParentId(999L);
|
||||
}
|
||||
|
||||
@@ -359,24 +347,22 @@ class SysMenuServiceTest {
|
||||
savedMenu.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(savedMenu));
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.createMenu(newMenu);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getStatus().equals(1) &&
|
||||
menu.getCreatedAt() != null)
|
||||
.expectNextMatches(menu -> menu.getStatus().equals(1) &&
|
||||
menu.getCreatedAt() != null)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).save(any(SysMenu.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateMenuWithCommand_WithDefaultStatus() {
|
||||
CreateMenuCommand command = new CreateMenuCommand(
|
||||
0L, "日志管理", "M", 3, "log", "log:manage", null
|
||||
);
|
||||
|
||||
0L, "日志管理", "M", 3, "log", "log:manage", null);
|
||||
|
||||
SysMenu createdMenu = new SysMenu();
|
||||
createdMenu.setId(3L);
|
||||
createdMenu.setMenuName("日志管理");
|
||||
@@ -387,31 +373,30 @@ class SysMenuServiceTest {
|
||||
createdMenu.setComponent("log");
|
||||
createdMenu.setStatus(1);
|
||||
createdMenu.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
|
||||
when(menuRepository.save(any(SysMenu.class))).thenReturn(Mono.just(createdMenu));
|
||||
|
||||
|
||||
Mono<SysMenu> result = menuService.createMenu(command);
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getMenuName().equals("日志管理") &&
|
||||
menu.getStatus().equals(1) &&
|
||||
menu.getCreatedAt() != null)
|
||||
.expectNextMatches(menu -> menu.getMenuName().equals("日志管理") &&
|
||||
menu.getStatus().equals(1) &&
|
||||
menu.getCreatedAt() != null)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).save(any(SysMenu.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildMenuTree_WithEmptyTree() {
|
||||
when(menuRepository.findAll()).thenReturn(Flux.empty());
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.buildMenuTree(menuService.findAll());
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextCount(0)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findAll();
|
||||
}
|
||||
|
||||
@@ -421,30 +406,29 @@ class SysMenuServiceTest {
|
||||
rootMenu.setId(1L);
|
||||
rootMenu.setMenuName("系统管理");
|
||||
rootMenu.setParentId(0L);
|
||||
|
||||
|
||||
SysMenu level1Menu = new SysMenu();
|
||||
level1Menu.setId(2L);
|
||||
level1Menu.setMenuName("用户管理");
|
||||
level1Menu.setParentId(1L);
|
||||
|
||||
|
||||
SysMenu level2Menu = new SysMenu();
|
||||
level2Menu.setId(3L);
|
||||
level2Menu.setMenuName("用户列表");
|
||||
level2Menu.setParentId(2L);
|
||||
|
||||
|
||||
when(menuRepository.findAll()).thenReturn(Flux.just(rootMenu, level1Menu, level2Menu));
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.buildMenuTree(menuService.findAll());
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextMatches(menu ->
|
||||
menu.getId().equals(1L) &&
|
||||
menu.getChildren() != null &&
|
||||
menu.getChildren().size() == 1 &&
|
||||
menu.getChildren().get(0).getChildren() != null &&
|
||||
menu.getChildren().get(0).getChildren().size() == 1)
|
||||
.expectNextMatches(menu -> menu.getId().equals(1L) &&
|
||||
menu.getChildren() != null &&
|
||||
menu.getChildren().size() == 1 &&
|
||||
menu.getChildren().get(0).getChildren() != null &&
|
||||
menu.getChildren().get(0).getChildren().size() == 1)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findAll();
|
||||
}
|
||||
|
||||
@@ -454,30 +438,30 @@ class SysMenuServiceTest {
|
||||
root1.setId(1L);
|
||||
root1.setMenuName("系统管理");
|
||||
root1.setParentId(0L);
|
||||
|
||||
|
||||
SysMenu root2 = new SysMenu();
|
||||
root2.setId(2L);
|
||||
root2.setMenuName("监控管理");
|
||||
root2.setParentId(0L);
|
||||
|
||||
|
||||
SysMenu child1 = new SysMenu();
|
||||
child1.setId(3L);
|
||||
child1.setMenuName("用户管理");
|
||||
child1.setParentId(1L);
|
||||
|
||||
|
||||
SysMenu child2 = new SysMenu();
|
||||
child2.setId(4L);
|
||||
child2.setMenuName("性能监控");
|
||||
child2.setParentId(2L);
|
||||
|
||||
|
||||
when(menuRepository.findAll()).thenReturn(Flux.just(root1, root2, child1, child2));
|
||||
|
||||
|
||||
Flux<SysMenu> result = menuService.buildMenuTree(menuService.findAll());
|
||||
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNextCount(2)
|
||||
.verifyComplete();
|
||||
|
||||
|
||||
verify(menuRepository).findAll();
|
||||
}
|
||||
}
|
||||
-1
@@ -12,7 +12,6 @@ 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.data.relational.core.query.Query;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
+529
-446
File diff suppressed because it is too large
Load Diff
+184
@@ -0,0 +1,184 @@
|
||||
package cn.novalon.manage.sys.dto.response;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AuthResponseTest {
|
||||
|
||||
@Test
|
||||
void testConstructorWithParameters() {
|
||||
AuthResponse response = new AuthResponse("test-token", 1L, "testuser");
|
||||
|
||||
assertEquals("test-token", response.getToken());
|
||||
assertEquals(1L, response.getUserId());
|
||||
assertEquals("testuser", response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDefaultConstructor() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
assertNull(response.getToken());
|
||||
assertNull(response.getUserId());
|
||||
assertNull(response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setToken("new-token");
|
||||
response.setUserId(2L);
|
||||
response.setUsername("newuser");
|
||||
|
||||
assertEquals("new-token", response.getToken());
|
||||
assertEquals(2L, response.getUserId());
|
||||
assertEquals("newuser", response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithNullValues() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setToken(null);
|
||||
response.setUserId(null);
|
||||
response.setUsername(null);
|
||||
|
||||
assertNull(response.getToken());
|
||||
assertNull(response.getUserId());
|
||||
assertNull(response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithEmptyStrings() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setToken("");
|
||||
response.setUsername("");
|
||||
|
||||
assertEquals("", response.getToken());
|
||||
assertEquals("", response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConstructorWithNullValues() {
|
||||
AuthResponse response = new AuthResponse(null, null, null);
|
||||
|
||||
assertNull(response.getToken());
|
||||
assertNull(response.getUserId());
|
||||
assertNull(response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConstructorWithEmptyStrings() {
|
||||
AuthResponse response = new AuthResponse("", 1L, "");
|
||||
|
||||
assertEquals("", response.getToken());
|
||||
assertEquals(1L, response.getUserId());
|
||||
assertEquals("", response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithBoundaryValues() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setUserId(Long.MAX_VALUE);
|
||||
response.setUserId(Long.MIN_VALUE);
|
||||
response.setUserId(0L);
|
||||
|
||||
assertEquals(0L, response.getUserId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithNegativeValues() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setUserId(-1L);
|
||||
|
||||
assertEquals(-1L, response.getUserId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithSpecialCharacters() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
String specialToken = "token@#$%^&*()";
|
||||
String specialUsername = "user@#$%^&*()";
|
||||
|
||||
response.setToken(specialToken);
|
||||
response.setUsername(specialUsername);
|
||||
|
||||
assertEquals(specialToken, response.getToken());
|
||||
assertEquals(specialUsername, response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithLongStrings() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
String longToken = "a".repeat(1000);
|
||||
String longUsername = "b".repeat(500);
|
||||
|
||||
response.setToken(longToken);
|
||||
response.setUsername(longUsername);
|
||||
|
||||
assertEquals(longToken, response.getToken());
|
||||
assertEquals(longUsername, response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithUnicodeCharacters() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
String unicodeToken = "token_测试_🔑";
|
||||
String unicodeUsername = "user_测试_👤";
|
||||
|
||||
response.setToken(unicodeToken);
|
||||
response.setUsername(unicodeUsername);
|
||||
|
||||
assertEquals(unicodeToken, response.getToken());
|
||||
assertEquals(unicodeUsername, response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithWhitespace() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setToken(" token ");
|
||||
response.setUsername(" user ");
|
||||
|
||||
assertEquals(" token ", response.getToken());
|
||||
assertEquals(" user ", response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultipleSetOperations() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setToken("token1");
|
||||
response.setToken("token2");
|
||||
|
||||
assertEquals("token2", response.getToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConstructorWithZeroUserId() {
|
||||
AuthResponse response = new AuthResponse("token", 0L, "user");
|
||||
|
||||
assertEquals("token", response.getToken());
|
||||
assertEquals(0L, response.getUserId());
|
||||
assertEquals("user", response.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithNumericStrings() {
|
||||
AuthResponse response = new AuthResponse();
|
||||
|
||||
response.setToken("12345");
|
||||
response.setUsername("67890");
|
||||
|
||||
assertEquals("12345", response.getToken());
|
||||
assertEquals("67890", response.getUsername());
|
||||
}
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
package cn.novalon.manage.sys.dto.response;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class FilePreviewResponseTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
response.setFileName("test.pdf");
|
||||
response.setFileType("application/pdf");
|
||||
response.setFileSize(1024L);
|
||||
response.setPreviewType("image");
|
||||
response.setPreviewData("base64data");
|
||||
|
||||
assertEquals("test.pdf", response.getFileName());
|
||||
assertEquals("application/pdf", response.getFileType());
|
||||
assertEquals(1024L, response.getFileSize());
|
||||
assertEquals("image", response.getPreviewType());
|
||||
assertEquals("base64data", response.getPreviewData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithNullValues() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
response.setFileName(null);
|
||||
response.setFileType(null);
|
||||
response.setFileSize(null);
|
||||
response.setPreviewType(null);
|
||||
response.setPreviewData(null);
|
||||
|
||||
assertNull(response.getFileName());
|
||||
assertNull(response.getFileType());
|
||||
assertNull(response.getFileSize());
|
||||
assertNull(response.getPreviewType());
|
||||
assertNull(response.getPreviewData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithEmptyStrings() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
response.setFileName("");
|
||||
response.setFileType("");
|
||||
response.setPreviewType("");
|
||||
response.setPreviewData("");
|
||||
|
||||
assertEquals("", response.getFileName());
|
||||
assertEquals("", response.getFileType());
|
||||
assertEquals("", response.getPreviewType());
|
||||
assertEquals("", response.getPreviewData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithBoundaryValues() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
response.setFileSize(Long.MAX_VALUE);
|
||||
response.setFileSize(Long.MIN_VALUE);
|
||||
response.setFileSize(0L);
|
||||
|
||||
assertEquals(0L, response.getFileSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithSpecialCharacters() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
String specialFileName = "文件名@#$%^&*().pdf";
|
||||
String specialFileType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
||||
|
||||
response.setFileName(specialFileName);
|
||||
response.setFileType(specialFileType);
|
||||
|
||||
assertEquals(specialFileName, response.getFileName());
|
||||
assertEquals(specialFileType, response.getFileType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithLongStrings() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
String longFileName = "a".repeat(1000) + ".pdf";
|
||||
String longPreviewData = "x".repeat(10000);
|
||||
|
||||
response.setFileName(longFileName);
|
||||
response.setPreviewData(longPreviewData);
|
||||
|
||||
assertEquals(longFileName, response.getFileName());
|
||||
assertEquals(longPreviewData, response.getPreviewData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithUnicodeCharacters() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
String unicodeFileName = "文件名_测试_📄.pdf";
|
||||
String unicodePreviewData = "数据_测试_🔍";
|
||||
|
||||
response.setFileName(unicodeFileName);
|
||||
response.setPreviewData(unicodePreviewData);
|
||||
|
||||
assertEquals(unicodeFileName, response.getFileName());
|
||||
assertEquals(unicodePreviewData, response.getPreviewData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithWhitespace() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
response.setFileName(" test.pdf ");
|
||||
response.setFileType(" application/pdf ");
|
||||
response.setPreviewType(" image ");
|
||||
|
||||
assertEquals(" test.pdf ", response.getFileName());
|
||||
assertEquals(" application/pdf ", response.getFileType());
|
||||
assertEquals(" image ", response.getPreviewType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultipleSetOperations() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
response.setFileName("file1.pdf");
|
||||
response.setFileName("file2.pdf");
|
||||
|
||||
assertEquals("file2.pdf", response.getFileName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithNumericStrings() {
|
||||
FilePreviewResponse response = new FilePreviewResponse();
|
||||
|
||||
response.setFileName("12345.pdf");
|
||||
response.setFileType("12345");
|
||||
|
||||
assertEquals("12345.pdf", response.getFileName());
|
||||
assertEquals("12345", response.getFileType());
|
||||
}
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package cn.novalon.manage.sys.dto.response;
|
||||
|
||||
import cn.novalon.manage.sys.core.domain.SysUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UserResponseTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
UserResponse response = new UserResponse();
|
||||
|
||||
response.setId(1L);
|
||||
response.setUsername("testuser");
|
||||
response.setEmail("test@example.com");
|
||||
response.setRoleId(2L);
|
||||
response.setStatus(1);
|
||||
response.setCreatedAt(LocalDateTime.now());
|
||||
response.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
assertEquals(1L, response.getId());
|
||||
assertEquals("testuser", response.getUsername());
|
||||
assertEquals("test@example.com", response.getEmail());
|
||||
assertEquals(2L, response.getRoleId());
|
||||
assertEquals(1, response.getStatus());
|
||||
assertNotNull(response.getCreatedAt());
|
||||
assertNotNull(response.getUpdatedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFromDomain() {
|
||||
SysUser user = new SysUser();
|
||||
user.setId(1L);
|
||||
user.setUsername("testuser");
|
||||
user.setEmail("test@example.com");
|
||||
user.setRoleId(2L);
|
||||
user.setStatus(1);
|
||||
user.setCreatedAt(LocalDateTime.now());
|
||||
user.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
UserResponse response = UserResponse.fromDomain(user);
|
||||
|
||||
assertEquals(user.getId(), response.getId());
|
||||
assertEquals(user.getUsername(), response.getUsername());
|
||||
assertEquals(user.getEmail(), response.getEmail());
|
||||
assertEquals(user.getRoleId(), response.getRoleId());
|
||||
assertEquals(user.getStatus(), response.getStatus());
|
||||
assertEquals(user.getCreatedAt(), response.getCreatedAt());
|
||||
assertEquals(user.getUpdatedAt(), response.getUpdatedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFromDomain_WithNullUser() {
|
||||
assertThrows(NullPointerException.class, () -> UserResponse.fromDomain(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFromDomain_WithNullFields() {
|
||||
SysUser user = new SysUser();
|
||||
|
||||
UserResponse response = UserResponse.fromDomain(user);
|
||||
|
||||
assertNull(response.getId());
|
||||
assertNull(response.getUsername());
|
||||
assertNull(response.getEmail());
|
||||
assertNull(response.getRoleId());
|
||||
assertNull(response.getStatus());
|
||||
assertNull(response.getCreatedAt());
|
||||
assertNull(response.getUpdatedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFromDomain_WithEmptyStrings() {
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername("");
|
||||
user.setEmail("");
|
||||
|
||||
UserResponse response = UserResponse.fromDomain(user);
|
||||
|
||||
assertEquals("", response.getUsername());
|
||||
assertEquals("", response.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithNullValues() {
|
||||
UserResponse response = new UserResponse();
|
||||
|
||||
response.setId(null);
|
||||
response.setUsername(null);
|
||||
response.setEmail(null);
|
||||
response.setRoleId(null);
|
||||
response.setStatus(null);
|
||||
response.setCreatedAt(null);
|
||||
response.setUpdatedAt(null);
|
||||
|
||||
assertNull(response.getId());
|
||||
assertNull(response.getUsername());
|
||||
assertNull(response.getEmail());
|
||||
assertNull(response.getRoleId());
|
||||
assertNull(response.getStatus());
|
||||
assertNull(response.getCreatedAt());
|
||||
assertNull(response.getUpdatedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithBoundaryValues() {
|
||||
UserResponse response = new UserResponse();
|
||||
|
||||
response.setId(Long.MAX_VALUE);
|
||||
response.setRoleId(Long.MIN_VALUE);
|
||||
response.setStatus(Integer.MAX_VALUE);
|
||||
|
||||
assertEquals(Long.MAX_VALUE, response.getId());
|
||||
assertEquals(Long.MIN_VALUE, response.getRoleId());
|
||||
assertEquals(Integer.MAX_VALUE, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithZeroValues() {
|
||||
UserResponse response = new UserResponse();
|
||||
|
||||
response.setId(0L);
|
||||
response.setRoleId(0L);
|
||||
response.setStatus(0);
|
||||
|
||||
assertEquals(0L, response.getId());
|
||||
assertEquals(0L, response.getRoleId());
|
||||
assertEquals(0, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettersWithNegativeValues() {
|
||||
UserResponse response = new UserResponse();
|
||||
|
||||
response.setId(-1L);
|
||||
response.setRoleId(-1L);
|
||||
response.setStatus(-1);
|
||||
|
||||
assertEquals(-1L, response.getId());
|
||||
assertEquals(-1L, response.getRoleId());
|
||||
assertEquals(-1, response.getStatus());
|
||||
}
|
||||
}
|
||||
-5
@@ -2,7 +2,6 @@ package cn.novalon.manage.sys.handler.auth;
|
||||
|
||||
import cn.novalon.manage.sys.dto.request.LoginRequest;
|
||||
import cn.novalon.manage.sys.dto.request.UserRegisterRequest;
|
||||
import cn.novalon.manage.sys.dto.response.AuthResponse;
|
||||
import cn.novalon.manage.sys.security.JwtTokenProvider;
|
||||
import cn.novalon.manage.sys.core.domain.SysUser;
|
||||
import cn.novalon.manage.sys.core.service.ISysUserService;
|
||||
@@ -14,16 +13,12 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.reactive.function.server.MockServerRequest;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.web.bind.support.WebExchangeBindException;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
-1
@@ -18,7 +18,6 @@ import reactor.test.StepVerifier;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
-2
@@ -20,8 +20,6 @@ import reactor.test.StepVerifier;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
package cn.novalon.manage.sys.handler.log;
|
||||
|
||||
import cn.novalon.manage.sys.core.domain.OperationLog;
|
||||
import cn.novalon.manage.sys.core.query.OperationLogQuery;
|
||||
import cn.novalon.manage.sys.core.service.IOperationLogService;
|
||||
import cn.novalon.manage.common.dto.PageRequest;
|
||||
import cn.novalon.manage.common.dto.PageResponse;
|
||||
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 java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class OperationLogHandlerTest {
|
||||
|
||||
@Mock
|
||||
private IOperationLogService logService;
|
||||
|
||||
private OperationLogHandler logHandler;
|
||||
private OperationLog testOperationLog;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
logHandler = new OperationLogHandler(logService);
|
||||
|
||||
testOperationLog = new OperationLog();
|
||||
testOperationLog.setId(1L);
|
||||
testOperationLog.setUsername("testuser");
|
||||
testOperationLog.setOperation("测试操作");
|
||||
testOperationLog.setMethod("testMethod");
|
||||
testOperationLog.setParams("test params");
|
||||
testOperationLog.setDuration(100L);
|
||||
testOperationLog.setIp("192.168.1.1");
|
||||
testOperationLog.setCreatedAt(LocalDateTime.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllOperationLogs() {
|
||||
when(logService.findAll()).thenReturn(Flux.just(testOperationLog));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder().build();
|
||||
Mono<ServerResponse> response = logHandler.getAllOperationLogs(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse -> serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetOperationLogById() {
|
||||
when(logService.findById(1L)).thenReturn(Mono.just(testOperationLog));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.pathVariable("id", "1")
|
||||
.build();
|
||||
Mono<ServerResponse> response = logHandler.getOperationLogById(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse -> serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).findById(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetOperationLogById_NotFound() {
|
||||
when(logService.findById(999L)).thenReturn(Mono.empty());
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.pathVariable("id", "999")
|
||||
.build();
|
||||
Mono<ServerResponse> response = logHandler.getOperationLogById(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse -> serverResponse.statusCode() == HttpStatus.NOT_FOUND)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).findById(999L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetOperationLogsByPage() {
|
||||
PageResponse<OperationLog> pageResponse = new PageResponse<>();
|
||||
pageResponse.setContent(java.util.Collections.singletonList(testOperationLog));
|
||||
pageResponse.setTotalElements(1L);
|
||||
pageResponse.setTotalPages(1);
|
||||
pageResponse.setPageSize(10);
|
||||
pageResponse.setCurrentPage(0);
|
||||
|
||||
when(logService.findByQueryWithPagination(any(OperationLogQuery.class), any(PageRequest.class)))
|
||||
.thenReturn(Mono.just(pageResponse));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("page", "0")
|
||||
.queryParam("size", "10")
|
||||
.queryParam("sort", "createdAt")
|
||||
.queryParam("order", "desc")
|
||||
.build();
|
||||
Mono<ServerResponse> response = logHandler.getOperationLogsByPage(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse -> serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).findByQueryWithPagination(any(OperationLogQuery.class), any(PageRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetOperationLogsByPageWithKeyword() {
|
||||
PageResponse<OperationLog> pageResponse = new PageResponse<>();
|
||||
pageResponse.setContent(java.util.Collections.singletonList(testOperationLog));
|
||||
pageResponse.setTotalElements(1L);
|
||||
pageResponse.setTotalPages(1);
|
||||
pageResponse.setPageSize(10);
|
||||
pageResponse.setCurrentPage(0);
|
||||
|
||||
when(logService.findByQueryWithPagination(any(OperationLogQuery.class), any(PageRequest.class)))
|
||||
.thenReturn(Mono.just(pageResponse));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("page", "0")
|
||||
.queryParam("size", "10")
|
||||
.queryParam("sort", "createdAt")
|
||||
.queryParam("order", "desc")
|
||||
.queryParam("keyword", "test")
|
||||
.build();
|
||||
Mono<ServerResponse> response = logHandler.getOperationLogsByPage(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse -> serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).findByQueryWithPagination(any(OperationLogQuery.class), any(PageRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetOperationLogCount() {
|
||||
when(logService.count()).thenReturn(Mono.just(100L));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder().build();
|
||||
Mono<ServerResponse> response = logHandler.getOperationLogCount(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse -> serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).count();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateOperationLog() {
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(testOperationLog));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(testOperationLog));
|
||||
Mono<ServerResponse> response = logHandler.createOperationLog(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse -> serverResponse.statusCode() == HttpStatus.CREATED)
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).save(any(OperationLog.class));
|
||||
}
|
||||
}
|
||||
-2
@@ -4,7 +4,6 @@ import cn.novalon.manage.sys.core.domain.SysLoginLog;
|
||||
import cn.novalon.manage.sys.core.domain.SysExceptionLog;
|
||||
import cn.novalon.manage.sys.core.service.ISysLoginLogService;
|
||||
import cn.novalon.manage.sys.core.service.ISysExceptionLogService;
|
||||
import cn.novalon.manage.common.dto.PageRequest;
|
||||
import cn.novalon.manage.common.dto.PageResponse;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -22,7 +21,6 @@ import reactor.test.StepVerifier;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
+49
-3
@@ -22,7 +22,6 @@ import reactor.test.StepVerifier;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -153,7 +152,17 @@ class MenuHandlerTest {
|
||||
|
||||
@Test
|
||||
void testGetMenusByType() {
|
||||
when(menuService.findAll()).thenReturn(Flux.just(testMenu));
|
||||
SysMenu menu1 = new SysMenu();
|
||||
menu1.setId(1L);
|
||||
menu1.setMenuName("系统管理");
|
||||
menu1.setMenuType("M");
|
||||
|
||||
SysMenu menu2 = new SysMenu();
|
||||
menu2.setId(2L);
|
||||
menu2.setMenuName("用户管理");
|
||||
menu2.setMenuType("C");
|
||||
|
||||
when(menuService.findAll()).thenReturn(Flux.just(menu1, menu2));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("menuType", "M")
|
||||
@@ -170,7 +179,17 @@ class MenuHandlerTest {
|
||||
|
||||
@Test
|
||||
void testGetMenusByType_Null() {
|
||||
when(menuService.findAll()).thenReturn(Flux.just(testMenu));
|
||||
SysMenu menu1 = new SysMenu();
|
||||
menu1.setId(1L);
|
||||
menu1.setMenuName("系统管理");
|
||||
menu1.setMenuType("M");
|
||||
|
||||
SysMenu menu2 = new SysMenu();
|
||||
menu2.setId(2L);
|
||||
menu2.setMenuName("用户管理");
|
||||
menu2.setMenuType("C");
|
||||
|
||||
when(menuService.findAll()).thenReturn(Flux.just(menu1, menu2));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.build();
|
||||
@@ -184,6 +203,33 @@ class MenuHandlerTest {
|
||||
verify(menuService).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMenusByType_NoMatch() {
|
||||
SysMenu menu1 = new SysMenu();
|
||||
menu1.setId(1L);
|
||||
menu1.setMenuName("系统管理");
|
||||
menu1.setMenuType("M");
|
||||
|
||||
SysMenu menu2 = new SysMenu();
|
||||
menu2.setId(2L);
|
||||
menu2.setMenuName("用户管理");
|
||||
menu2.setMenuType("C");
|
||||
|
||||
when(menuService.findAll()).thenReturn(Flux.just(menu1, menu2));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("menuType", "F")
|
||||
.build();
|
||||
Mono<ServerResponse> response = menuHandler.getMenusByType(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(menuService).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateMenu() {
|
||||
MenuCreateRequest createRequest = new MenuCreateRequest();
|
||||
|
||||
-1
@@ -22,7 +22,6 @@ import reactor.test.StepVerifier;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
package cn.novalon.manage.sys.interceptor;
|
||||
|
||||
import cn.novalon.manage.sys.core.domain.OperationLog;
|
||||
import cn.novalon.manage.sys.core.service.IOperationLogService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.HttpMethod;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class OperationLogFilterTest {
|
||||
|
||||
@Mock
|
||||
private IOperationLogService logService;
|
||||
|
||||
@Mock
|
||||
private WebFilterChain chain;
|
||||
|
||||
@Mock
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private OperationLogFilter filter;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
filter = new OperationLogFilter(logService, objectMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_SkipAuthEndpoints() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/auth/login").build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(chain).filter(exchange);
|
||||
verify(logService, never()).save(any(OperationLog.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_RecordSuccessLog() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users")
|
||||
.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(chain).filter(exchange);
|
||||
verify(logService).save(any(OperationLog.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_RecordErrorLog() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users")
|
||||
.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
RuntimeException error = new RuntimeException("Test error");
|
||||
when(chain.filter(exchange)).thenReturn(Mono.error(error));
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.expectError(RuntimeException.class)
|
||||
.verify();
|
||||
|
||||
verify(chain).filter(exchange);
|
||||
verify(logService).save(any(OperationLog.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_WithXForwardedForHeader() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users")
|
||||
.header("X-Forwarded-For", "192.168.1.1")
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).save(argThat(log -> "192.168.1.1".equals(log.getIp())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_WithXRealIPHeader() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users")
|
||||
.header("X-Real-IP", "10.0.0.1")
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).save(argThat(log -> "10.0.0.1".equals(log.getIp())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_WithMultipleIPsInXForwardedFor() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users")
|
||||
.header("X-Forwarded-For", "192.168.1.1, 10.0.0.1")
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).save(argThat(log -> "192.168.1.1".equals(log.getIp())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_WithUnknownHeader() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users")
|
||||
.header("X-Forwarded-For", "unknown")
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).save(any(OperationLog.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_DifferentHttpMethods() {
|
||||
HttpMethod[] methods = {HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.PATCH};
|
||||
|
||||
for (HttpMethod method : methods) {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.method(method, "/api/users")
|
||||
.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).save(argThat(log -> method.name().equals(log.getMethod())));
|
||||
reset(logService, chain);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_WithQueryParams() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users?page=1&size=10")
|
||||
.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.just(new OperationLog()));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(logService).save(argThat(log -> log.getParams() != null && !log.getParams().isEmpty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilter_LogSaveError() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/api/users")
|
||||
.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
when(logService.save(any(OperationLog.class))).thenReturn(Mono.error(new RuntimeException("Save failed")));
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, chain))
|
||||
.verifyComplete();
|
||||
|
||||
verify(chain).filter(exchange);
|
||||
verify(logService).save(any(OperationLog.class));
|
||||
}
|
||||
}
|
||||
-1
@@ -6,7 +6,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
Reference in New Issue
Block a user