mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sns): return per-entry failures from PublishBatch instead of aborting whole batch (#1008)
Real AWS SNS PublishBatch surfaces an attribute parsing error (e.g. invalid base64 in a BinaryValue) as a Failed entry with InvalidParameterValue + SenderFault=true, and continues publishing the remaining valid entries. Both SnsQueryHandler and SnsJsonHandler used to let the AwsException from parseMessageAttributes propagate out of the batch loop, returning a 400 (Query) or letting the exception bubble (JSON) and aborting the whole batch. They now catch the per-entry parse failure, record it as a Failed entry, and merge it with the regular batch result. Fix #992
This commit is contained in:
1 parent
6434fc91f7
commit
f5dba627be
4 files changed
+168
-5
No files matched your search
+103
@@ -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<Message> 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());
|
||||
}
|
||||
}
|
||||
+43
@@ -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<String, String> 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("<Failed>"), "response must contain Failed list");
|
||||
assertTrue(body.contains("<Id>bad</Id>"), "bad entry must appear in Failed list");
|
||||
assertTrue(body.contains("<Code>InvalidParameterValue</Code>"), "Failed entry must carry InvalidParameterValue code");
|
||||
assertTrue(body.contains("<SenderFault>true</SenderFault>"), "Failed entry must carry SenderFault=true");
|
||||
assertTrue(body.contains("<Id>good</Id>"), "good entry must appear in Successful list");
|
||||
|
||||
// Good entry must have been delivered to SQS; bad entry must NOT
|
||||
List<Message> 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
|
||||
|
||||
Reference in new issue
Block a user