From 14cae92ba5b8d522c5a9aa78afa16109457bbb49 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 1 Jul 2024 09:03:42 +0200 Subject: [PATCH] amend tests according to new API and revamp --- .../validators/HsBookingItemEntityValidator.java | 7 +------ .../validators/HsHostingAssetEntityProcessor.java | 3 ++- .../HsHostingAssetEntityValidatorRegistry.java | 1 - .../hs/booking/item/TestHsBookingItem.java | 6 ++++++ ...sCloudServerHostingAssetValidatorUnitTest.java | 13 +++++++++---- .../HsHostingAssetEntityValidatorUnitTest.java | 15 ++++++++------- ...anagedServerHostingAssetValidatorUnitTest.java | 12 ++++++------ ...agedWebspaceHostingAssetValidatorUnitTest.java | 2 +- .../HsUnixUserHostingAssetValidatorUnitTest.java | 2 +- 9 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/validators/HsBookingItemEntityValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/validators/HsBookingItemEntityValidator.java index bd36a97b..19ce9629 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/validators/HsBookingItemEntityValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/validators/HsBookingItemEntityValidator.java @@ -22,22 +22,17 @@ public class HsBookingItemEntityValidator extends HsEntityValidator validateEntity(final HsBookingItemEntity bookingItem) { - return validateProperties(bookingItem); + return enrich(prefix(bookingItem.toShortString(), "resources"), super.validateProperties(bookingItem)); } @Override public List validateContext(final HsBookingItemEntity bookingItem) { return sequentiallyValidate( - () -> validateProperties(bookingItem), () -> optionallyValidate(bookingItem.getParentItem()), () -> validateAgainstSubEntities(bookingItem) ); } - private List validateProperties(final HsBookingItemEntity bookingItem) { - return enrich(prefix(bookingItem.toShortString(), "resources"), super.validateProperties(bookingItem)); - } - private static List optionallyValidate(final HsBookingItemEntity bookingItem) { return bookingItem != null ? enrich(prefix(bookingItem.toShortString(), ""), diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityProcessor.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityProcessor.java index 286cbed4..8aa51ec8 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityProcessor.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityProcessor.java @@ -55,7 +55,8 @@ public class HsHostingAssetEntityProcessor { /// removes write-only-properties and ads computed-properties @SuppressWarnings("unchecked") public HsHostingAssetResource revampProperties() { - validator.revampProperties(entity, (Map) resource.getConfig()); + final var revampedProps = validator.revampProperties(entity, (Map) resource.getConfig()); + resource.setConfig(revampedProps); return resource; } } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistry.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistry.java index 690be147..a6c30712 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistry.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistry.java @@ -4,7 +4,6 @@ import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType; import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.model.HsHostingAssetResource; import net.hostsharing.hsadminng.hs.validation.HsEntityValidator; -import net.hostsharing.hsadminng.errors.MultiValidationException; import java.util.*; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/TestHsBookingItem.java b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/TestHsBookingItem.java index b2b43df9..bcb2baac 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/TestHsBookingItem.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/TestHsBookingItem.java @@ -16,6 +16,12 @@ public class TestHsBookingItem { .project(TEST_PROJECT) .type(HsBookingItemType.CLOUD_SERVER) .caption("test cloud server booking item") + .resources(Map.ofEntries( + entry("CPUs", 2), + entry("RAM", 4), + entry("SSD", 50), + entry("Traffic", 250) + )) .validity(Range.closedInfinite(LocalDate.of(2020, 1, 15))) .build(); 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 2344da87..ac9541bb 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 @@ -68,15 +68,15 @@ class HsCloudServerHostingAssetValidatorUnitTest { @Test void validatesBookingItemType() { // given - final var mangedServerHostingAssetEntity = HsHostingAssetEntity.builder() + final var mangedServerHostingAssetEntity = linkBookingItem(HsHostingAssetEntity.builder() .type(MANAGED_SERVER) .identifier("xyz00") - .bookingItem(TEST_CLOUD_SERVER_BOOKING_ITEM) - .build(); + .bookingItem(TEST_CLOUD_SERVER_BOOKING_ITEM.toBuilder().build()) + .build()); final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); // when - final var result = validator.validateContext(mangedServerHostingAssetEntity); + final var result = validator.validateEntity(mangedServerHostingAssetEntity); // then assertThat(result).containsExactlyInAnyOrder( @@ -103,4 +103,9 @@ class HsCloudServerHostingAssetValidatorUnitTest { "'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"); } + + private static HsHostingAssetEntity linkBookingItem(final HsHostingAssetEntity mangedServerHostingAssetEntity) { + mangedServerHostingAssetEntity.getBookingItem().setRelatedHostingAsset(mangedServerHostingAssetEntity); + return mangedServerHostingAssetEntity; + } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java index a6667210..82cae1d8 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java @@ -1,10 +1,6 @@ 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.booking.item.TestHsBookingItem; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity; -import net.hostsharing.hsadminng.mapper.Array; import org.junit.jupiter.api.Test; import java.util.List; @@ -22,10 +18,11 @@ class HsHostingAssetEntityValidatorUnitTest { final var managedServerHostingAssetEntity = HsHostingAssetEntity.builder() .type(MANAGED_SERVER) .identifier("vm1234") - .bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM) + .bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM.toBuilder().build()) .parentAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build()) .assignedToAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build()) .build(); + linkBookingItem(managedServerHostingAssetEntity); // when final var validator = HsHostingAssetEntityValidatorRegistry.forType(managedServerHostingAssetEntity.getType()); @@ -36,8 +33,12 @@ class HsHostingAssetEntityValidatorUnitTest { // then assertThat(result).containsExactlyInAnyOrder( - "'MANAGED_SERVER:vm1234.parentAsset' must be null but is set to D-???????-?:null", - "'MANAGED_SERVER:vm1234.assignedToAsset' must be null but is set to D-???????-?:null"); + "'MANAGED_SERVER:vm1234.parentAsset' must be null but is set to D-1234500:test project:test project booking item", + "'MANAGED_SERVER:vm1234.assignedToAsset' must be null but is set to D-1234500:test project:test project booking item"); + } + + private static void linkBookingItem(final HsHostingAssetEntity managedServerHostingAssetEntity) { + managedServerHostingAssetEntity.getBookingItem().setRelatedHostingAsset(managedServerHostingAssetEntity); } } 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 e40c7ab2..fd8d4800 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 @@ -33,12 +33,12 @@ class HsManagedServerHostingAssetValidatorUnitTest { final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedWebspaceHostingAssetEntity.getType()); // when - final var result = validator.validateContext(mangedWebspaceHostingAssetEntity); + final var result = validator.validateEntity(mangedWebspaceHostingAssetEntity); // then assertThat(result).containsExactlyInAnyOrder( - "'MANAGED_SERVER:vm1234.parentAsset' must be null but is set to D-???????-?:null", - "'MANAGED_SERVER:vm1234.assignedToAsset' must be null but is set to D-???????-?:null", + "'MANAGED_SERVER:vm1234.parentAsset' must be null but is set to D-1234500:test project:test project booking item", + "'MANAGED_SERVER:vm1234.assignedToAsset' must be null but is set to D-1234500:test project:test project booking item", "'MANAGED_SERVER:vm1234.config.monit_max_cpu_usage' is expected to be at least 10 but is 2", "'MANAGED_SERVER:vm1234.config.monit_max_ram_usage' is expected to be at most 100 but is 101", "'MANAGED_SERVER:vm1234.config.monit_max_hdd_usage' is expected to be of type class java.lang.Integer, but is of type 'String'"); @@ -75,12 +75,12 @@ class HsManagedServerHostingAssetValidatorUnitTest { final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); // when - final var result = validator.validateContext(mangedServerHostingAssetEntity); + final var result = validator.validateEntity(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"); + "'MANAGED_SERVER:xyz00.parentAsset' must be null but is set to D-1234500:test project:test cloud server booking item", + "'MANAGED_SERVER:xyz00.assignedToAsset' must be null but is set to D-1234500:test project:test cloud server booking item"); } } 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 92d52cc0..1d2c6d24 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 @@ -158,7 +158,7 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest { .build(); // when - final var result = validator.validateContext(mangedWebspaceHostingAssetEntity); + final var result = validator.validateEntity(mangedWebspaceHostingAssetEntity); // then assertThat(result).containsExactly( diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java index d720aaa0..4cb597dc 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java @@ -125,7 +125,7 @@ class HsUnixUserHostingAssetValidatorUnitTest { "{type=enumeration, propertyName=shell, values=[/bin/false, /bin/bash, /bin/csh, /bin/dash, /usr/bin/tcsh, /usr/bin/zsh, /usr/bin/passwd], defaultValue=/bin/false}", "{type=string, propertyName=homedir, readOnly=true, computed=true}", "{type=string, propertyName=totpKey, matchesRegEx=^0x([0-9A-Fa-f]{2})+$, minLength=20, maxLength=256, writeOnly=true, undisclosed=true}", - "{type=password, propertyName=password, minLength=8, maxLength=40, writeOnly=true, hashedUsing=SHA512, undisclosed=true}" + "{type=password, propertyName=password, minLength=8, maxLength=40, writeOnly=true, computed=true, hashedUsing=SHA512, undisclosed=true}" ); } }