From 7f16a5c39fa72be9e3721c897c706872afa83ee5 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 29 Aug 2024 09:51:47 +0200 Subject: [PATCH] integration test for hs_hosting_asset historization --- ...HostingAssetRepositoryIntegrationTest.java | 65 +++++++++++++------ .../rbac/context/ContextBasedTest.java | 18 +++++ 2 files changed, 64 insertions(+), 19 deletions(-) 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 469fbdf1..6a61dacf 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 @@ -23,6 +23,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; import java.util.Map; @@ -70,6 +72,50 @@ class HsHostingAssetRepositoryIntegrationTest extends ContextBasedTestWithCleanu @MockBean HttpServletRequest request; + @Test + public void auditJournalLogIsAvailable() { + // given + final var query = em.createNativeQuery(""" + select currentTask, targetTable, targetOp + from tx_journal_v + where targettable = 'hs_hosting_asset'; + """); + + // when + @SuppressWarnings("unchecked") final List customerLogEntries = query.getResultList(); + + // then + assertThat(customerLogEntries).map(Arrays::toString).contains( + "[creating hosting-asset test-data D-1000111 default project, hs_hosting_asset, INSERT]", + "[creating hosting-asset test-data D-1000212 default project, hs_hosting_asset, INSERT]", + "[creating hosting-asset test-data D-1000313 default project, hs_hosting_asset, INSERT]"); + } + + @Test + public void historizationIsAvailable() { + // given + final String nativeQuerySql = """ + select count(*) + from hs_hosting_asset_hv ha; + """; + + // when + historicalContext(Timestamp.from(ZonedDateTime.now().minusHours(1).toInstant())); + final var query = em.createNativeQuery(nativeQuerySql, Integer.class); + @SuppressWarnings("unchecked") final var countBefore = (Integer) query.getSingleResult(); + + // then + assertThat(countBefore).as("hs_hosting_asset_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_hosting_asset_hv should contain rows for a timestamp in the future").isGreaterThan(1); + } + @Nested class CreateAsset { @@ -391,25 +437,6 @@ class HsHostingAssetRepositoryIntegrationTest extends ContextBasedTestWithCleanu } } - @Test - public void auditJournalLogIsAvailable() { - // given - final var query = em.createNativeQuery(""" - select currentTask, targetTable, targetOp - from tx_journal_v - where targettable = 'hs_hosting_asset'; - """); - - // when - @SuppressWarnings("unchecked") final List customerLogEntries = query.getResultList(); - - // then - assertThat(customerLogEntries).map(Arrays::toString).contains( - "[creating hosting-asset test-data D-1000111 default project, hs_hosting_asset, INSERT]", - "[creating hosting-asset test-data D-1000212 default project, hs_hosting_asset, INSERT]", - "[creating hosting-asset test-data D-1000313 default project, hs_hosting_asset, INSERT]"); - } - private HsHostingAssetRealEntity givenSomeTemporaryAsset(final String projectCaption, final String identifier) { return jpaAttempt.transacted(() -> { context("superuser-alex@hostsharing.net"); // needed to determine creator 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 2e14c267..ff4faebf 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java @@ -9,6 +9,7 @@ import org.springframework.context.annotation.Import; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import java.sql.Timestamp; @Import(RbacGrantsDiagramService.class) public abstract class ContextBasedTest { @@ -47,4 +48,21 @@ public abstract class ContextBasedTest { protected void context(final String currentUser) { context(currentUser, null); } + + protected void historicalContext(final Long txId) { + final var query = em.createNativeQuery(""" + set local hsadminng.tx_history_txid to ':txid'; + """.replace(":txid", txId.toString())); + query.executeUpdate(); + } + + + protected void historicalContext(final Timestamp txTimestamp) { + // set local cannot be used with query parameters + final var query = em.createNativeQuery(""" + set local hsadminng.tx_history_timestamp to ':timestamp'; + """.replace(":timestamp", txTimestamp.toString())); + query.executeUpdate(); + } + }