diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java index 0a42c6fa5f..c42c035432 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java @@ -209,12 +209,14 @@ public class SnsJsonHandler { private Response handlePublishBatch(JsonNode request, String region) { String topicArn = request.path("TopicArn").asText(null); List> entries = new ArrayList<>(); + List entryAttributeFailures = new ArrayList<>(); JsonNode entriesNode = request.path("PublishBatchRequestEntries"); if (entriesNode.isArray()) { for (JsonNode entryNode : entriesNode) { + String id = entryNode.path("Id").asText(null); Map entry = new HashMap<>(); - entry.put("Id", entryNode.path("Id").asText(null)); + entry.put("Id", id); entry.put("Message", entryNode.path("Message").asText(null)); entry.put("Subject", entryNode.path("Subject").asText(null)); entry.put("MessageGroupId", entryNode.path("MessageGroupId").asText(null)); @@ -222,7 +224,14 @@ public class SnsJsonHandler { JsonNode attrsNode = entryNode.path("MessageAttributes"); if (attrsNode.isObject()) { - entry.put("MessageAttributes", parseMessageAttributes(attrsNode)); + try { + entry.put("MessageAttributes", parseMessageAttributes(attrsNode)); + } catch (AwsException e) { + // Real AWS SNS surfaces per-entry attribute errors as Failed entries + // and keeps processing the rest of the batch, instead of aborting. + entryAttributeFailures.add(new String[]{id, e.getErrorCode(), e.getMessage(), "true"}); + continue; + } } entries.add(entry); @@ -240,7 +249,9 @@ public class SnsJsonHandler { successful.add(item); } ArrayNode failed = response.putArray("Failed"); - for (String[] f : result.failed()) { + List allFailed = new ArrayList<>(result.failed()); + allFailed.addAll(entryAttributeFailures); + for (String[] f : allFailed) { ObjectNode item = objectMapper.createObjectNode(); item.put("Id", f[0]); item.put("Code", f[1]); diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java index 7a7a57060f..8a3aea1610 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java @@ -205,6 +205,7 @@ public class SnsQueryHandler { private Response handlePublishBatch(MultivaluedMap params, String region) { String topicArn = getParam(params, "TopicArn"); List> entries = new ArrayList<>(); + List entryAttributeFailures = new ArrayList<>(); for (int i = 1; ; i++) { String entryPrefix = "PublishBatchRequestEntries.member." + i; String id = getParam(params, entryPrefix + ".Id"); @@ -220,7 +221,10 @@ public class SnsQueryHandler { try { attributes = parseMessageAttributes(params, entryPrefix + ".MessageAttributes.entry."); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + // Real AWS SNS surfaces per-entry attribute errors as Failed entries + // and keeps processing the rest of the batch, instead of aborting. + entryAttributeFailures.add(new String[]{id, e.getErrorCode(), e.getMessage(), "true"}); + continue; } if (!attributes.isEmpty()) entry.put("MessageAttributes", attributes); entries.add(entry); @@ -233,7 +237,9 @@ public class SnsQueryHandler { xml.start("member").elem("Id", s[0]).elem("MessageId", s[1]).end("member"); } xml.end("Successful").start("Failed"); - for (String[] f : result.failed()) { + List allFailed = new ArrayList<>(result.failed()); + allFailed.addAll(entryAttributeFailures); + for (String[] f : allFailed) { xml.start("member").elem("Id", f[0]).elem("Code", f[1]) .elem("Message", f[2]).elem("SenderFault", f[3]).end("member"); } diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsJsonHandlerPublishBatchTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsJsonHandlerPublishBatchTest.java new file mode 100644 index 0000000000..12e8564440 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsJsonHandlerPublishBatchTest.java @@ -0,0 +1,103 @@ +package io.github.hectorvent.floci.services.sns; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.sqs.SqsService; +import io.github.hectorvent.floci.services.sqs.SqsServiceFactory; +import io.github.hectorvent.floci.services.sqs.model.Message; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests that SnsJsonHandler.handle("PublishBatch", ...) returns AWS-compatible + * partial-failure responses: a malformed entry must land in the Failed list + * (with InvalidParameterValue + SenderFault=true) instead of aborting the + * whole batch with a top-level error. + */ +class SnsJsonHandlerPublishBatchTest { + + private static final String REGION = "us-east-1"; + private static final String ACCOUNT = "000000000000"; + private static final String BASE_URL = "http://localhost:4566"; + + private final ObjectMapper objectMapper = new ObjectMapper(); + private SnsService snsService; + private SqsService sqsService; + private SnsJsonHandler handler; + + @BeforeEach + void setUp() { + RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT); + sqsService = SqsServiceFactory.createInMemory(BASE_URL, regionResolver); + snsService = new SnsService(new InMemoryStorage<>(), new InMemoryStorage<>(), + regionResolver, sqsService, null); + handler = new SnsJsonHandler(snsService, objectMapper); + } + + @Test + void publishBatch_invalidBinaryAttribute_failsOnlyOffendingEntryAndProcessesRest() throws Exception { + // Arrange + sqsService.createQueue("json-partial-fail-queue", Map.of(), REGION); + String queueArn = "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":json-partial-fail-queue"; + + snsService.createTopic("json-partial-fail-topic", Map.of(), null, REGION); + String topicArn = "arn:aws:sns:" + REGION + ":" + ACCOUNT + ":json-partial-fail-topic"; + snsService.subscribe(topicArn, "sqs", queueArn, REGION, Map.of("RawMessageDelivery", "true")); + + ObjectNode request = objectMapper.createObjectNode(); + request.put("TopicArn", topicArn); + ArrayNode entries = request.putArray("PublishBatchRequestEntries"); + + // Entry 1 — invalid base64 in BinaryValue; must NOT abort the batch + ObjectNode bad = entries.addObject(); + bad.put("Id", "bad"); + bad.put("Message", "ignored"); + ObjectNode badAttrs = bad.putObject("MessageAttributes"); + ObjectNode badBlob = badAttrs.putObject("blob"); + badBlob.put("DataType", "Binary"); + badBlob.put("BinaryValue", "@@@-not-base64-@@@"); + + // Entry 2 — valid; must be published + ObjectNode good = entries.addObject(); + good.put("Id", "good"); + good.put("Message", "valid-message"); + + // Act + JsonNode requestNode = objectMapper.readTree(objectMapper.writeValueAsString(request)); + Response response = handler.handle("PublishBatch", requestNode, REGION); + + // Assert — partial failure (matches real AWS SNS) + assertEquals(200, response.getStatus(), + "PublishBatch must return 200 with per-entry failures, not abort the whole batch"); + + JsonNode body = objectMapper.valueToTree(response.getEntity()); + JsonNode failed = body.path("Failed"); + JsonNode successful = body.path("Successful"); + + assertTrue(failed.isArray() && failed.size() == 1, "exactly one entry must be in Failed"); + JsonNode failedEntry = failed.get(0); + assertEquals("bad", failedEntry.path("Id").asText()); + assertEquals("InvalidParameterValue", failedEntry.path("Code").asText()); + assertTrue(failedEntry.path("SenderFault").asBoolean(), "SenderFault must be true"); + + assertTrue(successful.isArray() && successful.size() == 1, "exactly one entry must be in Successful"); + assertEquals("good", successful.get(0).path("Id").asText()); + + // Only the good entry should reach SQS + List messages = sqsService.receiveMessage( + BASE_URL + "/" + ACCOUNT + "/json-partial-fail-queue", 10, 30, 0, REGION); + assertEquals(1, messages.size(), "only the valid entry should be delivered"); + assertEquals("valid-message", messages.get(0).getBody()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsQueryHandlerPublishBatchTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsQueryHandlerPublishBatchTest.java index 0b49d6d539..656799b1b0 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsQueryHandlerPublishBatchTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsQueryHandlerPublishBatchTest.java @@ -88,6 +88,49 @@ class SnsQueryHandlerPublishBatchTest { } } + @Test + void publishBatch_invalidBinaryAttribute_failsOnlyOffendingEntryAndProcessesRest() { + // Arrange + sqsService.createQueue("partial-fail-queue", Map.of(), REGION); + String queueArn = "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":partial-fail-queue"; + + snsService.createTopic("partial-fail-topic", Map.of(), null, REGION); + String topicArn = "arn:aws:sns:" + REGION + ":" + ACCOUNT + ":partial-fail-topic"; + snsService.subscribe(topicArn, "sqs", queueArn, REGION, Map.of("RawMessageDelivery", "true")); + + MultivaluedMap params = new MultivaluedHashMap<>(); + params.add("TopicArn", topicArn); + + // Entry 1: invalid base64 BinaryValue — must NOT abort the batch + params.add("PublishBatchRequestEntries.member.1.Id", "bad"); + params.add("PublishBatchRequestEntries.member.1.Message", "ignored"); + params.add("PublishBatchRequestEntries.member.1.MessageAttributes.entry.1.Name", "blob"); + params.add("PublishBatchRequestEntries.member.1.MessageAttributes.entry.1.Value.DataType", "Binary"); + params.add("PublishBatchRequestEntries.member.1.MessageAttributes.entry.1.Value.BinaryValue", "@@@-not-base64-@@@"); + + // Entry 2: valid — must be published + putBatchEntry(params, 2, "good", "valid-message", attr("kind", "String", "ok")); + + // Act + Response response = handler.handle("PublishBatch", params, REGION); + + // Assert — HTTP 200, partial-failure semantics (matches real AWS SNS) + assertEquals(200, response.getStatus(), + "PublishBatch must return 200 with per-entry failures, not abort the whole batch"); + String body = (String) response.getEntity(); + assertTrue(body.contains(""), "response must contain Failed list"); + assertTrue(body.contains("bad"), "bad entry must appear in Failed list"); + assertTrue(body.contains("InvalidParameterValue"), "Failed entry must carry InvalidParameterValue code"); + assertTrue(body.contains("true"), "Failed entry must carry SenderFault=true"); + assertTrue(body.contains("good"), "good entry must appear in Successful list"); + + // Good entry must have been delivered to SQS; bad entry must NOT + List messages = sqsService.receiveMessage( + BASE_URL + "/" + ACCOUNT + "/partial-fail-queue", 10, 30, 0, REGION); + assertEquals(1, messages.size(), "only the valid entry should be delivered"); + assertEquals("valid-message", messages.get(0).getBody()); + } + @Test void publishBatch_entryWithoutMessageAttributes_doesNotPolluteOtherEntries() { // Arrange