HostingAsset-Hierarchie spec in enum HsHostingAssetType and generates PlantUML #72
@ -21,6 +21,9 @@ import static java.util.stream.Collectors.toSet;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.EntityTypeRelation.*;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationPolicy.OPTIONAL;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationPolicy.REQUIRED;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationType.ASSIGNED_TO_ASSET;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationType.BOOKING_ITEM;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationType.PARENT_ASSET;
|
||||
|
||||
public enum HsHostingAssetType implements Node {
|
||||
SAME_TYPE, // pseudo-type for recursive references
|
||||
@ -135,43 +138,49 @@ public enum HsHostingAssetType implements Node {
|
||||
// TODO.refa: try to get rid of the following similar methods:
|
||||
|
||||
public RelationPolicy bookingItemPolicy() {
|
||||
return stream(relations).filter(r -> r.relatedType instanceof HsBookingItemType)
|
||||
.map(r -> r.relationType)
|
||||
return stream(relations)
|
||||
.filter(r -> r.relationType == BOOKING_ITEM)
|
||||
.map(r -> r.relationPolicy)
|
||||
.reduce(HsHostingAssetType::onlyASingleElementExpectedException)
|
||||
.orElse(RelationPolicy.FORBIDDEN);
|
||||
}
|
||||
|
||||
public HsBookingItemType bookingItemType() {
|
||||
return stream(relations).filter(r -> r.relatedType instanceof HsBookingItemType)
|
||||
.map(r -> HsBookingItemType.valueOf(r.relatedType.toString()))
|
||||
return stream(relations)
|
||||
.filter(r -> r.relationType == BOOKING_ITEM)
|
||||
.map(r -> HsBookingItemType.valueOf(r.relatedType(this).toString()))
|
||||
.reduce(HsHostingAssetType::onlyASingleElementExpectedException)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public RelationPolicy parentAssetPolicy() {
|
||||
return stream(relations).filter(r -> r.relatedType instanceof HsHostingAssetType)
|
||||
.map(r -> r.relationType)
|
||||
return stream(relations)
|
||||
.filter(r -> r.relationType == PARENT_ASSET)
|
||||
.map(r -> r.relationPolicy)
|
||||
.reduce(HsHostingAssetType::onlyASingleElementExpectedException)
|
||||
.orElse(RelationPolicy.FORBIDDEN);
|
||||
}
|
||||
|
||||
public HsHostingAssetType parentAssetType() {
|
||||
return stream(relations).filter(r -> r.relatedType instanceof HsHostingAssetType)
|
||||
.map(r -> HsHostingAssetType.valueOf(r.relatedType.toString()))
|
||||
return stream(relations)
|
||||
.filter(r -> r.relationType == PARENT_ASSET)
|
||||
.map(r -> HsHostingAssetType.valueOf(r.relatedType(this).toString()))
|
||||
.reduce(HsHostingAssetType::onlyASingleElementExpectedException)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public RelationPolicy assignedToAssetPolicy() {
|
||||
return stream(relations).filter(r -> r.relatedType == (Object) HsHostingAssetType.class)
|
||||
.map(r -> r.relationType)
|
||||
return stream(relations)
|
||||
.filter(r -> r.relationType == ASSIGNED_TO_ASSET)
|
||||
.map(r -> r.relationPolicy)
|
||||
.reduce(HsHostingAssetType::onlyASingleElementExpectedException)
|
||||
.orElse(RelationPolicy.FORBIDDEN);
|
||||
}
|
||||
|
||||
public HsHostingAssetType assignedToAssetType() {
|
||||
return stream(relations).filter(r -> r.relatedType == (Object) HsHostingAssetType.class)
|
||||
.map(r -> HsHostingAssetType.valueOf(r.relatedType.toString()))
|
||||
return stream(relations)
|
||||
.filter(r -> r.relationType == ASSIGNED_TO_ASSET)
|
||||
.map(r -> HsHostingAssetType.valueOf(r.relatedType(this).toString()))
|
||||
.reduce(HsHostingAssetType::onlyASingleElementExpectedException)
|
||||
.orElse(null);
|
||||
}
|
||||
@ -183,14 +192,10 @@ public enum HsHostingAssetType implements Node {
|
||||
@Override
|
||||
public List<String> edges() {
|
||||
return stream(relations)
|
||||
.map(r -> nodeName() + r.edge + resolveNode(r.relatedType).nodeName())
|
||||
.map(r -> nodeName() + r.edge + r.relatedType(this).nodeName())
|
||||
.toList();
|
||||
}
|
||||
|
||||
private Node resolveNode(final Node node) {
|
||||
return node == SAME_TYPE ? this : node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String nodeName() {
|
||||
return "HA_" + name();
|
||||
@ -310,28 +315,41 @@ public enum HsHostingAssetType implements Node {
|
||||
public enum RelationPolicy {
|
||||
FORBIDDEN, OPTIONAL, REQUIRED
|
||||
}
|
||||
|
||||
public enum RelationType {
|
||||
BOOKING_ITEM,
|
||||
PARENT_ASSET,
|
||||
ASSIGNED_TO_ASSET
|
||||
}
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
class EntityTypeRelation<E, T extends Node> {
|
||||
final HsHostingAssetType.RelationPolicy relationType;
|
||||
|
||||
final HsHostingAssetType.RelationPolicy relationPolicy;
|
||||
final HsHostingAssetType.RelationType relationType;
|
||||
final Function<HsHostingAssetEntity, E> getter;
|
||||
final T relatedType;
|
||||
private final T relatedType;
|
||||
final String edge;
|
||||
|
||||
public T relatedType(final HsHostingAssetType referringType) {
|
||||
//noinspection unchecked
|
||||
return relatedType == HsHostingAssetType.SAME_TYPE ? (T) referringType : relatedType;
|
||||
}
|
||||
|
||||
static EntityTypeRelation<HsBookingItemEntity, HsBookingItemType> requires(final HsBookingItemType bookingItemType) {
|
||||
return new EntityTypeRelation<>(REQUIRED, HsHostingAssetEntity::getBookingItem, bookingItemType, " *==> ");
|
||||
return new EntityTypeRelation<>(REQUIRED, BOOKING_ITEM, HsHostingAssetEntity::getBookingItem, bookingItemType, " *==> ");
|
||||
}
|
||||
|
||||
static EntityTypeRelation<HsHostingAssetEntity, HsHostingAssetType> optionalParent(final HsHostingAssetType hostingAssetType) {
|
||||
return new EntityTypeRelation<>(OPTIONAL, HsHostingAssetEntity::getParentAsset, hostingAssetType, " o..> ");
|
||||
return new EntityTypeRelation<>(OPTIONAL, PARENT_ASSET, HsHostingAssetEntity::getParentAsset, hostingAssetType, " o..> ");
|
||||
}
|
||||
|
||||
static EntityTypeRelation<HsHostingAssetEntity, HsHostingAssetType> requiredParent(final HsHostingAssetType hostingAssetType) {
|
||||
return new EntityTypeRelation<>(REQUIRED, HsHostingAssetEntity::getParentAsset, hostingAssetType, " *==> ");
|
||||
return new EntityTypeRelation<>(REQUIRED, PARENT_ASSET, HsHostingAssetEntity::getParentAsset, hostingAssetType, " *==> ");
|
||||
}
|
||||
|
||||
static EntityTypeRelation<HsHostingAssetEntity, HsHostingAssetType> assignedTo(final HsHostingAssetType hostingAssetType) {
|
||||
return new EntityTypeRelation<>(REQUIRED, HsHostingAssetEntity::getAssignedToAsset, hostingAssetType, " o..> ");
|
||||
return new EntityTypeRelation<>(REQUIRED, ASSIGNED_TO_ASSET, HsHostingAssetEntity::getAssignedToAsset, hostingAssetType, " o..> ");
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ class HsDomainDnsSetupHostingAssetValidator extends HsHostingAssetEntityValidato
|
||||
|
||||
@Override
|
||||
protected Pattern identifierPattern(final HsHostingAssetEntity assetEntity) {
|
||||
return Pattern.compile("^" + assetEntity.getParentAsset().getIdentifier() + IDENTIFIER_SUFFIX + "$");
|
||||
return Pattern.compile("^" + assetEntity.getParentAsset().getIdentifier() + Pattern.quote(IDENTIFIER_SUFFIX) + "$");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -20,9 +20,6 @@ import java.util.stream.Stream;
|
||||
import static java.util.Arrays.stream;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Optional.ofNullable;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationPolicy.FORBIDDEN;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationPolicy.OPTIONAL;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.RelationPolicy.REQUIRED;
|
||||
|
||||
public abstract class HsHostingAssetEntityValidator extends HsEntityValidator<HsHostingAssetEntity> {
|
||||
|
||||
@ -34,7 +31,9 @@ public abstract class HsHostingAssetEntityValidator extends HsEntityValidator<Hs
|
||||
private final HsHostingAssetEntityValidator.AlarmContact alarmContactValidation;
|
||||
|
||||
HsHostingAssetEntityValidator(
|
||||
final HsHostingAssetType assetType, final AlarmContact alarmContactValidation, final ValidatableProperty<?, ?>... properties) {
|
||||
final HsHostingAssetType assetType,
|
||||
final AlarmContact alarmContactValidation,
|
||||
final ValidatableProperty<?, ?>... properties) {
|
||||
super(properties);
|
||||
this.bookingItemReferenceValidation = new ReferenceValidator<>(
|
||||
assetType.bookingItemPolicy(),
|
||||
@ -97,20 +96,23 @@ public abstract class HsHostingAssetEntityValidator extends HsEntityValidator<Hs
|
||||
|
||||
private static List<String> optionallyValidate(final HsHostingAssetEntity assetEntity) {
|
||||
return assetEntity != null
|
||||
? enrich(prefix(assetEntity.toShortString(), "parentAsset"),
|
||||
? enrich(
|
||||
prefix(assetEntity.toShortString(), "parentAsset"),
|
||||
HsHostingAssetEntityValidatorRegistry.forType(assetEntity.getType()).validateContext(assetEntity))
|
||||
: emptyList();
|
||||
}
|
||||
|
||||
private static List<String> optionallyValidate(final HsBookingItemEntity bookingItem) {
|
||||
return bookingItem != null
|
||||
? enrich(prefix(bookingItem.toShortString(), "bookingItem"),
|
||||
? enrich(
|
||||
prefix(bookingItem.toShortString(), "bookingItem"),
|
||||
HsBookingItemEntityValidatorRegistry.forType(bookingItem.getType()).validateContext(bookingItem))
|
||||
: emptyList();
|
||||
}
|
||||
|
||||
protected List<String> validateAgainstSubEntities(final HsHostingAssetEntity assetEntity) {
|
||||
return enrich(prefix(assetEntity.toShortString(), "config"),
|
||||
return enrich(
|
||||
prefix(assetEntity.toShortString(), "config"),
|
||||
stream(propertyValidators)
|
||||
.filter(ValidatableProperty::isTotalsValidator)
|
||||
.map(prop -> validateMaxTotalValue(assetEntity, prop))
|
||||
@ -140,7 +142,9 @@ public abstract class HsHostingAssetEntityValidator extends HsEntityValidator<Hs
|
||||
final var expectedIdentifierPattern = identifierPattern(assetEntity);
|
||||
if (assetEntity.getIdentifier() == null ||
|
||||
!expectedIdentifierPattern.matcher(assetEntity.getIdentifier()).matches()) {
|
||||
return List.of("'identifier' expected to match '"+expectedIdentifierPattern+"', but is '" + assetEntity.getIdentifier() + "'");
|
||||
return List.of(
|
||||
"'identifier' expected to match '" + expectedIdentifierPattern + "', but is '" + assetEntity.getIdentifier()
|
||||
+ "'");
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@ -150,42 +154,54 @@ public abstract class HsHostingAssetEntityValidator extends HsEntityValidator<Hs
|
||||
static class ReferenceValidator<S, T> {
|
||||
|
||||
private final HsHostingAssetType.RelationPolicy policy;
|
||||
private final T subEntityType;
|
||||
private final Function<HsHostingAssetEntity, S> subEntityGetter;
|
||||
private final Function<S,T> subEntityTypeGetter;
|
||||
private final T referencedEntityType;
|
||||
private final Function<HsHostingAssetEntity, S> referencedEntityGetter;
|
||||
private final Function<S, T> referencedEntityTypeGetter;
|
||||
|
||||
public ReferenceValidator(
|
||||
final HsHostingAssetType.RelationPolicy policy,
|
||||
final T subEntityType,
|
||||
final Function<HsHostingAssetEntity, S> subEntityGetter,
|
||||
final Function<S, T> subEntityTypeGetter) {
|
||||
final Function<HsHostingAssetEntity, S> referencedEntityGetter,
|
||||
final Function<S, T> referencedEntityTypeGetter) {
|
||||
this.policy = policy;
|
||||
this.subEntityType = subEntityType;
|
||||
this.subEntityGetter = subEntityGetter;
|
||||
this.subEntityTypeGetter = subEntityTypeGetter;
|
||||
this.referencedEntityType = subEntityType;
|
||||
this.referencedEntityGetter = referencedEntityGetter;
|
||||
this.referencedEntityTypeGetter = referencedEntityTypeGetter;
|
||||
}
|
||||
|
||||
public ReferenceValidator(
|
||||
final HsHostingAssetType.RelationPolicy policy,
|
||||
final Function<HsHostingAssetEntity, S> subEntityGetter) {
|
||||
final Function<HsHostingAssetEntity, S> referencedEntityGetter) {
|
||||
this.policy = policy;
|
||||
this.subEntityType = null;
|
||||
this.subEntityGetter = subEntityGetter;
|
||||
this.subEntityTypeGetter = e -> null;
|
||||
this.referencedEntityType = null;
|
||||
this.referencedEntityGetter = referencedEntityGetter;
|
||||
this.referencedEntityTypeGetter = e -> null;
|
||||
}
|
||||
|
||||
List<String> validate(final HsHostingAssetEntity assetEntity, final String referenceFieldName) {
|
||||
|
||||
final var subEntity = subEntityGetter.apply(assetEntity);
|
||||
if (policy == REQUIRED && subEntity == null) {
|
||||
return List.of(referenceFieldName + "' must not be null but is null");
|
||||
final var actualEntity = referencedEntityGetter.apply(assetEntity);
|
||||
final var actualEntityType = actualEntity != null ? referencedEntityTypeGetter.apply(actualEntity) : null;
|
||||
|
||||
switch (policy) {
|
||||
case REQUIRED:
|
||||
if (actualEntityType != referencedEntityType) {
|
||||
return List.of(actualEntityType == null
|
||||
? referenceFieldName + "' must be of type " + referencedEntityType + " but is null"
|
||||
: referenceFieldName + "' must be of type " + referencedEntityType + " but is of type " + actualEntityType);
|
||||
}
|
||||
if ((policy == FORBIDDEN) && (subEntity != null)) {
|
||||
return List.of(referenceFieldName + "' must be null but is set to "+ assetEntity.getBookingItem().toShortString());
|
||||
break;
|
||||
case OPTIONAL:
|
||||
if (actualEntityType != null && actualEntityType != referencedEntityType) {
|
||||
return List.of(referenceFieldName + "' must be null or of type " + referencedEntityType + " but is of type "
|
||||
+ actualEntityType);
|
||||
}
|
||||
final var subItemType = subEntity != null ? subEntityTypeGetter.apply(subEntity) : null;
|
||||
if (policy != OPTIONAL && subEntityType != null && subItemType != subEntityType) {
|
||||
return List.of(referenceFieldName + "' must be of type " + subEntityType + " but is of type " + subItemType);
|
||||
break;
|
||||
case FORBIDDEN:
|
||||
if (actualEntityType != null) {
|
||||
return List.of(referenceFieldName + "' must be null but is of type " + actualEntityType);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return emptyList();
|
||||
}
|
||||
|
@ -186,6 +186,24 @@ public class HsHostingAssetControllerRestTest {
|
||||
}
|
||||
]
|
||||
"""),
|
||||
DOMAIN_SETUP(
|
||||
List.of(
|
||||
HsHostingAssetEntity.builder()
|
||||
.type(HsHostingAssetType.DOMAIN_SETUP)
|
||||
.identifier("example.org")
|
||||
.caption("some fake Domain-Setup")
|
||||
.build()),
|
||||
"""
|
||||
[
|
||||
{
|
||||
"type": "DOMAIN_SETUP",
|
||||
"identifier": "example.org",
|
||||
"caption": "some fake Domain-Setup",
|
||||
"alarmContact": null,
|
||||
"config": {}
|
||||
}
|
||||
]
|
||||
"""),
|
||||
DOMAIN_DNS_SETUP(
|
||||
List.of(
|
||||
HsHostingAssetEntity.builder()
|
||||
|
@ -33,7 +33,7 @@ class HsCloudServerHostingAssetValidatorUnitTest {
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'CLOUD_SERVER:vm1234.bookingItem' must not be null but is null",
|
||||
"'CLOUD_SERVER:vm1234.bookingItem' must be of type CLOUD_SERVER but is null",
|
||||
"'CLOUD_SERVER:vm1234.config.RAM' is not expected but is set to '2000'");
|
||||
}
|
||||
|
||||
@ -84,14 +84,14 @@ class HsCloudServerHostingAssetValidatorUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesParentAndAssignedToAssetMustNotBeSet() {
|
||||
void rejectsInvalidReferencedEntities() {
|
||||
// given
|
||||
final var mangedServerHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(CLOUD_SERVER)
|
||||
.identifier("xyz00")
|
||||
.parentAsset(HsHostingAssetEntity.builder().build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().build())
|
||||
.identifier("vm1234")
|
||||
.bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build())
|
||||
.parentAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().type(CLOUD_SERVER).build())
|
||||
.build();
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType());
|
||||
|
||||
@ -100,7 +100,7 @@ class HsCloudServerHostingAssetValidatorUnitTest {
|
||||
|
||||
// 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");
|
||||
"'CLOUD_SERVER:vm1234.parentAsset' must be null but is of type MANAGED_SERVER",
|
||||
"'CLOUD_SERVER:vm1234.assignedToAsset' must be null but is of type CLOUD_SERVER");
|
||||
}
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest {
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly(
|
||||
"'identifier' expected to match '^example.org|DNS$', but is 'example.org'"
|
||||
"'identifier' expected to match '^example.org\\Q|DNS\\E$', but is 'example.org'"
|
||||
);
|
||||
}
|
||||
|
||||
@ -113,12 +113,12 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesReferencedEntities() {
|
||||
void rejectsInvalidReferencedEntities() {
|
||||
// given
|
||||
final var mangedServerHostingAssetEntity = validEntityBuilder()
|
||||
.parentAsset(HsHostingAssetEntity.builder().build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().build())
|
||||
.bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build())
|
||||
.parentAsset(null)
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().type(DOMAIN_SETUP).build())
|
||||
.build();
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType());
|
||||
|
||||
@ -127,9 +127,9 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest {
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'DOMAIN_DNS_SETUP:example.org|DNS.bookingItem' must be null but is set to D-???????-?:null",
|
||||
"'DOMAIN_DNS_SETUP:example.org|DNS.parentAsset' must be of type DOMAIN_SETUP but is of type null",
|
||||
"'DOMAIN_DNS_SETUP:example.org|DNS.assignedToAsset' must be null but is set to D-???????-?:null");
|
||||
"'DOMAIN_DNS_SETUP:example.org|DNS.bookingItem' must be null but is of type CLOUD_SERVER",
|
||||
"'DOMAIN_DNS_SETUP:example.org|DNS.parentAsset' must be of type DOMAIN_SETUP but is null",
|
||||
"'DOMAIN_DNS_SETUP:example.org|DNS.assignedToAsset' must be null but is of type DOMAIN_SETUP");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -0,0 +1,112 @@
|
||||
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.HsHostingAssetEntityBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_SETUP;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsDomainSetupHostingAssetValidatorUnitTest {
|
||||
|
||||
static HsHostingAssetEntityBuilder validEntityBuilder() {
|
||||
return HsHostingAssetEntity.builder()
|
||||
.type(DOMAIN_SETUP)
|
||||
.identifier("example.org");
|
||||
}
|
||||
|
||||
enum InvalidDomainNameIdentifier {
|
||||
EMPTY(""),
|
||||
TOO_LONG("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456890123456789.de"),
|
||||
DASH_AT_BEGINNING("-example.com"),
|
||||
DOT_AT_BEGINNING(".example.com"),
|
||||
DOT_AT_END("example.com.");
|
||||
|
||||
final String domainName;
|
||||
|
||||
InvalidDomainNameIdentifier(final String domainName) {
|
||||
this.domainName = domainName;
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(InvalidDomainNameIdentifier.class)
|
||||
void rejectsInvalidIdentifier(final InvalidDomainNameIdentifier testCase) {
|
||||
// given
|
||||
final var givenEntity = validEntityBuilder().identifier(testCase.domainName).build();
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType());
|
||||
|
||||
// when
|
||||
final var result = validator.validateEntity(givenEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly(
|
||||
"'identifier' expected to match '^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}', but is '"+testCase.domainName+"'"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
enum ValidDomainNameIdentifier {
|
||||
SIMPLE("exampe.org"),
|
||||
MAX_LENGTH("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz01234568901.de"),
|
||||
WITH_DASH("example-test.com"),
|
||||
SUBDOMAIN("test.example.com");
|
||||
|
||||
final String domainName;
|
||||
|
||||
ValidDomainNameIdentifier(final String domainName) {
|
||||
this.domainName = domainName;
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(ValidDomainNameIdentifier.class)
|
||||
void acceptsValidIdentifier(final ValidDomainNameIdentifier testCase) {
|
||||
// given
|
||||
final var givenEntity = validEntityBuilder().identifier(testCase.domainName).build();
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType());
|
||||
|
||||
// when
|
||||
final var result = validator.validateEntity(givenEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsNoProperties() {
|
||||
// when
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(CLOUD_SERVER);
|
||||
|
||||
// then
|
||||
assertThat(validator.properties()).map(Map::toString).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesReferencedEntities() {
|
||||
// given
|
||||
final var mangedServerHostingAssetEntity = validEntityBuilder()
|
||||
.parentAsset(HsHostingAssetEntity.builder().type(CLOUD_SERVER).build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build())
|
||||
.bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build())
|
||||
.build();
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType());
|
||||
|
||||
// when
|
||||
final var result = validator.validateEntity(mangedServerHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'DOMAIN_SETUP:example.org.bookingItem' must be null but is of type CLOUD_SERVER",
|
||||
"'DOMAIN_SETUP:example.org.parentAsset' must be null or of type DOMAIN_SETUP but is of type CLOUD_SERVER",
|
||||
"'DOMAIN_SETUP:example.org.assignedToAsset' must be null but is of type MANAGED_SERVER");
|
||||
}
|
||||
}
|
@ -107,8 +107,8 @@ class HsEMailAliasHostingAssetValidatorUnitTest {
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'EMAIL_ALIAS:abc00-office.bookingItem' must be null but is set to D-1234500:test project:test project booking item",
|
||||
"'EMAIL_ALIAS:abc00-office.bookingItem' must be null but is of type MANAGED_SERVER",
|
||||
"'EMAIL_ALIAS:abc00-office.parentAsset' must be of type MANAGED_WEBSPACE but is of type MANAGED_SERVER",
|
||||
"'EMAIL_ALIAS:abc00-office.assignedToAsset' must be null but is set to D-1234500:test project:test project booking item");
|
||||
"'EMAIL_ALIAS:abc00-office.assignedToAsset' must be null but is of type MANAGED_SERVER");
|
||||
}
|
||||
}
|
||||
|
@ -10,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.booking.item.TestHsBookingItem.TEST_MANAGED_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;
|
||||
|
||||
@ -22,8 +23,8 @@ class HsManagedServerHostingAssetValidatorUnitTest {
|
||||
.type(MANAGED_SERVER)
|
||||
.identifier("vm1234")
|
||||
.bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM)
|
||||
.parentAsset(HsHostingAssetEntity.builder().build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().build())
|
||||
.parentAsset(HsHostingAssetEntity.builder().type(CLOUD_SERVER).build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().type(CLOUD_SERVER).build())
|
||||
.config(Map.ofEntries(
|
||||
entry("monit_max_hdd_usage", "90"),
|
||||
entry("monit_max_cpu_usage", 2),
|
||||
@ -37,8 +38,8 @@ class HsManagedServerHostingAssetValidatorUnitTest {
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'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.parentAsset' must be null but is of type CLOUD_SERVER",
|
||||
"'MANAGED_SERVER:vm1234.assignedToAsset' must be null but is of type CLOUD_SERVER",
|
||||
"'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'");
|
||||
@ -63,14 +64,14 @@ class HsManagedServerHostingAssetValidatorUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesParentAndAssignedToAssetMustNotBeSet() {
|
||||
void rejectsInvalidReferencedEntities() {
|
||||
// given
|
||||
final var mangedServerHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_SERVER)
|
||||
.identifier("xyz00")
|
||||
.parentAsset(HsHostingAssetEntity.builder().build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().build())
|
||||
.bookingItem(TEST_CLOUD_SERVER_BOOKING_ITEM)
|
||||
.parentAsset(HsHostingAssetEntity.builder().type(CLOUD_SERVER).build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build())
|
||||
.build();
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType());
|
||||
|
||||
@ -80,7 +81,7 @@ class HsManagedServerHostingAssetValidatorUnitTest {
|
||||
// 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-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");
|
||||
"'MANAGED_SERVER:xyz00.parentAsset' must be null but is of type CLOUD_SERVER",
|
||||
"'MANAGED_SERVER:xyz00.assignedToAsset' must be null but is of type MANAGED_SERVER");
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static net.hostsharing.hsadminng.hs.booking.project.TestHsBookingProject.TEST_PROJECT;
|
||||
@ -142,7 +143,7 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesEntityReferences() {
|
||||
void rejectsInvalidEntityReferences() {
|
||||
// given
|
||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
@ -153,7 +154,7 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest {
|
||||
.resources(Map.ofEntries(entry("SSD", 25), entry("Traffic", 250)))
|
||||
.build())
|
||||
.parentAsset(cloudServerAssetEntity)
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().build())
|
||||
.assignedToAsset(HsHostingAssetEntity.builder().type(CLOUD_SERVER).build())
|
||||
.identifier("abc00")
|
||||
.build();
|
||||
|
||||
@ -163,7 +164,7 @@ class HsManagedWebspaceHostingAssetValidatorUnitTest {
|
||||
// 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");
|
||||
"'MANAGED_WEBSPACE:abc00.parentAsset' must be null or of type MANAGED_SERVER but is of type CLOUD_SERVER",
|
||||
"'MANAGED_WEBSPACE:abc00.assignedToAsset' must be null but is of type CLOUD_SERVER");
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import net.hostsharing.hsadminng.rbac.rbacgrant.RbacGrantsDiagramService;
|
||||
import net.hostsharing.hsadminng.rbac.rbacrole.RbacRoleEntity;
|
||||
import net.hostsharing.hsadminng.rbac.rbacrole.RbacRoleRepository;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.TestInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -170,7 +171,7 @@ public abstract class ContextBasedTestWithCleanup extends ContextBasedTest {
|
||||
this.testInfo = testInfo;
|
||||
}
|
||||
|
||||
//@AfterEach
|
||||
@AfterEach
|
||||
void cleanupAndCheckCleanup(final TestInfo testInfo) {
|
||||
// If the whole test method has its own transaction, cleanup makes no sense.
|
||||
// If that transaction even failed, cleaunup would cause an exception.
|
||||
|
Loading…
Reference in New Issue
Block a user