1
0

Taiga#457: improve bookingitem resource mapping error message (#218)

Co-authored-by: Michael Hoennig <michael@hoennig.de>
Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/218
Reviewed-by: Marc Sandlus <hsh-marcsandlus@noreply.dev.hostsharing.net>
This commit is contained in:
Michael Hoennig
2026-03-19 09:40:14 +01:00
parent 69e2fc09a6
commit 79d4d8c7f2
13 changed files with 139 additions and 20 deletions
@@ -174,6 +174,6 @@ public class HsBookingItemController implements HsBookingItemsApi {
.map(parentItemUuid -> em.find(HsBookingItemRealEntity.class, parentItemUuid))
.ifPresent(entity::setParentItem);
entity.setValidity(toPostgresDateRange(LocalDate.now(), resource.getValidTo()));
entity.putResources(KeyValueMap.from(resource.getResources()));
entity.putResources(KeyValueMap.from("resources", resource.getResources()));
};
}
@@ -21,7 +21,7 @@ public class HsBookingItemEntityPatcher implements EntityPatcher<HsBookingItemPa
OptionalFromJson.of(resource.getCaption())
.ifPresent(entity::setCaption);
Optional.ofNullable(resource.getResources())
.ifPresent(r -> entity.getResources().patch(KeyValueMap.from(resource.getResources())));
.ifPresent(r -> entity.getResources().patch(KeyValueMap.from("resources", resource.getResources())));
OptionalFromJson.of(resource.getValidTo())
.ifPresent(entity::setValidTo);
}
@@ -155,7 +155,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
}
final BiConsumer<HsHostingAssetInsertResource, HsHostingAssetRbacEntity> RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
entity.putConfig(KeyValueMap.from(resource.getConfig()));
entity.putConfig(KeyValueMap.from("config", resource.getConfig()));
if (resource.getBookingItemUuid() != null) {
entity.setBookingItem(realBookingItemRepo.findByUuid(resource.getBookingItemUuid())
.orElseThrow(() -> new EntityNotFoundException("ERROR: [400] bookingItemUuid %s not found".formatted(
@@ -24,7 +24,7 @@ public class HsHostingAssetEntityPatcher implements EntityPatcher<HsHostingAsset
OptionalFromJson.of(resource.getCaption())
.ifPresent(entity::setCaption);
Optional.ofNullable(resource.getConfig())
.ifPresent(r -> entity.getConfig().patch(KeyValueMap.from(resource.getConfig())));
.ifPresent(r -> entity.getConfig().patch(KeyValueMap.from("config", resource.getConfig())));
OptionalFromJson.of(resource.getAlarmContactUuid())
// HOWTO: patch nullable JSON resource uuid to an ntity reference
.ifPresent(newValue -> entity.setAlarmContact(
@@ -19,10 +19,10 @@ class HsOfficeContactEntityPatcher implements EntityPatcher<HsOfficeContactPatch
public void apply(final HsOfficeContactPatchResource resource) {
OptionalFromJson.of(resource.getCaption()).ifPresent(entity::setCaption);
Optional.ofNullable(resource.getPostalAddress())
.ifPresent(r -> entity.getPostalAddress().patch(KeyValueMap.from(resource.getPostalAddress())));
.ifPresent(r -> entity.getPostalAddress().patch(KeyValueMap.from("postalAddress", resource.getPostalAddress())));
Optional.ofNullable(resource.getEmailAddresses())
.ifPresent(r -> entity.getEmailAddresses().patch(KeyValueMap.from(resource.getEmailAddresses())));
.ifPresent(r -> entity.getEmailAddresses().patch(KeyValueMap.from("emailAddresses", resource.getEmailAddresses())));
Optional.ofNullable(resource.getPhoneNumbers())
.ifPresent(r -> entity.getPhoneNumbers().patch(KeyValueMap.from(resource.getPhoneNumbers())));
.ifPresent(r -> entity.getPhoneNumbers().patch(KeyValueMap.from("phoneNumbers", resource.getPhoneNumbers())));
}
}
@@ -19,9 +19,9 @@ public class HsOfficeContactFromResourceConverter<E extends HsOfficeContact>
final var resource = context.getSource();
final var entity = context.getDestinationType().getDeclaredConstructor().newInstance();
entity.setCaption(resource.getCaption());
entity.putPostalAddress(from(resource.getPostalAddress()));
entity.putEmailAddresses(from(resource.getEmailAddresses()));
entity.putPhoneNumbers(from(resource.getPhoneNumbers()));
entity.putPostalAddress(from("postalAddress", resource.getPostalAddress()));
entity.putEmailAddresses(from("emailAddresses", resource.getEmailAddresses()));
entity.putPhoneNumbers(from("phoneNumbers", resource.getPhoneNumbers()));
return entity;
}
}
@@ -184,8 +184,8 @@ public class HsOfficeRelationController implements HsOfficeRelationsApi {
@SuppressWarnings("unchecked")
final BiConsumer<HsOfficeContactInsertResource, HsOfficeContactRealEntity> CONTACT_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
entity.putPostalAddress(from(resource.getPostalAddress()));
entity.putEmailAddresses(from(resource.getEmailAddresses()));
entity.putPhoneNumbers(from(resource.getPhoneNumbers()));
entity.putPostalAddress(from("postalAddress", resource.getPostalAddress()));
entity.putEmailAddresses(from("emailAddresses", resource.getEmailAddresses()));
entity.putPhoneNumbers(from("phoneNumbers", resource.getPhoneNumbers()));
};
}
@@ -1,14 +1,15 @@
package net.hostsharing.hsadminng.mapper;
import jakarta.validation.ValidationException;
import java.util.Map;
public class KeyValueMap {
@SuppressWarnings("unchecked")
public static <T> Map<String, T> from(final Object obj) {
public static <T> Map<String, T> from(final String key, final Object obj) {
if (obj == null || obj instanceof Map<?, ?>) {
return (Map<String, T>) obj;
}
throw new ClassCastException("Map expected, but got: " + obj);
throw new ValidationException(key + ": Map expected, but got: " + obj);
}
}
@@ -71,7 +71,7 @@ class HsBookingItemEntityPatcherUnitTest extends PatchUnitTestBase<
final var entity = new HsBookingItemRbacEntity();
entity.setUuid(INITIAL_BOOKING_ITEM_UUID);
entity.setProject(PROJECT_TEST_ENTITY);
entity.getResources().putAll(KeyValueMap.from(INITIAL_RESOURCES));
entity.getResources().putAll(KeyValueMap.from("resources", INITIAL_RESOURCES));
entity.setCaption(INITIAL_CAPTION);
entity.setValidity(Range.closedInfinite(GIVEN_VALID_FROM));
return entity;
@@ -71,7 +71,7 @@ class HsHostingAssetEntityPatcherUnitTest extends PatchUnitTestBase<
final var entity = new HsHostingAssetRbacEntity();
entity.setUuid(INITIAL_BOOKING_ITEM_UUID);
entity.setBookingItem(CLOUD_SERVER_BOOKING_ITEM_REAL_ENTITY);
entity.getConfig().putAll(KeyValueMap.from(INITIAL_CONFIG));
entity.getConfig().putAll(KeyValueMap.from("config", INITIAL_CONFIG));
entity.setCaption(INITIAL_CAPTION);
entity.setAlarmContact(givenInitialContact);
return entity;
@@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.mapper;
import org.junit.jupiter.api.Test;
import jakarta.validation.ValidationException;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@@ -13,7 +14,7 @@ class KeyValueMapUnitTest {
@Test
void fromMap() {
final var result = KeyValueMap.from(Map.ofEntries(
final var result = KeyValueMap.from("propName", Map.ofEntries(
Map.entry("one", 1),
Map.entry("two", 2)
));
@@ -24,9 +25,10 @@ class KeyValueMapUnitTest {
@Test
void fromNonMap() {
final var exception = catchThrowable( () ->
KeyValueMap.from("not a map")
KeyValueMap.from("propMap", "not a map")
);
assertThat(exception).isInstanceOf(ClassCastException.class);
assertThat(exception).isInstanceOf(ValidationException.class);
assertThat(exception.getMessage()).isEqualTo("propMap: Map expected, but got: not a map");
}
}