delete test data before import

This commit is contained in:
Michael Hoennig 2024-01-11 16:49:10 +01:00
parent 1299b4e20a
commit e9a8699aa1

View File

@ -62,6 +62,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ImportOfficeTables extends ContextBasedTest { public class ImportOfficeTables extends ContextBasedTest {
// TODO: use real rbacSuperuser for actual import
private static final String rbacSuperuser = "superuser-alex@hostsharing.net";
private static NavigableMap<Integer, HsOfficeContactEntity> contacts = new TreeMap<>(); private static NavigableMap<Integer, HsOfficeContactEntity> contacts = new TreeMap<>();
private static NavigableMap<Integer, HsOfficePersonEntity> persons = new TreeMap<>(); private static NavigableMap<Integer, HsOfficePersonEntity> persons = new TreeMap<>();
private static NavigableMap<Integer, HsOfficePartnerEntity> partners = new TreeMap<>(); private static NavigableMap<Integer, HsOfficePartnerEntity> partners = new TreeMap<>();
@ -244,32 +247,131 @@ public class ImportOfficeTables extends ContextBasedTest {
@Commit @Commit
void persistEntities() { void persistEntities() {
context("superuser-alex@hostsharing.net"); // TODO: use real user for actual import deleteTestDataFromHsOfficeTables();
resetFromHsOfficeSequences();
deleteFromTestTables();
deleteFromRbacTables();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
contacts.forEach((id, contact) -> em.persist(contact)); contacts.forEach((id, contact) -> em.persist(contact));
updateLegacyIds(contacts, "hs_office_contact_legacy_id", "contact_id"); updateLegacyIds(contacts, "hs_office_contact_legacy_id", "contact_id");
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
persons.forEach((id, person) -> em.persist(person)); persons.forEach((id, person) -> em.persist(person));
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
partners.forEach((id, partner) -> em.persist(partner)); partners.forEach((id, partner) -> em.persist(partner));
updateLegacyIds(partners, "hs_office_partner_legacy_id", "bp_id"); updateLegacyIds(partners, "hs_office_partner_legacy_id", "bp_id");
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
debitors.forEach((id, debitor) -> em.persist(debitor)); debitors.forEach((id, debitor) -> em.persist(debitor));
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
memberships.forEach((id, membership) -> em.persist(membership)); memberships.forEach((id, membership) -> em.persist(membership));
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
bankAccounts.forEach((id, account) -> em.persist(account)); bankAccounts.forEach((id, account) -> em.persist(account));
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
sepaMandates.forEach((id, mandate) -> em.persist(mandate)); sepaMandates.forEach((id, mandate) -> em.persist(mandate));
updateLegacyIds(sepaMandates, "hs_office_sepamandate_legacy_id", "sepa_mandate_id"); updateLegacyIds(sepaMandates, "hs_office_sepamandate_legacy_id", "sepa_mandate_id");
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
coopShares.forEach((id, shareTransaction) -> em.persist(shareTransaction)); coopShares.forEach((id, shareTransaction) -> em.persist(shareTransaction));
updateLegacyIds(coopShares, "hs_office_coopsharestransaction_legacy_id", "member_share_id"); updateLegacyIds(coopShares, "hs_office_coopsharestransaction_legacy_id", "member_share_id");
}).assertSuccessful();
jpaAttempt.transacted(() -> {
context(rbacSuperuser);
coopAssets.forEach((id, assetTransaction) -> em.persist(assetTransaction)); coopAssets.forEach((id, assetTransaction) -> em.persist(assetTransaction));
updateLegacyIds(coopShares, "hs_office_coopassetstransaction_legacy_id", "member_asset_id"); updateLegacyIds(coopShares, "hs_office_coopassetstransaction_legacy_id", "member_asset_id");
}).assertSuccessful();
} }
private <E extends HasUuid> void updateLegacyIds(Map<Integer, E> 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 <E extends HasUuid> void updateLegacyIds(
Map<Integer, E> entities,
final String legacyIdTable,
final String legacyIdColumn) {
entities.forEach((id, entity) -> em.createNativeQuery(""" entities.forEach((id, entity) -> em.createNativeQuery("""
UPDATE ${legacyIdTable} UPDATE ${legacyIdTable}
SET ${legacyIdColumn} = :legacyId SET ${legacyIdColumn} = :legacyId
@ -337,7 +439,9 @@ public class ImportOfficeTables extends ContextBasedTest {
final var membership = HsOfficeMembershipEntity.builder() final var membership = HsOfficeMembershipEntity.builder()
.partner(partner) .partner(partner)
.memberNumber(rec.getInteger("member_id")) .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")) .membershipFeeBillable(rec.isEmpty("member_role"))
.reasonForTermination( .reasonForTermination(
isBlank(rec.getString("member_until")) isBlank(rec.getString("member_until"))