package software.amazon.datasync.task; import software.amazon.awssdk.services.datasync.model.*; import software.amazon.cloudformation.exceptions.CfnGeneralServiceException; import software.amazon.cloudformation.exceptions.CfnNotFoundException; import software.amazon.cloudformation.exceptions.CfnServiceInternalErrorException; 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.Mock; import org.mockito.junit.jupiter.MockitoExtension; 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 ReadHandlerTest { @Mock private AmazonWebServicesClientProxy proxy; @Mock private Logger logger; @BeforeEach public void setup() { proxy = mock(AmazonWebServicesClientProxy.class); logger = mock(Logger.class); } @Test public void handleRequest_SimpleSuccess() { final ReadHandler handler = new ReadHandler(); final DescribeTaskResponse describeTaskResponse = buildDefaultResponse(); final ListTagsForResourceResponse listTagsForResourceResponse = TagTestResources.buildDefaultTagsResponse(); doReturn(describeTaskResponse) .when(proxy) .injectCredentialsAndInvokeV2( any(DescribeTaskRequest.class), any() ); doReturn(listTagsForResourceResponse) .when(proxy) .injectCredentialsAndInvokeV2( any(ListTagsForResourceRequest.class), any() ); final ResourceModel model = buildDefaultModel(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); 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(); } @Test public void handleRequest_FailureNotFoundRequest() { final ReadHandler handler = new ReadHandler(); doThrow(InvalidRequestException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(DescribeTaskRequest.class), any()); final ResourceModel model = buildDefaultModel(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); assertThrows(CfnNotFoundException.class, () -> { handler.handleRequest(proxy, request, null, logger); } ); } @Test public void handleRequest_FailureInternalException() { final ReadHandler handler = new ReadHandler(); doThrow(InternalException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(DescribeTaskRequest.class), any()); final ResourceModel model = buildDefaultModel(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); assertThrows(CfnServiceInternalErrorException.class, () -> { handler.handleRequest(proxy, request, null, logger); } ); } @Test public void handleRequest_FailureDataSyncException() { final ReadHandler handler = new ReadHandler(); doThrow(DataSyncException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(DescribeTaskRequest.class), any()); final ResourceModel model = buildDefaultModel(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); assertThrows(CfnGeneralServiceException.class, () -> { handler.handleRequest(proxy, request, null, logger); } ); } private static DescribeTaskResponse buildDefaultResponse() { final String taskArn = "arn:aws:datasync:us-east-2:123456789012:task/task-01234567890123456"; final String destinationLocationArn = "arn:aws:datasync:us-east-1:123456789012:location/loc-12345678901234567"; final String sourceLocationArn = "arn:aws:datasync:us-east-1:123456789012:location/loc-12345678901234567"; return DescribeTaskResponse.builder() .taskArn(taskArn) .destinationLocationArn(destinationLocationArn) .sourceLocationArn(sourceLocationArn) .build(); } private static ResourceModel buildDefaultModel() { final String taskArn = "arn:aws:datasync:us-east-2:123456789012:task/task-01234567890123456"; return ResourceModel.builder() .taskArn(taskArn) .tags(TagTestResources.defaultTags) .build(); } }