Search in sources :

Example 1 with FederationStrategy

use of ddf.catalog.federation.FederationStrategy in project ddf by codice.

the class FanoutCatalogFrameworkTest method testQueryReplacesSourceId.

@Test
public void testQueryReplacesSourceId() throws Exception {
    ConnectedSource source1 = mock(ConnectedSource.class);
    ConnectedSource source2 = mock(ConnectedSource.class);
    when(source1.getId()).thenReturn("source1");
    when(source2.getId()).thenReturn("source2");
    frameworkProperties.setConnectedSources(ImmutableList.of(source1, source2));
    frameworkProperties.setQueryResponsePostProcessor(mock(QueryResponsePostProcessor.class));
    QueryRequestImpl queryRequest = new QueryRequestImpl(mock(Query.class));
    MetacardImpl meta1 = new MetacardImpl();
    MetacardImpl meta2 = new MetacardImpl();
    meta1.setSourceId("source1");
    meta2.setSourceId("source2");
    ResultImpl result1 = new ResultImpl(meta1);
    ResultImpl result2 = new ResultImpl(meta2);
    List<Result> results = new ArrayList<>();
    results.add(result1);
    results.add(result2);
    QueryResponseImpl queryResponse = new QueryResponseImpl(queryRequest, results, 2);
    FederationStrategy strategy = mock(FederationStrategy.class);
    when(strategy.federate(anyList(), any())).thenReturn(queryResponse);
    QueryResponse response = framework.query(queryRequest, strategy);
    for (Result result : response.getResults()) {
        assertEquals(result.getMetacard().getSourceId(), NEW_SOURCE_ID);
    }
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) ConnectedSource(ddf.catalog.source.ConnectedSource) Query(ddf.catalog.operation.Query) FederationStrategy(ddf.catalog.federation.FederationStrategy) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 2 with FederationStrategy

use of ddf.catalog.federation.FederationStrategy in project ddf by codice.

the class CatalogFrameworkImplTest method testPreQueryStopExecution.

@Test(expected = FederationException.class)
public void testPreQueryStopExecution() throws UnsupportedQueryException, FederationException, SourceUnavailableException {
    SourcePoller poller = mock(SourcePoller.class);
    when(poller.getCachedSource(isA(Source.class))).thenReturn(null);
    MockMemoryProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, new Date());
    FederationStrategy federationStrategy = mock(FederationStrategy.class);
    QueryRequest request = mock(QueryRequest.class);
    when(request.getQuery()).thenReturn(mock(Query.class));
    PreQueryPlugin stopQueryPlugin = new PreQueryPlugin() {

        @Override
        public QueryRequest process(QueryRequest input) throws PluginExecutionException, StopProcessingException {
            throw new StopProcessingException("Testing that the framework will stop the query.");
        }
    };
    FrameworkProperties frameworkProperties = new FrameworkProperties();
    frameworkProperties.setSourcePoller(poller);
    frameworkProperties.setPreQuery(Arrays.asList(stopQueryPlugin));
    frameworkProperties.setFederationStrategy(federationStrategy);
    frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
    CatalogFrameworkImpl framework = createFramework(frameworkProperties);
    framework.query(request);
}
Also used : ContentType(ddf.catalog.data.ContentType) QueryRequest(ddf.catalog.operation.QueryRequest) Query(ddf.catalog.operation.Query) FederationStrategy(ddf.catalog.federation.FederationStrategy) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Source(ddf.catalog.source.Source) ByteSource(com.google.common.io.ByteSource) CachedSource(ddf.catalog.util.impl.CachedSource) FederatedSource(ddf.catalog.source.FederatedSource) Date(java.util.Date) SourcePoller(ddf.catalog.util.impl.SourcePoller) PreQueryPlugin(ddf.catalog.plugin.PreQueryPlugin) Test(org.junit.Test)

Example 3 with FederationStrategy

use of ddf.catalog.federation.FederationStrategy in project ddf by codice.

the class QueryOperations method query.

//
// Helper methods
//
QueryResponse query(QueryRequest queryRequest, FederationStrategy strategy, boolean overrideFanoutRename, boolean fanoutEnabled) throws UnsupportedQueryException, FederationException {
    FederationStrategy fedStrategy = strategy;
    QueryResponse queryResponse;
    queryRequest = setFlagsOnRequest(queryRequest);
    try {
        queryRequest = validateQueryRequest(queryRequest);
        queryRequest = getFanoutQuery(queryRequest, fanoutEnabled);
        queryRequest = preProcessPreAuthorizationPlugins(queryRequest);
        queryRequest = populateQueryRequestPolicyMap(queryRequest);
        queryRequest = processPreQueryAccessPlugins(queryRequest);
        queryRequest = processPreQueryPlugins(queryRequest);
        queryRequest = validateQueryRequest(queryRequest);
        if (fedStrategy == null) {
            if (frameworkProperties.getFederationStrategy() == null) {
                throw new FederationException("No Federation Strategies exist.  Cannot execute federated query.");
            } else {
                LOGGER.debug("FederationStrategy was not specified, using default strategy: " + frameworkProperties.getFederationStrategy().getClass());
                fedStrategy = frameworkProperties.getFederationStrategy();
            }
        }
        queryResponse = doQuery(queryRequest, fedStrategy);
        queryResponse = injectAttributes(queryResponse);
        queryResponse = validateFixQueryResponse(queryResponse, overrideFanoutRename, fanoutEnabled);
        queryResponse = postProcessPreAuthorizationPlugins(queryResponse);
        queryResponse = populateQueryResponsePolicyMap(queryResponse);
        queryResponse = processPostQueryAccessPlugins(queryResponse);
        queryResponse = processPostQueryPlugins(queryResponse);
    } catch (RuntimeException re) {
        throw new UnsupportedQueryException("Exception during runtime while performing query", re);
    }
    return queryResponse;
}
Also used : FederationStrategy(ddf.catalog.federation.FederationStrategy) QueryResponse(ddf.catalog.operation.QueryResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException)

Example 4 with FederationStrategy

use of ddf.catalog.federation.FederationStrategy 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);
    // Mock register the provider in the container
    // Mock the source poller
    SourcePoller mockPoller = mock(SourcePoller.class);
    when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
    // 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.setSourcePoller(mockPoller);
    frameworkProperties.setResourceReaders(resourceReaders);
    frameworkProperties.setFederationStrategy(strategy);
    frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
    SourceOperations sourceOps = new SourceOperations(frameworkProperties);
    QueryOperations queryOps = new QueryOperations(frameworkProperties, sourceOps, null, null);
    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) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) URI(java.net.URI) Source(ddf.catalog.source.Source) ByteSource(com.google.common.io.ByteSource) CachedSource(ddf.catalog.util.impl.CachedSource) FederatedSource(ddf.catalog.source.FederatedSource) SourcePoller(ddf.catalog.util.impl.SourcePoller) Result(ddf.catalog.data.Result) HashSet(java.util.HashSet) QueryRequest(ddf.catalog.operation.QueryRequest) SourceOperations(ddf.catalog.impl.operations.SourceOperations) FederationStrategy(ddf.catalog.federation.FederationStrategy) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with FederationStrategy

use of ddf.catalog.federation.FederationStrategy in project ddf by codice.

the class CatalogFrameworkImplTest method createDummyCatalogFramework.

private CatalogFramework createDummyCatalogFramework(CatalogProvider provider, Map<String, CatalogStore> stores, Map<String, FederatedSource> sources, MockEventProcessor eventAdmin) {
    SourcePoller mockPoller = mock(SourcePoller.class);
    when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
    FederationStrategy federationStrategy = new FederationStrategy() {

        @Override
        public QueryResponse federate(List<Source> sources, QueryRequest query) throws FederationException {
            List<Result> results = new ArrayList<>();
            for (Source source : sources) {
                try {
                    SourceResponse response = source.query(query);
                    results.addAll(response.getResults());
                } catch (UnsupportedQueryException e) {
                }
            }
            return new QueryResponseImpl(query, results, results.size());
        }
    };
    ArrayList<PostIngestPlugin> postIngestPlugins = new ArrayList<>();
    postIngestPlugins.add(eventAdmin);
    FrameworkProperties frameworkProperties = new FrameworkProperties();
    frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
    frameworkProperties.setStorageProviders(Collections.singletonList(storageProvider));
    frameworkProperties.setCatalogStoresMap(stores);
    frameworkProperties.setSourcePoller(mockPoller);
    frameworkProperties.setPreIngest(new ArrayList<>());
    frameworkProperties.setPostIngest(postIngestPlugins);
    frameworkProperties.setPreQuery(new ArrayList<>());
    frameworkProperties.setPostQuery(new ArrayList<>());
    frameworkProperties.setPolicyPlugins(new ArrayList<>());
    frameworkProperties.setAccessPlugins(new ArrayList<>());
    frameworkProperties.setFederatedSources(sources);
    frameworkProperties.setConnectedSources(new ArrayList<>());
    frameworkProperties.setFederationStrategy(federationStrategy);
    frameworkProperties.setQueryResponsePostProcessor(new QueryResponsePostProcessor(null, null));
    frameworkProperties.setFilterBuilder(new GeotoolsFilterBuilder());
    frameworkProperties.setValidationQueryFactory(new ValidationQueryFactory(new GeotoolsFilterAdapterImpl(), new GeotoolsFilterBuilder()));
    frameworkProperties.setDefaultAttributeValueRegistry(defaultAttributeValueRegistry);
    return createFramework(frameworkProperties);
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) FederationStrategy(ddf.catalog.federation.FederationStrategy) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) ValidationQueryFactory(ddf.catalog.cache.solr.impl.ValidationQueryFactory) Source(ddf.catalog.source.Source) ByteSource(com.google.common.io.ByteSource) CachedSource(ddf.catalog.util.impl.CachedSource) FederatedSource(ddf.catalog.source.FederatedSource) SourcePoller(ddf.catalog.util.impl.SourcePoller) Result(ddf.catalog.data.Result) PostIngestPlugin(ddf.catalog.plugin.PostIngestPlugin) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.anyList(org.mockito.Matchers.anyList) GeotoolsFilterAdapterImpl(ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl)

Aggregations

FederationStrategy (ddf.catalog.federation.FederationStrategy)8 Result (ddf.catalog.data.Result)6 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 ByteSource (com.google.common.io.ByteSource)5 QueryRequest (ddf.catalog.operation.QueryRequest)5 QueryResponse (ddf.catalog.operation.QueryResponse)5 FederatedSource (ddf.catalog.source.FederatedSource)5 Source (ddf.catalog.source.Source)5 CachedSource (ddf.catalog.util.impl.CachedSource)5 SourcePoller (ddf.catalog.util.impl.SourcePoller)5 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)4 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)4 CatalogProvider (ddf.catalog.source.CatalogProvider)4 Metacard (ddf.catalog.data.Metacard)3 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 ResultImpl (ddf.catalog.data.impl.ResultImpl)3 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)3 ValidationQueryFactory (ddf.catalog.cache.solr.impl.ValidationQueryFactory)2 QueryOperations (ddf.catalog.impl.operations.QueryOperations)2