integration test for hs_hosting_asset historization

This commit is contained in:
Michael Hoennig 2024-08-29 09:51:47 +02:00
parent 07accbb47d
commit 7f16a5c39f
2 changed files with 64 additions and 19 deletions

View File

@ -23,6 +23,8 @@ import org.springframework.orm.jpa.JpaSystemException;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.ZonedDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -70,6 +72,50 @@ class HsHostingAssetRepositoryIntegrationTest extends ContextBasedTestWithCleanu
@MockBean @MockBean
HttpServletRequest request; 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<Object[]> 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 @Nested
class CreateAsset { 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<Object[]> 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) { private HsHostingAssetRealEntity givenSomeTemporaryAsset(final String projectCaption, final String identifier) {
return jpaAttempt.transacted(() -> { return jpaAttempt.transacted(() -> {
context("superuser-alex@hostsharing.net"); // needed to determine creator context("superuser-alex@hostsharing.net"); // needed to determine creator

View File

@ -9,6 +9,7 @@ import org.springframework.context.annotation.Import;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import java.sql.Timestamp;
@Import(RbacGrantsDiagramService.class) @Import(RbacGrantsDiagramService.class)
public abstract class ContextBasedTest { public abstract class ContextBasedTest {
@ -47,4 +48,21 @@ public abstract class ContextBasedTest {
protected void context(final String currentUser) { protected void context(final String currentUser) {
context(currentUser, null); 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();
}
} }