Search in sources :

Example 11 with VersionListing

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);
    }
}
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 12 with VersionListing

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

Example 13 with VersionListing

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();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) VersionListing(com.amazonaws.services.s3.model.VersionListing) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest)

Aggregations

VersionListing (com.amazonaws.services.s3.model.VersionListing)11 S3VersionSummary (com.amazonaws.services.s3.model.S3VersionSummary)9 AmazonServiceException (com.amazonaws.AmazonServiceException)7 ListVersionsRequest (com.amazonaws.services.s3.model.ListVersionsRequest)7 Test (org.junit.Test)6 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)5 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 AmazonClientException (com.amazonaws.AmazonClientException)3 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)3 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)3 IOException (java.io.IOException)3 SdkClientException (com.amazonaws.SdkClientException)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 Regions (com.amazonaws.regions.Regions)2 RetryPolicy (com.amazonaws.retry.RetryPolicy)2 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)2 ArrayList (java.util.ArrayList)2 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2