mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-01-10 00:06:51 +01:00
#2700 handling exceptions
This commit is contained in:
parent
1d4fa4941f
commit
3a2c874e52
@ -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<FileInfo> 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<FileInfo> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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());
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user