From c08a05c717f41f59c5dbed700ced464234eb38cc Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 9 Jul 2024 17:02:07 +0200 Subject: [PATCH] add HsDomainEMailSubmissionSetupHostingAssetValidator --- .../hs/booking/item/HsBookingItemType.java | 4 +- .../HostingAssetEntityValidatorRegistry.java | 1 + ...lSubmissionSetupHostingAssetValidator.java | 34 +++++ .../hs-hosting/hs-hosting-asset-schemas.yaml | 3 +- .../HsHostingAssetControllerRestTest.java | 22 ++- ...ingAssetPropsControllerAcceptanceTest.java | 3 +- .../asset/HsHostingAssetTypeUnitTest.java | 6 - ...gAssetEntityValidatorRegistryUnitTest.java | 3 +- ...ionSetupHostingAssetValidatorUnitTest.java | 133 ++++++++++++++++++ 9 files changed, 195 insertions(+), 14 deletions(-) create mode 100644 src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEMailSubmissionSetupHostingAssetValidator.java create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEmailSubmissionSetupHostingAssetValidatorUnitTest.java diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemType.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemType.java index 720b3ecc..8b51def8 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemType.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemType.java @@ -8,9 +8,7 @@ public enum HsBookingItemType implements Node { PRIVATE_CLOUD, CLOUD_SERVER(PRIVATE_CLOUD), MANAGED_SERVER(PRIVATE_CLOUD), - MANAGED_WEBSPACE(MANAGED_SERVER), - DOMAIN_DNS_SETUP, // TODO.spec: experimental - DOMAIN_EMAIL_SUBMISSION_SETUP; // TODO.spec: experimental + MANAGED_WEBSPACE(MANAGED_SERVER); private final HsBookingItemType parentItemType; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistry.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistry.java index 62e47dec..6efbed70 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistry.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistry.java @@ -23,6 +23,7 @@ public class HostingAssetEntityValidatorRegistry { register(DOMAIN_SETUP, new HsDomainSetupHostingAssetValidator()); register(DOMAIN_DNS_SETUP, new HsDomainDnsSetupHostingAssetValidator()); register(DOMAIN_HTTP_SETUP, new HsDomainHttpSetupHostingAssetValidator()); + register(DOMAIN_EMAIL_SUBMISSION_SETUP, new HsDomainEMailSubmissionSetupHostingAssetValidator()); } private static void register(final Enum type, final HsEntityValidator validator) { diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEMailSubmissionSetupHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEMailSubmissionSetupHostingAssetValidator.java new file mode 100644 index 00000000..aa80d332 --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEMailSubmissionSetupHostingAssetValidator.java @@ -0,0 +1,34 @@ +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_EMAIL_SUBMISSION_SETUP; + +class HsDomainEMailSubmissionSetupHostingAssetValidator extends HostingAssetEntityValidator { + + public static final String IDENTIFIER_SUFFIX = "|SMTP"; + + HsDomainEMailSubmissionSetupHostingAssetValidator() { + super( + DOMAIN_EMAIL_SUBMISSION_SETUP, + AlarmContact.isOptional(), + + NO_EXTRA_PROPERTIES); + } + + @Override + protected Pattern identifierPattern(final HsHostingAssetEntity assetEntity) { + return Pattern.compile("^" + assetEntity.getParentAsset().getIdentifier() + Pattern.quote(IDENTIFIER_SUFFIX) + "$"); + } + + @Override + public void preprocessEntity(final HsHostingAssetEntity entity) { + super.preprocessEntity(entity); + if (entity.getIdentifier() == null) { + ofNullable(entity.getParentAsset()).ifPresent(pa -> entity.setIdentifier(pa.getIdentifier() + IDENTIFIER_SUFFIX)); + } + } +} diff --git a/src/main/resources/api-definition/hs-hosting/hs-hosting-asset-schemas.yaml b/src/main/resources/api-definition/hs-hosting/hs-hosting-asset-schemas.yaml index a9ab7f64..11cb1050 100644 --- a/src/main/resources/api-definition/hs-hosting/hs-hosting-asset-schemas.yaml +++ b/src/main/resources/api-definition/hs-hosting/hs-hosting-asset-schemas.yaml @@ -13,7 +13,8 @@ components: - DOMAIN_SETUP - DOMAIN_DNS_SETUP - DOMAIN_HTTP_SETUP - - DOMAIN_EMAIL_SETUP + - DOMAIN_EMAIL_SUBMISSION_SETUP + - DOMAIN_EMAIL_MAILBOX_SETUP - EMAIL_ALIAS - EMAIL_ADDRESS - PGSQL_USER 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 bdaacf04..76c5a379 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 @@ -251,7 +251,7 @@ public class HsHostingAssetControllerRestTest { List.of( HsHostingAssetEntity.builder() .type(HsHostingAssetType.DOMAIN_HTTP_SETUP) - .identifier("example.org") + .identifier("example.org|HTTP") .caption("some fake Domain-HTTP-Setup") .config(Map.ofEntries( entry("htdocsfallback", false), @@ -276,7 +276,7 @@ public class HsHostingAssetControllerRestTest { [ { "type": "DOMAIN_HTTP_SETUP", - "identifier": "example.org", + "identifier": "example.org|HTTP", "caption": "some fake Domain-HTTP-Setup", "alarmContact": null, "config": { @@ -299,6 +299,24 @@ public class HsHostingAssetControllerRestTest { } } ] + """), + DOMAIN_EMAIL_SUBMISSION_SETUP( + List.of( + HsHostingAssetEntity.builder() + .type(HsHostingAssetType.DOMAIN_EMAIL_SUBMISSION_SETUP) + .identifier("example.org|SMTP") + .caption("some fake Domain-SMTP-Setup") + .build()), + """ + [ + { + "type": "DOMAIN_EMAIL_SUBMISSION_SETUP", + "identifier": "example.org|SMTP", + "caption": "some fake Domain-SMTP-Setup", + "alarmContact": null, + "config": {} + } + ] """); 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 dd4afc09..ac011d68 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 @@ -38,7 +38,8 @@ class HsHostingAssetPropsControllerAcceptanceTest { "EMAIL_ALIAS", "DOMAIN_SETUP", "DOMAIN_DNS_SETUP", - "DOMAIN_HTTP_SETUP" + "DOMAIN_HTTP_SETUP", + "DOMAIN_EMAIL_SUBMISSION_SETUP" ] """)); // @formatter:on diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetTypeUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetTypeUnitTest.java index 794c3f25..91d141a2 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetTypeUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetTypeUnitTest.java @@ -25,8 +25,6 @@ class HsHostingAssetTypeUnitTest { entity BI_CLOUD_SERVER entity BI_MANAGED_SERVER entity BI_MANAGED_WEBSPACE - entity BI_DOMAIN_DNS_SETUP - entity BI_DOMAIN_EMAIL_SUBMISSION_SETUP } package Hosting #feb28c{ @@ -96,8 +94,6 @@ class HsHostingAssetTypeUnitTest { entity BI_CLOUD_SERVER entity BI_MANAGED_SERVER entity BI_MANAGED_WEBSPACE - entity BI_DOMAIN_DNS_SETUP - entity BI_DOMAIN_EMAIL_SUBMISSION_SETUP } package Hosting #feb28c{ @@ -160,8 +156,6 @@ class HsHostingAssetTypeUnitTest { entity BI_CLOUD_SERVER entity BI_MANAGED_SERVER entity BI_MANAGED_WEBSPACE - entity BI_DOMAIN_DNS_SETUP - entity BI_DOMAIN_EMAIL_SUBMISSION_SETUP } package Hosting #feb28c{ diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistryUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistryUnitTest.java index daf0704f..a041742b 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistryUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntityValidatorRegistryUnitTest.java @@ -36,7 +36,8 @@ class HostingAssetEntityValidatorRegistryUnitTest { HsHostingAssetType.EMAIL_ALIAS, HsHostingAssetType.DOMAIN_SETUP, HsHostingAssetType.DOMAIN_DNS_SETUP, - HsHostingAssetType.DOMAIN_HTTP_SETUP + HsHostingAssetType.DOMAIN_HTTP_SETUP, + HsHostingAssetType.DOMAIN_EMAIL_SUBMISSION_SETUP ); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEmailSubmissionSetupHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEmailSubmissionSetupHostingAssetValidatorUnitTest.java new file mode 100644 index 00000000..57c847fc --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainEmailSubmissionSetupHostingAssetValidatorUnitTest.java @@ -0,0 +1,133 @@ +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 java.util.Map; + +import static java.util.Map.entry; +import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_EMAIL_SUBMISSION_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 org.assertj.core.api.Assertions.assertThat; + +class HsDomainEmailSubmissionSetupHostingAssetValidatorUnitTest { + + static final HsHostingAssetEntity validDomainSetupEntity = HsHostingAssetEntity.builder() + .type(DOMAIN_SETUP) + .identifier("example.org") + .build(); + + static HsHostingAssetEntityBuilder validEntityBuilder() { + return HsHostingAssetEntity.builder() + .type(DOMAIN_EMAIL_SUBMISSION_SETUP) + .parentAsset(validDomainSetupEntity) + .assignedToAsset(HsHostingAssetEntity.builder().type(MANAGED_WEBSPACE).build()) + .identifier("example.org|SMTP"); + } + + @Test + void containsExpectedProperties() { + // when + final var validator = HostingAssetEntityValidatorRegistry.forType(DOMAIN_EMAIL_SUBMISSION_SETUP); + + // then + assertThat(validator.properties()).map(Map::toString).isEmpty(); + } + + @Test + void preprocessesTakesIdentifierFromParent() { + // given + final var givenEntity = validEntityBuilder().build(); + assertThat(givenEntity.getParentAsset().getIdentifier()).as("precondition failed").isEqualTo("example.org"); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + + // when + validator.preprocessEntity(givenEntity); + + // then + assertThat(givenEntity.getIdentifier()).isEqualTo("example.org|SMTP"); + } + + @Test + void rejectsInvalidIdentifier() { + // given + final var givenEntity = validEntityBuilder().identifier("example.org").build(); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + + // when + final var result = validator.validateEntity(givenEntity); + + // then + assertThat(result).containsExactly( + "'identifier' expected to match '^example.org\\Q|SMTP\\E$', but is 'example.org'" + ); + } + + @Test + void acceptsValidIdentifier() { + // given + final var givenEntity = validEntityBuilder().identifier(validDomainSetupEntity.getIdentifier()+"|SMTP").build(); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + + // when + final var result = validator.validateEntity(givenEntity); + + // then + assertThat(result).isEmpty(); + } + + @Test + void rejectsInvalidReferencedEntities() { + // given + final var mangedServerHostingAssetEntity = validEntityBuilder() + .bookingItem(HsBookingItemEntity.builder().type(HsBookingItemType.CLOUD_SERVER).build()) + .parentAsset(HsHostingAssetEntity.builder().type(MANAGED_WEBSPACE).build()) + .assignedToAsset(null) + .build(); + final var validator = HostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + + // when + final var result = validator.validateEntity(mangedServerHostingAssetEntity); + + // then + assertThat(result).containsExactlyInAnyOrder( + "'DOMAIN_EMAIL_SUBMISSION_SETUP:example.org|SMTP.bookingItem' must be null but is of type CLOUD_SERVER", + "'DOMAIN_EMAIL_SUBMISSION_SETUP:example.org|SMTP.parentAsset' must be of type DOMAIN_SETUP but is of type MANAGED_WEBSPACE", + "'DOMAIN_EMAIL_SUBMISSION_SETUP:example.org|SMTP.assignedToAsset' must be of type MANAGED_WEBSPACE but is null"); + } + + @Test + void acceptsValidEntity() { + // given + final var givenEntity = validEntityBuilder().build(); + final var validator = HostingAssetEntityValidatorRegistry.forType(givenEntity.getType()); + + // when + final var errors = validator.validateEntity(givenEntity); + + // then + assertThat(errors).isEmpty(); + } + + @Test + void rejectsInvalidProperties() { + // given + final var mangedServerHostingAssetEntity = validEntityBuilder() + .config(Map.ofEntries( + entry("any", "false") + )) + .build(); + final var validator = HostingAssetEntityValidatorRegistry.forType(mangedServerHostingAssetEntity.getType()); + + // when + final var result = validator.validateEntity(mangedServerHostingAssetEntity); + + // then + assertThat(result).containsExactlyInAnyOrder( + "'DOMAIN_EMAIL_SUBMISSION_SETUP:example.org|SMTP.config.any' is not expected but is set to 'false'"); + } +}