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);
}
}
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);
}
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;
}
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());
}
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);
}
Aggregations