Skip to content

Commit

Permalink
Fix (integration-tests) : replace existing DataSource configuration w…
Browse files Browse the repository at this point in the history
…ith DataSourceConfigOverload

This change enhances connection management by using an in-memory database and facilitates integration testing.
  • Loading branch information
Lob2018 committed Jan 25, 2025
1 parent 589b0f2 commit 50757bd
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import fr.softsf.sudokufx.annotations.ExcludedFromCoverageReportGenerated;
import fr.softsf.sudokufx.utils.MyLogback;
import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore;
import fr.softsf.sudokufx.utils.os.OsFolderFactoryManager;
import org.flywaydb.core.Flyway;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.*;

import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME;

/**
* Configuration class for setting up dynamic data sources and related beans.
*/
@Configuration
@Profile("default")
@PropertySource("classpath:fr/softsf/sudokufx/application.properties")
@ExcludedFromCoverageReportGenerated
public class DataSourceConfig {

/**
Expand Down
16 changes: 0 additions & 16 deletions src/main/resources/fr/softsf/sudokufx/application-test.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import fr.softsf.sudokufx.dto.SoftwareDto;
import fr.softsf.sudokufx.service.SoftwareService;
import fr.softsf.sudokufx.utils.database.configuration.DataSourceConfigOverload;
import fr.softsf.sudokufx.viewmodel.FullMenuViewModel;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
Expand All @@ -23,8 +23,8 @@

@Slf4j
@SpringBootTest
@TestPropertySource("classpath:fr/softsf/sudokufx/application-test.properties")
@ActiveProfiles("test")
@ActiveProfiles("overload")
@Import(DataSourceConfigOverload.class)
@ExtendWith(SystemStubsExtension.class)
class FullMenuViewModelITest {

Expand All @@ -42,16 +42,10 @@ class FullMenuViewModelITest {

@BeforeEach
void eachSetup() {
log.info("↓↓↓↓↓↓↓↓↓↓ START INTEGRATION TEST : FullMenuViewModelITest ↓↓↓↓↓↓↓↓↓↓");
systemProperties.set(APP_NAME_PROPERTY, "SudokuFX");
systemProperties.set(APP_VERSION_PROPERTY, "1.0.0");
}

@AfterEach
void eachEnded() {
log.info("↑↑↑↑↑↑↑↑↑↑ END INTEGRATION TEST : FullMenuViewModelITest ↑↑↑↑↑↑↑↑↑↑");
}

@Test
void testSoftwareFound() {
fullMenuViewModel.test();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package fr.softsf.sudokufx.utils.database.configuration;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import fr.softsf.sudokufx.utils.MyLogback;
import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore;
import fr.softsf.sudokufx.utils.os.OsFolderFactoryManager;
import org.flywaydb.core.Flyway;
import org.springframework.context.annotation.*;

import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME;

/**
* Overloaded configuration class for setting up dynamic data sources and related beans.
*/
@Configuration
@Profile("overload")
@PropertySource("classpath:application-test.properties")
public class DataSourceConfigOverload {

/**
* Initializes Logback logging framework.
*
* @param myLogback Custom Logback configuration bean
* @return Always returns 0
*/
@Bean
int logbackInitialization(final MyLogback myLogback) {
myLogback.printLogEntryMessage();
return 0;
}

/**
* Creates and configures the main DataSource for the application. This bean
* depends on logbackInitialization to ensure Logback is properly set up.
*
* @param osFolderFactory Factory for creating OS-specific folders
* @param keystore Application keystore for secure storage
* @return Configured DataSource
*/
@Bean
@DependsOn({"logbackInitialization"})
HikariDataSource hikariDataSource(final OsFolderFactoryManager osFolderFactory, final ApplicationKeystore keystore) {
keystore.setupApplicationKeystore();
final HikariConfig config = new HikariConfig();
config.setPoolName("SudoFXHikariConnection");
config.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
config.setJdbcUrl("jdbc:hsqldb:mem:" + osFolderFactory.osFolderFactory().getOsDataFolderPath() + "/" + DATABASE_NAME.getPath()+"Test" + ";shutdown=true");
config.setUsername(keystore.getUsername());
config.setPassword(keystore.getPassword());
config.setMaximumPoolSize(2);
config.setMinimumIdle(1);
config.setAutoCommit(false);
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
return new HikariDataSource(config);
}

/**
* Configures Flyway with a specified location for migration scripts.
* <p>
* This method initializes a Flyway instance that will manage database migrations
* using the provided data source. It specifies the location of the migration scripts
* which Flyway will execute to ensure the database schema is up-to-date.
*
* @param hikariDataSource the HikariDataSource used by Flyway to connect to the database.
* This data source should be properly configured with the necessary
* connection details (URL, username, password).
* @return a Flyway instance configured with the specified data source and migration script location.
* The initMethod "migrate" will be called automatically after the bean is created,
* applying any pending migrations to the database.
*/
@Bean(initMethod = "migrate")
Flyway flyway(final HikariDataSource hikariDataSource) {
return Flyway.configure()
.dataSource(hikariDataSource)
.locations("classpath:fr/softsf/sudokufx/flyway/scripts/hsqldb/migration")
.load();
}
}
6 changes: 6 additions & 0 deletions src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hibernate configuration
spring.jpa.hibernate.ddl-auto=validate

# Development configuration
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

0 comments on commit 50757bd

Please sign in to comment.