From 9e4146c511609f50cbe59d1401c8e116c833b59c Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 24 Jun 2024 09:51:29 +0200 Subject: [PATCH] add feature tests for new validation config options --- .../hs/booking/item/HsBookingItemEntity.java | 2 +- .../HsHostingAssetEntityValidator.java | 1 + ...udServerHostingAssetValidatorUnitTest.java | 42 +++++++++++++++++++ ...edServerHostingAssetValidatorUnitTest.java | 22 ++++++++++ ...WebspaceHostingAssetValidatorUnitTest.java | 41 ++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntity.java index 7961223e..94b80984 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntity.java @@ -61,8 +61,8 @@ import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.SQL.directlyFetche import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.rbacViewFor; import static net.hostsharing.hsadminng.stringify.Stringify.stringify; -@Builder @Entity +@Builder(toBuilder = true) @Table(name = "hs_booking_item_rv") @Getter @Setter diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidator.java index cb6a2220..15ea12df 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidator.java @@ -102,6 +102,7 @@ public abstract class HsHostingAssetEntityValidator extends HsEntityValidator propDef) { diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidatorUnitTest.java index cd872766..fff0fd56 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidatorUnitTest.java @@ -1,5 +1,7 @@ package net.hostsharing.hsadminng.hs.hosting.asset.validators; +import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity; +import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity; import org.junit.jupiter.api.Test; @@ -8,6 +10,7 @@ import java.util.Map; import static java.util.Map.entry; import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_CLOUD_SERVER_BOOKING_ITEM; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER; import static org.assertj.core.api.Assertions.assertThat; class HsCloudServerHostingAssetValidatorUnitTest { @@ -61,4 +64,43 @@ class HsCloudServerHostingAssetValidatorUnitTest { // then assertThat(validator.properties()).map(Map::toString).isEmpty(); } + + @Test + void validatesBookingItemType() { + // given + final var mangedServerHostingAssetEntity = HsHostingAssetEntity.builder() + .type(MANAGED_SERVER) + .identifier("xyz00") + .bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build()) + .build(); + final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + + // when + final var result = validator.validate(mangedServerHostingAssetEntity); + + // then + assertThat(result).containsExactlyInAnyOrder( + "'MANAGED_SERVER:xyz00.bookingItem' must be of type MANAGED_SERVER but is of type CLOUD_SERVER"); + } + + @Test + void validatesParentAndAssignedToAssetMustNotBeSet() { + // given + final var mangedServerHostingAssetEntity = HsHostingAssetEntity.builder() + .type(CLOUD_SERVER) + .identifier("xyz00") + .parentAsset(HsHostingAssetEntity.builder().build()) + .assignedToAsset(HsHostingAssetEntity.builder().build()) + .bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build()) + .build(); + final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + + // when + final var result = validator.validate(mangedServerHostingAssetEntity); + + // then + assertThat(result).containsExactlyInAnyOrder( + "'CLOUD_SERVER:xyz00.parentAsset' must be null but is set to D-???????-?:null", + "'CLOUD_SERVER:xyz00.assignedToAsset' must be null but is set to D-???????-?:null"); + } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedServerHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedServerHostingAssetValidatorUnitTest.java index 24189add..010bbf54 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedServerHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedServerHostingAssetValidatorUnitTest.java @@ -59,4 +59,26 @@ class HsManagedServerHostingAssetValidatorUnitTest { assertThat(result).containsExactlyInAnyOrder( "'identifier' expected to match '^vm[0-9][0-9][0-9][0-9]$', but is 'xyz00'"); } + + @Test + void validatesParentAndAssignedToAssetMustNotBeSet() { + // given + final var mangedServerHostingAssetEntity = HsHostingAssetEntity.builder() + .type(MANAGED_SERVER) + .identifier("xyz00") + .parentAsset(HsHostingAssetEntity.builder().build()) + .assignedToAsset(HsHostingAssetEntity.builder().build()) + .bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build()) + .build(); + final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + + // when + final var result = validator.validate(mangedServerHostingAssetEntity); + + // then + assertThat(result).containsExactlyInAnyOrder( + "'MANAGED_SERVER:xyz00.bookingItem' must be of type MANAGED_SERVER but is of type CLOUD_SERVER", + "'MANAGED_SERVER:xyz00.parentAsset' must be null but is set to D-???????-?:null", + "'MANAGED_SERVER:xyz00.assignedToAsset' must be null but is set to D-???????-?:null"); + } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidatorUnitTest.java index 014721af..d67d0ca3 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidatorUnitTest.java @@ -28,6 +28,11 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest { entry("SLA-EMail", true) )) .build(); + final HsBookingItemEntity cloudServerBookingItem = managedServerBookingItem.toBuilder() + .type(HsBookingItemType.CLOUD_SERVER) + .caption("Test Cloud-Server") + .build(); + final HsHostingAssetEntity mangedServerAssetEntity = HsHostingAssetEntity.builder() .type(HsHostingAssetType.MANAGED_SERVER) .bookingItem(managedServerBookingItem) @@ -38,6 +43,16 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest { entry("monit_max_ram_usage", 90) )) .build(); + final HsHostingAssetEntity cloudServerAssetEntity = HsHostingAssetEntity.builder() + .type(HsHostingAssetType.CLOUD_SERVER) + .bookingItem(cloudServerBookingItem) + .identifier("vm1234") + .config(Map.ofEntries( + entry("monit_max_ssd_usage", 70), + entry("monit_max_cpu_usage", 80), + entry("monit_max_ram_usage", 90) + )) + .build(); @Test void validatesIdentifierAndReferencedEntities() { @@ -99,4 +114,30 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest { // then assertThat(result).isEmpty(); } + + @Test + void validatesEntityReferences() { + // given + final var validator = HsHostingAssetEntityValidatorRegistry.forType(MANAGED_WEBSPACE); + final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder() + .type(MANAGED_WEBSPACE) + .bookingItem(HsBookingItemEntity.builder() + .type(HsBookingItemType.MANAGED_SERVER) + .caption("some ManagedServer") + .resources(Map.ofEntries(entry("SSD", 25), entry("Traffic", 250))) + .build()) + .parentAsset(cloudServerAssetEntity) + .assignedToAsset(HsHostingAssetEntity.builder().build()) + .identifier("abc00") + .build(); + + // when + final var result = validator.validate(mangedWebspaceHostingAssetEntity); + + // then + assertThat(result).containsExactly( + "'MANAGED_WEBSPACE:abc00.bookingItem' must be of type MANAGED_WEBSPACE but is of type MANAGED_SERVER", + "'MANAGED_WEBSPACE:abc00.parentAsset' must be of type MANAGED_SERVER but is of type CLOUD_SERVER", + "'MANAGED_WEBSPACE:abc00.assignedToAsset' must be null but is set to D-???????-?:some ManagedServer"); + } }