add rbacrole test in before+after to all Integration+Acceptance-tests

This commit is contained in:
Michael Hoennig 2024-01-29 10:03:51 +01:00
parent ebb94969e7
commit df025503d9
21 changed files with 137 additions and 83 deletions

View File

@ -8,9 +8,12 @@ import java.util.UUID;
public interface RbacRoleRepository extends Repository<RbacRoleEntity, UUID> {
/**
* 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<RbacRoleEntity> findAll();

View File

@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
public abstract class ContextBasedTest {
@Autowired
Context context;
protected Context context;
TestInfo test;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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";

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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()

View File

@ -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;

View File

@ -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<UUID> 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"));
});
});
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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<UUID, Class<? extends HasUuid>> entitiesToCleanup = new HashMap<>();
private long objectCountBefore;
private Set<String> rbacRolesBefore;
public UUID toCleanup(final Class<? extends HasUuid> 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<String> allRbacRoles() {
if (SLOW_BUT_COMPARING_ROLE_NAMES) {
return rbacRoleRepo.findAll().stream()
.map(RbacRoleEntity::getRoleName).sorted()
.collect(toSet());
}
return emptySet();
}
}