hosting-asset-validation-beyond-property-validators #65

Merged
hsh-michaelhoennig merged 6 commits from hosting-asset-validation-beyond-property-validators into master 2024-06-24 12:33:15 +02:00
5 changed files with 107 additions and 1 deletions
Showing only changes of commit 9e4146c511 - Show all commits

View File

@ -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.rbac.rbacdef.RbacView.rbacViewFor;
import static net.hostsharing.hsadminng.stringify.Stringify.stringify; import static net.hostsharing.hsadminng.stringify.Stringify.stringify;
@Builder
@Entity @Entity
@Builder(toBuilder = true)
@Table(name = "hs_booking_item_rv") @Table(name = "hs_booking_item_rv")
@Getter @Getter
@Setter @Setter

View File

@ -102,6 +102,7 @@ public abstract class HsHostingAssetEntityValidator extends HsEntityValidator<Hs
.toList()); .toList());
} }
// TODO.test: check, if there are any hosting assets which need this validation at all
private String validateMaxTotalValue( private String validateMaxTotalValue(
final HsHostingAssetEntity hostingAsset, final HsHostingAssetEntity hostingAsset,
final ValidatableProperty<?> propDef) { final ValidatableProperty<?> propDef) {

View File

@ -1,5 +1,7 @@
package net.hostsharing.hsadminng.hs.hosting.asset.validators; 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 net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -8,6 +10,7 @@ import java.util.Map;
import static java.util.Map.entry; 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.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.CLOUD_SERVER;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
class HsCloudServerHostingAssetValidatorUnitTest { class HsCloudServerHostingAssetValidatorUnitTest {
@ -61,4 +64,43 @@ class HsCloudServerHostingAssetValidatorUnitTest {
// then // then
assertThat(validator.properties()).map(Map::toString).isEmpty(); 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");
}
} }

View File

@ -59,4 +59,26 @@ class HsManagedServerHostingAssetValidatorUnitTest {
assertThat(result).containsExactlyInAnyOrder( assertThat(result).containsExactlyInAnyOrder(
"'identifier' expected to match '^vm[0-9][0-9][0-9][0-9]$', but is 'xyz00'"); "'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");
}
} }

View File

@ -28,6 +28,11 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest {
entry("SLA-EMail", true) entry("SLA-EMail", true)
)) ))
.build(); .build();
final HsBookingItemEntity cloudServerBookingItem = managedServerBookingItem.toBuilder()
.type(HsBookingItemType.CLOUD_SERVER)
.caption("Test Cloud-Server")
.build();
final HsHostingAssetEntity mangedServerAssetEntity = HsHostingAssetEntity.builder() final HsHostingAssetEntity mangedServerAssetEntity = HsHostingAssetEntity.builder()
.type(HsHostingAssetType.MANAGED_SERVER) .type(HsHostingAssetType.MANAGED_SERVER)
.bookingItem(managedServerBookingItem) .bookingItem(managedServerBookingItem)
@ -38,6 +43,16 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest {
entry("monit_max_ram_usage", 90) entry("monit_max_ram_usage", 90)
)) ))
.build(); .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 @Test
void validatesIdentifierAndReferencedEntities() { void validatesIdentifierAndReferencedEntities() {
@ -99,4 +114,30 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest {
// then // then
assertThat(result).isEmpty(); 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");
}
} }