add-domain-email-setup-validation #74

Merged
hsh-michaelhoennig merged 8 commits from add-domain-email-setup-validation into master 2024-07-11 10:43:48 +02:00
9 changed files with 195 additions and 14 deletions
Showing only changes of commit c08a05c717 - Show all commits

View File

@ -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;

View File

@ -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<HsHostingAssetType> type, final HsEntityValidator<HsHostingAssetEntity> validator) {

View File

@ -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));
}
}
}

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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{

View File

@ -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
);
}
}

View File

@ -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'");
}
}