diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeTables.java b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeTables.java index 6bf9ca94..50102697 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeTables.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeTables.java @@ -62,6 +62,9 @@ import static org.assertj.core.api.Assertions.assertThat; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class ImportOfficeTables extends ContextBasedTest { + // TODO: use real rbacSuperuser for actual import + private static final String rbacSuperuser = "superuser-alex@hostsharing.net"; + private static NavigableMap contacts = new TreeMap<>(); private static NavigableMap persons = new TreeMap<>(); private static NavigableMap partners = new TreeMap<>(); @@ -244,37 +247,136 @@ public class ImportOfficeTables extends ContextBasedTest { @Commit void persistEntities() { - context("superuser-alex@hostsharing.net"); // TODO: use real user for actual import + deleteTestDataFromHsOfficeTables(); + resetFromHsOfficeSequences(); + deleteFromTestTables(); + deleteFromRbacTables(); - contacts.forEach((id, contact) -> em.persist(contact)); - updateLegacyIds(contacts, "hs_office_contact_legacy_id", "contact_id"); + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + contacts.forEach((id, contact) -> em.persist(contact)); + updateLegacyIds(contacts, "hs_office_contact_legacy_id", "contact_id"); + }).assertSuccessful(); - persons.forEach((id, person) -> em.persist(person)); + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + persons.forEach((id, person) -> em.persist(person)); + }).assertSuccessful(); - partners.forEach((id, partner) -> em.persist(partner)); - updateLegacyIds(partners, "hs_office_partner_legacy_id", "bp_id"); + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + partners.forEach((id, partner) -> em.persist(partner)); + updateLegacyIds(partners, "hs_office_partner_legacy_id", "bp_id"); + }).assertSuccessful(); - debitors.forEach((id, debitor) -> em.persist(debitor)); - memberships.forEach((id, membership) -> em.persist(membership)); - bankAccounts.forEach((id, account) -> em.persist(account)); + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + debitors.forEach((id, debitor) -> em.persist(debitor)); + }).assertSuccessful(); - sepaMandates.forEach((id, mandate) -> em.persist(mandate)); - updateLegacyIds(sepaMandates, "hs_office_sepamandate_legacy_id", "sepa_mandate_id"); + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + memberships.forEach((id, membership) -> em.persist(membership)); - coopShares.forEach((id, shareTransaction) -> em.persist(shareTransaction)); - updateLegacyIds(coopShares, "hs_office_coopsharestransaction_legacy_id", "member_share_id"); + }).assertSuccessful(); - coopAssets.forEach((id, assetTransaction) -> em.persist(assetTransaction)); - updateLegacyIds(coopShares, "hs_office_coopassetstransaction_legacy_id", "member_asset_id"); + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + bankAccounts.forEach((id, account) -> em.persist(account)); + + }).assertSuccessful(); + + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + sepaMandates.forEach((id, mandate) -> em.persist(mandate)); + updateLegacyIds(sepaMandates, "hs_office_sepamandate_legacy_id", "sepa_mandate_id"); + + }).assertSuccessful(); + + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + coopShares.forEach((id, shareTransaction) -> em.persist(shareTransaction)); + updateLegacyIds(coopShares, "hs_office_coopsharestransaction_legacy_id", "member_share_id"); + + }).assertSuccessful(); + + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + coopAssets.forEach((id, assetTransaction) -> em.persist(assetTransaction)); + updateLegacyIds(coopShares, "hs_office_coopassetstransaction_legacy_id", "member_asset_id"); + }).assertSuccessful(); } - private void updateLegacyIds(Map entities, final String legacyIdTable, final String legacyIdColumn) { + private void deleteTestDataFromHsOfficeTables() { + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + em.createNativeQuery("DELETE FROM hs_office_relationship 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(); + em.createNativeQuery("DELETE FROM hs_office_coopsharestransaction_legacy_id WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_membership WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_sepamandate WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_sepamandate_legacy_id WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_debitor WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_bankaccount WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_partner WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_partner_details WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_contact WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM hs_office_person WHERE true").executeUpdate(); + }).assertSuccessful(); + } + + private void resetFromHsOfficeSequences() { + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + em.createNativeQuery("ALTER SEQUENCE hs_office_contact_legacy_id_seq RESTART WITH 1000000000;").executeUpdate(); + em.createNativeQuery("ALTER SEQUENCE hs_office_coopassetstransaction_legacy_id_seq RESTART WITH 1000000000;").executeUpdate(); + em.createNativeQuery("ALTER SEQUENCE public.hs_office_coopsharestransaction_legacy_id_seq RESTART WITH 1000000000;").executeUpdate(); + em.createNativeQuery("ALTER SEQUENCE public.hs_office_partner_legacy_id_seq RESTART WITH 1000000000;").executeUpdate(); + em.createNativeQuery("ALTER SEQUENCE public.hs_office_sepamandate_legacy_id_seq RESTART WITH 1000000000;").executeUpdate(); + }); + } + + private void deleteFromTestTables() { + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + em.createNativeQuery("DELETE FROM test_domain WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM test_package WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM test_customer WHERE true").executeUpdate(); + }).assertSuccessful(); + } + + private void deleteFromRbacTables() { + jpaAttempt.transacted(() -> { + context(rbacSuperuser); + final var usersNotToDelete = em.createNativeQuery("SELECT name FROM rbacuser WHERE name like 'superuser-%'").getResultList(); + final var usersToDelete = em.createNativeQuery("SELECT name FROM rbacuser WHERE name not like 'superuser-%'").getResultList(); + System.getenv(); + }); + + jpaAttempt.transacted(() -> { + context(rbacSuperuser); +// em.createNativeQuery("DELETE FROM rbacobject WHERE objecttable like 'hs_%'").executeUpdate(); +// em.createNativeQuery("DELETE FROM rbacgrants WHERE true").executeUpdate(); +// em.createNativeQuery("DELETE FROM rbacpermission WHERE true").executeUpdate(); +// em.createNativeQuery("DELETE FROM rbacreference WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM rbacuser_rv WHERE name not like 'superuser-%'").executeUpdate(); + em.createNativeQuery("DELETE FROM tx_journal WHERE true").executeUpdate(); + em.createNativeQuery("DELETE FROM tx_context WHERE true").executeUpdate(); + }).assertSuccessful(); + } + + private void updateLegacyIds( + Map entities, + final String legacyIdTable, + final String legacyIdColumn) { entities.forEach((id, entity) -> em.createNativeQuery(""" - UPDATE ${legacyIdTable} - SET ${legacyIdColumn} = :legacyId - WHERE uuid = :uuid - """ + UPDATE ${legacyIdTable} + SET ${legacyIdColumn} = :legacyId + WHERE uuid = :uuid + """ .replace("${legacyIdTable}", legacyIdTable) .replace("${legacyIdColumn}", legacyIdColumn)) .setParameter("legacyId", id) @@ -337,7 +439,9 @@ public class ImportOfficeTables extends ContextBasedTest { final var membership = HsOfficeMembershipEntity.builder() .partner(partner) .memberNumber(rec.getInteger("member_id")) - .validity(toPostgresDateRange(rec.getLocalDate("member_since"), rec.getLocalDate("member_until"))) + .validity(toPostgresDateRange( + rec.getLocalDate("member_since"), + rec.getLocalDate("member_until"))) .membershipFeeBillable(rec.isEmpty("member_role")) .reasonForTermination( isBlank(rec.getString("member_until")) @@ -366,10 +470,10 @@ public class ImportOfficeTables extends ContextBasedTest { .transactionType( "SUBSCRIPTION".equals(rec.getString("action")) ? HsOfficeCoopSharesTransactionType.SUBSCRIPTION - : "UNSUBSCRIPTION".equals(rec.getString("action")) + : "UNSUBSCRIPTION".equals(rec.getString("action")) ? HsOfficeCoopSharesTransactionType.CANCELLATION : HsOfficeCoopSharesTransactionType.ADJUSTMENT - ) + ) .shareCount(rec.getInteger("quantity")) .reference(rec.getString("comment")) .build();