From 54d741a6c710f75fde9c88d7e6caf6b613eebb79 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 7 Oct 2024 14:20:44 +0200 Subject: [PATCH] acceptance test for auto-creating hosting asset for managed webspace booking item --- .../booking/item/HsBookingItemController.java | 23 ++++++++++++------- .../ManagedWebspaceHostingAssetFactory.java | 11 +++++++-- .../hs-booking/hs-booking-item-schemas.yaml | 4 ++++ ...HsBookingItemControllerAcceptanceTest.java | 6 +++++ 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java index 8a78ba2a..4a6b3729 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.UUID; import java.util.function.BiConsumer; +import static java.util.Optional.ofNullable; import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange; @RestController @@ -81,14 +82,7 @@ public class HsBookingItemController implements HsBookingItemsApi { .validateContext() .mapUsing(e -> mapper.map(e, HsBookingItemResource.class, ITEM_TO_RESOURCE_POSTMAPPER)) .revampProperties(); - - try { - final var bookingItemRealEntity = em.getReference(HsBookingItemRealEntity.class, saveProcessor.getEntity().getUuid()); - applicationEventPublisher.publishEvent(new BookingItemCreatedAppEvent( - this, bookingItemRealEntity, jsonMapper.writeValueAsString(body.getAsset()))); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } + publishSavedEvent(saveProcessor, body); final var uri = MvcUriComponentsBuilder.fromController(getClass()) @@ -148,6 +142,16 @@ public class HsBookingItemController implements HsBookingItemsApi { return ResponseEntity.ok(mapped); } + private void publishSavedEvent(final BookingItemEntitySaveProcessor saveProcessor, final HsBookingItemInsertResource body) { + try { + final var bookingItemRealEntity = em.getReference(HsBookingItemRealEntity.class, saveProcessor.getEntity().getUuid()); + applicationEventPublisher.publishEvent(new BookingItemCreatedAppEvent( + this, bookingItemRealEntity, jsonMapper.writeValueAsString(body.getAsset()))); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + final BiConsumer ITEM_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { resource.setValidFrom(entity.getValidity().lower()); if (entity.getValidity().hasUpperBound()) { @@ -159,6 +163,9 @@ public class HsBookingItemController implements HsBookingItemsApi { final BiConsumer RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { entity.setProject(em.find(HsBookingProjectRealEntity.class, resource.getProjectUuid())); + ofNullable(resource.getParentItemUuid()) + .map(parentItemUuid -> em.find(HsBookingItemRealEntity.class, parentItemUuid)) + .ifPresent(entity::setParentItem); entity.setValidity(toPostgresDateRange(LocalDate.now(), resource.getValidTo())); entity.putResources(KeyValueMap.from(resource.getResources())); }; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ManagedWebspaceHostingAssetFactory.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ManagedWebspaceHostingAssetFactory.java index 70752572..e820ad40 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ManagedWebspaceHostingAssetFactory.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ManagedWebspaceHostingAssetFactory.java @@ -10,7 +10,8 @@ import net.hostsharing.hsadminng.persistence.EntityManagerWrapper; import jakarta.validation.ValidationException; -import static java.util.Optional.ofNullable; +import java.util.Optional; + public class ManagedWebspaceHostingAssetFactory extends HostingAssetFactory { @@ -26,13 +27,19 @@ public class ManagedWebspaceHostingAssetFactory extends HostingAssetFactory { protected HsHostingAsset create() { if (asset.getType() != HsHostingAssetTypeResource.MANAGED_WEBSPACE) { throw new ValidationException("requires MANAGED_WEBSPACE hosting asset, but got " + - ofNullable(asset) + Optional.of(asset) .map(HsHostingAssetAutoInsertResource::getType) .map(Enum::name) .orElse(null)); } final var managedWebspaceHostingAsset = standardMapper.map(asset, HsHostingAssetRealEntity.class); managedWebspaceHostingAsset.setBookingItem(fromBookingItem); + emw.createQuery( + "SELECT asset FROM HsHostingAssetRealEntity asset WHERE asset.bookingItem.uuid=:bookingItemUuid", + HsHostingAssetRealEntity.class) + .setParameter("bookingItemUuid", fromBookingItem.getParentItem().getUuid()) + .getResultStream().findFirst() + .ifPresent(managedWebspaceHostingAsset::setParentAsset); return managedWebspaceHostingAsset; } diff --git a/src/main/resources/api-definition/hs-booking/hs-booking-item-schemas.yaml b/src/main/resources/api-definition/hs-booking/hs-booking-item-schemas.yaml index 22d17ce3..354bec43 100644 --- a/src/main/resources/api-definition/hs-booking/hs-booking-item-schemas.yaml +++ b/src/main/resources/api-definition/hs-booking/hs-booking-item-schemas.yaml @@ -56,6 +56,10 @@ components: type: string format: uuid nullable: false + parentItemUuid: + type: string + format: uuid + nullable: false type: $ref: '#/components/schemas/HsBookingItemType' identifier: diff --git a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerAcceptanceTest.java index 66e2dff1..0217667c 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerAcceptanceTest.java @@ -34,6 +34,7 @@ import java.util.UUID; import static java.util.Map.entry; import static java.util.Optional.ofNullable; import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.MANAGED_WEBSPACE; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.UNIX_USER; import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.lenientlyEquals; import static org.assertj.core.api.Assertions.assertThat; @@ -189,6 +190,9 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup context.define("superuser-alex@hostsharing.net", "hs_booking.project#D-1000111-D-1000111defaultproject:AGENT"); final var givenProject = findDefaultProjectOfDebitorNumber(1000111); + final var givenManagedServer = realHostingAssetRepo.findByTypeAndIdentifier(MANAGED_SERVER, "vm1011").stream() + .map(HsHostingAsset::getBookingItem) + .findFirst().orElseThrow(); final var location = RestAssured // @formatter:off .given() @@ -197,6 +201,7 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup .body(""" { "projectUuid": "{projectUuid}", + "parentItemUuid": "{managedServerUuid}", "type": "MANAGED_WEBSPACE", "caption": "some managed webspace", "resources": { @@ -210,6 +215,7 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup } """ .replace("{projectUuid}", givenProject.getUuid().toString()) + .replace("{managedServerUuid}", givenManagedServer.getUuid().toString()) ) .port(port) .when()