package software.amazon.devopsguru.notificationchannel; import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import software.amazon.awssdk.services.devopsguru.DevOpsGuruClient; import software.amazon.awssdk.services.devopsguru.model.AccessDeniedException; import software.amazon.awssdk.services.devopsguru.model.AddNotificationChannelRequest; import software.amazon.awssdk.services.devopsguru.model.AddNotificationChannelResponse; import software.amazon.awssdk.services.devopsguru.model.InternalServerException; import software.amazon.awssdk.services.devopsguru.model.ListNotificationChannelsRequest; import software.amazon.awssdk.services.devopsguru.model.ListNotificationChannelsResponse; import software.amazon.awssdk.services.devopsguru.model.NotificationChannel; import software.amazon.awssdk.services.devopsguru.model.ResourceNotFoundException; import software.amazon.awssdk.services.devopsguru.model.ServiceQuotaExceededException; import software.amazon.awssdk.services.devopsguru.model.ThrottlingException; import software.amazon.awssdk.services.devopsguru.model.ValidationException; import software.amazon.cloudformation.exceptions.CfnAccessDeniedException; import software.amazon.cloudformation.exceptions.CfnAlreadyExistsException; import software.amazon.cloudformation.exceptions.CfnInternalFailureException; import software.amazon.cloudformation.exceptions.CfnInvalidRequestException; import software.amazon.cloudformation.exceptions.CfnNotFoundException; import software.amazon.cloudformation.exceptions.CfnServiceInternalErrorException; import software.amazon.cloudformation.exceptions.CfnServiceLimitExceededException; import software.amazon.cloudformation.exceptions.CfnThrottlingException; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ProxyClient; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import java.time.Duration; import java.util.Arrays; import java.util.List; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) public class CreateHandlerTest extends AbstractTestBase { @Mock private AmazonWebServicesClientProxy proxy; @Mock private ProxyClient proxyClient; @Mock DevOpsGuruClient sdkClient; private CreateHandler handler; @BeforeEach public void setup() { handler = new CreateHandler(); proxy = new AmazonWebServicesClientProxy(logger, MOCK_CREDENTIALS, () -> Duration.ofSeconds(600).toMillis()); sdkClient = mock(DevOpsGuruClient.class); proxyClient = MOCK_PROXY(proxy, sdkClient); } @ParameterizedTest(name = "run #{index} with [{arguments}]") @MethodSource("validationNotificationChannelWithResourceModelParams") public void handleRequest_successful(final Pair, ResourceModel> mappingPair) { final NotificationChannel secondChannel = mappingPair.getKey().get(1); final ResourceModel model = mappingPair.getValue(); final List otherChannels = Arrays.asList(secondChannel); final ListNotificationChannelsResponse listNotificationChannelsResponse = ListNotificationChannelsResponse.builder().channels(otherChannels).build(); when(proxyClient.client().listNotificationChannels(any(ListNotificationChannelsRequest.class))).thenReturn(listNotificationChannelsResponse); final AddNotificationChannelResponse addNotificationChannelResponse = AddNotificationChannelResponse.builder().id(id).build(); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenReturn(addNotificationChannelResponse); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger); assertResponse(response, model); } @Test public void handleRequest_checkForPreCreateResourceExistenceThrowCfnAlreadyExistsException() { final NotificationChannel channel = constructNotificationChannel(topicArn, id, insightSeveritiesFilter, messageTypesFilter); final List channels = Arrays.asList(channel); final ResourceModel model = constructResourceModel(topicArn, id, insightSeveritiesFilter, messageTypesFilter); final ListNotificationChannelsResponse listNotificationChannelsResponse = ListNotificationChannelsResponse.builder().channels(channels).build(); when(proxyClient.client().listNotificationChannels(any(ListNotificationChannelsRequest.class))).thenReturn(listNotificationChannelsResponse); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); assertThatExceptionOfType(CfnAlreadyExistsException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); } @Test public void handleRequest_exceptions(){ final NotificationChannel secondChannel = constructNotificationChannel(topicArn1, id1, insightSeveritiesFilter, messageTypesFilter); final ResourceModel model = constructResourceModel(topicArn, id, insightSeveritiesFilter, messageTypesFilter); final List channels = Arrays.asList(secondChannel); final ListNotificationChannelsResponse listNotificationChannelsResponse = ListNotificationChannelsResponse.builder().channels(channels).build(); when(proxyClient.client().listNotificationChannels(any(ListNotificationChannelsRequest.class))).thenReturn(listNotificationChannelsResponse); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenThrow(AccessDeniedException.class); assertThatExceptionOfType(CfnAccessDeniedException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenThrow(InternalServerException.class); assertThatExceptionOfType(CfnServiceInternalErrorException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenThrow(ResourceNotFoundException.class); assertThatExceptionOfType(CfnNotFoundException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenThrow(ValidationException.class); assertThatExceptionOfType(CfnInvalidRequestException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenThrow(ThrottlingException.class); assertThatExceptionOfType(CfnThrottlingException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenThrow(ServiceQuotaExceededException.class); assertThatExceptionOfType(CfnServiceLimitExceededException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); when(proxyClient.client().addNotificationChannel(any(AddNotificationChannelRequest.class))).thenThrow(RuntimeException.class); assertThatExceptionOfType(CfnInternalFailureException.class).isThrownBy(() -> handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, logger)); } }