-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Order API TDD] rest-assured 테스트 격리 코드 추가
- Loading branch information
1 parent
2ae8051
commit 45c3b6e
Showing
4 changed files
with
78 additions
and
6 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
65 changes: 65 additions & 0 deletions
65
product-order-service/src/test/java/com/example/productorderservice/DatabaseCleanup.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,65 @@ | ||
package com.example.productorderservice; | ||
|
||
import com.google.common.base.CaseFormat; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.metamodel.EntityType; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
public class DatabaseCleanup implements InitializingBean { | ||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
private List<String> tableNames; | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
// JPA 모든 Entity 조회 | ||
final Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities(); | ||
// @Entity, @Table 선언된 엔티티의 테이블 이름을 가져와서 리스트에 담기 | ||
tableNames = entities.stream() | ||
.filter(e -> isEntity(e) && hasTableAnnotation(e)) | ||
.map(e -> { | ||
String tableName = e.getJavaType().getAnnotation(Table.class).name(); | ||
return tableName.isBlank() ? CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, e.getName()) : tableName; | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
// @Entity 선언이 되어있지만 @Table 선언이 되어있지 않는 엔티티는 UPPER_CAMEL -> LOWER_UNDERSCORE 변환 | ||
final List<String> entityNames = entities.stream() | ||
.filter(e -> isEntity(e) && !hasTableAnnotation(e)) | ||
.map(e -> CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, e.getName())) | ||
.toList(); | ||
|
||
tableNames.addAll(entityNames); | ||
} | ||
|
||
private boolean isEntity(final EntityType<?> e) { | ||
return null != e.getJavaType().getAnnotation(Entity.class); | ||
} | ||
|
||
private boolean hasTableAnnotation(final EntityType<?> e) { | ||
return null != e.getJavaType().getAnnotation(Table.class); | ||
} | ||
|
||
@Transactional | ||
public void execute() { | ||
entityManager.flush(); | ||
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate(); | ||
|
||
for (final String tableName : tableNames) { | ||
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName).executeUpdate(); | ||
entityManager.createNativeQuery("ALTER TABLE " + tableName + " ALTER COLUMN ID RESTART WITH 1").executeUpdate(); | ||
} | ||
|
||
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate(); | ||
} | ||
} |
11 changes: 10 additions & 1 deletion
11
product-order-service/src/test/java/com/example/productorderservice/product/ApiTest.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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
package com.example.productorderservice.product; | ||
|
||
import com.example.productorderservice.DatabaseCleanup; | ||
import io.restassured.RestAssured; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class ApiTest { | ||
|
||
@Autowired | ||
private DatabaseCleanup databaseCleanup; | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
RestAssured.port = port; | ||
if (RestAssured.port == RestAssured.UNDEFINED_PORT) { | ||
RestAssured.port = port; | ||
databaseCleanup.afterPropertiesSet(); | ||
} | ||
databaseCleanup.execute(); | ||
} | ||
} |
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