From 9b151aa5903813b5d4c1a36645e8b1ec53066421 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Fri, 1 Nov 2024 14:44:38 +0000 Subject: [PATCH 1/4] feat : upgrade to spring boot 3.4.0-RC1 --- jpa/boot-data-multipledatasources/pom.xml | 6 +++--- .../multipledatasources/bootstrap/DataInitilizer.java | 4 ++-- .../configuration/CardHolderDataSourceConfiguration.java | 2 +- .../configuration/MemberDataSourceConfiguration.java | 2 +- .../{model => entities}/cardholder/CardHolder.java | 2 +- .../{model => entities}/member/Member.java | 2 +- .../repository/cardholder/CardHolderRepository.java | 2 +- .../repository/member/MemberRepository.java | 2 +- .../service/impl/DetailsServiceImpl.java | 4 ++-- .../multipledatasources/ApplicationIntegrationTest.java | 4 ++-- .../controller/DetailsControllerTest.java | 4 ++-- .../service/impl/DetailsServiceImplTest.java | 4 ++-- 12 files changed, 19 insertions(+), 19 deletions(-) rename jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/{model => entities}/cardholder/CardHolder.java (94%) rename jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/{model => entities}/member/Member.java (94%) diff --git a/jpa/boot-data-multipledatasources/pom.xml b/jpa/boot-data-multipledatasources/pom.xml index 8d7d12b7e..b69e2b1dc 100644 --- a/jpa/boot-data-multipledatasources/pom.xml +++ b/jpa/boot-data-multipledatasources/pom.xml @@ -5,13 +5,13 @@ org.springframework.boot spring-boot-starter-parent - 3.3.5 + 3.4.0-RC1 com.example boot-data-multipledatasources 0.0.1-SNAPSHOT - multipledatasources + boot-data-multipledatasources Demo project for using multiple datasource's in a Spring Boot Application @@ -220,7 +220,7 @@ - 2.47.0 + 2.50.0 diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitilizer.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitilizer.java index b8d6abbd8..57df2688c 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitilizer.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitilizer.java @@ -1,7 +1,7 @@ package com.example.multipledatasources.bootstrap; -import com.example.multipledatasources.model.cardholder.CardHolder; -import com.example.multipledatasources.model.member.Member; +import com.example.multipledatasources.entities.cardholder.CardHolder; +import com.example.multipledatasources.entities.member.Member; import com.example.multipledatasources.repository.cardholder.CardHolderRepository; import com.example.multipledatasources.repository.member.MemberRepository; import lombok.RequiredArgsConstructor; diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java index 014d5b1f5..8726f9eb0 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java @@ -1,6 +1,6 @@ package com.example.multipledatasources.configuration; -import com.example.multipledatasources.model.cardholder.CardHolder; +import com.example.multipledatasources.entities.cardholder.CardHolder; import com.example.multipledatasources.repository.cardholder.CardHolderRepository; import com.zaxxer.hikari.HikariDataSource; import jakarta.persistence.EntityManagerFactory; diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java index 04f8f3dcd..c97abdb43 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java @@ -1,6 +1,6 @@ package com.example.multipledatasources.configuration; -import com.example.multipledatasources.model.member.Member; +import com.example.multipledatasources.entities.member.Member; import com.example.multipledatasources.repository.member.MemberRepository; import com.zaxxer.hikari.HikariDataSource; import jakarta.persistence.EntityManagerFactory; diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/model/cardholder/CardHolder.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/entities/cardholder/CardHolder.java similarity index 94% rename from jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/model/cardholder/CardHolder.java rename to jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/entities/cardholder/CardHolder.java index e90e8b672..37169d263 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/model/cardholder/CardHolder.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/entities/cardholder/CardHolder.java @@ -1,4 +1,4 @@ -package com.example.multipledatasources.model.cardholder; +package com.example.multipledatasources.entities.cardholder; import jakarta.persistence.Column; import jakarta.persistence.Entity; diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/model/member/Member.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/entities/member/Member.java similarity index 94% rename from jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/model/member/Member.java rename to jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/entities/member/Member.java index 1bcb1d461..d1d994bff 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/model/member/Member.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/entities/member/Member.java @@ -1,4 +1,4 @@ -package com.example.multipledatasources.model.member; +package com.example.multipledatasources.entities.member; import jakarta.persistence.Column; import jakarta.persistence.Entity; diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/cardholder/CardHolderRepository.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/cardholder/CardHolderRepository.java index db10152eb..f9cb0477d 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/cardholder/CardHolderRepository.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/cardholder/CardHolderRepository.java @@ -1,6 +1,6 @@ package com.example.multipledatasources.repository.cardholder; -import com.example.multipledatasources.model.cardholder.CardHolder; +import com.example.multipledatasources.entities.cardholder.CardHolder; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/member/MemberRepository.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/member/MemberRepository.java index b50c9a3df..091faa549 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/member/MemberRepository.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/repository/member/MemberRepository.java @@ -1,6 +1,6 @@ package com.example.multipledatasources.repository.member; -import com.example.multipledatasources.model.member.Member; +import com.example.multipledatasources.entities.member.Member; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/service/impl/DetailsServiceImpl.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/service/impl/DetailsServiceImpl.java index 659fb4e97..113ce9235 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/service/impl/DetailsServiceImpl.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/service/impl/DetailsServiceImpl.java @@ -1,8 +1,8 @@ package com.example.multipledatasources.service.impl; import com.example.multipledatasources.dto.ResponseDto; -import com.example.multipledatasources.model.cardholder.CardHolder; -import com.example.multipledatasources.model.member.Member; +import com.example.multipledatasources.entities.cardholder.CardHolder; +import com.example.multipledatasources.entities.member.Member; import com.example.multipledatasources.repository.cardholder.CardHolderRepository; import com.example.multipledatasources.repository.member.MemberRepository; import com.example.multipledatasources.service.DetailsService; diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java index 4a4b764f7..eb0dbc0d2 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java @@ -25,11 +25,11 @@ class ApplicationIntegrationTest { @Container - private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer<>("mysql:9.0"); + private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer<>("mysql:9.1"); @Container private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = - new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.3-alpine")); + new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.0-alpine")); @DynamicPropertySource static void registerDynamicProperties(DynamicPropertyRegistry registry) { diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/controller/DetailsControllerTest.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/controller/DetailsControllerTest.java index aa424e421..d555fb1ba 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/controller/DetailsControllerTest.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/controller/DetailsControllerTest.java @@ -10,8 +10,8 @@ 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.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; @WebMvcTest(controllers = DetailsController.class) @@ -20,7 +20,7 @@ class DetailsControllerTest { @Autowired private MockMvc mockMvc; - @MockBean + @MockitoBean private DetailsService detailsService; @Test diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/service/impl/DetailsServiceImplTest.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/service/impl/DetailsServiceImplTest.java index 27b75f2ee..90c29fc98 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/service/impl/DetailsServiceImplTest.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/service/impl/DetailsServiceImplTest.java @@ -4,8 +4,8 @@ import static org.mockito.BDDMockito.given; import com.example.multipledatasources.dto.ResponseDto; -import com.example.multipledatasources.model.cardholder.CardHolder; -import com.example.multipledatasources.model.member.Member; +import com.example.multipledatasources.entities.cardholder.CardHolder; +import com.example.multipledatasources.entities.member.Member; import com.example.multipledatasources.repository.cardholder.CardHolderRepository; import com.example.multipledatasources.repository.member.MemberRepository; import java.util.Optional; From b1a99727618cd14c181ff7ee09f0184434183ad0 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Fri, 1 Nov 2024 15:43:52 +0000 Subject: [PATCH 2/4] use testcontainers in dev mode --- .../ApplicationIntegrationTest.java | 28 +---------- .../TestMultipleDataSourcesApplication.java | 31 ++----------- .../common/ContainersConfiguration.java | 46 +++++++++++++++++++ 3 files changed, 51 insertions(+), 54 deletions(-) create mode 100644 jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java index eb0dbc0d2..f3fdfadaa 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java @@ -5,42 +5,18 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.example.multipledatasources.common.ContainersConfiguration; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; -import org.testcontainers.containers.MySQLContainer; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ContainersConfiguration.class) @AutoConfigureMockMvc -@Testcontainers(disabledWithoutDocker = true, parallel = true) class ApplicationIntegrationTest { - @Container - private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer<>("mysql:9.1"); - - @Container - private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = - new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.0-alpine")); - - @DynamicPropertySource - static void registerDynamicProperties(DynamicPropertyRegistry registry) { - registry.add("app.datasource.cardholder.url", MY_SQL_CONTAINER::getJdbcUrl); - registry.add("app.datasource.cardholder.username", MY_SQL_CONTAINER::getUsername); - registry.add("app.datasource.cardholder.password", MY_SQL_CONTAINER::getPassword); - registry.add("app.datasource.member.url", POSTGRE_SQL_CONTAINER::getJdbcUrl); - registry.add("app.datasource.member.username", POSTGRE_SQL_CONTAINER::getUsername); - registry.add("app.datasource.member.password", POSTGRE_SQL_CONTAINER::getPassword); - } - @Autowired private MockMvc mockMvc; diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/TestMultipleDataSourcesApplication.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/TestMultipleDataSourcesApplication.java index bda41d6f9..7a83dcb07 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/TestMultipleDataSourcesApplication.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/TestMultipleDataSourcesApplication.java @@ -1,38 +1,13 @@ package com.example.multipledatasources; +import com.example.multipledatasources.common.ContainersConfiguration; import org.springframework.boot.SpringApplication; -import org.springframework.boot.test.context.TestConfiguration; -import org.testcontainers.containers.MySQLContainer; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.lifecycle.Startables; -import org.testcontainers.utility.DockerImageName; -@TestConfiguration -public class TestMultipleDataSourcesApplication { - - private static final MySQLContainer MY_SQL_CONTAINER = - new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1")); - - private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = - new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.0-alpine")); - - static { - Startables.deepStart(MY_SQL_CONTAINER, POSTGRE_SQL_CONTAINER).join(); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - MY_SQL_CONTAINER.stop(); - POSTGRE_SQL_CONTAINER.stop(); - })); - } +class TestMultipleDataSourcesApplication { public static void main(String[] args) { - System.setProperty("app.datasource.cardholder.url", MY_SQL_CONTAINER.getJdbcUrl()); - System.setProperty("app.datasource.cardholder.username", MY_SQL_CONTAINER.getUsername()); - System.setProperty("app.datasource.cardholder.password", MY_SQL_CONTAINER.getPassword()); - System.setProperty("app.datasource.member.url", POSTGRE_SQL_CONTAINER.getJdbcUrl()); - System.setProperty("app.datasource.member.username", POSTGRE_SQL_CONTAINER.getUsername()); - System.setProperty("app.datasource.member.password", POSTGRE_SQL_CONTAINER.getPassword()); SpringApplication.from(MultipleDataSourcesApplication::main) - .with(TestMultipleDataSourcesApplication.class) + .with(ContainersConfiguration.class) .run(args); } } diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java new file mode 100644 index 000000000..bc715ca4b --- /dev/null +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java @@ -0,0 +1,46 @@ +package com.example.multipledatasources.common; + +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.DynamicPropertyRegistrar; +import org.testcontainers.containers.MySQLContainer; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.utility.DockerImageName; + +@TestConfiguration(proxyBeanMethods = false) +public class ContainersConfiguration { + + @Bean + MySQLContainer mySQLContainer() { + MySQLContainer mysql = + new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1")); + mysql.start(); + return mysql; + } + + @Bean + PostgreSQLContainer postgreSQLContainer() { + PostgreSQLContainer postgres = + new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.0-alpine")); + postgres.start(); + return postgres; + } + + @Bean + public DynamicPropertyRegistrar kafkaProperties( + MySQLContainer mySQLContainer, PostgreSQLContainer postgreSQLContainer) { + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + mySQLContainer.stop(); + postgreSQLContainer.stop(); + })); + return (properties) -> { + // Connect our Spring application to our Testcontainers instances + properties.add("app.datasource.cardholder.url", mySQLContainer::getJdbcUrl); + properties.add("app.datasource.cardholder.username", mySQLContainer::getUsername); + properties.add("app.datasource.cardholder.password", mySQLContainer::getPassword); + properties.add("app.datasource.member.url", postgreSQLContainer::getJdbcUrl); + properties.add("app.datasource.member.username", postgreSQLContainer::getUsername); + properties.add("app.datasource.member.password", postgreSQLContainer::getPassword); + }; + } +} From ea9ac64f5ec329d13c2f8b4f4a647f5c9be80f64 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Fri, 1 Nov 2024 16:16:04 +0000 Subject: [PATCH 3/4] polish code --- .../ApplicationIntegrationTest.java | 4 +- .../common/ContainersConfiguration.java | 38 +++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java index f3fdfadaa..ebee06210 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/ApplicationIntegrationTest.java @@ -13,7 +13,9 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ContainersConfiguration.class) +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = {ContainersConfiguration.class, MultipleDataSourcesApplication.class}) @AutoConfigureMockMvc class ApplicationIntegrationTest { diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java index bc715ca4b..61aef1a2a 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java @@ -1,5 +1,6 @@ package com.example.multipledatasources.common; +import jakarta.annotation.PreDestroy; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.test.context.DynamicPropertyRegistrar; @@ -7,32 +8,35 @@ import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.utility.DockerImageName; +/** + * Test configuration class that manages MySQL and PostgreSQL containers for integration testing. + * This class is responsible for starting the containers, registering their connection properties, + * and ensuring proper cleanup on shutdown. + */ @TestConfiguration(proxyBeanMethods = false) public class ContainersConfiguration { + private MySQLContainer mySQLContainer; + private PostgreSQLContainer postgreSQLContainer; + @Bean MySQLContainer mySQLContainer() { - MySQLContainer mysql = - new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1")); - mysql.start(); - return mysql; + mySQLContainer = new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1")); + mySQLContainer.start(); + return mySQLContainer; } @Bean PostgreSQLContainer postgreSQLContainer() { - PostgreSQLContainer postgres = + postgreSQLContainer = new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.0-alpine")); - postgres.start(); - return postgres; + postgreSQLContainer.start(); + return postgreSQLContainer; } @Bean - public DynamicPropertyRegistrar kafkaProperties( + public DynamicPropertyRegistrar databaseProperties( MySQLContainer mySQLContainer, PostgreSQLContainer postgreSQLContainer) { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - mySQLContainer.stop(); - postgreSQLContainer.stop(); - })); return (properties) -> { // Connect our Spring application to our Testcontainers instances properties.add("app.datasource.cardholder.url", mySQLContainer::getJdbcUrl); @@ -43,4 +47,14 @@ public DynamicPropertyRegistrar kafkaProperties( properties.add("app.datasource.member.password", postgreSQLContainer::getPassword); }; } + + @PreDestroy + public void cleanup() { + if (mySQLContainer != null && mySQLContainer.isRunning()) { + mySQLContainer.stop(); + } + if (postgreSQLContainer != null && postgreSQLContainer.isRunning()) { + postgreSQLContainer.stop(); + } + } } From aae93a65b3c37f96a7d114cf90986b55d4710078 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Fri, 1 Nov 2024 16:20:47 +0000 Subject: [PATCH 4/4] Upgrade --- jpa/boot-jpa-locks/docker/docker-compose.yml | 2 +- jpa/boot-jpa-locks/pom.xml | 25 +++++++++++++++++-- .../com/example/locks/utils/AppConstants.java | 1 + .../src/main/resources/application.properties | 2 ++ .../example/locks/SchemaValidationTest.java | 20 ++++++++++++--- .../com/example/locks/TestApplication.java | 7 ++++-- .../locks/common/ContainersConfig.java | 2 +- .../web/controllers/MovieControllerTest.java | 4 +-- 8 files changed, 51 insertions(+), 12 deletions(-) diff --git a/jpa/boot-jpa-locks/docker/docker-compose.yml b/jpa/boot-jpa-locks/docker/docker-compose.yml index 1396f1d86..aeacee398 100644 --- a/jpa/boot-jpa-locks/docker/docker-compose.yml +++ b/jpa/boot-jpa-locks/docker/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.8' services: postgresqldb: - image: postgres:16.4-alpine + image: postgres:17.0-alpine environment: - POSTGRES_USER=appuser - POSTGRES_PASSWORD=secret diff --git a/jpa/boot-jpa-locks/pom.xml b/jpa/boot-jpa-locks/pom.xml index d2cec97b3..57e1ff670 100644 --- a/jpa/boot-jpa-locks/pom.xml +++ b/jpa/boot-jpa-locks/pom.xml @@ -7,7 +7,7 @@ org.springframework.boot spring-boot-starter-parent - 3.3.5 + 3.4.0-RC1 com.example.locks @@ -272,7 +272,7 @@ - 2.47.0 + 2.50.0 @@ -385,4 +385,25 @@ + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + diff --git a/jpa/boot-jpa-locks/src/main/java/com/example/locks/utils/AppConstants.java b/jpa/boot-jpa-locks/src/main/java/com/example/locks/utils/AppConstants.java index 1fc1e35cc..f16d9996d 100644 --- a/jpa/boot-jpa-locks/src/main/java/com/example/locks/utils/AppConstants.java +++ b/jpa/boot-jpa-locks/src/main/java/com/example/locks/utils/AppConstants.java @@ -4,6 +4,7 @@ @UtilityClass public final class AppConstants { + public static final String PROFILE_LOCAL = "local"; public static final String PROFILE_PROD = "prod"; public static final String PROFILE_NOT_PROD = "!" + PROFILE_PROD; public static final String PROFILE_TEST = "test"; diff --git a/jpa/boot-jpa-locks/src/main/resources/application.properties b/jpa/boot-jpa-locks/src/main/resources/application.properties index d8622a800..a5039997e 100644 --- a/jpa/boot-jpa-locks/src/main/resources/application.properties +++ b/jpa/boot-jpa-locks/src/main/resources/application.properties @@ -14,6 +14,8 @@ spring.jpa.show-sql=false spring.jpa.open-in-view=false spring.data.jpa.repositories.bootstrap-mode=deferred spring.datasource.hikari.auto-commit=false +spring.datasource.hikari.pool-name=HikariPool-${spring.application.name} +spring.datasource.hikari.data-source-properties.ApplicationName=${spring.application.name} spring.jpa.hibernate.ddl-auto=none #spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.jdbc.time_zone=UTC diff --git a/jpa/boot-jpa-locks/src/test/java/com/example/locks/SchemaValidationTest.java b/jpa/boot-jpa-locks/src/test/java/com/example/locks/SchemaValidationTest.java index 4088ba22f..54ab51625 100644 --- a/jpa/boot-jpa-locks/src/test/java/com/example/locks/SchemaValidationTest.java +++ b/jpa/boot-jpa-locks/src/test/java/com/example/locks/SchemaValidationTest.java @@ -1,14 +1,26 @@ package com.example.locks; +import static org.assertj.core.api.Assertions.assertThat; + import com.example.locks.common.ContainersConfig; +import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.boot.testcontainers.context.ImportTestcontainers; +import org.springframework.context.annotation.Import; -@DataJpaTest(properties = {"spring.jpa.hibernate.ddl-auto=validate", "spring.test.database.replace=none"}) -@ImportTestcontainers(ContainersConfig.class) +@DataJpaTest(properties = {"spring.jpa.hibernate.ddl-auto=validate"}) +@AutoConfigureTestDatabase +@Import(ContainersConfig.class) class SchemaValidationTest { + @Autowired + private DataSource dataSource; + @Test - void validateJpaMappingsWithDbSchema() {} + void validateJpaMappingsWithDbSchema() { + assertThat(dataSource).isInstanceOf(HikariDataSource.class); + } } diff --git a/jpa/boot-jpa-locks/src/test/java/com/example/locks/TestApplication.java b/jpa/boot-jpa-locks/src/test/java/com/example/locks/TestApplication.java index 7ce575990..402de513d 100644 --- a/jpa/boot-jpa-locks/src/test/java/com/example/locks/TestApplication.java +++ b/jpa/boot-jpa-locks/src/test/java/com/example/locks/TestApplication.java @@ -1,12 +1,15 @@ package com.example.locks; import com.example.locks.common.ContainersConfig; +import com.example.locks.utils.AppConstants; import org.springframework.boot.SpringApplication; public class TestApplication { public static void main(String[] args) { - System.setProperty("spring.profiles.active", "local"); - SpringApplication.from(Application::main).with(ContainersConfig.class).run(args); + SpringApplication.from(Application::main) + .with(ContainersConfig.class) + .withAdditionalProfiles(AppConstants.PROFILE_LOCAL) + .run(args); } } diff --git a/jpa/boot-jpa-locks/src/test/java/com/example/locks/common/ContainersConfig.java b/jpa/boot-jpa-locks/src/test/java/com/example/locks/common/ContainersConfig.java index 413b05c2d..a3e6b56d1 100644 --- a/jpa/boot-jpa-locks/src/test/java/com/example/locks/common/ContainersConfig.java +++ b/jpa/boot-jpa-locks/src/test/java/com/example/locks/common/ContainersConfig.java @@ -12,6 +12,6 @@ public class ContainersConfig { @Bean @ServiceConnection PostgreSQLContainer postgreSQLContainer() { - return new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("16.3-alpine")); + return new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.0-alpine")); } } diff --git a/jpa/boot-jpa-locks/src/test/java/com/example/locks/web/controllers/MovieControllerTest.java b/jpa/boot-jpa-locks/src/test/java/com/example/locks/web/controllers/MovieControllerTest.java index 6422d1ac8..9730e448c 100644 --- a/jpa/boot-jpa-locks/src/test/java/com/example/locks/web/controllers/MovieControllerTest.java +++ b/jpa/boot-jpa-locks/src/test/java/com/example/locks/web/controllers/MovieControllerTest.java @@ -33,12 +33,12 @@ 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.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.http.HttpHeaders; 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 = MovieController.class) @@ -48,7 +48,7 @@ class MovieControllerTest { @Autowired private MockMvc mockMvc; - @MockBean + @MockitoBean private MovieService movieService; @Autowired