Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(cloudwatch): return ResourceNotFound for SetAlarmState on missing alarm (#1621)

Matt Van Horn2c746b25a1e4
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java
index bf17a75434..b9d9476f9a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.cloudwatch.metrics;
+import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -266,7 +267,7 @@ public class CloudWatchMetricsService {
public void setAlarmState(String alarmName, String stateValue, String stateReason, String stateReasonData, String region) {
String key = region + "::" + alarmName;
MetricAlarm alarm = alarmStore.get(key)
- .orElseThrow(() -> new RuntimeException("Alarm not found: " + alarmName));
+ .orElseThrow(() -> new AwsException("ResourceNotFound", "Alarm not found: " + alarmName, 404));
alarm.setStateValue(stateValue);
alarm.setStateReason(stateReason);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java
index 969ba0356f..ec23f2084c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.cloudwatch.metrics;
+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.cloudwatch.metrics.model.Dimension;
@@ -229,6 +230,15 @@ class CloudWatchMetricsServiceTest {
assertEquals("Threshold breached", updated.getStateReason());
}
+ @Test
+ void setAlarmStateOnNonexistentAlarmThrowsResourceNotFound() {
+ AwsException ex = assertThrows(AwsException.class,
+ () -> service.setAlarmState("missing-alarm", "ALARM", "reason", null, REGION));
+
+ assertEquals(404, ex.getHttpStatus());
+ assertEquals("ResourceNotFound", ex.getErrorCode());
+ }
+
@Test
void buildDimKeyIsDeterministic() {
List<Dimension> dims1 = List.of(new Dimension("B", "2"), new Dimension("A", "1"));
@@ -243,4 +253,4 @@ class CloudWatchMetricsServiceTest {
assertEquals("", CloudWatchMetricsService.buildDimKey(List.of()));
assertEquals("", CloudWatchMetricsService.buildDimKey(null));
}
-}
\ No newline at end of file
+}