From 1c688473b44d4d8c8a8860777a0bd27a95cecab8 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Tue, 21 May 2024 03:07:59 +0000 Subject: [PATCH 1/2] feat : depend on test containers to run integrationtest --- ...boot-jpa-hibernate2ndlevelcache-sample.yml | 6 ---- .vscode/launch.json | 10 ++++++ .../hibernatecache/TestApplication.java | 35 +++++++++++++++++++ .../common/ContainersConfig.java | 2 +- .../src/test/resources/redisson-test.yml | 2 -- 5 files changed, 46 insertions(+), 9 deletions(-) delete mode 100644 jpa/boot-hibernate2ndlevelcache-sample/src/test/resources/redisson-test.yml diff --git a/.github/workflows/boot-jpa-hibernate2ndlevelcache-sample.yml b/.github/workflows/boot-jpa-hibernate2ndlevelcache-sample.yml index bd9935c6b..dd7453164 100644 --- a/.github/workflows/boot-jpa-hibernate2ndlevelcache-sample.yml +++ b/.github/workflows/boot-jpa-hibernate2ndlevelcache-sample.yml @@ -31,11 +31,5 @@ jobs: java-version: 21 distribution: "temurin" cache: "maven" - - name: Start up redis via Docker Compose - run: | - cd docker - docker compose up -d redis - sleep 5 - docker ps -a - name: Build and analyze run: ./mvnw clean verify diff --git a/.vscode/launch.json b/.vscode/launch.json index 939849c22..94ef3969b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -113,6 +113,16 @@ "projectName": "boot-shedlock-sample", "args": "--spring.profiles.active=local", "envFile": "${workspaceFolder}/.env" + }, + { + "type": "java", + "name": "Spring Boot-TestApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.example.hibernatecache.TestApplication", + "projectName": "boot-hibernate2ndlevelcache-sample", + "args": "", + "envFile": "${workspaceFolder}/.env" } ] } \ No newline at end of file diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java index 5f4024cfb..ac49e8593 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java @@ -1,15 +1,50 @@ package com.example.hibernatecache; import com.example.hibernatecache.common.ContainersConfig; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import org.springframework.boot.SpringApplication; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.testcontainers.context.ImportTestcontainers; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.context.annotation.Bean; +import org.springframework.core.io.ClassPathResource; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.DockerImageName; @TestConfiguration(proxyBeanMethods = false) @ImportTestcontainers(ContainersConfig.class) public class TestApplication { + @Bean + @ServiceConnection("redis") + GenericContainer redisContainer() throws IOException { + GenericContainer redisContiner = + new GenericContainer<>(DockerImageName.parse("redis").withTag("7.2.5-alpine")) + .withExposedPorts(6379); + redisContiner.start(); + String ymlContent = + """ + singleServerConfig: + address: "redis://%s:%d" + """; + String formatted = + ymlContent.formatted(redisContiner.getHost(), redisContiner.getMappedPort(6379)); + String resourcesPath = new ClassPathResource("").getURL().getPath(); + + String yamlFilePath = resourcesPath + "redisson-test.yml"; + + // Create the YML FIle + File yamlFile = new File(yamlFilePath); + FileWriter writer = new FileWriter(yamlFile); + writer.write(formatted); + writer.close(); + return redisContiner; + } + public static void main(String[] args) { + System.setProperty("spring.profiles.active", "test"); SpringApplication.from(Application::main).with(TestApplication.class).run(args); } } diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/common/ContainersConfig.java b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/common/ContainersConfig.java index 3c3035091..b969ed5d2 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/common/ContainersConfig.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/common/ContainersConfig.java @@ -6,5 +6,5 @@ public interface ContainersConfig { @ServiceConnection - PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer<>("postgres:16.2-alpine"); + PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer<>("postgres:16.3-alpine"); } diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/test/resources/redisson-test.yml b/jpa/boot-hibernate2ndlevelcache-sample/src/test/resources/redisson-test.yml deleted file mode 100644 index ab834b4cb..000000000 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/test/resources/redisson-test.yml +++ /dev/null @@ -1,2 +0,0 @@ -singleServerConfig: - address: "redis://localhost:6379" From 1d95d7d3c1e6d7e75b48ca648da3a5c1a122b686 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Tue, 21 May 2024 03:21:02 +0000 Subject: [PATCH 2/2] fix : code review comments --- .../TestUltimateRedisApplication.java | 8 +++---- .../hibernatecache/TestApplication.java | 22 ++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java index 50996b8cb..aa76830d0 100644 --- a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java +++ b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java @@ -14,12 +14,12 @@ class TestUltimateRedisApplication { @Bean @ServiceConnection(name = "redis") GenericContainer redisContainer(DynamicPropertyRegistry dynamicPropertyRegistry) { - GenericContainer redisContiner = + GenericContainer redisContainer = new GenericContainer<>(DockerImageName.parse("redis").withTag("7.2.5-alpine")) .withExposedPorts(6379); - dynamicPropertyRegistry.add("cache.redis-port", () -> redisContiner.getMappedPort(6379)); - dynamicPropertyRegistry.add("cache.redis-host", redisContiner::getHost); - return redisContiner; + dynamicPropertyRegistry.add("cache.redis-port", () -> redisContainer.getMappedPort(6379)); + dynamicPropertyRegistry.add("cache.redis-host", redisContainer::getHost); + return redisContainer; } public static void main(String[] args) { diff --git a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java index ac49e8593..ab97cc880 100644 --- a/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java +++ b/jpa/boot-hibernate2ndlevelcache-sample/src/test/java/com/example/hibernatecache/TestApplication.java @@ -18,29 +18,25 @@ public class TestApplication { @Bean - @ServiceConnection("redis") + @ServiceConnection(name = "redis") GenericContainer redisContainer() throws IOException { - GenericContainer redisContiner = + GenericContainer redisContainer = new GenericContainer<>(DockerImageName.parse("redis").withTag("7.2.5-alpine")) .withExposedPorts(6379); - redisContiner.start(); + redisContainer.start(); String ymlContent = """ singleServerConfig: address: "redis://%s:%d" """; - String formatted = - ymlContent.formatted(redisContiner.getHost(), redisContiner.getMappedPort(6379)); + String finalYml = + ymlContent.formatted(redisContainer.getHost(), redisContainer.getMappedPort(6379)); String resourcesPath = new ClassPathResource("").getURL().getPath(); - String yamlFilePath = resourcesPath + "redisson-test.yml"; - - // Create the YML FIle - File yamlFile = new File(yamlFilePath); - FileWriter writer = new FileWriter(yamlFile); - writer.write(formatted); - writer.close(); - return redisContiner; + try (FileWriter writer = new FileWriter(new File(yamlFilePath))) { + writer.write(finalYml); + } + return redisContainer; } public static void main(String[] args) {