Light, fluffy, and always free - The AWS Local Emulator alternative
fix(apigatewayv2): add missing JSON handler operations and stageName auto-deploy (#393)
TiMcd62fda11164
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.javaindex 5145d47ab7..090a00ac13 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandler.java@@ -11,6 +11,7 @@ import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import jakarta.ws.rs.core.Response;+import java.util.LinkedHashMap;import java.util.List;import java.util.Map;@@ -49,7 +50,10 @@ public class ApiGatewayV2JsonHandler {case "GetStages" -> handleGetStages(request, region);case "DeleteStage" -> handleDeleteStage(request, region);case "CreateDeployment" -> handleCreateDeployment(request, region);+ case "GetDeployment" -> handleGetDeployment(request, region);case "GetDeployments" -> handleGetDeployments(request, region);+ case "DeleteDeployment" -> handleDeleteDeployment(request, region);+ case "DeleteIntegration" -> handleDeleteIntegration(request, region);default -> JsonErrorResponseUtils.createUnknownOperationErrorResponse(action);};} catch (AwsException e) {@@ -61,7 +65,7 @@ public class ApiGatewayV2JsonHandler {private Response handleCreateApi(JsonNode request, String region) {@SuppressWarnings("unchecked")- Map<String, Object> map = objectMapper.convertValue(request, Map.class);+ Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));Api api = service.createApi(region, map);return Response.status(201).entity(toApiNode(api).toString()).build();}@@ -90,7 +94,7 @@ public class ApiGatewayV2JsonHandler {private Response handleCreateAuthorizer(JsonNode request, String region) {String apiId = request.path("ApiId").asText();@SuppressWarnings("unchecked")- Map<String, Object> map = objectMapper.convertValue(request, Map.class);+ Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));Authorizer auth = service.createAuthorizer(region, apiId, map);return Response.status(201).entity(toAuthorizerNode(auth).toString()).build();}@@ -122,7 +126,7 @@ public class ApiGatewayV2JsonHandler {private Response handleCreateRoute(JsonNode request, String region) {String apiId = request.path("ApiId").asText();@SuppressWarnings("unchecked")- Map<String, Object> map = objectMapper.convertValue(request, Map.class);+ Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));Route route = service.createRoute(region, apiId, map);return Response.status(201).entity(toRouteNode(route).toString()).build();}@@ -154,7 +158,7 @@ public class ApiGatewayV2JsonHandler {private Response handleCreateIntegration(JsonNode request, String region) {String apiId = request.path("ApiId").asText();@SuppressWarnings("unchecked")- Map<String, Object> map = objectMapper.convertValue(request, Map.class);+ Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));Integration integration = service.createIntegration(region, apiId, map);return Response.status(201).entity(toIntegrationNode(integration).toString()).build();}@@ -179,7 +183,7 @@ public class ApiGatewayV2JsonHandler {private Response handleCreateStage(JsonNode request, String region) {String apiId = request.path("ApiId").asText();@SuppressWarnings("unchecked")- Map<String, Object> map = objectMapper.convertValue(request, Map.class);+ Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));Stage stage = service.createStage(region, apiId, map);return Response.status(201).entity(toStageNode(stage).toString()).build();}@@ -211,11 +215,17 @@ public class ApiGatewayV2JsonHandler {private Response handleCreateDeployment(JsonNode request, String region) {String apiId = request.path("ApiId").asText();@SuppressWarnings("unchecked")- Map<String, Object> map = objectMapper.convertValue(request, Map.class);+ Map<String, Object> map = toLowerCamelCase(objectMapper.convertValue(request, Map.class));Deployment deployment = service.createDeployment(region, apiId, map);return Response.status(201).entity(toDeploymentNode(deployment).toString()).build();}+ private Response handleGetDeployment(JsonNode request, String region) {+ String apiId = request.path("ApiId").asText();+ String deploymentId = request.path("DeploymentId").asText();+ return Response.ok(toDeploymentNode(service.getDeployment(region, apiId, deploymentId)).toString()).build();+ }+private Response handleGetDeployments(JsonNode request, String region) {String apiId = request.path("ApiId").asText();List<Deployment> deployments = service.getDeployments(region, apiId);@@ -225,6 +235,20 @@ public class ApiGatewayV2JsonHandler {return Response.ok(root.toString()).build();}+ private Response handleDeleteDeployment(JsonNode request, String region) {+ String apiId = request.path("ApiId").asText();+ String deploymentId = request.path("DeploymentId").asText();+ service.deleteDeployment(region, apiId, deploymentId);+ return Response.noContent().build();+ }++ private Response handleDeleteIntegration(JsonNode request, String region) {+ String apiId = request.path("ApiId").asText();+ String integrationId = request.path("IntegrationId").asText();+ service.deleteIntegration(region, apiId, integrationId);+ return Response.noContent().build();+ }+// ──────────────────────────── Serializers ────────────────────────────private ObjectNode toApiNode(Api api) {@@ -297,4 +321,33 @@ public class ApiGatewayV2JsonHandler {return node;}+ /**+ * Converts PascalCase map keys to lowerCamelCase so the service layer's+ * field lookups work regardless of whether the request arrived via the+ * REST path (lowerCamelCase body) or JSON 1.1 path (PascalCase body).+ */+ @SuppressWarnings("unchecked")+ private Map<String, Object> toLowerCamelCase(Map<String, Object> map) {+ Map<String, Object> result = new LinkedHashMap<>();+ for (Map.Entry<String, Object> entry : map.entrySet()) {+ String key = entry.getKey();+ if (!key.isEmpty() && Character.isUpperCase(key.charAt(0))) {+ key = Character.toLowerCase(key.charAt(0)) + key.substring(1);+ }+ result.put(key, normalizeValue(entry.getValue()));+ }+ return result;+ }++ @SuppressWarnings("unchecked")+ private Object normalizeValue(Object value) {+ if (value instanceof Map) {+ return toLowerCamelCase((Map<String, Object>) value);+ }+ if (value instanceof List<?> list) {+ return list.stream().map(this::normalizeValue).toList();+ }+ return value;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.javaindex 279daeb29c..1dd93d5ed2 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java@@ -264,6 +264,16 @@ public class ApiGatewayV2Service {public Deployment createDeployment(String region, String apiId, Map<String, Object> request) {getApi(region, apiId);++ // Validate stage exists before creating deployment to avoid orphans+ String stageName = (String) request.get("stageName");+ Stage stage = null;+ if (stageName != null && !stageName.isBlank()) {+ stage = stageStore.get(stageKey(region, apiId, stageName))+ .orElseThrow(() -> new AwsException("NotFoundException",+ "Stage " + stageName + " not found", 404));+ }+Deployment deployment = new Deployment();deployment.setDeploymentId(shortId(8));deployment.setDeploymentStatus("DEPLOYED");@@ -271,6 +281,13 @@ public class ApiGatewayV2Service {deployment.setCreatedDate(System.currentTimeMillis());deploymentStore.put(deploymentKey(region, apiId, deployment.getDeploymentId()), deployment);++ if (stage != null) {+ stage.setDeploymentId(deployment.getDeploymentId());+ stage.setLastUpdatedDate(System.currentTimeMillis());+ stageStore.put(stageKey(region, apiId, stageName), stage);+ }+return deployment;}diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandlerTest.javanew file mode 100644index 0000000000..7964222c36--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2JsonHandlerTest.java@@ -0,0 +1,216 @@+package io.github.hectorvent.floci.services.apigatewayv2;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.http.ContentType;+import org.junit.jupiter.api.BeforeAll;+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.not;+import static org.hamcrest.Matchers.notNullValue;++/**+ * Tests for API Gateway v2 fixes:+ * - createDeployment stageName auto-deploy+ * - GetDeployment, DeleteDeployment, DeleteIntegration+ * - JSON 1.1 handler PascalCase normalization and missing switch cases+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class ApiGatewayV2JsonHandlerTest {++ private static final String AMZ_JSON = "application/x-amz-json-1.1";+ private static final String TARGET_PREFIX = "AmazonApiGatewayV2.";+ private static final String AUTH_HEADER =+ "AWS4-HMAC-SHA256 Credential=test/20260413/us-east-1/apigatewayv2/aws4_request";++ private static String apiId;+ private static String integrationId;+ private static String deploymentId;++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ // ──────────────────────────── JSON 1.1 handler path ────────────────────────────++ @Test+ @Order(1)+ void json11CreateApiWithPascalCaseKeys() {+ apiId = given()+ .contentType(AMZ_JSON)+ .header("X-Amz-Target", TARGET_PREFIX + "CreateApi")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {"Name":"json11-test","ProtocolType":"HTTP"}+ """)+ .when().post("/")+ .then()+ .statusCode(201)+ .body("ApiId", notNullValue())+ .body("Name", equalTo("json11-test"))+ .body("ProtocolType", equalTo("HTTP"))+ .extract().path("ApiId");+ }++ @Test+ @Order(2)+ void json11CreateIntegrationWithPascalCaseKeys() {+ integrationId = given()+ .contentType(AMZ_JSON)+ .header("X-Amz-Target", TARGET_PREFIX + "CreateIntegration")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {"ApiId":"%s","IntegrationType":"AWS_PROXY","IntegrationUri":"arn:aws:lambda:us-east-1:000000000000:function:test","PayloadFormatVersion":"2.0"}+ """.formatted(apiId))+ .when().post("/")+ .then()+ .statusCode(201)+ .body("IntegrationId", notNullValue())+ .extract().path("IntegrationId");+ }++ @Test+ @Order(3)+ void json11CreateDeploymentAndStage() {+ deploymentId = given()+ .contentType(AMZ_JSON)+ .header("X-Amz-Target", TARGET_PREFIX + "CreateDeployment")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {"ApiId":"%s","Description":"initial"}+ """.formatted(apiId))+ .when().post("/")+ .then()+ .statusCode(201)+ .body("DeploymentId", notNullValue())+ .extract().path("DeploymentId");++ given()+ .contentType(AMZ_JSON)+ .header("X-Amz-Target", TARGET_PREFIX + "CreateStage")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {"ApiId":"%s","StageName":"prod","DeploymentId":"%s"}+ """.formatted(apiId, deploymentId))+ .when().post("/")+ .then()+ .statusCode(201)+ .body("StageName", equalTo("prod"))+ .body("DeploymentId", equalTo(deploymentId));+ }++ @Test+ @Order(4)+ void json11GetDeployment() {+ given()+ .contentType(AMZ_JSON)+ .header("X-Amz-Target", TARGET_PREFIX + "GetDeployment")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {"ApiId":"%s","DeploymentId":"%s"}+ """.formatted(apiId, deploymentId))+ .when().post("/")+ .then()+ .statusCode(200)+ .body("DeploymentId", equalTo(deploymentId))+ .body("Description", equalTo("initial"));+ }++ // ──────────────────────────── stageName auto-deploy ────────────────────────────++ @Test+ @Order(5)+ void createDeploymentWithStageNameAutoDeploysToStage() {+ String newDeploymentId = given()+ .contentType(ContentType.JSON)+ .body("""+ {"description":"auto-deploy","stageName":"prod"}+ """)+ .when().post("/v2/apis/" + apiId + "/deployments")+ .then()+ .statusCode(201)+ .body("deploymentId", notNullValue())+ .extract().path("deploymentId");++ given()+ .when().get("/v2/apis/" + apiId + "/stages/prod")+ .then()+ .statusCode(200)+ .body("deploymentId", equalTo(newDeploymentId))+ .body("deploymentId", not(equalTo(deploymentId)));+ }++ @Test+ @Order(6)+ void createDeploymentWithMissingStageName404sWithoutOrphan() {+ // Count deployments before+ int beforeCount = given()+ .when().get("/v2/apis/" + apiId + "/deployments")+ .then().statusCode(200)+ .extract().jsonPath().getList("items").size();++ given()+ .contentType(ContentType.JSON)+ .body("""+ {"description":"bad stage","stageName":"nonexistent"}+ """)+ .when().post("/v2/apis/" + apiId + "/deployments")+ .then()+ .statusCode(404);++ // Verify no orphan deployment was created+ int afterCount = given()+ .when().get("/v2/apis/" + apiId + "/deployments")+ .then().statusCode(200)+ .extract().jsonPath().getList("items").size();++ org.junit.jupiter.api.Assertions.assertEquals(beforeCount, afterCount,+ "No orphan deployment should be created when stageName is invalid");+ }++ // ──────────────────────────── JSON 1.1 delete operations ────────────────────────────++ @Test+ @Order(7)+ void json11DeleteIntegration() {+ given()+ .contentType(AMZ_JSON)+ .header("X-Amz-Target", TARGET_PREFIX + "DeleteIntegration")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {"ApiId":"%s","IntegrationId":"%s"}+ """.formatted(apiId, integrationId))+ .when().post("/")+ .then()+ .statusCode(204);+ }++ @Test+ @Order(8)+ void json11DeleteDeployment() {+ given()+ .contentType(AMZ_JSON)+ .header("X-Amz-Target", TARGET_PREFIX + "DeleteDeployment")+ .header("Authorization", AUTH_HEADER)+ .body("""+ {"ApiId":"%s","DeploymentId":"%s"}+ """.formatted(apiId, deploymentId))+ .when().post("/")+ .then()+ .statusCode(204);+ }++ @Test+ @Order(9)+ void cleanup() {+ given().when().delete("/v2/apis/" + apiId + "/stages/prod").then().statusCode(204);+ given().when().delete("/v2/apis/" + apiId).then().statusCode(204);+ }+}