|
| 1 | +package info.unterrainer.commons.rdbutils; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.time.LocalDateTime; |
| 6 | +import java.time.temporal.ChronoUnit; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import info.unterrainer.commons.jreutils.DateUtils; |
| 13 | +import info.unterrainer.commons.rdbutils.converters.LocalDateTimeConverter; |
| 14 | +import info.unterrainer.commons.rdbutils.exceptions.RdbUtilException; |
| 15 | +import info.unterrainer.commons.rdbutils.jpas.TestJpa; |
| 16 | +import jakarta.persistence.EntityManagerFactory; |
| 17 | +import jakarta.persistence.Query; |
| 18 | +import jakarta.persistence.TypedQuery; |
| 19 | +import lombok.extern.slf4j.Slf4j; |
| 20 | + |
| 21 | +@Slf4j |
| 22 | +public class LocalDateTimeConverterIntegrationTests { |
| 23 | + |
| 24 | + LocalDateTimeConverter converter = new LocalDateTimeConverter(); |
| 25 | + |
| 26 | + public static EntityManagerFactory emf; |
| 27 | + |
| 28 | + @BeforeAll |
| 29 | + public static void setupClass() { |
| 30 | + try { |
| 31 | + emf = RdbUtils.createAutoclosingEntityManagerFactory(ManualTests.class, "test"); |
| 32 | + } catch (RdbUtilException e) { |
| 33 | + log.error("Error getting EntityManagerFactory", e); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void beforeMethod() { |
| 39 | + deleteTestTable(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void persistingAndReadingEntityWorks() { |
| 44 | + LocalDateTime nowUtc = DateUtils.nowUtc(); |
| 45 | + TestJpa testJpa = TestJpa.builder().message("test").createdOn(nowUtc).editedOn(nowUtc).build(); |
| 46 | + |
| 47 | + persistTestEntity(testJpa); |
| 48 | + TestJpa jpa = selectFirstTestEntity(); |
| 49 | + assertThat(jpa.getEditedOn()).isEqualTo(testJpa.getEditedOn().truncatedTo(ChronoUnit.MICROS)); |
| 50 | + } |
| 51 | + |
| 52 | + private int deleteTestTable() { |
| 53 | + return Transactions.withNewTransactionReturning(emf, em -> { |
| 54 | + Query q = em.createQuery(String.format("DELETE FROM %s AS o", TestJpa.class.getSimpleName())); |
| 55 | + int entitiesDeleted = q.executeUpdate(); |
| 56 | + log.info("[{}] entities deleted", entitiesDeleted); |
| 57 | + return entitiesDeleted; |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + private void persistTestEntity(final TestJpa jpa) { |
| 62 | + Transactions.withNewTransaction(emf, em -> { |
| 63 | + em.persist(jpa); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + private TestJpa selectFirstTestEntity() { |
| 68 | + return Transactions.withNewTransactionReturning(emf, em -> { |
| 69 | + TypedQuery<TestJpa> q = em |
| 70 | + .createQuery(String.format("SELECT o FROM %s AS o", TestJpa.class.getSimpleName()), TestJpa.class); |
| 71 | + q.setMaxResults(1); |
| 72 | + return q.getSingleResult(); |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
0 commit comments