Light, fluffy, and always free - The AWS Local Emulator alternative
fix(backup): return HTTP 400 for ResourceNotFoundException (#5794)
Allen Sanborna7df271bb317
diff --git a/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java b/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.javaindex 8729ab2fc1..74eb600808 100644--- a/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java+++ b/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java@@ -125,7 +125,7 @@ public class BackupService {public BackupVault describeBackupVault(String vaultName, String region) {return vaultStore.get(vaultKey(region, vaultName))- .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup vault not found: " + vaultName, 404));+ .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup vault not found: " + vaultName, 400));}public void deleteBackupVault(String vaultName, String region) {@@ -511,7 +511,7 @@ public class BackupService {public BackupPlan getBackupPlan(String planId) {return planStore.get(planId)- .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup plan not found: " + planId, 404));+ .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup plan not found: " + planId, 400));}public BackupPlan updateBackupPlan(String planId, String planName, List<BackupRule> rules) {@@ -564,9 +564,9 @@ public class BackupService {public BackupSelection getBackupSelection(String planId, String selectionId) {BackupSelection sel = selectionStore.get(selectionId)- .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup selection not found: " + selectionId, 404));+ .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup selection not found: " + selectionId, 400));if (!planId.equals(sel.getBackupPlanId())) {- throw new AwsException("ResourceNotFoundException", "Backup selection not found in plan: " + planId, 404);+ throw new AwsException("ResourceNotFoundException", "Backup selection not found in plan: " + planId, 400);}return sel;}@@ -623,7 +623,7 @@ public class BackupService {public BackupJob describeBackupJob(String jobId) {return jobStore.get(jobId)- .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup job not found: " + jobId, 404));+ .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup job not found: " + jobId, 400));}public void stopBackupJob(String jobId) {@@ -657,7 +657,7 @@ public class BackupService {return recoveryStore.get(recoveryPointArn).filter(rp -> vaultName.equals(rp.getBackupVaultName())).orElseThrow(() -> new AwsException("ResourceNotFoundException",- "Recovery point not found: " + recoveryPointArn, 404));+ "Recovery point not found: " + recoveryPointArn, 400));}public List<RecoveryPoint> listRecoveryPointsByBackupVault(String vaultName, String region) {@@ -842,7 +842,7 @@ public class BackupService {if (plan.isPresent()) {return new HashMap<>();}- throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 404);+ throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400);}private void applyTags(String arn, Map<String, String> newTags) {@@ -855,7 +855,7 @@ public class BackupService {vaultStore.put(vaultKey(vault), vault);return;}- throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 404);+ throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400);}private void removeTags(String arn, List<String> tagKeys) {@@ -868,7 +868,7 @@ public class BackupService {vaultStore.put(vaultKey(vault), vault);return;}- throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 404);+ throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400);}private static void assignRuleIds(List<BackupRule> rules) {diff --git a/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.javaindex caa764f3b0..93499689ee 100644--- a/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.java@@ -534,6 +534,83 @@ class BackupIntegrationTest {.body("Tags.env", equalTo("test"));}+ // ── ResourceNotFoundException is HTTP 400 on every lookup ──────────────────++ @Test+ @Order(63)+ void missingSelectionReturns400() {+ given().header("Authorization", AUTH)+ .when().get("/backup/plans/" + planId + "/selections/no-such-selection")+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));++ given().header("Authorization", AUTH)+ .when().delete("/backup/plans/" + planId + "/selections/no-such-selection")+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));+ }++ @Test+ @Order(64)+ void selectionLookedUpUnderAnotherPlanReturns400() {+ String otherPlanId = given()+ .header("Authorization", AUTH)+ .contentType("application/json")+ .body("""+ {+ "BackupPlan": {+ "BackupPlanName": "other-plan",+ "Rules": [{+ "RuleName": "daily",+ "TargetBackupVaultName": "%s",+ "ScheduleExpression": "cron(0 12 * * ? *)"+ }]+ }+ }+ """.formatted(VAULT_NAME))+ .when().put("/backup/plans/")+ .then().statusCode(200).extract().path("BackupPlanId");++ given().header("Authorization", AUTH)+ .when().get("/backup/plans/" + otherPlanId + "/selections/" + selectionId)+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));++ given().header("Authorization", AUTH)+ .when().delete("/backup/plans/" + otherPlanId)+ .then().statusCode(204);+ }++ @Test+ @Order(65)+ void missingRecoveryPointReturns400() {+ // The vault exists; the recovery point was deleted at Order(54).+ given().header("Authorization", AUTH)+ .when().get("/backup-vaults/" + VAULT_NAME + "/recovery-points/" + recoveryPointArn)+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));++ given().header("Authorization", AUTH)+ .when().delete("/backup-vaults/" + VAULT_NAME + "/recovery-points/" + recoveryPointArn)+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));+ }++ @Test+ @Order(66)+ void tagOperationsOnAMissingResourceReturn400() {+ String missingArn = "arn:aws:backup:us-east-1:000000000000:backup-vault:no-such-vault";++ given().header("Authorization", AUTH)+ .when().get("/tags/" + missingArn)+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));++ given().header("Authorization", AUTH).contentType("application/json")+ .body("{\"Tags\":{\"team\":\"platform\"}}")+ .when().post("/tags/" + missingArn)+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));++ given().header("Authorization", AUTH).contentType("application/json")+ .body("{\"TagKeyList\":[\"team\"]}")+ .when().post("/untag/" + missingArn)+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));+ }+// ── Supported Resource Types ───────────────────────────────────────────────@Test@@ -587,13 +664,27 @@ class BackupIntegrationTest {@Test@Order(83)- void describeDeletedVaultReturns404() {+ void describeDeletedVaultReturns400() {+ // AWS Backup documents ResourceNotFoundException as HTTP 400, not 404.given().header("Authorization", AUTH).when().get("/backup-vaults/" + VAULT_NAME).then()- .statusCode(404);+ .statusCode(400)+ .body("__type", equalTo("ResourceNotFoundException"));+ }++ @Test+ @Order(84)+ void missingPlanAndJobReturn400() {+ given().header("Authorization", AUTH)+ .when().get("/backup/plans/no-such-plan/")+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));++ given().header("Authorization", AUTH)+ .when().get("/backup-jobs/no-such-job")+ .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}// ── Vault sub-resources: access policy, notifications, lock ────────────────@@ -670,7 +761,7 @@ class BackupIntegrationTest {given().header("Authorization", AUTH).contentType("application/json").body("{\"Policy\":\"{}\"}").when().put("/backup-vaults/no-such-vault/access-policy")- .then().statusCode(404).body("message", containsString("no-such-vault"));+ .then().statusCode(400).body("message", containsString("no-such-vault"));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.javaindex 42dd4e2589..13ae0caeb4 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.java@@ -93,8 +93,8 @@ class BackupVaultCfnIntegrationTest {cloudFormation("DeleteStack", Map.of());CfnStackWaits.awaitStackDeleted(STACK);- describeVault(NAMED).statusCode(404);- describeVault(unnamed).statusCode(404);+ describeVault(NAMED).statusCode(400).body("__type", equalTo("ResourceNotFoundException"));+ describeVault(unnamed).statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}private static void cloudFormation(String action, Map<String, String> parameters) {diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.javaindex ac14af5991..79048f854c 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.java@@ -39,7 +39,7 @@ class BackupVaultCfnProvisionerTest {private static final String ARN = "arn:aws:backup:us-east-1:000000000000:backup-vault:my-vault";private static final String KMS = "arn:aws:kms:us-east-1:000000000000:key/abc";private static final AwsException NOT_FOUND =- new AwsException("ResourceNotFoundException", "Backup vault not found: my-vault", 404);+ new AwsException("ResourceNotFoundException", "Backup vault not found: my-vault", 400);private final BackupService backup = mock(BackupService.class);private final BackupVaultCfnProvisioner provisioner = new BackupVaultCfnProvisioner(backup);