use of ddf.catalog.source.CatalogProvider in project ddf by codice.
the class FanoutCatalogFrameworkTest method testBlacklistedTagCreateStorageRequestFails.
@Test(expected = IngestException.class)
public void testBlacklistedTagCreateStorageRequestFails() throws Exception {
Metacard metacard = new MetacardImpl();
metacard.setAttribute(new AttributeImpl(Metacard.TAGS, "blacklisted"));
CatalogProvider catalogProvider = mock(CatalogProvider.class);
doReturn(true).when(catalogProvider).isAvailable();
StorageProvider storageProvider = new MockMemoryStorageProvider();
MimeTypeMapper mimeTypeMapper = mock(MimeTypeMapper.class);
doReturn("extension").when(mimeTypeMapper).getFileExtensionForMimeType(anyString());
InputTransformer transformer = mock(InputTransformer.class);
doReturn(metacard).when(transformer).transform(any(InputStream.class));
MimeTypeToTransformerMapper mimeTypeToTransformerMapper = mock(MimeTypeToTransformerMapper.class);
doReturn(Collections.singletonList(transformer)).when(mimeTypeToTransformerMapper).findMatches(any(Class.class), any(MimeType.class));
frameworkProperties.setCatalogProviders(Collections.singletonList(catalogProvider));
frameworkProperties.setStorageProviders(Collections.singletonList(storageProvider));
frameworkProperties.setMimeTypeMapper(mimeTypeMapper);
frameworkProperties.setMimeTypeToTransformerMapper(mimeTypeToTransformerMapper);
OperationsSecuritySupport opsSecurity = new OperationsSecuritySupport();
MetacardFactory metacardFactory = new MetacardFactory(frameworkProperties.getMimeTypeToTransformerMapper(), uuidGenerator);
OperationsMetacardSupport opsMetacard = new OperationsMetacardSupport(frameworkProperties, metacardFactory);
final SourcePoller<SourceStatus> mockStatusSourcePoller = mock(SourcePoller.class);
doAnswer(invocationOnMock -> Optional.of(((Source) invocationOnMock.getArguments()[0]).isAvailable() ? SourceStatus.AVAILABLE : SourceStatus.UNAVAILABLE)).when(mockStatusSourcePoller).getCachedValueForSource(any(Source.class));
SourceOperations sourceOperations = new SourceOperations(frameworkProperties, sourceActionRegistry, mockStatusSourcePoller, mock(SourcePoller.class));
sourceOperations.bind(catalogProvider);
sourceOperations.bind(storageProvider);
TransformOperations transformOperations = new TransformOperations(frameworkProperties);
QueryOperations queryOperations = new QueryOperations(frameworkProperties, sourceOperations, opsSecurity, opsMetacard);
OperationsCatalogStoreSupport opsCatStore = new OperationsCatalogStoreSupport(frameworkProperties, sourceOperations);
OperationsStorageSupport opsStorage = new OperationsStorageSupport(sourceOperations, queryOperations);
ResourceOperations resourceOperations = new ResourceOperations(frameworkProperties, queryOperations, opsSecurity);
OperationsMetacardSupport opsMetacardSupport = new OperationsMetacardSupport(frameworkProperties, metacardFactory);
// Need to set these for InputValidation to work
System.setProperty("bad.files", "none");
System.setProperty("bad.file.extensions", "none");
System.setProperty("bad.mime.types", "none");
System.setProperty("ignore.files", "");
CreateOperations createOperations = new CreateOperations(frameworkProperties, queryOperations, sourceOperations, opsSecurity, opsMetacardSupport, opsCatStore, opsStorage);
framework = new CatalogFrameworkImpl(createOperations, null, null, queryOperations, resourceOperations, sourceOperations, transformOperations);
framework.setId(NEW_SOURCE_ID);
framework.setFanoutEnabled(true);
framework.setFanoutTagBlacklist(Collections.singletonList("blacklisted"));
ContentItem item = new ContentItemImpl(uuidGenerator.generateUuid(), ByteSource.empty(), "text/xml", "filename.xml", 0L, metacard);
CreateStorageRequest request = new CreateStorageRequestImpl(Collections.singletonList(item), new HashMap<>());
framework.create(request);
}
use of ddf.catalog.source.CatalogProvider 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());
}
use of ddf.catalog.source.CatalogProvider in project ddf by codice.
the class CatalogFrameworkImplTest method testPostQueryStopExecution.
@Test(expected = FederationException.class)
public void testPostQueryStopExecution() throws UnsupportedQueryException, FederationException, SourceUnavailableException {
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.setFilterBuilder(new GeotoolsFilterBuilder());
CatalogFrameworkImpl framework = createFramework(props);
framework.query(request);
}
use of ddf.catalog.source.CatalogProvider 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));
}
use of ddf.catalog.source.CatalogProvider in project ddf by codice.
the class CatalogFrameworkImplTest method testNullFederatedQuery.
/**
* Tests that the framework properly throws a catalog exception when the federated query being
* passed in is null.
*
* @throws UnsupportedQueryException
*/
@Test(expected = UnsupportedQueryException.class)
public void testNullFederatedQuery() throws UnsupportedQueryException {
boolean isAvailable = false;
CatalogProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), isAvailable, new Date());
createDefaultFederatedSourceList(isAvailable);
CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, null, true);
try {
framework.query(null, null);
} catch (FederationException e) {
fail();
} catch (SourceUnavailableException e) {
fail();
}
}
Aggregations