Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(eventbridge): keep persisted rules from disappearing on reload (#878)

Yoshiyuki Ohtaff80dac7809d
diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java
index 0d91ca7132..2c195eb347 100644
--- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java
+++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java
@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.eventbridge.model;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.quarkus.runtime.annotations.RegisterForReflection;
@@ -8,6 +10,7 @@ import java.util.HashMap;
import java.util.Map;
@RegisterForReflection
+@JsonIgnoreProperties(ignoreUnknown = true)
public class Rule {
private String name;
@@ -57,6 +60,7 @@ public class Rule {
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
+ @JsonIgnore
public String getRegion() {
return AwsArnUtils.regionOrDefault(arn, null);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/RulePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/RulePersistenceTest.java
new file mode 100644
index 0000000000..b252162bd7
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/RulePersistenceTest.java
@@ -0,0 +1,84 @@
+package io.github.hectorvent.floci.services.eventbridge;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import io.github.hectorvent.floci.core.storage.PersistentStorage;
+import io.github.hectorvent.floci.services.eventbridge.model.Rule;
+import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class RulePersistenceTest {
+
+ @TempDir
+ Path tempDir;
+
+ @Test
+ void rulePersistsAcrossInstances() {
+ Path filePath = tempDir.resolve("eventbridge-rules.json");
+ String key = "000000000000/rule:us-east-1:default/my-rule";
+
+ Rule rule = new Rule();
+ rule.setName("my-rule");
+ rule.setArn("arn:aws:events:us-east-1:000000000000:rule/my-rule");
+ rule.setAccountId("000000000000");
+ rule.setEventBusName("default");
+ rule.setScheduleExpression("rate(5 minutes)");
+ rule.setState(RuleState.ENABLED);
+
+ var writer = new PersistentStorage<String, Rule>(
+ filePath, new TypeReference<Map<String, Rule>>() {});
+ writer.put(key, rule);
+
+ var reader = new PersistentStorage<String, Rule>(
+ filePath, new TypeReference<Map<String, Rule>>() {});
+ reader.load();
+
+ Optional<Rule> loaded = reader.get(key);
+ assertTrue(loaded.isPresent(),
+ "Rule should survive a fresh PersistentStorage instance + load()");
+ assertEquals("my-rule", loaded.get().getName());
+ assertEquals("arn:aws:events:us-east-1:000000000000:rule/my-rule", loaded.get().getArn());
+ assertEquals("rate(5 minutes)", loaded.get().getScheduleExpression());
+ assertEquals(RuleState.ENABLED, loaded.get().getState());
+ }
+
+ @Test
+ void ignoresUnknownPropertiesOnLoad() throws Exception {
+ // A persisted file may contain fields the current Rule class does not
+ // recognize: either a derived field written by an older Floci version
+ // (e.g. "region") or a brand new field from a future version. Loading
+ // must not fail in either case.
+ Path filePath = tempDir.resolve("eventbridge-rules.json");
+ String key = "000000000000/rule:us-east-1:default/legacy-rule";
+ String legacyJson = """
+ {
+ "%s" : {
+ "name" : "legacy-rule",
+ "arn" : "arn:aws:events:us-east-1:000000000000:rule/legacy-rule",
+ "accountId" : "000000000000",
+ "eventBusName" : "default",
+ "scheduleExpression" : "rate(5 minutes)",
+ "state" : "ENABLED",
+ "fieldFromFutureVersion" : "whatever"
+ }
+ }
+ """.formatted(key);
+ Files.writeString(filePath, legacyJson);
+
+ var store = new PersistentStorage<String, Rule>(
+ filePath, new TypeReference<Map<String, Rule>>() {});
+ store.load();
+
+ Optional<Rule> loaded = store.get(key);
+ assertTrue(loaded.isPresent(),
+ "Rules with unknown extra fields should still load");
+ assertEquals("legacy-rule", loaded.get().getName());
+ }
+}