Search in sources :

Example 16 with ResourceRetriever

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();
}
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)

Example 17 with ResourceRetriever

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);
}
Also used : Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) Test(org.junit.Test)

Example 18 with ResourceRetriever

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();
}
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) ReliableResource(ddf.catalog.resource.data.ReliableResource) Test(org.junit.Test)

Example 19 with ResourceRetriever

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();
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with ResourceRetriever

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;
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) MimeTypeParseException(javax.activation.MimeTypeParseException) HashMap(java.util.HashMap) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) IOException(java.io.IOException) MimeType(javax.activation.MimeType) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyLong(org.mockito.Matchers.anyLong) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap)

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