package software.amazon.iot.thing; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import software.amazon.awssdk.services.iot.model.DescribeThingRequest; import software.amazon.awssdk.services.iot.model.DescribeThingResponse; import software.amazon.awssdk.services.iot.model.InternalFailureException; import software.amazon.awssdk.services.iot.model.InvalidRequestException; import software.amazon.awssdk.services.iot.model.ResourceNotFoundException; 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.CfnGeneralServiceException; import software.amazon.cloudformation.exceptions.CfnInternalFailureException; import software.amazon.cloudformation.exceptions.CfnInvalidRequestException; import software.amazon.cloudformation.exceptions.CfnNotFoundException; import software.amazon.cloudformation.exceptions.CfnThrottlingException; import software.amazon.cloudformation.proxy.OperationStatus; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; 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.verify; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) public class ReadHandlerTest extends AbstractTestBase { ReadHandler handler= new ReadHandler(); @Test public void handleRequest_SimpleSuccess() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); ResourceModel expectedModel = ResourceModel.builder() .thingName(T_Name) .id(T_ID) .arn(T_ARN) .attributePayload(AttributePayload.builder().build()) .build(); when(iotClient.describeThing(any(DescribeThingRequest.class))) .thenReturn(DescribeThingResponse.builder() .thingArn(T_ARN) .thingId(T_ID) .thingName(T_Name) .build()); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, LOGGER); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModel()).isEqualTo(expectedModel); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); } @Test public void handleRequest_InternalFailureException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.describeThing(any(DescribeThingRequest.class))) .thenThrow(InternalFailureException.builder().build()); assertThrows(CfnInternalFailureException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).describeThing(any(DescribeThingRequest.class)); } @Test public void handleRequest_InvalidRequestException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.describeThing(any(DescribeThingRequest.class))) .thenThrow(InvalidRequestException.builder().build()); assertThrows(CfnInvalidRequestException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).describeThing(any(DescribeThingRequest.class)); } @Test public void handleRequest_ResourceNotFoundException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.describeThing(any(DescribeThingRequest.class))) .thenThrow(ResourceNotFoundException.builder().build()); assertThrows(CfnNotFoundException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).describeThing(any(DescribeThingRequest.class)); } @Test public void handleRequest_ServiceUnavailableException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.describeThing(any(DescribeThingRequest.class))) .thenThrow(ServiceUnavailableException.builder().build()); assertThrows(CfnGeneralServiceException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).describeThing(any(DescribeThingRequest.class)); } @Test public void handleRequest_ThrottlingException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.describeThing(any(DescribeThingRequest.class))) .thenThrow(ThrottlingException.builder().build()); assertThrows(CfnThrottlingException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).describeThing(any(DescribeThingRequest.class)); } @Test public void handleRequest_UnauthorizedException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.describeThing(any(DescribeThingRequest.class))) .thenThrow(UnauthorizedException.builder().build()); assertThrows(CfnAccessDeniedException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).describeThing(any(DescribeThingRequest.class)); } }