From cc991908168f127eb1bd9e8ff2d7db213bd2086a Mon Sep 17 00:00:00 2001 From: Valentin Shirshov Date: Mon, 9 Oct 2023 17:32:30 +0200 Subject: [PATCH] Add more tests --- .../rds/dbinstance/UpdateHandlerTest.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/aws-rds-dbinstance/src/test/java/software/amazon/rds/dbinstance/UpdateHandlerTest.java b/aws-rds-dbinstance/src/test/java/software/amazon/rds/dbinstance/UpdateHandlerTest.java index 18db2b06d..65319a8da 100644 --- a/aws-rds-dbinstance/src/test/java/software/amazon/rds/dbinstance/UpdateHandlerTest.java +++ b/aws-rds-dbinstance/src/test/java/software/amazon/rds/dbinstance/UpdateHandlerTest.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; @@ -313,6 +314,41 @@ public void handleRequest_SuccessTagsAddOnly() { verify(rdsProxy.client(), times(2)).describeDBInstances(any(DescribeDbInstancesRequest.class)); } + @Test + public void handleRequest_ResourceTagsPrioritizedOverStackTags() { + final AddTagsToResourceResponse addTagsToResourceResponse = AddTagsToResourceResponse.builder().build(); + when(rdsProxy.client().addTagsToResource(any(AddTagsToResourceRequest.class))).thenReturn(addTagsToResourceResponse); + + final CallbackContext context = new CallbackContext(); + context.setUpdated(true); + context.setRebooted(true); + context.setUpdatedRoles(true); + context.setStorageAllocated(true); + + List updatedTags = new ArrayList<>(TAG_LIST); + updatedTags.add(Tag.builder().key("tag-key").value("resource-level").build()); + + test_handleRequest_base( + context, + ResourceHandlerRequest.builder() + .previousResourceTags(Collections.emptyMap()) + .desiredResourceTags(Translator.translateTagsToRequest(Collections.singleton( + Tag.builder().key("tag-key").value("stack-level").build() + ))), + () -> DB_INSTANCE_ACTIVE, + () -> RESOURCE_MODEL_BLDR().build(), + () -> RESOURCE_MODEL_BLDR().tags(updatedTags).build(), + expectSuccess() + ); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(AddTagsToResourceRequest.class); + verify(rdsProxy.client()).addTagsToResource(captor.capture()); + Assertions.assertThat(captor.getValue().tags()) + .containsExactlyInAnyOrder( + software.amazon.awssdk.services.rds.model.Tag .builder().key("tag-key").value("resource-level").build()); + verify(rdsProxy.client(), times(2)).describeDBInstances(any(DescribeDbInstancesRequest.class)); + } + @Test public void handleRequest_SuccessTagsRemoveOnly() { final RemoveTagsFromResourceResponse removeTagsFromResourceResponse = RemoveTagsFromResourceResponse.builder().build(); @@ -336,6 +372,62 @@ public void handleRequest_SuccessTagsRemoveOnly() { verify(rdsProxy.client(), times(2)).describeDBInstances(any(DescribeDbInstancesRequest.class)); } + @Test + public void handleRequest_StackLevelTagsRemovalDoesNotRemoveTagsIfTheyExistOnResourceLevel() { + final CallbackContext context = new CallbackContext(); + context.setUpdated(true); + context.setRebooted(true); + context.setUpdatedRoles(true); + context.setStorageAllocated(true); + + test_handleRequest_base( + context, + ResourceHandlerRequest.builder() + .previousResourceTags(Translator.translateTagsToRequest(TAG_LIST)) + .desiredResourceTags(Collections.emptyMap()), + () -> DB_INSTANCE_ACTIVE, + () -> RESOURCE_MODEL_BLDR().build(), + () -> RESOURCE_MODEL_BLDR().build(), + expectSuccess() + ); + + verify(rdsProxy.client(), times(1)).describeDBInstances(any(DescribeDbInstancesRequest.class)); + } + + @Test + public void handleRequest_FallBackToStackLevelTagsIfResourceLevelWasRemoved() { + final AddTagsToResourceResponse addTagsToResourceResponse = AddTagsToResourceResponse.builder().build(); + when(rdsProxy.client().addTagsToResource(any(AddTagsToResourceRequest.class))).thenReturn(addTagsToResourceResponse); + + final CallbackContext context = new CallbackContext(); + context.setUpdated(true); + context.setRebooted(true); + context.setUpdatedRoles(true); + context.setStorageAllocated(true); + + test_handleRequest_base( + context, + ResourceHandlerRequest.builder() + .previousResourceTags(Translator.translateTagsToRequest(Collections.singleton( + Tag.builder().key("tag-key").value("stack-level").build()))) + .desiredResourceTags(Translator.translateTagsToRequest(Collections.singleton( + Tag.builder().key("tag-key").value("stack-level").build()))), + () -> DB_INSTANCE_ACTIVE, + () -> RESOURCE_MODEL_BLDR().tags(Collections.singletonList(Tag.builder().key("tag-key").value("resource-level").build())).build(), + () -> RESOURCE_MODEL_BLDR().tags(Collections.emptyList()).build(), + expectSuccess() + ); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(AddTagsToResourceRequest.class); + verify(rdsProxy.client()).addTagsToResource(captor.capture()); + Assertions.assertThat(captor.getValue().tags()) + .containsExactlyInAnyOrder( + software.amazon.awssdk.services.rds.model.Tag .builder().key("tag-key").value("stack-level").build()); + verify(rdsProxy.client()).removeTagsFromResource(any(RemoveTagsFromResourceRequest.class)); + + verify(rdsProxy.client(), times(2)).describeDBInstances(any(DescribeDbInstancesRequest.class)); + } + @Test public void handleRequest_DeleteNonExistingRole() { // compute a complete sequence of transitions from the initial set of roles to the final one.