Search in sources :

Example 11 with ResourceReader

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

the class OpenSearchSourceTest method testRetrieveResource.

/**
 * Basic retrieve product case. Tests the url sent to the connection is correct.
 */
@Test
public void testRetrieveResource() throws Exception {
    // given
    ResourceReader mockReader = mock(ResourceReader.class);
    when(response.getEntity()).thenReturn(getBinaryData());
    when(mockReader.retrieveResource(any(URI.class), any(Map.class))).thenReturn(new ResourceResponseImpl(new ResourceImpl(getBinaryData(), "")));
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList("application/octet-stream"));
    when(response.getHeaders()).thenReturn(headers);
    source.setLocalQueryOnly(true);
    source.setResourceReader(mockReader);
    Map<String, Serializable> requestProperties = new HashMap<>();
    requestProperties.put(ID_ATTRIBUTE_NAME, SAMPLE_ID);
    // when
    ResourceResponse response = source.retrieveResource(null, requestProperties);
    // then
    assertThat(response.getResource().getByteArray().length, is(3));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 12 with ResourceReader

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

the class OpenSearchSourceTest method testRetrieveResourceBasicAuth.

/**
 * Retrieve Product case using Basic Authentication. Test that the properties map passed to the
 * resource reader includes a username and password.
 */
@Test
public void testRetrieveResourceBasicAuth() throws Exception {
    ResourceReader mockReader = mock(ResourceReader.class);
    when(response.getEntity()).thenReturn(getBinaryData());
    when(mockReader.retrieveResource(any(URI.class), argThat(allOf(hasEntry("username", (Serializable) "user"), hasEntry("password", (Serializable) "secret"))))).thenReturn(new ResourceResponseImpl(new ResourceImpl(getBinaryData(), "")));
    when(encryptionService.decryptValue("secret")).thenReturn("secret");
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList("application/octet-stream"));
    when(response.getHeaders()).thenReturn(headers);
    source.setLocalQueryOnly(true);
    source.setResourceReader(mockReader);
    source.setUsername("user");
    source.setPassword("secret");
    Map<String, Serializable> requestProperties = new HashMap<>();
    requestProperties.put(ID_ATTRIBUTE_NAME, SAMPLE_ID);
    ResourceResponse response = source.retrieveResource(null, requestProperties);
    assertThat(response.getResource().getByteArray().length, is(3));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Serializable(java.io.Serializable) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) Test(org.junit.Test)

Example 13 with ResourceReader

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

the class CatalogFrameworkImplTest method testGetResourceToTestSecondResourceReaderWithSameSchemeGetsCalledIfFirstDoesNotReturnAnything.

/**
 * Tests that multiple ResourceReaders with the same scheme will be invoked if the first one did
 * not return a Response.
 *
 * @throws Exception
 */
@Test
// CACHE
@Ignore
public void testGetResourceToTestSecondResourceReaderWithSameSchemeGetsCalledIfFirstDoesNotReturnAnything() throws Exception {
    String localProviderName = "ddf";
    final String EXPECTED = "result from mockResourceResponse2";
    final String DDF = "ddf";
    // Mock a Catalog Provider
    CatalogProvider provider = mock(CatalogProvider.class);
    when(provider.getId()).thenReturn(localProviderName);
    when(provider.isAvailable(isA(SourceMonitor.class))).thenReturn(true);
    when(provider.isAvailable()).thenReturn(true);
    // Create two ResourceReaders. The first should not return anything
    // and the second should.
    ResourceReader resourceReader1 = mock(ResourceReader.class);
    ResourceReader resourceReader2 = mock(ResourceReader.class);
    // Set the supported Schemes so that both ResourceReaders use
    // the same scheme ("DAD")
    Set<String> supportedSchemes = new HashSet<String>();
    supportedSchemes.add("DAD");
    when(resourceReader1.getSupportedSchemes()).thenReturn(supportedSchemes);
    when(resourceReader2.getSupportedSchemes()).thenReturn(supportedSchemes);
    List<ResourceReader> resourceReaders = new ArrayList<ResourceReader>();
    resourceReaders.add(resourceReader1);
    resourceReaders.add(resourceReader2);
    // Set up the requests and responses. The first ResourceReader will return null
    // and the second one will retrieve a value, showing that if more than one
    // ResourceReader with the same scheme are used, they will be called until a
    // response is returned
    ResourceRequest mockResourceRequest = mock(ResourceRequest.class);
    URI myURI = new URI("DAD", "host", "/path", "fragment");
    when(mockResourceRequest.getAttributeValue()).thenReturn(myURI);
    when(mockResourceRequest.getAttributeName()).thenReturn(new String(ResourceRequest.GET_RESOURCE_BY_PRODUCT_URI));
    Result result = mock(Result.class);
    Metacard metacard = mock(Metacard.class);
    when(metacard.getResourceURI()).thenReturn(myURI);
    when(result.getMetacard()).thenReturn(metacard);
    List<Result> results = new ArrayList<Result>();
    results.add(result);
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getResults()).thenReturn(results);
    List<Source> federatedSources = new ArrayList<Source>();
    FederationStrategy strategy = mock(FederationStrategy.class);
    when(strategy.federate(isA(federatedSources.getClass()), isA(QueryRequest.class))).thenReturn(queryResponse);
    ResourceResponse mockResourceResponse1 = mock(ResourceResponse.class);
    when(mockResourceResponse1.getRequest()).thenReturn(mockResourceRequest);
    when(mockResourceResponse1.getResource()).thenReturn(null);
    when(resourceReader1.retrieveResource(any(URI.class), anyMap())).thenReturn(null);
    Resource mockResource = mock(Resource.class);
    when(mockResource.getName()).thenReturn(EXPECTED);
    ResourceResponse mockResourceResponse2 = mock(ResourceResponse.class);
    when(mockResourceResponse2.getResource()).thenReturn(mockResource);
    when(resourceReader2.retrieveResource(any(URI.class), anyMap())).thenReturn(mockResourceResponse2);
    FrameworkProperties frameworkProperties = new FrameworkProperties();
    frameworkProperties.setResourceReaders(resourceReaders);
    frameworkProperties.setFederationStrategy(strategy);
    frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
    final SourcePoller<SourceStatus> mockStatusSourcePoller = mock(SourcePoller.class);
    when(mockStatusSourcePoller.getCachedValueForSource(isA(Source.class))).thenReturn(Optional.empty());
    final SourcePoller<Set<ContentType>> mockContentTypesSourcePoller = mock(SourcePoller.class);
    when(mockContentTypesSourcePoller.getCachedValueForSource(isA(Source.class))).thenReturn(Optional.empty());
    SourceOperations sourceOps = new SourceOperations(frameworkProperties, sourceActionRegistry, mockStatusSourcePoller, mockContentTypesSourcePoller);
    QueryOperations queryOps = new QueryOperations(frameworkProperties, sourceOps, null, null);
    queryOps.setSecurityLogger(mock(SecurityLogger.class));
    queryOps.setPermissions(new PermissionsImpl());
    ResourceOperations resOps = new ResourceOperations(frameworkProperties, queryOps, null);
    resOps.setId(DDF);
    CatalogFrameworkImpl catalogFramework = new CatalogFrameworkImpl(null, null, null, null, resOps, null, null);
    sourceOps.bind(provider);
    ResourceResponse response = catalogFramework.getResource(mockResourceRequest, DDF);
    // Verify that the Response is as expected
    org.junit.Assert.assertEquals(EXPECTED, response.getResource().getName());
    // Verify that resourceReader1 was called 1 time
    // This line is equivalent to verify(resourceReader1,
    // times(1)).retrieveResource(any(URI.class), anyMap());
    verify(resourceReader1).retrieveResource(any(URI.class), anyMap());
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) Source(ddf.catalog.source.Source) ByteSource(com.google.common.io.ByteSource) FederatedSource(ddf.catalog.source.FederatedSource) Result(ddf.catalog.data.Result) PermissionsImpl(ddf.security.permission.impl.PermissionsImpl) HashSet(java.util.HashSet) QueryRequest(ddf.catalog.operation.QueryRequest) SourceOperations(ddf.catalog.impl.operations.SourceOperations) FederationStrategy(ddf.catalog.federation.FederationStrategy) SourceStatus(org.codice.ddf.catalog.sourcepoller.SourceStatus) ResourceOperations(ddf.catalog.impl.operations.ResourceOperations) Resource(ddf.catalog.resource.Resource) SourceMonitor(ddf.catalog.source.SourceMonitor) Metacard(ddf.catalog.data.Metacard) ResourceResponse(ddf.catalog.operation.ResourceResponse) CatalogProvider(ddf.catalog.source.CatalogProvider) QueryOperations(ddf.catalog.impl.operations.QueryOperations) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceRequest(ddf.catalog.operation.ResourceRequest) SecurityLogger(ddf.security.audit.SecurityLogger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with ResourceReader

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

the class CatalogFrameworkImplTest method testGetResourceOptions.

/**
 * Tests that you can get a resource's (product) options. Covers the case where the source ID
 * specified is actually the local catalog provider's site name (so this reduces down to a
 * getResourceOptions for local provider); and the case where a federated source is specified.
 *
 * <p>Test for DDF-1763.
 *
 * @throws Exception
 */
@Test
public void testGetResourceOptions() throws Exception {
    String localProviderName = "ddf";
    String federatedSite1Name = "fed-site-1";
    String metacardId = "123";
    // The resource's URI
    URI metacardUri = new URI("http:///27+Nov+12+12%3A30%3A04?MyPhotograph%0Ahttp%3A%2F%2F172.18.14.53%3A8080%2Fabc%2Fimages%2FActionable.jpg%0AMyAttachment%0Ahttp%3A%2F%2F172.18.14.53%3A8080%2Fabc#abc.xyz.dao.URLResourceOptionDataAccessObject");
    Set<String> supportedOptions = new HashSet<String>();
    supportedOptions.add("MyPhotograph");
    supportedOptions.add("MyAttachment");
    // Catalog Provider
    CatalogProvider provider = mock(CatalogProvider.class);
    when(provider.getId()).thenReturn(localProviderName);
    when(provider.isAvailable(isA(SourceMonitor.class))).thenReturn(true);
    when(provider.isAvailable()).thenReturn(true);
    // Federated Source 1
    FederatedSource federatedSource1 = mock(FederatedSource.class);
    when(federatedSource1.getId()).thenReturn(federatedSite1Name);
    when(federatedSource1.isAvailable(isA(SourceMonitor.class))).thenReturn(true);
    when(federatedSource1.isAvailable()).thenReturn(true);
    when(federatedSource1.getOptions(isA(Metacard.class))).thenReturn(supportedOptions);
    List<FederatedSource> federatedSources = new ArrayList<FederatedSource>();
    federatedSources.add(federatedSource1);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId(metacardId);
    metacard.setResourceURI(metacardUri);
    Result result = new ResultImpl(metacard);
    List<Result> results = new ArrayList<>();
    results.add(result);
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getResults()).thenReturn(results);
    FederationStrategy strategy = mock(FederationStrategy.class);
    when(strategy.federate(isA(federatedSources.getClass()), isA(QueryRequest.class))).thenReturn(queryResponse);
    ResourceReader resourceReader = mock(ResourceReader.class);
    Set<String> supportedSchemes = new HashSet<String>();
    supportedSchemes.add("http");
    when(resourceReader.getSupportedSchemes()).thenReturn(supportedSchemes);
    when(resourceReader.getOptions(isA(Metacard.class))).thenReturn(supportedOptions);
    List<ResourceReader> resourceReaders = new ArrayList<ResourceReader>();
    resourceReaders.add(resourceReader);
    FrameworkProperties props = new FrameworkProperties();
    props.setCatalogProviders(Collections.singletonList((CatalogProvider) provider));
    props.setFederatedSources(Collections.singletonList(federatedSource1));
    props.setResourceReaders(resourceReaders);
    props.setFederationStrategy(strategy);
    props.setQueryResponsePostProcessor(mock(QueryResponsePostProcessor.class));
    props.setFilterBuilder(new GeotoolsFilterBuilder());
    props.setDefaultAttributeValueRegistry(defaultAttributeValueRegistry);
    CatalogFrameworkImpl framework = createFramework(props);
    framework.setId("ddf");
    Set<String> ids = new HashSet<String>();
    for (FederatedSource source : federatedSources) {
        ids.add(source.getId());
    }
    ids.add(framework.getId());
    // site name = local provider
    Map<String, Set<String>> optionsMap = framework.getResourceOptions(metacardId, localProviderName);
    LOGGER.debug("localProvider optionsMap = {}", optionsMap);
    assertThat(optionsMap, hasEntry("RESOURCE_OPTION", supportedOptions));
    // site name = federated site's name
    optionsMap = framework.getResourceOptions(metacardId, federatedSite1Name);
    LOGGER.debug("federatedSource optionsMap = {}", optionsMap);
    assertThat(optionsMap, hasEntry("RESOURCE_OPTION", supportedOptions));
    // site name = null (should default to local provider)
    optionsMap = framework.getResourceOptions(metacardId, null);
    LOGGER.debug("localProvider optionsMap = {}", optionsMap);
    assertThat(optionsMap, hasEntry("RESOURCE_OPTION", supportedOptions));
    // site name = empty string (should default to local provider)
    optionsMap = framework.getResourceOptions(metacardId, "");
    LOGGER.debug("localProvider optionsMap = {}", optionsMap);
    assertThat(optionsMap, hasEntry("RESOURCE_OPTION", supportedOptions));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) Result(ddf.catalog.data.Result) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) HashSet(java.util.HashSet) QueryRequest(ddf.catalog.operation.QueryRequest) FederationStrategy(ddf.catalog.federation.FederationStrategy) SourceMonitor(ddf.catalog.source.SourceMonitor) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FederatedSource(ddf.catalog.source.FederatedSource) Metacard(ddf.catalog.data.Metacard) CatalogProvider(ddf.catalog.source.CatalogProvider) QueryResponse(ddf.catalog.operation.QueryResponse) Test(org.junit.Test)

Example 15 with ResourceReader

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

the class CswSourceTest 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, permissions);
    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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.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) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

ResourceReader (ddf.catalog.resource.ResourceReader)15 URI (java.net.URI)14 ResourceResponse (ddf.catalog.operation.ResourceResponse)12 HashMap (java.util.HashMap)12 Test (org.junit.Test)12 Serializable (java.io.Serializable)11 Map (java.util.Map)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 Resource (ddf.catalog.resource.Resource)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)3 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)3 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)3 Csw (org.codice.ddf.spatial.ogc.csw.catalog.common.Csw)3 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)3 GetRecordByIdRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest)3 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)3 Matchers.anyMap (org.mockito.Matchers.anyMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Metacard (ddf.catalog.data.Metacard)2