S3TimeOrderPrefixFileExplorer extends S3PrefixFileExplorer { @Override public List<S3ObjectSummary> fetch() { ++numOfReq; final ListObjectsRequest req = new ListObjectsRequest(bucketName, pathPrefix, lastPath, null, 1024); final ObjectListing objectListing = new DefaultRetryable<ObjectListing>("Listing objects") { @Override public ObjectListing call() { return s3Client.listObjects(req); } }.executeWith(retryExecutor); lastPath = objectListing.getNextMarker(); return objectListing.getObjectSummaries() .stream() .filter(s3ObjectSummary -> s3ObjectSummary.getLastModified().before(to) && (!from.isPresent() || s3ObjectSummary.getLastModified().equals(from.get()) || s3ObjectSummary.getLastModified().after(from.get()))) .collect(Collectors.toList()); } S3TimeOrderPrefixFileExplorer(final String bucket, final AmazonS3 client, final RetryExecutor retryExecutor,
            final String pathPrefix, final boolean skipGlacierObjects, final Optional<Date> from, final Date to); @Override List<S3ObjectSummary> fetch(); @Override boolean hasNext();  }
@Test public void fetch_should_return_filtered_objects_before_end_time() { final S3ObjectSummary s3ObjectBefore = mock(S3ObjectSummary.class); final Calendar cal = Calendar.getInstance(); cal.set(2019, Calendar.MAY, 24, 10, 0); when(s3ObjectBefore.getLastModified()).thenReturn(cal.getTime()); final S3ObjectSummary s3ObjectAfter = mock(S3ObjectSummary.class); cal.set(2019, Calendar.MAY, 26, 10, 0); when(s3ObjectAfter.getLastModified()).thenReturn(cal.getTime()); final ObjectListing ol = mock(ObjectListing.class); when(s3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(ol); when(ol.getObjectSummaries()).thenReturn(Arrays.asList(s3ObjectBefore, s3ObjectAfter)); final List<S3ObjectSummary> result = s3TimeOrderPrefixFileExplorer.fetch(); assertEquals(1, result.size()); assertEquals(s3ObjectBefore, result.get(0)); }
@Test public void fetch_should_return_filtered_objects_after_or_equals_begin_time() { final Calendar to = Calendar.getInstance(); to.set(2019, Calendar.MAY, 25, 10, 0); final Calendar from = Calendar.getInstance(); from.set(2019, Calendar.MAY, 24, 10, 0); s3TimeOrderPrefixFileExplorer = new S3TimeOrderPrefixFileExplorer(BUCKET_NAME, s3Client, null, PATH_PREFIX, false, Optional.of(from.getTime()), to.getTime()); final S3ObjectSummary s3ObjectEqual = mock(S3ObjectSummary.class); final Calendar equalCal = Calendar.getInstance(); equalCal.set(2019, Calendar.MAY, 24, 10, 0); when(s3ObjectEqual.getLastModified()).thenReturn(equalCal.getTime()); final S3ObjectSummary s3ObjectBefore = mock(S3ObjectSummary.class); final Calendar beforeCal = Calendar.getInstance(); beforeCal.set(2019, Calendar.MAY, 24, 20, 0); when(s3ObjectBefore.getLastModified()).thenReturn(beforeCal.getTime()); final S3ObjectSummary s3ObjectAfter = mock(S3ObjectSummary.class); final Calendar afterCal = Calendar.getInstance(); afterCal.set(2019, Calendar.MAY, 26, 10, 0); when(s3ObjectAfter.getLastModified()).thenReturn(afterCal.getTime()); final ObjectListing ol = mock(ObjectListing.class); when(s3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(ol); when(ol.getObjectSummaries()).thenReturn(Arrays.asList(s3ObjectEqual, s3ObjectBefore, s3ObjectAfter)); final List<S3ObjectSummary> result = s3TimeOrderPrefixFileExplorer.fetch(); assertEquals(2, result.size()); assertEquals(s3ObjectEqual, result.get(0)); assertEquals(s3ObjectBefore, result.get(1)); }