package software.amazon.sns.topic; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.*; import software.amazon.cloudformation.exceptions.CfnNotFoundException; import software.amazon.cloudformation.proxy.*; import java.time.Duration; import java.util.HashMap; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) public class DeleteHandlerTest extends AbstractTestBase { @Mock private AmazonWebServicesClientProxy proxy; @Mock private ProxyClient proxyClient; @Mock SnsClient client; private DeleteHandler handler; @BeforeEach public void setup() { handler = new DeleteHandler(); proxy = new AmazonWebServicesClientProxy(logger, MOCK_CREDENTIALS, () -> Duration.ofSeconds(600).toMillis()); client = mock(SnsClient.class); proxyClient = MOCK_PROXY(proxy, client); } @AfterEach public void postExecute() { verify(client, atLeastOnce()).serviceName(); verifyNoMoreInteractions(client); } @Test public void handleRequest_SimpleSuccess() { final ResourceModel model = ResourceModel.builder() .topicArn("arn:aws:sns:us-east-1:123456789012:sns-topic-name") .build(); Map attributes = new HashMap<>(); attributes.put(TopicAttributeName.DISPLAY_NAME.toString(), "topic-display-name"); attributes.put(TopicAttributeName.TOPIC_ARN.toString(), "arn:aws:sns:us-east-1:123456789012:sns-topic-name"); final GetTopicAttributesResponse getTopicAttributesResponse = GetTopicAttributesResponse.builder() .attributes(attributes) .build(); when(proxyClient.client().getTopicAttributes(any(GetTopicAttributesRequest.class))).thenReturn(getTopicAttributesResponse); final DeleteTopicResponse deleteTopicResponse = DeleteTopicResponse.builder().build(); when(proxyClient.client().deleteTopic(any(DeleteTopicRequest.class))).thenReturn(deleteTopicResponse); final ResourceHandlerRequest request = ResourceHandlerRequest.builder().desiredResourceState(model).build(); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); verify(proxyClient.client()).getTopicAttributes(any(GetTopicAttributesRequest.class)); verify(proxyClient.client()).deleteTopic(any(DeleteTopicRequest.class)); } @Test public void handleRequest_NotFound() { final ResourceModel model = ResourceModel.builder() .topicArn("arn:aws:sns:us-east-1:123456789012:sns-topic-name") .build(); when(proxyClient.client().getTopicAttributes(any(GetTopicAttributesRequest.class))).thenThrow(NotFoundException.class); final ResourceHandlerRequest request = ResourceHandlerRequest.builder().desiredResourceState(model).build(); assertThrows(CfnNotFoundException.class, () -> {handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger);}); verify(proxyClient.client()).getTopicAttributes(any(GetTopicAttributesRequest.class)); } }