acceptance test for auto-creating hosting asset for managed webspace booking item

This commit is contained in:
Michael Hoennig 2024-10-07 14:20:44 +02:00
parent f5b839cf52
commit 54d741a6c7
4 changed files with 34 additions and 10 deletions

View File

@ -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<HsBookingItem, HsBookingItemResource> 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<HsBookingItemInsertResource, HsBookingItemRbacEntity> 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()));
};

View File

@ -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;
}

View File

@ -56,6 +56,10 @@ components:
type: string
format: uuid
nullable: false
parentItemUuid:
type: string
format: uuid
nullable: false
type:
$ref: '#/components/schemas/HsBookingItemType'
identifier:

View File

@ -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()