fix(apigateway): return embedded resource methods (#5109)

* fix(apigateway): return embedded resource methods

* fix(apigateway): guard method maps during concurrent reads

---------

Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
vamsiakandVamsi Akavarapu authored and GitHub committed 2026-10-05 11:44:40 -04:00
1 parent 840d1bb6c1
commit d508a4e197
7 files changed
+396 -9

No files matched your search

@@ -0,0 +1,139 @@
package io.github.hectorvent.floci.services.apigateway;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
@QuarkusTest
class ApiGatewayResourceMethodsIntegrationTest {
private String apiId;
private String rootId;
private String resourceId;
@BeforeEach
void setup() {
apiId = given().contentType(ContentType.JSON).body("{\"name\":\"resource-methods\"}")
.when().post("/restapis").then().statusCode(201).extract().path("id");
rootId = given().when().get(resourcesPath()).then().statusCode(200).extract().path("item[0].id");
resourceId = given().contentType(ContentType.JSON).body("{\"pathPart\":\"widgets\"}")
.when().post(resourcesPath() + "/" + rootId)
.then().statusCode(201).extract().path("id");
given().contentType(ContentType.JSON).body("""
{"authorizationType":"NONE","requestParameters":{"method.request.header.X-Trace":true},
"requestModels":{"application/json":"Empty"}}
""")
.when().put(methodPath("GET")).then().statusCode(201);
given().contentType(ContentType.JSON).body("{\"authorizationType\":\"AWS_IAM\",\"apiKeyRequired\":true}")
.when().put(methodPath("POST")).then().statusCode(201);
given().contentType(ContentType.JSON).body("""
{"responseParameters":{"method.response.header.X-Trace":true}}
""")
.when().put(methodPath("GET") + "/responses/200").then().statusCode(201);
given().contentType(ContentType.JSON).body("""
{"type":"HTTP","httpMethod":"POST","uri":"https://example.invalid/widgets",
"timeoutInMillis":12000,"requestTemplates":{"application/json":"{}"},
"requestParameters":{"integration.request.header.X-Trace":"method.request.header.X-Trace"}}
""")
.when().put(methodPath("GET") + "/integration").then().statusCode(201);
}
@AfterEach
void cleanup() {
if (apiId != null) {
given().when().delete("/restapis/" + apiId).then().statusCode(202);
}
}
@Test
void collectionEmbedReturnsMethodAndIntegrationMetadata() {
given().queryParam("embed", "methods").when().get(resourcesPath())
.then().statusCode(200)
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods.GET.httpMethod", equalTo("GET"))
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods.GET.authorizationType", equalTo("NONE"))
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods.POST.authorizationType", equalTo("AWS_IAM"))
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods.POST.apiKeyRequired", equalTo(true))
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods.GET.methodIntegration.uri",
equalTo("https://example.invalid/widgets"));
}
@Test
void individualEmbedPreservesStoredRequestAndResponseConfiguration() {
given().queryParam("embed", "methods").when().get(resourcePath())
.then().statusCode(200)
.body("resourceMethods.GET.requestParameters.'method.request.header.X-Trace'", equalTo(true))
.body("resourceMethods.GET.requestModels.'application/json'", equalTo("Empty"))
.body("resourceMethods.GET.methodResponses.'200'.statusCode", equalTo("200"))
.body("resourceMethods.GET.methodResponses.'200'.responseParameters.'method.response.header.X-Trace'",
equalTo(true))
.body("resourceMethods.GET.methodIntegration.timeoutInMillis", equalTo(12000))
.body("resourceMethods.GET.methodIntegration.requestTemplates.'application/json'", equalTo("{}"))
.body("resourceMethods.GET.methodIntegration.requestParameters.'integration.request.header.X-Trace'",
equalTo("method.request.header.X-Trace"));
}
@Test
void ordinaryReadsReturnMethodNamesWithoutErasingStoredMetadata() {
given().when().get(resourcesPath()).then().statusCode(200)
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods.GET", anEmptyMap())
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods.POST", anEmptyMap());
given().when().get(resourcePath()).then().statusCode(200)
.body("resourceMethods.GET", anEmptyMap()).body("resourceMethods.POST", anEmptyMap());
given().when().get(methodPath("GET")).then().statusCode(200)
.body("authorizationType", equalTo("NONE"))
.body("methodIntegration.uri", equalTo("https://example.invalid/widgets"));
given().queryParam("embed", "methods").when().get(resourcePath()).then().statusCode(200)
.body("resourceMethods.GET.authorizationType", equalTo("NONE"));
}
@Test
void emptyResourcesDoNotInventMethods() {
given().queryParam("embed", "methods").when().get(resourcesPath() + "/" + rootId)
.then().statusCode(200).body("resourceMethods", nullValue());
given().queryParam("embed", "methods").when().get(resourcesPath()).then().statusCode(200)
.body("item.find { it.id == '" + rootId + "' }.resourceMethods", nullValue());
}
@Test
void embeddedReadsReflectMethodUpdatesAndDeletion() {
given().contentType(ContentType.JSON).body("""
{"patchOperations":[{"op":"replace","path":"/authorizationType","value":"AWS_IAM"}]}
""")
.when().patch(methodPath("GET")).then().statusCode(200);
given().when().delete(methodPath("POST")).then().statusCode(202);
given().queryParam("embed", "methods").when().get(resourcePath()).then().statusCode(200)
.body("resourceMethods.GET.authorizationType", equalTo("AWS_IAM"))
.body("resourceMethods", not(hasKey("POST")));
given().queryParam("embed", "methods").when().get(resourcesPath()).then().statusCode(200)
.body("item.find { it.id == '" + resourceId + "' }.resourceMethods", not(hasKey("POST")));
}
@Test
void embeddedReadsKeepNotFoundErrors() {
given().queryParam("embed", "methods").when().get("/restapis/missing-api/resources")
.then().statusCode(404);
given().queryParam("embed", "methods").when().get(resourcesPath() + "/missing-resource")
.then().statusCode(404);
}
private String resourcesPath() {
return "/restapis/" + apiId + "/resources";
}
private String resourcePath() {
return resourcesPath() + "/" + resourceId;
}
private String methodPath(String httpMethod) {
return resourcePath() + "/methods/" + httpMethod;
}
}
@@ -0,0 +1,121 @@
package io.github.hectorvent.floci.services.apigateway.model;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ApiGatewayMethodMapConcurrencyTest {
enum MapSource { NEW, SETTER, JACKSON, NULL_RESET }
@ParameterizedTest
@EnumSource(MapSource.class)
void resourceMethodsTolerateWritesDuringReadTraversal(MapSource source) throws Exception {
ApiGatewayResource resource = switch (source) {
case NEW -> new ApiGatewayResource();
case SETTER -> {
ApiGatewayResource configured = new ApiGatewayResource();
configured.setResourceMethods(new HashMap<>());
yield configured;
}
case JACKSON -> new ObjectMapper().readValue(
"{\"resourceMethods\":{\"GET\":{\"httpMethod\":\"GET\"}}}", ApiGatewayResource.class);
case NULL_RESET -> {
ApiGatewayResource configured = new ApiGatewayResource();
configured.setResourceMethods(null);
yield configured;
}
};
MethodConfig method = new MethodConfig();
method.setHttpMethod("GET");
mutateDuringTraversal(resource.getResourceMethods(), "GET", "POST", "DELETE", method);
}
@ParameterizedTest
@EnumSource(MapSource.class)
void methodResponsesTolerateWritesDuringReadTraversal(MapSource source) throws Exception {
MethodConfig method = switch (source) {
case NEW -> new MethodConfig();
case SETTER -> {
MethodConfig configured = new MethodConfig();
configured.setMethodResponses(new HashMap<>());
yield configured;
}
case JACKSON -> new ObjectMapper().readValue(
"{\"methodResponses\":{\"200\":{\"statusCode\":\"200\"}}}", MethodConfig.class);
case NULL_RESET -> {
MethodConfig configured = new MethodConfig();
configured.setMethodResponses(null);
yield configured;
}
};
mutateDuringTraversal(method.getMethodResponses(), "200", "400", "500",
new MethodResponse("200", Map.of("method.response.header.X-Trace", true)));
}
@Test
void resourceSetterOwnsItsMapAndKeepsMethodConfiguration() {
MethodConfig method = new MethodConfig();
method.setHttpMethod("GET");
Map<String, MethodConfig> input = new HashMap<>(Map.of("GET", method));
ApiGatewayResource resource = new ApiGatewayResource();
resource.setResourceMethods(input);
input.clear();
assertSame(method, resource.getResourceMethods().get("GET"));
resource.getResourceMethods().put("POST", new MethodConfig());
assertTrue(input.isEmpty(), "Mutating the stored map must not mutate the setter's input");
}
@Test
void responseSetterOwnsItsMapAndKeepsResponseConfiguration() {
MethodResponse response = new MethodResponse("200", Map.of("method.response.header.X-Trace", true));
Map<String, MethodResponse> input = new HashMap<>(Map.of("200", response));
MethodConfig method = new MethodConfig();
method.setMethodResponses(input);
input.clear();
assertEquals(response, method.getMethodResponses().get("200"));
method.getMethodResponses().put("400", new MethodResponse("400", Map.of()));
assertTrue(input.isEmpty(), "Mutating the stored map must not mutate the setter's input");
}
private static <T> void mutateDuringTraversal(Map<String, T> map, String first, String second,
String added, T value) {
map.put(first, value);
map.put(second, value);
AtomicBoolean changed = new AtomicBoolean();
try (ExecutorService writer = Executors.newSingleThreadExecutor()) {
assertDoesNotThrow(() -> map.forEach((key, entry) -> {
if (changed.compareAndSet(false, true)) {
Future<?> mutation = writer.submit(() -> {
map.put(added, value);
map.remove(second);
});
assertDoesNotThrow(() -> mutation.get(5, TimeUnit.SECONDS));
}
assertNotNull(key);
assertNotNull(entry);
}));
}
assertTrue(changed.get(), "The writer must run while the reader is traversing the map");
assertSame(value, map.get(added));
assertFalse(map.containsKey(second));
}
}