diff --git a/.vscode/launch.json b/.vscode/launch.json index f85e4d6aa..4d8a521d8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "java", + "name": "MultiDataSourceMultiTenancyApplicationTest", + "request": "launch", + "mainClass": "com.example.multitenancy.MultiDataSourceMultiTenancyApplicationTest", + "projectName": "multidatasource-multitenancy" + }, { "type": "java", "name": "Spring Boot-Application", @@ -153,6 +160,16 @@ "projectName": "boot-jndi-sample", "args": "--spring.profiles.active=local", "envFile": "${workspaceFolder}/.env" + }, + { + "type": "java", + "name": "Spring Boot-Application", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.example.multitenancy.MultiDataSourceMultiTenancyApplication", + "projectName": "multidatasource-multitenancy", + "args": "", + "envFile": "${workspaceFolder}/.env" } ] } \ No newline at end of file diff --git a/jpa/multitenancy/multidatasource-multitenancy/docker/docker-compose.yml b/jpa/multitenancy/multidatasource-multitenancy/docker/docker-compose.yml index 5950a20c3..04b10ad7a 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/docker/docker-compose.yml +++ b/jpa/multitenancy/multidatasource-multitenancy/docker/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.8' services: postgresqldb: - image: postgres:16.5-alpine + image: postgres:17.1-alpine environment: - POSTGRES_USER=appuser - POSTGRES_PASSWORD=secret diff --git a/jpa/multitenancy/multidatasource-multitenancy/pom.xml b/jpa/multitenancy/multidatasource-multitenancy/pom.xml index 47918b1e0..2acdbac32 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/pom.xml +++ b/jpa/multitenancy/multidatasource-multitenancy/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.3.6 + 3.4.0 com.example.multitenancy @@ -19,7 +19,7 @@ UTF-8 21 - 2.6.0 + 2.7.0-RC1 ${project.build.directory}/test-results 2.43.0 diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/Application.java b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/MultiDataSourceMultiTenancyApplication.java similarity index 76% rename from jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/Application.java rename to jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/MultiDataSourceMultiTenancyApplication.java index da56bf63f..bb3ac1ff6 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/Application.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/MultiDataSourceMultiTenancyApplication.java @@ -7,9 +7,9 @@ @SpringBootApplication @EnableConfigurationProperties({ApplicationProperties.class}) -public class Application { +public class MultiDataSourceMultiTenancyApplication { public static void main(String[] args) { - SpringApplication.run(Application.class, args); + SpringApplication.run(MultiDataSourceMultiTenancyApplication.class, args); } } diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/GlobalExceptionHandler.java b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/GlobalExceptionHandler.java index 1d17693db..ac93debf7 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/GlobalExceptionHandler.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/GlobalExceptionHandler.java @@ -16,7 +16,7 @@ @ControllerAdvice @Order(Ordered.HIGHEST_PRECEDENCE) -public class GlobalExceptionHandler { +class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/SwaggerConfig.java b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/SwaggerConfig.java index 3ad1ec9df..22346c5b4 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/SwaggerConfig.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/SwaggerConfig.java @@ -5,6 +5,8 @@ import io.swagger.v3.oas.annotations.servers.Server; import org.springframework.context.annotation.Configuration; -@Configuration -@OpenAPIDefinition(info = @Info(title = "db", version = "v1"), servers = @Server(url = "/")) -public class SwaggerConfig {} +@Configuration(proxyBeanMethods = false) +@OpenAPIDefinition( + info = @Info(title = "multidatasource-multitenancy", version = "v1"), + servers = @Server(url = "/")) +class SwaggerConfig {} diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/WebMvcConfig.java b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/WebMvcConfig.java index 528ba7feb..b18540d4f 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/WebMvcConfig.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/WebMvcConfig.java @@ -1,25 +1,30 @@ package com.example.multitenancy.config; import com.example.multitenancy.config.multitenant.MultiTenantInterceptor; -import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; +import org.springframework.lang.NonNull; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -@Configuration -@RequiredArgsConstructor -public class WebMvcConfig implements WebMvcConfigurer { +@Configuration(proxyBeanMethods = false) +class WebMvcConfig implements WebMvcConfigurer { + private final ApplicationProperties properties; private final MultiTenantInterceptor multiTenantInterceptor; + WebMvcConfig(ApplicationProperties properties, MultiTenantInterceptor multiTenantInterceptor) { + this.properties = properties; + this.multiTenantInterceptor = multiTenantInterceptor; + } + @Override - public void addInterceptors(InterceptorRegistry registry) { + public void addInterceptors(@NonNull InterceptorRegistry registry) { registry.addInterceptor(multiTenantInterceptor); } @Override - public void addCorsMappings(CorsRegistry registry) { + public void addCorsMappings(@NonNull CorsRegistry registry) { registry.addMapping(properties.getCors().getPathPattern()) .allowedMethods(properties.getCors().getAllowedMethods()) .allowedHeaders(properties.getCors().getAllowedHeaders()) diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/PrimaryDataSourceConfiguration.java b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/PrimaryDataSourceConfiguration.java index 9ff952eba..a47570b39 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/PrimaryDataSourceConfiguration.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/PrimaryDataSourceConfiguration.java @@ -2,12 +2,12 @@ import com.example.multitenancy.config.multitenant.TenantIdentifierResolver; import com.example.multitenancy.primary.entities.PrimaryCustomer; +import com.example.multitenancy.primary.repositories.PrimaryCustomerRepository; import com.example.multitenancy.utils.DatabaseType; import java.util.HashMap; import java.util.Map; import java.util.Objects; import javax.sql.DataSource; -import lombok.RequiredArgsConstructor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; @@ -21,17 +21,22 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; -@Configuration +@Configuration(proxyBeanMethods = false) @EnableJpaRepositories( - basePackages = "com.example.multitenancy.primary.repositories", + basePackageClasses = PrimaryCustomerRepository.class, entityManagerFactoryRef = "primaryEntityManagerFactory", transactionManagerRef = "primaryTransactionManager") -@RequiredArgsConstructor public class PrimaryDataSourceConfiguration { private final JpaProperties jpaProperties; private final TenantIdentifierResolver tenantIdentifierResolver; + public PrimaryDataSourceConfiguration( + JpaProperties jpaProperties, TenantIdentifierResolver tenantIdentifierResolver) { + this.jpaProperties = jpaProperties; + this.tenantIdentifierResolver = tenantIdentifierResolver; + } + @Bean(name = "primaryEntityManagerFactory") LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory( EntityManagerFactoryBuilder builder, diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/SecondaryDataSourceConfiguration.java b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/SecondaryDataSourceConfiguration.java index 6c4f09ca0..fb903d260 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/SecondaryDataSourceConfiguration.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multidatasource/SecondaryDataSourceConfiguration.java @@ -2,16 +2,17 @@ import com.example.multitenancy.config.multitenant.TenantIdentifierResolver; import com.example.multitenancy.secondary.entities.SecondaryCustomer; +import com.example.multitenancy.secondary.repositories.SecondaryCustomerRepository; import com.example.multitenancy.utils.DatabaseType; import java.util.HashMap; import java.util.Map; import java.util.Objects; import javax.sql.DataSource; -import lombok.RequiredArgsConstructor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; @@ -21,17 +22,23 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; -@Configuration +@Configuration(proxyBeanMethods = false) +@EntityScan(basePackageClasses = SecondaryCustomer.class) @EnableJpaRepositories( - basePackages = "com.example.multitenancy.secondary.repositories", + basePackageClasses = SecondaryCustomerRepository.class, entityManagerFactoryRef = "secondaryEntityManagerFactory", transactionManagerRef = "secondaryTransactionManager") -@RequiredArgsConstructor public class SecondaryDataSourceConfiguration { private final JpaProperties jpaProperties; private final TenantIdentifierResolver tenantIdentifierResolver; + public SecondaryDataSourceConfiguration( + JpaProperties jpaProperties, TenantIdentifierResolver tenantIdentifierResolver) { + this.jpaProperties = jpaProperties; + this.tenantIdentifierResolver = tenantIdentifierResolver; + } + @Bean(name = "secondaryEntityManagerFactory") LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory( EntityManagerFactoryBuilder builder, diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multitenant/MultiDatasourceConfig.java b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multitenant/MultiDatasourceConfig.java index 6650ba1a1..b4588a967 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multitenant/MultiDatasourceConfig.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/java/com/example/multitenancy/config/multitenant/MultiDatasourceConfig.java @@ -5,6 +5,7 @@ import liquibase.UpdateSummaryEnum; import liquibase.UpdateSummaryOutputEnum; import liquibase.integration.spring.SpringLiquibase; +import liquibase.ui.UIServiceEnum; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties; @@ -12,6 +13,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; @Configuration(proxyBeanMethods = false) public class MultiDatasourceConfig { @@ -58,7 +61,10 @@ DataSourceProperties secondaryDataSourceProperties() { DataSource secondaryDataSource( @Qualifier("secondaryDataSourceProperties") DataSourceProperties secondaryDataSourceProperties) { - return secondaryDataSourceProperties.initializeDataSourceBuilder().build(); + return secondaryDataSourceProperties + .initializeDataSourceBuilder() + .type(HikariDataSource.class) + .build(); } @Bean @@ -81,7 +87,10 @@ private SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperti liquibase.setDataSource(dataSource); liquibase.setChangeLog(properties.getChangeLog()); liquibase.setClearCheckSums(properties.isClearChecksums()); - liquibase.setContexts(properties.getContexts()); + if (!CollectionUtils.isEmpty(properties.getContexts())) { + liquibase.setContexts( + StringUtils.collectionToCommaDelimitedString(properties.getContexts())); + } liquibase.setDefaultSchema(properties.getDefaultSchema()); liquibase.setLiquibaseSchema(properties.getLiquibaseSchema()); liquibase.setLiquibaseTablespace(properties.getLiquibaseTablespace()); @@ -89,7 +98,10 @@ private SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperti liquibase.setDatabaseChangeLogLockTable(properties.getDatabaseChangeLogLockTable()); liquibase.setDropFirst(properties.isDropFirst()); liquibase.setShouldRun(properties.isEnabled()); - liquibase.setLabelFilter(properties.getLabelFilter()); + if (!CollectionUtils.isEmpty(properties.getLabelFilter())) { + liquibase.setLabelFilter( + StringUtils.collectionToCommaDelimitedString(properties.getLabelFilter())); + } liquibase.setChangeLogParameters(properties.getParameters()); liquibase.setRollbackFile(properties.getRollbackFile()); liquibase.setTestRollbackOnUpdate(properties.isTestRollbackOnUpdate()); @@ -101,6 +113,9 @@ private SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperti liquibase.setShowSummaryOutput( UpdateSummaryOutputEnum.valueOf(properties.getShowSummaryOutput().name())); } + if (properties.getUiService() != null) { + liquibase.setUiService(UIServiceEnum.valueOf(properties.getUiService().name())); + } return liquibase; } } diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/application.properties b/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/application.properties index 5ae4104a7..db21e226b 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/application.properties +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/application.properties @@ -1,11 +1,10 @@ spring.application.name=multidatasource-multitenancy server.port=8080 server.shutdown=graceful -spring.main.allow-bean-definition-overriding=true spring.jmx.enabled=false ################ Logging ##################### -logging.file.name=logs/multidatasource-multitenancy.log +logging.file.name=logs/${spring.application.name}.log logging.level.web=INFO logging.level.sql=INFO ## To enable transaction details logging @@ -20,7 +19,7 @@ management.endpoint.health.show-details=always ################ Database ##################### spring.jpa.show-sql=true spring.jpa.open-in-view=false -spring.jpa.hibernate.ddl-auto=validate +spring.jpa.hibernate.ddl-auto=none #spring.jpa.properties.hibernate.format_sql=true #spring.jpa.properties.hibernate.hbm2ddl.auto=validate spring.jpa.properties.hibernate.jdbc.time_zone=UTC @@ -30,6 +29,7 @@ spring.jpa.properties.hibernate.order_inserts=true spring.jpa.properties.hibernate.order_updates=true spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true +spring.jpa.properties.hibernate.query.plan_cache_max_size=4096 spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true @@ -38,12 +38,14 @@ datasource.primary.username=appuser datasource.primary.password=secret datasource.primary.driverClassName=org.postgresql.Driver datasource.primary.configuration.auto-commit=false +datasource.primary.configuration.pool-name=primaryPool datasource.primary.liquibase.change-log= classpath:/db/changelog/db.primary.changelog-master.yml datasource.secondary.url=jdbc:postgresql://localhost:5432/secondary datasource.secondary.username=appuser datasource.secondary.password=secret datasource.secondary.driverClassName=org.postgresql.Driver datasource.secondary.hikari.auto-commit=false +datasource.secondary.hikari.pool-name=secondaryPool datasource.secondary.liquibase.change-log= classpath:/db/changelog/db.secondary.changelog-master.yml spring.mvc.problemdetails.enabled=true diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/db/changelog/primary/db.changelog-xml-db.xml b/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/db/changelog/primary/db.changelog-xml-db.xml index 0bd3d2836..c30fbe7d6 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/db/changelog/primary/db.changelog-xml-db.xml +++ b/jpa/multitenancy/multidatasource-multitenancy/src/main/resources/db/changelog/primary/db.changelog-xml-db.xml @@ -3,7 +3,7 @@ xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog - http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.23.xsd"> + https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.29.xsd"> @@ -31,7 +31,7 @@ - + diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/MultiDataSourceMultiTenancyApplicationTest.java b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/MultiDataSourceMultiTenancyApplicationTest.java new file mode 100644 index 000000000..03563b4fe --- /dev/null +++ b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/MultiDataSourceMultiTenancyApplicationTest.java @@ -0,0 +1,15 @@ +package com.example.multitenancy; + +import com.example.multitenancy.common.ContainersConfiguration; +import com.example.multitenancy.utils.AppConstants; +import org.springframework.boot.SpringApplication; + +class MultiDataSourceMultiTenancyApplicationTest { + + public static void main(String[] args) { + SpringApplication.from(MultiDataSourceMultiTenancyApplication::main) + .with(ContainersConfiguration.class) + .withAdditionalProfiles(AppConstants.PROFILE_TEST) + .run(args); + } +} diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/AbstractIntegrationTest.java b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/AbstractIntegrationTest.java index 742fd1794..31c6a223a 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/AbstractIntegrationTest.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/AbstractIntegrationTest.java @@ -14,9 +14,9 @@ import org.springframework.test.web.servlet.MockMvc; @ActiveProfiles({PROFILE_TEST}) -@SpringBootTest(webEnvironment = RANDOM_PORT) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContainersConfiguration.class) @AutoConfigureMockMvc -public abstract class AbstractIntegrationTest extends DBContainerInitializer { +public abstract class AbstractIntegrationTest { @Autowired protected MockMvc mockMvc; diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/ContainersConfiguration.java b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/ContainersConfiguration.java new file mode 100644 index 000000000..67e2ae6fe --- /dev/null +++ b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/ContainersConfiguration.java @@ -0,0 +1,40 @@ +package com.example.multitenancy.common; + +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.DynamicPropertyRegistrar; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.oracle.OracleContainer; +import org.testcontainers.utility.DockerImageName; + +@TestConfiguration(proxyBeanMethods = false) +public class ContainersConfiguration { + + @Bean + PostgreSQLContainer postgreSQLContainer() { + return new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.1-alpine")); + } + + @Bean + OracleContainer oracleContainer() { + return new OracleContainer( + DockerImageName.parse("gvenzl/oracle-free").withTag("23-slim-faststart")) + .withReuse(true); + } + + @Bean + DynamicPropertyRegistrar databaseProperties( + OracleContainer oracleContainer, PostgreSQLContainer postgreSQLContainer) { + oracleContainer.start(); + postgreSQLContainer.start(); + return (propertyRegistry) -> { + // Connect our Spring application to our Testcontainers instances + propertyRegistry.add("datasource.primary.url", oracleContainer::getJdbcUrl); + propertyRegistry.add("datasource.primary.username", oracleContainer::getUsername); + propertyRegistry.add("datasource.primary.password", oracleContainer::getPassword); + propertyRegistry.add("datasource.secondary.url", postgreSQLContainer::getJdbcUrl); + propertyRegistry.add("datasource.secondary.username", postgreSQLContainer::getUsername); + propertyRegistry.add("datasource.secondary.password", postgreSQLContainer::getPassword); + }; + } +} diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/DBContainerInitializer.java b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/DBContainerInitializer.java deleted file mode 100644 index 337acff45..000000000 --- a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/common/DBContainerInitializer.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.example.multitenancy.common; - -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.lifecycle.Startables; -import org.testcontainers.oracle.OracleContainer; -import org.testcontainers.utility.DockerImageName; - -public class DBContainerInitializer { - - private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = - new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("16.3-alpine")) - .withReuse(true); - - private static final OracleContainer ORACLE_CONTAINER = - new OracleContainer( - DockerImageName.parse("gvenzl/oracle-free") - .withTag("23-slim-faststart")) - .withReuse(true); - - static { - Startables.deepStart(POSTGRE_SQL_CONTAINER, ORACLE_CONTAINER).join(); - } - - @DynamicPropertySource - static void addsDynamicProperties(DynamicPropertyRegistry propertyRegistry) { - propertyRegistry.add("datasource.primary.url", ORACLE_CONTAINER::getJdbcUrl); - propertyRegistry.add("datasource.primary.username", ORACLE_CONTAINER::getUsername); - propertyRegistry.add("datasource.primary.password", ORACLE_CONTAINER::getPassword); - propertyRegistry.add("datasource.secondary.url", POSTGRE_SQL_CONTAINER::getJdbcUrl); - propertyRegistry.add("datasource.secondary.username", POSTGRE_SQL_CONTAINER::getUsername); - propertyRegistry.add("datasource.secondary.password", POSTGRE_SQL_CONTAINER::getPassword); - } -} diff --git a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/web/controllers/PrimaryCustomerControllerTest.java b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/web/controllers/PrimaryCustomerControllerTest.java index 82c135c6b..00e62aefd 100644 --- a/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/web/controllers/PrimaryCustomerControllerTest.java +++ b/jpa/multitenancy/multidatasource-multitenancy/src/test/java/com/example/multitenancy/web/controllers/PrimaryCustomerControllerTest.java @@ -27,9 +27,9 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; @WebMvcTest(controllers = PrimaryCustomerController.class) @@ -39,8 +39,8 @@ class PrimaryCustomerControllerTest { @Autowired private MockMvc mockMvc; @Autowired private ObjectMapper objectMapper; - @MockBean private PrimaryCustomerService primaryCustomerService; - @MockBean private TenantIdentifierResolver tenantIdentifierResolver; + @MockitoBean private PrimaryCustomerService primaryCustomerService; + @MockitoBean private TenantIdentifierResolver tenantIdentifierResolver; private List primaryCustomerList;