use of ddf.catalog.resourceretriever.ResourceRetriever 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.resourceretriever.ResourceRetriever in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testDownloadWithNullResourceRequest.
@Test(expected = DownloadException.class)
public void testDownloadWithNullResourceRequest() throws Exception {
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
ResourceRetriever retriever = mock(ResourceRetriever.class);
downloadMgr.download(null, metacard, retriever);
}
use of ddf.catalog.resourceretriever.ResourceRetriever 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.resourceretriever.ResourceRetriever in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testStoreWithInputStreamRecoverableErrorCachingEnabled.
/**
* Test that if an Exception is thrown while reading the product's InputStream that
* download and caching is interrupted, retried and both successfully complete on the second attempt.
*
* @throws Exception
*/
@Test
@Ignore
public void testStoreWithInputStreamRecoverableErrorCachingEnabled() throws Exception {
mis = new MockInputStream(productInputFilename);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.INPUT_STREAM_IO_EXCEPTION);
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);
// Verifies client read same contents as product input file
verifyClientBytesRead(clientBytesRead);
cleanup();
}
use of ddf.catalog.resourceretriever.ResourceRetriever in project ddf by codice.
the class ReliableResourceDownloadManagerTest method getMockResourceRetrieverWithRetryCapability.
private ResourceRetriever getMockResourceRetrieverWithRetryCapability(final RetryType retryType, final boolean readSlow) throws Exception {
// Mocking to support re-retrieval of product when error encountered
// during caching.
ResourceRetriever retriever = mock(ResourceRetriever.class);
when(retriever.retrieveResource()).thenAnswer(new Answer<Object>() {
int invocationCount = 0;
public Object answer(InvocationOnMock invocation) throws ResourceNotFoundException {
// Create new InputStream for retrieving the same product. This
// simulates re-retrieving the product from the remote source.
invocationCount++;
if (readSlow) {
mis = new MockInputStream(productInputFilename, true);
mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
} else {
mis = new MockInputStream(productInputFilename);
}
if (retryType == RetryType.INPUT_STREAM_IO_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(5);
} else {
mis.setInvocationCountToThrowIOException(-1);
}
} else if (retryType == RetryType.TIMEOUT_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToTimeout(3);
mis.setReadDelay(MONITOR_PERIOD * 2, TimeUnit.SECONDS);
} else {
mis.setInvocationCountToTimeout(-1);
mis.setReadDelay(0, TimeUnit.SECONDS);
}
} else if (retryType == RetryType.NETWORK_CONNECTION_UP_AND_DOWN) {
mis.setInvocationCountToThrowIOException(2);
} else if (retryType == RetryType.NETWORK_CONNECTION_DROPPED) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(2);
} else {
throw new ResourceNotFoundException();
}
}
// Reset the mock Resource so that it can be reconfigured to return
// the new InputStream
reset(resource);
when(resource.getInputStream()).thenReturn(mis);
when(resource.getName()).thenReturn("test-resource");
try {
when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
} catch (MimeTypeParseException e) {
}
// Reset the mock ResourceResponse so that it can be reconfigured to return
// the new Resource
reset(resourceResponse);
when(resourceResponse.getRequest()).thenReturn(resourceRequest);
when(resourceResponse.getResource()).thenReturn(resource);
when(resourceResponse.getProperties()).thenReturn(new HashMap<String, Serializable>());
return resourceResponse;
}
});
ArgumentCaptor<Long> bytesReadArg = ArgumentCaptor.forClass(Long.class);
// Mocking to support re-retrieval of product when error encountered
// during caching. This resource retriever supports skipping.
when(retriever.retrieveResource(anyLong())).thenAnswer(new Answer<Object>() {
int invocationCount = 0;
public Object answer(InvocationOnMock invocation) throws ResourceNotFoundException, IOException {
// Create new InputStream for retrieving the same product. This
// simulates re-retrieving the product from the remote source.
invocationCount++;
if (readSlow) {
mis = new MockInputStream(productInputFilename, true);
mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
} else {
mis = new MockInputStream(productInputFilename);
}
// Skip the number of bytes that have already been read
Object[] args = invocation.getArguments();
long bytesToSkip = (Long) args[0];
mis.skip(bytesToSkip);
if (retryType == RetryType.INPUT_STREAM_IO_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(5);
} else {
mis.setInvocationCountToThrowIOException(-1);
}
} else if (retryType == RetryType.TIMEOUT_EXCEPTION) {
if (invocationCount == 1) {
mis.setInvocationCountToTimeout(3);
mis.setReadDelay(MONITOR_PERIOD * 2, TimeUnit.SECONDS);
} else {
mis.setInvocationCountToTimeout(-1);
mis.setReadDelay(0, TimeUnit.MILLISECONDS);
}
} else if (retryType == RetryType.NETWORK_CONNECTION_UP_AND_DOWN) {
mis.setInvocationCountToThrowIOException(2);
} else if (retryType == RetryType.NETWORK_CONNECTION_DROPPED) {
if (invocationCount == 1) {
mis.setInvocationCountToThrowIOException(2);
} else {
throw new ResourceNotFoundException();
}
}
// Reset the mock Resource so that it can be reconfigured to return
// the new InputStream
reset(resource);
when(resource.getInputStream()).thenReturn(mis);
when(resource.getName()).thenReturn("test-resource");
try {
when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
} catch (MimeTypeParseException e) {
}
// Reset the mock ResourceResponse so that it can be reconfigured to return
// the new Resource
reset(resourceResponse);
when(resourceResponse.getRequest()).thenReturn(resourceRequest);
when(resourceResponse.getResource()).thenReturn(resource);
Map<String, Serializable> responseProperties = new HashMap<>();
responseProperties.put("BytesSkipped", true);
when(resourceResponse.getProperties()).thenReturn(responseProperties);
when(resourceResponse.containsPropertyName("BytesSkipped")).thenReturn(true);
when(resourceResponse.getPropertyValue("BytesSkipped")).thenReturn(true);
return resourceResponse;
}
});
return retriever;
}
Aggregations