feat(ses): support v2 tenant resource associations (#2710)

* feat(ses): support v2 tenant resource associations

* fix(ses): reject nameless association ARNs and stop key-suffix resource matching
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-08-29 14:29:36 -04:00
1 parent 2a7c94b04d
commit 67f75c7e1d
9 files changed
+1106 -23

No files matched your search

@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.services.ses.model.ReceiptRuleSet;
import io.github.hectorvent.floci.services.ses.model.SentEmail;
import io.github.hectorvent.floci.services.ses.model.SuppressedDestination;
import io.github.hectorvent.floci.services.ses.model.Tenant;
import io.github.hectorvent.floci.services.ses.model.TenantResourceAssociation;
import io.github.hectorvent.floci.services.route53.Route53Service;
import java.security.SecureRandom;
@@ -48,6 +49,8 @@ final class SesServiceTestBuilder {
private final InMemoryStorage<String, ReceiptRuleSet> receiptRuleStore = new InMemoryStorage<>();
private final InMemoryStorage<String, CustomVerificationEmailTemplate> cvetStore = new InMemoryStorage<>();
private final InMemoryStorage<String, Tenant> tenantStore = new InMemoryStorage<>();
private final InMemoryStorage<String, TenantResourceAssociation> tenantAssociationStore =
new InMemoryStorage<>();
private SmtpRelay smtpRelay = mock(SmtpRelay.class);
// Default null, matching the production null-Route53 case: SesService treats a null Route53Service
@@ -124,7 +127,7 @@ final class SesServiceTestBuilder {
new SesPolicyService(policyStore, objectMapper),
new SesReceiptRuleService(receiptRuleStore, clock),
new SesCvetService(cvetStore),
new SesTenantService(tenantStore, clock, new SecureRandom()),
new SesTenantService(tenantStore, tenantAssociationStore, clock, new SecureRandom()),
smtpRelay,
objectMapper,
route53Service,
@@ -0,0 +1,279 @@
package io.github.hectorvent.floci.services.ses;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
/**
* Integration tests for the SES v2 tenant resource associations (Phase 2): the RPC-style
* {@code /tenants/resources}, {@code /tenants/resources/delete}, {@code /tenants/resources/list} and
* {@code /resources/tenants/list}. The wire quirks are probe-confirmed against real AWS: ResourceType
* values are the ARN segments (identity / configuration-set / template) — not the SDK enum spelling —
* delete is idempotent, backing-resource deletion is blocked while associated, and DeleteTenant
* cascades.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SesTenantAssociationV2IntegrationTest {
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
private static final String TENANT = "floci-assoc-tenant";
private static final String TENANT_B = "floci-assoc-tenant-b";
private static final String IDENTITY = "floci-assoc.example.com";
private static final String CONFIG_SET = "floci-assoc-cs";
private static final String TEMPLATE = "floci-assoc-tpl";
private static final String ARN_PREFIX = "arn:aws:ses:us-east-1:000000000000:";
private static final String IDENTITY_ARN = ARN_PREFIX + "identity/" + IDENTITY;
private static final String CONFIG_SET_ARN = ARN_PREFIX + "configuration-set/" + CONFIG_SET;
private static final String TEMPLATE_ARN = ARN_PREFIX + "template/" + TEMPLATE;
private static io.restassured.specification.RequestSpecification v2() {
return given().contentType("application/json").header("Authorization", AUTH);
}
private static void associate(String tenant, String arn) {
v2().body("{\"TenantName\":\"" + tenant + "\",\"ResourceArn\":\"" + arn + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(200);
}
@Test
@Order(1)
void setup_createTenantsAndResources() {
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
.when().post("/v2/email/tenants").then().statusCode(200);
v2().body("{\"TenantName\":\"" + TENANT_B + "\"}")
.when().post("/v2/email/tenants").then().statusCode(200);
v2().body("{\"EmailIdentity\":\"" + IDENTITY + "\"}")
.when().post("/v2/email/identities").then().statusCode(200);
v2().body("{\"ConfigurationSetName\":\"" + CONFIG_SET + "\"}")
.when().post("/v2/email/configuration-sets").then().statusCode(200);
v2().body("{\"TemplateName\":\"" + TEMPLATE + "\",\"TemplateContent\":{\"Subject\":\"s\",\"Text\":\"t\"}}")
.when().post("/v2/email/templates").then().statusCode(200);
}
@Test
@Order(2)
void createAssociation_returnsEmptyObject() {
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(200)
.body("isEmpty()", equalTo(true));
associate(TENANT, CONFIG_SET_ARN);
associate(TENANT, TEMPLATE_ARN);
associate(TENANT_B, IDENTITY_ARN);
}
@Test
@Order(3)
void createAssociation_duplicate_returnsAlreadyExists() {
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("__type", equalTo("AlreadyExistsException"))
// "Resources" is AWS's own grammar.
.body("message", equalTo("Resources " + IDENTITY_ARN
+ " has already been associated with tenant " + TENANT));
}
@Test
@Order(4)
void createAssociation_errorMatrix() {
v2().body("{\"TenantName\":\"ghost-tenant\",\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(404)
.body("__type", equalTo("NotFoundException"))
.body("message", equalTo("The requested tenant <ghost-tenant> does not exist."));
// Missing resources 404 with a per-type message; the configuration-set trailing colon is AWS's.
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + ARN_PREFIX
+ "configuration-set/ghost-cs\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(404)
.body("message", equalTo("Configuration set <ghost-cs> does not exist:"));
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + ARN_PREFIX
+ "identity/ghost.example.com\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(404)
.body("message", equalTo("Identity <ghost.example.com> does not exist"));
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + ARN_PREFIX
+ "template/ghost-tpl\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(404)
.body("message", equalTo("Email template <ghost-tpl> does not exist"));
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"not-an-arn\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("message", equalTo("Provided resource identifier is not an SES resource"));
v2().body("{\"TenantName\":\"" + TENANT
+ "\",\"ResourceArn\":\"arn:aws:sqs:us-east-1:000000000000:q\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("message", equalTo("Provided ARN is not in SES resource ARN format"));
v2().body("{\"TenantName\":\"" + TENANT
+ "\",\"ResourceArn\":\"arn:aws:ses:us-east-1:000000000000:contact-list/x\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("message", equalTo("Unsupported resource type: contact-list"));
v2().body("{\"TenantName\":\"" + TENANT
+ "\",\"ResourceArn\":\"arn:aws:ses:eu-west-1:000000000000:identity/" + IDENTITY + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("message", equalTo("Resource <arn:aws:ses:eu-west-1:000000000000:identity/"
+ IDENTITY + "> must be in the same region"));
v2().body("{\"TenantName\":\"" + TENANT
+ "\",\"ResourceArn\":\"arn:aws:ses:us-east-1:111111111111:identity/" + IDENTITY + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("message", equalTo("Resource <arn:aws:ses:us-east-1:111111111111:identity/"
+ IDENTITY + "> must be in the same account"));
v2().body("{\"TenantName\":\"\",\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("message", equalTo("TenantName cannot be empty"));
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
.when().post("/v2/email/tenants/resources").then().statusCode(400)
.body("message", equalTo("1 validation error detected: Value at 'resourceArn' failed "
+ "to satisfy constraint: Member must not be null"));
}
@Test
@Order(5)
void listTenantResources_sortedByArn_lowercaseTypes_nullNextToken() {
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(200)
.body("NextToken", nullValue())
.body("TenantResources", hasSize(3))
.body("TenantResources[0].ResourceType", equalTo("configuration-set"))
.body("TenantResources[0].ResourceArn", equalTo(CONFIG_SET_ARN))
.body("TenantResources[1].ResourceType", equalTo("identity"))
.body("TenantResources[2].ResourceType", equalTo("template"));
}
@Test
@Order(6)
void listTenantResources_filterUsesWireValues() {
v2().body("{\"TenantName\":\"" + TENANT + "\",\"Filter\":{\"RESOURCE_TYPE\":\"identity\"}}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(200)
.body("TenantResources", hasSize(1))
.body("TenantResources[0].ResourceArn", equalTo(IDENTITY_ARN));
// Real AWS rejects the SDK's enum spelling — only the ARN-segment values are valid.
v2().body("{\"TenantName\":\"" + TENANT + "\",\"Filter\":{\"RESOURCE_TYPE\":\"EMAIL_IDENTITY\"}}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("Invalid resource type EMAIL_IDENTITY specified."));
}
@Test
@Order(7)
void listTenantResources_pagingValidation() {
v2().body("{\"TenantName\":\"" + TENANT + "\",\"PageSize\":0}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(400)
.body("message", equalTo("1 validation error detected: Value '0' at 'pageSize' failed "
+ "to satisfy constraint: Member must have value greater than or equal to 1"));
v2().body("{\"TenantName\":\"" + TENANT + "\",\"NextToken\":\"garbage-token\"}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(400)
.body("message", equalTo("Invalid Next Token"));
// An integral value outside the int range must be rejected, not silently truncated
// (4294967296 would truncate to 0 and produce the wrong Smithy message).
v2().body("{\"TenantName\":\"" + TENANT + "\",\"PageSize\":4294967296}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(400)
.body("__type", equalTo("SerializationException"));
v2().body("{\"TenantName\":\"ghost-tenant\"}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(404)
.body("__type", equalTo("NotFoundException"));
}
@Test
@Order(8)
void listResourceTenants_returnsMetadataWithoutTenantArn() {
v2().body("{\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(200)
.body("NextToken", nullValue())
.body("ResourceTenants", hasSize(2))
.body("ResourceTenants[0].TenantName", equalTo(TENANT))
.body("ResourceTenants[0].TenantId", startsWith("tn-"))
.body("ResourceTenants[0].ResourceArn", equalTo(IDENTITY_ARN))
.body("ResourceTenants[0].AssociatedTimestamp", notNullValue())
.body("ResourceTenants[0].TenantArn", nullValue())
.body("ResourceTenants[1].TenantName", equalTo(TENANT_B));
v2().body("{\"ResourceArn\":\"" + ARN_PREFIX + "configuration-set/ghost-cs\"}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(404)
.body("message", equalTo("Configuration set <ghost-cs> does not exist:"));
v2().body("{\"ResourceArn\":\"nope\"}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(400)
.body("message", equalTo("Provided resource identifier is not an SES resource"));
}
@Test
@Order(9)
void deleteBackingResource_whileAssociated_isBlocked() {
v2().when().delete("/v2/email/identities/" + IDENTITY).then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("Cannot delete <" + IDENTITY_ARN + "> because it has tenant "
+ "associations. Remove all tenant associations and try again."));
v2().when().delete("/v2/email/configuration-sets/" + CONFIG_SET).then().statusCode(400)
.body("message", equalTo("Cannot delete <" + CONFIG_SET_ARN + "> because it has tenant "
+ "associations. Remove all tenant associations and try again."));
v2().when().delete("/v2/email/templates/" + TEMPLATE).then().statusCode(400)
.body("message", equalTo("Cannot delete <" + TEMPLATE_ARN + "> because it has tenant "
+ "associations. Remove all tenant associations and try again."));
}
@Test
@Order(10)
void deleteAssociation_isIdempotent_butValidatesTenantAndResource() {
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + TEMPLATE_ARN + "\"}")
.when().post("/v2/email/tenants/resources/delete").then().statusCode(200)
.body("isEmpty()", equalTo(true));
// Removing an association that no longer exists is a silent success on AWS.
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + TEMPLATE_ARN + "\"}")
.when().post("/v2/email/tenants/resources/delete").then().statusCode(200);
v2().body("{\"TenantName\":\"ghost-tenant\",\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/tenants/resources/delete").then().statusCode(404)
.body("__type", equalTo("NotFoundException"));
v2().body("{\"TenantName\":\"" + TENANT + "\",\"ResourceArn\":\"" + ARN_PREFIX
+ "configuration-set/ghost-cs\"}")
.when().post("/v2/email/tenants/resources/delete").then().statusCode(404)
.body("message", equalTo("Configuration set <ghost-cs> does not exist:"));
}
@Test
@Order(11)
void deleteTenant_cascadesAssociations_andRecreationStartsClean() {
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
.when().post("/v2/email/tenants/delete").then().statusCode(200);
// Only TENANT_B's association survives the cascade.
v2().body("{\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(200)
.body("ResourceTenants", hasSize(1))
.body("ResourceTenants[0].TenantName", equalTo(TENANT_B));
v2().body("{\"ResourceArn\":\"" + CONFIG_SET_ARN + "\"}")
.when().post("/v2/email/resources/tenants/list").then().statusCode(200)
.body("ResourceTenants", hasSize(0));
// A recreated same-name tenant has a new TenantId and sees none of the old associations.
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
.when().post("/v2/email/tenants").then().statusCode(200);
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
.when().post("/v2/email/tenants/resources/list").then().statusCode(200)
.body("TenantResources", hasSize(0));
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
.when().post("/v2/email/tenants/delete").then().statusCode(200);
}
@Test
@Order(12)
void cleanup_afterDisassociation_backingResourcesDeletable() {
v2().body("{\"TenantName\":\"" + TENANT_B + "\",\"ResourceArn\":\"" + IDENTITY_ARN + "\"}")
.when().post("/v2/email/tenants/resources/delete").then().statusCode(200);
v2().body("{\"TenantName\":\"" + TENANT_B + "\"}")
.when().post("/v2/email/tenants/delete").then().statusCode(200);
v2().when().delete("/v2/email/identities/" + IDENTITY).then().statusCode(200);
v2().when().delete("/v2/email/configuration-sets/" + CONFIG_SET).then().statusCode(200);
v2().when().delete("/v2/email/templates/" + TEMPLATE).then().statusCode(200);
}
}
@@ -16,8 +16,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for the tenant domain (Phase 1 CRUD): create/get/list/delete, id/ARN generation, and the
* probe-confirmed name validation and duplicate/not-found errors. Constructed with just its own store.
* Unit test for the tenant domain: the Phase 1 CRUD (create/get/list/delete, id/ARN generation, name
* validation) and the Phase 2 resource associations (ARN parsing, duplicate/idempotency semantics,
* sorting, cascade on tenant delete). Constructed with just its own stores; resource existence is the
* facade's concern and is covered by the integration tests.
*/
class SesTenantServiceTest {
@@ -27,7 +29,8 @@ class SesTenantServiceTest {
@BeforeEach
void setUp() {
service = new SesTenantService(new InMemoryStorage<>(), Clock.systemUTC(), new SecureRandom());
service = new SesTenantService(new InMemoryStorage<>(), new InMemoryStorage<>(),
Clock.systemUTC(), new SecureRandom());
}
@Test
@@ -130,4 +133,199 @@ class SesTenantServiceTest {
AwsException d = assertThrows(AwsException.class, () -> service.deleteTenant(" ", REGION));
assertEquals("BadRequestException", d.getErrorCode());
}
// ──────────────────────── Resource associations (Phase 2) ────────────────────────
private static SesTenantService.AssociationResource identityRef(String name) {
return SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":identity/" + name, ACCOUNT, REGION);
}
@Test
void parseResourceArn_acceptsThreeTypes_andExtractsName() {
SesTenantService.AssociationResource ref = SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":configuration-set/my-cs", ACCOUNT, REGION);
assertEquals("configuration-set", ref.type());
assertEquals("my-cs", ref.name());
}
@Test
void parseResourceArn_errorChainMatchesAws() {
assertEquals("Provided resource identifier is not an SES resource",
assertThrows(AwsException.class,
() -> SesTenantService.parseResourceArn("not-an-arn", ACCOUNT, REGION)).getMessage());
assertEquals("Provided ARN is not in SES resource ARN format",
assertThrows(AwsException.class, () -> SesTenantService.parseResourceArn(
"arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":q", ACCOUNT, REGION)).getMessage());
assertEquals("Unsupported resource type: contact-list",
assertThrows(AwsException.class, () -> SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":contact-list/x", ACCOUNT, REGION)).getMessage());
assertTrue(assertThrows(AwsException.class, () -> SesTenantService.parseResourceArn(
"arn:aws:ses:eu-west-1:" + ACCOUNT + ":identity/a", ACCOUNT, REGION))
.getMessage().endsWith("must be in the same region"));
assertTrue(assertThrows(AwsException.class, () -> SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":111111111111:identity/a", ACCOUNT, REGION))
.getMessage().endsWith("must be in the same account"));
assertTrue(assertThrows(AwsException.class,
() -> SesTenantService.parseResourceArn(null, ACCOUNT, REGION))
.getMessage().contains("'resourceArn'"));
}
@Test
void parseResourceArn_rejectsMissingOrEmptyName() {
// A supported type without a name segment must be rejected as malformed, not resolved to an
// empty-name resource that would 404 with a misleading message.
assertEquals("Provided resource identifier is not an SES resource",
assertThrows(AwsException.class, () -> SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":identity", ACCOUNT, REGION)).getMessage());
assertEquals("Provided resource identifier is not an SES resource",
assertThrows(AwsException.class, () -> SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":identity/", ACCOUNT, REGION)).getMessage());
}
@Test
void resourceLookups_dontAliasNamesContainingDelimiter() {
// Floci barely restricts resource names, so "x" and "template::x" can both exist; the
// lookups must not let a key-suffix match blur them together.
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
SesTenantService.AssociationResource plain = SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":template/x", ACCOUNT, REGION);
SesTenantService.AssociationResource tricky = SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":template/template::x", ACCOUNT, REGION);
service.associate(tenant, tricky, REGION, () -> {});
assertEquals(0, service.listResourceTenants(plain, REGION).size());
assertTrue(service.findAssociationForResource("template", "x", REGION).isEmpty());
assertEquals(1, service.listResourceTenants(tricky, REGION).size());
assertTrue(service.findAssociationForResource("template", "template::x", REGION).isPresent());
}
@Test
void associate_duplicateThrows_awsGrammarPreserved() {
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
SesTenantService.AssociationResource ref = identityRef("example.com");
service.associate(tenant, ref, REGION, () -> {});
AwsException e = assertThrows(AwsException.class,
() -> service.associate(tenant, ref, REGION, () -> {}));
assertEquals("AlreadyExistsException", e.getErrorCode());
assertEquals("Resources " + ref.arn() + " has already been associated with tenant acme",
e.getMessage());
}
@Test
void disassociate_isIdempotent() {
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
service.disassociate(tenant, identityRef("example.com"), REGION);
service.associate(tenant, identityRef("example.com"), REGION, () -> {});
service.disassociate(tenant, identityRef("example.com"), REGION);
assertEquals(0, service.listTenantResources(tenant, null, REGION).size());
}
@Test
void listTenantResources_sortedByArn_andFilterable() {
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
service.associate(tenant, identityRef("example.com"), REGION, () -> {});
service.associate(tenant, SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":template/tpl", ACCOUNT, REGION),
REGION, () -> {});
service.associate(tenant, SesTenantService.parseResourceArn(
"arn:aws:ses:" + REGION + ":" + ACCOUNT + ":configuration-set/cs", ACCOUNT, REGION),
REGION, () -> {});
var all = service.listTenantResources(tenant, null, REGION);
assertEquals(List.of("configuration-set", "identity", "template"),
all.stream().map(a -> a.resourceType()).toList());
var filtered = service.listTenantResources(tenant, "identity", REGION);
assertEquals(1, filtered.size());
assertEquals("identity", filtered.get(0).resourceType());
}
@Test
void listResourceTenants_findsAllTenants_ofOneResource() {
Tenant a = service.createTenant("acme", List.of(), ACCOUNT, REGION);
Tenant b = service.createTenant("beta", List.of(), ACCOUNT, REGION);
service.associate(a, identityRef("example.com"), REGION, () -> {});
service.associate(b, identityRef("example.com"), REGION, () -> {});
var tenants = service.listResourceTenants(identityRef("example.com"), REGION);
assertEquals(2, tenants.size());
assertEquals("acme", tenants.get(0).tenantName());
assertEquals(a.tenantId(), tenants.get(0).tenantId());
}
@Test
void deleteTenant_cascadesAssociations_recreatedTenantSeesNone() {
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
service.associate(tenant, identityRef("example.com"), REGION, () -> {});
service.deleteTenant("acme", REGION);
assertEquals(0, service.listResourceTenants(identityRef("example.com"), REGION).size());
Tenant recreated = service.createTenant("acme", List.of(), ACCOUNT, REGION);
assertEquals(0, service.listTenantResources(recreated, null, REGION).size());
}
@Test
void associate_staleTenant_throwsNotFound() {
// A tenant resolved before a concurrent DeleteTenant (same or recreated name) must not be
// able to insert an orphan association: associate revalidates the TenantId under the lock.
Tenant stale = service.createTenant("acme", List.of(), ACCOUNT, REGION);
service.deleteTenant("acme", REGION);
AwsException gone = assertThrows(AwsException.class,
() -> service.associate(stale, identityRef("example.com"), REGION, () -> {}));
assertEquals("NotFoundException", gone.getErrorCode());
service.createTenant("acme", List.of(), ACCOUNT, REGION);
AwsException recreated = assertThrows(AwsException.class,
() -> service.associate(stale, identityRef("example.com"), REGION, () -> {}));
assertEquals("NotFoundException", recreated.getErrorCode());
}
@Test
void deleteBackingResource_blocksWhileAssociated_runsActionOtherwise() {
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
service.associate(tenant, identityRef("example.com"), REGION, () -> {});
AwsException blocked = assertThrows(AwsException.class, () -> service.deleteBackingResource(
"identity", "example.com", REGION, () -> {}));
assertEquals("Cannot delete <" + identityRef("example.com").arn() + "> because it has tenant "
+ "associations. Remove all tenant associations and try again.", blocked.getMessage());
boolean[] ran = {false};
service.deleteBackingResource("identity", "other.com", REGION, () -> ran[0] = true);
assertTrue(ran[0]);
}
@Test
void findAssociationForResource_backsDeleteGuards() {
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
service.associate(tenant, identityRef("example.com"), REGION, () -> {});
assertTrue(service.findAssociationForResource("identity", "example.com", REGION).isPresent());
assertTrue(service.findAssociationForResource("identity", "other.com", REGION).isEmpty());
assertTrue(service.findAssociationForResource("identity", "example.com", "eu-west-1").isEmpty());
}
@Test
void tenantForAssociation_hasServiceLevelEmptyMessage() {
AwsException empty = assertThrows(AwsException.class,
() -> service.tenantForAssociation("", REGION));
assertEquals("TenantName cannot be empty", empty.getMessage());
AwsException absent = assertThrows(AwsException.class,
() -> service.tenantForAssociation(null, REGION));
assertTrue(absent.getMessage().contains("'tenantName'"));
AwsException missing = assertThrows(AwsException.class,
() -> service.tenantForAssociation("ghost", REGION));
assertEquals("NotFoundException", missing.getErrorCode());
}
@Test
void validateFilterAndPaging_matchAwsMessages() {
assertEquals("Invalid resource type NOPE specified.",
assertThrows(AwsException.class,
() -> SesTenantService.validateResourceTypeFilter("NOPE")).getMessage());
assertEquals("Invalid resource type EMAIL_IDENTITY specified.",
assertThrows(AwsException.class,
() -> SesTenantService.validateResourceTypeFilter("EMAIL_IDENTITY")).getMessage());
assertTrue(assertThrows(AwsException.class,
() -> SesTenantService.validateListPaging(0, null))
.getMessage().contains("greater than or equal to 1"));
assertEquals("Invalid Next Token",
assertThrows(AwsException.class,
() -> SesTenantService.validateListPaging(null, "garbage")).getMessage());
}
}