Skip to content

Commit

Permalink
refactor: 도커 컴포즈 사용하도록 롤백 및 database cleaner 실행할때 레디스도 같이 초기화하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
seulgi99 committed Aug 2, 2024
1 parent 8b56532 commit f2ac4d9
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/develop_build_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/production_build_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pull_request_gradle_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 9 additions & 17 deletions src/test/java/com/gdschongik/gdsc/config/TestRedisConfig.java
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/gdschongik/gdsc/helper/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public abstract class IntegrationTest {
@Autowired
protected DatabaseCleaner databaseCleaner;

@Autowired
protected RedisCleaner redisCleaner;

@Autowired
protected MemberRepository memberRepository;

Expand Down Expand Up @@ -87,6 +90,7 @@ public abstract class IntegrationTest {
@BeforeEach
void setUp() {
databaseCleaner.execute();
redisCleaner.execute();
doNothing().when(delegateMemberDiscordEventHandler).delegate(any());
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/gdschongik/gdsc/helper/RedisCleaner.java
Original file line number Diff line number Diff line change
@@ -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<String, String> connection;
private RedisCommands<String, String> 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();
}
}
7 changes: 5 additions & 2 deletions src/test/java/com/gdschongik/gdsc/helper/RepositoryTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -22,8 +21,12 @@ public abstract class RepositoryTest {
@Autowired
protected TestEntityManager testEntityManager;

@Autowired
protected RedisCleaner redisCleaner;

@BeforeEach
void setUp() {
databaseCleaner.execute();
redisCleaner.execute();
}
}

0 comments on commit f2ac4d9

Please sign in to comment.