use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloaderTest method testClientOutputStreamException.
@Test
@Ignore
public // Can't figure out how to throw IOExcetion from CountingOutputStream
void testClientOutputStreamException() throws Exception {
downloaderConfig.setCacheEnabled(true);
ResourceCacheImpl mockCache = mock(ResourceCacheImpl.class);
when(mockCache.isPending(anyString())).thenReturn(false);
when(mockCache.getProductCacheDirectory()).thenReturn(productCacheDirectory);
downloaderConfig.setResourceCache(mockCache);
mis = new MockInputStream(productInputFilename);
ResourceResponse mockResponse = getMockResourceResponse(mis);
ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), "123", mockResponse, getMockRetriever());
downloader.setupDownload(mockMetacard, new DownloadStatusInfoImpl());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CountingOutputStream mockCountingFbos = new CountingOutputStream(baos);
IOUtils.closeQuietly(baos);
downloader.setCountingOutputStream(mockCountingFbos);
downloader.run();
verify(mockPublisher, times(1)).postRetrievalStatus(any(ResourceResponse.class), eq(ProductRetrievalStatus.CANCELLED), any(Metacard.class), anyString(), anyLong(), eq(DOWNLOAD_ID));
verify(mockCache, times(1)).removePendingCacheEntry(anyString());
assertThat(downloaderConfig.isCacheEnabled(), is(false));
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloaderTest method testIOExceptionDuringRead.
@Test
public void testIOExceptionDuringRead() throws Exception {
ResourceResponse mockResponse = getMockResourceResponse(mockStream);
when(mockStream.read(any(byte[].class))).thenThrow(new IOException());
int retries = 5;
downloaderConfig.setMaxRetryAttempts(retries);
DownloadStatusInfoImpl downloadStatusInfo = new DownloadStatusInfoImpl();
downloadStatusInfo.setSubjectOperations(new SubjectUtils());
ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), DOWNLOAD_ID, mockResponse, getMockRetriever());
downloader.setupDownload(mockMetacard, downloadStatusInfo);
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 ReliableResourceDownloaderTest method testCacheExceptionDuringWrite.
@Test
public void testCacheExceptionDuringWrite() throws Exception {
downloaderConfig.setCacheEnabled(true);
ResourceCacheImpl mockCache = mock(ResourceCacheImpl.class);
when(mockCache.isPending(anyString())).thenReturn(false);
when(mockCache.getProductCacheDirectory()).thenReturn(productCacheDirectory);
downloaderConfig.setResourceCache(mockCache);
mis = new MockInputStream(productInputFilename);
ResourceResponse mockResponse = getMockResourceResponse(mis);
ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), "123", mockResponse, getMockRetriever());
DownloadStatusInfoImpl downloadStatusInfo = new DownloadStatusInfoImpl();
downloadStatusInfo.setSubjectOperations(new SubjectUtils());
downloader.setupDownload(mockMetacard, downloadStatusInfo);
FileOutputStream mockFos = mock(FileOutputStream.class);
doThrow(new IOException()).when(mockFos).write(any(byte[].class), anyInt(), anyInt());
downloader.setFileOutputStream(mockFos);
downloader.run();
verify(mockPublisher, times(1)).postRetrievalStatus(any(ResourceResponse.class), eq(ProductRetrievalStatus.RETRYING), any(Metacard.class), anyString(), anyLong(), eq(DOWNLOAD_ID));
verify(mockCache, times(1)).removePendingCacheEntry(anyString());
assertThat(downloaderConfig.isCacheEnabled(), is(false));
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testStreamToClientExceptionDuringProductDownloadCachingEnabled.
/**
* Tests that if exception with the FileBackedOutputStream being written to and concurrently read
* by the client occurs during a product retrieval, then the product download to the client is
* stopped, but the caching of the file continues.
*
* @throws Exception
*/
@Test
@Ignore
public // does not seem to detect this and continues to stream successfully to the client.
void testStreamToClientExceptionDuringProductDownloadCachingEnabled() throws Exception {
mis = new MockInputStream(productInputFilename);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
downloadMgr = new ReliableResourceDownloadManager(getDownloaderConfig(), downloadStatusInfo, Executors.newSingleThreadExecutor());
// Use small chunk size so download takes long enough for client
// to have time to simulate FileBackedOutputStream exception
int chunkSize = 2;
downloadMgr.setChunkSize(chunkSize);
ResourceRetriever retriever = mock(ResourceRetriever.class);
when(retriever.retrieveResource()).thenReturn(resourceResponse);
ArgumentCaptor<ReliableResource> argument = ArgumentCaptor.forClass(ReliableResource.class);
ResourceResponse newResourceResponse = downloadMgr.download(resourceRequest, metacard, retriever);
assertThat(newResourceResponse, is(notNullValue()));
productInputStream = newResourceResponse.getResource().getInputStream();
assertThat(productInputStream, is(instanceOf(ReliableResourceInputStream.class)));
// On second chunk read by client it will close the download manager's cache file output stream
// to simulate a cache file exception that should be detected by the ReliableResourceCallable
executor = Executors.newCachedThreadPool();
ProductDownloadClient productDownloadClient = new ProductDownloadClient(productInputStream, chunkSize);
productDownloadClient.setSimulateFbosException(chunkSize, downloadMgr);
future = executor.submit(productDownloadClient);
ByteArrayOutputStream clientBytesRead = future.get();
// Verify client did not receive entire product download
assertTrue(clientBytesRead.size() < expectedFileSize);
// Captures the ReliableResource object that should have been put in the ResourceCacheImpl's map
verify(resourceCache, timeout(3000)).put(argument.capture());
verifyCaching(argument.getValue(), EXPECTED_CACHE_KEY);
cleanup();
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class URLResourceReaderTest method verifyFile.
private void verifyFile(String filePath, String filename, String expectedMimeType, String... rootResourceDirectories) throws Exception {
URLResourceReader resourceReader = new URLResourceReader(mimeTypeMapper, clientBuilderFactory);
resourceReader.setRootResourceDirectories(new HashSet<String>(Arrays.asList(rootResourceDirectories)));
HashMap<String, Serializable> arguments = new HashMap<String, Serializable>();
LOGGER.info("Getting resource: {}", filePath);
// Test using the URL ResourceReader
File file = new File(filePath);
URI uri = file.toURI();
LOGGER.info("URI: {}", uri.toString());
ResourceResponse resourceResponse = resourceReader.retrieveResource(uri, arguments);
Resource resource = resourceResponse.getResource();
assert (resource != null);
LOGGER.info("MimeType: {}", resource.getMimeType());
LOGGER.info("Got resource: {}", resource.getName());
String name = resource.getName();
assertNotNull(name);
assertThat(name, is(filename));
assertThat(resource.getMimeType().toString(), containsString(expectedMimeType));
}
Aggregations