From 0af389d7c67fa684e8802c2e2c4a2e7ef606f41f Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 10 Jul 2024 15:54:02 +0200 Subject: [PATCH] add-domain-http-setup-validation (#73) Co-authored-by: Michael Hoennig Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/73 Reviewed-by: Timotheus Pokorra --- .../asset/HsHostingAssetController.java | 10 +- .../asset/HsHostingAssetPropsController.java | 6 +- ...a => HostingAssetEntitySaveProcessor.java} | 18 +- ....java => HostingAssetEntityValidator.java} | 8 +- ... HostingAssetEntityValidatorRegistry.java} | 3 +- .../HsCloudServerHostingAssetValidator.java | 2 +- ...HsDomainDnsSetupHostingAssetValidator.java | 2 +- ...sDomainHttpSetupHostingAssetValidator.java | 56 ++++++ .../HsDomainSetupHostingAssetValidator.java | 6 +- .../HsEMailAliasHostingAssetValidator.java | 2 +- .../HsManagedServerHostingAssetValidator.java | 2 +- ...sManagedWebspaceHostingAssetValidator.java | 2 +- .../HsUnixUserHostingAssetValidator.java | 2 +- .../hs/validation/StringProperty.java | 11 +- .../hs/validation/ValidatableProperty.java | 4 +- .../HsHostingAssetControllerRestTest.java | 53 ++++++ ...ingAssetPropsControllerAcceptanceTest.java | 3 +- ...AssetEntityValidatorRegistryUnitTest.java} | 9 +- ...udServerHostingAssetValidatorUnitTest.java | 10 +- ...DnsSetupHostingAssetValidatorUnitTest.java | 22 +-- ...ttpSetupHostingAssetValidatorUnitTest.java | 163 ++++++++++++++++++ ...ainSetupHostingAssetValidatorUnitTest.java | 8 +- ...ailAliasHostingAssetValidatorUnitTest.java | 10 +- ...edServerHostingAssetValidatorUnitTest.java | 8 +- ...WebspaceHostingAssetValidatorUnitTest.java | 10 +- ...UnixUserHostingAssetValidatorUnitTest.java | 14 +- 26 files changed, 363 insertions(+), 81 deletions(-) rename src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/{HsHostingAssetEntityProcessor.java => HostingAssetEntitySaveProcessor.java} (81%) rename src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/{HsHostingAssetEntityValidator.java => HostingAssetEntityValidator.java} (96%) rename src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/{HsHostingAssetEntityValidatorRegistry.java => HostingAssetEntityValidatorRegistry.java} (94%) create mode 100644 src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidator.java rename src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/{HsHostingAssetEntityValidatorRegistryUnitTest.java => HostingAssetEntityValidatorRegistryUnitTest.java} (80%) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidatorUnitTest.java diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java index 6e082c05..3ca7efff 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java @@ -1,8 +1,8 @@ package net.hostsharing.hsadminng.hs.hosting.asset; import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemRepository; -import net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityProcessor; -import net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry; +import net.hostsharing.hsadminng.hs.hosting.asset.validators.HostingAssetEntitySaveProcessor; +import net.hostsharing.hsadminng.hs.hosting.asset.validators.HostingAssetEntityValidatorRegistry; import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.api.HsHostingAssetsApi; import net.hostsharing.hsadminng.context.Context; @@ -72,7 +72,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi { final var entity = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); - final var mapped = new HsHostingAssetEntityProcessor(entity) + final var mapped = new HostingAssetEntitySaveProcessor(entity) .preprocessEntity() .validateEntity() .prepareForSave() @@ -133,7 +133,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi { new HsHostingAssetEntityPatcher(em, entity).apply(body); - final var mapped = new HsHostingAssetEntityProcessor(entity) + final var mapped = new HostingAssetEntitySaveProcessor(entity) .preprocessEntity() .validateEntity() .prepareForSave() @@ -161,6 +161,6 @@ public class HsHostingAssetController implements HsHostingAssetsApi { @SuppressWarnings("unchecked") final BiConsumer ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) - -> resource.setConfig(HsHostingAssetEntityValidatorRegistry.forType(entity.getType()) + -> resource.setConfig(HostingAssetEntityValidatorRegistry.forType(entity.getType()) .revampProperties(entity, (Map) resource.getConfig())); } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsController.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsController.java index 0da530bd..ca8bbb08 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsController.java @@ -1,6 +1,6 @@ package net.hostsharing.hsadminng.hs.hosting.asset; -import net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry; +import net.hostsharing.hsadminng.hs.hosting.asset.validators.HostingAssetEntityValidatorRegistry; import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.api.HsHostingAssetPropsApi; import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.model.HsHostingAssetTypeResource; import org.springframework.http.ResponseEntity; @@ -15,7 +15,7 @@ public class HsHostingAssetPropsController implements HsHostingAssetPropsApi { @Override public ResponseEntity> listAssetTypes() { - final var resource = HsHostingAssetEntityValidatorRegistry.types().stream() + final var resource = HostingAssetEntityValidatorRegistry.types().stream() .map(Enum::name) .toList(); return ResponseEntity.ok(resource); @@ -26,7 +26,7 @@ public class HsHostingAssetPropsController implements HsHostingAssetPropsApi { final HsHostingAssetTypeResource assetType) { final Enum type = HsHostingAssetType.of(assetType); - final var propValidators = HsHostingAssetEntityValidatorRegistry.forType(type); + final var propValidators = HostingAssetEntityValidatorRegistry.forType(type); final List> resource = propValidators.properties(); return ResponseEntity.ok(toListOfObjects(resource)); } 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/HostingAssetEntitySaveProcessor.java similarity index 81% rename from src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityProcessor.java rename to src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntitySaveProcessor.java index cc192bc7..189b3314 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/HostingAssetEntitySaveProcessor.java @@ -11,27 +11,27 @@ import java.util.function.Function; /** * Wraps the steps of the pararation, validation, mapping and revamp around saving of a HsHostingAssetEntity into a readable API. */ -public class HsHostingAssetEntityProcessor { +public class HostingAssetEntitySaveProcessor { private final HsEntityValidator validator; private String expectedStep = "preprocessEntity"; private HsHostingAssetEntity entity; private HsHostingAssetResource resource; - public HsHostingAssetEntityProcessor(final HsHostingAssetEntity entity) { + public HostingAssetEntitySaveProcessor(final HsHostingAssetEntity entity) { this.entity = entity; - this.validator = HsHostingAssetEntityValidatorRegistry.forType(entity.getType()); + this.validator = HostingAssetEntityValidatorRegistry.forType(entity.getType()); } /// initial step allowing to set default values before any validations - public HsHostingAssetEntityProcessor preprocessEntity() { + public HostingAssetEntitySaveProcessor preprocessEntity() { step("preprocessEntity", "validateEntity"); validator.preprocessEntity(entity); return this; } /// validates the entity itself including its properties - public HsHostingAssetEntityProcessor validateEntity() { + public HostingAssetEntitySaveProcessor validateEntity() { step("validateEntity", "prepareForSave"); MultiValidationException.throwIfNotEmpty(validator.validateEntity(entity)); return this; @@ -39,27 +39,27 @@ public class HsHostingAssetEntityProcessor { /// hashing passwords etc. @SuppressWarnings("unchecked") - public HsHostingAssetEntityProcessor prepareForSave() { + public HostingAssetEntitySaveProcessor prepareForSave() { step("prepareForSave", "saveUsing"); validator.prepareProperties(entity); return this; } - public HsHostingAssetEntityProcessor saveUsing(final Function saveFunction) { + public HostingAssetEntitySaveProcessor saveUsing(final Function saveFunction) { step("saveUsing", "validateContext"); entity = saveFunction.apply(entity); return this; } /// validates the entity within it's parent and child hierarchy (e.g. totals validators and other limits) - public HsHostingAssetEntityProcessor validateContext() { + public HostingAssetEntitySaveProcessor validateContext() { step("validateContext", "mapUsing"); MultiValidationException.throwIfNotEmpty(validator.validateContext(entity)); return this; } /// maps entity to JSON resource representation - public HsHostingAssetEntityProcessor mapUsing( + public HostingAssetEntitySaveProcessor mapUsing( final Function mapFunction) { step("mapUsing", "revampProperties"); resource = mapFunction.apply(entity); 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/HostingAssetEntityValidator.java similarity index 96% rename from src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidator.java rename to src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidator.java index 187630fb..0c0282e0 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/HostingAssetEntityValidator.java @@ -21,16 +21,16 @@ import static java.util.Arrays.stream; import static java.util.Collections.emptyList; import static java.util.Optional.ofNullable; -public abstract class HsHostingAssetEntityValidator extends HsEntityValidator { +public abstract class HostingAssetEntityValidator extends HsEntityValidator { static final ValidatableProperty[] NO_EXTRA_PROPERTIES = new ValidatableProperty[0]; private final ReferenceValidator bookingItemReferenceValidation; private final ReferenceValidator parentAssetReferenceValidation; private final ReferenceValidator assignedToAssetReferenceValidation; - private final HsHostingAssetEntityValidator.AlarmContact alarmContactValidation; + private final HostingAssetEntityValidator.AlarmContact alarmContactValidation; - HsHostingAssetEntityValidator( + HostingAssetEntityValidator( final HsHostingAssetType assetType, final AlarmContact alarmContactValidation, final ValidatableProperty... properties) { @@ -98,7 +98,7 @@ public abstract class HsHostingAssetEntityValidator extends HsEntityValidator, HsEntityValidator> validators = new HashMap<>(); static { @@ -22,6 +22,7 @@ public class HsHostingAssetEntityValidatorRegistry { register(EMAIL_ALIAS, new HsEMailAliasHostingAssetValidator()); register(DOMAIN_SETUP, new HsDomainSetupHostingAssetValidator()); register(DOMAIN_DNS_SETUP, new HsDomainDnsSetupHostingAssetValidator()); + register(DOMAIN_HTTP_SETUP, new HsDomainHttpSetupHostingAssetValidator()); } private static void register(final Enum type, final HsEntityValidator validator) { diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidator.java index 9413dcf2..840e5841 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsCloudServerHostingAssetValidator.java @@ -6,7 +6,7 @@ import java.util.regex.Pattern; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER; -class HsCloudServerHostingAssetValidator extends HsHostingAssetEntityValidator { +class HsCloudServerHostingAssetValidator extends HostingAssetEntityValidator { HsCloudServerHostingAssetValidator() { super( diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java index c263be60..06e8b72a 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java @@ -15,7 +15,7 @@ import static net.hostsharing.hsadminng.hs.validation.BooleanProperty.booleanPro import static net.hostsharing.hsadminng.hs.validation.IntegerProperty.integerProperty; import static net.hostsharing.hsadminng.hs.validation.StringProperty.stringProperty; -class HsDomainDnsSetupHostingAssetValidator extends HsHostingAssetEntityValidator { +class HsDomainDnsSetupHostingAssetValidator extends HostingAssetEntityValidator { // according to RFC 1035 (section 5) and RFC 1034 static final String RR_REGEX_NAME = "([a-z0-9\\.-]+|@)\\s+"; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidator.java new file mode 100644 index 00000000..9065f7d9 --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidator.java @@ -0,0 +1,56 @@ +package net.hostsharing.hsadminng.hs.hosting.asset.validators; + +import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity; + +import java.util.regex.Pattern; + +import static java.util.Optional.ofNullable; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_HTTP_SETUP; +import static net.hostsharing.hsadminng.hs.validation.ArrayProperty.arrayOf; +import static net.hostsharing.hsadminng.hs.validation.BooleanProperty.booleanProperty; +import static net.hostsharing.hsadminng.hs.validation.StringProperty.stringProperty; + +class HsDomainHttpSetupHostingAssetValidator extends HostingAssetEntityValidator { + + public static final String IDENTIFIER_SUFFIX = "|HTTP"; + public static final String FILESYSTEM_PATH = "^/"; + public static final String PARTIAL_DOMAIN_NAME_REGEX = "(?!-)[A-Za-z0-9-]{1,63}(? entity.setIdentifier(pa.getIdentifier() + IDENTIFIER_SUFFIX)); + } + } +} diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java index e16b1356..483472a7 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java @@ -7,9 +7,9 @@ import java.util.regex.Pattern; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_SETUP; -class HsDomainSetupHostingAssetValidator extends HsHostingAssetEntityValidator { +class HsDomainSetupHostingAssetValidator extends HostingAssetEntityValidator { - public static final String DOMAIN_NAME_REGEX = "^((?!-)[A-Za-z0-9-]{1,63}(?> extends ValidatableProp protected static final String[] KEY_ORDER = Array.join( ValidatableProperty.KEY_ORDER_HEAD, - Array.of("matchesRegEx", "minLength", "maxLength"), + Array.of("matchesRegEx", "minLength", "maxLength", "provided"), ValidatableProperty.KEY_ORDER_TAIL, Array.of("undisclosed")); + private String[] provided; private Pattern[] matchesRegEx; private Integer minLength; private Integer maxLength; @@ -50,6 +51,12 @@ public class StringProperty

> extends ValidatableProp return self(); } + /// predifined values, similar to fixed values in a combobox + public P provided(final String... provided) { + this.provided = provided; + return self(); + } + /** * The property value is not disclosed in error messages. * @@ -70,7 +77,7 @@ public class StringProperty

> extends ValidatableProp } if (matchesRegEx != null && stream(matchesRegEx).map(p -> p.matcher(propValue)).noneMatch(Matcher::matches)) { - result.add(propertyName + "' is expected to match any of " + Arrays.toString(matchesRegEx) + " but " + display(propValue) + " does not match any"); + result.add(propertyName + "' is expected to match any of " + Arrays.toString(matchesRegEx) + " but " + display(propValue) + " does not match" + (matchesRegEx.length>1?" any":"")); } if (isReadOnly() && propValue != null) { result.add(propertyName + "' is readonly but given as " + display(propValue)); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/validation/ValidatableProperty.java b/src/main/java/net/hostsharing/hsadminng/hs/validation/ValidatableProperty.java index 346ee08b..01daf6aa 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/validation/ValidatableProperty.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/validation/ValidatableProperty.java @@ -182,8 +182,8 @@ protected void setDeferredInit(final Function[], T[]> //noinspection unchecked validate(result, (T) propValue, propsProvider); } else { - result.add(propertyName + "' is expected to be of type " + type + ", " + - "but is of type '" + propValue.getClass().getSimpleName() + "'"); + result.add(propertyName + "' is expected to be of type " + type.getSimpleName() + ", " + + "but is of type " + propValue.getClass().getSimpleName() + ""); } } return result; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java index eed85585..bdaacf04 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java @@ -246,6 +246,59 @@ public class HsHostingAssetControllerRestTest { } } ] + """), + DOMAIN_HTTP_SETUP( + List.of( + HsHostingAssetEntity.builder() + .type(HsHostingAssetType.DOMAIN_HTTP_SETUP) + .identifier("example.org") + .caption("some fake Domain-HTTP-Setup") + .config(Map.ofEntries( + entry("htdocsfallback", false), + entry("indexes", false), + entry("cgi", false), + entry("passenger", false), + entry("passenger-errorpage", true), + entry("fastcgi", false), + entry("autoconfig", false), + entry("greylisting", false), + entry("includes", false), + entry("letsencrypt", false), + entry("multiviews", false), + entry("fcgi-php-bin", "/usr/lib/cgi-bin/php8"), + entry("passenger-nodejs", "/usr/bin/node-js7"), + entry("passenger-python", "/usr/bin/python6"), + entry("passenger-ruby", "/usr/bin/ruby5"), + entry("subdomains", Array.of("www", "test1", "test2")) + )) + .build()), + """ + [ + { + "type": "DOMAIN_HTTP_SETUP", + "identifier": "example.org", + "caption": "some fake Domain-HTTP-Setup", + "alarmContact": null, + "config": { + "autoconfig": false, + "cgi": false, + "fastcgi": false, + "greylisting": false, + "htdocsfallback": false, + "includes": false, + "indexes": false, + "letsencrypt": false, + "multiviews": false, + "passenger": false, + "passenger-errorpage": true, + "passenger-nodejs": "/usr/bin/node-js7", + "passenger-python": "/usr/bin/python6", + "passenger-ruby": "/usr/bin/ruby5", + "fcgi-php-bin": "/usr/lib/cgi-bin/php8", + "subdomains": ["www","test1","test2"] + } + } + ] """); final HsHostingAssetType assetType; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsControllerAcceptanceTest.java index bd571075..dd4afc09 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropsControllerAcceptanceTest.java @@ -37,7 +37,8 @@ class HsHostingAssetPropsControllerAcceptanceTest { "UNIX_USER", "EMAIL_ALIAS", "DOMAIN_SETUP", - "DOMAIN_DNS_SETUP" + "DOMAIN_DNS_SETUP", + "DOMAIN_HTTP_SETUP" ] """)); // @formatter:on diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistryUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistryUnitTest.java similarity index 80% rename from src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistryUnitTest.java rename to src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistryUnitTest.java index 4e4abae9..daf0704f 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistryUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistryUnitTest.java @@ -6,13 +6,13 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -class HsHostingAssetEntityValidatorRegistryUnitTest { +class HostingAssetEntityValidatorRegistryUnitTest { @Test void forTypeWithUnknownTypeThrowsException() { // when final var thrown = catchThrowable(() -> { - HsHostingAssetEntityValidatorRegistry.forType(null); + HostingAssetEntityValidatorRegistry.forType(null); }); // then @@ -22,7 +22,7 @@ class HsHostingAssetEntityValidatorRegistryUnitTest { @Test void typesReturnsAllImplementedTypes() { // when - final var types = HsHostingAssetEntityValidatorRegistry.types(); + final var types = HostingAssetEntityValidatorRegistry.types(); // then // TODO.test: when all types are implemented, replace with set of all types: @@ -35,7 +35,8 @@ class HsHostingAssetEntityValidatorRegistryUnitTest { HsHostingAssetType.UNIX_USER, HsHostingAssetType.EMAIL_ALIAS, HsHostingAssetType.DOMAIN_SETUP, - HsHostingAssetType.DOMAIN_DNS_SETUP + HsHostingAssetType.DOMAIN_DNS_SETUP, + HsHostingAssetType.DOMAIN_HTTP_SETUP ); } } 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 8361c5f4..b7e3516a 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 @@ -25,7 +25,7 @@ class HsCloudServerHostingAssetValidatorUnitTest { entry("RAM", 2000) )) .build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(cloudServerHostingAssetEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(cloudServerHostingAssetEntity.getType()); // when @@ -45,7 +45,7 @@ class HsCloudServerHostingAssetValidatorUnitTest { .identifier("xyz99") .bookingItem(TEST_CLOUD_SERVER_BOOKING_ITEM) .build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(cloudServerHostingAssetEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(cloudServerHostingAssetEntity.getType()); // when @@ -59,7 +59,7 @@ class HsCloudServerHostingAssetValidatorUnitTest { @Test void containsAllValidations() { // when - final var validator = HsHostingAssetEntityValidatorRegistry.forType(CLOUD_SERVER); + final var validator = HostingAssetEntityValidatorRegistry.forType(CLOUD_SERVER); // then assertThat(validator.properties()).map(Map::toString).isEmpty(); @@ -73,7 +73,7 @@ class HsCloudServerHostingAssetValidatorUnitTest { .identifier("xyz00") .bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build()) .build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); // when final var result = validator.validateEntity(mangedServerHostingAssetEntity); @@ -93,7 +93,7 @@ class HsCloudServerHostingAssetValidatorUnitTest { .parentAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build()) .assignedToAsset(HsHostingAssetEntity.builder().type(CLOUD_SERVER).build()) .build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); // when final var result = validator.validateEntity(mangedServerHostingAssetEntity); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java index 681196ae..715138ec 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java @@ -46,7 +46,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { @Test void containsExpectedProperties() { // when - final var validator = HsHostingAssetEntityValidatorRegistry.forType(DOMAIN_DNS_SETUP); + final var validator = HostingAssetEntityValidatorRegistry.forType(DOMAIN_DNS_SETUP); // then assertThat(validator.properties()).map(Map::toString).containsExactlyInAnyOrder( @@ -75,7 +75,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { // given final var givenEntity = validEntityBuilder().build(); assertThat(givenEntity.getParentAsset().getIdentifier()).as("preconditon failed").isEqualTo("example.org"); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); // when validator.preprocessEntity(givenEntity); @@ -88,7 +88,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { void rejectsInvalidIdentifier() { // given final var givenEntity = validEntityBuilder().identifier("example.org").build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); // when final var result = validator.validateEntity(givenEntity); @@ -103,7 +103,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { void acceptsValidIdentifier() { // given final var givenEntity = validEntityBuilder().identifier(validDomainSetupEntity.getIdentifier()+"|DNS").build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); // when final var result = validator.validateEntity(givenEntity); @@ -120,7 +120,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { .parentAsset(null) .assignedToAsset(HsHostingAssetEntity.builder().type(DOMAIN_SETUP).build()) .build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); // when final var result = validator.validateEntity(mangedServerHostingAssetEntity); @@ -136,7 +136,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { void acceptsValidEntity() { // given final var givenEntity = validEntityBuilder().build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); // when final var errors = validator.validateEntity(givenEntity); @@ -146,7 +146,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { } @Test - void recectsInvalidProperties() { + void rejectsInvalidProperties() { // given final var mangedServerHostingAssetEntity = validEntityBuilder() .config(Map.ofEntries( @@ -156,14 +156,14 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { "www BAD1 Record-Class missing / not enough columns")) )) .build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); // when final var result = validator.validateEntity(mangedServerHostingAssetEntity); // then assertThat(result).containsExactlyInAnyOrder( - "'DOMAIN_DNS_SETUP:example.org|DNS.config.TTL' is expected to be of type class java.lang.Integer, but is of type 'String'", + "'DOMAIN_DNS_SETUP:example.org|DNS.config.TTL' is expected to be of type Integer, but is of type String", "'DOMAIN_DNS_SETUP:example.org|DNS.config.user-RR' is expected to match any of [([a-z0-9\\.-]+|@)\\s+(([1-9][0-9]*[mMhHdDwW]{0,1})+\\s+)*IN\\s+[A-Z]+\\s+[^;].*(;.*)*, ([a-z0-9\\.-]+|@)\\s+IN\\s+(([1-9][0-9]*[mMhHdDwW]{0,1})+\\s+)*[A-Z]+\\s+[^;].*(;.*)*] but '@ 1814400 IN 1814400 BAD1 TTL only allowed once' does not match any", "'DOMAIN_DNS_SETUP:example.org|DNS.config.user-RR' is expected to match any of [([a-z0-9\\.-]+|@)\\s+(([1-9][0-9]*[mMhHdDwW]{0,1})+\\s+)*IN\\s+[A-Z]+\\s+[^;].*(;.*)*, ([a-z0-9\\.-]+|@)\\s+IN\\s+(([1-9][0-9]*[mMhHdDwW]{0,1})+\\s+)*[A-Z]+\\s+[^;].*(;.*)*] but 'www BAD1 Record-Class missing / not enough columns' does not match any"); } @@ -200,7 +200,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { void generatesZonefile() { // given final var givenEntity = validEntityBuilder().build(); - final var validator = (HsDomainDnsSetupHostingAssetValidator) HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + final var validator = (HsDomainDnsSetupHostingAssetValidator) HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); // when final var zonefile = validator.toZonefileString(givenEntity); @@ -231,7 +231,7 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { )) )) .build(); - final var validator = HsHostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); // when final var errors = validator.validateContext(givenEntity); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidatorUnitTest.java new file mode 100644 index 00000000..c84dd2b1 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainHttpSetupHostingAssetValidatorUnitTest.java @@ -0,0 +1,163 @@ +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 net.hostsharing.hsadminng.mapper.Array; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static java.util.Map.entry; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_HTTP_SETUP; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_SETUP; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.UNIX_USER; +import static org.assertj.core.api.Assertions.assertThat; + +class HsDomainHttpSetupHostingAssetValidatorUnitTest { + + static final HsHostingAssetEntity validDomainSetupEntity = HsHostingAssetEntity.builder() + .type(DOMAIN_SETUP) + .identifier("example.org") + .build(); + + static HsHostingAssetEntityBuilder validEntityBuilder() { + return HsHostingAssetEntity.builder() + .type(DOMAIN_HTTP_SETUP) + .parentAsset(validDomainSetupEntity) + .assignedToAsset(HsHostingAssetEntity.builder().type(UNIX_USER).build()) + .identifier("example.org|HTTP") + .config(Map.ofEntries( + entry("passenger-errorpage", true), + entry("subdomains", Array.of("www", "test") + ) + )); + } + + @Test + void containsExpectedProperties() { + // when + final var validator = HostingAssetEntityValidatorRegistry.forType(DOMAIN_HTTP_SETUP); + + // then + assertThat(validator.properties()).map(Map::toString).containsExactlyInAnyOrder( + "{type=boolean, propertyName=htdocsfallback, defaultValue=true}", + "{type=boolean, propertyName=indexes, defaultValue=true}", + "{type=boolean, propertyName=cgi, defaultValue=true}", + "{type=boolean, propertyName=passenger, defaultValue=true}", + "{type=boolean, propertyName=passenger-errorpage}", + "{type=boolean, propertyName=fastcgi, defaultValue=true}", + "{type=boolean, propertyName=autoconfig, defaultValue=true}", + "{type=boolean, propertyName=greylisting, defaultValue=true}", + "{type=boolean, propertyName=includes, defaultValue=true}", + "{type=boolean, propertyName=letsencrypt, defaultValue=true}", + "{type=boolean, propertyName=multiviews, defaultValue=true}", + "{type=string, propertyName=fcgi-php-bin, matchesRegEx=[^/], provided=[/usr/lib/cgi-bin/php], defaultValue=/usr/lib/cgi-bin/php}", + "{type=string, propertyName=passenger-nodejs, matchesRegEx=[^/], provided=[/usr/bin/node], defaultValue=/usr/bin/node}", + "{type=string, propertyName=passenger-python, matchesRegEx=[^/], provided=[/usr/bin/python3], defaultValue=/usr/bin/python3}", + "{type=string, propertyName=passenger-ruby, matchesRegEx=[^/], provided=[/usr/bin/ruby], defaultValue=/usr/bin/ruby}", + "{type=string[], propertyName=subdomains, elementsOf={type=string, propertyName=subdomains, matchesRegEx=[(?!-)[A-Za-z0-9-]{1,63}(?