Search in sources :

Example 1 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class TestResourceMetacardTransformer method getResource.

private Resource getResource(MimeType mimeType, URI uri) throws Exception {
    Resource resource = mock(Resource.class);
    when(resource.getMimeType()).thenReturn(mimeType);
    when(resource.getMimeTypeValue()).thenReturn((mimeType == null) ? null : mimeType.getBaseType());
    when(resource.getInputStream()).thenReturn(uri.toURL().openConnection().getInputStream());
    return resource;
}
Also used : Resource(ddf.catalog.resource.Resource)

Example 2 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class ResourceReaderTest method doVerification.

private ResourceResponse doVerification(URI uri, String filename, String expectedMimeType, Map<String, Serializable> arguments) throws URISyntaxException, IOException, ResourceNotFoundException {
    URLResourceReader resourceReader = new TestURLResourceReader(mimeTypeMapper);
    resourceReader.setRootResourceDirectories(ImmutableSet.of(ABSOLUTE_PATH + TEST_PATH));
    // Test using the URL ResourceReader
    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));
    assertTrue(resource.getMimeType().toString().contains(expectedMimeType));
    return resourceResponse;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Resource(ddf.catalog.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class ResourceReaderTest method verifyFile.

private void verifyFile(String filePath, String filename, String expectedMimeType, String... rootResourceDirectories) throws Exception {
    URLResourceReader resourceReader = new URLResourceReader(mimeTypeMapper);
    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)

Example 4 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class TestCswSource method testRetrieveResourceUsingGetRecordById.

@Test
public void testRetrieveResourceUsingGetRecordById() throws CswException, ResourceNotFoundException, IOException, ResourceNotSupportedException, URISyntaxException {
    Csw csw = createMockCsw();
    CswRecordCollection collection = mock(CswRecordCollection.class);
    Resource resource = mock(Resource.class);
    when(collection.getResource()).thenReturn(resource);
    when(csw.getRecordById(any(GetRecordByIdRequest.class), anyString())).thenReturn(collection);
    AbstractCswSource cswSource = getCswSource(csw, mockContext, null, null, null, null);
    ResourceReader reader = mock(ResourceReader.class);
    when(reader.retrieveResource(any(URI.class), any(Map.class))).thenReturn(mock(ResourceResponse.class));
    cswSource.setResourceReader(reader);
    Map<String, Serializable> props = new HashMap<>();
    props.put(Core.ID, "ID");
    cswSource.retrieveResource(new URI("http://example.com/resource"), props);
    // Verify
    verify(csw, times(1)).getRecordById(any(GetRecordByIdRequest.class), any(String.class));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Resource(ddf.catalog.resource.Resource) Matchers.anyString(org.mockito.Matchers.anyString) GetRecordByIdRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest) URI(java.net.URI) ResourceResponse(ddf.catalog.operation.ResourceResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class TestCswSource method testRetrieveResourceUsingGetRecordByIdWithNoId.

@Test(expected = ResourceNotFoundException.class)
public void testRetrieveResourceUsingGetRecordByIdWithNoId() throws CswException, ResourceNotFoundException, IOException, ResourceNotSupportedException, URISyntaxException {
    Csw csw = createMockCsw();
    CswRecordCollection collection = mock(CswRecordCollection.class);
    Resource resource = mock(Resource.class);
    when(collection.getResource()).thenReturn(resource);
    when(csw.getRecordById(any(GetRecordByIdRequest.class), anyString())).thenReturn(collection);
    AbstractCswSource cswSource = getCswSource(csw, mockContext, null, null, null, null);
    ResourceReader reader = mock(ResourceReader.class);
    when(reader.retrieveResource(any(URI.class), any(Map.class))).thenReturn(mock(ResourceResponse.class));
    cswSource.setResourceReader(reader);
    Map<String, Serializable> props = new HashMap<>();
    cswSource.retrieveResource(new URI("http://example.com/resource"), props);
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Resource(ddf.catalog.resource.Resource) Matchers.anyString(org.mockito.Matchers.anyString) GetRecordByIdRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest) URI(java.net.URI) ResourceResponse(ddf.catalog.operation.ResourceResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Resource (ddf.catalog.resource.Resource)59 ResourceResponse (ddf.catalog.operation.ResourceResponse)29 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ResourceRequest (ddf.catalog.operation.ResourceRequest)14 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)13 URI (java.net.URI)11 HashMap (java.util.HashMap)11 InputStream (java.io.InputStream)10 Serializable (java.io.Serializable)10 MimeType (javax.activation.MimeType)10 Result (ddf.catalog.data.Result)9 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)9 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)9 IOException (java.io.IOException)9 Map (java.util.Map)9 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)8 Metacard (ddf.catalog.data.Metacard)7