WIP: addSepaMandateWithBankAccountData

This commit is contained in:
Dev und Test fuer hsadminng 2025-02-24 21:27:29 +01:00
parent 73509990e1
commit bfbbaed5c3
2 changed files with 61 additions and 5 deletions

View File

@ -2,13 +2,12 @@ package net.hostsharing.hsadminng.hs.office.sepamandate;
import io.micrometer.core.annotation.Timed; import io.micrometer.core.annotation.Timed;
import net.hostsharing.hsadminng.context.Context; 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.bankaccount.HsOfficeBankAccountRepository;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository; 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.api.HsOfficeSepaMandatesApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeDebitorResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*;
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.mapper.StrictMapper; import net.hostsharing.hsadminng.mapper.StrictMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -20,6 +19,7 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import jakarta.validation.ValidationException; import jakarta.validation.ValidationException;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; 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); 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 saved = sepaMandateRepo.save(entityToSave);
final var uri = final var uri =

View File

@ -137,7 +137,7 @@ class HsOfficeSepaMandateControllerAcceptanceTest extends ContextBasedTestWithCl
class PostNewSepaMandate { class PostNewSepaMandate {
@Test @Test
void globalAdmin_canPostNewSepaMandate() { void globalAdmin_canPostNewSepaMandateWithBankAccountUuid() {
context.define("superuser-alex@hostsharing.net"); context.define("superuser-alex@hostsharing.net");
final var givenDebitor = debitorRepo.findDebitorsByOptionalNameLike("Third").get(0); final var givenDebitor = debitorRepo.findDebitorsByOptionalNameLike("Third").get(0);
@ -177,6 +177,51 @@ class HsOfficeSepaMandateControllerAcceptanceTest extends ContextBasedTestWithCl
assertThat(newSubjectUuid).isNotNull(); 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 // TODO.test: move validation tests to a ...WebMvcTest
@Test @Test
void globalAdmin_canNotPostNewSepaMandateWhenDebitorUuidIsMissing() { void globalAdmin_canNotPostNewSepaMandateWhenDebitorUuidIsMissing() {