diff --git a/src/test/java/net/hostsharing/hsadminng/hs/booking/project/HsBookingProjectRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/booking/project/HsBookingProjectRepositoryIntegrationTest.java index b03b6c76..e7b58acc 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/booking/project/HsBookingProjectRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/booking/project/HsBookingProjectRepositoryIntegrationTest.java @@ -20,6 +20,8 @@ import org.springframework.orm.jpa.JpaSystemException; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.servlet.http.HttpServletRequest; +import java.sql.Timestamp; +import java.time.ZonedDateTime; import java.util.Arrays; import java.util.List; @@ -57,6 +59,50 @@ class HsBookingProjectRepositoryIntegrationTest extends ContextBasedTestWithClea @MockBean HttpServletRequest request; + @Test + public void auditJournalLogIsAvailable() { + // given + final var query = em.createNativeQuery(""" + select currentTask, targetTable, targetOp + from tx_journal_v + where targettable = 'hs_booking_project'; + """); + + // when + @SuppressWarnings("unchecked") final List customerLogEntries = query.getResultList(); + + // then + assertThat(customerLogEntries).map(Arrays::toString).contains( + "[creating booking-project test-data 1000111, hs_booking_project, INSERT]", + "[creating booking-project test-data 1000212, hs_booking_project, INSERT]", + "[creating booking-project test-data 1000313, hs_booking_project, INSERT]"); + } + + @Test + public void historizationIsAvailable() { + // given + final String nativeQuerySql = """ + select count(*) + from hs_booking_project_hv ha; + """; + + // when + historicalContext(Timestamp.from(ZonedDateTime.now().minusDays(1).toInstant())); + final var query = em.createNativeQuery(nativeQuerySql, Integer.class); + @SuppressWarnings("unchecked") final var countBefore = (Integer) query.getSingleResult(); + + // then + assertThat(countBefore).as("hs_booking_project_hv should not contain rows for a timestamp in the past").isEqualTo(0); + + // and when + historicalContext(Timestamp.from(ZonedDateTime.now().plusHours(1).toInstant())); + em.createNativeQuery(nativeQuerySql, Integer.class); + @SuppressWarnings("unchecked") final var countAfter = (Integer) query.getSingleResult(); + + // then + assertThat(countAfter).as("hs_booking_project_hv should contain rows for a timestamp in the future").isGreaterThan(1); + } + @Nested class CreateBookingProject { @@ -283,25 +329,6 @@ class HsBookingProjectRepositoryIntegrationTest extends ContextBasedTestWithClea } } - @Test - public void auditJournalLogIsAvailable() { - // given - final var query = em.createNativeQuery(""" - select currentTask, targetTable, targetOp - from tx_journal_v - where targettable = 'hs_booking_project'; - """); - - // when - @SuppressWarnings("unchecked") final List customerLogEntries = query.getResultList(); - - // then - assertThat(customerLogEntries).map(Arrays::toString).contains( - "[creating booking-project test-data 1000111, hs_booking_project, INSERT]", - "[creating booking-project test-data 1000212, hs_booking_project, INSERT]", - "[creating booking-project test-data 1000313, hs_booking_project, INSERT]"); - } - private HsBookingProjectRealEntity givenSomeTemporaryBookingProject(final int debitorNumber) { return jpaAttempt.transacted(() -> { context("superuser-alex@hostsharing.net"); diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java index ff4faebf..59704ad4 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java @@ -50,19 +50,24 @@ public abstract class ContextBasedTest { } protected void historicalContext(final Long txId) { - final var query = em.createNativeQuery(""" + // set local cannot be used with query parameters + em.createNativeQuery(""" set local hsadminng.tx_history_txid to ':txid'; - """.replace(":txid", txId.toString())); - query.executeUpdate(); + """.replace(":txid", txId.toString())).executeUpdate(); + em.createNativeQuery(""" + set local hsadminng.tx_history_timestamp to ''; + """).executeUpdate(); } protected void historicalContext(final Timestamp txTimestamp) { // set local cannot be used with query parameters - final var query = em.createNativeQuery(""" + em.createNativeQuery(""" set local hsadminng.tx_history_timestamp to ':timestamp'; - """.replace(":timestamp", txTimestamp.toString())); - query.executeUpdate(); + """.replace(":timestamp", txTimestamp.toString())).executeUpdate(); + em.createNativeQuery(""" + set local hsadminng.tx_history_txid to ''; + """).executeUpdate(); } }