use of ddf.catalog.cache.MockInputStream 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());
downloader.setupDownload(mockMetacard, new DownloadStatusInfoImpl());
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.cache.MockInputStream in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testRetryAttemptsExhaustedDuringProductDownload.
/**
* Tests that if network connection drops repeatedly during a product retrieval such that all
* of the retry attempts are used up before entire product is downloaded, then the (partially) cached file is deleted and not
* placed in the cache map, and product download fails.
*
* @throws Exception
*/
@Test
@Ignore
public void testRetryAttemptsExhaustedDuringProductDownload() throws Exception {
mis = new MockInputStream(productInputFilename);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.NETWORK_CONNECTION_UP_AND_DOWN);
int chunkSize = 50;
startDownload(true, chunkSize, false, metacard, retriever);
ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
// Verify client did not receive entire product download
assertTrue(clientBytesRead.size() < expectedFileSize);
// Verify product was not cached, i.e., its pending caching entry was removed
String cacheKey = new CacheKey(metacard, resourceResponse.getRequest()).generateKey();
verify(resourceCache, timeout(3000)).removePendingCacheEntry(cacheKey);
cleanup();
}
use of ddf.catalog.cache.MockInputStream in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testNetworkConnectionDroppedDuringProductDownload.
/**
* Tests that if network connection dropped during a product retrieval that is in progress and
* actively being cached, that the (partially) cached file is deleted and not
* placed in the cache map.
*
* @throws Exception
*/
@Test
@Ignore
public void testNetworkConnectionDroppedDuringProductDownload() throws Exception {
mis = new MockInputStream(productInputFilename);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.NETWORK_CONNECTION_DROPPED);
int chunkSize = 50;
startDownload(true, chunkSize, false, metacard, retriever);
ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
// Verify client did not receive entire product download
assertTrue(clientBytesRead.size() < expectedFileSize);
// Verify product was not cached, i.e., its pending caching entry was removed
String cacheKey = new CacheKey(metacard, resourceResponse.getRequest()).generateKey();
verify(resourceCache, timeout(3000)).removePendingCacheEntry(cacheKey);
cleanup();
}
use of ddf.catalog.cache.MockInputStream in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testDownloadWithCaching.
@Test
public //@Ignore
void testDownloadWithCaching() 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);
CacheKey cacheKey = new CacheKey(metacard, resourceResponse.getRequest());
String key = cacheKey.generateKey();
when(resourceCache.isPending(key)).thenReturn(false);
int chunkSize = 50;
startDownload(true, chunkSize, false, metacard, retriever);
ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
// Captures the ReliableResource object that should have been put in the ResourceCacheImpl's map
ArgumentCaptor<ReliableResource> argument = ArgumentCaptor.forClass(ReliableResource.class);
verify(resourceCache).put(argument.capture());
verifyCaching(argument.getValue(), EXPECTED_CACHE_KEY);
verifyClientBytesRead(clientBytesRead);
cleanup();
}
use of ddf.catalog.cache.MockInputStream 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));
}
Aggregations