diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java index a3476d8..4bc5d59 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java @@ -1,76 +1,116 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.util.List; -import org.hibernate.Transaction; +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests.TestConfiguration; + +@SpringBootTest +@ContextConfiguration(classes = TestConfiguration.class) +public class PersonDaoTest { -public class PersonDaoTest extends AbstractDatabaseTest { + @Autowired + private PersonDao personDao; + @Autowired + private EntityManager entityManager; - private PersonDaoImpl personDao; private Person person; + private TypedQuery q; + + private final String truncateString = createTruncateString(); @BeforeEach public void setUp() throws Exception { - personDao = new PersonDaoImpl(); - personDao.em = session; person = new Person(); person.setPrename("prename"); person.setSurname("surname"); -// Date time = new GregorianCalendar(2018, Calendar.NOVEMBER, 8).getTime(); person.setBirth(LocalDate.of(2018, 11, 8)); person.setPassword("password"); + q = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class); + assertTrue(q.getResultList().isEmpty()); + } + + @AfterEach + public void clearDatabase() throws IOException { + Query truncateQuery = entityManager.createNativeQuery(truncateString); + transactional(() -> truncateQuery.executeUpdate()); + } + + private String createTruncateString() { + try { + InputStream resource = getClass().getResourceAsStream("/truncateTables.sql"); + return IOUtils.toString(resource, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new RuntimeException(e); + } } @Test public void testSave() { - storePerson(); - List stored = session.createNamedQuery(Person.QUERY_FINDALL, Person.class).list(); + transactional(() -> personDao.save(person)); + List stored = q.getResultList(); assertEquals(1, stored.size()); assertEquals(person, stored.get(0)); assertEquals(person, personDao.get(person.getId())); } - public void storePerson() { - Transaction tx = session.beginTransaction(); - personDao.save(person); - tx.commit(); - } - @Test public void testUpdate() { - storePerson(); + + transactional(() -> personDao.save(person)); person.setSurname("surname2"); person.setPrename("prename2"); - Transaction tx = session.beginTransaction(); - personDao.update(person); - tx.commit(); - List stored = session.createNamedQuery(Person.QUERY_FINDALL, Person.class).list(); + transactional(() -> personDao.update(person)); + + List stored = q.getResultList(); assertEquals(1, stored.size()); assertEquals(person, stored.get(0)); } + public void transactional(Runnable r) { + EntityTransaction tx = entityManager.getTransaction(); + tx.begin(); + r.run(); + tx.commit(); + } + @Disabled @Test public void testListAll() { - storePerson(); - session.detach(person); + transactional(() -> personDao.save(person)); + entityManager.detach(person); + person = new Person(); + person.setId(0); person.setSurname("surname2"); person.setPrename("prename2"); person.setBirth(LocalDate.of(2018, 11, 8)); person.setPassword("password"); - storePerson(); + transactional(() -> personDao.save(person)); List stored = personDao.listAll(); assertEquals(2, stored.size()); diff --git a/src/test/resources/truncateTables.sql b/src/test/resources/truncateTables.sql index c3463cb..10d0fc4 100644 --- a/src/test/resources/truncateTables.sql +++ b/src/test/resources/truncateTables.sql @@ -1,12 +1,12 @@ SET FOREIGN_KEY_CHECKS=0; -TRUNCATE TABLE clubevent_addon; -TRUNCATE TABLE event_has_altersgruppe; +-- TRUNCATE TABLE clubevent_addon; +-- TRUNCATE TABLE event_has_altersgruppe; TRUNCATE TABLE altersgruppe; TRUNCATE TABLE pflichten; TRUNCATE TABLE clubevent_has_person; TRUNCATE TABLE ClubEvent; -TRUNCATE TABLE persongroup; +-- TRUNCATE TABLE persongroup; TRUNCATE TABLE startpass_startrechte; TRUNCATE TABLE deleted_entries; TRUNCATE TABLE clubevent;