diff --git a/audit-base/src/main/java/com/wultra/core/audit/base/database/DatabaseAuditWriter.java b/audit-base/src/main/java/com/wultra/core/audit/base/database/DatabaseAuditWriter.java index 137e296..e64c143 100644 --- a/audit-base/src/main/java/com/wultra/core/audit/base/database/DatabaseAuditWriter.java +++ b/audit-base/src/main/java/com/wultra/core/audit/base/database/DatabaseAuditWriter.java @@ -258,11 +258,11 @@ public void cleanup() { final LocalDateTime cleanupLimit = LocalDateTime.now().minusDays(cleanupDays); synchronized (CLEANUP_LOCK) { transactionTemplate.executeWithoutResult(status -> { - jdbcTemplate.execute("DELETE FROM " + tableNameAudit + " WHERE timestamp_created < ?", (PreparedStatementCallback) ps -> { + jdbcTemplate.execute("DELETE FROM " + tableNameParam + " WHERE timestamp_created < ?", (PreparedStatementCallback) ps -> { ps.setTimestamp(1, Timestamp.valueOf(cleanupLimit)); return ps.execute(); }); - jdbcTemplate.execute("DELETE FROM " + tableNameParam + " WHERE timestamp_created < ?", (PreparedStatementCallback) ps -> { + jdbcTemplate.execute("DELETE FROM " + tableNameAudit + " WHERE timestamp_created < ?", (PreparedStatementCallback) ps -> { ps.setTimestamp(1, Timestamp.valueOf(cleanupLimit)); return ps.execute(); }); diff --git a/audit-base/src/test/java/com/wultra/core/audit/base/AuditParamEnabledTest.java b/audit-base/src/test/java/com/wultra/core/audit/base/AuditParamEnabledTest.java index e5c7e68..3b1d5fc 100644 --- a/audit-base/src/test/java/com/wultra/core/audit/base/AuditParamEnabledTest.java +++ b/audit-base/src/test/java/com/wultra/core/audit/base/AuditParamEnabledTest.java @@ -33,7 +33,11 @@ import static org.junit.jupiter.api.Assertions.*; -@SpringBootTest(classes = TestApplication.class, properties = {"audit.db.table.param.enabled=true"}) +@SpringBootTest(classes = TestApplication.class, properties = { + "audit.db.table.param.enabled=true", + "audit.db.cleanup.days=-1", // time shift to the future to enable cleanup test + "powerauth.audit.cleanup.delay.initial=60000" // delay the job start due to slow builds +}) @Sql(scripts = "/db_schema.sql") class AuditParamEnabledTest { @@ -122,4 +126,33 @@ void testAuditMoreParams() { assertEquals(new JsonUtil().serializeObject(timestamp), rs3.getString("param_value")); } + @Test + void testAuditCleanup() { + final Audit audit = auditFactory.getAudit(); + audit.info("test message", AuditDetail.builder().param("my_id", "test_id").build()); + audit.flush(); + + assertEquals(1, countAuditLogs()); + assertEquals(1, countAuditParams()); + + audit.cleanup(); + + assertEquals(0, countAuditLogs()); + assertEquals(0, countAuditParams()); + } + + private int countAuditLogs() { + return count("audit_log"); + } + + private int countAuditParams() { + return count("audit_param"); + } + + private int count(final String tableName) { + final SqlRowSet rs = jdbcTemplate.queryForRowSet("SELECT COUNT(*) FROM " + tableName); + assertTrue(rs.next()); + return rs.getInt(1); + } + } \ No newline at end of file diff --git a/audit-base/src/test/java/com/wultra/core/audit/base/AuditTest.java b/audit-base/src/test/java/com/wultra/core/audit/base/AuditTest.java index c921a91..e568832 100644 --- a/audit-base/src/test/java/com/wultra/core/audit/base/AuditTest.java +++ b/audit-base/src/test/java/com/wultra/core/audit/base/AuditTest.java @@ -17,6 +17,7 @@ import com.wultra.core.audit.base.model.AuditDetail; import com.wultra.core.audit.base.model.AuditLevel; +import org.awaitility.Awaitility; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -26,6 +27,7 @@ import org.springframework.test.context.jdbc.Sql; import java.sql.Timestamp; +import java.time.Duration; import static org.junit.jupiter.api.Assertions.*; @@ -219,4 +221,14 @@ void testAuditTrace() { assertTrue(rs.next()); assertEquals(0, rs.getInt(1)); } + + @Test + void testScheduledFlush() { + Audit audit = auditFactory.getAudit(); + audit.info("test message"); + + Awaitility.await() + .atMost(Duration.ofSeconds(5)) + .until(() -> jdbcTemplate.queryForRowSet("SELECT * FROM audit_log").next()); + } }