mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sns): deliver topic broadcasts to application-protocol platform endpoints (#1856)
* feat(sns): deliver topic broadcasts to application-protocol platform endpoints
Add an application-protocol case to deliverMessage so publishing to a
topic fans out to subscribed mobile push platform endpoints, matching
real SNS behavior. Thread messageStructure through the delivery boundary
so platform-specific payload keys (GCM/FCM/APNS) resolve per endpoint,
and factor the push-capture tail of publishToEndpoint into a shared
capturePushForEndpoint helper. Disabled endpoints are skipped in the
fan-out rather than failing the publish. Broadcast pushes surface in the
same retrospection API keyed by EndpointArn.
* fix(sns): validate MessageStructure synchronously before topic fan-out
Topic Publish with MessageStructure=json now validates that the message is
a JSON object with a top-level default entry up front, mirroring the real
SNS API. Previously a malformed envelope was only rejected lazily inside
per-subscriber delivery, where deliverMessage's catch swallowed the
InvalidParameter and logged a warning while publish still returned a
success messageId, so application subscribers silently received no push.
* fix(sns): validate MessageStructure per entry in PublishBatch
publish() validates a MessageStructure="json" envelope synchronously before
fan-out, but publishBatch() only threaded MessageStructure into deliverMessage.
A batch entry with a malformed or default-less envelope was therefore added to
Successful while deliverMessage's catch swallowed the resulting InvalidParameter,
dropping the push for every application subscriber.
Run the same validation per entry, reporting a bad envelope as that entry's
BatchResultErrorEntry (InvalidParameter, senderFault=true) instead of throwing,
so the rest of the batch still publishes as real PublishBatch does.
* fix(sns): resolve MessageStructure=json payload per protocol on fan-out
Under MessageStructure="json", only the application protocol consumed the
envelope: sqs, lambda, http/https, email, email-json and sms each delivered
the raw JSON structure. Real SNS resolves per-protocol for every subscriber,
picking the value under the protocol key and falling back to default.
deliverMessage now resolves the payload for each subscription through the
(renamed) resolveProtocolPayload, which already keyed off an arbitrary
string; application keeps resolving against the push platform inside
capturePushForEndpoint. validateTopicMessageStructure guarantees default
exists before fan-out, so the fallback cannot come up empty.
* fix(sns): ignore non-string MessageStructure=json keys, matching AWS
Under MessageStructure=json, real SNS ignores a protocol key whose value
is not a string ("Non-string values will cause the key to be ignored"),
falling back to default rather than stringifying it. resolveProtocolPayload
was calling protocolValue.toString(), so {"default":"hi","sqs":42} delivered
the literal "42" to an SQS subscriber instead of "hi".
Only textual values are now accepted for both the protocol key and the
default fallback; non-textual values fall through. validateTopicMessageStructure
is tightened to require a textual default so a non-string default is rejected
synchronously rather than throwing (and being swallowed) during fan-out.
Adds regression tests for a non-string protocol key falling back to default
and for rejection of a non-string default.
* chore: remove unnecessary file
This commit is contained in:
1 parent
ebd4a078d3
commit
f45d64a00e
4 files changed
+455
-22
No files matched your search
@@ -322,6 +322,154 @@ class SnsMobilePushTest {
|
||||
assertEquals("PlatformApplicationDisabled", e.getErrorCode());
|
||||
}
|
||||
|
||||
// --- Publish: topic broadcast to platform endpoints (Protocol="application") ---
|
||||
|
||||
@Test
|
||||
void publish_topicBroadcastToApplicationEndpointsCapturesForEachDevice() {
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint deviceA = snsService.createPlatformEndpoint(app.getArn(), "fcm-token-a", null, Map.of(), REGION);
|
||||
PlatformEndpoint deviceB = snsService.createPlatformEndpoint(app.getArn(), "fcm-token-b", null, Map.of(), REGION);
|
||||
|
||||
String topicArn = snsService.createTopic("market-alerts", null, null, REGION).getTopicArn();
|
||||
snsService.subscribe(topicArn, "application", deviceA.getArn(), REGION, Map.of());
|
||||
snsService.subscribe(topicArn, "application", deviceB.getArn(), REGION, Map.of());
|
||||
|
||||
String envelope = "{"
|
||||
+ "\"default\":\"market open\","
|
||||
+ "\"GCM\":\"{\\\"notification\\\":{\\\"body\\\":\\\"market alert\\\"}}\""
|
||||
+ "}";
|
||||
String messageId = snsService.publish(topicArn, null, null, envelope, null, "json",
|
||||
null, null, null, REGION);
|
||||
assertNotNull(messageId);
|
||||
|
||||
List<PushNotification> capturedA = snsService.peekPushNotifications(deviceA.getArn());
|
||||
List<PushNotification> capturedB = snsService.peekPushNotifications(deviceB.getArn());
|
||||
assertEquals(1, capturedA.size());
|
||||
assertEquals(1, capturedB.size());
|
||||
assertEquals("GCM", capturedA.get(0).platform());
|
||||
assertEquals("fcm-token-a", capturedA.get(0).token());
|
||||
assertEquals("fcm-token-b", capturedB.get(0).token());
|
||||
assertTrue(capturedA.get(0).payload().contains("market alert"));
|
||||
assertTrue(capturedB.get(0).payload().contains("market alert"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_topicBroadcastSkipsDisabledEndpointButDeliversToEnabled() {
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint enabled = snsService.createPlatformEndpoint(app.getArn(), "fcm-token-live", null, Map.of(), REGION);
|
||||
PlatformEndpoint disabled = snsService.createPlatformEndpoint(app.getArn(), "fcm-token-dead", null, Map.of(), REGION);
|
||||
snsService.setEndpointAttributes(disabled.getArn(), Map.of("Enabled", "false"), REGION);
|
||||
|
||||
String topicArn = snsService.createTopic("market-alerts", null, null, REGION).getTopicArn();
|
||||
snsService.subscribe(topicArn, "application", enabled.getArn(), REGION, Map.of());
|
||||
snsService.subscribe(topicArn, "application", disabled.getArn(), REGION, Map.of());
|
||||
|
||||
assertDoesNotThrow(() -> snsService.publish(topicArn, null, null,
|
||||
"{\"GCM\":\"hi\",\"default\":\"hi\"}", null, "json", null, null, null, REGION));
|
||||
|
||||
assertEquals(1, snsService.peekPushNotifications(enabled.getArn()).size());
|
||||
assertTrue(snsService.peekPushNotifications(disabled.getArn()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_topicBroadcastFallsBackToDefaultWhenPlatformKeyMissing() {
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint device = snsService.createPlatformEndpoint(app.getArn(), "fcm-token", null, Map.of(), REGION);
|
||||
|
||||
String topicArn = snsService.createTopic("market-alerts", null, null, REGION).getTopicArn();
|
||||
snsService.subscribe(topicArn, "application", device.getArn(), REGION, Map.of());
|
||||
|
||||
snsService.publish(topicArn, null, null, "{\"default\":\"fallback body\"}", null, "json",
|
||||
null, null, null, REGION);
|
||||
|
||||
List<PushNotification> captured = snsService.peekPushNotifications(device.getArn());
|
||||
assertEquals(1, captured.size());
|
||||
assertEquals("fallback body", captured.get(0).payload());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_topicBroadcastRejectsJsonStructureMissingDefaultBeforeFanOut() {
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint device = snsService.createPlatformEndpoint(app.getArn(), "fcm-token", null, Map.of(), REGION);
|
||||
|
||||
String topicArn = snsService.createTopic("market-alerts", null, null, REGION).getTopicArn();
|
||||
snsService.subscribe(topicArn, "application", device.getArn(), REGION, Map.of());
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> snsService.publish(
|
||||
topicArn, null, null, "{\"GCM\":\"only gcm\"}", null, "json",
|
||||
null, null, null, REGION));
|
||||
assertEquals("InvalidParameter", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
assertTrue(e.getMessage().contains("default"));
|
||||
assertTrue(snsService.peekPushNotifications(device.getArn()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_topicBroadcastRejectsInvalidJsonStructureBeforeFanOut() {
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint device = snsService.createPlatformEndpoint(app.getArn(), "fcm-token", null, Map.of(), REGION);
|
||||
|
||||
String topicArn = snsService.createTopic("market-alerts", null, null, REGION).getTopicArn();
|
||||
snsService.subscribe(topicArn, "application", device.getArn(), REGION, Map.of());
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> snsService.publish(
|
||||
topicArn, null, null, "not json at all", null, "json",
|
||||
null, null, null, REGION));
|
||||
assertEquals("InvalidParameter", e.getErrorCode());
|
||||
assertTrue(snsService.peekPushNotifications(device.getArn()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishBatch_rejectsEntryWithJsonStructureMissingDefaultWithoutFailingBatch() {
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint device = snsService.createPlatformEndpoint(app.getArn(), "fcm-token", null, Map.of(), REGION);
|
||||
|
||||
String topicArn = snsService.createTopic("market-alerts", null, null, REGION).getTopicArn();
|
||||
snsService.subscribe(topicArn, "application", device.getArn(), REGION, Map.of());
|
||||
|
||||
var result = snsService.publishBatch(topicArn, List.of(
|
||||
Map.of("Id", "bad", "Message", "{\"GCM\":\"only gcm\"}", "MessageStructure", "json"),
|
||||
Map.of("Id", "good", "Message", "{\"default\":\"ok body\"}", "MessageStructure", "json")),
|
||||
REGION);
|
||||
|
||||
assertEquals(1, result.failed().size());
|
||||
assertEquals("bad", result.failed().get(0)[0]);
|
||||
assertEquals("InvalidParameter", result.failed().get(0)[1]);
|
||||
assertTrue(result.failed().get(0)[2].contains("default"));
|
||||
assertEquals("true", result.failed().get(0)[3]);
|
||||
|
||||
assertEquals(1, result.successful().size());
|
||||
assertEquals("good", result.successful().get(0)[0]);
|
||||
|
||||
List<PushNotification> captured = snsService.peekPushNotifications(device.getArn());
|
||||
assertEquals(1, captured.size());
|
||||
assertEquals("ok body", captured.get(0).payload());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishBatch_rejectsEntryWithInvalidJsonStructureWithoutFailingBatch() {
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint device = snsService.createPlatformEndpoint(app.getArn(), "fcm-token", null, Map.of(), REGION);
|
||||
|
||||
String topicArn = snsService.createTopic("market-alerts", null, null, REGION).getTopicArn();
|
||||
snsService.subscribe(topicArn, "application", device.getArn(), REGION, Map.of());
|
||||
|
||||
var result = snsService.publishBatch(topicArn, List.of(
|
||||
Map.of("Id", "bad", "Message", "not json at all", "MessageStructure", "json"),
|
||||
Map.of("Id", "good", "Message", "plain text")),
|
||||
REGION);
|
||||
|
||||
assertEquals(1, result.failed().size());
|
||||
assertEquals("bad", result.failed().get(0)[0]);
|
||||
assertEquals("InvalidParameter", result.failed().get(0)[1]);
|
||||
assertEquals(1, result.successful().size());
|
||||
assertEquals("good", result.successful().get(0)[0]);
|
||||
|
||||
List<PushNotification> captured = snsService.peekPushNotifications(device.getArn());
|
||||
assertEquals(1, captured.size());
|
||||
assertEquals("plain text", captured.get(0).payload());
|
||||
}
|
||||
|
||||
// --- Inspection helpers ---
|
||||
|
||||
@Test
|
||||
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
package io.github.hectorvent.floci.services.sns;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.sns.model.PlatformApplication;
|
||||
import io.github.hectorvent.floci.services.sns.model.PlatformEndpoint;
|
||||
import io.github.hectorvent.floci.services.sns.model.PushNotification;
|
||||
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 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.*;
|
||||
|
||||
/**
|
||||
* Tests that {@code MessageStructure="json"} resolves a per-protocol payload for every
|
||||
* subscription protocol, not just {@code application}, matching real SNS: each subscriber
|
||||
* receives the value under its protocol key, falling back to {@code default}.
|
||||
*/
|
||||
class SnsProtocolPayloadResolutionTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String BASE_URL = "http://localhost:4566";
|
||||
|
||||
private SnsService snsService;
|
||||
private SqsService sqsService;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_jsonStructure_deliversSqsKeyToRawSqsSubscriber() {
|
||||
String queueUrl = subscribeQueue("raw-queue", Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
snsService.publish(topic("alerts"), null, null,
|
||||
"{\"default\":\"hello\",\"sqs\":\"hi sqs\"}", null, "json",
|
||||
null, null, null, REGION);
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
assertEquals("hi sqs", messages.get(0).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_jsonStructure_embedsSqsKeyInSnsEnvelope() {
|
||||
String queueUrl = subscribeQueue("envelope-queue", Map.of());
|
||||
|
||||
snsService.publish(topic("alerts"), null, null,
|
||||
"{\"default\":\"hello\",\"sqs\":\"hi sqs\"}", null, "json",
|
||||
null, null, null, REGION);
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
String body = messages.get(0).getBody();
|
||||
assertTrue(body.contains("\"Message\":\"hi sqs\""),
|
||||
"SNS envelope should carry the resolved sqs payload, got: " + body);
|
||||
assertFalse(body.contains("\\\"default\\\""),
|
||||
"SNS envelope should not carry the raw json structure, got: " + body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_jsonStructure_fallsBackToDefaultWhenProtocolKeyAbsent() {
|
||||
String queueUrl = subscribeQueue("fallback-queue", Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
snsService.publish(topic("alerts"), null, null,
|
||||
"{\"default\":\"hello\",\"GCM\":\"push only\"}", null, "json",
|
||||
null, null, null, REGION);
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
assertEquals("hello", messages.get(0).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_jsonStructure_ignoresNonStringProtocolKeyAndFallsBackToDefault() {
|
||||
String queueUrl = subscribeQueue("nonstring-queue", Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
// Real SNS ignores a key whose value isn't a string, delivering default instead of "42".
|
||||
snsService.publish(topic("alerts"), null, null,
|
||||
"{\"default\":\"hi\",\"sqs\":42}", null, "json",
|
||||
null, null, null, REGION);
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
assertEquals("hi", messages.get(0).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_jsonStructure_rejectsNonStringDefault() {
|
||||
subscribeQueue("nonstring-default-queue", Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> snsService.publish(
|
||||
topic("alerts"), null, null,
|
||||
"{\"default\":42}", null, "json",
|
||||
null, null, null, REGION));
|
||||
assertEquals("InvalidParameter", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_withoutJsonStructure_deliversRawMessageUnchanged() {
|
||||
String queueUrl = subscribeQueue("plain-queue", Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
snsService.publish(topic("alerts"), null, null,
|
||||
"{\"default\":\"hello\",\"sqs\":\"hi sqs\"}", null, null,
|
||||
null, null, null, REGION);
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
assertEquals("{\"default\":\"hello\",\"sqs\":\"hi sqs\"}", messages.get(0).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_jsonStructure_resolvesPerProtocolForSqsAndPlatformEndpointOnSameTopic() {
|
||||
String topicArn = topic("mixed");
|
||||
String queueUrl = subscribeQueueTo(topicArn, "mixed-queue", Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
PlatformApplication app = snsService.createPlatformApplication("android-app", "GCM", Map.of(), REGION);
|
||||
PlatformEndpoint device = snsService.createPlatformEndpoint(app.getArn(), "fcm-token", null, Map.of(), REGION);
|
||||
snsService.subscribe(topicArn, "application", device.getArn(), REGION, Map.of());
|
||||
|
||||
snsService.publish(topicArn, null, null,
|
||||
"{\"default\":\"hello\",\"sqs\":\"hi sqs\",\"GCM\":\"hi device\"}", null, "json",
|
||||
null, null, null, REGION);
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
assertEquals("hi sqs", messages.get(0).getBody());
|
||||
|
||||
List<PushNotification> captured = snsService.peekPushNotifications(device.getArn());
|
||||
assertEquals(1, captured.size());
|
||||
assertEquals("hi device", captured.get(0).payload());
|
||||
}
|
||||
|
||||
private String topic(String name) {
|
||||
return snsService.createTopic(name, null, null, REGION).getTopicArn();
|
||||
}
|
||||
|
||||
private String subscribeQueue(String queueName, Map<String, String> attributes) {
|
||||
return subscribeQueueTo(topic("alerts"), queueName, attributes);
|
||||
}
|
||||
|
||||
private String subscribeQueueTo(String topicArn, String queueName, Map<String, String> attributes) {
|
||||
sqsService.createQueue(queueName, Map.of(), REGION);
|
||||
String queueArn = "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":" + queueName;
|
||||
snsService.subscribe(topicArn, "sqs", queueArn, REGION, attributes);
|
||||
return BASE_URL + "/" + ACCOUNT + "/" + queueName;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user