integration test for hs_booking_project historization

This commit is contained in:
Michael Hoennig 2024-08-29 10:32:27 +02:00
parent 7bb5e14d01
commit 19033e15bc
2 changed files with 57 additions and 25 deletions

View File

@ -20,6 +20,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;
@ -57,6 +59,50 @@ class HsBookingProjectRepositoryIntegrationTest extends ContextBasedTestWithClea
@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_booking_project';
""");
// when
@SuppressWarnings("unchecked") final List<Object[]> 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 @Nested
class CreateBookingProject { 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<Object[]> 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) { private HsBookingProjectRealEntity givenSomeTemporaryBookingProject(final int debitorNumber) {
return jpaAttempt.transacted(() -> { return jpaAttempt.transacted(() -> {
context("superuser-alex@hostsharing.net"); context("superuser-alex@hostsharing.net");

View File

@ -50,19 +50,24 @@ public abstract class ContextBasedTest {
} }
protected void historicalContext(final Long txId) { 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'; set local hsadminng.tx_history_txid to ':txid';
""".replace(":txid", txId.toString())); """.replace(":txid", txId.toString())).executeUpdate();
query.executeUpdate(); em.createNativeQuery("""
set local hsadminng.tx_history_timestamp to '';
""").executeUpdate();
} }
protected void historicalContext(final Timestamp txTimestamp) { protected void historicalContext(final Timestamp txTimestamp) {
// set local cannot be used with query parameters // set local cannot be used with query parameters
final var query = em.createNativeQuery(""" em.createNativeQuery("""
set local hsadminng.tx_history_timestamp to ':timestamp'; set local hsadminng.tx_history_timestamp to ':timestamp';
""".replace(":timestamp", txTimestamp.toString())); """.replace(":timestamp", txTimestamp.toString())).executeUpdate();
query.executeUpdate(); em.createNativeQuery("""
set local hsadminng.tx_history_txid to '';
""").executeUpdate();
} }
} }