From 0701d8a629747873a84799614fdc33cbde68fe37 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 25 Sep 2024 17:13:56 +0200 Subject: [PATCH] add namePrefix to mapper error message --- .../debitor/HsOfficeDebitorController.java | 2 +- .../hostsharing/hsadminng/mapper/Mapper.java | 23 +++++++++++++++---- ...OfficeDebitorControllerAcceptanceTest.java | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java index a2466aa9..a9109c13 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java @@ -84,7 +84,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class); if ( body.getDebitorRel() != null ) { body.getDebitorRel().setType(DEBITOR.name()); - final var debitorRel = mapper.map(body.getDebitorRel(), HsOfficeRelationRealEntity.class); + final var debitorRel = mapper.map("debitorRel.", body.getDebitorRel(), HsOfficeRelationRealEntity.class); validateEntityExists("debitorRel.anchorUuid", debitorRel.getAnchor()); validateEntityExists("debitorRel.holderUuid", debitorRel.getHolder()); validateEntityExists("debitorRel.contactUuid", debitorRel.getContact()); diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java index b568a0e6..aabfdfd9 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java @@ -47,6 +47,10 @@ abstract class Mapper extends ModelMapper { @Override public D map(final Object source, final Class destinationType) { + return map("", source, destinationType); + } + + public D map(final String namePrefix, final Object source, final Class destinationType) { final var target = super.map(source, destinationType); for (Field f : getDeclaredFieldsIncludingSuperClasses(destinationType)) { if (f.getAnnotation(ManyToOne.class) == null) { @@ -66,7 +70,7 @@ abstract class Mapper extends ModelMapper { if (subEntityUuid == null) { continue; } - ReflectionUtils.setField(f, target, fetchEntity(f.getType(), subEntityUuid)); + ReflectionUtils.setField(f, target, fetchEntity(namePrefix + f.getName() + ".uuid", f.getType(), subEntityUuid)); } return target; } @@ -82,13 +86,15 @@ abstract class Mapper extends ModelMapper { .toArray(Field[]::new); } - public E fetchEntity(final Class entityClass, final Object subEntityUuid) { - // using getReference would be more efficent, but results in very technical error messages + public E fetchEntity(final String propertyName, final Class entityClass, final Object subEntityUuid) { + // using getReference would be more efficient, but results in very technical error messages final var entity = em.find(entityClass, subEntityUuid); // FIXME: try getReference if (entity != null) { return entity; } - throw new ValidationException("Unable to find " + DisplayName.of(entityClass) + " by uuid: " + subEntityUuid); + throw new ValidationException( + "Unable to find " + DisplayName.of(entityClass) + + " by " + propertyName + ": " + subEntityUuid); } public T map(final S source, final Class targetClass, final BiConsumer postMapper) { @@ -99,4 +105,13 @@ abstract class Mapper extends ModelMapper { postMapper.accept(source, target); return target; } + + public T map(final String namePrefix, final S source, final Class targetClass, final BiConsumer postMapper) { + if (source == null) { + return null; + } + final var target = map(source, targetClass); + postMapper.accept(source, target); + return target; + } } 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 98ba650c..7f3b2281 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 @@ -405,7 +405,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu .post("http://localhost/api/hs/office/debitors") .then().log().all().assertThat() .statusCode(400) - .body("message", is("ERROR: [400] Unable to find RealContact by debitorRel.contactUuid: 00000000-0000-0000-0000-000000000000")); + .body("message", is("ERROR: [400] Unable to find RealContact by debitorRel.contact.uuid: 00000000-0000-0000-0000-000000000000")); // @formatter:on } @@ -433,7 +433,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu .post("http://localhost/api/hs/office/debitors") .then().log().all().assertThat() .statusCode(400) - .body("message", is("ERROR: [400] Unable to find RealRelation by uuid: 00000000-0000-0000-0000-000000000000")); + .body("message", is("ERROR: [400] Unable to find RealRelation by debitorRel.uuid: 00000000-0000-0000-0000-000000000000")); // @formatter:on } }