use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testDownloadWithoutCaching.
@Test
public //@Ignore
void testDownloadWithoutCaching() throws Exception {
mis = new MockInputStream(productInputFilename);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
ResourceRetriever retriever = mock(ResourceRetriever.class);
when(retriever.retrieveResource()).thenReturn(resourceResponse);
int chunkSize = 50;
downloadMgr.setChunkSize(chunkSize);
ResourceResponse newResourceResponse = downloadMgr.download(resourceRequest, metacard, retriever);
assertThat(newResourceResponse, is(notNullValue()));
productInputStream = newResourceResponse.getResource().getInputStream();
assertThat(productInputStream, is(instanceOf(ReliableResourceInputStream.class)));
ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
verifyClientBytesRead(clientBytesRead);
cleanup();
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloaderTest method getMockResourceResponse.
private ResourceResponse getMockResourceResponse(InputStream stream) throws Exception {
ResourceRequest resourceRequest = mock(ResourceRequest.class);
Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
mockResource = mock(Resource.class);
when(mockResource.getInputStream()).thenReturn(stream);
when(mockResource.getName()).thenReturn("test-resource");
when(mockResource.getMimeType()).thenReturn(new MimeType("text/plain"));
mockResponse = mock(ResourceResponse.class);
when(mockResponse.getRequest()).thenReturn(resourceRequest);
when(mockResponse.getResource()).thenReturn(mockResource);
Map<String, Serializable> responseProperties = new HashMap<String, Serializable>();
when(mockResponse.getProperties()).thenReturn(responseProperties);
return mockResponse;
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloaderTest method testNullReliableResourceCallableAndStatus.
@Test
public void testNullReliableResourceCallableAndStatus() throws Exception {
ResourceResponse mockResponse = getMockResourceResponse(mockStream);
ResourceRetriever mockResourceRetriever = mock(ResourceRetriever.class);
when(mockResourceRetriever.retrieveResource(any(Byte.class))).thenReturn(mockResponse);
ReliableResourceStatus resourceStatus = new ReliableResourceStatus(DownloadStatus.RESOURCE_DOWNLOAD_INTERRUPTED, 0L);
ReliableResourceCallable mockCallable = mock(ReliableResourceCallable.class);
when(mockCallable.getReliableResourceStatus()).thenReturn(resourceStatus);
PowerMockito.whenNew(ReliableResourceCallable.class).withAnyArguments().thenReturn(null, mockCallable);
PowerMockito.whenNew(ResourceRetrievalMonitor.class).withAnyArguments().thenThrow(new CancellationException());
int retries = 5;
downloaderConfig.setMaxRetryAttempts(retries);
ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), DOWNLOAD_ID, mockResponse, mockResourceRetriever);
downloader.setupDownload(mockMetacard, new DownloadStatusInfoImpl());
downloader.run();
verify(mockPublisher, times(retries)).postRetrievalStatus(any(ResourceResponse.class), eq(ProductRetrievalStatus.RETRYING), any(Metacard.class), anyString(), anyLong(), eq(DOWNLOAD_ID));
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloadManagerTest method getMockResourceResponse.
private ResourceResponse getMockResourceResponse() throws Exception {
resourceRequest = mock(ResourceRequest.class);
Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
resource = mock(Resource.class);
when(resource.getInputStream()).thenReturn(mis);
when(resource.getName()).thenReturn("test-resource");
when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
resourceResponse = mock(ResourceResponse.class);
when(resourceResponse.getRequest()).thenReturn(resourceRequest);
when(resourceResponse.getResource()).thenReturn(resource);
Map<String, Serializable> responseProperties = new HashMap<String, Serializable>();
when(resourceResponse.getProperties()).thenReturn(responseProperties);
return resourceResponse;
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloadManager method startDownload.
private ResourceResponse startDownload(String downloadIdentifier, ResourceResponse resourceResponse, ResourceRetriever retriever, Metacard metacard) {
AtomicBoolean downloadStarted = new AtomicBoolean(Boolean.FALSE);
ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, downloadStarted, downloadIdentifier, resourceResponse, retriever);
ResourceResponse response = downloader.setupDownload(metacard, downloadStatusInfo);
response.getProperties().put(DOWNLOAD_ID_PROPERTY_KEY, downloadIdentifier);
// Start download in separate thread so can return ResourceResponse with
// ReliableResourceInputStream available for client to start reading from
executor.submit(downloader);
// Wait for download to get started before returning control to client
Stopwatch stopwatch = Stopwatch.createStarted();
while (!downloadStarted.get()) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
if (elapsedTime > ONE_SECOND_IN_MS) {
LOGGER.debug("downloadStarted still FALSE - elapsedTime = {}", elapsedTime);
break;
}
}
LOGGER.debug("elapsedTime = {}", stopwatch.elapsed(TimeUnit.MILLISECONDS));
stopwatch.stop();
return response;
}
Aggregations