From bfbbaed5c33c22ec76b02b62b1b6c9c90758ecee Mon Sep 17 00:00:00 2001 From: Dev und Test fuer hsadminng Date: Mon, 24 Feb 2025 21:27:29 +0100 Subject: [PATCH] WIP: addSepaMandateWithBankAccountData --- .../HsOfficeSepaMandateController.java | 19 ++++++-- ...ceSepaMandateControllerAcceptanceTest.java | 47 ++++++++++++++++++- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java index 61399d1e..9ddc66f7 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java @@ -2,13 +2,12 @@ package net.hostsharing.hsadminng.hs.office.sepamandate; import io.micrometer.core.annotation.Timed; import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.errors.Validate; +import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity; import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountRepository; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeSepaMandatesApi; -import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeDebitorResource; -import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandateInsertResource; -import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandatePatchResource; -import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandateResource; +import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*; import net.hostsharing.hsadminng.mapper.StrictMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -20,6 +19,7 @@ import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.validation.ValidationException; import java.util.List; +import java.util.NoSuchElementException; import java.util.UUID; import java.util.function.BiConsumer; @@ -75,6 +75,17 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi { final var entityToSave = mapper.map(body, HsOfficeSepaMandateEntity.class, SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER); + Validate.validate("bankAccount, bankAccount.uuid").exactlyOne(body.getBankAccount(), body.getBankAccountUuid()); + if ( body.getBankAccountUuid() != null) { + entityToSave.setBankAccount(bankAccountRepo.findByUuid(body.getBankAccountUuid()).orElseThrow( + () -> new NoSuchElementException("cannot find BankAccount by bankAccountUuid: " + body.getBankAccountUuid()) + )); + } else { + entityToSave.setBankAccount(bankAccountRepo.save( + mapper.map(body.getBankAccount(), HsOfficeBankAccountEntity.class) + ) ); + } + final var saved = sepaMandateRepo.save(entityToSave); final var uri = 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 4336334e..56a68182 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 @@ -137,7 +137,7 @@ class HsOfficeSepaMandateControllerAcceptanceTest extends ContextBasedTestWithCl class PostNewSepaMandate { @Test - void globalAdmin_canPostNewSepaMandate() { + void globalAdmin_canPostNewSepaMandateWithBankAccountUuid() { context.define("superuser-alex@hostsharing.net"); final var givenDebitor = debitorRepo.findDebitorsByOptionalNameLike("Third").get(0); @@ -177,6 +177,51 @@ class HsOfficeSepaMandateControllerAcceptanceTest extends ContextBasedTestWithCl assertThat(newSubjectUuid).isNotNull(); } + @Test + void globalAdmin_canPostNewSepaMandateWithBankAccountData() { + + context.define("superuser-alex@hostsharing.net"); + final var givenDebitor = debitorRepo.findDebitorsByOptionalNameLike("Third").get(0); + + final var location = RestAssured // @formatter:off + .given() + .header("current-subject", "superuser-alex@hostsharing.net") + .contentType(ContentType.JSON) + .body(""" + { + "debitor.uuid": "%s", + "bankAccount": { + "holder": "Fourth eG B", + "iban": "DE02200505501015871393" + "bic": "HASPDEHH" + }, + "reference": "temp ref CAT A", + "agreement": "2020-01-02", + "validFrom": "2022-10-13" + } + """.formatted(givenDebitor.getUuid(), givenBankAccount.getUuid())) + .port(port) + .when() + .post("http://localhost/api/hs/office/sepamandates") + .then().log().all().assertThat() + .statusCode(201) + .contentType(ContentType.JSON) + .body("uuid", isUuidValid()) + .body("debitor.partner.partnerNumber", is("P-10003")) + .body("bankAccount.holder", is("Fourth eG B")) + .body("bankAccount.iban", is("DE02200505501015871393")) + .body("reference", is("temp ref CAT A")) + .body("validFrom", is("2022-10-13")) + .body("validTo", equalTo(null)) + .header("Location", startsWith("http://localhost")) + .extract().header("Location"); // @formatter:on + + // finally, the new sepaMandate can be accessed under the generated UUID + final var newSubjectUuid = UUID.fromString( + location.substring(location.lastIndexOf('/') + 1)); + assertThat(newSubjectUuid).isNotNull(); + } + // TODO.test: move validation tests to a ...WebMvcTest @Test void globalAdmin_canNotPostNewSepaMandateWhenDebitorUuidIsMissing() {