test legacy-id-triggers #149

Merged
hsh-michaelhoennig merged 3 commits from feature/test-legacy-id-triggers into master 2025-01-22 10:08:22 +01:00
12 changed files with 73 additions and 33 deletions
Showing only changes of commit 2a2d0130e0 - Show all commits

View File

@ -4,13 +4,13 @@
-- Once we don't need the external remote views anymore, create revert changesets.
-- ============================================================================
--changeset michael.hoennig:hs-office-contact-MIGRATION-mapping endDelimiter:--//
--changeset michael.hoennig:hs-office-contact-MIGRATION-legacy-mapping endDelimiter:--//
-- ----------------------------------------------------------------------------
CREATE TABLE hs_office.contact_legacy_id
(
uuid uuid NOT NULL REFERENCES hs_office.contact(uuid),
contact_id integer NOT NULL
uuid uuid PRIMARY KEY NOT NULL REFERENCES hs_office.contact(uuid),
contact_id integer UNIQUE NOT NULL
);
--//

View File

@ -4,13 +4,13 @@
-- Once we don't need the external remote views anymore, create revert changesets.
-- ============================================================================
--changeset michael.hoennig:hs-office-partner-MIGRATION-mapping endDelimiter:--//
--changeset michael.hoennig:hs-office-partner-MIGRATION-legacy-mapping endDelimiter:--//
-- ----------------------------------------------------------------------------
CREATE TABLE hs_office.partner_legacy_id
(
uuid uuid NOT NULL REFERENCES hs_office.partner(uuid),
bp_id integer NOT NULL
uuid uuid PRIMARY KEY NOT NULL REFERENCES hs_office.partner(uuid),
bp_id integer UNIQUE NOT NULL
);
--//

View File

@ -4,13 +4,13 @@
-- Once we don't need the external remote views anymore, create revert changesets.
-- ============================================================================
--changeset michael.hoennig:hs-office-sepamandate-MIGRATION-mapping endDelimiter:--//
--changeset michael.hoennig:hs-office-sepamandate-MIGRATION-legacy-mapping endDelimiter:--//
-- ----------------------------------------------------------------------------
CREATE TABLE hs_office.sepamandate_legacy_id
(
uuid uuid NOT NULL REFERENCES hs_office.sepamandate(uuid),
sepa_mandate_id integer NOT NULL
uuid uuid PRIMARY KEY NOT NULL REFERENCES hs_office.sepamandate(uuid),
sepa_mandate_id integer UNIQUE NOT NULL
);
--//

View File

@ -4,13 +4,13 @@
-- Once we don't need the external remote views anymore, create revert changesets.
-- ============================================================================
--changeset michael.hoennig:hs-office-coopshares-MIGRATION-mapping endDelimiter:--//
--changeset michael.hoennig:hs-office-coopshares-MIGRATION-legacy-mapping endDelimiter:--//
-- ----------------------------------------------------------------------------
CREATE TABLE hs_office.coopsharetx_legacy_id
(
uuid uuid NOT NULL REFERENCES hs_office.coopsharetx(uuid),
member_share_id integer NOT NULL
uuid uuid PRIMARY KEY NOT NULL REFERENCES hs_office.coopsharetx(uuid),
member_share_id integer UNIQUE NOT NULL
);
--//

View File

@ -4,13 +4,13 @@
-- Once we don't need the external remote views anymore, create revert changesets.
-- ============================================================================
--changeset michael.hoennig:hs-office-coopassets-MIGRATION-mapping endDelimiter:--//
--changeset michael.hoennig:hs-office-coopassets-MIGRATION-legacy-mapping endDelimiter:--//
-- ----------------------------------------------------------------------------
CREATE TABLE hs_office.coopassettx_legacy_id
(
uuid uuid NOT NULL REFERENCES hs_office.coopassettx(uuid),
member_asset_id integer NOT NULL
uuid uuid PRIMARY KEY NOT NULL REFERENCES hs_office.coopassettx(uuid),
member_asset_id integer UNIQUE NOT NULL
);
--//

View File

@ -4,13 +4,13 @@
-- Once we don't need the external remote views anymore, create revert changesets.
-- ============================================================================
--changeset hs-hosting-asset-MIGRATION-mapping:1 endDelimiter:--//
--changeset hs-hosting-asset-MIGRATION-legacy-mapping:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
CREATE TABLE hs_hosting.asset_legacy_id
(
uuid uuid NOT NULL REFERENCES hs_hosting.asset(uuid),
legacy_id integer NOT NULL
uuid uuid PRIMARY KEY NOT NULL REFERENCES hs_hosting.asset(uuid),
legacy_id integer NOT NULL -- not unique because we sometimes create multiple asset types for the same legacy asset
);
--//

View File

@ -1,10 +1,10 @@
package net.hostsharing.hsadminng.hs.office.contact;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup;
import net.hostsharing.hsadminng.mapper.Array;
import net.hostsharing.hsadminng.rbac.grant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.role.RawRbacRoleRepository;
import net.hostsharing.hsadminng.mapper.Array;
import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup;
import net.hostsharing.hsadminng.rbac.test.JpaAttempt;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Nested;
@ -12,8 +12,8 @@ import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
@ -29,7 +29,7 @@ import static net.hostsharing.hsadminng.rbac.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@Import( { Context.class, JpaAttempt.class })
@Import({ Context.class, JpaAttempt.class })
@Tag("officeIntegrationTest")
class HsOfficeContactRbacRepositoryIntegrationTest extends ContextBasedTestWithCleanup {
@ -62,8 +62,9 @@ class HsOfficeContactRbacRepositoryIntegrationTest extends ContextBasedTestWithC
// when
final var result = attempt(em, () -> toCleanup(contactRepo.save(
hsOfficeContact("a new contact", "contact-admin@www.example.com"))));
final var result = attempt(
em, () -> toCleanup(contactRepo.save(
hsOfficeContact("a new contact", "contact-admin@www.example.com"))));
// then
result.assertSuccessful();
@ -79,14 +80,16 @@ class HsOfficeContactRbacRepositoryIntegrationTest extends ContextBasedTestWithC
final var count = contactRepo.count();
// when
final var result = attempt(em, () -> toCleanup(contactRepo.save(
hsOfficeContact("another new contact", "another-new-contact@example.com"))));
final var result = attempt(
em, () -> toCleanup(contactRepo.save(
hsOfficeContact("another new contact", "another-new-contact@example.com"))));
// then
result.assertSuccessful();
assertThat(result.returnedValue()).isNotNull().extracting(HsOfficeContactRbacEntity::getUuid).isNotNull();
assertThatContactIsPersisted(result.returnedValue());
assertThat(contactRepo.count()).isEqualTo(count + 1);
assertHasLegacyId(result.returnedValue(), "contact_id");
}
@Test
@ -97,8 +100,9 @@ class HsOfficeContactRbacRepositoryIntegrationTest extends ContextBasedTestWithC
final var initialGrantNames = distinctGrantDisplaysOf(rawGrantRepo.findAll());
// when
attempt(em, () -> toCleanup(contactRepo.save(
hsOfficeContact("another new contact", "another-new-contact@example.com")))
attempt(
em, () -> toCleanup(contactRepo.save(
hsOfficeContact("another new contact", "another-new-contact@example.com")))
).assumeSuccessful();
// then
@ -300,10 +304,11 @@ class HsOfficeContactRbacRepositoryIntegrationTest extends ContextBasedTestWithC
private HsOfficeContactRbacEntity givenSomeTemporaryContact(final String createdByUser) {
final var random = RandomStringUtils.randomAlphabetic(12);
return givenSomeTemporaryContact(createdByUser, () ->
hsOfficeContact(
"some temporary contact #" + random,
"some-temporary-contact" + random + "@example.com"));
return givenSomeTemporaryContact(
createdByUser, () ->
hsOfficeContact(
"some temporary contact #" + random,
"some-temporary-contact" + random + "@example.com"));
}
void exactlyTheseContactsAreReturned(final List<HsOfficeContactRbacEntity> actualResult, final String... contactCaptions) {

View File

@ -83,6 +83,7 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase
assertThat(result.returnedValue()).isNotNull().extracting(HsOfficeCoopAssetsTransactionEntity::getUuid).isNotNull();
assertThatCoopAssetsTransactionIsPersisted(result.returnedValue());
assertThat(coopAssetsTransactionRepo.count()).isEqualTo(count + 1);
assertHasLegacyId(result.returnedValue(), "member_asset_id");
}
@Test

View File

@ -82,6 +82,7 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
assertThat(result.returnedValue()).isNotNull().extracting(HsOfficeCoopSharesTransactionEntity::getUuid).isNotNull();
assertThatCoopSharesTransactionIsPersisted(result.returnedValue());
assertThat(coopSharesTransactionRepo.count()).isEqualTo(count + 1);
assertHasLegacyId(result.returnedValue(), "member_share_id");
}
@Test

View File

@ -98,6 +98,7 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
assertThat(result.returnedValue()).isNotNull().extracting(HsOfficePartnerRbacEntity::getUuid).isNotNull();
assertThatPartnerIsPersisted(result.returnedValue());
assertThat(partnerRepo.count()).isEqualTo(count + 1);
assertHasLegacyId(result.returnedValue(), "bp_id");
}
@Test

View File

@ -89,6 +89,7 @@ class HsOfficeSepaMandateRepositoryIntegrationTest extends ContextBasedTestWithC
assertThat(result.returnedValue()).isNotNull().extracting(HsOfficeSepaMandateEntity::getUuid).isNotNull();
assertThatSepaMandateIsPersisted(result.returnedValue());
assertThat(sepaMandateRepo.count()).isEqualTo(count + 1);
assertHasLegacyId(result.returnedValue(), "sepa_mandate_id");
}
@Test

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.rbac.context;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.persistence.BaseEntity;
import net.hostsharing.hsadminng.rbac.grant.RbacGrantsDiagramService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
@ -9,8 +10,11 @@ import org.springframework.context.annotation.Import;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Table;
import java.sql.Timestamp;
import static org.assertj.core.api.Assertions.assertThat;
@Import(RbacGrantsDiagramService.class)
public abstract class ContextBasedTest {
@ -59,7 +63,6 @@ public abstract class ContextBasedTest {
""").executeUpdate();
}
protected void historicalContext(final Timestamp txTimestamp) {
// set local cannot be used with query parameters
em.createNativeQuery("""
@ -70,4 +73,32 @@ public abstract class ContextBasedTest {
""").executeUpdate();
}
protected void assertHasLegacyId(final BaseEntity<?> entity, final String legacyIdColumnName) {
final var table = entity.getClass().getAnnotation(Table.class);
final var legacyIdTable = table.schema() + "." + table.name().replace("_rv", "") + "_legacy_id";
final var legacyId = getSingleLegacyIdByUuidOrFail(entity, legacyIdTable, legacyIdColumnName);
assertThatLegacyIdExistsExactlyOnce(legacyIdColumnName, legacyIdTable, legacyId);
}
private void assertThatLegacyIdExistsExactlyOnce(final String legacyIdColumnName, final String legacyIdTable, final long legacyId) {
em.createNativeQuery("SELECT *" +
" FROM " + legacyIdTable +
" WHERE " + legacyIdColumnName + " = '" + legacyId + "'")
.getSingleResult(); // fails if there is not exactly one result
}
private long getSingleLegacyIdByUuidOrFail(final BaseEntity<?> entity,
final String legacyIdTable,
final String legacyIdColumnName) {
final var uuid = entity.getUuid();
@SuppressWarnings("unchecked") // the auto-formatter should keep this linebreak
final var legacyId = (Long) em.createNativeQuery("SELECT " + legacyIdColumnName +
" FROM " + legacyIdTable +
" WHERE uuid = '" + uuid + "'", Long.class)
.getSingleResult();
assertThat(legacyId).isNotNull();
return legacyId;
}
}