Compare commits

...

2 Commits

Author SHA1 Message Date
Michael Hoennig
c6e0bd8606 improve error message 2024-06-19 17:44:13 +02:00
Michael Hoennig
766510e078 implement asTotalLimit(...) validation for SLA-EMail etc. 2024-06-19 17:20:13 +02:00
5 changed files with 46 additions and 21 deletions

View File

@ -3,6 +3,7 @@ package net.hostsharing.hsadminng.hs.booking.item.validators;
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity; import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator; import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import net.hostsharing.hsadminng.hs.validation.ValidatableProperty; import net.hostsharing.hsadminng.hs.validation.ValidatableProperty;
import org.apache.commons.lang3.BooleanUtils;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -59,17 +60,18 @@ public class HsBookingItemEntityValidator extends HsEntityValidator<HsBookingIte
final var totalValue = ofNullable(bookingItem.getSubBookingItems()).orElse(emptyList()) final var totalValue = ofNullable(bookingItem.getSubBookingItems()).orElse(emptyList())
.stream() .stream()
.map(subItem -> propDef.getValue(subItem.getResources())) .map(subItem -> propDef.getValue(subItem.getResources()))
.map(v -> v instanceof Boolean ? BooleanUtils.toInteger((Boolean)v) : v) // FIXME: remove?
.map(HsBookingItemEntityValidator::toIntegerWithDefault0) .map(HsBookingItemEntityValidator::toIntegerWithDefault0)
.reduce(0, Integer::sum); .reduce(0, Integer::sum);
final var maxValue = getIntegerValueWithDefault0(propDef, bookingItem.getResources()); final var maxValue = getIntegerValueWithDefault0(propDef, bookingItem.getResources());
if (propDef.thresholdPercentage() != null ) { if (propDef.thresholdPercentage() != null ) {
return totalValue > (maxValue * propDef.thresholdPercentage() / 100) return totalValue > (maxValue * propDef.thresholdPercentage() / 100)
? "%s' maximum total is %d%s, but actual total %s %d%s, which exceeds threshold of %d%%" ? "%s' maximum total is %d%s, but actual total %s is %d%s, which exceeds threshold of %d%%"
.formatted(propName, maxValue, propUnit, propName, totalValue, propUnit, propDef.thresholdPercentage()) .formatted(propName, maxValue, propUnit, propName, totalValue, propUnit, propDef.thresholdPercentage())
: null; : null;
} else { } else {
return totalValue > maxValue return totalValue > maxValue
? "%s' maximum total is %d%s, but actual total %s %d%s" ? "%s' maximum total is %d%s, but actual total %s is %d%s"
.formatted(propName, maxValue, propUnit, propName, totalValue, propUnit) .formatted(propName, maxValue, propUnit, propName, totalValue, propUnit)
: null; : null;
} }

View File

@ -29,11 +29,11 @@ class HsPrivateCloudBookingItemValidator extends HsBookingItemEntityValidator {
integerProperty("SLA-Platform EXT4H") .min( 0).max( 20).withDefault(0).asTotalLimitFor("SLA-Platform", "EXT4H"), integerProperty("SLA-Platform EXT4H") .min( 0).max( 20).withDefault(0).asTotalLimitFor("SLA-Platform", "EXT4H"),
integerProperty("SLA-Platform EXT2H") .min( 0).max( 20).withDefault(0).asTotalLimitFor("SLA-Platform", "EXT2H"), integerProperty("SLA-Platform EXT2H") .min( 0).max( 20).withDefault(0).asTotalLimitFor("SLA-Platform", "EXT2H"),
integerProperty("SLA-EMail") .min( 0).max( 20).optional().asTotalLimitFor("SLA-Platform", "BASIC"), integerProperty("SLA-EMail") .min( 0).max( 20).withDefault(0).asTotalLimit(),
integerProperty("SLA-Maria") .min( 0).max( 20).optional().asTotalLimitFor("SLA-Platform", "BASIC"), integerProperty("SLA-Maria") .min( 0).max( 20).withDefault(0).asTotalLimit(),
integerProperty("SLA-PgSQL") .min( 0).max( 20).optional().asTotalLimitFor("SLA-Platform", "BASIC"), integerProperty("SLA-PgSQL") .min( 0).max( 20).withDefault(0).asTotalLimit(),
integerProperty("SLA-Office") .min( 0).max( 20).optional().asTotalLimitFor("SLA-Platform", "BASIC"), integerProperty("SLA-Office") .min( 0).max( 20).withDefault(0).asTotalLimit(),
integerProperty("SLA-Web") .min( 0).max( 20).optional().asTotalLimitFor("SLA-Platform", "BASIC") integerProperty("SLA-Web") .min( 0).max( 20).withDefault(0).asTotalLimit()
// @formatter:on // @formatter:on
); );
} }

View File

@ -69,7 +69,7 @@ public class HsHostingAssetEntityValidator extends HsEntityValidator<HsHostingAs
.reduce(0, Integer::sum); .reduce(0, Integer::sum);
final var maxValue = getIntegerValueWithDefault0(propDef, hostingAsset.getConfig()); final var maxValue = getIntegerValueWithDefault0(propDef, hostingAsset.getConfig());
return totalValue > maxValue return totalValue > maxValue
? "%s' maximum total is %d%s, but actual total is %s %d%s".formatted( ? "%s' maximum total is %d%s, but actual total %s is %d%s".formatted(
propName, maxValue, propUnit, propName, totalValue, propUnit) propName, maxValue, propUnit, propName, totalValue, propUnit)
: null; : null;
} }

View File

@ -81,7 +81,9 @@ public abstract class ValidatableProperty<T> {
final long limitingValue = ofNullable(prop.getValue(entity.getResources())).orElse(0); final long limitingValue = ofNullable(prop.getValue(entity.getResources())).orElse(0);
if (total > factor*limitingValue) { if (total > factor*limitingValue) {
return List.of(limitingValue*factor + " total " + propertyName + "=" + propertyValue + " booked, but " + total + " utilized"); return List.of(
prop.propertyName() + " maximum total is " + (factor*limitingValue) + ", but actual total for " + propertyName + "=" + propertyValue + " is " + total
);
} }
return emptyList(); return emptyList();
}; };

View File

@ -28,32 +28,38 @@ class HsPrivateCloudBookingItemValidatorUnitTest {
// given // given
final var privateCloudBookingItemEntity = HsBookingItemEntity.builder() final var privateCloudBookingItemEntity = HsBookingItemEntity.builder()
.type(PRIVATE_CLOUD) .type(PRIVATE_CLOUD)
.caption("myPC")
.resources(ofEntries( .resources(ofEntries(
entry("CPUs", 4), entry("CPUs", 4),
entry("RAM", 20), entry("RAM", 20),
entry("SSD", 100), entry("SSD", 100),
entry("Traffic", 5000), entry("Traffic", 5000),
entry("SLA-Platform EXT4H", 2) entry("SLA-Platform EXT4H", 2),
entry("SLA-EMail", 2)
)) ))
.subBookingItems(of( .subBookingItems(of(
HsBookingItemEntity.builder() HsBookingItemEntity.builder()
.type(MANAGED_SERVER) .type(MANAGED_SERVER)
.caption("myMS-1")
.resources(ofEntries( .resources(ofEntries(
entry("CPUs", 2), entry("CPUs", 2),
entry("RAM", 10), entry("RAM", 10),
entry("SSD", 50), entry("SSD", 50),
entry("Traffic", 2500), entry("Traffic", 2500),
entry("SLA-Platform", "EXT4H") entry("SLA-Platform", "EXT4H"),
entry("SLA-EMail", true)
)) ))
.build(), .build(),
HsBookingItemEntity.builder() HsBookingItemEntity.builder()
.type(CLOUD_SERVER) .type(CLOUD_SERVER)
.caption("myMS-2")
.resources(ofEntries( .resources(ofEntries(
entry("CPUs", 2), entry("CPUs", 2),
entry("RAM", 10), entry("RAM", 10),
entry("SSD", 50), entry("SSD", 50),
entry("Traffic", 2500), entry("Traffic", 2500),
entry("SLA-Platform", "EXT4H") entry("SLA-Platform", "EXT4H"),
entry("SLA-EMail", true)
)) ))
.build() .build()
)) ))
@ -72,32 +78,42 @@ class HsPrivateCloudBookingItemValidatorUnitTest {
final var privateCloudBookingItemEntity = HsBookingItemEntity.builder() final var privateCloudBookingItemEntity = HsBookingItemEntity.builder()
.project(project) .project(project)
.type(PRIVATE_CLOUD) .type(PRIVATE_CLOUD)
.caption("myPC")
.resources(ofEntries( .resources(ofEntries(
entry("CPUs", 4), entry("CPUs", 4),
entry("RAM", 20), entry("RAM", 20),
entry("SSD", 100), entry("SSD", 100),
entry("Traffic", 5000), entry("Traffic", 5000),
entry("SLA-Platform EXT2H", 1) entry("SLA-Platform EXT2H", 1),
entry("SLA-EMail", 1)
)) ))
.subBookingItems(of( .subBookingItems(of(
HsBookingItemEntity.builder() HsBookingItemEntity.builder()
.type(MANAGED_SERVER) .type(MANAGED_SERVER)
.caption("myMS-1")
.resources(ofEntries( .resources(ofEntries(
entry("CPUs", 3), entry("CPUs", 3),
entry("RAM", 20), entry("RAM", 20),
entry("SSD", 100), entry("SSD", 100),
entry("Traffic", 3000), entry("Traffic", 3000),
entry("SLA-Platform", "EXT2H") entry("SLA-Platform", "EXT2H"),
entry("SLA-EMail", true)
)) ))
.build(), .build(),
HsBookingItemEntity.builder() HsBookingItemEntity.builder()
.type(CLOUD_SERVER) .type(CLOUD_SERVER)
.caption("myMS-2")
.resources(ofEntries( .resources(ofEntries(
entry("CPUs", 2), entry("CPUs", 2),
entry("RAM", 10), entry("RAM", 10),
entry("SSD", 50), entry("SSD", 50),
entry("Traffic", 2500), entry("Traffic", 2500),
entry("SLA-Platform", "EXT2H") entry("SLA-Platform", "EXT2H"),
entry("SLA-EMail", true),
entry("SLA-Maria", true),
entry("SLA-PgSQL", true),
entry("SLA-Office", true),
entry("SLA-Web", true)
)) ))
.build() .build()
)) ))
@ -108,11 +124,16 @@ class HsPrivateCloudBookingItemValidatorUnitTest {
// then // then
assertThat(result).containsExactlyInAnyOrder( assertThat(result).containsExactlyInAnyOrder(
"'D-12345:Test-Project:null.resources.CPUs' maximum total is 4, but actual total CPUs 5", "'D-12345:Test-Project:myPC.resources.CPUs' maximum total is 4, but actual total CPUs is 5",
"'D-12345:Test-Project:null.resources.RAM' maximum total is 20 GB, but actual total RAM 30 GB", "'D-12345:Test-Project:myPC.resources.RAM' maximum total is 20 GB, but actual total RAM is 30 GB",
"'D-12345:Test-Project:null.resources.SSD' maximum total is 100 GB, but actual total SSD 150 GB", "'D-12345:Test-Project:myPC.resources.SSD' maximum total is 100 GB, but actual total SSD is 150 GB",
"'D-12345:Test-Project:null.resources.Traffic' maximum total is 5000 GB, but actual total Traffic 5500 GB", "'D-12345:Test-Project:myPC.resources.Traffic' maximum total is 5000 GB, but actual total Traffic is 5500 GB",
"'D-12345:Test-Project:null.resources.1 total SLA-Platform=EXT2H booked, but 2 utilized" "'D-12345:Test-Project:myPC.resources.SLA-Platform EXT2H maximum total is 1, but actual total for SLA-Platform=EXT2H is 2",
); "'D-12345:Test-Project:myPC.resources.SLA-EMail' maximum total is 1, but actual total SLA-EMail is 2",
"'D-12345:Test-Project:myPC.resources.SLA-Maria' maximum total is 0, but actual total SLA-Maria is 1",
"'D-12345:Test-Project:myPC.resources.SLA-PgSQL' maximum total is 0, but actual total SLA-PgSQL is 1",
"'D-12345:Test-Project:myPC.resources.SLA-Office' maximum total is 0, but actual total SLA-Office is 1",
"'D-12345:Test-Project:myPC.resources.SLA-Web' maximum total is 0, but actual total SLA-Web is 1"
);
} }
} }