Skip to content

Commit

Permalink
[Order API TDD] rest-assured 테스트 격리 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jihunparkme committed Feb 26, 2024
1 parent 2ae8051 commit 45c3b6e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
3 changes: 3 additions & 0 deletions product-order-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.google.guava:guava:33.0.0-jre'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.4.0'

}

tasks.named('test') {
Expand Down
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();
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import static org.assertj.core.api.Assertions.assertThat;

class ProductApiTest extends ApiTest {

@Autowired
private ProductService productService;


@Test
void 상품등록() {
final var request = getAddProductRequest();
Expand Down

0 comments on commit 45c3b6e

Please sign in to comment.