@Mock public ObjectListing listNextBatchOfObjects(ObjectListing previousObjectListing) throws AmazonClientException { ObjectListing listing = new ObjectListing(); listing.setBucketName(previousObjectListing.getBucketName()); listing.setPrefix(previousObjectListing.getPrefix()); return new ObjectListing(); } }
@Mock public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) throws AmazonClientException { ObjectListing listing = new ObjectListing(); listing.setBucketName(listObjectsRequest.getBucketName()); listing.setPrefix(listObjectsRequest.getPrefix()); return listing; }
@Override public ObjectListing listNextBatchOfObjects(ListNextBatchOfObjectsRequest listNextBatchOfObjectsRequest) throws SdkClientException, AmazonServiceException { listNextBatchOfObjectsRequest = beforeClientExecution(listNextBatchOfObjectsRequest); rejectNull(listNextBatchOfObjectsRequest, "The request object parameter must be specified when listing the next batch of objects in a bucket"); ObjectListing previousObjectListing = listNextBatchOfObjectsRequest.getPreviousObjectListing(); if (!previousObjectListing.isTruncated()) { ObjectListing emptyListing = new ObjectListing(); emptyListing.setBucketName(previousObjectListing.getBucketName()); emptyListing.setDelimiter(previousObjectListing.getDelimiter()); emptyListing.setMarker(previousObjectListing.getNextMarker()); emptyListing.setMaxKeys(previousObjectListing.getMaxKeys()); emptyListing.setPrefix(previousObjectListing.getPrefix()); emptyListing.setEncodingType(previousObjectListing.getEncodingType()); emptyListing.setTruncated(false); return emptyListing; } return listObjects(listNextBatchOfObjectsRequest.toListObjectsRequest()); }
@Override public ObjectListing listNextBatchOfObjects( ListNextBatchOfObjectsRequest listNextBatchOfObjectsRequest) throws AmazonClientException, AmazonServiceException { final ObjectListing previousObjectListing = listNextBatchOfObjectsRequest.getPreviousObjectListing(); if (!previousObjectListing.isTruncated()) { final ObjectListing emptyListing = new ObjectListing(); emptyListing.setBucketName(previousObjectListing.getBucketName()); emptyListing.setDelimiter(previousObjectListing.getDelimiter()); emptyListing.setMarker(previousObjectListing.getNextMarker()); emptyListing.setMaxKeys(previousObjectListing.getMaxKeys()); emptyListing.setPrefix(previousObjectListing.getPrefix()); emptyListing.setEncodingType(previousObjectListing.getEncodingType()); emptyListing.setTruncated(false); return emptyListing; } return listObjects(listNextBatchOfObjectsRequest.toListObjectsRequest()); }
@Override public ObjectListing listObjects(String bucketName, String prefix) throws AmazonClientException { ObjectListing listing = new ObjectListing(); List<S3ObjectSummary> objectSummaries = listing.getObjectSummaries();
private ObjectListing s3objectList(String key, StorageClass storageClass) throws Exception { ObjectListing list = new ObjectListing(); S3ObjectSummary element = new S3ObjectSummary(); element.setKey(key); element.setStorageClass(storageClass.toString()); List<S3ObjectSummary> objectSummaries = new ArrayList<>(); objectSummaries.add(element); Field field = list.getClass().getDeclaredField("objectSummaries"); field.setAccessible(true); field.set(list, objectSummaries); return list; }
@Test public void listS3FilesByPrefix() { doReturn(new ObjectListing()).when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix(builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true); }
@Test public void listS3FileByPrefix_with_retry() { doThrow(new RuntimeException()).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(1)); }
@Test(expected = SomeException.class) public void listS3FileByPrefix_on_retry_gave_up_should_throw_the_original_exception() { doThrow(new SomeException()).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(0)); }
@Test(expected = AmazonServiceException.class) public void listS3FileByPrefix_on_retry_gave_up_should_throw_the_original_exception_in_forbidden_code() { AmazonServiceException exception = new AmazonServiceException("Forbidden exception"); exception.setStatusCode(HttpStatus.SC_FORBIDDEN); exception.setErrorType(AmazonServiceException.ErrorType.Client); doThrow(exception).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(1)); }
@Override public ObjectListing answer(InvocationOnMock invocationOnMock) throws Throwable { final ListObjectsRequest req = (ListObjectsRequest) invocationOnMock.getArguments()[0]; ObjectListing listing = new ObjectListing();
@Test(expected = AmazonServiceException.class) public void listS3FileByPrefix_on_retry_gave_up_should_throw_the_original_exception_in_methodnotallow_code() { AmazonServiceException exception = new AmazonServiceException("method not allow exception"); exception.setStatusCode(HttpStatus.SC_METHOD_NOT_ALLOWED); exception.setErrorType(AmazonServiceException.ErrorType.Client); doThrow(exception).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(1)); }
@Bean public AmazonS3 amazonS3() { AmazonS3 amazonS3 = Mockito.mock(AmazonS3.class); willAnswer(invocation -> { ObjectListing objectListing = new ObjectListing(); List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries(); for (S3Object s3Object : S3_OBJECTS) { S3ObjectSummary s3ObjectSummary = new S3ObjectSummary(); s3ObjectSummary.setBucketName(S3_BUCKET); s3ObjectSummary.setKey(s3Object.getKey()); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, 1); s3ObjectSummary.setLastModified(calendar.getTime()); objectSummaries.add(s3ObjectSummary); } return objectListing; }).given(amazonS3).listObjects(any(ListObjectsRequest.class)); for (final S3Object s3Object : S3_OBJECTS) { willAnswer(invocation -> s3Object).given(amazonS3).getObject(S3_BUCKET, s3Object.getKey()); } return amazonS3; }
@Bean public AmazonS3 amazonS3() { AmazonS3 amazonS3 = Mockito.mock(AmazonS3.class); willAnswer(invocation -> { ObjectListing objectListing = new ObjectListing(); List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries(); for (S3Object s3Object : S3_OBJECTS) { S3ObjectSummary s3ObjectSummary = new S3ObjectSummary(); s3ObjectSummary.setBucketName(S3_BUCKET); s3ObjectSummary.setKey(s3Object.getKey()); s3ObjectSummary.setLastModified(new Date(new File(s3Object.getKey()).lastModified())); objectSummaries.add(s3ObjectSummary); } return objectListing; }).given(amazonS3).listObjects(any(ListObjectsRequest.class)); for (final S3Object s3Object : S3_OBJECTS) { willAnswer(invocation -> s3Object).given(amazonS3).getObject(S3_BUCKET, s3Object.getKey()); } return amazonS3; }
@Test(expected = AmazonServiceException.class) public void listS3FileByPrefix_on_retry_gave_up_should_throw_the_original_exception_in_expiredToken_code() { AmazonServiceException exception = new AmazonServiceException("expired token exception"); exception.setStatusCode(HttpStatus.SC_BAD_REQUEST); exception.setErrorCode("ExpiredToken"); exception.setErrorType(AmazonServiceException.ErrorType.Client); doThrow(exception).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(1)); }
@Override public ObjectListing answer(InvocationOnMock invocation) throws Throwable { ListObjectsRequest listObjectsRequest = invocation.getArgument(0); String marker = listObjectsRequest.getMarker(); ObjectListing objectListing = new ObjectListing(); if (marker == null) { objectListing.setNextMarker("marker"); objectListing.setTruncated(true); } else { assertEquals("marker", marker); S3ObjectSummary s3ObjectSummary = new S3ObjectSummary(); s3ObjectSummary.setKey(expectedFilePath); s3ObjectSummary.setSize(expectedFileSize); objectListing.getObjectSummaries().add(s3ObjectSummary); } return objectListing; } });
ObjectListing objectListing = new ObjectListing(); objectListing.setBucketName(bucketName);
@Override public ObjectListing listNextBatchOfObjects( ListNextBatchOfObjectsRequest listNextBatchOfObjectsRequest) throws AmazonClientException, AmazonServiceException { final ObjectListing previousObjectListing = listNextBatchOfObjectsRequest.getPreviousObjectListing(); if (!previousObjectListing.isTruncated()) { final ObjectListing emptyListing = new ObjectListing(); emptyListing.setBucketName(previousObjectListing.getBucketName()); emptyListing.setDelimiter(previousObjectListing.getDelimiter()); emptyListing.setMarker(previousObjectListing.getNextMarker()); emptyListing.setMaxKeys(previousObjectListing.getMaxKeys()); emptyListing.setPrefix(previousObjectListing.getPrefix()); emptyListing.setEncodingType(previousObjectListing.getEncodingType()); emptyListing.setTruncated(false); return emptyListing; } return listObjects(listNextBatchOfObjectsRequest.toListObjectsRequest()); }
@Override public ObjectListing listNextBatchOfObjects(ListNextBatchOfObjectsRequest listNextBatchOfObjectsRequest) throws SdkClientException, AmazonServiceException { listNextBatchOfObjectsRequest = beforeClientExecution(listNextBatchOfObjectsRequest); rejectNull(listNextBatchOfObjectsRequest, "The request object parameter must be specified when listing the next batch of objects in a bucket"); ObjectListing previousObjectListing = listNextBatchOfObjectsRequest.getPreviousObjectListing(); if (!previousObjectListing.isTruncated()) { ObjectListing emptyListing = new ObjectListing(); emptyListing.setBucketName(previousObjectListing.getBucketName()); emptyListing.setDelimiter(previousObjectListing.getDelimiter()); emptyListing.setMarker(previousObjectListing.getNextMarker()); emptyListing.setMaxKeys(previousObjectListing.getMaxKeys()); emptyListing.setPrefix(previousObjectListing.getPrefix()); emptyListing.setEncodingType(previousObjectListing.getEncodingType()); emptyListing.setTruncated(false); return emptyListing; } return listObjects(listNextBatchOfObjectsRequest.toListObjectsRequest()); }
assertEquals(s3KeyPrefix, listObjectsRequest.getPrefix()); ObjectListing objectListing = new ObjectListing();