Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Shirshov committed Oct 9, 2023
1 parent dbe93f1 commit cc99190
Showing 1 changed file with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Tag> updatedTags = new ArrayList<>(TAG_LIST);
updatedTags.add(Tag.builder().key("tag-key").value("resource-level").build());

test_handleRequest_base(
context,
ResourceHandlerRequest.<ResourceModel>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<AddTagsToResourceRequest> 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();
Expand All @@ -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.<ResourceModel>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.<ResourceModel>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<AddTagsToResourceRequest> 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.
Expand Down

0 comments on commit cc99190

Please sign in to comment.