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();
}
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;
}
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);
}
}
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);
}
}
Aggregations