Light, fluffy, and always free - The AWS Local Emulator alternative
fix(redshift-serverless): return HTTP 400 for ResourceNotFoundException and ConflictException (#5409)
Vũ Thế Huy972ff03f0058
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.javaindex ca0334cf8e..94bd01733c 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java@@ -216,8 +216,7 @@ public class RedshiftServerlessService implements Resettable {validateNamespaceName(namespaceName);String key = storageKey(region, namespaceName);if (namespaces.get(key).isPresent()) {- throw new AwsException("ConflictException",- "The namespace " + namespaceName + " already exists.", 409);+ throw conflict("The namespace " + namespaceName + " already exists.");}Namespace namespace = new Namespace();@@ -340,8 +339,7 @@ public class RedshiftServerlessService implements Resettable {// A workgroup cannot outlive its namespace; the model lists ConflictException for this call// but not its trigger, so this guard is the single place to relax if AWS turns out to differ.if (workgroupOf(namespaceName, region).isPresent()) {- throw new AwsException("ConflictException",- "The namespace " + namespaceName + " still has a workgroup. Delete the workgroup first.", 409);+ throw conflict("The namespace " + namespaceName + " still has a workgroup. Delete the workgroup first.");}namespaces.delete(storageKey(region, namespaceName));deleted.setStatus("DELETING");@@ -363,8 +361,7 @@ public class RedshiftServerlessService implements Resettable {Namespace namespace = getNamespace(namespaceName, region);String key = storageKey(region, snapshotName);if (snapshots.get(key).isPresent()) {- throw new AwsException("ConflictException",- "The snapshot " + snapshotName + " already exists.", 409);+ throw conflict("The snapshot " + snapshotName + " already exists.");}RedshiftServerlessSnapshot snapshot = new RedshiftServerlessSnapshot();snapshot.setSnapshotName(snapshotName);@@ -459,8 +456,7 @@ public class RedshiftServerlessService implements Resettable {public RedshiftServerlessSnapshot getSnapshot(String snapshotName, String region) {requireSnapshotName(snapshotName);return snapshots.get(storageKey(region, snapshotName))- .orElseThrow(() -> new AwsException("ResourceNotFoundException",- "The snapshot " + snapshotName + " was not found.", 404));+ .orElseThrow(() -> snapshotNotFound(snapshotName));}/**@@ -516,8 +512,7 @@ public class RedshiftServerlessService implements Resettable {validateNamespaceName(namespaceName);RedshiftServerlessSnapshot snapshot = getSnapshot(snapshotName, region);if (ownerAccount != null && !ownerAccount.isBlank() && !ownerAccount.equals(snapshot.getOwnerAccount())) {- throw new AwsException("ResourceNotFoundException",- "The snapshot " + snapshotName + " was not found.", 404);+ throw snapshotNotFound(snapshotName);}Workgroup workgroup = getWorkgroup(workgroupName, region);if (!namespaceName.equals(workgroup.getNamespaceName())) {@@ -641,7 +636,7 @@ public class RedshiftServerlessService implements Resettable {private static AwsException resourceNotFound(String resourceArn) {return new AwsException("ResourceNotFoundException",- "The resource " + resourceArn + " was not found.", 404);+ "The resource " + resourceArn + " was not found.", 400);}public synchronized Workgroup createWorkgroup(String workgroupName, String namespaceName,@@ -652,12 +647,10 @@ public class RedshiftServerlessService implements Resettable {Namespace namespace = getNamespace(namespaceName, region);String key = storageKey(region, workgroupName);if (workgroups.get(key).isPresent()) {- throw new AwsException("ConflictException",- "The workgroup " + workgroupName + " already exists.", 409);+ throw conflict("The workgroup " + workgroupName + " already exists.");}if (workgroupOf(namespaceName, region).isPresent()) {- throw new AwsException("ConflictException",- "The namespace " + namespaceName + " already has a workgroup.", 409);+ throw conflict("The namespace " + namespaceName + " already has a workgroup.");}Workgroup workgroup = new Workgroup();@@ -1022,7 +1015,7 @@ public class RedshiftServerlessService implements Resettable {private static AwsException workgroupNotFound(String workgroupName) {return new AwsException("ResourceNotFoundException",- "The workgroup " + workgroupName + " was not found.", 404);+ "The workgroup " + workgroupName + " was not found.", 400);}private static String storageKey(String region, String namespaceName) {@@ -1031,7 +1024,16 @@ public class RedshiftServerlessService implements Resettable {private static AwsException notFound(String namespaceName) {return new AwsException("ResourceNotFoundException",- "The namespace " + namespaceName + " was not found.", 404);+ "The namespace " + namespaceName + " was not found.", 400);+ }++ private static AwsException snapshotNotFound(String snapshotName) {+ return new AwsException("ResourceNotFoundException",+ "The snapshot " + snapshotName + " was not found.", 400);+ }++ private static AwsException conflict(String message) {+ return new AwsException("ConflictException", message, 400);}private static AwsException validation(String message) {diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.javaindex 124ba3220f..098fdb28a3 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java@@ -149,8 +149,8 @@ class RedshiftServerlessCfnIntegrationTest {CfnStackWaits.awaitStackDeleted(STACK);// The workgroup is deleted before the namespace, which would otherwise be refused.- call("GetWorkgroup", "{\"workgroupName\":\"" + WORKGROUP + "\"}").statusCode(404);- call("GetNamespace", "{\"namespaceName\":\"" + NAMESPACE + "\"}").statusCode(404);+ call("GetWorkgroup", "{\"workgroupName\":\"" + WORKGROUP + "\"}").statusCode(400);+ call("GetNamespace", "{\"namespaceName\":\"" + NAMESPACE + "\"}").statusCode(400);}private static void cloudFormation(String action, String capacity) {diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.javaindex 49d07f0300..ae6eaa1ca8 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java@@ -115,7 +115,7 @@ class RedshiftServerlessIntegrationTest {.body("namespace.status", equalTo("DELETING"));call("GetNamespace", "{\"namespaceName\":\"lifecycle-ns\"}")- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}@@ -124,7 +124,7 @@ class RedshiftServerlessIntegrationTest {call("CreateNamespace", "{\"namespaceName\":\"conflict-ns\",\"adminUsername\":\"admin\"}").statusCode(200);call("CreateNamespace", "{\"namespaceName\":\"conflict-ns\",\"adminUsername\":\"admin\"}")- .statusCode(409)+ .statusCode(400).body("__type", equalTo("ConflictException"));call("DeleteNamespace", "{\"namespaceName\":\"conflict-ns\"}").statusCode(200);}@@ -132,7 +132,7 @@ class RedshiftServerlessIntegrationTest {@Testvoid deletingAnUnknownNamespaceReturnsResourceNotFound() {call("DeleteNamespace", "{\"namespaceName\":\"absent-ns\"}")- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}@@ -188,7 +188,7 @@ class RedshiftServerlessIntegrationTest {String absent = "arn:aws:redshift-serverless:us-east-1:000000000000:"+ "namespace/00000000-0000-0000-0000-000000000000";call("ListTagsForResource", "{\"resourceArn\":\"" + absent + "\"}")- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}@@ -217,7 +217,7 @@ class RedshiftServerlessIntegrationTest {.extract().path("snapshot.snapshotArn");call("CreateSnapshot", "{\"snapshotName\":\"snap-life\",\"namespaceName\":\"snap-life-ns\"}")- .statusCode(409)+ .statusCode(400).body("__type", equalTo("ConflictException"));call("GetSnapshot", "{\"snapshotName\":\"snap-life\"}")@@ -265,7 +265,7 @@ class RedshiftServerlessIntegrationTest {.statusCode(200).body("snapshot.snapshotName", equalTo("snap-life"));call("GetSnapshot", "{\"snapshotName\":\"snap-life\"}")- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}@@ -308,14 +308,14 @@ class RedshiftServerlessIntegrationTest {.body("workgroup.pendingTrackName", equalTo("trailing"));call("DeleteNamespace", "{\"namespaceName\":\"wg-life-ns\"}")- .statusCode(409)+ .statusCode(400).body("__type", equalTo("ConflictException"));call("DeleteWorkgroup", "{\"workgroupName\":\"wg-life\"}").statusCode(200).body("workgroup.status", equalTo("DELETING"));call("GetWorkgroup", "{\"workgroupName\":\"wg-life\"}")- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));call("DeleteNamespace", "{\"namespaceName\":\"wg-life-ns\"}").statusCode(200);}@@ -347,7 +347,7 @@ class RedshiftServerlessIntegrationTest {@Testvoid workgroupCreateRejectsAnUnknownNamespaceADuplicateAndABadPort() {call("CreateWorkgroup", "{\"workgroupName\":\"wg-orphan\",\"namespaceName\":\"absent-ns\"}")- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));call("CreateNamespace", "{\"namespaceName\":\"wg-err-ns\",\"adminUsername\":\"admin\"}").statusCode(200);@@ -356,7 +356,7 @@ class RedshiftServerlessIntegrationTest {.body("__type", equalTo("ValidationException"));call("CreateWorkgroup", "{\"workgroupName\":\"wg-err\",\"namespaceName\":\"wg-err-ns\"}").statusCode(200);call("CreateWorkgroup", "{\"workgroupName\":\"wg-err\",\"namespaceName\":\"wg-err-ns\"}")- .statusCode(409)+ .statusCode(400).body("__type", equalTo("ConflictException"));call("DeleteWorkgroup", "{\"workgroupName\":\"wg-err\"}").statusCode(200);diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.javaindex 80aff1a594..4d7ca9dac0 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java@@ -236,7 +236,7 @@ class RedshiftServerlessJsonHandlerTest {assertEquals(400, wrongType.getStatus());Response missing = handler.handle("GetCredentials", parse("{\"workgroupName\":\"absent-wg\"}"), REGION, "AUTH");- assertEquals(404, missing.getStatus());+ assertEquals(400, missing.getStatus());assertEquals("ResourceNotFoundException", errorType(missing));Response customDomain = handler.handle("GetCredentials",@@ -313,7 +313,7 @@ class RedshiftServerlessJsonHandlerTest {assertEquals("DELETING", deleted.get("status").textValue());Response missing = handler.handle("GetWorkgroup", request("workgroupName", "del-shape-wg"), REGION);- assertEquals(404, missing.getStatus());+ assertEquals(400, missing.getStatus());assertEquals("ResourceNotFoundException", errorType(missing));}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.javaindex 7bdb66a2e6..57a8173de6 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java@@ -368,8 +368,10 @@ class RedshiftServerlessServiceTest {@Testvoid createWorkgroupRequiresAnExistingNamespace() {- assertEquals("ResourceNotFoundException", assertThrows(AwsException.class,- () -> createWorkgroup("wg-orphan", "absent-ns")).getErrorCode());+ AwsException ex = assertThrows(AwsException.class,+ () -> createWorkgroup("wg-orphan", "absent-ns"));+ assertEquals("ResourceNotFoundException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());}@Test@@ -378,10 +380,15 @@ class RedshiftServerlessServiceTest {create("two-ns");createWorkgroup("wg-first", "one-ns");- assertEquals("ConflictException", assertThrows(AwsException.class,- () -> createWorkgroup("wg-second", "one-ns")).getErrorCode());- assertEquals("ConflictException", assertThrows(AwsException.class,- () -> createWorkgroup("wg-first", "two-ns")).getErrorCode());+ AwsException conflict1 = assertThrows(AwsException.class,+ () -> createWorkgroup("wg-second", "one-ns"));+ assertEquals("ConflictException", conflict1.getErrorCode());+ assertEquals(400, conflict1.getHttpStatus());++ AwsException conflict2 = assertThrows(AwsException.class,+ () -> createWorkgroup("wg-first", "two-ns"));+ assertEquals("ConflictException", conflict2.getErrorCode());+ assertEquals(400, conflict2.getHttpStatus());}@Test