Search in sources :

Example 76 with ResourceResponse

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));
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountingOutputStream(com.google.common.io.CountingOutputStream) Metacard(ddf.catalog.data.Metacard) ResourceCacheImpl(ddf.catalog.cache.impl.ResourceCacheImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) DownloadStatusInfoImpl(ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 77 with ResourceResponse

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));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SubjectUtils(ddf.security.service.impl.SubjectUtils) Metacard(ddf.catalog.data.Metacard) ResourceResponse(ddf.catalog.operation.ResourceResponse) DownloadStatusInfoImpl(ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl) IOException(java.io.IOException) Test(org.junit.Test)

Example 78 with ResourceResponse

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));
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SubjectUtils(ddf.security.service.impl.SubjectUtils) Metacard(ddf.catalog.data.Metacard) ResourceCacheImpl(ddf.catalog.cache.impl.ResourceCacheImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) DownloadStatusInfoImpl(ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 79 with ResourceResponse

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

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));
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Resource(ddf.catalog.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) URI(java.net.URI)

Aggregations

ResourceResponse (ddf.catalog.operation.ResourceResponse)94 Test (org.junit.Test)49 URI (java.net.URI)30 HashMap (java.util.HashMap)28 Serializable (java.io.Serializable)26 Resource (ddf.catalog.resource.Resource)25 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)23 ResourceRequest (ddf.catalog.operation.ResourceRequest)18 IOException (java.io.IOException)18 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)17 Metacard (ddf.catalog.data.Metacard)16 MimeType (javax.activation.MimeType)16 Response (javax.ws.rs.core.Response)15 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)14 URLResourceReader (ddf.catalog.resource.impl.URLResourceReader)14 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)10 InputStream (java.io.InputStream)9 Map (java.util.Map)9 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)8 StringWriter (java.io.StringWriter)8