package software.amazon.iot.jobtemplate; import org.junit.jupiter.api.Assertions; import org.mockito.Mockito; import software.amazon.awssdk.services.iot.model.ConflictException; import software.amazon.awssdk.services.iot.model.CreateJobTemplateRequest; import software.amazon.awssdk.services.iot.model.CreateJobTemplateResponse; 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.exceptions.CfnAccessDeniedException; import software.amazon.cloudformation.exceptions.CfnAlreadyExistsException; import software.amazon.cloudformation.exceptions.CfnGeneralServiceException; import software.amazon.cloudformation.exceptions.CfnInternalFailureException; import software.amazon.cloudformation.exceptions.CfnInvalidRequestException; 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.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.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.ArgumentMatchers.any; @ExtendWith(MockitoExtension.class) public class CreateHandlerTest extends HandlerTestBase{ private CreateHandler handler; @BeforeEach public void setup() { proxy = mock(AmazonWebServicesClientProxy.class); logger = mock(Logger.class); handler = new CreateHandler(); } @Test public void handleRequest_SimpleSuccess() { final ResourceModel model = ResourceModel.builder() .description(JOB_TEMPLATE_DESCRIPTION) .build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); final CreateJobTemplateResponse expectedResponse = getCreateResponse(); final ResourceModel expectedModel = ResourceModel.builder() .arn(JOB_TEMPLATE_ARN) .jobTemplateId(JOB_TEMPLATE_ID) .description(JOB_TEMPLATE_DESCRIPTION) .build(); Mockito.when(proxy.injectCredentialsAndInvokeV2(any(CreateJobTemplateRequest.class),any())).thenReturn(expectedResponse); 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(expectedModel); } @Test public void handleRequest_Complex() { final ResourceModel model = ResourceModel.builder() .description(JOB_TEMPLATE_DESCRIPTION) .abortConfig(Translator.getAbortConfig(getAbortConfig())) .jobExecutionsRolloutConfig(Translator.getJobExecutionsRolloutConfig(getJobExecutionsRolloutConfig())) .presignedUrlConfig(Translator.getPresignedUrlConfig(getPresignedUrlConfig())) .timeoutConfig(Translator.getTimeoutConfig(getTimeoutConfig())) .jobExecutionsRetryConfig(Translator.getRetryConfig(getRetryConfig())) .build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); final CreateJobTemplateResponse expectedResponse = getCreateResponse(); final ResourceModel expectedModel = ResourceModel.builder() .jobTemplateId(JOB_TEMPLATE_ID) .arn(JOB_TEMPLATE_ARN) .description(JOB_TEMPLATE_DESCRIPTION) .abortConfig(model.getAbortConfig()) .jobExecutionsRolloutConfig(model.getJobExecutionsRolloutConfig()) .presignedUrlConfig(model.getPresignedUrlConfig()) .timeoutConfig(model.getTimeoutConfig()) .jobExecutionsRetryConfig(model.getJobExecutionsRetryConfig()) .build(); Mockito.when(proxy.injectCredentialsAndInvokeV2(any(CreateJobTemplateRequest.class),any())).thenReturn(expectedResponse); 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(expectedModel); } @Test public void handleRequest_ConflictException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(ConflictException.builder().build()) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnAlreadyExistsException.class, () -> handler.handleRequest(proxy, request, null, logger)); } @Test public void handleRequest_InvalidRequestException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(InvalidRequestException.builder().build()) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnInvalidRequestException.class, () -> handler.handleRequest(proxy, request, null, logger)); } @Test public void handleRequest_LimitExceededException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(LimitExceededException.builder().build()) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnServiceLimitExceededException.class, () -> handler.handleRequest(proxy, request, null, logger)); } @Test public void handleRequest_InternalFailureException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(InternalFailureException.builder().build()) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnServiceInternalErrorException.class, () -> handler.handleRequest(proxy, request, null, logger)); } @Test public void handleRequest_ThrottlingException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(ThrottlingException.builder().build()) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnThrottlingException.class, () -> handler.handleRequest(proxy, request, null, logger)); } @Test public void handleRequest_ServiceUnavailableException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(ServiceUnavailableException.builder().build()) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnGeneralServiceException.class, () -> handler.handleRequest(proxy, request, null, logger)); } @Test public void handleRequest_UnauthorizedException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(UnauthorizedException.builder().build()) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnAccessDeniedException.class, () -> handler.handleRequest(proxy, request, null, logger)); } @Test public void handleRequest_UnexpectedException() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); doThrow(RuntimeException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); Assertions.assertThrows(CfnInternalFailureException.class, () -> handler.handleRequest(proxy, request, null, logger)); } }