From 7bb5e14d0153b2410246fc3ee432b201f07e1c8e Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 29 Aug 2024 10:08:11 +0200 Subject: [PATCH] integration test for hs_booking_item historization --- ...sBookingItemRepositoryIntegrationTest.java | 65 +++++++++++++------ ...HostingAssetRepositoryIntegrationTest.java | 2 +- .../hsadminng/hs/migration/CsvDataImport.java | 2 + 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemRepositoryIntegrationTest.java index 0a40aabf..cef07860 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemRepositoryIntegrationTest.java @@ -20,7 +20,9 @@ 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.LocalDate; +import java.time.ZonedDateTime; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -62,6 +64,50 @@ class HsBookingItemRepositoryIntegrationTest extends ContextBasedTestWithCleanup @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_item'; + """); + + // when + @SuppressWarnings("unchecked") final List customerLogEntries = query.getResultList(); + + // then + assertThat(customerLogEntries).map(Arrays::toString).contains( + "[creating booking-item test-data 1000111, hs_booking_item, INSERT]", + "[creating booking-item test-data 1000212, hs_booking_item, INSERT]", + "[creating booking-item test-data 1000313, hs_booking_item, INSERT]"); + } + + @Test + public void historizationIsAvailable() { + // given + final String nativeQuerySql = """ + select count(*) + from hs_booking_item_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_item 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_item should contain rows for a timestamp in the future").isGreaterThan(1); + } + @Nested class CreateBookingItem { @@ -304,25 +350,6 @@ class HsBookingItemRepositoryIntegrationTest extends ContextBasedTestWithCleanup } } - @Test - public void auditJournalLogIsAvailable() { - // given - final var query = em.createNativeQuery(""" - select currentTask, targetTable, targetOp - from tx_journal_v - where targettable = 'hs_booking_item'; - """); - - // when - @SuppressWarnings("unchecked") final List customerLogEntries = query.getResultList(); - - // then - assertThat(customerLogEntries).map(Arrays::toString).contains( - "[creating booking-item test-data 1000111, hs_booking_item, INSERT]", - "[creating booking-item test-data 1000212, hs_booking_item, INSERT]", - "[creating booking-item test-data 1000313, hs_booking_item, INSERT]"); - } - private HsBookingItem givenSomeTemporaryBookingItem(final String projectCaption) { return jpaAttempt.transacted(() -> { context("superuser-alex@hostsharing.net"); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java index 6a61dacf..aea6917b 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java @@ -100,7 +100,7 @@ class HsHostingAssetRepositoryIntegrationTest extends ContextBasedTestWithCleanu """; // when - historicalContext(Timestamp.from(ZonedDateTime.now().minusHours(1).toInstant())); + historicalContext(Timestamp.from(ZonedDateTime.now().minusDays(1).toInstant())); final var query = em.createNativeQuery(nativeQuerySql, Integer.class); @SuppressWarnings("unchecked") final var countBefore = (Integer) query.getSingleResult(); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java b/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java index 4d4253c0..d10f3577 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java @@ -251,7 +251,9 @@ public class CsvDataImport extends ContextBasedTest { em.createNativeQuery("delete from hs_hosting_asset where true").executeUpdate(); em.createNativeQuery("delete from hs_hosting_asset_ex where true").executeUpdate(); em.createNativeQuery("delete from hs_booking_item where true").executeUpdate(); + em.createNativeQuery("delete from hs_booking_item_ex where true").executeUpdate(); em.createNativeQuery("delete from hs_booking_project where true").executeUpdate(); + em.createNativeQuery("delete from hs_booking_project_ex where true").executeUpdate(); em.createNativeQuery("delete from hs_office_coopassetstransaction where true").executeUpdate(); em.createNativeQuery("delete from hs_office_coopassetstransaction_legacy_id where true").executeUpdate(); em.createNativeQuery("delete from hs_office_coopsharestransaction where true").executeUpdate();