From df025503d9a92820ef8f9308f1a471de46510c25 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 29 Jan 2024 10:03:51 +0100 Subject: [PATCH] add rbacrole test in before+after to all Integration+Acceptance-tests --- .../rbac/rbacrole/RbacRoleRepository.java | 9 +- .../hsadminng/context/ContextBasedTest.java | 2 +- ...ceBankAccountControllerAcceptanceTest.java | 3 +- ...eBankAccountRepositoryIntegrationTest.java | 4 +- ...OfficeContactControllerAcceptanceTest.java | 3 +- ...fficeContactRepositoryIntegrationTest.java | 4 +- ...tsTransactionControllerAcceptanceTest.java | 3 +- ...sTransactionRepositoryIntegrationTest.java | 4 +- ...esTransactionControllerAcceptanceTest.java | 3 +- ...sTransactionRepositoryIntegrationTest.java | 4 +- ...OfficeDebitorControllerAcceptanceTest.java | 3 +- ...iceMembershipControllerAcceptanceTest.java | 3 +- ...ceMembershipRepositoryIntegrationTest.java | 4 +- ...OfficePartnerControllerAcceptanceTest.java | 25 +---- ...fficePartnerRepositoryIntegrationTest.java | 4 +- ...sOfficePersonControllerAcceptanceTest.java | 5 +- ...OfficePersonRepositoryIntegrationTest.java | 4 +- ...eRelationshipControllerAcceptanceTest.java | 29 +----- ...ceSepaMandateControllerAcceptanceTest.java | 9 +- ...eSepaMandateRepositoryIntegrationTest.java | 4 +- .../test/ContextBasedTestWithCleanup.java | 91 +++++++++++++++++++ 21 files changed, 137 insertions(+), 83 deletions(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java diff --git a/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleRepository.java b/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleRepository.java index 5d13f4db..747bb119 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleRepository.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleRepository.java @@ -8,9 +8,12 @@ import java.util.UUID; public interface RbacRoleRepository extends Repository { /** - * Returns all instances of the type. - * - * @return all entities + * @return the number of persistent RbacRoleEntity instances, mostly for testing purposes. + */ + long count(); + + /** + * @return all persistent RbacRoleEntity instances, mostly for testing purposes. */ List findAll(); diff --git a/src/test/java/net/hostsharing/hsadminng/context/ContextBasedTest.java b/src/test/java/net/hostsharing/hsadminng/context/ContextBasedTest.java index 828097e9..1069fa5f 100644 --- a/src/test/java/net/hostsharing/hsadminng/context/ContextBasedTest.java +++ b/src/test/java/net/hostsharing/hsadminng/context/ContextBasedTest.java @@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired; public abstract class ContextBasedTest { @Autowired - Context context; + protected Context context; TestInfo test; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java index a3946507..edb45fc9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java @@ -4,6 +4,7 @@ import io.restassured.RestAssured; import io.restassured.http.ContentType; import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.apache.commons.lang3.RandomStringUtils; @@ -29,7 +30,7 @@ import static org.hamcrest.Matchers.startsWith; classes = { HsadminNgApplication.class, JpaAttempt.class } ) @Transactional -class HsOfficeBankAccountControllerAcceptanceTest { +class HsOfficeBankAccountControllerAcceptanceTest extends ContextBasedTestWithCleanup { @LocalServerPort private Integer port; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java index a4fcef18..6046d55a 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java @@ -1,7 +1,7 @@ package net.hostsharing.hsadminng.hs.office.bankaccount; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import({ Context.class, JpaAttempt.class }) -class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficeBankAccountRepository bankAccountRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java index d5eb3eb0..a1ecda9c 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java @@ -4,6 +4,7 @@ import io.restassured.RestAssured; import io.restassured.http.ContentType; import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.apache.commons.lang3.RandomStringUtils; @@ -32,7 +33,7 @@ import static org.hamcrest.Matchers.startsWith; classes = { HsadminNgApplication.class, JpaAttempt.class } ) @Transactional -class HsOfficeContactControllerAcceptanceTest { +class HsOfficeContactControllerAcceptanceTest extends ContextBasedTestWithCleanup { @LocalServerPort private Integer port; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java index 83078403..02a875ae 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java @@ -1,7 +1,7 @@ package net.hostsharing.hsadminng.hs.office.contact; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import( { Context.class, JpaAttempt.class }) -class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficeContactRepository contactRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java index b5dfa429..04122059 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java @@ -5,6 +5,7 @@ import io.restassured.http.ContentType; import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.junit.jupiter.api.AfterEach; @@ -32,7 +33,7 @@ import static org.hamcrest.Matchers.startsWith; classes = { HsadminNgApplication.class, JpaAttempt.class } ) @Transactional -class HsOfficeCoopAssetsTransactionControllerAcceptanceTest { +class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBasedTestWithCleanup { @LocalServerPort Integer port; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java index e8279f3e..6200e296 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java @@ -1,8 +1,8 @@ package net.hostsharing.hsadminng.hs.office.coopassets; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import( { Context.class, JpaAttempt.class }) -class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java index 787fe467..3d120cd1 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java @@ -5,6 +5,7 @@ import io.restassured.http.ContentType; import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.junit.jupiter.api.AfterEach; @@ -29,7 +30,7 @@ import static org.hamcrest.Matchers.startsWith; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {HsadminNgApplication.class, JpaAttempt.class}) @Transactional -class HsOfficeCoopSharesTransactionControllerAcceptanceTest { +class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBasedTestWithCleanup { @Autowired Context context; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java index 9569f8e2..2956e25f 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java @@ -1,8 +1,8 @@ package net.hostsharing.hsadminng.hs.office.coopshares; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import( { Context.class, JpaAttempt.class }) -class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java index eacda619..839039a2 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java @@ -7,6 +7,7 @@ import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountRepository; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository; import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.json.JSONException; @@ -33,7 +34,7 @@ import static org.hamcrest.Matchers.*; classes = { HsadminNgApplication.class, JpaAttempt.class } ) @Transactional -class HsOfficeDebitorControllerAcceptanceTest { +class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanup { private static final int LOWEST_TEMP_DEBITOR_SUFFIX = 90; private static byte nextDebitorSuffix = LOWEST_TEMP_DEBITOR_SUFFIX; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java index 7afafaff..47ba386a 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java @@ -7,6 +7,7 @@ import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.json.JSONException; @@ -34,7 +35,7 @@ import static org.hamcrest.Matchers.*; classes = { HsadminNgApplication.class, JpaAttempt.class } ) @Transactional -class HsOfficeMembershipControllerAcceptanceTest { +class HsOfficeMembershipControllerAcceptanceTest extends ContextBasedTestWithCleanup { private static String TEMP_MEMBER_NUMBER_SUFFIX = "90"; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java index 4acd4907..751717aa 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java @@ -2,9 +2,9 @@ package net.hostsharing.hsadminng.hs.office.membership; import com.vladmihalcea.hibernate.type.range.Range; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import( { Context.class, JpaAttempt.class }) -class HsOfficeMembershipRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficeMembershipRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficeMembershipRepository membershipRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java index dabbd825..eff9dc61 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java @@ -3,7 +3,6 @@ package net.hostsharing.hsadminng.hs.office.partner; import io.restassured.RestAssured; import io.restassured.http.ContentType; import net.hostsharing.hsadminng.HsadminNgApplication; -import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity; @@ -11,6 +10,7 @@ import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository; import net.hostsharing.hsadminng.hs.office.relationship.HsOfficeRelationshipEntity; import net.hostsharing.hsadminng.hs.office.relationship.HsOfficeRelationshipRepository; import net.hostsharing.hsadminng.hs.office.relationship.HsOfficeRelationshipType; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.junit.jupiter.api.*; @@ -19,8 +19,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.transaction.annotation.Transactional; -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; import java.util.List; import java.util.UUID; @@ -33,19 +31,13 @@ import static org.hamcrest.Matchers.*; webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { HsadminNgApplication.class, JpaAttempt.class } ) -class HsOfficePartnerControllerAcceptanceTest { +class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanup { private static final UUID GIVEN_NON_EXISTING_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); @LocalServerPort private Integer port; - @Autowired - Context context; - - @Autowired - Context contextMock; - @Autowired HsOfficePartnerRepository partnerRepo; @@ -61,10 +53,6 @@ class HsOfficePartnerControllerAcceptanceTest { @Autowired JpaAttempt jpaAttempt; - @PersistenceContext - EntityManager em; - private long relationshipCountBefore; - @Nested @Accepts({ "Partner:F(Find)" }) @Transactional @@ -489,12 +477,6 @@ class HsOfficePartnerControllerAcceptanceTest { }).assertSuccessful().returnedValue(); } - @BeforeEach - void countRelationships() { - context.define("superuser-alex@hostsharing.net", null); - relationshipCountBefore = relationshipRepository.count(); - } - @AfterEach @SuppressWarnings("unchecked") void cleanup() { @@ -524,8 +506,5 @@ class HsOfficePartnerControllerAcceptanceTest { .getSingleResult(); }).assertSuccessful().returnedValue(); System.err.println("@AfterEach" + ": " + deleted + " records deleted, " + remaining + " remaining"); - - context.define("superuser-alex@hostsharing.net", null); - assertThat(relationshipRepository.count()).as("not all relationships got cleaned up").isEqualTo(relationshipCountBefore); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java index a69b0495..ebfc755b 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java @@ -1,12 +1,12 @@ package net.hostsharing.hsadminng.hs.office.partner; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository; import net.hostsharing.hsadminng.hs.office.relationship.HsOfficeRelationshipEntity; import net.hostsharing.hsadminng.hs.office.relationship.HsOfficeRelationshipRepository; import net.hostsharing.hsadminng.hs.office.relationship.HsOfficeRelationshipType; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import( { Context.class, JpaAttempt.class }) -class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficePartnerRepository partnerRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java index c370a9c2..fb713951 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java @@ -4,6 +4,7 @@ import io.restassured.RestAssured; import io.restassured.http.ContentType; import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.apache.commons.lang3.RandomStringUtils; @@ -29,7 +30,7 @@ import static org.hamcrest.Matchers.*; webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { HsadminNgApplication.class, JpaAttempt.class } ) -class HsOfficePersonControllerAcceptanceTest { +class HsOfficePersonControllerAcceptanceTest extends ContextBasedTestWithCleanup { @LocalServerPort private Integer port; @@ -54,7 +55,7 @@ class HsOfficePersonControllerAcceptanceTest { class ListPersons { @Test - void globalAdmin_withoutAssumedRoles_canViewAllPersons_ifNoCriteriaGiven() throws JSONException { + void globalAdmin_withoutAssumedRoles_canViewAllPersons_ifNoCriteriaGiven() { RestAssured // @formatter:off .given() diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java index 72f853b9..5e5d97e2 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java @@ -1,7 +1,7 @@ package net.hostsharing.hsadminng.hs.office.person; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import( { Context.class, JpaAttempt.class }) -class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficePersonRepository personRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipControllerAcceptanceTest.java index b53512e4..b24c6877 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipControllerAcceptanceTest.java @@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.office.relationship; import io.restassured.RestAssured; import io.restassured.http.ContentType; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; @@ -10,7 +11,6 @@ import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeRelati import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository; import net.hostsharing.test.JpaAttempt; import org.json.JSONException; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -18,8 +18,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.transaction.annotation.Transactional; -import java.util.HashSet; -import java.util.Set; import java.util.UUID; import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid; @@ -33,7 +31,7 @@ import static org.hamcrest.Matchers.startsWith; classes = { HsadminNgApplication.class, JpaAttempt.class } ) @Transactional -class HsOfficeRelationshipControllerAcceptanceTest { +class HsOfficeRelationshipControllerAcceptanceTest extends ContextBasedTestWithCleanup { public static final UUID GIVEN_NON_EXISTING_HOLDER_PERSON_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); @LocalServerPort @@ -57,8 +55,6 @@ class HsOfficeRelationshipControllerAcceptanceTest { @Autowired JpaAttempt jpaAttempt; - Set tempRelationshipUuids = new HashSet<>(); - @Nested @Accepts({ "Relationship:F(Find)" }) class ListRelationships { @@ -165,7 +161,7 @@ class HsOfficeRelationshipControllerAcceptanceTest { .extract().header("Location"); // @formatter:on // finally, the new relationship can be accessed under the generated UUID - final var newUserUuid = toCleanup(UUID.fromString( + final var newUserUuid = toCleanup(HsOfficeRelationshipEntity.class, UUID.fromString( location.substring(location.lastIndexOf('/') + 1))); assertThat(newUserUuid).isNotNull(); } @@ -475,27 +471,10 @@ class HsOfficeRelationshipControllerAcceptanceTest { .contact(givenContact) .build(); - toCleanup(newRelationship.getUuid()); + toCleanup(HsOfficeRelationshipEntity.class, newRelationship.getUuid()); return relationshipRepo.save(newRelationship); }).assertSuccessful().returnedValue(); } - private UUID toCleanup(final UUID tempRelationshipUuid) { - tempRelationshipUuids.add(tempRelationshipUuid); - return tempRelationshipUuid; - } - - @AfterEach - void cleanup() { - tempRelationshipUuids.forEach(uuid -> { - jpaAttempt.transacted(() -> { - context.define("superuser-alex@hostsharing.net", null); - System.out.println("DELETING temporary relationship: " + uuid); - final var count = relationshipRepo.deleteByUuid(uuid); - System.out.println("DELETED temporary relationship: " + uuid + (count > 0 ? " successful" : " failed")); - }); - }); - } - } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java index b716222d..a914ba52 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java @@ -7,6 +7,7 @@ import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountRepository; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.test.Accepts; import net.hostsharing.test.JpaAttempt; import org.json.JSONException; @@ -34,17 +35,11 @@ import static org.hamcrest.Matchers.*; classes = { HsadminNgApplication.class, JpaAttempt.class } ) @Transactional -class HsOfficeSepaMandateControllerAcceptanceTest { +class HsOfficeSepaMandateControllerAcceptanceTest extends ContextBasedTestWithCleanup { @LocalServerPort private Integer port; - @Autowired - Context context; - - @Autowired - Context contextMock; - @Autowired HsOfficeSepaMandateRepository sepaMandateRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java index ce63e2e8..5c87dedd 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java @@ -2,9 +2,9 @@ package net.hostsharing.hsadminng.hs.office.sepamandate; import com.vladmihalcea.hibernate.type.range.Range; import net.hostsharing.hsadminng.context.Context; -import net.hostsharing.hsadminng.context.ContextBasedTest; import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountRepository; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; +import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.test.Array; @@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @Import({ Context.class, JpaAttempt.class }) -class HsOfficeSepaMandateRepositoryIntegrationTest extends ContextBasedTest { +class HsOfficeSepaMandateRepositoryIntegrationTest extends ContextBasedTestWithCleanup { @Autowired HsOfficeSepaMandateRepository sepaMandateRepo; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java b/src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java new file mode 100644 index 00000000..96b53200 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java @@ -0,0 +1,91 @@ +package net.hostsharing.hsadminng.hs.office.test; + +import net.hostsharing.hsadminng.context.ContextBasedTest; +import net.hostsharing.hsadminng.hs.office.migration.HasUuid; +import net.hostsharing.hsadminng.rbac.rbacrole.RbacRoleEntity; +import net.hostsharing.hsadminng.rbac.rbacrole.RbacRoleRepository; +import net.hostsharing.test.JpaAttempt; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.springframework.beans.factory.annotation.Autowired; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import static java.util.stream.Collectors.toSet; +import static org.apache.commons.collections4.SetUtils.*; +import static org.assertj.core.api.Assertions.assertThat; + +public abstract class ContextBasedTestWithCleanup extends ContextBasedTest { + + private static final boolean SLOW_BUT_COMPARING_ROLE_NAMES = true; + + @PersistenceContext + protected EntityManager em; + + @Autowired + RbacRoleRepository rbacRoleRepo; + + @Autowired + JpaAttempt jpaAttempt; + + private Map> entitiesToCleanup = new HashMap<>(); + + private long objectCountBefore; + private Set rbacRolesBefore; + + public UUID toCleanup(final Class entityClass, final UUID uuidToCleanup) { + entitiesToCleanup.put(uuidToCleanup, entityClass); + return uuidToCleanup; + } + + @BeforeEach + //@Transactional -- TODO: check why this does not work but jpaAttempt.transacted does work + void retrieveExistingRoles() { + jpaAttempt.transacted(() -> { + context.define("superuser-alex@hostsharing.net", null); + objectCountBefore = rbacRoleRepo.count(); + rbacRolesBefore = allRbacRoles(); + }); + } + + @AfterEach + void cleanup() { + jpaAttempt.transacted(() -> { + entitiesToCleanup.forEach((uuid, entityClass) -> { + context.define("superuser-alex@hostsharing.net", null); + System.out.println("DELETING temporary " + uuid + ": " + uuid); + em.remove(em.getReference(entityClass, uuid)); + }); + }); //.assertSuccessful(); + + jpaAttempt.transacted(() -> { + context.define("superuser-alex@hostsharing.net", null); + if ( rbacRoleRepo.count() != objectCountBefore) { + final var rbacRolesAfter = allRbacRoles(); + final var missingRbacRoles = difference(rbacRolesBefore, rbacRolesAfter); + final var spuriousRbacRoles = difference(rbacRolesAfter, rbacRolesBefore); + assertThat(spuriousRbacRoles).isEqualTo(missingRbacRoles); // only true if both are empty + + // if the diff does not appear in IntelliJ IDEA, go to "Help/Edit Custom VM Options" and increase the threshold: + // -Didea.junit.message.length.threshold=1000000 + assertThat(rbacRoleRepo.count()).as("not all RBAC roles got cleaned up (most likely because not all business objects got cleaned up)").isEqualTo(objectCountBefore); + } + }).assertSuccessful(); + } + + @NotNull + private Set allRbacRoles() { + if (SLOW_BUT_COMPARING_ROLE_NAMES) { + return rbacRoleRepo.findAll().stream() + .map(RbacRoleEntity::getRoleName).sorted() + .collect(toSet()); + } + return emptySet(); + } +}