Light, fluffy, and always free - The AWS Local Emulator alternative
fix(elasticache): use the AWS wire codes UserNotFound and UserAlreadyExists (#5452)
Vũ Thế Huyb318794fa268
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.javaindex 995af422b2..57ca6f16ee 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java@@ -31,7 +31,9 @@ import software.amazon.awssdk.services.elasticache.model.ModifyReplicationGroupRimport software.amazon.awssdk.services.elasticache.model.ModifyUserRequest;import software.amazon.awssdk.services.elasticache.model.ModifyUserResponse;import software.amazon.awssdk.services.elasticache.model.ReplicationGroup;+import software.amazon.awssdk.services.elasticache.model.UserAlreadyExistsException;import software.amazon.awssdk.services.elasticache.model.UserGroupNotFoundException;+import software.amazon.awssdk.services.elasticache.model.UserNotFoundException;import java.io.IOException;import java.io.InputStream;@@ -193,8 +195,6 @@ class ElastiCacheTest {void createDuplicateUserThrows409() {requireUser();- // Floci returns generic error code, SDK maps to ElastiCacheException- // rather than UserAlreadyExistsException (pre-existing deviation)assertThatThrownBy(() -> elasticache.createUser(CreateUserRequest.builder().userId(userId).userName(userName)@@ -205,7 +205,7 @@ class ElastiCacheTest {.passwords("user-password-1").build()).build()))- .isInstanceOf(ElastiCacheException.class)+ .isInstanceOf(UserAlreadyExistsException.class).hasMessageContaining("already exists");}@@ -276,12 +276,10 @@ class ElastiCacheTest {elasticache.deleteUser(DeleteUserRequest.builder().userId(userId).build());- // Floci returns generic error code, SDK maps to ElastiCacheException- // rather than UserNotFoundException (pre-existing deviation)assertThatThrownBy(() -> elasticache.describeUsers(DescribeUsersRequest.builder().userId(userId).build()))- .isInstanceOf(ElastiCacheException.class)+ .isInstanceOf(UserNotFoundException.class).hasMessageContaining("not found");userCreated = false;}diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.javaindex bcea43b5c6..e312e2d5a2 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java@@ -455,7 +455,7 @@ public class ElastiCacheCfnProvisioner implements CfnResourceProvisioner {case USER_GROUP -> CfnDeletes.safeDelete("ElastiCache user group", physicalId,() -> elastiCacheService.deleteUserGroup(physicalId), "UserGroupNotFound");case USER -> CfnDeletes.safeDelete("ElastiCache user", physicalId,- () -> elastiCacheService.deleteUser(physicalId), "UserNotFoundFault");+ () -> elastiCacheService.deleteUser(physicalId), "UserNotFound");default -> {}}diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.javaindex d1fb908dfa..35a7c75bae 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java@@ -1806,7 +1806,7 @@ public class ElastiCacheService implements ResourceProvider {public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode,List<String> passwords, String accessString, String engine) {if (users.get(userId).isPresent()) {- throw new AwsException("UserAlreadyExistsFault",+ throw new AwsException("UserAlreadyExists","User " + userId + " already exists.", 400);}@@ -1827,7 +1827,7 @@ public class ElastiCacheService implements ResourceProvider {public ElastiCacheUser getUser(String userId) {return users.get(userId).orElseThrow(() ->- new AwsException("UserNotFoundFault", "User " + userId + " not found.", 404));+ new AwsException("UserNotFound", "User " + userId + " not found.", 404));}public Collection<ElastiCacheUser> listUsers(String filterUserId, String filterEngine) {@@ -1835,7 +1835,7 @@ public class ElastiCacheService implements ResourceProvider {if (filterUserId != null && !filterUserId.isBlank()) {return users.get(filterUserId).map(List::of)- .orElseThrow(() -> new AwsException("UserNotFoundFault",+ .orElseThrow(() -> new AwsException("UserNotFound","User " + filterUserId + " not found.", 404));}if (filterEngine != null && !filterEngine.isBlank()) {@@ -1890,7 +1890,7 @@ public class ElastiCacheService implements ResourceProvider {public void deleteUser(String userId) {if (users.get(userId).isEmpty()) {- throw new AwsException("UserNotFoundFault", "User " + userId + " not found.", 404);+ throw new AwsException("UserNotFound", "User " + userId + " not found.", 404);}synchronized (userGroupLock) {users.delete(userId);diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.javaindex 6bc112f2fc..e3ff0bc0c9 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java@@ -679,7 +679,7 @@ class ElastiCacheCfnProvisionerTest {@Testvoid deleteToleratesAlreadyDeletedButPropagatesOtherFailures() {- doThrow(new AwsException("UserNotFoundFault", "gone", 404))+ doThrow(new AwsException("UserNotFound", "gone", 404)).when(cache).deleteUser("gone");provisioner.delete("AWS::ElastiCache::User", "gone", "us-east-1");diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.javaindex cc8688c806..c3c933090c 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java@@ -464,6 +464,20 @@ class ElastiCacheServiceTest {assertEquals(Set.of("valkey-user-id"), service.getUserGroup("vk-group").getUserIds());}+ @Test+ void userErrorsUseTheBotocoreWireCodesWithoutTheFaultSuffix() {+ service.createUser("dupe-user", "dupe", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis");++ AwsException duplicate = assertThrows(AwsException.class, () -> service.createUser(+ "dupe-user", "dupe", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis"));+ assertEquals("UserAlreadyExists", duplicate.getErrorCode());+ assertEquals(400, duplicate.getHttpStatus());+ assertEquals("UserNotFound", assertThrows(AwsException.class, () -> service.getUser("absent")).getErrorCode());+ assertEquals("UserNotFound",+ assertThrows(AwsException.class, () -> service.listUsers("absent", null)).getErrorCode());+ assertEquals("UserNotFound", assertThrows(AwsException.class, () -> service.deleteUser("absent")).getErrorCode());+ }+@Testvoid singleArgAuthFallsBackToGroupAuthToken() {service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");