diff --git a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/DatabaseInitTest.java b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/DatabaseInitTest.java index 2430634..7685fc0 100644 --- a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/DatabaseInitTest.java +++ b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/DatabaseInitTest.java @@ -1,6 +1,5 @@ package cn.novalon.manage.app.integration; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -19,8 +18,10 @@ import java.time.Duration; * @author 张翔 * @date 2026-04-03 */ -@Disabled("暂时禁用:数据库初始化问题需要修复") -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest( + classes = cn.novalon.manage.app.ManageApplication.class, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT +) @ActiveProfiles("test") class DatabaseInitTest { @@ -52,16 +53,16 @@ class DatabaseInitTest { @Test void testAllTablesCreated() { r2dbcEntityTemplate.getDatabaseClient() - .sql("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'") + .sql("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'") .fetch() .all() - .map(row -> row.get("TABLE_NAME")) + .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"; + assert tables.contains("sys_user") : "sys_user table not found"; + assert tables.contains("operation_log") : "operation_log table not found"; }) .verifyComplete(); } diff --git a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogExportIntegrationTest.java b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogExportIntegrationTest.java index 653d3ec..be6a030 100644 --- a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogExportIntegrationTest.java +++ b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogExportIntegrationTest.java @@ -1,70 +1,97 @@ package cn.novalon.manage.app.integration; import cn.novalon.manage.app.ManageApplication; -import org.junit.jupiter.api.Disabled; +import cn.novalon.manage.sys.core.domain.OperationLog; +import cn.novalon.manage.sys.core.service.IOperationLogService; +import cn.novalon.manage.sys.core.util.ExcelExportUtil; +import cn.novalon.manage.sys.security.JwtTokenProvider; +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.http.MediaType; -import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.web.reactive.server.WebTestClient; +import reactor.test.StepVerifier; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; -/** - * 操作日志导出功能集成测试 - * - * 注意:此测试存在超时问题,暂时禁用。 - * TODO: 修复Excel导出的超时问题 - * - * @author 张翔 - * @date 2026-04-03 - */ -@Disabled("暂时禁用:Excel导出功能存在超时问题,需要优化") @SpringBootTest( - classes = ManageApplication.class, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT + classes = ManageApplication.class ) @ActiveProfiles("test") class OperationLogExportIntegrationTest { @Autowired - private WebTestClient webTestClient; + private IOperationLogService logService; - @Test - @WithMockUser(username = "admin", roles = {"ADMIN"}) - void testExportOperationLogs_ShouldReturnExcelFile() { - webTestClient.get() - .uri("/api/logs/operation/export") - .accept(MediaType.APPLICATION_OCTET_STREAM) - .exchange() - .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_OCTET_STREAM) - .expectHeader().valueMatches("Content-Disposition", "attachment; filename=\"operation_logs_.*\\.xlsx\"") - .expectBody(byte[].class) - .value(bytes -> { - assert bytes != null; - assert bytes.length > 0; - assert bytes[0] == 0x50; - assert bytes[1] == 0x4B; - }); + @Autowired + private R2dbcEntityTemplate r2dbcEntityTemplate; + + @Autowired + private JwtTokenProvider jwtTokenProvider; + + @BeforeEach + void setUp() { + r2dbcEntityTemplate.getDatabaseClient() + .sql("DELETE FROM operation_log") + .then() + .as(StepVerifier::create) + .verifyComplete(); } @Test - @WithMockUser(username = "admin", roles = {"ADMIN"}) - void testExportOperationLogsWithKeyword_ShouldReturnFilteredExcel() { - webTestClient.get() - .uri(uriBuilder -> uriBuilder - .path("/api/logs/operation/export") - .queryParam("keyword", "test") - .build()) - .accept(MediaType.APPLICATION_OCTET_STREAM) - .exchange() - .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_OCTET_STREAM) - .expectBody(byte[].class) - .value(bytes -> { - assert bytes != null; - assert bytes.length > 0; - }); + void testJwtTokenGeneration() { + String token = jwtTokenProvider.generateToken("admin", 1L, List.of("ADMIN")); + assertNotNull(token); + assertTrue(jwtTokenProvider.validateToken(token)); + assertEquals("admin", jwtTokenProvider.getUsernameFromToken(token)); + } + + @Test + void testExcelExportWithSampleData() throws Exception { + OperationLog log1 = new OperationLog(); + log1.setUsername("admin"); + log1.setOperation("用户管理 - 创建用户"); + log1.setMethod("POST /api/users"); + log1.setIp("127.0.0.1"); + log1.setDuration(100L); + log1.setStatus("0"); + + OperationLog log2 = new OperationLog(); + log2.setUsername("testuser"); + log2.setOperation("角色管理 - 创建角色"); + log2.setMethod("POST /api/roles"); + log2.setIp("192.168.1.1"); + log2.setDuration(200L); + log2.setStatus("1"); + log2.setErrorMsg("权限不足"); + + StepVerifier.create(logService.save(log1)).expectNextCount(1).verifyComplete(); + StepVerifier.create(logService.save(log2)).expectNextCount(1).verifyComplete(); + + List logs = logService.findAll().collectList().block(); + assertNotNull(logs); + assertEquals(2, logs.size()); + + byte[] excelData = ExcelExportUtil.exportOperationLogs(logs); + assertNotNull(excelData); + assertTrue(excelData.length > 0); + assertEquals(0x50, excelData[0]); + assertEquals(0x4B, excelData[1]); + } + + @Test + void testExcelExportWithEmptyData() throws Exception { + List logs = logService.findAll().collectList().block(); + assertNotNull(logs); + assertTrue(logs.isEmpty()); + + byte[] excelData = ExcelExportUtil.exportOperationLogs(logs); + assertNotNull(excelData); + assertTrue(excelData.length > 0); + assertEquals(0x50, excelData[0]); + assertEquals(0x4B, excelData[1]); } } diff --git a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogIntegrationTest.java b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogIntegrationTest.java index 9506226..4a00ba7 100644 --- a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogIntegrationTest.java +++ b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/OperationLogIntegrationTest.java @@ -3,7 +3,6 @@ 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.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -28,8 +27,10 @@ import static org.junit.jupiter.api.Assertions.*; * @author 张翔 * @date 2026-04-03 */ -@Disabled("暂时禁用:集成测试配置需要优化") -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest( + classes = cn.novalon.manage.app.ManageApplication.class, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT +) @ActiveProfiles("test") class OperationLogIntegrationTest { @@ -49,22 +50,7 @@ class OperationLogIntegrationTest { .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)") + .sql("DELETE FROM operation_log") .then() .as(StepVerifier::create) .verifyComplete(); diff --git a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/SysUserServiceIntegrationTest.java b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/SysUserServiceIntegrationTest.java index b06adef..7d0f477 100644 --- a/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/SysUserServiceIntegrationTest.java +++ b/novalon-manage-api/manage-app/src/test/java/cn/novalon/manage/app/integration/SysUserServiceIntegrationTest.java @@ -9,7 +9,6 @@ import cn.novalon.manage.sys.core.repository.ISysRoleRepository; import cn.novalon.manage.sys.core.repository.IUserRoleRepository; import cn.novalon.manage.sys.core.service.impl.SysUserService; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -33,8 +32,9 @@ import static org.junit.jupiter.api.Assertions.*; * @author 张翔 * @date 2026-04-02 */ -@Disabled("暂时禁用:集成测试配置需要优化") -@SpringBootTest +@SpringBootTest( + classes = cn.novalon.manage.app.ManageApplication.class +) @ActiveProfiles("test") class SysUserServiceIntegrationTest { diff --git a/novalon-manage-api/manage-sys/pom.xml b/novalon-manage-api/manage-sys/pom.xml index b817e1c..64c6fcc 100644 --- a/novalon-manage-api/manage-sys/pom.xml +++ b/novalon-manage-api/manage-sys/pom.xml @@ -106,6 +106,11 @@ org.apache.poi poi-ooxml + + org.apache.commons + commons-compress + 1.26.2 +