diff --git a/server/src/test/java/com/project/yozmcafe/DataInitializer.java b/server/src/test/java/com/project/yozmcafe/DataInitializer.java index 02421e2f..d5a318a8 100644 --- a/server/src/test/java/com/project/yozmcafe/DataInitializer.java +++ b/server/src/test/java/com/project/yozmcafe/DataInitializer.java @@ -4,14 +4,10 @@ import jakarta.persistence.PersistenceContext; import jakarta.persistence.Query; import org.junit.jupiter.api.BeforeEach; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; -import javax.sql.DataSource; -import java.sql.ResultSet; -import java.sql.Statement; import java.util.ArrayList; import java.util.List; @@ -21,20 +17,18 @@ public class DataInitializer { private static final int OFF = 0; private static final int ON = 1; - private static final int FIRST_COLUMN = 1; private static final String FLYWAY = "flyway"; + private static final String TRUNCATE = "TRUNCATE "; - @Autowired - private DataSource dataSource; @PersistenceContext private EntityManager em; - private final List deleteDMLs = new ArrayList<>(); + private final List truncationDMLs = new ArrayList<>(); @BeforeEach @Transactional public void deleteAll() { - if (deleteDMLs.isEmpty()) { + if (truncationDMLs.isEmpty()) { init(); } @@ -48,25 +42,17 @@ private void setForeignKeyEnabled(final int enabled) { } private void truncateAllTables() { - deleteDMLs.stream() + truncationDMLs.stream() .map(em::createNativeQuery) .forEach(Query::executeUpdate); } private void init() { - try (final Statement statement = dataSource.getConnection().createStatement()) { - final ResultSet resultSet = statement.executeQuery("SHOW TABLES "); + final List tableNames = em.createNativeQuery("SHOW TABLES ").getResultList(); - while (resultSet.next()) { - final String tableName = resultSet.getString(FIRST_COLUMN); - if (tableName.contains(FLYWAY)) { - continue; - } - - deleteDMLs.add("TRUNCATE " + tableName); - } - } catch (Exception e) { - e.printStackTrace(); - } + tableNames.stream() + .filter(name -> !name.contains(FLYWAY)) + .map(TRUNCATE::concat) + .forEach(truncationDMLs::add); } }