Merge pull request #11 from wkrzywiec/add-new-book-event
database cleanup
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package io.wkrzywiec.hexagonal.library;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Table;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
//https://gist.github.com/JorgenRingen/a56837fc07e630c32280b8e3d14c2d24
|
||||
|
||||
@Service
|
||||
public class DatabaseCleanup implements InitializingBean {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
private List<String> tableNames;
|
||||
|
||||
@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("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
tableNames = entityManager.getMetamodel().getEntities().stream()
|
||||
.filter(e -> e.getJavaType().getAnnotation(Table.class) != null)
|
||||
.map(e -> e.getJavaType().getAnnotation(Table.class).name())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.DatabaseCleanup;
|
||||
import io.wkrzywiec.hexagonal.library.TestData;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.infrastructure.BookRepository;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Book;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -27,6 +29,10 @@ public class MakeReservationComponentTest {
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
private DatabaseCleanup databaseCleanup;
|
||||
|
||||
|
||||
private String baseURL;
|
||||
|
||||
@BeforeEach
|
||||
@@ -45,6 +51,11 @@ public class MakeReservationComponentTest {
|
||||
"john.doe@test.com");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void after() {
|
||||
databaseCleanup.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Reserve available book")
|
||||
public void givenBookIsAvailable_thenMakeReservation_thenBookIsReserved() {
|
||||
|
||||
@@ -2,8 +2,10 @@ package io.wkrzywiec.hexagonal.library.inventory;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.wkrzywiec.hexagonal.library.DatabaseCleanup;
|
||||
import io.wkrzywiec.hexagonal.library.TestData;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.AddNewBookCommand;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -26,6 +28,9 @@ public class AddNewBookComponentTest {
|
||||
@Autowired
|
||||
private JdbcTemplate jdbc;
|
||||
|
||||
@Autowired
|
||||
private DatabaseCleanup databaseCleanup;
|
||||
|
||||
private String baseURL;
|
||||
|
||||
@BeforeEach
|
||||
@@ -34,6 +39,11 @@ public class AddNewBookComponentTest {
|
||||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void after() {
|
||||
databaseCleanup.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Search for a new book in Google Books")
|
||||
public void whenSearchForBook_thenGetList(){
|
||||
|
||||
Reference in New Issue
Block a user