-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEV-32] 로컬 db mysql 변환 및 TestContainer 설정 (#75)
- Loading branch information
Showing
19 changed files
with
259 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
services: | ||
ddingdong-local-db: | ||
image: mysql:8.0 | ||
container_name: ddingdong_local_mysql | ||
platform: linux/x86_64 | ||
environment: # 환경 변수 설정 | ||
MYSQL_ROOT_PASSWORD: 1234 | ||
MYSQL_DATABASE: ddingdong_local_db | ||
MYSQL_CHARSET: utf8mb4 | ||
MYSQL_COLLATION: utf8mb4_unicode_ci | ||
TZ: Asia/Seoul | ||
ports: | ||
- "3307:3306" | ||
volumes: | ||
- backup-store:/var/lib/mysql | ||
volumes: | ||
backup-store : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/test/java/ddingdong/ddingdongBE/common/config/TestConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ddingdong.ddingdongBE.common.config; | ||
|
||
import ddingdong.ddingdongBE.common.support.DataInitializer; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@TestConfiguration | ||
@Import(DataInitializer.class) | ||
public class TestConfig { | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/ddingdong/ddingdongBE/common/support/DataInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ddingdong.ddingdongBE.common.support; | ||
|
||
import javax.persistence.PersistenceContext; | ||
import javax.persistence.Query; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Profile("test") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Component | ||
public class DataInitializer { | ||
|
||
private static final String OFF_FOREIGN_CONSTRAINTS = "SET foreign_key_checks = false"; | ||
private static final String ON_FOREIGN_CONSTRAINTS = "SET foreign_key_checks = true"; | ||
private static final String TRUNCATE_SQL_FORMAT = "TRUNCATE %s"; | ||
|
||
private static final List<String> truncationDMLs = new ArrayList<>(); | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
public void deleteAll() { | ||
if (truncationDMLs.isEmpty()) { | ||
init(); | ||
} | ||
|
||
em.createNativeQuery(OFF_FOREIGN_CONSTRAINTS).executeUpdate(); | ||
truncationDMLs.stream() | ||
.map(em::createNativeQuery) | ||
.forEach(Query::executeUpdate); | ||
em.createNativeQuery(ON_FOREIGN_CONSTRAINTS).executeUpdate(); | ||
} | ||
|
||
private void init() { | ||
final List<String> tableNames = em.createNativeQuery("SHOW TABLES ").getResultList(); | ||
|
||
tableNames.stream() | ||
.map(tableName -> String.format(TRUNCATE_SQL_FORMAT, tableName)) | ||
.forEach(truncationDMLs::add); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/ddingdong/ddingdongBE/common/support/DataJpaTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ddingdong.ddingdongBE.common.support; | ||
|
||
import ddingdong.ddingdongBE.common.config.TestConfig; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
@DataJpaTest | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
public abstract class DataJpaTestSupport extends TestContainerSupport { | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/ddingdong/ddingdongBE/common/support/TestContainerSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ddingdong.ddingdongBE.common.support; | ||
|
||
|
||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
import ddingdong.ddingdongBE.common.config.TestConfig; | ||
import lombok.NoArgsConstructor; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.JdbcDatabaseContainer; | ||
import org.testcontainers.containers.MySQLContainer; | ||
|
||
@NoArgsConstructor(access = PROTECTED) | ||
@ActiveProfiles("test") | ||
@Import(TestConfig.class) | ||
public abstract class TestContainerSupport { | ||
|
||
private static final String MYSQL_IMAGE = "mysql:8"; | ||
private static final int MYSQL_PORT = 3306; | ||
private static final JdbcDatabaseContainer<?> MYSQL; | ||
|
||
@Autowired | ||
private DataInitializer dataInitializer; | ||
|
||
// 싱글톤 | ||
static { | ||
MYSQL = new MySQLContainer<>(MYSQL_IMAGE) | ||
.withExposedPorts(MYSQL_PORT) | ||
.withReuse(true); | ||
|
||
MYSQL.start(); | ||
} | ||
|
||
// 동적으로 DB 속성 할당 | ||
@DynamicPropertySource | ||
public static void setUp(DynamicPropertyRegistry registry) { | ||
registry.add("spring.datasource.driver-class-name", MYSQL::getDriverClassName); | ||
registry.add("spring.datasource.url", MYSQL::getJdbcUrl); | ||
registry.add("spring.datasource.username", MYSQL::getUsername); | ||
registry.add("spring.datasource.password", MYSQL::getPassword); | ||
} | ||
|
||
@BeforeEach | ||
void delete() { | ||
dataInitializer.deleteAll(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/ddingdong/ddingdongBE/common/support/WebApiIntegrationTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ddingdong.ddingdongBE.common.support; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
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.test.web.servlet.MockMvc; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
public abstract class WebApiIntegrationTestSupport extends TestContainerSupport { | ||
|
||
@Autowired | ||
protected MockMvc mockMvc; | ||
|
||
@Autowired | ||
protected ObjectMapper objectMapper; | ||
|
||
protected String toJson(Object object) throws JsonProcessingException { | ||
return objectMapper.writeValueAsString(object); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...BE/support/WithMockAuthenticatedUser.java → ...on/support/WithMockAuthenticatedUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...henticatedUserSecurityContextFactory.java → ...henticatedUserSecurityContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.