|
| 1 | +package com.huntly.server.task; |
| 2 | + |
| 3 | +import com.huntly.server.domain.entity.GlobalSetting; |
| 4 | +import com.huntly.server.service.GlobalSettingService; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.io.TempDir; |
| 8 | +import org.mockito.Mock; |
| 9 | +import org.mockito.MockitoAnnotations; |
| 10 | + |
| 11 | +import javax.sql.DataSource; |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.sql.Connection; |
| 17 | +import java.sql.PreparedStatement; |
| 18 | +import java.sql.SQLException; |
| 19 | +import java.text.SimpleDateFormat; |
| 20 | +import java.time.Instant; |
| 21 | +import java.time.temporal.ChronoUnit; |
| 22 | +import java.util.Date; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.mockito.ArgumentMatchers.anyString; |
| 26 | +import static org.mockito.ArgumentMatchers.eq; |
| 27 | +import static org.mockito.Mockito.*; |
| 28 | + |
| 29 | +class DatabaseBackupTaskTest { |
| 30 | + |
| 31 | + @Mock |
| 32 | + private GlobalSettingService settingService; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private DataSource dataSource; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private Connection connection; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private PreparedStatement preparedStatement; |
| 42 | + |
| 43 | + private DatabaseBackupTask backupTask; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setUp() throws SQLException { |
| 47 | + MockitoAnnotations.openMocks(this); |
| 48 | + when(dataSource.getConnection()).thenReturn(connection); |
| 49 | + when(connection.prepareStatement(anyString())).thenReturn(preparedStatement); |
| 50 | + backupTask = new DatabaseBackupTask(settingService, dataSource); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void autoBackupDatabase_whenBackupPathNotConfigured_shouldSkip() throws SQLException { |
| 55 | + GlobalSetting setting = new GlobalSetting(); |
| 56 | + setting.setBackupPath(""); |
| 57 | + when(settingService.getGlobalSetting()).thenReturn(setting); |
| 58 | + |
| 59 | + backupTask.autoBackupDatabase(); |
| 60 | + |
| 61 | + verify(dataSource, never()).getConnection(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void autoBackupDatabase_shouldExecuteVacuumIntoAndCleanExpired(@TempDir Path backupDir) throws IOException, SQLException { |
| 66 | + // Mock settings |
| 67 | + GlobalSetting setting = new GlobalSetting(); |
| 68 | + setting.setBackupPath(backupDir.toString()); |
| 69 | + setting.setBackupKeepDays(2); |
| 70 | + when(settingService.getGlobalSetting()).thenReturn(setting); |
| 71 | + |
| 72 | + // Pre-create some mock backup files |
| 73 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); |
| 74 | + |
| 75 | + // 1. A very old backup (e.g. 5 days ago) - should be deleted |
| 76 | + Instant fiveDaysAgo = Instant.now().minus(5, ChronoUnit.DAYS); |
| 77 | + String oldTimestamp = sdf.format(Date.from(fiveDaysAgo)); |
| 78 | + Path oldBackupFile = backupDir.resolve("db_backup_" + oldTimestamp + ".sqlite"); |
| 79 | + Files.writeString(oldBackupFile, "old-content"); |
| 80 | + |
| 81 | + // 2. A recent backup (e.g. 1 day ago) - should be kept |
| 82 | + Instant oneDayAgo = Instant.now().minus(1, ChronoUnit.DAYS); |
| 83 | + String recentTimestamp = sdf.format(Date.from(oneDayAgo)); |
| 84 | + Path recentBackupFile = backupDir.resolve("db_backup_" + recentTimestamp + ".sqlite"); |
| 85 | + Files.writeString(recentBackupFile, "recent-content"); |
| 86 | + |
| 87 | + // Run backup |
| 88 | + backupTask.autoBackupDatabase(); |
| 89 | + |
| 90 | + // Verify VACUUM INTO was called |
| 91 | + verify(connection).prepareStatement(eq("VACUUM INTO ?")); |
| 92 | + verify(preparedStatement).setString(eq(1), anyString()); |
| 93 | + verify(preparedStatement).execute(); |
| 94 | + |
| 95 | + // Verify old backup was deleted |
| 96 | + assertThat(Files.exists(oldBackupFile)).isFalse(); |
| 97 | + // Verify recent backup is kept |
| 98 | + assertThat(Files.exists(recentBackupFile)).isTrue(); |
| 99 | + } |
| 100 | +} |
0 commit comments