package software.amazon.ecr.registrypolicy; import java.time.Duration; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; import software.amazon.awssdk.services.ecr.EcrClient; import software.amazon.awssdk.services.ecr.model.DeleteRegistryPolicyRequest; import software.amazon.awssdk.services.ecr.model.DeleteRegistryPolicyResponse; import software.amazon.awssdk.services.ecr.model.GetRegistryPolicyRequest; import software.amazon.awssdk.services.ecr.model.GetRegistryPolicyResponse; import software.amazon.awssdk.services.ecr.model.InvalidParameterException; import software.amazon.awssdk.services.ecr.model.RegistryPolicyNotFoundException; import software.amazon.awssdk.services.ecr.model.ServerException; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.HandlerErrorCode; import software.amazon.cloudformation.proxy.OperationStatus; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ProxyClient; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import org.junit.jupiter.api.AfterEach; 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.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) class DeleteHandlerTest extends AbstractTestBase { @Mock private EcrClient ecrMock; @Mock private ProxyClient proxyClientMock; private AmazonWebServicesClientProxy proxy; private DeleteHandler handler; @BeforeEach public void setup() { proxy = new AmazonWebServicesClientProxy(logger, MOCK_CREDENTIALS, () -> Duration.ofSeconds(600).toMillis()); handler = new DeleteHandler(); when(proxyClientMock.client()).thenReturn(ecrMock); when(ecrMock.getRegistryPolicy(any(GetRegistryPolicyRequest.class))) .thenReturn(GetRegistryPolicyResponse.builder() .registryId(TEST_REGISTRY_ID) .policyText(REGISTRY_POLICY_OUTPUT_TEXT) .build()); when(ecrMock.deleteRegistryPolicy(any(DeleteRegistryPolicyRequest.class))).thenReturn( DeleteRegistryPolicyResponse.builder().build()); } @AfterEach public void tear_down() { verify(ecrMock, atLeastOnce()).serviceName(); } @Test void handleRequest_SimpleSuccess() { final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClientMock, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackDelaySeconds()).isZero(); assertThat(response.getResourceModel()).isNull(); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); } @Test void handleRequest_InvalidParameterException() { when(ecrMock.deleteRegistryPolicy(any(DeleteRegistryPolicyRequest.class))) .thenThrow(InvalidParameterException.builder().message("testInvalidException").build()); final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClientMock, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(response.isSuccess()).isFalse(); assertThat(response.getCallbackDelaySeconds()).isZero(); assertThat(response.getResourceModel()).isNotNull(); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).startsWith("testInvalidException"); assertThat(response.getErrorCode()).isEqualTo(HandlerErrorCode.InvalidRequest); } @Test void handleRequest_PolicyNotFoundException() { when(ecrMock.deleteRegistryPolicy(any(DeleteRegistryPolicyRequest.class))) .thenThrow(RegistryPolicyNotFoundException.builder().message("testRegistryNotFound").build()); final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClientMock, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(response.isSuccess()).isFalse(); assertThat(response.getCallbackDelaySeconds()).isZero(); assertThat(response.getResourceModel()).isNotNull(); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).startsWith("testRegistryNotFound"); assertThat(response.getErrorCode()).isEqualTo(HandlerErrorCode.NotFound); } @Test void handleRequest_ServerException() { when(ecrMock.deleteRegistryPolicy(any(DeleteRegistryPolicyRequest.class))) .thenThrow(ServerException.builder().message("testServerException").build()); final ResourceModel model = ResourceModel.builder().build(); final ResourceHandlerRequest request = ResourceHandlerRequest.builder() .desiredResourceState(model) .build(); final ProgressEvent response = handler.handleRequest(proxy, request, new CallbackContext(), proxyClientMock, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.FAILED); assertThat(response.isSuccess()).isFalse(); assertThat(response.getCallbackDelaySeconds()).isZero(); assertThat(response.getResourceModel()).isNotNull(); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).startsWith("testServerException"); assertThat(response.getErrorCode()).isEqualTo(HandlerErrorCode.ServiceInternalError); } }