fix(apigatewayv2): add missing JSON handler operations and stageName auto-deploy (#393)

* fix(apigatewayv2): add missing JSON handler operations and stageName auto-deploy

The JSON 1.1 handler was missing GetDeployment, DeleteDeployment, and
DeleteIntegration operations. Callers using the X-Amz-Target path
(non-SDK, Terraform, CLI --cli-input-json) would get UnknownOperation
errors for these actions.

Additionally, the handler passed PascalCase map keys from the JSON 1.1
payload directly to service methods that expect lowerCamelCase. Added
key normalization so both REST and JSON 1.1 paths produce consistent
input for the service layer.

createDeployment now respects the stageName parameter: when provided,
it auto-deploys by updating the named stage's deploymentId and
lastUpdatedDate, matching AWS behavior. Throws NotFoundException if
the stage doesn't exist.

- Add GetDeployment, DeleteDeployment, DeleteIntegration switch cases
- Add toLowerCamelCase() key normalizer for JSON 1.1 map inputs
- Implement stageName auto-deploy in createDeployment
- 9 new integration tests covering the full deployment lifecycle,
  stageName auto-deploy, missing stage 404, and delete operations

* fix: address review feedback on apigatewayv2 changes

- Validate stage exists before persisting deployment to prevent
  orphan deployments on invalid stageName (Codex finding)
- Add JSON 1.1 path tests using X-Amz-Target and PascalCase keys
  to directly exercise the handler's normalization and new switch
  cases (both reviewers)
- Handle List values in toLowerCamelCase normalization for nested
  object arrays (Gemini suggestion)
- Add orphan deployment assertion to missing-stage 404 test
This commit is contained in:
TiM authored and GitHub committed 2026-04-13 11:01:03 -05:00
1 parent f053db583a
commit cd62fda111
3 files changed
+292 -6

No files matched your search

@@ -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);
}
}