diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayMethodResponseCompatibilityTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayMethodResponseCompatibilityTest.java new file mode 100644 index 0000000000..29d73ac3f5 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayMethodResponseCompatibilityTest.java @@ -0,0 +1,51 @@ +package com.floci.test; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.apigateway.ApiGatewayClient; +import software.amazon.awssdk.services.apigateway.model.PatchOperation; +import software.amazon.awssdk.services.apigateway.model.Resource; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("API Gateway method responses") +class ApiGatewayMethodResponseCompatibilityTest { + + @Test + void responseParametersRoundTripThroughTheSdk() { + try (ApiGatewayClient apiGateway = TestFixtures.apiGatewayClient()) { + String apiId = apiGateway.createRestApi(request -> request + .name(TestFixtures.uniqueName("method-response"))).id(); + try { + String rootId = apiGateway.getResources(request -> request.restApiId(apiId)) + .items().stream().filter(resource -> "/".equals(resource.path())) + .findFirst().map(Resource::id).orElseThrow(); + String resourceId = apiGateway.createResource(request -> request + .restApiId(apiId).parentId(rootId).pathPart("items")).id(); + apiGateway.putMethod(request -> request.restApiId(apiId) + .resourceId(resourceId).httpMethod("GET").authorizationType("NONE")); + + Map initial = Map.of("method.response.header.X-First", false); + assertThat(apiGateway.putMethodResponse(request -> request.restApiId(apiId) + .resourceId(resourceId).httpMethod("GET").statusCode("200") + .responseParameters(initial)).responseParameters()).isEqualTo(initial); + + Map updated = Map.of( + "method.response.header.X-First", false, + "method.response.header.X-Second", true); + assertThat(apiGateway.updateMethodResponse(request -> request.restApiId(apiId) + .resourceId(resourceId).httpMethod("GET").statusCode("200") + .patchOperations(PatchOperation.builder().op("add") + .path("/responseParameters/method.response.header.X-Second") + .value("true").build())).responseParameters()).isEqualTo(updated); + assertThat(apiGateway.getMethodResponse(request -> request.restApiId(apiId) + .resourceId(resourceId).httpMethod("GET").statusCode("200")) + .responseParameters()).isEqualTo(updated); + } finally { + apiGateway.deleteRestApi(request -> request.restApiId(apiId)); + } + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java index 843127925b..39cc902340 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java @@ -173,6 +173,22 @@ public class ApiGatewayController { } } + @PATCH + @Path("/restapis/{apiId}/resources/{resourceId}/methods/{httpMethod}/responses/{statusCode}") + public Response updateMethodResponse(@Context HttpHeaders headers, + @PathParam("apiId") String apiId, + @PathParam("resourceId") String resourceId, + @PathParam("httpMethod") String httpMethod, + @PathParam("statusCode") String statusCode, + String body) { + String region = regionResolver.resolveRegion(headers); + List> patchOperations = parsePatchOperations(body); + MethodResponse response = service.updateMethodResponse( + region, apiId, resourceId, httpMethod, statusCode, patchOperations); + return Response.status(201).entity(toMethodResponseNode(response).toString()) + .type(MediaType.APPLICATION_JSON).build(); + } + @DELETE @Path("/restapis/{apiId}/resources/{resourceId}/methods/{httpMethod}/responses/{statusCode}") public Response deleteMethodResponse(@Context HttpHeaders headers, @@ -2235,6 +2251,10 @@ public class ApiGatewayController { private ObjectNode toMethodResponseNode(MethodResponse r) { ObjectNode node = objectMapper.createObjectNode(); node.put("statusCode", r.statusCode()); + if (r.responseParameters() != null && !r.responseParameters().isEmpty()) { + ObjectNode parameters = node.putObject("responseParameters"); + r.responseParameters().forEach(parameters::put); + } return node; } diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java index c2bfbc8505..6adb3a240d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java @@ -541,7 +541,7 @@ public class ApiGatewayService implements ResourceProvider { resourceStore.put(resourceKey(region, apiId, resourceId), resource); } - public MethodResponse putMethodResponse(String region, String apiId, String resourceId, + public synchronized MethodResponse putMethodResponse(String region, String apiId, String resourceId, String httpMethod, String statusCode, Map request) { MethodConfig method = getMethod(region, apiId, resourceId, httpMethod); @@ -564,7 +564,41 @@ public class ApiGatewayService implements ResourceProvider { return mr; } - public void deleteMethodResponse(String region, String apiId, String resourceId, + public synchronized MethodResponse updateMethodResponse(String region, String apiId, String resourceId, + String httpMethod, String statusCode, + List> patchOperations) { + MethodResponse response = getMethodResponse(region, apiId, resourceId, httpMethod, statusCode); + Map parameters = new HashMap<>( + response.responseParameters() != null ? response.responseParameters() : Map.of()); + if (patchOperations != null) { + for (Map patch : patchOperations) { + String path = patch.get("path"); + if (path == null || !path.startsWith("/responseParameters/method.response.header.") + || path.length() == "/responseParameters/method.response.header.".length()) { + throw new AwsException("BadRequestException", "Invalid patch operation", 400); + } + String name = unescapeJsonPointer(path.substring("/responseParameters/".length())); + String op = patch.get("op"); + if ("remove".equals(op)) { + parameters.remove(name); + } else if ("add".equals(op) || "replace".equals(op)) { + String value = patch.get("value"); + if (!"true".equals(value) && !"false".equals(value)) { + throw new AwsException("BadRequestException", "Invalid patch operation", 400); + } + parameters.put(name, Boolean.parseBoolean(value)); + } else { + throw new AwsException("BadRequestException", "Invalid patch operation", 400); + } + } + } + MethodResponse updated = new MethodResponse(statusCode, parameters); + getMethod(region, apiId, resourceId, httpMethod).getMethodResponses().put(statusCode, updated); + resourceStore.put(resourceKey(region, apiId, resourceId), getResource(region, apiId, resourceId)); + return updated; + } + + public synchronized void deleteMethodResponse(String region, String apiId, String resourceId, String httpMethod, String statusCode) { MethodConfig method = getMethod(region, apiId, resourceId, httpMethod); if (method.getMethodResponses().remove(statusCode) == null) { diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayMethodResponseParametersIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayMethodResponseParametersIntegrationTest.java new file mode 100644 index 0000000000..6a5f7cc37b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayMethodResponseParametersIntegrationTest.java @@ -0,0 +1,89 @@ +package io.github.hectorvent.floci.services.apigateway; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import java.util.stream.IntStream; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; + +@QuarkusTest +class ApiGatewayMethodResponseParametersIntegrationTest { + + @Test + void putGetAndPatchResponseParameters() { + String apiId = given().contentType("application/json") + .body("{\"name\":\"method-response-parameters\"}") + .when().post("/restapis").then().statusCode(201).extract().path("id"); + String resourceId = given().when().get("/restapis/" + apiId + "/resources") + .then().statusCode(200).extract().path("item[0].id"); + given().contentType("application/json").body("{\"authorizationType\":\"NONE\"}") + .when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET") + .then().statusCode(201); + + String path = "/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET/responses/200"; + given().contentType("application/json") + .body("{\"responseParameters\":{\"method.response.header.X-First\":false}}") + .when().put(path).then().statusCode(201) + .body("responseParameters.'method.response.header.X-First'", equalTo(false)); + given().when().get(path).then().statusCode(200) + .body("responseParameters.'method.response.header.X-First'", equalTo(false)); + + given().contentType("application/json") + .body("{\"patchOperations\":[{\"op\":\"add\"," + + "\"path\":\"/responseParameters/method.response.header.X-Second\"," + + "\"value\":\"true\"}]}") + .when().patch(path).then().statusCode(201) + .body("responseParameters.'method.response.header.X-First'", equalTo(false)) + .body("responseParameters.'method.response.header.X-Second'", equalTo(true)); + given().contentType("application/json") + .body("{\"patchOperations\":[{\"op\":\"replace\"," + + "\"path\":\"/responseParameters/method.response.header.X-First\"," + + "\"value\":\"true\"},{\"op\":\"remove\"," + + "\"path\":\"/responseParameters/method.response.header.X-Second\"}]}") + .when().patch(path).then().statusCode(201) + .body("responseParameters.'method.response.header.X-First'", equalTo(true)) + .body("responseParameters", not(hasKey("method.response.header.X-Second"))); + given().when().get(path).then().statusCode(200) + .body("responseParameters.'method.response.header.X-First'", equalTo(true)); + + given().contentType("application/json") + .body("{\"patchOperations\":[{\"op\":\"add\"," + + "\"path\":\"/responseParameters/method.response.header.X~0Tag\"," + + "\"value\":\"true\"}]}") + .when().patch(path).then().statusCode(201) + .body("responseParameters.'method.response.header.X~Tag'", equalTo(true)) + .body("responseParameters", not(hasKey("method.response.header.X~0Tag"))); + given().when().get(path).then().statusCode(200) + .body("responseParameters.'method.response.header.X~Tag'", equalTo(true)); + + IntStream.range(0, 16).parallel().forEach(index -> given().contentType("application/json") + .body("{\"patchOperations\":[{\"op\":\"add\"," + + "\"path\":\"/responseParameters/method.response.header.X-Concurrent-" + + index + "\",\"value\":\"true\"}]}") + .when().patch(path).then().statusCode(201)); + for (int index = 0; index < 16; index++) { + given().when().get(path).then().statusCode(200) + .body("responseParameters.'method.response.header.X-Concurrent-" + index + "'", + equalTo(true)); + } + + given().contentType("application/json") + .body("{\"patchOperations\":[{\"op\":\"replace\"," + + "\"path\":\"/responseParameters/method.response.header.X-First\"," + + "\"value\":\"false\"},{\"op\":\"add\"," + + "\"path\":\"/responseParameters/method.response.header.X-Bad\"," + + "\"value\":\"maybe\"}]}") + .when().patch(path).then().statusCode(400); + given().when().get(path).then().statusCode(200) + .body("responseParameters.'method.response.header.X-First'", equalTo(true)); + given().contentType("application/json") + .body("{\"patchOperations\":[{\"op\":\"add\"," + + "\"path\":\"/responseParameters/method.response.header.X-First\"," + + "\"value\":\"true\"}]}") + .when().patch(path.replace("/200", "/404")).then().statusCode(404); + } +}