Search in sources :

Example 6 with S3VersionSummary

use of com.amazonaws.services.s3.model.S3VersionSummary in project herd by FINRAOS.

the class S3DaoImplTest method testDeleteDirectoryMultiObjectDeleteException.

@Test
public void testDeleteDirectoryMultiObjectDeleteException() {
    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);
    // Create a retry policy.
    RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);
    // Create an S3 version summary.
    S3VersionSummary s3VersionSummary = new S3VersionSummary();
    s3VersionSummary.setKey(S3_KEY);
    s3VersionSummary.setVersionId(S3_VERSION_ID);
    // Create a version listing.
    VersionListing versionListing = new VersionListing();
    versionListing.setVersionSummaries(Arrays.asList(s3VersionSummary));
    // Create a delete error.
    MultiObjectDeleteException.DeleteError deleteError = new MultiObjectDeleteException.DeleteError();
    deleteError.setKey(S3_KEY);
    deleteError.setVersionId(S3_VERSION_ID);
    deleteError.setCode(ERROR_CODE);
    deleteError.setMessage(ERROR_MESSAGE);
    // Create a multi object delete exception.
    MultiObjectDeleteException multiObjectDeleteException = new MultiObjectDeleteException(Arrays.asList(deleteError), new ArrayList<>());
    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class))).thenReturn(versionListing);
    when(s3Operations.deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class))).thenThrow(multiObjectDeleteException);
    // Try to call the method under test.
    try {
        s3DaoImpl.deleteDirectory(s3FileTransferRequestParamsDto);
    } catch (IllegalStateException e) {
        assertEquals(String.format("Failed to delete keys/key versions with prefix \"%s\" from bucket \"%s\". " + "Reason: One or more objects could not be deleted (Service: null; Status Code: 0; Error Code: null; Request ID: null; S3 Extended Request ID: null)", S3_KEY_PREFIX, S3_BUCKET_NAME), e.getMessage());
    }
    // Verify the external calls.
    verify(retryPolicyFactory, times(2)).getRetryPolicy();
    verify(s3Operations).listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class));
    verify(s3Operations).deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) VersionListing(com.amazonaws.services.s3.model.VersionListing) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest) RetryPolicy(com.amazonaws.retry.RetryPolicy) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 7 with S3VersionSummary

use of com.amazonaws.services.s3.model.S3VersionSummary in project herd by FINRAOS.

the class S3DaoImpl method listVersions.

@Override
public List<DeleteObjectsRequest.KeyVersion> listVersions(final S3FileTransferRequestParamsDto params) {
    Assert.isTrue(!isRootKeyPrefix(params.getS3KeyPrefix()), "Listing of S3 key versions from root directory is not allowed.");
    AmazonS3Client s3Client = getAmazonS3(params);
    List<DeleteObjectsRequest.KeyVersion> keyVersions = new ArrayList<>();
    try {
        ListVersionsRequest listVersionsRequest = new ListVersionsRequest().withBucketName(params.getS3BucketName()).withPrefix(params.getS3KeyPrefix());
        VersionListing versionListing;
        do {
            versionListing = s3Operations.listVersions(listVersionsRequest, s3Client);
            for (S3VersionSummary versionSummary : versionListing.getVersionSummaries()) {
                keyVersions.add(new DeleteObjectsRequest.KeyVersion(versionSummary.getKey(), versionSummary.getVersionId()));
            }
            listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker());
            listVersionsRequest.setVersionIdMarker(versionListing.getNextVersionIdMarker());
        } while (versionListing.isTruncated());
    } catch (AmazonS3Exception amazonS3Exception) {
        if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(amazonS3Exception.getErrorCode())) {
            throw new IllegalArgumentException("The specified bucket '" + params.getS3BucketName() + "' does not exist.", amazonS3Exception);
        }
        throw new IllegalStateException("Error accessing S3", amazonS3Exception);
    } catch (AmazonClientException e) {
        throw new IllegalStateException(String.format("Failed to list keys/key versions with prefix \"%s\" from bucket \"%s\". Reason: %s", params.getS3KeyPrefix(), params.getS3BucketName(), e.getMessage()), e);
    } finally {
        // Shutdown the AmazonS3Client instance to release resources.
        s3Client.shutdown();
    }
    return keyVersions;
}
Also used : VersionListing(com.amazonaws.services.s3.model.VersionListing) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest)

Example 8 with S3VersionSummary

use of com.amazonaws.services.s3.model.S3VersionSummary in project herd by FINRAOS.

the class S3DaoTest method testDeleteDirectoryAssertMultiObjectDeleteExceptionLogged.

/**
 * Asserts that when delete directory is called but S3 throws MultiObjectDeleteException, the exception is logged properly.
 */
@Test
// TODO: Log4J2 - This test works within an IDE, but not from Maven. We need to figure out why.
@Ignore
public void testDeleteDirectoryAssertMultiObjectDeleteExceptionLogged() throws Exception {
    // Inject mock
    S3Operations mockS3Operations = mock(S3Operations.class);
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
    // Override logger with my own appender to inspect the output
    String loggerName = S3DaoImpl.class.getName();
    LogLevel originalLoggerLevel = getLogLevel(loggerName);
    setLogLevel(loggerName, LogLevel.ERROR);
    String appenderName = "TestWriterAppender";
    StringWriter stringWriter = addLoggingWriterAppender(appenderName);
    try {
        // Set up mocked behavior
        // Return a list of mock version listing
        VersionListing versionListing = new VersionListing();
        S3VersionSummary s3VersionSummary = new S3VersionSummary();
        s3VersionSummary.setKey("s3VersionSummaryKey");
        s3VersionSummary.setVersionId("s3VersionSummaryVersionId");
        versionListing.setVersionSummaries(Arrays.asList(s3VersionSummary));
        when(mockS3Operations.listVersions(any(), any())).thenReturn(versionListing);
        // Have mock implementation throw exception
        List<DeleteError> errors = new ArrayList<>();
        {
            DeleteError deleteError = new DeleteError();
            deleteError.setCode("deleteError1Code");
            deleteError.setKey("deleteError1Key");
            deleteError.setMessage("deleteError1Message");
            deleteError.setVersionId("deleteError1VersionId");
            errors.add(deleteError);
        }
        {
            DeleteError deleteError = new DeleteError();
            deleteError.setCode("deleteError2Code");
            deleteError.setKey("deleteError2Key");
            deleteError.setMessage("deleteError2Message");
            deleteError.setVersionId("deleteError2VersionId");
            errors.add(deleteError);
        }
        List<DeletedObject> deletedObjects = new ArrayList<>();
        MultiObjectDeleteException multiObjectDeleteException = new MultiObjectDeleteException(errors, deletedObjects);
        when(mockS3Operations.deleteObjects(any(), any())).thenThrow(multiObjectDeleteException);
        // try the operation and catch exception
        try {
            S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
            s3FileTransferRequestParamsDto.setS3KeyPrefix("/test/prefix");
            s3Dao.deleteDirectory(s3FileTransferRequestParamsDto);
            fail();
        } catch (Exception e) {
            // Inspect and assert exception
            assertEquals(IllegalStateException.class, e.getClass());
            assertEquals(multiObjectDeleteException, e.getCause());
        }
        assertEquals(String.format("Error deleting multiple objects. Below are the list of objects which failed to delete.%n" + "s3Key=\"deleteError1Key\" s3VersionId=\"deleteError1VersionId\" " + "s3DeleteErrorCode=\"deleteError1Code\" s3DeleteErrorMessage=\"deleteError1Message\"%n" + "s3Key=\"deleteError2Key\" s3VersionId=\"deleteError2VersionId\" " + "s3DeleteErrorCode=\"deleteError2Code\" s3DeleteErrorMessage=\"deleteError2Message\"%n%n"), stringWriter.toString());
    } finally {
        // Restore original resources
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
        setLogLevel(loggerName, originalLoggerLevel);
        removeLoggingAppender(appenderName);
    }
}
Also used : DeleteError(com.amazonaws.services.s3.model.MultiObjectDeleteException.DeleteError) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) VersionListing(com.amazonaws.services.s3.model.VersionListing) ArrayList(java.util.ArrayList) LogLevel(org.finra.herd.core.helper.LogLevel) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) StringWriter(java.io.StringWriter) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) DeletedObject(com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with S3VersionSummary

use of com.amazonaws.services.s3.model.S3VersionSummary in project herd by FINRAOS.

the class S3DaoTest method testListVersionsAssertKeyAndVersionIdMarker.

@Test
public void testListVersionsAssertKeyAndVersionIdMarker() {
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    S3Operations mockS3Operations = mock(S3Operations.class);
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
    try {
        String s3BucketName = "s3BucketName";
        String s3KeyPrefix = "s3KeyPrefix";
        String expectedKey = "key";
        String expectedVersionId = "versionId";
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
        s3FileTransferRequestParamsDto.setS3BucketName(s3BucketName);
        s3FileTransferRequestParamsDto.setS3KeyPrefix(s3KeyPrefix);
        when(mockS3Operations.listVersions(any(), any())).then(new Answer<VersionListing>() {

            @Override
            public VersionListing answer(InvocationOnMock invocation) throws Throwable {
                ListVersionsRequest listVersionsRequest = invocation.getArgument(0);
                String keyMarker = listVersionsRequest.getKeyMarker();
                String versionIdMarker = listVersionsRequest.getVersionIdMarker();
                VersionListing versionListing = new VersionListing();
                if (keyMarker == null || versionIdMarker == null) {
                    versionListing.setTruncated(true);
                    versionListing.setNextKeyMarker("nextKeyMarker");
                    versionListing.setNextVersionIdMarker("nextVersionIdMarker");
                } else {
                    assertEquals("nextKeyMarker", listVersionsRequest.getKeyMarker());
                    assertEquals("nextVersionIdMarker", listVersionsRequest.getVersionIdMarker());
                    S3VersionSummary s3VersionSummary = new S3VersionSummary();
                    s3VersionSummary.setKey(expectedKey);
                    s3VersionSummary.setVersionId(expectedVersionId);
                    versionListing.getVersionSummaries().add(s3VersionSummary);
                }
                return versionListing;
            }
        });
        List<KeyVersion> listVersions = s3Dao.listVersions(s3FileTransferRequestParamsDto);
        assertEquals(1, listVersions.size());
        assertEquals(expectedKey, listVersions.get(0).getKey());
        assertEquals(expectedVersionId, listVersions.get(0).getVersion());
    } finally {
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
    }
}
Also used : S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) VersionListing(com.amazonaws.services.s3.model.VersionListing) InvocationOnMock(org.mockito.invocation.InvocationOnMock) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) KeyVersion(com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest) Test(org.junit.Test)

Aggregations

S3VersionSummary (com.amazonaws.services.s3.model.S3VersionSummary)9 VersionListing (com.amazonaws.services.s3.model.VersionListing)9 ListVersionsRequest (com.amazonaws.services.s3.model.ListVersionsRequest)5 Test (org.junit.Test)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)4 AmazonClientException (com.amazonaws.AmazonClientException)3 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)3 IOException (java.io.IOException)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)2 ArrayList (java.util.ArrayList)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2 RetryPolicy (com.amazonaws.retry.RetryPolicy)1 KeyVersion (com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion)1 DeletedObject (com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject)1 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)1 DeleteError (com.amazonaws.services.s3.model.MultiObjectDeleteException.DeleteError)1