use of ddf.catalog.util.impl.SourcePoller in project ddf by codice.
the class CatalogFrameworkImplTest method testGetFederatedSourcesDuplicates.
@Test
public void testGetFederatedSourcesDuplicates() {
List<FederatedSource> federatedSources = createDefaultFederatedSourceList(true);
// Duplicate Site
FederatedSource siteC2 = new MockSource("C", "Site C2", "v1.0", "DDF", null, true, new Date());
federatedSources.add(siteC2);
// Expected Sites
List<FederatedSource> expectedSources = createDefaultFederatedSourceList(true);
// Mock register the federated sources in the container
// Mock the source poller
SourcePoller mockPoller = mock(SourcePoller.class);
when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
FrameworkProperties frameworkProperties = new FrameworkProperties();
frameworkProperties.setSourcePoller(mockPoller);
Map<String, FederatedSource> sources = new HashMap<>();
for (FederatedSource federatedSource : expectedSources) {
sources.put(federatedSource.getId(), federatedSource);
}
frameworkProperties.setFederatedSources(sources);
CatalogFrameworkImpl framework = createFramework(frameworkProperties);
// Returned Sites
SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
SourceInfoResponse response = null;
try {
response = framework.getSourceInfo(request);
} catch (SourceUnavailableException e) {
LOGGER.debug("SourceUnavilable", e);
fail();
}
Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
// should contain ONLY the original federated sites and the catalog framework's
// site info (even though it has no local catalog provider configured) - hence,
// the "+1"
assertEquals(expectedSources.size(), sourceDescriptors.size());
}
use of ddf.catalog.util.impl.SourcePoller in project ddf by codice.
the class CatalogFrameworkImplTest method setup.
@Before
public void setup() throws StopProcessingException, PluginExecutionException, URISyntaxException, FederationException, IOException, CatalogTransformerException, InterruptedException {
System.setProperty("bad.files", "crossdomain.xml,clientaccesspolicy.xml,.htaccess,.htpasswd,hosts,passwd,group,resolv.conf,nfs.conf,ftpd.conf,ntp.conf,web.config,robots.txt");
System.setProperty("bad.file.extensions", ".exe,.jsp,.html,.js,.php,.phtml,.php3,.php4,.php5,.phps,.shtml,.jhtml,.pl,.py,.cgi,.msi,.com,.scr,.gadget,.application,.pif,.hta,.cpl,.msc,.jar,.kar,.bat,.cmd,.vb,.vbs,.vbe,.jse,.ws,.wsf,.wsc,.wsh,.ps1,.ps1xml,.ps2,.ps2xml,.psc1,.psc2,.msh,.msh1,.msh2,.mshxml,.msh1xml,.msh2xml,.scf,.lnk,.inf,.reg,.dll,.vxd,.cpl,.cfg,.config,.crt,.cert,.pem,.jks,.p12,.p7b,.key,.der,.csr,.jsb,.mhtml,.mht,.xhtml,.xht");
System.setProperty("bad.mime.types", "text/html,text/javascript,text/x-javascript,application/x-shellscript,text/scriptlet,application/x-msdownload,application/x-msmetafile");
// Setup
/*
* Prepare to capture the ResourceResponse argument passed into
* PostResourcePlugin.process(). We will verify that it contains a non-null ResourceRequest
* in the verification section of this test.
*/
argument = ArgumentCaptor.forClass(ResourceResponse.class);
Resource mockResource = mock(Resource.class);
mockResourceRequest = mock(ResourceRequest.class);
when(mockResourceRequest.getAttributeValue()).thenReturn(new URI("myURI"));
when(mockResourceRequest.getAttributeName()).thenReturn(new String("myName"));
mockResourceResponse = mock(ResourceResponse.class);
when(mockResourceResponse.getRequest()).thenReturn(mockResourceRequest);
when(mockResourceResponse.getResource()).thenReturn(mockResource);
mockPostResourcePlugin = mock(PostResourcePlugin.class);
/*
* We verify (see verification section of test) that PostResourcePlugin.process() receives a
* ResourceResponse with a non-null ResourceRequest. We assume that it works correctly and
* returns a ResourceResponse with a non-null ResourceRequest, so we return our
* mockResouceResponse that contains a non-null ResourceRequest.
*/
when(mockPostResourcePlugin.process(isA(ResourceResponse.class))).thenReturn(mockResourceResponse);
List<PostResourcePlugin> mockPostResourcePlugins = new ArrayList<PostResourcePlugin>();
mockPostResourcePlugins.add(mockPostResourcePlugin);
eventAdmin = new MockEventProcessor();
provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<>(), true, new Date());
storageProvider = new MockMemoryStorageProvider();
// Mock register the provider in the container
// Mock the source poller
SourcePoller mockPoller = mock(SourcePoller.class);
when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
ArrayList<PostIngestPlugin> postIngestPlugins = new ArrayList<PostIngestPlugin>();
postIngestPlugins.add(eventAdmin);
mockFederationStrategy = mock(FederationStrategy.class);
Result mockFederationResult = mock(Result.class);
when(mockFederationResult.getMetacard()).thenReturn(new MetacardImpl());
QueryRequest mockQueryRequest = mock(QueryRequest.class);
Query mockQuery = mock(Query.class);
when(mockQuery.getTimeoutMillis()).thenReturn(1L);
when(mockQueryRequest.getQuery()).thenReturn(mockQuery);
QueryResponseImpl queryResponse = new QueryResponseImpl(mockQueryRequest, Collections.singletonList(mockFederationResult), 1);
when(mockFederationStrategy.federate(anyList(), anyObject())).thenReturn(queryResponse);
federatedSources = createDefaultFederatedSourceList(true);
MimeTypeResolver mimeTypeResolver = mock(MimeTypeResolver.class);
MimeTypeToTransformerMapper mimeTypeToTransformerMapper = mock(MimeTypeToTransformerMapper.class);
InputTransformer inputTransformer = mock(InputTransformer.class);
when(inputTransformer.transform(any(InputStream.class))).thenReturn(new MetacardImpl());
when(mimeTypeToTransformerMapper.findMatches(any(Class.class), any(MimeType.class))).thenReturn(Collections.singletonList(inputTransformer));
mockRemoteDeleteOperations = mock(RemoteDeleteOperations.class);
FrameworkProperties frameworkProperties = new FrameworkProperties();
frameworkProperties.setAccessPlugins(new ArrayList<>());
frameworkProperties.setPolicyPlugins(new ArrayList<>());
frameworkProperties.setSourcePoller(mockPoller);
frameworkProperties.setCatalogProviders(Collections.singletonList((CatalogProvider) provider));
frameworkProperties.setPostResource(mockPostResourcePlugins);
frameworkProperties.setFederationStrategy(mockFederationStrategy);
frameworkProperties.setFilterBuilder(new GeotoolsFilterBuilder());
frameworkProperties.setPreIngest(new ArrayList<>());
frameworkProperties.setPostIngest(postIngestPlugins);
frameworkProperties.setPreQuery(new ArrayList<>());
frameworkProperties.setPostQuery(new ArrayList<>());
frameworkProperties.setPreResource(new ArrayList<>());
frameworkProperties.setPostResource(new ArrayList<>());
frameworkProperties.setQueryResponsePostProcessor(mock(QueryResponsePostProcessor.class));
frameworkProperties.setStorageProviders(Collections.singletonList(storageProvider));
frameworkProperties.setMimeTypeMapper(new MimeTypeMapperImpl(Collections.singletonList(mimeTypeResolver)));
frameworkProperties.setMimeTypeToTransformerMapper(mimeTypeToTransformerMapper);
frameworkProperties.setValidationQueryFactory(new ValidationQueryFactory(new GeotoolsFilterAdapterImpl(), new GeotoolsFilterBuilder()));
Map<String, FederatedSource> federatedSourceMap = new HashMap<>();
if (federatedSources != null) {
for (FederatedSource source : federatedSources) {
federatedSourceMap.put(source.getId(), source);
}
}
SourcePollerRunner runner = new SourcePollerRunner();
SourcePoller poller = new SourcePoller(runner);
for (FederatedSource source : federatedSources) {
runner.bind(source);
}
runner.bind(provider);
int wait = 0;
while (wait < 5) {
for (FederatedSource source : federatedSources) {
CachedSource cachedSource = poller.getCachedSource(source);
if (cachedSource == null || !cachedSource.isAvailable()) {
Thread.sleep(100);
wait++;
break;
}
}
CachedSource cachedProvider = poller.getCachedSource(provider);
if (cachedProvider == null || !cachedProvider.isAvailable()) {
Thread.sleep(100);
}
wait++;
}
frameworkProperties.setSourcePoller(poller);
frameworkProperties.setFederatedSources(federatedSourceMap);
defaultAttributeValueRegistry = new DefaultAttributeValueRegistryImpl();
frameworkProperties.setDefaultAttributeValueRegistry(defaultAttributeValueRegistry);
attributeInjector = spy(new AttributeInjectorImpl(new AttributeRegistryImpl()));
frameworkProperties.setAttributeInjectors(Collections.singletonList(attributeInjector));
uuidGenerator = mock(UuidGenerator.class);
when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
OperationsSecuritySupport opsSecurity = new OperationsSecuritySupport();
MetacardFactory metacardFactory = new MetacardFactory(mimeTypeToTransformerMapper, uuidGenerator);
OperationsMetacardSupport opsMetacard = new OperationsMetacardSupport(frameworkProperties, metacardFactory);
SourceOperations sourceOperations = new SourceOperations(frameworkProperties);
TransformOperations transformOperations = new TransformOperations(frameworkProperties);
Historian historian = new Historian();
historian.setHistoryEnabled(false);
QueryOperations queryOperations = new QueryOperations(frameworkProperties, sourceOperations, opsSecurity, opsMetacard);
OperationsStorageSupport opsStorage = new OperationsStorageSupport(sourceOperations, queryOperations);
opsStorage.setHistorian(historian);
OperationsCatalogStoreSupport opsCatStore = new OperationsCatalogStoreSupport(frameworkProperties, sourceOperations);
CreateOperations createOperations = new CreateOperations(frameworkProperties, queryOperations, sourceOperations, opsSecurity, opsMetacard, opsCatStore, opsStorage);
UpdateOperations updateOperations = new UpdateOperations(frameworkProperties, queryOperations, sourceOperations, opsSecurity, opsMetacard, opsCatStore, opsStorage);
deleteOperations = new DeleteOperations(frameworkProperties, queryOperations, sourceOperations, opsSecurity, opsMetacard);
deleteOperations.setOpsCatStoreSupport(opsCatStore);
ResourceOperations resOps = new ResourceOperations(frameworkProperties, queryOperations, opsSecurity) {
@Override
protected ResourceInfo getResourceInfo(ResourceRequest resourceRequest, String site, boolean isEnterprise, StringBuilder federatedSite, Map<String, Serializable> requestProperties, boolean fanoutEnabled) throws ResourceNotSupportedException, ResourceNotFoundException {
URI uri = null;
Metacard metacard = new MetacardImpl();
try {
uri = new URI("myURI");
} catch (URISyntaxException e) {
}
return new ResourceInfo(metacard, uri);
}
};
updateOperations.setHistorian(historian);
deleteOperations.setHistorian(historian);
framework = new CatalogFrameworkImpl(createOperations, updateOperations, deleteOperations, queryOperations, resOps, sourceOperations, transformOperations);
// Conditionally bind objects if framework properties are setup
if (!CollectionUtils.isEmpty(frameworkProperties.getCatalogProviders())) {
sourceOperations.bind(provider);
}
sourceOperations.bind(storageProvider);
resourceFramework = new CatalogFrameworkImpl(createOperations, updateOperations, deleteOperations, queryOperations, resOps, sourceOperations, transformOperations);
// Conditionally bind objects if framework properties are setup
if (!CollectionUtils.isEmpty(frameworkProperties.getCatalogProviders())) {
sourceOperations.bind(provider);
}
sourceOperations.bind(storageProvider);
ThreadContext.bind(mock(Subject.class));
}
use of ddf.catalog.util.impl.SourcePoller 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);
}
use of ddf.catalog.util.impl.SourcePoller in project ddf by codice.
the class CatalogFrameworkImplTest method testPostQueryStopExecution.
@Test(expected = FederationException.class)
public void testPostQueryStopExecution() throws UnsupportedQueryException, FederationException, SourceUnavailableException {
SourcePoller poller = mock(SourcePoller.class);
when(poller.getCachedSource(isA(Source.class))).thenReturn(null);
BundleContext context = null;
FilterFactory filterFactory = new FilterFactoryImpl();
Filter filter = filterFactory.like(filterFactory.property(Metacard.METADATA), "goodyear", "*", "?", "/", false);
QueryRequest request = new QueryRequestImpl(new QueryImpl(filter));
SourceResponseImpl sourceResponse = new SourceResponseImpl(request, new ArrayList<Result>());
QueryResponseImpl queryResponse = new QueryResponseImpl(sourceResponse, "anyId");
CatalogProvider provider = mock(CatalogProvider.class);
when(provider.query(isA(QueryRequest.class))).thenReturn(sourceResponse);
FederationStrategy federationStrategy = mock(FederationStrategy.class);
when(federationStrategy.federate(isA(List.class), isA(QueryRequest.class))).thenReturn(queryResponse);
PostQueryPlugin stopQueryPlugin = new PostQueryPlugin() {
@Override
public QueryResponse process(QueryResponse input) throws PluginExecutionException, StopProcessingException {
throw new StopProcessingException("Testing that the framework will stop the query.");
}
};
FrameworkProperties props = new FrameworkProperties();
props.setCatalogProviders(Collections.singletonList((CatalogProvider) provider));
props.setBundleContext(context);
props.setPostQuery(Arrays.asList(stopQueryPlugin));
props.setFederationStrategy(federationStrategy);
props.setQueryResponsePostProcessor(mock(QueryResponsePostProcessor.class));
props.setSourcePoller(poller);
props.setFilterBuilder(new GeotoolsFilterBuilder());
CatalogFrameworkImpl framework = createFramework(props);
framework.query(request);
}
use of ddf.catalog.util.impl.SourcePoller in project ddf by codice.
the class CatalogFrameworkImplTest method testGetUnavailableFederatedSources.
@Test
public void testGetUnavailableFederatedSources() {
List<FederatedSource> federatedSources = createDefaultFederatedSourceList(false);
CatalogProvider catalogProvider = mock(CatalogProvider.class);
// Mock register the federated sources in the container
SourcePollerRunner runner = new SourcePollerRunner();
SourcePoller poller = new SourcePoller(runner);
for (FederatedSource source : federatedSources) {
runner.bind(source);
}
runner.bind(catalogProvider);
FrameworkProperties frameworkProperties = new FrameworkProperties();
frameworkProperties.setSourcePoller(poller);
Map<String, FederatedSource> sources = new HashMap<>();
for (FederatedSource federatedSource : federatedSources) {
sources.put(federatedSource.getId(), federatedSource);
}
frameworkProperties.setFederatedSources(sources);
frameworkProperties.setCatalogProviders(Collections.singletonList(catalogProvider));
CatalogFrameworkImpl framework = createFramework(frameworkProperties);
SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
SourceInfoResponse response = null;
try {
response = framework.getSourceInfo(request);
} catch (SourceUnavailableException e) {
fail();
}
Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
for (SourceDescriptor descriptor : sourceDescriptors) {
LOGGER.debug("Descriptor id: {}", descriptor.getSourceId());
if (StringUtils.isNotBlank(descriptor.getId())) {
assertFalse(descriptor.isAvailable());
// No contentTypes should be listed if the source is unavailable
assertTrue(descriptor.getContentTypes().isEmpty());
}
}
// The "+1" is to account for the CatalogFramework source descriptor.
// Even if no local catalog provider is configured, the catalog
// framework's
// site info is included in the SourceDescriptos list.
assertEquals(federatedSources.size() + 1, sourceDescriptors.size());
}
Aggregations