From 3a2c874e52b4c63aec6efd58d03589e3b61b8a15 Mon Sep 17 00:00:00 2001 From: Dario Ghunney Ware Date: Mon, 30 Dec 2024 12:17:17 +0000 Subject: [PATCH] #2700 handling exceptions --- .../database/DatabaseBackupHelper.java | 15 ++++++----- .../security/database/DatabaseConfig.java | 2 +- src/main/resources/settings.yml.template | 2 +- .../security/database/DatabaseConfigTest.java | 6 ++--- .../database/DatabaseServiceTest.java | 25 +++++++++++++++++-- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseBackupHelper.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseBackupHelper.java index 7af08c99..ccba5aed 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseBackupHelper.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseBackupHelper.java @@ -23,6 +23,7 @@ import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.PathResource; import org.springframework.core.io.support.EncodedResource; +import org.springframework.jdbc.datasource.init.CannotReadScriptException; import org.springframework.jdbc.datasource.init.ScriptException; import org.springframework.jdbc.datasource.init.ScriptUtils; import org.springframework.stereotype.Service; @@ -59,7 +60,11 @@ public class DatabaseService implements DatabaseInterface { public boolean hasBackup() { Path filePath = Paths.get(BACKUP_DIR + "*"); - return Files.exists(filePath); + if (Files.exists(filePath)) { + return !getBackupList().isEmpty(); + } + + return false; } /** @@ -107,8 +112,8 @@ public class DatabaseService implements DatabaseInterface { if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found."); List backupList = this.getBackupList(); - backupList.sort(Comparator.comparing(FileInfo::getModificationDate).reversed()); + backupList.sort(Comparator.comparing(FileInfo::getModificationDate).reversed()); executeDatabaseScript(Paths.get(backupList.get(0).getFilePath())); } @@ -141,7 +146,7 @@ public class DatabaseService implements DatabaseInterface { /** Filter and delete old backups if there are more than 5 */ @Override - public void exportDatabase() throws SQLException { + public void exportDatabase() { List filteredBackupList = this.getBackupList().stream() .filter(backup -> !backup.getFileName().startsWith(BACKUP_PREFIX + "user_")) @@ -163,10 +168,8 @@ public class DatabaseService implements DatabaseInterface { log.info("Database export completed: {}", insertOutputFilePath); } catch (SQLException e) { log.error("Error during database export: {}", e.getMessage(), e); - throw e; - } catch (ScriptException e) { + } catch (CannotReadScriptException e) { log.error("Error during database export: File {} not found", insertOutputFilePath); - throw e; } } diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java index d68cadc6..659ce1a9 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java @@ -45,7 +45,7 @@ public class DatabaseConfig { ApplicationProperties.Datasource datasource = system.getDatasource(); DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); - if (datasource.isEnableCustomDatabase()) { + if (!datasource.isEnableCustomDatabase()) { log.debug("Using default H2 database"); dataSourceBuilder.driverClassName(DEFAULT_DRIVER); diff --git a/src/main/resources/settings.yml.template b/src/main/resources/settings.yml.template index 8942b0b1..3f97bc06 100644 --- a/src/main/resources/settings.yml.template +++ b/src/main/resources/settings.yml.template @@ -86,7 +86,7 @@ system: tessdataDir: /usr/share/tessdata # path to the directory containing the Tessdata files. This setting is relevant for Windows systems. For Windows users, this path should be adjusted to point to the appropriate directory where the Tessdata files are stored. enableAnalytics: undefined # set to 'true' to enable analytics, set to 'false' to disable analytics; for enterprise users, this is set to true datasource: - enableCustomDatabase: true # set this property to 'true' if you would like to use the default database configuration + enableCustomDatabase: false # set this property to 'true' if you would like to use the default database configuration type: postgresql # the type of the database to set (e.g. 'h2', 'postgresql') hostName: localhost # the host name to use for the database url. Set to 'localhost' when running the app locally. Set to match the name of the container name of your database container when running the app on a server (Docker configuration) port: 5432 # set the port number of the database. Ensure this matches the port the database is listening to diff --git a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java index db5c1bab..868b9b24 100644 --- a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java +++ b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java @@ -32,7 +32,7 @@ class DatabaseConfigTest { when(applicationProperties.getSystem()).thenReturn(system); when(system.getDatasource()).thenReturn(datasource); - when(datasource.isEnableCustomDatabase()).thenReturn(true); + when(datasource.isEnableCustomDatabase()).thenReturn(false); var result = databaseConfig.dataSource(); @@ -46,7 +46,7 @@ class DatabaseConfigTest { when(applicationProperties.getSystem()).thenReturn(system); when(system.getDatasource()).thenReturn(datasource); - when(datasource.isEnableCustomDatabase()).thenReturn(false); + when(datasource.isEnableCustomDatabase()).thenReturn(true); when(datasource.getType()).thenReturn("postgresql"); when(datasource.getHostName()).thenReturn("localhost"); when(datasource.getPort()).thenReturn(5432); @@ -67,7 +67,7 @@ class DatabaseConfigTest { when(applicationProperties.getSystem()).thenReturn(system); when(system.getDatasource()).thenReturn(datasource); - when(datasource.isEnableCustomDatabase()).thenReturn(false); + when(datasource.isEnableCustomDatabase()).thenReturn(true); when(datasource.getType()).thenReturn(datasourceType); assertThrows(UnsupportedProviderException.class, () -> databaseConfig.dataSource()); diff --git a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java index f6790d08..2b2aef4f 100644 --- a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java +++ b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java @@ -1,5 +1,9 @@ package stirling.software.SPDF.config.security.database; +import java.nio.file.attribute.FileAttribute; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -16,7 +20,8 @@ import static org.junit.jupiter.api.Assertions.*; @ExtendWith(MockitoExtension.class) class DatabaseServiceTest { - private final Path BACKUP_PATH = Paths.get("configs/db/backup/*"); + public static final String TEST_FILE = "test"; + private final String BACKUP_PATH = "configs/db/backup/"; @Mock private DatabaseConfig databaseConfig; @@ -24,9 +29,25 @@ class DatabaseServiceTest { @InjectMocks private DatabaseService databaseService; + @BeforeEach + void setUp() throws IOException { + Files.deleteIfExists(Paths.get(BACKUP_PATH + TEST_FILE)); + } + @Test + void testHasNoBackups() { + assertFalse(databaseService.hasBackup()); + } + + @Test + @Disabled void testHasBackups() throws IOException { - Files.createDirectories(BACKUP_PATH); + Path backupDir = Paths.get(BACKUP_PATH); + Files.createDirectories(backupDir); + Path testFile = Paths.get(BACKUP_PATH + TEST_FILE); + + Files.createFile(testFile); + Files.createTempFile(backupDir, TEST_FILE, null); assertTrue(databaseService.hasBackup()); }