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.cloudformation.model.OperationStatus; 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.awssdk.services.iot.model.UpdateThingRequest; import software.amazon.awssdk.services.iot.model.UpdateThingResponse; import software.amazon.awssdk.services.iot.model.VersionConflictException; 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.CfnResourceConflictException; import software.amazon.cloudformation.exceptions.CfnThrottlingException; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import java.util.HashMap; import java.util.Map; 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 UpdateHandlerTest extends AbstractTestBase { UpdateHandler handler = new UpdateHandler(); @Test public void handleRequest_AddAttributes() { Map attributes = new HashMap<>(); attributes.put("attr1","val1"); attributes.put("attr2","val2"); final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .attributePayload(AttributePayload.builder().attributes(attributes).build()) .build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenReturn(UpdateThingResponse.builder() .build()); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClient, LOGGER); assertThat(response).isNotNull(); assertThat(response.getStatus().toString()).isEqualTo(OperationStatus.SUCCESS.toString()); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModel()).isEqualTo(request.getDesiredResourceState()); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); assertThat(response.getResourceModel().getAttributePayload().getAttributes()).isEqualTo(attributes); } @Test public void handleRequest_InternalFailureException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenThrow(InternalFailureException.builder().build()); assertThrows(CfnInternalFailureException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).updateThing(any(UpdateThingRequest.class)); } @Test public void handleRequest_InvalidRequestException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenThrow(InvalidRequestException.builder().build()); assertThrows(CfnInvalidRequestException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).updateThing(any(UpdateThingRequest.class)); } @Test public void handleRequest_ResourceNotFoundException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenThrow(ResourceNotFoundException.builder().build()); assertThrows(CfnNotFoundException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).updateThing(any(UpdateThingRequest.class)); } @Test public void handleRequest_ServiceUnavailableException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenThrow(ServiceUnavailableException.builder().build()); assertThrows(CfnGeneralServiceException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).updateThing(any(UpdateThingRequest.class)); } @Test public void handleRequest_ThrottlingException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenThrow(ThrottlingException.builder().build()); assertThrows(CfnThrottlingException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).updateThing(any(UpdateThingRequest.class)); } @Test public void handleRequest_UnauthorizedException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenThrow(UnauthorizedException.builder().build()); assertThrows(CfnAccessDeniedException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).updateThing(any(UpdateThingRequest.class)); } @Test public void handleRequest_VersionConflictException() { final ResourceModel model = ResourceModel.builder() .thingName(T_Name) .build(); final ResourceHandlerRequest request = defaultRequestBuilder(model).build(); when(iotClient.updateThing(any(UpdateThingRequest.class))) .thenThrow(VersionConflictException.builder().build()); assertThrows(CfnResourceConflictException.class, () -> handler.handleRequest(proxy, request, new CallbackContext(),proxyClient,LOGGER)); verify(iotClient).updateThing(any(UpdateThingRequest.class)); } }