diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java index 6e065f6b00..617338f52d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java @@ -42,6 +42,7 @@ import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; import java.util.Base64; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -1008,6 +1009,18 @@ public class ApiGatewayExecuteController { try { if (target.action() == null) { MultivaluedMap formParams = parseFormUrlEncoded(transformedBody); + + if ("sqs".equals(target.service()) && target.path() != null) { + formParams.computeIfAbsent("QueueUrl", (_) -> { + String resourcePath = target.path(); + int indexOfLastSlash = resourcePath.lastIndexOf('/'); + String queueName = indexOfLastSlash >= 0 && indexOfLastSlash < resourcePath.length() - 1 + ? resourcePath.substring(indexOfLastSlash + 1) : resourcePath; + + return Collections.singletonList(queueName); + }); + } + serviceResponse = serviceRouter.invokeQuery(target.service(), formParams, region); } else { JsonNode requestJson = objectMapper.readTree(transformedBody); diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/AwsServiceRouter.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/AwsServiceRouter.java index 343f316f10..0352056ea1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/AwsServiceRouter.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/AwsServiceRouter.java @@ -134,7 +134,7 @@ public class AwsServiceRouter { } if (resourceSpec.startsWith("path/")) { // Action is supplied by the rendered request body (query protocol). - return new IntegrationTarget(region, service, null); + return new IntegrationTarget(region, service, null, resourceSpec.substring("path/".length())); } return null; } diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewaySqsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewaySqsIntegrationTest.java index 75a1c85bb7..2cf54ddb80 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewaySqsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewaySqsIntegrationTest.java @@ -3,14 +3,12 @@ package io.github.hectorvent.floci.services.apigateway; import com.fasterxml.jackson.databind.ObjectMapper; import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.hamcrest.Matchers.not; /** * Integration tests for the API Gateway → SQS AWS integration using the path-style @@ -22,80 +20,186 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; * the same pattern real services and LocalStack use. */ @QuarkusTest -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class ApiGatewaySqsIntegrationTest { private static final ObjectMapper mapper = new ObjectMapper(); + private static final String URI_ACCOUNT_ID = "123456789012"; private static final String QUEUE_NAME = "apigw-sqs-test"; + private static final String PATH_QUEUE_NAME = "apigw-sqs-path-queue"; + private static final String EXPLICIT_QUEUE_NAME = "apigw-sqs-explicit-queue"; + private static final String STAGE_NAME = "test"; - private static String apiId; - private static String rootId; - private static String resourceId; - private static String queueUrl; + private String queueUrl; + private String apiId; - // ──────────────── Setup: SQS queue ──────────────── + private String pathQueueUrl; + private String explicitQueueUrl; + private String explicitApiId; - @Test @Order(0) - void setup_createQueue() { - queueUrl = given() + @BeforeEach + void setUp() throws Exception { + setupPathDerivedQueueUrl(); + setupExplicitQueueUrl(); + } + + @Test + void sqsIntegrationSendMessage() { + // The integration returns the SQS query-protocol XML response. + given() + .contentType(ContentType.JSON) + .body("{\"hello\":\"world\"}") + .when().post("/execute-api/" + apiId + "/" + STAGE_NAME + "/send") + .then() + .statusCode(200) + .body(containsString("")) + .body(containsString("")); + } + + @Test + void sqsIntegrationMessageLandsOnQueue() { + // The body the producer POSTed should be the MessageBody now sitting on the queue. + given() + .contentType(ContentType.JSON) + .body("{\"hello\":\"world\"}") + .when().post("/execute-api/" + apiId + "/" + STAGE_NAME + "/send") + .then().statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", queueUrl) + .formParam("MaxNumberOfMessages", "1") + .when().post("/") + .then().statusCode(200) + .body(containsString("{"hello":"world"}")); + } + + @Test + void explicitQueueUrlSendMessage() { + given() + .contentType(ContentType.JSON) + .body("{\"target\":\"explicit-queue\"}") + .when().post("/execute-api/" + explicitApiId + "/" + STAGE_NAME + "/send") + .then() + .statusCode(200) + .body(containsString("")) + .body(containsString("")); + } + + @Test + void explicitQueueUrlMessageLandsOnExplicitQueue() { + given() + .contentType(ContentType.JSON) + .body("{\"target\":\"explicit-queue\"}") + .when().post("/execute-api/" + explicitApiId + "/" + STAGE_NAME + "/send") + .then().statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", explicitQueueUrl) + .formParam("MaxNumberOfMessages", "1") + .when().post("/") + .then().statusCode(200) + .body(containsString("{"target":"explicit-queue"}")); + } + + @Test + void explicitQueueUrlPathQueueRemainsEmpty() { + given() + .contentType(ContentType.JSON) + .body("{\"target\":\"explicit-queue\"}") + .when().post("/execute-api/" + explicitApiId + "/" + STAGE_NAME + "/send") + .then().statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", pathQueueUrl) + .formParam("MaxNumberOfMessages", "1") + .when().post("/") + .then().statusCode(200) + .body(not(containsString(""))); + } + + private void setupPathDerivedQueueUrl() throws Exception { + queueUrl = createQueue(QUEUE_NAME); + apiId = createRestApi("sqs-integration-test", "APIGW → SQS"); + + String rootId = getRootResourceId(apiId); + String resourceId = createResource(apiId, rootId, "send"); + + String vtl = "Action=SendMessage" + + "&MessageBody=$util.urlEncode($input.body)"; + configurePostSqsIntegration(apiId, resourceId, QUEUE_NAME, vtl); + deployStage(apiId, STAGE_NAME); + } + + private void setupExplicitQueueUrl() throws Exception { + pathQueueUrl = createQueue(PATH_QUEUE_NAME); + explicitQueueUrl = createQueue(EXPLICIT_QUEUE_NAME); + explicitApiId = createRestApi("sqs-explicit-queueurl-test", "APIGW → SQS explicit QueueUrl"); + + String rootId = getRootResourceId(explicitApiId); + String resourceId = createResource(explicitApiId, rootId, "send"); + + String vtl = "Action=SendMessage" + + "&QueueUrl=$util.urlEncode(\"" + explicitQueueUrl + "\")" + + "&MessageBody=$util.urlEncode($input.body)"; + configurePostSqsIntegration(explicitApiId, resourceId, PATH_QUEUE_NAME, vtl); + deployStage(explicitApiId, STAGE_NAME); + } + + private String createQueue(String queueName) { + return given() .contentType("application/x-www-form-urlencoded") .formParam("Action", "CreateQueue") - .formParam("QueueName", QUEUE_NAME) + .formParam("QueueName", queueName) .when().post("/") .then().statusCode(200) .body(containsString("")) .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl"); - assertNotNull(queueUrl); } - // ──────────────── Setup: API Gateway REST API ──────────────── - - @Test @Order(1) - void setup_createRestApi() { - apiId = given() + private String createRestApi(String name, String description) { + return given() .contentType(ContentType.JSON) - .body("{\"name\":\"sqs-integration-test\",\"description\":\"APIGW → SQS\"}") + .body("{\"name\":\"" + name + "\",\"description\":\"" + description + "\"}") .when().post("/restapis") .then().statusCode(201) .extract().path("id"); - assertNotNull(apiId); } - @Test @Order(2) - void setup_getRootResource() { - rootId = given() + private String getRootResourceId(String apiId) { + return given() .when().get("/restapis/" + apiId + "/resources") .then().statusCode(200) .extract().path("item[0].id"); - assertNotNull(rootId); } - @Test @Order(3) - void setup_createResource() { - resourceId = given() + private String createResource(String apiId, String parentId, String pathPart) { + return given() .contentType(ContentType.JSON) - .body("{\"pathPart\":\"send\"}") - .when().post("/restapis/" + apiId + "/resources/" + rootId) + .body("{\"pathPart\":\"" + pathPart + "\"}") + .when().post("/restapis/" + apiId + "/resources/" + parentId) .then().statusCode(201) .extract().path("id"); - assertNotNull(resourceId); } - @Test @Order(4) - void setup_configureMethod() throws Exception { - // POST method, no auth. + private void configurePostSqsIntegration(String apiId, String resourceId, String uriQueueName, String vtl) + throws Exception { given() .contentType(ContentType.JSON) .body("{\"authorizationType\":\"NONE\"}") .when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/POST") .then().statusCode(201); - // AWS integration targeting SQS via the path-style URI. The request template renders - // the SQS query protocol; the Content-Type override marks the body form-urlencoded. var integrationNode = mapper.createObjectNode(); integrationNode.put("type", "AWS"); integrationNode.put("httpMethod", "POST"); - integrationNode.put("uri", "arn:aws:apigateway:us-east-1:sqs:path/000000000000/" + QUEUE_NAME); + integrationNode.put("uri", "arn:aws:apigateway:us-east-1:sqs:path/" + URI_ACCOUNT_ID + "/" + uriQueueName); integrationNode.put("passthroughBehavior", "NEVER"); var reqParams = mapper.createObjectNode(); @@ -103,16 +207,14 @@ class ApiGatewaySqsIntegrationTest { integrationNode.set("requestParameters", reqParams); var reqTemplates = mapper.createObjectNode(); - String vtl = "Action=SendMessage" - + "&QueueUrl=$util.urlEncode(\"" + queueUrl + "\")" - + "&MessageBody=$util.urlEncode($input.body)"; reqTemplates.put("application/json", vtl); integrationNode.set("requestTemplates", reqTemplates); given() .contentType(ContentType.JSON) .body(mapper.writeValueAsString(integrationNode)) - .when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/POST/integration") + .when().put("/restapis/" + apiId + "/resources/" + resourceId + + "/methods/POST/integration") .then().statusCode(201); // Default 200 integration response (no response template — pass the SQS XML through). @@ -124,8 +226,7 @@ class ApiGatewaySqsIntegrationTest { .then().statusCode(201); } - @Test @Order(5) - void setup_deployAndCreateStage() { + private void deployStage(String apiId, String stageName) { String deploymentId = given() .contentType(ContentType.JSON) .body("{\"description\":\"test\"}") @@ -135,37 +236,8 @@ class ApiGatewaySqsIntegrationTest { given() .contentType(ContentType.JSON) - .body("{\"stageName\":\"test\",\"deploymentId\":\"" + deploymentId + "\"}") + .body("{\"stageName\":\"" + stageName + "\",\"deploymentId\":\"" + deploymentId + "\"}") .when().post("/restapis/" + apiId + "/stages") .then().statusCode(201); } - - // ──────────────── Test: APIGW → SQS SendMessage ──────────────── - - @Test @Order(10) - void sqsIntegration_sendMessage() { - // The integration returns the SQS query-protocol XML response. - given() - .contentType(ContentType.JSON) - .body("{\"hello\":\"world\"}") - .when().post("/execute-api/" + apiId + "/test/send") - .then() - .statusCode(200) - .body(containsString("")) - .body(containsString("")); - } - - @Test @Order(11) - void sqsIntegration_messageLandsOnQueue() { - // The body the producer POSTed should be the MessageBody now sitting on the queue. - given() - .contentType("application/x-www-form-urlencoded") - .formParam("Action", "ReceiveMessage") - .formParam("QueueUrl", queueUrl) - .formParam("MaxNumberOfMessages", "1") - .when().post("/") - .then().statusCode(200) - .body(containsString("{"hello":"world"}")); - } }