Light, fluffy, and always free - The AWS Local Emulator alternative
fix(servicequotas): implement GetRequestedServiceQuotaChange (#5561)
Matheus Oliveira60d4d6b7a5c2
diff --git a/docs/services/index.md b/docs/services/index.mdindex f714e64613..20861bc403 100644--- a/docs/services/index.md+++ b/docs/services/index.md@@ -111,7 +111,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac| [CodePipeline](codepipeline.md) | `POST /` + `X-Amz-Target: CodePipeline_20150709.*` | JSON 1.1 | 44 || [AWS Network Firewall](network-firewall.md) | `POST /` + `X-Amz-Target: NetworkFirewall_20201112.*` | JSON 1.0 | 27 || [AWS Service Catalog](service-catalog.md) | `POST /` + `X-Amz-Target: AWS242ServiceCatalogService.*` | JSON 1.1 | 89 |-| [Service Quotas](servicequotas.md) | `POST /` + `X-Amz-Target: ServiceQuotasV20190624.*` | JSON 1.1 | 6 |+| [Service Quotas](servicequotas.md) | `POST /` + `X-Amz-Target: ServiceQuotasV20190624.*` | JSON 1.1 | 7 || [AWS Budgets](budgets.md) | `POST /` + `X-Amz-Target: AWSBudgetServiceGateway.*` | JSON 1.1 | 26 || [AWS RAM](ram.md) | `POST /{operationname}` (lowercase), `DELETE /deleteresourceshare` | REST JSON | 15 || [Control Catalog](controlcatalog.md) | `/get-control`, `/list-controls` | REST JSON | 2 |diff --git a/docs/services/servicequotas.md b/docs/services/servicequotas.mdindex 12d2c6dd2f..8bd4ae3902 100644--- a/docs/services/servicequotas.md+++ b/docs/services/servicequotas.md@@ -16,8 +16,10 @@ network ACL", 20) carry their AWS default values, so tooling that compares a desthe default before requesting an increase takes the same path it would against AWS.Applied quotas and AWS default quotas return the same data, and quota values are static.-`RequestServiceQuotaIncrease` is accepted and validated but does not change any quota value —-see Limitations.+`RequestServiceQuotaIncrease` is accepted and validated, records the request, and lets you read+it back with `GetRequestedServiceQuotaChange` or list it with+`ListRequestedServiceQuotaChangeHistoryByQuota`, but it does not change any quota value. See+Limitations.## Supported Actions@@ -28,19 +30,23 @@ see Limitations.| `GetServiceQuota` | Returns one quota by service and quota code, else `NoSuchResourceException` || `GetAWSDefaultServiceQuota` | Same as GetServiceQuota; defaults equal applied values || `ListAWSDefaultServiceQuotas` | Same as ListServiceQuotas; defaults equal applied values |-| `RequestServiceQuotaIncrease` | Validates and echoes an increase request as `PENDING`; not persisted, quota unchanged |-| `ListRequestedServiceQuotaChangeHistoryByQuota` | Validates the service/quota pair and returns the locally recorded request history; currently empty for the static catalog |+| `RequestServiceQuotaIncrease` | Validates and stores an increase request as `PENDING`; a second open request for the same quota is refused with `ResourceAlreadyExistsException` |+| `GetRequestedServiceQuotaChange` | Returns a stored request by id, else `NoSuchResourceException` |+| `ListRequestedServiceQuotaChangeHistoryByQuota` | Lists the stored requests for a service/quota pair, newest first |<!-- floci:actions:end -->## Limitations-- **Quota increase requests are not persisted.** `RequestServiceQuotaIncrease` validates its- input, resolves the quota, and returns a well-formed `RequestedQuota`, but keeps no state.- The request is observable only in the response that creates it.-- **`Status` is always `PENDING` and never advances.** Nothing processes requests, so no- request ever reaches `APPROVED`, `CASE_OPENED`, or `DENIED`. `PENDING` is what real AWS- returns on creation, so a caller that only reads the creation response sees faithful data;- a caller that polls for completion will wait forever.+- **Quota increase requests are persisted per region, but `Status` never advances.** A+ successful `RequestServiceQuotaIncrease` is stored under its own id and can be read back with+ `GetRequestedServiceQuotaChange` or listed with `ListRequestedServiceQuotaChangeHistoryByQuota`+ in the region that created it. Because a request stays `PENDING`, a second+ `RequestServiceQuotaIncrease` for the same quota in the same region is refused with+ `ResourceAlreadyExistsException`, matching AWS's one-open-request-per-quota rule. Nothing+ processes a request, however, so it stays `PENDING` and+ never reaches `APPROVED`, `CASE_OPENED`, or `DENIED`. `PENDING` is what real AWS returns on+ creation, so a caller that reads the creation response sees faithful data; a caller that polls+ for completion will wait forever.- **Unknown service codes generate a quota catalog rather than failing.** This is deliberate, sothe code has to be well-formed to be trusted: `ServiceCode` is validated against the modeled`[a-zA-Z][a-zA-Z0-9-]{1,63}` (max 63 characters) and a malformed one is rejected with@@ -48,8 +54,8 @@ see Limitations.- **A requested increase does not change the quota.** `GetServiceQuota` continues to returnthe catalog value after a successful increase request. Quota values remain static by designso pipelines never stall on an unenforced limit.-- `GetRequestedServiceQuotaChange` and `ListRequestedServiceQuotaChangeHistory` are not- implemented — there is no request store for them to read.+- `ListRequestedServiceQuotaChangeHistory` (the account-wide variant without a quota filter) is+ not implemented.- `CaseId` is never returned; no support case is opened. `SupportCaseAllowed` in the request isaccepted and ignored, as the emulator has no case-opening path either way.- The quota-increase template operations (`PutServiceQuotaIncreaseRequestIntoTemplate` anddiff --git a/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasJsonHandler.javaindex d2b49a89b3..4a1f067310 100644--- a/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasJsonHandler.java@@ -33,10 +33,14 @@ public class ServiceQuotasJsonHandler {case "GetAWSDefaultServiceQuota" -> handleGetServiceQuota(request, region, accountId);case "ListAWSDefaultServiceQuotas" -> handleListServiceQuotas(request, region, accountId);case "RequestServiceQuotaIncrease" -> handleRequestServiceQuotaIncrease(request, region, accountId);+ case "GetRequestedServiceQuotaChange" ->+ Response.ok(service.getRequestedServiceQuotaChange(+ stringOrNull(request, "RequestId"), region)).build();case "ListRequestedServiceQuotaChangeHistoryByQuota" ->Response.ok(service.listRequestedServiceQuotaChangeHistoryByQuota(stringOrNull(request, "ServiceCode"), stringOrNull(request, "QuotaCode"),- stringOrNull(request, "NextToken"), integerOrNull(request, "MaxResults"))).build();+ stringOrNull(request, "NextToken"), integerOrNull(request, "MaxResults"),+ region)).build();default -> Response.status(400).entity(new AwsErrorResponse("UnknownOperationException","Unknown operation: ServiceQuotasV20190624." + action))diff --git a/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasService.java b/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasService.javaindex 34398d4ac8..0f0e8bcc89 100644--- a/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasService.java+++ b/src/main/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasService.java@@ -1,11 +1,14 @@package io.github.hectorvent.floci.services.servicequotas;+import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.Base64Url;+import io.github.hectorvent.floci.core.storage.StorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -15,6 +18,7 @@ import java.util.ArrayList;import java.util.Base64;import java.util.List;import java.util.Map;+import java.util.UUID;import java.util.regex.Pattern;import java.util.zip.CRC32;@@ -74,10 +78,15 @@ public class ServiceQuotasService {"Concurrent operations");private final ObjectMapper objectMapper;+ private final StorageBackend<String, ObjectNode> requestedQuotaStore;+ private final Object openRequestLock = new Object();@Inject- public ServiceQuotasService(ObjectMapper objectMapper) {+ public ServiceQuotasService(StorageFactory storageFactory, ObjectMapper objectMapper) {this.objectMapper = objectMapper;+ this.requestedQuotaStore = storageFactory.create("servicequotas",+ "servicequotas-requested-changes.json",+ new TypeReference<Map<String, ObjectNode>>() {});}public ObjectNode listServiceQuotas(String serviceCode, String quotaCodeFilter, String nextToken,@@ -87,7 +96,7 @@ public class ServiceQuotasService {if (quotaCodeFilter != null && !quotaCodeFilter.isEmpty()) {quotas = quotas.stream().filter(q -> q.quotaCode().equals(quotaCodeFilter)).toList();}- Page page = paginate(quotas, nextToken, maxResults);+ Page<QuotaDefinition> page = paginate(quotas, nextToken, maxResults);ObjectNode response = objectMapper.createObjectNode();ArrayNode array = response.putArray("Quotas");@@ -171,7 +180,7 @@ public class ServiceQuotasService {}}- private static Page paginate(List<QuotaDefinition> items, String nextToken, Integer maxResults) {+ private static <T> Page<T> paginate(List<T> items, String nextToken, Integer maxResults) {if (maxResults != null && (maxResults < 1 || maxResults > 100)) {throw new AwsException("IllegalArgumentException","Invalid input: MaxResults must be between 1 and 100.", 400);@@ -202,7 +211,8 @@ public class ServiceQuotasService {}public ObjectNode listRequestedServiceQuotaChangeHistoryByQuota(String serviceCode, String quotaCode,- String nextToken, Integer maxResults) {+ String nextToken, Integer maxResults,+ String region) {requireServiceCode(serviceCode);if (quotaCode == null || quotaCode.isBlank()) {throw new AwsException("IllegalArgumentException", "Invalid input: QuotaCode must not be empty.", 400);@@ -212,21 +222,29 @@ public class ServiceQuotasService {throw new AwsException("NoSuchResourceException","The request failed because the specified service quota does not exist.", 400);}- if (maxResults != null && (maxResults < 1 || maxResults > 100)) {- throw new AwsException("IllegalArgumentException", "Invalid input: MaxResults must be between 1 and 100.", 400);- }- if (nextToken != null && !nextToken.isEmpty()) {- throw new AwsException("InvalidPaginationTokenException", "Invalid NextToken.", 400);+ List<ObjectNode> matching = new ArrayList<>();+ for (ObjectNode requested : requestedQuotaStore.scan(key -> key.startsWith(region + "/"))) {+ if (serviceCode.equals(requested.path("ServiceCode").asText())+ && quotaCode.equals(requested.path("QuotaCode").asText())) {+ matching.add(requested);+ }}+ Page<ObjectNode> page = paginate(matching, nextToken, maxResults);ObjectNode response = objectMapper.createObjectNode();- response.putArray("RequestedQuotas");+ ArrayNode array = response.putArray("RequestedQuotas");+ for (ObjectNode requested : page.items()) {+ array.add(requested.deepCopy());+ }+ if (page.nextToken() != null) {+ response.put("NextToken", page.nextToken());+ }return response;}record QuotaDefinition(String quotaCode, String quotaName, double value, boolean globalQuota) {}- private record Page(List<QuotaDefinition> items, String nextToken) {+ private record Page<T>(List<T> items, String nextToken) {}public ObjectNode requestServiceQuotaIncrease(String serviceCode, String quotaCode, Double desiredValue,@@ -250,9 +268,7 @@ public class ServiceQuotasService {.orElseThrow(() -> new AwsException("NoSuchResourceException","The request failed because the specified service quota does not exist.", 400));- CRC32 crc = new CRC32();- crc.update((serviceCode + "/" + quotaCode).getBytes(StandardCharsets.UTF_8));- String id = "%08X".formatted(crc.getValue());+ String id = UUID.randomUUID().toString();long now = Instant.now().getEpochSecond();@@ -279,8 +295,43 @@ public class ServiceQuotasService {requestedQuota.set("QuotaContext", context);}+ storeOpenRequest(region, serviceCode, quotaCode, id, requestedQuota);+ObjectNode response = objectMapper.createObjectNode();response.set("RequestedQuota", requestedQuota);return response;}++ /**+ * AWS allows only one open increase request per quota, so the duplicate check and the+ * write must stand or fall as a unit: concurrent callers for the same quota must not+ * both pass the check.+ */+ private void storeOpenRequest(String region, String serviceCode, String quotaCode, String id,+ ObjectNode requestedQuota) {+ synchronized (openRequestLock) {+ boolean hasOpen = requestedQuotaStore.scan(key -> key.startsWith(region + "/")).stream()+ .anyMatch(r -> serviceCode.equals(r.path("ServiceCode").asText())+ && quotaCode.equals(r.path("QuotaCode").asText())+ && "PENDING".equals(r.path("Status").asText()));+ if (hasOpen) {+ throw new AwsException("ResourceAlreadyExistsException",+ "Only one open service quota increase request is allowed per quota.", 400);+ }+ requestedQuotaStore.put(region + "/" + id, requestedQuota.deepCopy());+ }+ }++ public ObjectNode getRequestedServiceQuotaChange(String requestId, String region) {+ if (requestId == null || requestId.isEmpty()) {+ throw new AwsException("IllegalArgumentException",+ "Invalid input: RequestId must not be empty.", 400);+ }+ ObjectNode requestedQuota = requestedQuotaStore.get(region + "/" + requestId)+ .orElseThrow(() -> new AwsException("NoSuchResourceException",+ "The request failed because the specified request ID does not exist.", 400));+ ObjectNode response = objectMapper.createObjectNode();+ response.set("RequestedQuota", requestedQuota.deepCopy());+ return response;+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/servicequotas/RequestServiceQuotaIncreaseConsumerTest.java b/src/test/java/io/github/hectorvent/floci/services/servicequotas/RequestServiceQuotaIncreaseConsumerTest.javaindex 9435403de4..049370a646 100644--- a/src/test/java/io/github/hectorvent/floci/services/servicequotas/RequestServiceQuotaIncreaseConsumerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/servicequotas/RequestServiceQuotaIncreaseConsumerTest.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.servicequotas;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import static io.restassured.RestAssured.given;@@ -42,6 +43,11 @@ class RequestServiceQuotaIncreaseConsumerTest {RestAssuredJsonUtils.configureAwsContentTypes();}+ @BeforeEach+ void resetState() {+ given().post("/_floci/state/reset").then().statusCode(200);+ }+private static io.restassured.response.Response request(String body) {return given().contentType(CONTENT_TYPE)@@ -58,7 +64,7 @@ class RequestServiceQuotaIncreaseConsumerTest {+ "\",\"DesiredValue\":9000}").then().statusCode(200)- .body("RequestedQuota.Id", matchesRegex("[0-9A-F]{8}"))+ .body("RequestedQuota.Id", notNullValue()).body("RequestedQuota.ServiceCode", equalTo("codebuild")).body("RequestedQuota.ServiceName", equalTo("AWS CodeBuild")).body("RequestedQuota.QuotaCode", equalTo(CONCURRENT_BUILDS_QUOTA))@@ -87,15 +93,14 @@ class RequestServiceQuotaIncreaseConsumerTest {.body("RequestedQuota.ServiceCode", equalTo("widgetfactory"));}- /** The id must be stable so a caller can correlate a repeated request. */+ /** AWS allows one open increase request per quota; a repeat is refused. */@Test- void requestIncrease_sameQuotaTwice_returnsSameId() {+ void requestIncrease_sameQuotaTwice_returnsResourceAlreadyExists() {String body = "{\"ServiceCode\":\"lambda\",\"QuotaCode\":\"L-B99A9384\",\"DesiredValue\":2000}";- String first = request(body).then().statusCode(200)- .extract().path("RequestedQuota.Id");- String second = request(body).then().statusCode(200)- .extract().path("RequestedQuota.Id");- assertEquals(first, second);+ request(body).then().statusCode(200);+ request(body).then()+ .statusCode(400)+ .body("__type", equalTo("ResourceAlreadyExistsException"));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasIntegrationTest.javaindex 303f1d1937..253cd29150 100644--- a/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasIntegrationTest.java@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;import io.restassured.response.Response;import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import java.util.List;@@ -14,6 +15,7 @@ import static org.hamcrest.Matchers.greaterThanOrEqualTo;import static org.hamcrest.Matchers.notNullValue;import static org.hamcrest.Matchers.startsWith;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotEquals;import static org.junit.jupiter.api.Assertions.assertNotNull;import static org.junit.jupiter.api.Assertions.assertNull;@@ -35,6 +37,11 @@ class ServiceQuotasIntegrationTest {RestAssuredJsonUtils.configureAwsContentTypes();}+ @BeforeEach+ void resetState() {+ given().post("/_floci/state/reset").then().statusCode(200);+ }+@Testvoid listServiceQuotas_codebuild_includesConcurrentlyRunningBuilds() {String quotaPath = "Quotas.find { it.QuotaCode == '" + CONCURRENT_BUILDS_QUOTA + "' }";@@ -212,7 +219,7 @@ class ServiceQuotasIntegrationTest {.contentType(CONTENT_TYPE).header("X-Amz-Target", "ServiceQuotasV20190624.ListRequestedServiceQuotaChangeHistoryByQuota").header("Authorization", AUTH_HEADER)- .body("{\"ServiceCode\":\"organizations\",\"QuotaCode\":\"L-E619E033\"}")+ .body("{\"ServiceCode\":\"vpc\",\"QuotaCode\":\"L-FE5A380F\"}").when().post("/").then()@@ -226,7 +233,7 @@ class ServiceQuotasIntegrationTest {.contentType(CONTENT_TYPE).header("X-Amz-Target", "ServiceQuotasV20190624.ListRequestedServiceQuotaChangeHistoryByQuota").header("Authorization", AUTH_HEADER)- .body("{\"ServiceCode\":\"organizations\",\"QuotaCode\":\"L-E619E033\",\"NextToken\":\"MQ\"}")+ .body("{\"ServiceCode\":\"vpc\",\"QuotaCode\":\"L-FE5A380F\",\"NextToken\":\"MQ\"}").when().post("/").then()@@ -264,6 +271,117 @@ class ServiceQuotasIntegrationTest {.body("RequestedQuota.GlobalQuota", equalTo(true));}+ @Test+ void getRequestedServiceQuotaChangeReturnsStoredRequest() {+ String requestId =+ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.RequestServiceQuotaIncrease")+ .header("Authorization", AUTH_HEADER)+ .body("{\"ServiceCode\":\"codebuild\",\"QuotaCode\":\"L-2DC20C30\",\"DesiredValue\":9000}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract()+ .path("RequestedQuota.Id");++ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.GetRequestedServiceQuotaChange")+ .header("Authorization", AUTH_HEADER)+ .body("{\"RequestId\":\"" + requestId + "\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("RequestedQuota.Id", equalTo(requestId))+ .body("RequestedQuota.ServiceCode", equalTo("codebuild"))+ .body("RequestedQuota.QuotaCode", equalTo("L-2DC20C30"))+ .body("RequestedQuota.DesiredValue", notNullValue())+ .body("RequestedQuota.Status", equalTo("PENDING"));+ }++ @Test+ void getRequestedServiceQuotaChange_unknownId_returnsNoSuchResource() {+ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.GetRequestedServiceQuotaChange")+ .header("Authorization", AUTH_HEADER)+ .body("{\"RequestId\":\"DEADBEEF\"}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("NoSuchResourceException"));+ }++ @Test+ void getRequestedServiceQuotaChange_emptyRequestId_returnsIllegalArgument() {+ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.GetRequestedServiceQuotaChange")+ .header("Authorization", AUTH_HEADER)+ .body("{\"RequestId\":\"\"}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("IllegalArgumentException"));+ }++ @Test+ void requestedIncreaseAppearsInHistoryForItsQuota() {+ String requestId =+ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.RequestServiceQuotaIncrease")+ .header("Authorization", AUTH_HEADER)+ .body("{\"ServiceCode\":\"vpc\",\"QuotaCode\":\"L-2AEEBF1A\",\"DesiredValue\":40}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract()+ .path("RequestedQuota.Id");++ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.ListRequestedServiceQuotaChangeHistoryByQuota")+ .header("Authorization", AUTH_HEADER)+ .body("{\"ServiceCode\":\"vpc\",\"QuotaCode\":\"L-2AEEBF1A\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("RequestedQuotas.find { it.Id == '" + requestId + "' }.QuotaCode", equalTo("L-2AEEBF1A"))+ .body("RequestedQuotas.find { it.Id == '" + requestId + "' }.ServiceCode", equalTo("vpc"));+ }++ @Test+ void repeatedRequestsForSameQuotaReturnResourceAlreadyExists() {+ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.RequestServiceQuotaIncrease")+ .header("Authorization", AUTH_HEADER)+ .body("{\"ServiceCode\":\"lambda\",\"QuotaCode\":\"L-B99A9384\",\"DesiredValue\":7000}")+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .contentType(CONTENT_TYPE)+ .header("X-Amz-Target", "ServiceQuotasV20190624.RequestServiceQuotaIncrease")+ .header("Authorization", AUTH_HEADER)+ .body("{\"ServiceCode\":\"lambda\",\"QuotaCode\":\"L-B99A9384\",\"DesiredValue\":8000}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ResourceAlreadyExistsException"));+ }+@Testvoid getServiceQuota_unknownQuotaCode_returnsNoSuchResource() {given()diff --git a/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasServiceTest.javaindex f1b31d84b9..4b027d34d5 100644--- a/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/servicequotas/ServiceQuotasServiceTest.java@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.testing.InMemoryStorageFactory;import org.junit.jupiter.api.Test;import java.util.HashMap;@@ -16,7 +17,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;class ServiceQuotasServiceTest {- private final ServiceQuotasService service = new ServiceQuotasService(new ObjectMapper());+ private static final String ACCOUNT_ID = "000000000000";++ private final ServiceQuotasService service =+ new ServiceQuotasService(new InMemoryStorageFactory(ACCOUNT_ID), new ObjectMapper());@Testvoid syntheticQuotaCodesAreStableAndWellFormed() {@@ -105,8 +109,82 @@ class ServiceQuotasServiceTest {void wellFormedServiceCodesAreStillAccepted() {// Including an unknown-but-valid code: generating a catalog for those is deliberate.for (String good : List.of("codebuild", "widgetfactory", "elasticloadbalancing", "AWS-Thing", "ec")) {- assertTrue(service.listServiceQuotas(good, null, null, null, "us-east-1", "000000000000")+ assertTrue(service.listServiceQuotas(good, null, null, null, "us-east-1", ACCOUNT_ID).withArray("Quotas").size() > 0, good);}}++ @Test+ void requestedIncreaseIsReadableBackById() {+ ObjectNode created = service.requestServiceQuotaIncrease(+ "codebuild", "L-2DC20C30", 9000.0, null, "us-east-1", ACCOUNT_ID)+ .withObject("RequestedQuota");+ String id = created.path("Id").asText();++ ObjectNode read = service.getRequestedServiceQuotaChange(id, "us-east-1").withObject("RequestedQuota");+ assertEquals(id, read.path("Id").asText());+ assertEquals("codebuild", read.path("ServiceCode").asText());+ assertEquals("L-2DC20C30", read.path("QuotaCode").asText());+ assertEquals("Concurrently running builds", read.path("QuotaName").asText());+ assertEquals(9000.0, read.path("DesiredValue").asDouble());+ assertEquals("PENDING", read.path("Status").asText());+ }++ @Test+ void repeatedRequestsForTheSameQuotaReturnResourceAlreadyExists() {+ service.requestServiceQuotaIncrease(+ "codebuild", "L-2DC20C30", 9000.0, null, "us-east-1", ACCOUNT_ID);+ AwsException e = assertThrows(AwsException.class, () -> service.requestServiceQuotaIncrease(+ "codebuild", "L-2DC20C30", 9500.0, null, "us-east-1", ACCOUNT_ID));+ assertEquals("ResourceAlreadyExistsException", e.getErrorCode());+ assertEquals(400, e.getHttpStatus());+ }++ @Test+ void requestStoredInOneRegionIsNotVisibleInAnother() {+ String id = service.requestServiceQuotaIncrease(+ "codebuild", "L-2DC20C30", 9000.0, null, "us-east-1", ACCOUNT_ID)+ .path("RequestedQuota").path("Id").asText();++ AwsException e = assertThrows(AwsException.class,+ () -> service.getRequestedServiceQuotaChange(id, "eu-west-1"));+ assertEquals("NoSuchResourceException", e.getErrorCode());+ }++ @Test+ void historyListsOnlyTheRequestedQuota() {+ service.requestServiceQuotaIncrease(+ "codebuild", "L-2DC20C30", 9000.0, null, "us-east-1", ACCOUNT_ID);+ service.requestServiceQuotaIncrease(+ "lambda", "L-B99A9384", 8000.0, null, "us-east-1", ACCOUNT_ID);++ JsonNode codebuild = service.listRequestedServiceQuotaChangeHistoryByQuota(+ "codebuild", "L-2DC20C30", null, null, "us-east-1");+ assertEquals(1, codebuild.path("RequestedQuotas").size());+ assertEquals("codebuild", codebuild.path("RequestedQuotas").get(0).path("ServiceCode").asText());++ JsonNode lambda = service.listRequestedServiceQuotaChangeHistoryByQuota(+ "lambda", "L-B99A9384", null, null, "us-east-1");+ assertEquals(1, lambda.path("RequestedQuotas").size());+ assertEquals("lambda", lambda.path("RequestedQuotas").get(0).path("ServiceCode").asText());+ }++ @Test+ void missingRequestIdThrowsIllegalArgument() {+ AwsException empty = assertThrows(AwsException.class,+ () -> service.getRequestedServiceQuotaChange("", "us-east-1"));+ assertEquals("IllegalArgumentException", empty.getErrorCode());+ assertEquals(400, empty.getHttpStatus());+ AwsException nullId = assertThrows(AwsException.class,+ () -> service.getRequestedServiceQuotaChange(null, "us-east-1"));+ assertEquals("IllegalArgumentException", nullId.getErrorCode());+ }++ @Test+ void unknownRequestIdThrowsNoSuchResource() {+ AwsException e = assertThrows(AwsException.class,+ () -> service.getRequestedServiceQuotaChange("DEADBEEF", "us-east-1"));+ assertEquals("NoSuchResourceException", e.getErrorCode());+ assertEquals(400, e.getHttpStatus());+ }}diff --git a/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactory.java b/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactory.javanew file mode 100644index 0000000000..c22e5b6e8f--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactory.java@@ -0,0 +1,48 @@+package io.github.hectorvent.floci.testing;++import com.fasterxml.jackson.core.type.TypeReference;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.StorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.core.storage.WriteProfile;++import java.util.HashMap;+import java.util.Map;++/**+ * Test {@link StorageFactory} that hands out in-memory stores so a service can be constructed+ * without touching disk.+ *+ * <p>Stores are keyed by file name and shared, mirroring the runtime factory where two services+ * created on the same factory see each other's writes. Every store is scoped to the account id+ * passed to the constructor, so a service that reads through {@link AccountAwareStorageBackend}+ * behaves as it does under the emulator.+ *+ * <p>This is the shared replacement for the private in-memory {@code StorageFactory} copies that+ * individual service tests used to define; migrating those to it is a separate follow-up.+ */+public final class InMemoryStorageFactory extends StorageFactory {++ private final String accountId;+ private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();++ public InMemoryStorageFactory(String accountId) {+ super(null, null);+ this.accountId = accountId;+ }++ @Override+ public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,+ TypeReference<Map<String, V>> typeReference) {+ return create(serviceName, fileName, typeReference, WriteProfile.DEFAULT);+ }++ @Override+ @SuppressWarnings("unchecked")+ public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,+ TypeReference<Map<String, V>> typeReference,+ WriteProfile profile) {+ return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,+ ignored -> AccountAwareStorageBackend.inMemory(accountId));+ }+}