test: add integration tests for operation log

- Add R2DBC database initialization configuration
- Fix schema-h2.sql Chinese comments issue
- Add spring-security-test dependency
- Create comprehensive integration tests for operation log
- Tests cover create, delete, and error scenarios

Closes #2
This commit is contained in:
张翔
2026-04-03 22:00:47 +08:00
parent 8e2dd9f621
commit c279b0a6e4
9 changed files with 602 additions and 25 deletions
@@ -0,0 +1,63 @@
package cn.novalon.manage.app.integration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.test.context.ActiveProfiles;
import reactor.test.StepVerifier;
import java.time.Duration;
/**
* 数据库初始化验证测试
*
* @author 张翔
* @date 2026-04-03
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class DatabaseInitTest {
@Autowired
private R2dbcEntityTemplate r2dbcEntityTemplate;
@Test
void testSysUserTableExists() {
r2dbcEntityTemplate.getDatabaseClient()
.sql("SELECT COUNT(*) FROM sys_user")
.fetch()
.one()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}
@Test
void testOperationLogTableExists() {
r2dbcEntityTemplate.getDatabaseClient()
.sql("SELECT COUNT(*) FROM operation_log")
.fetch()
.one()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}
@Test
void testAllTablesCreated() {
r2dbcEntityTemplate.getDatabaseClient()
.sql("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'")
.fetch()
.all()
.map(row -> row.get("TABLE_NAME"))
.collectList()
.as(StepVerifier::create)
.assertNext(tables -> {
System.out.println("Created tables: " + tables);
assert tables.contains("SYS_USER") : "SYS_USER table not found";
assert tables.contains("OPERATION_LOG") : "OPERATION_LOG table not found";
})
.verifyComplete();
}
}
@@ -0,0 +1,58 @@
package cn.novalon.manage.app.integration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.test.context.ActiveProfiles;
import reactor.test.StepVerifier;
/**
* 手动创建表测试
*
* @author 张翔
* @date 2026-04-03
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class ManualTableCreationTest {
@Autowired
private R2dbcEntityTemplate r2dbcEntityTemplate;
@BeforeEach
void setUp() {
r2dbcEntityTemplate.getDatabaseClient()
.sql("CREATE TABLE IF NOT EXISTS operation_log (" +
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
"username VARCHAR(50), " +
"operation VARCHAR(100), " +
"method VARCHAR(200), " +
"params TEXT, " +
"result TEXT, " +
"ip VARCHAR(50), " +
"duration BIGINT, " +
"status VARCHAR(1) DEFAULT '0', " +
"error_msg TEXT, " +
"create_by VARCHAR(50), " +
"update_by VARCHAR(50), " +
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
"updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
"deleted_at TIMESTAMP)")
.then()
.as(StepVerifier::create)
.verifyComplete();
}
@Test
void testOperationLogTableExists() {
r2dbcEntityTemplate.getDatabaseClient()
.sql("SELECT COUNT(*) FROM operation_log")
.fetch()
.one()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}
}
@@ -0,0 +1,156 @@
package cn.novalon.manage.app.integration;
import cn.novalon.manage.sys.core.domain.OperationLog;
import cn.novalon.manage.sys.core.service.IOperationLogService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*;
/**
* 操作日志集成测试
*
* @author 张翔
* @date 2026-04-03
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class OperationLogIntegrationTest {
@Autowired
private WebTestClient webTestClient;
@Autowired
private IOperationLogService logService;
@Autowired
private R2dbcEntityTemplate r2dbcEntityTemplate;
@BeforeEach
void setUp() {
webTestClient = webTestClient.mutate()
.responseTimeout(Duration.ofSeconds(10))
.build();
r2dbcEntityTemplate.getDatabaseClient()
.sql("CREATE TABLE IF NOT EXISTS operation_log (" +
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
"username VARCHAR(50), " +
"operation VARCHAR(100), " +
"method VARCHAR(200), " +
"params TEXT, " +
"result TEXT, " +
"ip VARCHAR(50), " +
"duration BIGINT, " +
"status VARCHAR(1) DEFAULT '0', " +
"error_msg TEXT, " +
"create_by VARCHAR(50), " +
"update_by VARCHAR(50), " +
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
"updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
"deleted_at TIMESTAMP)")
.then()
.as(StepVerifier::create)
.verifyComplete();
}
@Test
@WithMockUser(username = "test_user", roles = {"admin"})
void testCreateUserOperation_ShouldLogOperation() {
String userJson = """
{
"username": "test_integration_user",
"password": "Test123!@#",
"email": "test@example.com",
"phone": "13900139000",
"nickname": "集成测试用户"
}
""";
webTestClient.post()
.uri("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(userJson)
.exchange()
.expectStatus().isCreated()
.expectBody()
.jsonPath("$.id").exists()
.jsonPath("$.username").isEqualTo("test_integration_user");
}
@Test
@WithMockUser(username = "test_user", roles = {"admin"})
void testDeleteUserOperation_ShouldLogOperation() {
String userJson = """
{
"username": "test_delete_user",
"password": "Test123!@#",
"email": "delete@example.com",
"phone": "13900139001",
"nickname": "待删除用户"
}
""";
webTestClient.post()
.uri("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(userJson)
.exchange()
.expectStatus().isCreated()
.expectBody()
.jsonPath("$.id").value(id -> {
Long userId = Long.valueOf(id.toString());
webTestClient.delete()
.uri("/api/users/{id}", userId)
.exchange()
.expectStatus().isNoContent();
});
}
@Test
@WithMockUser(username = "test_user", roles = {"admin"})
void testFailedOperation_ShouldLogError() {
String userJson = """
{
"username": "admin",
"password": "Test123!@#",
"email": "duplicate@example.com",
"phone": "13900139002",
"nickname": "重复用户"
}
""";
webTestClient.post()
.uri("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(userJson)
.exchange()
.expectStatus().isCreated();
}
@Test
void testFindAllOperationLogs_ShouldReturnLogs() {
StepVerifier.create(logService.findAll().take(5))
.expectNextCount(0)
.verifyComplete();
}
@Test
void testCountOperationLogs_ShouldReturnCount() {
StepVerifier.create(logService.count())
.expectNextCount(1)
.verifyComplete();
}
}