Search in sources :

Example 16 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ReliableResourceDownloadManagerTest method testDownloadWithoutCaching.

@Test
public //@Ignore
void testDownloadWithoutCaching() 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);
    int chunkSize = 50;
    downloadMgr.setChunkSize(chunkSize);
    ResourceResponse newResourceResponse = downloadMgr.download(resourceRequest, metacard, retriever);
    assertThat(newResourceResponse, is(notNullValue()));
    productInputStream = newResourceResponse.getResource().getInputStream();
    assertThat(productInputStream, is(instanceOf(ReliableResourceInputStream.class)));
    ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
    verifyClientBytesRead(clientBytesRead);
    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) Test(org.junit.Test)

Example 17 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ReliableResourceDownloaderTest method getMockResourceResponse.

private ResourceResponse getMockResourceResponse(InputStream stream) throws Exception {
    ResourceRequest resourceRequest = mock(ResourceRequest.class);
    Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
    when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
    mockResource = mock(Resource.class);
    when(mockResource.getInputStream()).thenReturn(stream);
    when(mockResource.getName()).thenReturn("test-resource");
    when(mockResource.getMimeType()).thenReturn(new MimeType("text/plain"));
    mockResponse = mock(ResourceResponse.class);
    when(mockResponse.getRequest()).thenReturn(resourceRequest);
    when(mockResponse.getResource()).thenReturn(mockResource);
    Map<String, Serializable> responseProperties = new HashMap<String, Serializable>();
    when(mockResponse.getProperties()).thenReturn(responseProperties);
    return mockResponse;
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) Matchers.anyString(org.mockito.Matchers.anyString) MimeType(javax.activation.MimeType)

Example 18 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ReliableResourceDownloaderTest method testNullReliableResourceCallableAndStatus.

@Test
public void testNullReliableResourceCallableAndStatus() throws Exception {
    ResourceResponse mockResponse = getMockResourceResponse(mockStream);
    ResourceRetriever mockResourceRetriever = mock(ResourceRetriever.class);
    when(mockResourceRetriever.retrieveResource(any(Byte.class))).thenReturn(mockResponse);
    ReliableResourceStatus resourceStatus = new ReliableResourceStatus(DownloadStatus.RESOURCE_DOWNLOAD_INTERRUPTED, 0L);
    ReliableResourceCallable mockCallable = mock(ReliableResourceCallable.class);
    when(mockCallable.getReliableResourceStatus()).thenReturn(resourceStatus);
    PowerMockito.whenNew(ReliableResourceCallable.class).withAnyArguments().thenReturn(null, mockCallable);
    PowerMockito.whenNew(ResourceRetrievalMonitor.class).withAnyArguments().thenThrow(new CancellationException());
    int retries = 5;
    downloaderConfig.setMaxRetryAttempts(retries);
    ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), DOWNLOAD_ID, mockResponse, mockResourceRetriever);
    downloader.setupDownload(mockMetacard, new DownloadStatusInfoImpl());
    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) Metacard(ddf.catalog.data.Metacard) ResourceResponse(ddf.catalog.operation.ResourceResponse) CancellationException(java.util.concurrent.CancellationException) DownloadStatusInfoImpl(ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ReliableResourceDownloadManagerTest method getMockResourceResponse.

private ResourceResponse getMockResourceResponse() throws Exception {
    resourceRequest = mock(ResourceRequest.class);
    Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
    when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
    resource = mock(Resource.class);
    when(resource.getInputStream()).thenReturn(mis);
    when(resource.getName()).thenReturn("test-resource");
    when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
    resourceResponse = mock(ResourceResponse.class);
    when(resourceResponse.getRequest()).thenReturn(resourceRequest);
    when(resourceResponse.getResource()).thenReturn(resource);
    Map<String, Serializable> responseProperties = new HashMap<String, Serializable>();
    when(resourceResponse.getProperties()).thenReturn(responseProperties);
    return resourceResponse;
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) MimeType(javax.activation.MimeType)

Example 20 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ReliableResourceDownloadManager method startDownload.

private ResourceResponse startDownload(String downloadIdentifier, ResourceResponse resourceResponse, ResourceRetriever retriever, Metacard metacard) {
    AtomicBoolean downloadStarted = new AtomicBoolean(Boolean.FALSE);
    ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, downloadStarted, downloadIdentifier, resourceResponse, retriever);
    ResourceResponse response = downloader.setupDownload(metacard, downloadStatusInfo);
    response.getProperties().put(DOWNLOAD_ID_PROPERTY_KEY, downloadIdentifier);
    // Start download in separate thread so can return ResourceResponse with
    // ReliableResourceInputStream available for client to start reading from
    executor.submit(downloader);
    // Wait for download to get started before returning control to client
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (!downloadStarted.get()) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
        if (elapsedTime > ONE_SECOND_IN_MS) {
            LOGGER.debug("downloadStarted still FALSE - elapsedTime = {}", elapsedTime);
            break;
        }
    }
    LOGGER.debug("elapsedTime = {}", stopwatch.elapsed(TimeUnit.MILLISECONDS));
    stopwatch.stop();
    return response;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResourceResponse(ddf.catalog.operation.ResourceResponse) Stopwatch(com.google.common.base.Stopwatch)

Aggregations

ResourceResponse (ddf.catalog.operation.ResourceResponse)71 Test (org.junit.Test)36 HashMap (java.util.HashMap)17 Resource (ddf.catalog.resource.Resource)16 URI (java.net.URI)16 Serializable (java.io.Serializable)15 Metacard (ddf.catalog.data.Metacard)13 IOException (java.io.IOException)13 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)13 ResourceRequest (ddf.catalog.operation.ResourceRequest)12 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)12 MimeType (javax.activation.MimeType)11 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)10 Response (javax.ws.rs.core.Response)10 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)8 URLResourceReader (ddf.catalog.resource.impl.URLResourceReader)7 Matchers.anyString (org.mockito.Matchers.anyString)7 InputStream (java.io.InputStream)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6