use of com.amazonaws.services.s3.model.VersionListing 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.VersionListing 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);
}
}
use of com.amazonaws.services.s3.model.VersionListing in project aws-doc-sdk-examples by awsdocs.
the class ListKeysVersioningEnabledBucket method main.
public static void main(String[] args) {
Regions clientRegion = Regions.DEFAULT_REGION;
String bucketName = "*** Bucket name ***";
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
// Retrieve the list of versions. If the bucket contains more versions
// than the specified maximum number of results, Amazon S3 returns
// one page of results per request.
ListVersionsRequest request = new ListVersionsRequest().withBucketName(bucketName).withMaxResults(2);
VersionListing versionListing = s3Client.listVersions(request);
int numVersions = 0, numPages = 0;
while (true) {
numPages++;
for (S3VersionSummary objectSummary : versionListing.getVersionSummaries()) {
System.out.printf("Retrieved object %s, version %s\n", objectSummary.getKey(), objectSummary.getVersionId());
numVersions++;
}
// there are, retrieve them. Otherwise, exit the loop.
if (versionListing.isTruncated()) {
versionListing = s3Client.listNextBatchOfVersions(versionListing);
} else {
break;
}
}
System.out.println(numVersions + " object versions retrieved in " + numPages + " pages");
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
} catch (SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
}
Aggregations