simplified deletion of temp data in HsOfficeBankAccountControllerAcceptanceTest

This commit is contained in:
Michael Hoennig 2022-10-21 17:30:28 +02:00
parent a39cf73cf0
commit 97fa23165c

View File

@ -14,8 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.HashSet; import javax.persistence.EntityManager;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid; import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
@ -46,7 +45,8 @@ class HsOfficeBankAccountControllerAcceptanceTest {
@Autowired @Autowired
JpaAttempt jpaAttempt; JpaAttempt jpaAttempt;
Set<UUID> tempBankAccountUuids = new HashSet<>(); @Autowired
EntityManager em;
@Nested @Nested
@Accepts({ "bankaccount:F(Find)" }) @Accepts({ "bankaccount:F(Find)" })
@ -128,7 +128,7 @@ class HsOfficeBankAccountControllerAcceptanceTest {
.contentType(ContentType.JSON) .contentType(ContentType.JSON)
.body(""" .body("""
{ {
"holder": "new test holder", "holder": "temp test holder",
"iban": "DE88100900001234567892", "iban": "DE88100900001234567892",
"bic": "BEVODEBB" "bic": "BEVODEBB"
} }
@ -140,15 +140,15 @@ class HsOfficeBankAccountControllerAcceptanceTest {
.statusCode(201) .statusCode(201)
.contentType(ContentType.JSON) .contentType(ContentType.JSON)
.body("uuid", isUuidValid()) .body("uuid", isUuidValid())
.body("holder", is("new test holder")) .body("holder", is("temp test holder"))
.body("iban", is("DE88100900001234567892")) .body("iban", is("DE88100900001234567892"))
.body("bic", is("BEVODEBB")) .body("bic", is("BEVODEBB"))
.header("Location", startsWith("http://localhost")) .header("Location", startsWith("http://localhost"))
.extract().header("Location"); // @formatter:on .extract().header("Location"); // @formatter:on
// finally, the new bankaccount can be accessed under the generated UUID // finally, the new bankaccount can be accessed under the generated UUID
final var newUserUuid = toCleanup(UUID.fromString( final var newUserUuid = UUID.fromString(
location.substring(location.lastIndexOf('/') + 1))); location.substring(location.lastIndexOf('/') + 1));
assertThat(newUserUuid).isNotNull(); assertThat(newUserUuid).isNotNull();
} }
} }
@ -234,7 +234,7 @@ class HsOfficeBankAccountControllerAcceptanceTest {
.contentType(ContentType.JSON) .contentType(ContentType.JSON)
.body(""" .body("""
{ {
"holder": "patched holder", "holder": "temp holder - patched",
"iban": "DE02701500000000594937", "iban": "DE02701500000000594937",
"bic": "SSKMDEMM" "bic": "SSKMDEMM"
} }
@ -329,36 +329,16 @@ class HsOfficeBankAccountControllerAcceptanceTest {
.bic("INGDDEFFXXX") .bic("INGDDEFFXXX")
.build(); .build();
toCleanup(newBankAccount.getUuid());
return bankAccountRepo.save(newBankAccount); return bankAccountRepo.save(newBankAccount);
}).assertSuccessful().returnedValue(); }).assertSuccessful().returnedValue();
} }
private UUID toCleanup(final UUID tempBankAccountUuid) {
tempBankAccountUuids.add(tempBankAccountUuid);
return tempBankAccountUuid;
}
@BeforeEach @BeforeEach
@AfterEach @AfterEach
void cleanup() { void cleanup() {
jpaAttempt.transacted(() -> { jpaAttempt.transacted(() -> {
context.define("superuser-alex@hostsharing.net", null); context.define("superuser-alex@hostsharing.net", null);
tempBankAccountUuids.addAll( em.createQuery("DELETE FROM HsOfficeBankAccountEntity b WHERE b.holder LIKE 'temp %'").executeUpdate();
bankAccountRepo.findByOptionalHolderLike("some temp acc")
.stream()
.map(HsOfficeBankAccountEntity::getUuid)
.toList()
);
});
tempBankAccountUuids.forEach(uuid -> {
jpaAttempt.transacted(() -> {
context.define("superuser-alex@hostsharing.net", null);
System.out.println("DELETING temporary bankaccount: " + uuid);
final var count = bankAccountRepo.deleteByUuid(uuid);
System.out.println("DELETED temporary bankaccount: " + uuid + (count > 0 ? " successful" : " failed"));
}).assertSuccessful();
}); });
} }