Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(ses): move ListTenantResources into SesTenantService and drop unused resolveSuppressionReason (#4978)
Kenshin Okinakaa68f8eab3a7d
diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.javaindex fc6510a4e4..190ed1401c 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java@@ -891,8 +891,9 @@ public class SesService {// ──────────────────────── Tenants (multi-tenancy) ────────────────────────// Tenants live in SesTenantService, which the v2 controller calls directly for the tenant- // record and its suppression attributes; the facade keeps the resource associations (they- // check the identity, configuration set or template exists) and the delete cascade.+ // record, its suppression attributes and the tenant's resource list; the facade keeps the+ // association operations that check the identity, configuration set or template exists, and+ // the delete cascade.public void deleteTenant(String tenantName, String region) {// The tenant-scoped suppression entries live in the suppression domain; the callback runs@@ -925,18 +926,8 @@ public class SesService {tenantService.disassociate(tenant, ref, region);}- // Probe-confirmed order on both lists: the tenant or resource is resolved first and the filter is- // checked next, so a bad page on a missing tenant is the 404; the page size and token come last.- public PaginatedResult<TenantResourceAssociation> listTenantResources(String tenantName,- String resourceTypeFilter,- SesListPaging paging,- Integer pageSize, String nextToken,- String region) {- Tenant tenant = tenantService.tenantForAssociation(tenantName, region);- SesTenantService.validateResourceTypeFilter(resourceTypeFilter);- return tenantService.listTenantResources(tenant, resourceTypeFilter, region, paging, pageSize, nextToken);- }-+ // Probe-confirmed order: the resource is resolved first, so a bad page on a missing resource is+ // the 404; the page size and token come last.public PaginatedResult<TenantResourceAssociation> listResourceTenants(String resourceArn, SesListPaging paging,Integer pageSize, String nextToken,String accountId, String region) {@@ -1239,7 +1230,7 @@ public class SesService {// TenantName sends the operation to that tenant's own list, which is fully separate from the// account list on AWS, and the reason and address validation still runs first, matching the// probed precedence where request validation precedes tenant existence. The send filters- // (collectSuppressedReasons, resolveSuppressionReason) read entries back through the service.+ // (collectSuppressedReasons) read entries back through the service.public void putSuppressedDestination(String region, String emailAddress, String reason,String tenantName) {@@ -1430,42 +1421,6 @@ public class SesService {return kept;}- /**- * Resolve the suppression reason that applies to a given recipient in the given region- * for sends using {@code configurationSetName}, or {@code null} if the recipient is not- * suppressed. The recipient is suppressed only when it appears in the address-level- * suppression list AND its stored reason intersects the effective {@code suppressedReasons}- * — the configuration set's {@code SuppressionOptions} override if present, else the- * account-level reasons. {@code configurationSetName} may be {@code null} or blank to- * scope the check to account-level reasons only.- *- * <p>The returned value is one of {@code "BOUNCE"} / {@code "COMPLAINT"}, allowing- * callers (publishSendEvents) to map the recipient to a synthetic Bounce / Complaint- * event without consulting the store again. Both the per-address suppression entries- * and the account-level / per-CS {@code suppressedReasons} go through reason validation- * (in {@link SesSuppressionService} and {@link SesConfigurationSetService} respectively),- * which enforces exact case-sensitive equality with the two canonical values, so- * {@code entry.getReason()} is guaranteed to be canonical and downstream- * {@code .equals("BOUNCE")} / {@code .equals("COMPLAINT")} checks are safe.- */- String resolveSuppressionReason(String emailAddress, String configurationSetName, String region) {- if (emailAddress == null || emailAddress.isBlank()) {- return null;- }- // Read through the suppression service so this shares its normalization and legacy-key- // fallback with GET/DELETE (lookups can't drift apart from inserts).- SuppressedDestination entry = suppressionService.findSuppressedDestination(region, emailAddress)- .orElse(null);- if (entry == null || entry.getReason() == null) {- return null;- }- List<String> effective = getEffectiveSuppressedReasons(configurationSetName, region);- if (effective == null || effective.isEmpty()) {- return null;- }- return effective.contains(entry.getReason()) ? entry.getReason() : null;- }-/*** Also called by the controller ahead of the tenant send gate: AWS reports an empty inline* template before it looks the tenant up (probe-confirmed), so the check must not stay behinddiff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionService.javaindex c277e95b95..891e347a4d 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionService.java@@ -31,7 +31,7 @@ import java.util.TreeSet;* rather than splitting one off, both stores move together into this service and the helper becomes* a private detail of it. The send path keeps its cross-domain orchestration* ({@code getEffectiveSuppressedReasons} reads a configuration set's options or falls back here;- * {@code collectSuppressedReasons}/{@code resolveSuppressionReason} filter a send) in the+ * {@code collectSuppressedReasons} filters a send) in the* {@link SesService} facade, which reads entries back through {@link #findSuppressedDestination}.*/@ApplicationScoped@@ -136,9 +136,9 @@ public class SesSuppressionService {}/**- * Reads a suppression entry without throwing, so the facade's send-path filters- * ({@code collectSuppressedReasons} / {@code resolveSuppressionReason}) can look one up by raw- * address and share this service's normalization and legacy-key fallback.+ * Reads a suppression entry without throwing, so the facade's send-path filter+ * ({@code collectSuppressedReasons}) can look one up by raw address and share this service's+ * normalization and legacy-key fallback.*/public Optional<SuppressedDestination> findSuppressedDestination(String region, String rawEmail) {if (rawEmail == null || rawEmail.isBlank()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.javaindex fc66f95fbb..49ca25ffc4 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java@@ -34,10 +34,10 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAb/*** SES V2 tenant endpoints ({@code /v2/email/tenants}, {@code /v2/email/tenant} and- * {@code /v2/email/resources/tenants}). Tenant create, get, list and suppression attributes- * call {@link SesTenantService} directly; the resource- * associations and tenant delete go through the {@link SesService} facade, which checks the- * associated identity, configuration set or template exists and cascades the tenant's+ * {@code /v2/email/resources/tenants}). Tenant create, get, list, suppression attributes and the+ * tenant's resource list call {@link SesTenantService} directly; association create and delete,+ * the resource's tenant list and tenant delete go through the {@link SesService} facade, which+ * checks the associated identity, configuration set or template exists and cascades the tenant's* suppression entries, work that spans several domains.*/@Path("/v2/email")@@ -230,9 +230,9 @@ public class SesTenantController {}Integer pageSize = intMemberOrAbsent(request, "PageSize");String nextToken = stringMemberOrAbsent(request, "NextToken");- PaginatedResult<TenantResourceAssociation> page = sesService.listTenantResources(- tenantName, resourceTypeFilter, SesListPaging.V2_LIST_TENANT_RESOURCES, pageSize, nextToken,- region);+ PaginatedResult<TenantResourceAssociation> page = tenantService.listTenantResources(+ tenantName, resourceTypeFilter, region, SesListPaging.V2_LIST_TENANT_RESOURCES, pageSize,+ nextToken);ObjectNode result = objectMapper.createObjectNode();result.put("NextToken", page.nextToken());ArrayNode resources = result.putArray("TenantResources");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 9fa4fb8a5f..9f02e492bf 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@@ -419,9 +419,13 @@ public class SesTenantService {ref.arn(), tenant.tenantName(), region);}- public PaginatedResult<TenantResourceAssociation> listTenantResources(Tenant tenant, String typeFilter,+ // Probe-confirmed order: the tenant is resolved first and the filter is checked next, so a bad page+ // on a missing tenant is the 404; the page size and token come last.+ public PaginatedResult<TenantResourceAssociation> listTenantResources(String tenantName, String typeFilter,String region, SesListPaging paging,Integer pageSize, String nextToken) {+ Tenant tenant = tenantForAssociation(tenantName, region);+ validateResourceTypeFilter(typeFilter);return paging.page(region, tenant.tenantId() + "/" + Objects.toString(typeFilter, ""),listTenantResources(tenant, typeFilter, region), TenantResourceAssociation::resourceArn,pageSize, nextToken);diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSuppressionReasonTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSuppressionReasonTest.javaindex 238dc03ef3..79961997ab 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSuppressionReasonTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSuppressionReasonTest.java@@ -7,18 +7,18 @@ import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import java.util.ArrayList;+import java.util.Arrays;import java.util.List;+import java.util.Map;import static org.junit.jupiter.api.Assertions.assertEquals;-import static org.junit.jupiter.api.Assertions.assertNull;/**- * Covers {@link SesService#resolveSuppressionReason(String, String)} — the per-recipient- * lookup that publishSendEvents uses to map suppressed addresses to synthetic Bounce or- * Complaint events.+ * Covers {@link SesService#collectSuppressedReasons}, the per-send lookup that filters the relay+ * and maps suppressed recipients to synthetic Bounce or Complaint events.*- * <p>The helper returns a reason only when the address is on the suppression list AND- * the address's stored reason intersects {@link AccountSuppressionAttributes#getSuppressedReasons()}.+ * <p>An address is reported only when it is on the suppression list and its stored reason+ * intersects {@link AccountSuppressionAttributes#getSuppressedReasons()}.*/class SesServiceSuppressionReasonTest {@@ -39,47 +39,49 @@ class SesServiceSuppressionReasonTest {}@Test- void notOnList_returnsNull() {+ void notOnList_returnsEmpty() {// Default fresh account: suppressedReasons defaults to [BOUNCE, COMPLAINT], but the- // address is not on the list, so resolution returns null.- assertNull(service.resolveSuppressionReason("[email protected]", null, REGION));+ // address is not on the list, so nothing is reported.+ assertEquals(Map.of(), reasons("[email protected]"));}@Testvoid onListAndReasonInAccountSettings_returnsReason() {suppression.putSuppressedDestination(REGION, "[email protected]", "BOUNCE");// Account-level suppressedReasons defaults to [BOUNCE, COMPLAINT].- assertEquals("BOUNCE", service.resolveSuppressionReason("[email protected]", null, REGION));+ assertEquals(Map.of("[email protected]", "BOUNCE"), reasons("[email protected]"));}@Test- void onListButReasonNotInAccountSettings_returnsNull() {+ void onListButReasonNotInAccountSettings_returnsEmpty() {suppression.putSuppressedDestination(REGION, "[email protected]", "COMPLAINT");// Narrow the account settings to BOUNCE only.suppression.putAccountSuppressionAttributes(REGION, List.of("BOUNCE"));- assertNull(service.resolveSuppressionReason("[email protected]", null, REGION));+ assertEquals(Map.of(), reasons("[email protected]"));}@Test- void accountSettingsEmpty_returnsNull() {+ void accountSettingsEmpty_returnsEmpty() {suppression.putSuppressedDestination(REGION, "[email protected]", "BOUNCE");// Disable account-level suppression by passing an empty list.suppression.putAccountSuppressionAttributes(REGION, new ArrayList<>());- assertNull(service.resolveSuppressionReason("[email protected]", null, REGION));+ assertEquals(Map.of(), reasons("[email protected]"));}@Testvoid leadingTrailingWhitespaceIsNormalized() {suppression.putSuppressedDestination(REGION, "[email protected]", "BOUNCE");// Caller may pass the recipient with surrounding whitespace (e.g. from a header).- assertEquals("BOUNCE",- service.resolveSuppressionReason(" [email protected] ", null, REGION));+ assertEquals(Map.of(" [email protected] ", "BOUNCE"), reasons(" [email protected] "));}@Test- void nullOrBlankInput_returnsNull() {- assertNull(service.resolveSuppressionReason(null, null, REGION));- assertNull(service.resolveSuppressionReason("", null, REGION));- assertNull(service.resolveSuppressionReason(" ", null, REGION));+ void nullOrBlankInput_returnsEmpty() {+ assertEquals(Map.of(), service.collectSuppressedReasons(Arrays.asList(null, "", " "), null, REGION));+ assertEquals(Map.of(), service.collectSuppressedReasons(null, null, REGION));+ }++ private Map<String, String> reasons(String address) {+ return service.collectSuppressedReasons(List.of(address), null, REGION);}}diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesSuppressionLegacyKeyTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesSuppressionLegacyKeyTest.javaindex f18c03d3e8..487a6c1e12 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesSuppressionLegacyKeyTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesSuppressionLegacyKeyTest.java@@ -5,6 +5,9 @@ import io.github.hectorvent.floci.services.ses.model.SuppressedDestination;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import java.util.List;+import java.util.Map;+import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -57,11 +60,12 @@ class SesSuppressionLegacyKeyTest {@Testvoid sendTimeSuppressionReasonReachesLegacyEntry() {- // The event / send-time path (resolveSuppressionReason, collectSuppressedReasons)- // must also honor the legacy key, or a legacy entry stays deletable but no longer- // suppresses sends. Default fresh account suppresses [BOUNCE, COMPLAINT].+ // The send-time path (collectSuppressedReasons) must also honor the legacy key, or a+ // legacy entry stays deletable but no longer suppresses sends. Default fresh account+ // suppresses [BOUNCE, COMPLAINT].seedLegacyEntry();- assertEquals("BOUNCE", service.resolveSuppressionReason(LEGACY_ADDR, null, REGION));+ assertEquals(Map.of(LEGACY_ADDR, "BOUNCE"),+ service.collectSuppressedReasons(List.of(LEGACY_ADDR), null, REGION));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantServiceTest.javaindex 085eed2a1a..5c0cb30bcc 100644--- a/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesTenantServiceTest.java@@ -598,11 +598,11 @@ class SesTenantServiceTest {service.associate(tenant, identityRef(domain), REGION, () -> {});}- PaginatedResult<TenantResourceAssociation> first = service.listTenantResources(tenant, null, REGION,+ PaginatedResult<TenantResourceAssociation> first = service.listTenantResources("acme", null, REGION,SesListPaging.V2_LIST_TENANT_RESOURCES, 2, null);assertEquals(List.of(identityRef("a.example.com").arn(), identityRef("b.example.com").arn()),first.items().stream().map(TenantResourceAssociation::resourceArn).toList());- PaginatedResult<TenantResourceAssociation> rest = service.listTenantResources(tenant, null, REGION,+ PaginatedResult<TenantResourceAssociation> rest = service.listTenantResources("acme", null, REGION,SesListPaging.V2_LIST_TENANT_RESOURCES, 2, first.nextToken());assertEquals(List.of(identityRef("c.example.com").arn()),rest.items().stream().map(TenantResourceAssociation::resourceArn).toList());