mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(s3): support Filter rules in PutBucketNotificationConfiguration (#178)
* fix(s3Notification): ensure filter in the XML body does not crash the xml parser * fix(S3): notification configuration should support filtering by key name * refacto: use a generic xml parser method as a base, instead of creating a while new XML parser
This commit is contained in:
1 parent
845ac85363
commit
ef06fc3493
9 files changed
+585
-25
No files matched your search
@@ -0,0 +1,216 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class XmlParserTest {
|
||||
|
||||
// --- extractGroupsMulti: nested element resilience ---
|
||||
|
||||
@Test
|
||||
void extractGroupsMultiSkipsNestedElementsAndParsesLeaves() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Id>g1</Id>
|
||||
<Arn>arn:example</Arn>
|
||||
<Event>event:one</Event>
|
||||
<Nested>
|
||||
<Deep>value</Deep>
|
||||
</Nested>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, List<String>>> groups = XmlParser.extractGroupsMulti(xml, "Group");
|
||||
|
||||
assertEquals(1, groups.size());
|
||||
assertEquals(List.of("g1"), groups.get(0).get("Id"));
|
||||
assertEquals(List.of("arn:example"), groups.get(0).get("Arn"));
|
||||
assertEquals(List.of("event:one"), groups.get(0).get("Event"));
|
||||
assertNull(groups.get(0).get("Nested"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractGroupsMultiNestedElementBeforeLeaves() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Nested><Child>x</Child></Nested>
|
||||
<Name>after-nested</Name>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, List<String>>> groups = XmlParser.extractGroupsMulti(xml, "Group");
|
||||
|
||||
assertEquals(1, groups.size());
|
||||
assertEquals(List.of("after-nested"), groups.get(0).get("Name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractGroupsMultiMultipleGroupsWithAndWithoutNested() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Key>a</Key>
|
||||
</Group>
|
||||
<Group>
|
||||
<Key>b</Key>
|
||||
<Container><Inner>skip</Inner></Container>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, List<String>>> groups = XmlParser.extractGroupsMulti(xml, "Group");
|
||||
|
||||
assertEquals(2, groups.size());
|
||||
assertEquals(List.of("a"), groups.get(0).get("Key"));
|
||||
assertEquals(List.of("b"), groups.get(1).get("Key"));
|
||||
}
|
||||
|
||||
// --- extractGroups: same resilience for single-value variant ---
|
||||
|
||||
@Test
|
||||
void extractGroupsSkipsNestedElements() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Name>test</Name>
|
||||
<Nested><Deep>skip</Deep></Nested>
|
||||
<Value>kept</Value>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, String>> groups = XmlParser.extractGroups(xml, "Group");
|
||||
|
||||
assertEquals(1, groups.size());
|
||||
assertEquals("test", groups.get(0).get("Name"));
|
||||
assertEquals("kept", groups.get(0).get("Value"));
|
||||
assertNull(groups.get(0).get("Nested"));
|
||||
}
|
||||
|
||||
// --- extractPairsPerGroup ---
|
||||
|
||||
@Test
|
||||
void extractPairsPerGroupBasic() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Leaf>text</Leaf>
|
||||
<Wrapper>
|
||||
<Pair>
|
||||
<Key>color</Key>
|
||||
<Val>red</Val>
|
||||
</Pair>
|
||||
</Wrapper>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, String>> pairs =
|
||||
XmlParser.extractPairsPerGroup(xml, "Group", "Pair", "Key", "Val");
|
||||
|
||||
assertEquals(1, pairs.size());
|
||||
assertEquals("red", pairs.get(0).get("color"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractPairsPerGroupMultiplePairsPerGroup() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Wrapper>
|
||||
<Rule><Name>prefix</Name><Value>images/</Value></Rule>
|
||||
<Rule><Name>suffix</Name><Value>.jpg</Value></Rule>
|
||||
</Wrapper>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, String>> pairs =
|
||||
XmlParser.extractPairsPerGroup(xml, "Group", "Rule", "Name", "Value");
|
||||
|
||||
assertEquals(1, pairs.size());
|
||||
assertEquals("images/", pairs.get(0).get("prefix"));
|
||||
assertEquals(".jpg", pairs.get(0).get("suffix"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractPairsPerGroupMultipleGroups() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Tag><Key>env</Key><Val>prod</Val></Tag>
|
||||
</Group>
|
||||
<Group>
|
||||
<Tag><Key>team</Key><Val>infra</Val></Tag>
|
||||
<Tag><Key>cost</Key><Val>shared</Val></Tag>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, String>> pairs =
|
||||
XmlParser.extractPairsPerGroup(xml, "Group", "Tag", "Key", "Val");
|
||||
|
||||
assertEquals(2, pairs.size());
|
||||
assertEquals(Map.of("env", "prod"), pairs.get(0));
|
||||
assertEquals(Map.of("team", "infra", "cost", "shared"), pairs.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractPairsPerGroupEmptyWhenNoPairsFound() {
|
||||
String xml = """
|
||||
<Root>
|
||||
<Group>
|
||||
<Name>no-pairs-here</Name>
|
||||
</Group>
|
||||
</Root>
|
||||
""";
|
||||
|
||||
List<Map<String, String>> pairs =
|
||||
XmlParser.extractPairsPerGroup(xml, "Group", "Pair", "Key", "Value");
|
||||
|
||||
assertEquals(1, pairs.size());
|
||||
assertTrue(pairs.get(0).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractPairsPerGroupNullAndEmptyXml() {
|
||||
assertTrue(XmlParser.extractPairsPerGroup(null, "G", "P", "K", "V").isEmpty());
|
||||
assertTrue(XmlParser.extractPairsPerGroup("", "G", "P", "K", "V").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractPairsPerGroupIndexAlignedWithExtractGroupsMulti() {
|
||||
String xml = """
|
||||
<Conf>
|
||||
<QueueConfiguration>
|
||||
<Queue>arn:q1</Queue>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
</QueueConfiguration>
|
||||
<QueueConfiguration>
|
||||
<Queue>arn:q2</Queue>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
<Filter><S3Key>
|
||||
<FilterRule><Name>prefix</Name><Value>logs/</Value></FilterRule>
|
||||
</S3Key></Filter>
|
||||
</QueueConfiguration>
|
||||
</Conf>
|
||||
""";
|
||||
|
||||
var groups = XmlParser.extractGroupsMulti(xml, "QueueConfiguration");
|
||||
var filters = XmlParser.extractPairsPerGroup(xml, "QueueConfiguration",
|
||||
"FilterRule", "Name", "Value");
|
||||
|
||||
assertEquals(2, groups.size());
|
||||
assertEquals(2, filters.size());
|
||||
assertTrue(filters.get(0).isEmpty());
|
||||
assertEquals("logs/", filters.get(1).get("prefix"));
|
||||
}
|
||||
}
|
||||
@@ -873,4 +873,112 @@ class S3IntegrationTest {
|
||||
given().delete("/encoding-test-bucket/composite-encoded.txt");
|
||||
given().delete("/encoding-test-bucket");
|
||||
}
|
||||
|
||||
// --- S3 Notification Configuration with Filter ---
|
||||
|
||||
@Test
|
||||
@Order(90)
|
||||
void createNotificationBucket() {
|
||||
given()
|
||||
.when()
|
||||
.put("/notif-test-bucket")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(91)
|
||||
void putNotificationConfigWithFilterIsNotDropped() {
|
||||
String xml = """
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>my-notif</Id>
|
||||
<Queue>arn:aws:sqs:us-east-1:000000000000:test-queue</Queue>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>incoming/</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.queryParam("notification", "")
|
||||
.body(xml)
|
||||
.when()
|
||||
.put("/notif-test-bucket")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.queryParam("notification", "")
|
||||
.when()
|
||||
.get("/notif-test-bucket")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("QueueConfiguration"))
|
||||
.body(containsString("arn:aws:sqs:us-east-1:000000000000:test-queue"))
|
||||
.body(containsString("s3:ObjectCreated:*"))
|
||||
// Verify filter rules are preserved in round-trip
|
||||
.body(containsString("Filter"))
|
||||
.body(containsString("FilterRule"))
|
||||
.body(containsString("<Name>prefix</Name>"))
|
||||
.body(containsString("<Value>incoming/</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(92)
|
||||
void putNotificationConfigWithFilterBeforeQueueIsNotDropped() {
|
||||
// Filter appears BEFORE Queue — ensures element order doesn't matter
|
||||
String xml = """
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>filter-first</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>.csv</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:aws:sqs:us-east-1:000000000000:csv-queue</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.queryParam("notification", "")
|
||||
.body(xml)
|
||||
.when()
|
||||
.put("/notif-test-bucket")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.queryParam("notification", "")
|
||||
.when()
|
||||
.get("/notif-test-bucket")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("QueueConfiguration"))
|
||||
.body(containsString("arn:aws:sqs:us-east-1:000000000000:csv-queue"))
|
||||
.body(containsString("s3:ObjectCreated:Put"))
|
||||
.body(containsString("<Name>suffix</Name>"))
|
||||
.body(containsString("<Value>.csv</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(93)
|
||||
void cleanupNotificationBucket() {
|
||||
given().delete("/notif-test-bucket");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3.model.FilterRule;
|
||||
import io.github.hectorvent.floci.services.s3.model.QueueNotification;
|
||||
import io.github.hectorvent.floci.services.s3.model.TopicNotification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class S3NotificationModelTest {
|
||||
|
||||
// --- matchesKey ---
|
||||
|
||||
@Test
|
||||
void matchesKeyWithNullFilterRulesFromJacksonDeserialization() {
|
||||
var qn = new QueueNotification("id", "arn:aws:sqs:us-east-1:000000000000:q",
|
||||
List.of("s3:ObjectCreated:*"), null);
|
||||
assertTrue(qn.matchesKey("anything"));
|
||||
|
||||
var tn = new TopicNotification("id", "arn:aws:sns:us-east-1:000000000000:t",
|
||||
List.of("s3:ObjectCreated:*"), null);
|
||||
assertTrue(tn.matchesKey("anything"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesKeyWithEmptyFilterRulesMatchesAll() {
|
||||
var qn = new QueueNotification("id", "arn", List.of("s3:ObjectCreated:*"));
|
||||
assertTrue(qn.matchesKey("anything"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesKeyEnforcesAllRules() {
|
||||
var qn = new QueueNotification("id", "arn", List.of("s3:ObjectCreated:*"),
|
||||
List.of(new FilterRule("prefix", "images/"), new FilterRule("suffix", ".jpg")));
|
||||
assertTrue(qn.matchesKey("images/photo.jpg"));
|
||||
assertFalse(qn.matchesKey("images/photo.png"));
|
||||
assertFalse(qn.matchesKey("docs/photo.jpg"));
|
||||
}
|
||||
|
||||
// --- FilterRule.matches ---
|
||||
|
||||
@Test
|
||||
void filterRulePrefixMatch() {
|
||||
FilterRule rule = new FilterRule("prefix", "images/");
|
||||
assertTrue(rule.matches("images/photo.jpg"));
|
||||
assertFalse(rule.matches("docs/file.txt"));
|
||||
assertFalse(rule.matches(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterRuleSuffixMatch() {
|
||||
FilterRule rule = new FilterRule("suffix", ".jpg");
|
||||
assertTrue(rule.matches("images/photo.jpg"));
|
||||
assertFalse(rule.matches("images/photo.png"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterRuleUnknownNameDoesNotMatch() {
|
||||
FilterRule rule = new FilterRule("extension", ".jpg");
|
||||
assertFalse(rule.matches("photo.jpg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterRuleNameIsCaseInsensitive() {
|
||||
assertTrue(new FilterRule("Prefix", "images/").matches("images/photo.jpg"));
|
||||
assertTrue(new FilterRule("SUFFIX", ".jpg").matches("photo.jpg"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user