diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsException.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsException.java index 58616dbf93..f792d3ac25 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsException.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsException.java @@ -1,11 +1,29 @@ package io.github.hectorvent.floci.core.common; +import java.util.Map; + /** * Base exception for AWS emulator errors. * Maps to AWS-style error responses with code, message, and HTTP status. + *

+ * Some services use different error code formats for Query (XML) and JSON protocols. + * {@link #jsonType()} returns the JSON-protocol {@code __type} value that the AWS SDK v2 + * uses to instantiate a specific typed exception rather than falling back to a generic one. */ public class AwsException extends RuntimeException { + /** + * Maps Query-protocol error codes to their JSON-protocol {@code __type} equivalents. + * Codes absent from this map are used as-is for both protocols. + */ + private static final Map JSON_TYPE_BY_QUERY_CODE = Map.of( + "AWS.SimpleQueueService.NonExistentQueue", "QueueDoesNotExist", + "QueueAlreadyExists", "QueueNameExists", + "ReceiptHandleIsInvalid", "ReceiptHandleIsInvalid", + "TooManyEntriesInBatchRequest", "TooManyEntriesInBatchRequest", + "BatchEntryIdNotUnique", "BatchEntryIdNotDistinct" + ); + private final String errorCode; private final int httpStatus; @@ -22,4 +40,12 @@ public class AwsException extends RuntimeException { public int getHttpStatus() { return httpStatus; } + + /** + * Returns the JSON-protocol {@code __type} value for this error. + * The AWS SDK v2 uses this to map responses to typed exception classes. + */ + public String jsonType() { + return JSON_TYPE_BY_QUERY_CODE.getOrDefault(errorCode, errorCode); + } } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsExceptionMapper.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsExceptionMapper.java index 161a65aada..7ddb33f84a 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsExceptionMapper.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsExceptionMapper.java @@ -18,8 +18,8 @@ public class AwsExceptionMapper implements ExceptionMapper { public Response toResponse(AwsException exception) { LOG.debugv("Mapping exception: {0} - {1}", exception.getErrorCode(), exception.getMessage()); return Response.status(exception.getHttpStatus()) - .entity(new AwsErrorResponse(exception.getErrorCode(), exception.getMessage())) .type(MediaType.APPLICATION_JSON) + .entity(new AwsErrorResponse(exception.jsonType(), exception.getMessage())) .build(); } } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java index cf34f05dfb..88a11f8b03 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java @@ -135,7 +135,7 @@ public class AwsJsonController { } catch (AwsException e) { return Response.status(e.getHttpStatus()) .type(MediaType.APPLICATION_JSON) - .entity(new AwsErrorResponse(e.getErrorCode(), e.getMessage())) + .entity(new AwsErrorResponse(e.jsonType(), e.getMessage())) .build(); } catch (Exception e) { LOG.error("Error processing " + serviceName + " JSON request", e); @@ -329,7 +329,7 @@ public class AwsJsonController { private Response cborErrorResponse(AwsException e, String protocolHeader) { try { byte[] errBytes = CBOR_MAPPER.writeValueAsBytes( - new AwsErrorResponse(e.getErrorCode(), e.getMessage())); + new AwsErrorResponse(e.jsonType(), e.getMessage())); return Response.status(e.getHttpStatus()) .header(protocolHeader, "rpc-v2-cbor") .type("application/cbor") diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java index b78afb189f..3c5707610e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java @@ -1,6 +1,9 @@ package io.github.hectorvent.floci.services.sqs; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import io.restassured.config.EncoderConfig; +import io.restassured.http.ContentType; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -215,4 +218,92 @@ class SqsIntegrationTest { .statusCode(400) .body(containsString("UnsupportedOperation")); } + + @Test + void createQueue_idempotent_sameAttributes() { + String queueName = "idempotent-test-queue"; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .formParam("Attribute.1.Name", "VisibilityTimeout") + .formParam("Attribute.1.Value", "60") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString(queueName)); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .formParam("Attribute.1.Name", "VisibilityTimeout") + .formParam("Attribute.1.Value", "60") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString(queueName)); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", "http://localhost:4566/000000000000/" + queueName) + .when() + .post("/"); + } + + @Test + void createQueue_conflictingAttributes_returns400() { + String queueName = "conflict-test-queue"; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .formParam("Attribute.1.Name", "VisibilityTimeout") + .formParam("Attribute.1.Value", "30") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .formParam("Attribute.1.Name", "VisibilityTimeout") + .formParam("Attribute.1.Value", "60") + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("QueueNameExists")); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", "http://localhost:4566/000000000000/" + queueName) + .when() + .post("/"); + } + + @Test + void jsonProtocol_nonExistentQueue_returnsQueueDoesNotExist() { + given() + .config(RestAssured.config().encoderConfig( + EncoderConfig.encoderConfig() + .encodeContentTypeAs("application/x-amz-json-1.0", ContentType.TEXT))) + .contentType("application/x-amz-json-1.0") + .header("X-Amz-Target", "AmazonSQS.GetQueueUrl") + .body("{\"QueueName\": \"no-such-queue-xyz\"}") + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("QueueDoesNotExist")) + .body(not(containsString("AWS.SimpleQueueService.NonExistentQueue"))); + } }