package software.amazon.iot.resourcespecificlogging; import software.amazon.awssdk.services.iot.model.InternalFailureException; import software.amazon.awssdk.services.iot.model.InvalidRequestException; import software.amazon.awssdk.services.iot.model.LimitExceededException; import software.amazon.awssdk.services.iot.model.ServiceUnavailableException; import software.amazon.awssdk.services.iot.model.ThrottlingException; import software.amazon.awssdk.services.iot.model.UnauthorizedException; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.HandlerErrorCode; import software.amazon.cloudformation.proxy.Logger; import software.amazon.cloudformation.proxy.OperationStatus; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) public class ReadHandlerTest extends HandlerTestBase { private ReadHandler handler; @BeforeEach public void setup() { proxy = mock(AmazonWebServicesClientProxy.class); logger = mock(Logger.class); handler = new ReadHandler(); } @Test public void handleRequest_SimpleSuccess() { ResourceModel model = createDefaultModelWithTargetId(); ResourceHandlerRequest request = createCfnRequest(model); when(proxy.injectCredentialsAndInvokeV2(eq(LIST_REQUEST), any())) .thenReturn(LIST_RESPONSE); final ProgressEvent response = handler.handleRequest(proxy, request, null, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); assertThat(response.getResourceModel()).isEqualTo(model); } @Test public void handleTargetLoggingResourceNotFound() { ResourceModel model = createDefaultModelWithTargetId(); ResourceHandlerRequest request = createCfnRequest(model); when(proxy.injectCredentialsAndInvokeV2(eq(LIST_REQUEST), any())) .thenReturn(LIST_EMPTY_RESPONSE); ProgressEvent response = handler.handleRequest(proxy, request, null, logger); ProgressEvent expectedResult = ProgressEvent.failed( model, null, HandlerErrorCode.NotFound, "The logLevel for this target doesn't exist."); assertThat(response).isEqualTo(expectedResult); } @Test public void handleRequest_InvalidRequestException() { ResourceHandlerRequest cfnRequest = createCfnRequest(createDefaultModelWithTargetId()); doThrow(InvalidRequestException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent progressEvent = handler.handleRequest(proxy, cfnRequest, null, logger); assertThat(progressEvent.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(progressEvent.getErrorCode()).isEqualTo(HandlerErrorCode.InvalidRequest); } @Test public void handleRequest_UnauthorizedException() { ResourceHandlerRequest cfnRequest = createCfnRequest(createDefaultModelWithTargetId()); doThrow(UnauthorizedException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent progressEvent = handler.handleRequest(proxy, cfnRequest, null, logger); assertThat(progressEvent.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(progressEvent.getErrorCode()).isEqualTo(HandlerErrorCode.AccessDenied); } @Test public void handleRequest_InternalFailureException() { ResourceHandlerRequest cfnRequest = createCfnRequest(createDefaultModelWithTargetId()); doThrow(InternalFailureException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent progressEvent = handler.handleRequest(proxy, cfnRequest, null, logger); assertThat(progressEvent.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(progressEvent.getErrorCode()).isEqualTo(HandlerErrorCode.InternalFailure); } @Test public void handleRequest_ThrottlingException() { ResourceHandlerRequest cfnRequest = createCfnRequest(createDefaultModelWithTargetId()); doThrow(ThrottlingException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent progressEvent = handler.handleRequest(proxy, cfnRequest, null, logger); assertThat(progressEvent.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(progressEvent.getErrorCode()).isEqualTo(HandlerErrorCode.Throttling); } @Test public void handleRequest_LimitExceededException() { ResourceHandlerRequest cfnRequest = createCfnRequest(createDefaultModelWithTargetId()); doThrow(LimitExceededException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent progressEvent = handler.handleRequest(proxy, cfnRequest, null, logger); assertThat(progressEvent.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(progressEvent.getErrorCode()).isEqualTo(HandlerErrorCode.ServiceLimitExceeded); } @Test public void handleRequest_ServiceUnavailableException() { ResourceHandlerRequest cfnRequest = createCfnRequest(createDefaultModelWithTargetId()); doThrow(ServiceUnavailableException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent progressEvent = handler.handleRequest(proxy, cfnRequest, null, logger); assertThat(progressEvent.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(progressEvent.getErrorCode()).isEqualTo(HandlerErrorCode.GeneralServiceException); } }