Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(ses): build not-null violations with SmithyValidationErrors (#6746)
Kenshin Okinaka24d989cb07ab
diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.javaindex 713705f940..298818725a 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.java@@ -162,16 +162,18 @@ public class SesAccountService {String useCaseDescription, List<String> additionalContacts) {List<String> errors = new ArrayList<>();if (mailType == null) {- errors.add(constraint("mailType", "Member must not be null"));+ errors.add(SmithyValidationErrors.notNullWithoutValue("mailType"));} else if (!MAIL_TYPES.contains(mailType)) {errors.add(constraint("mailType", "Member must satisfy enum value set: [MARKETING, TRANSACTIONAL]"));}if (websiteUrl == null) {- errors.add(constraint("websiteURL", "Member must not be null"));- } else if (websiteUrl.isEmpty()) {- errors.add(constraint("websiteURL", "Member must have length greater than or equal to 1"));- } else if (websiteUrl.length() > WEBSITE_URL_MAX) {- errors.add(constraint("websiteURL", "Member must have length less than or equal to " + WEBSITE_URL_MAX));+ errors.add(SmithyValidationErrors.notNullWithoutValue("websiteURL"));+ } else {+ String websiteUrlLength = SmithyValidationErrors.sizeOutOfRange("websiteURL", websiteUrl.length(),+ 1, WEBSITE_URL_MAX);+ if (websiteUrlLength != null) {+ errors.add(websiteUrlLength);+ }}if (contactLanguage != null && !CONTACT_LANGUAGES.contains(contactLanguage)) {errors.add(constraint("contactLanguage", "Member must satisfy enum value set: [EN, JA]"));@@ -181,12 +183,10 @@ public class SesAccountService {"Member must have length less than or equal to " + USE_CASE_DESCRIPTION_MAX));}if (additionalContacts != null) {- if (additionalContacts.isEmpty()) {- errors.add(constraint("additionalContactEmailAddresses",- "Member must have length greater than or equal to 1"));- } else if (additionalContacts.size() > CONTACTS_MAX) {- errors.add(constraint("additionalContactEmailAddresses",- "Member must have length less than or equal to " + CONTACTS_MAX));+ String contactsSize = SmithyValidationErrors.sizeOutOfRange("additionalContactEmailAddresses",+ additionalContacts.size(), 1, CONTACTS_MAX);+ if (contactsSize != null) {+ errors.add(contactsSize);}for (String contact : additionalContacts) {if (contact == null || contact.length() < CONTACT_MIN || contact.length() > CONTACT_MAXdiff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.javaindex 3c1ecea085..a8225cc6c6 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetService.java@@ -280,8 +280,7 @@ public class SesConfigurationSetService implements SesTaggable {Objects.requireNonNull(verifiedDomainIdentity, "verifiedDomainIdentity probe is required");if (domain == null) {throw SmithyValidationErrors.validationError(- "Value at 'trackingOptions' failed to satisfy constraint: "- + "Member must not be null");+ SmithyValidationErrors.notNullWithoutValue("trackingOptions"));}if (domain.isBlank()) {throw new AwsException("InvalidTrackingOptions",diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.javaindex 6140f1d36e..067a66e50b 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.java@@ -30,6 +30,8 @@ import java.util.Set;import java.util.function.UnaryOperator;import java.util.regex.Pattern;+import static io.github.hectorvent.floci.core.common.SmithyValidationErrors.notNullWithoutValue;+/*** Contact lists and contacts (the {@code contactListStore} + {@code contactStore}), extracted from* {@link SesService} as the fifth step of the store-based domain split.@@ -224,7 +226,7 @@ public class SesContactService implements SesTaggable {private static void validateContactListName(String name) {if (name == null) {- throw validationError("contactListName", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue("contactListName"));}if (name.isBlank()) {throw new AwsException("BadRequestException", "ContactListName can't be blank.", 400);@@ -250,20 +252,20 @@ public class SesContactService implements SesTaggable {// Phase 1 — protocol-layer (Smithy) validation: null members and the subscription-status// enum. AWS reports these before any service-level constraint.if (name == null) {- throw validationError("contactListName", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue("contactListName"));}if (topics != null) {for (int i = 0; i < topics.size(); i++) {Topic t = topics.get(i);String member = "topics." + (i + 1) + ".member.";if (t.getTopicName() == null) {- throw validationError(member + "topicName", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue(member + "topicName"));}if (t.getDisplayName() == null) {- throw validationError(member + "displayName", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue(member + "displayName"));}if (t.getDefaultSubscriptionStatus() == null) {- throw validationError(member + "defaultSubscriptionStatus", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue(member + "defaultSubscriptionStatus"));}if (!SUBSCRIPTION_STATUSES.contains(t.getDefaultSubscriptionStatus())) {throw validationError(member + "defaultSubscriptionStatus",@@ -545,10 +547,10 @@ public class SesContactService implements SesTaggable {TopicPreference p = prefs.get(i);String member = "topicPreferences." + (i + 1) + ".member.";if (p.getTopicName() == null) {- throw validationError(member + "topicName", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue(member + "topicName"));}if (p.getSubscriptionStatus() == null) {- throw validationError(member + "subscriptionStatus", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue(member + "subscriptionStatus"));}if (!SUBSCRIPTION_STATUSES.contains(p.getSubscriptionStatus())) {throw validationError(member + "subscriptionStatus",@@ -592,7 +594,7 @@ public class SesContactService implements SesTaggable {// Verified against real AWS: a missing/null required member is a Smithy validation error,// an empty/blank value is "can't be blank", and only a non-blank malformed value is "invalid".if (emailAddress == null) {- throw validationError("emailAddress", "Member must not be null");+ throw SmithyValidationErrors.badRequest(notNullWithoutValue("emailAddress"));}if (emailAddress.isBlank()) {throw new AwsException("BadRequestException", "EmailAddress can't be blank.", 400);diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesExportMetrics.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesExportMetrics.javaindex b5ca3a063a..4309160030 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesExportMetrics.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesExportMetrics.java@@ -100,7 +100,7 @@ final class SesExportMetrics {private static void validateDimensions(JsonNode dimensions, List<String> violations) {if (!dimensions.isObject()) {- violations.add(constraint("dimensions", "Member must not be null"));+ violations.add(SmithyValidationErrors.notNullWithoutValue(PATH + "dimensions"));return;}if (dimensions.isEmpty()) {@@ -133,7 +133,7 @@ final class SesExportMetrics {private static void validateMetrics(JsonNode metrics, List<String> violations) {if (!metrics.isArray()) {- violations.add(constraint("metrics", "Member must not be null"));+ violations.add(SmithyValidationErrors.notNullWithoutValue(PATH + "metrics"));return;}if (metrics.isEmpty()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateService.javaindex 9735fb0c2e..781fb41b75 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateService.java@@ -93,13 +93,12 @@ public class SesIdentityCertificateService {List<String> violations = new ArrayList<>();requireIdentityMember(emailIdentity, violations);if (certificateArn == null) {- violations.add(violation("certificateArn", "Member must not be null"));+ violations.add(SmithyValidationErrors.notNullWithoutValue("certificateArn"));} else {- if (certificateArn.length() < 20) {- violations.add(violation("certificateArn", "Member must have length greater than or equal to 20"));- }- if (certificateArn.length() > 2048) {- violations.add(violation("certificateArn", "Member must have length less than or equal to 2048"));+ String certificateArnLength = SmithyValidationErrors.sizeOutOfRange("certificateArn",+ certificateArn.length(), 20, 2048);+ if (certificateArnLength != null) {+ violations.add(certificateArnLength);}if (!CERTIFICATE_ARN.matcher(certificateArn).matches()) {violations.add(violation("certificateArn",@@ -335,7 +334,7 @@ public class SesIdentityCertificateService {private static void requireIdentityMember(String emailIdentity, List<String> violations) {if (emailIdentity == null) {- violations.add(violation("emailIdentity", "Member must not be null"));+ violations.add(SmithyValidationErrors.notNullWithoutValue("emailIdentity"));} else if (emailIdentity.isEmpty()) {violations.add(violation("emailIdentity", "Member must have length greater than or equal to 1"));}diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.javaindex 2081285af2..f1213d9b7e 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.java@@ -279,8 +279,7 @@ public class SesIdentityController {if (policyNode.isMissingNode() || policyNode.isNull()) {// Verified against AWS: a missing/null required member is a Smithy validation error.throw SmithyValidationErrors.badRequest(- "Value at 'policy' failed to satisfy constraint: "- + "Member must not be null");+ SmithyValidationErrors.notNullWithoutValue("policy"));}if (!policyNode.isTextual()) {// Policy is a String; AWS rejects a non-string with an empty-bodied 400. Don't coercediff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricsController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricsController.javaindex ca47dbb56a..613b05fe3f 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricsController.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricsController.java@@ -341,7 +341,8 @@ public class SesMetricsController {private static Instant requireTimestamp(JsonNode query, String field, String member, int position) {JsonNode node = query.path(field);if (node.isMissingNode() || node.isNull()) {- throw aggregated(List.of(memberConstraint(position, member, "Member must not be null")));+ throw aggregated(List.of(+ SmithyValidationErrors.notNullWithoutValue("queries." + position + ".member." + member)));}if (node.isNumber()) {try {diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesReceiptRuleService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesReceiptRuleService.javaindex e8bf7960bf..0a4e9b9b5c 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesReceiptRuleService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesReceiptRuleService.java@@ -521,8 +521,7 @@ public class SesReceiptRuleService {private static void requireMember(List<String> violations, ReceiptAction action,String member, String path) {if (action.property(member) == null) {- violations.add("Value at '" + path + "' failed to satisfy constraint: "- + "Member must not be null");+ violations.add(SmithyValidationErrors.notNullWithoutValue(path));}}@@ -733,8 +732,7 @@ public class SesReceiptRuleService {// Probed: an absent FilterName is the one case the wire rejects; deleting a missing// or malformed name succeeds without error.throw SmithyValidationErrors.validationError(- "Value at 'filterName' failed to satisfy "- + "constraint: Member must not be null");+ SmithyValidationErrors.notNullWithoutValue("filterName"));}receiptFilterStore.delete(receiptFilterKey(region, filterName));LOG.infov("Deleted SES receipt filter: {0} in region {1}", filterName, region);diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesReputationEntityService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesReputationEntityService.javaindex 5828c52a28..ff99e2c20f 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesReputationEntityService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesReputationEntityService.java@@ -86,7 +86,7 @@ public class SesReputationEntityService {requireReference(reference);if (status == null) {throw SmithyValidationErrors.badRequest(- "Value at 'sendingStatus' failed to satisfy constraint: Member must not be null");+ SmithyValidationErrors.notNullWithoutValue("sendingStatus"));}if (!SENDING_STATUSES.contains(status)) {throw SmithyValidationErrors.badRequest("Value at 'sendingStatus' failed to satisfy constraint: "diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.javaindex db5184557a..83ca49c600 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java@@ -238,7 +238,7 @@ public class SesSendController {validateTrackingOverrides(request);JsonNode bulkEntries = request.path("BulkEmailEntries");if (bulkEntries.isMissingNode() || bulkEntries.isNull()) {- throw constraintViolation(null, "bulkEmailEntries", "Member must not be null");+ throw SmithyValidationErrors.badRequest(SmithyValidationErrors.notNullWithoutValue("bulkEmailEntries"));}String fromEmailAddress = request.path("FromEmailAddress").asText(null);if (fromEmailAddress == null || fromEmailAddress.isBlank()) {@@ -494,11 +494,13 @@ public class SesSendController {String memberPrefix = location + "." + index + ".member.";String fileName = attachmentString(node, "FileName", memberPrefix + "fileName", 0, 255);if (fileName == null) {- throw constraintViolation(null, memberPrefix + "fileName", "Member must not be null");+ throw SmithyValidationErrors.badRequest(+ SmithyValidationErrors.notNullWithoutValue(memberPrefix + "fileName"));}JsonNode rawNode = node.get("RawContent");if (rawNode == null || rawNode.isNull()) {- throw constraintViolation(null, memberPrefix + "rawContent", "Member must not be null");+ throw SmithyValidationErrors.badRequest(+ SmithyValidationErrors.notNullWithoutValue(memberPrefix + "rawContent"));}byte[] rawContent;try {@@ -568,13 +570,13 @@ public class SesSendController {}private static AwsException constraintViolation(String value, String path, String constraint) {- String subject = value == null ? "Value" : "Value '" + value + "'";return SmithyValidationErrors.badRequest(- subject + " at '" + path + "' failed to satisfy constraint: " + constraint);+ "Value '" + value + "' at '" + path + "' failed to satisfy constraint: " + constraint);}private static AwsException missingHeaderMember(String location, int index, String member) {- return constraintViolation(null, location + "." + index + ".member." + member, "Member must not be null");+ return SmithyValidationErrors.badRequest(+ SmithyValidationErrors.notNullWithoutValue(location + "." + index + ".member." + member));}/**diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.javaindex 43ed772d5d..bdf5f62f33 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java@@ -407,8 +407,7 @@ public class SesTenantService implements SesTaggable {public static AssociationResource parseResourceArn(String resourceArn, String accountId, String region) {if (resourceArn == null) {throw SmithyValidationErrors.badRequest(- "Value at 'resourceArn' failed to satisfy constraint: "- + "Member must not be null");+ SmithyValidationErrors.notNullWithoutValue("resourceArn"));}String[] parts = resourceArn.split(":", 6);if (parts.length < 6 || !"arn".equals(parts[0])) {@@ -678,8 +677,7 @@ public class SesTenantService implements SesTaggable {if (name == null) {if (createTenantSmithyVariants) {throw SmithyValidationErrors.badRequest(- "Value at 'tenantName' failed to satisfy "- + "constraint: Member must not be null");+ SmithyValidationErrors.notNullWithoutValue("tenantName"));}throw new AwsException("BadRequestException", "TenantName cannot be empty", 400);}diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateServiceTest.javaindex c75d6ef217..89b19886cd 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesIdentityCertificateServiceTest.java@@ -117,6 +117,15 @@ class SesIdentityCertificateServiceTest {() -> service.associate(DOMAIN, "[email protected]", "not-an-arn", REGION));}+ @Test+ void associate_overlongArnReportsTheLengthConstraint() {+ String arn = CERT_ARN + "a".repeat(2049 - CERT_ARN.length());++ assertBadRequest("1 validation error detected: Value at 'certificateArn' failed to satisfy constraint: "+ + "Member must have length less than or equal to 2048",+ () -> service.associate(DOMAIN, "[email protected]", arn, REGION));+ }+@Testvoid associate_missingIdentityIsNotFound() {AwsException e = assertThrows(AwsException.class,