From f2ac4d9ef5c98a2e238f526c1ef10eeff6deff79 Mon Sep 17 00:00:00 2001 From: seulgi99 Date: Sat, 3 Aug 2024 01:33:54 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=8F=84=EC=BB=A4=20=EC=BB=B4?= =?UTF-8?q?=ED=8F=AC=EC=A6=88=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=A1=A4=EB=B0=B1=20=EB=B0=8F=20database=20cleaner?= =?UTF-8?q?=20=EC=8B=A4=ED=96=89=ED=95=A0=EB=95=8C=20=EB=A0=88=EB=94=94?= =?UTF-8?q?=EC=8A=A4=EB=8F=84=20=EA=B0=99=EC=9D=B4=20=EC=B4=88=EA=B8=B0?= =?UTF-8?q?=ED=99=94=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/develop_build_deploy.yml | 4 +++ .github/workflows/production_build_deploy.yml | 4 +++ .../workflows/pull_request_gradle_build.yml | 4 +++ build.gradle | 1 - .../gdsc/config/TestRedisConfig.java | 26 ++++++--------- .../UnivEmailVerificationServiceTest.java | 3 -- .../gdsc/helper/IntegrationTest.java | 4 +++ .../gdschongik/gdsc/helper/RedisCleaner.java | 32 +++++++++++++++++++ .../gdsc/helper/RepositoryTest.java | 7 ++-- 9 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 src/test/java/com/gdschongik/gdsc/helper/RedisCleaner.java diff --git a/.github/workflows/develop_build_deploy.yml b/.github/workflows/develop_build_deploy.yml index 327548dba..79250f20e 100644 --- a/.github/workflows/develop_build_deploy.yml +++ b/.github/workflows/develop_build_deploy.yml @@ -36,6 +36,10 @@ jobs: - name: Run chmod to make gradlew executable run: chmod +x ./gradlew + # Redis 컨테이너 실행 + - name: Start containers + run: docker-compose -f ./docker-compose-test.yaml up -d + # Gradle 빌드 - name: Setup Gradle uses: gradle/actions/setup-gradle@v3 diff --git a/.github/workflows/production_build_deploy.yml b/.github/workflows/production_build_deploy.yml index 05ac36fe7..6e2144604 100644 --- a/.github/workflows/production_build_deploy.yml +++ b/.github/workflows/production_build_deploy.yml @@ -36,6 +36,10 @@ jobs: java-version: ${{ matrix.java-version }} distribution: ${{ matrix.distribution }} + # Redis 컨테이너 실행 + - name: Start containers + run: docker-compose -f ./docker-compose-test.yaml up -d + # Gradlew 실행 허용 - name: Run chmod to make gradlew executable run: chmod +x ./gradlew diff --git a/.github/workflows/pull_request_gradle_build.yml b/.github/workflows/pull_request_gradle_build.yml index 4819893b7..93673e0c0 100644 --- a/.github/workflows/pull_request_gradle_build.yml +++ b/.github/workflows/pull_request_gradle_build.yml @@ -24,6 +24,10 @@ jobs: - name: gradlew 권한 부여 run: chmod +x ./gradlew + # Redis 컨테이너 실행 + - name: Start containers + run: docker-compose -f ./docker-compose-test.yaml up -d + - name: Setup Gradle id: gradle uses: gradle/actions/setup-gradle@v3 diff --git a/build.gradle b/build.gradle index dfddafb7e..f3579ce80 100644 --- a/build.gradle +++ b/build.gradle @@ -60,7 +60,6 @@ dependencies { // Redis implementation 'org.springframework.boot:spring-boot-starter-data-redis' - testImplementation 'org.testcontainers:testcontainers' // Querydsl implementation 'com.querydsl:querydsl-jpa:5.1.0:jakarta' diff --git a/src/test/java/com/gdschongik/gdsc/config/TestRedisConfig.java b/src/test/java/com/gdschongik/gdsc/config/TestRedisConfig.java index f4e1eb55d..8e827c08c 100644 --- a/src/test/java/com/gdschongik/gdsc/config/TestRedisConfig.java +++ b/src/test/java/com/gdschongik/gdsc/config/TestRedisConfig.java @@ -1,20 +1,12 @@ package com.gdschongik.gdsc.config; -import org.junit.jupiter.api.extension.BeforeAllCallback; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.utility.DockerImageName; +import com.gdschongik.gdsc.global.config.RedisConfig; +import com.gdschongik.gdsc.global.property.RedisProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Import; -public class TestRedisConfig implements BeforeAllCallback { - private static final String REDIS_IMAGE = "redis:alpine"; - private static final int REDIS_PORT = 6379; - private GenericContainer redis; - - @Override - public void beforeAll(ExtensionContext context) { - redis = new GenericContainer(DockerImageName.parse(REDIS_IMAGE)).withExposedPorts(REDIS_PORT); - redis.start(); - System.setProperty("spring.data.redis.host", redis.getHost()); - System.setProperty("spring.data.redis.port", String.valueOf(redis.getMappedPort(REDIS_PORT))); - } -} +@TestConfiguration +@EnableConfigurationProperties({RedisProperty.class}) +@Import({RedisConfig.class}) +public class TestRedisConfig {} diff --git a/src/test/java/com/gdschongik/gdsc/domain/email/application/UnivEmailVerificationServiceTest.java b/src/test/java/com/gdschongik/gdsc/domain/email/application/UnivEmailVerificationServiceTest.java index 03b506776..f08f177ce 100644 --- a/src/test/java/com/gdschongik/gdsc/domain/email/application/UnivEmailVerificationServiceTest.java +++ b/src/test/java/com/gdschongik/gdsc/domain/email/application/UnivEmailVerificationServiceTest.java @@ -4,7 +4,6 @@ import static com.gdschongik.gdsc.global.exception.ErrorCode.*; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import com.gdschongik.gdsc.config.TestRedisConfig; import com.gdschongik.gdsc.domain.email.dto.request.UnivEmailVerificationRequest; import com.gdschongik.gdsc.domain.member.domain.Member; import com.gdschongik.gdsc.global.exception.CustomException; @@ -13,11 +12,9 @@ import com.gdschongik.gdsc.helper.IntegrationTest; import org.junit.jupiter.api.Nested; 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.mock.mockito.MockBean; -@ExtendWith(TestRedisConfig.class) public class UnivEmailVerificationServiceTest extends IntegrationTest { @Autowired diff --git a/src/test/java/com/gdschongik/gdsc/helper/IntegrationTest.java b/src/test/java/com/gdschongik/gdsc/helper/IntegrationTest.java index b3ba6cf3e..236887590 100644 --- a/src/test/java/com/gdschongik/gdsc/helper/IntegrationTest.java +++ b/src/test/java/com/gdschongik/gdsc/helper/IntegrationTest.java @@ -51,6 +51,9 @@ public abstract class IntegrationTest { @Autowired protected DatabaseCleaner databaseCleaner; + @Autowired + protected RedisCleaner redisCleaner; + @Autowired protected MemberRepository memberRepository; @@ -87,6 +90,7 @@ public abstract class IntegrationTest { @BeforeEach void setUp() { databaseCleaner.execute(); + redisCleaner.execute(); doNothing().when(delegateMemberDiscordEventHandler).delegate(any()); } diff --git a/src/test/java/com/gdschongik/gdsc/helper/RedisCleaner.java b/src/test/java/com/gdschongik/gdsc/helper/RedisCleaner.java new file mode 100644 index 000000000..ff655ecb4 --- /dev/null +++ b/src/test/java/com/gdschongik/gdsc/helper/RedisCleaner.java @@ -0,0 +1,32 @@ +package com.gdschongik.gdsc.helper; + +import com.gdschongik.gdsc.global.property.RedisProperty; +import io.lettuce.core.RedisClient; +import io.lettuce.core.RedisURI; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.sync.RedisCommands; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class RedisCleaner implements InitializingBean { + private final RedisProperty redisProperty; + private RedisClient redisClient; + private StatefulRedisConnection connection; + private RedisCommands commands; + + @Override + public void afterPropertiesSet() { + RedisURI redisUri = RedisURI.Builder.redis(redisProperty.getHost(), redisProperty.getPort()) + .build(); + redisClient = RedisClient.create(redisUri); + connection = redisClient.connect(); + commands = connection.sync(); + } + + public void execute() { + commands.flushdb(); + } +} diff --git a/src/test/java/com/gdschongik/gdsc/helper/RepositoryTest.java b/src/test/java/com/gdschongik/gdsc/helper/RepositoryTest.java index 3b00b1e35..978874ab2 100644 --- a/src/test/java/com/gdschongik/gdsc/helper/RepositoryTest.java +++ b/src/test/java/com/gdschongik/gdsc/helper/RepositoryTest.java @@ -1,7 +1,6 @@ package com.gdschongik.gdsc.helper; import com.gdschongik.gdsc.config.TestQuerydslConfig; -import com.gdschongik.gdsc.config.TestRedisConfig; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; @@ -11,7 +10,7 @@ import org.springframework.test.context.ActiveProfiles; @DataJpaTest -@Import({TestQuerydslConfig.class, TestRedisConfig.class, DatabaseCleaner.class}) +@Import({TestQuerydslConfig.class, DatabaseCleaner.class, RedisCleaner.class}) @ActiveProfiles("test") @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) public abstract class RepositoryTest { @@ -22,8 +21,12 @@ public abstract class RepositoryTest { @Autowired protected TestEntityManager testEntityManager; + @Autowired + protected RedisCleaner redisCleaner; + @BeforeEach void setUp() { databaseCleaner.execute(); + redisCleaner.execute(); } }