Search in sources :

Example 11 with ResourceRetriever

use of ddf.catalog.resourceretriever.ResourceRetriever in project ddf by codice.

the class ReliableResourceDownloadManagerTest method testDownloadWithNullMetacard.

@Test(expected = DownloadException.class)
public void testDownloadWithNullMetacard() throws Exception {
    resourceRequest = mock(ResourceRequest.class);
    ResourceRetriever retriever = mock(ResourceRetriever.class);
    downloadMgr.download(resourceRequest, null, retriever);
}
Also used : ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ResourceRequest(ddf.catalog.operation.ResourceRequest) Test(org.junit.Test)

Example 12 with ResourceRetriever

use of ddf.catalog.resourceretriever.ResourceRetriever in project ddf by codice.

the class ReliableResourceDownloadManagerTest method testStoreWithTimeoutExceptionCachingEnabled.

/**
     * Test storing product in cache and one of the chunks being stored takes too long, triggering
     * the CacheMonitor to interrupt the caching. Verify that caching is retried and successfully
     * completes on the second attempt.
     *
     * @throws Exception
     */
@Test
public //@Ignore
void testStoreWithTimeoutExceptionCachingEnabled() throws Exception {
    mis = new MockInputStream(productInputFilename);
    Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
    resourceResponse = getMockResourceResponse();
    ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.TIMEOUT_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);
    verifyClientBytesRead(clientBytesRead);
    cleanup();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReliableResource(ddf.catalog.resource.data.ReliableResource) Test(org.junit.Test)

Example 13 with ResourceRetriever

use of ddf.catalog.resourceretriever.ResourceRetriever in project ddf by codice.

the class ReliableResourceDownloadManagerTest method testClientCancelProductDownloadCachingStops.

/**
     * Tests that if user/client cancels a product retrieval that is in progress and
     * actively being cached, and the admin has not configured caching to continue,
     * that the (partially) cached file is deleted and not placed in the cache map.
     *
     * @throws Exception
     */
@Test
@Ignore
public void testClientCancelProductDownloadCachingStops() throws Exception {
    mis = new MockInputStream(productInputFilename, true);
    mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
    Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
    resourceResponse = getMockResourceResponse();
    // Need the product InputStream (MockInputStream) to read slower so that client has time to
    // start reading from the ReliableResourceInputStream and close it, simulating a cancel of
    // the product download
    ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.CLIENT_CANCELS_DOWNLOAD, true);
    int chunkSize = 50;
    startDownload(true, chunkSize, false, metacard, retriever);
    // On second read of ReliableResourceInputStream, client will close the stream simulating a cancel
    // of the product download
    clientRead(chunkSize, productInputStream, 2);
    // 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();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) CacheKey(ddf.catalog.cache.impl.CacheKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with ResourceRetriever

use of ddf.catalog.resourceretriever.ResourceRetriever 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();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReliableResource(ddf.catalog.resource.data.ReliableResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with ResourceRetriever

use of ddf.catalog.resourceretriever.ResourceRetriever 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();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheKey(ddf.catalog.cache.impl.CacheKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ResourceRetriever (ddf.catalog.resourceretriever.ResourceRetriever)21 Test (org.junit.Test)19 Metacard (ddf.catalog.data.Metacard)18 MockInputStream (ddf.catalog.cache.MockInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Ignore (org.junit.Ignore)8 CacheKey (ddf.catalog.cache.impl.CacheKey)6 ReliableResource (ddf.catalog.resource.data.ReliableResource)6 ResourceRequest (ddf.catalog.operation.ResourceRequest)5 ResourceResponse (ddf.catalog.operation.ResourceResponse)3 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)2 IOException (java.io.IOException)2 DownloadStatusInfoImpl (ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl)1 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MimeType (javax.activation.MimeType)1 MimeTypeParseException (javax.activation.MimeTypeParseException)1