From 5fd03a68a8a00b3a122b08083375cb5a0695e492 Mon Sep 17 00:00:00 2001 From: Ludy87 Date: Sun, 10 Aug 2025 12:12:22 +0200 Subject: [PATCH] Create ScheduledTasksTest.java --- .../security/database/ScheduledTasksTest.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 app/proprietary/src/test/java/stirling/software/proprietary/security/database/ScheduledTasksTest.java diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/security/database/ScheduledTasksTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/security/database/ScheduledTasksTest.java new file mode 100644 index 000000000..0cdeeb3ec --- /dev/null +++ b/app/proprietary/src/test/java/stirling/software/proprietary/security/database/ScheduledTasksTest.java @@ -0,0 +1,68 @@ +package stirling.software.proprietary.security.database; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.lang.reflect.Method; +import java.sql.SQLException; +import java.util.Arrays; + +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.context.annotation.Conditional; +import org.springframework.scheduling.annotation.Scheduled; + +import stirling.software.common.model.exception.UnsupportedProviderException; +import stirling.software.proprietary.security.service.DatabaseServiceInterface; + +@ExtendWith(MockitoExtension.class) +class ScheduledTasksTest { + + @Mock private DatabaseServiceInterface databaseService; + + @Test + void performBackup_calls_exportDatabase() throws Exception { + ScheduledTasks tasks = new ScheduledTasks(databaseService); + + tasks.performBackup(); + + verify(databaseService, times(1)).exportDatabase(); + verifyNoMoreInteractions(databaseService); + } + + @Test + void performBackup_propagates_SQLException() throws Exception { + ScheduledTasks tasks = new ScheduledTasks(databaseService); + doThrow(new SQLException("boom")).when(databaseService).exportDatabase(); + + assertThrows(SQLException.class, tasks::performBackup); + } + + @Test + void performBackup_propagates_UnsupportedProviderException() throws Exception { + ScheduledTasks tasks = new ScheduledTasks(databaseService); + doThrow(new UnsupportedProviderException("nope")).when(databaseService).exportDatabase(); + + assertThrows(UnsupportedProviderException.class, tasks::performBackup); + } + + @Test + void hasScheduledAnnotation_withExpectedCron() throws Exception { + Method m = ScheduledTasks.class.getDeclaredMethod("performBackup"); + Scheduled scheduled = m.getAnnotation(Scheduled.class); + assertNotNull(scheduled, "@Scheduled annotation missing on performBackup()"); + assertEquals("0 0 0 * * ?", scheduled.cron(), "Unexpected cron expression"); + } + + @Test + void classHasConditional_onH2SQLCondition() { + Conditional conditional = ScheduledTasks.class.getAnnotation(Conditional.class); + assertNotNull(conditional, "@Conditional missing on ScheduledTasks class"); + + boolean containsH2 = + Arrays.stream(conditional.value()).anyMatch(c -> c == H2SQLCondition.class); + assertTrue(containsH2, "@Conditional should include H2SQLCondition"); + } +}