Search in sources :

Example 6 with SourceStatus

use of org.codice.ddf.catalog.sourcepoller.SourceStatus 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);
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) OperationsCatalogStoreSupport(ddf.catalog.impl.operations.OperationsCatalogStoreSupport) MimeTypeMapper(ddf.mime.MimeTypeMapper) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) MockMemoryStorageProvider(ddf.catalog.content.impl.MockMemoryStorageProvider) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) FederatedSource(ddf.catalog.source.FederatedSource) Source(ddf.catalog.source.Source) ConnectedSource(ddf.catalog.source.ConnectedSource) ByteSource(com.google.common.io.ByteSource) SourcePoller(org.codice.ddf.catalog.sourcepoller.SourcePoller) MetacardFactory(ddf.catalog.impl.operations.MetacardFactory) OperationsStorageSupport(ddf.catalog.impl.operations.OperationsStorageSupport) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) SourceOperations(ddf.catalog.impl.operations.SourceOperations) SourceStatus(org.codice.ddf.catalog.sourcepoller.SourceStatus) InputStream(java.io.InputStream) ResourceOperations(ddf.catalog.impl.operations.ResourceOperations) StorageProvider(ddf.catalog.content.StorageProvider) MockMemoryStorageProvider(ddf.catalog.content.impl.MockMemoryStorageProvider) TransformOperations(ddf.catalog.impl.operations.TransformOperations) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) CatalogProvider(ddf.catalog.source.CatalogProvider) QueryOperations(ddf.catalog.impl.operations.QueryOperations) OperationsSecuritySupport(ddf.catalog.impl.operations.OperationsSecuritySupport) CreateOperations(ddf.catalog.impl.operations.CreateOperations) OperationsMetacardSupport(ddf.catalog.impl.operations.OperationsMetacardSupport) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) Test(org.junit.Test)

Example 7 with SourceStatus

use of org.codice.ddf.catalog.sourcepoller.SourceStatus in project ddf by codice.

the class CatalogFrameworkQueryTest method initFramework.

@Before
public void initFramework() {
    MockMemoryProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, new Date());
    final SourcePoller<SourceStatus> mockStatusSourcePoller = mock(SourcePoller.class);
    when(mockStatusSourcePoller.getCachedValueForSource(isA(Source.class))).thenReturn(Optional.of(SourceStatus.AVAILABLE));
    ArrayList<PostIngestPlugin> postIngestPlugins = new ArrayList<>();
    FrameworkProperties props = new FrameworkProperties();
    props.setCatalogProviders(Collections.singletonList(provider));
    props.setPostIngest(postIngestPlugins);
    props.setFederationStrategy(new MockFederationStrategy());
    props.setQueryResponsePostProcessor(mock(QueryResponsePostProcessor.class));
    props.setFilterBuilder(new GeotoolsFilterBuilder());
    props.setDefaultAttributeValueRegistry(new DefaultAttributeValueRegistryImpl());
    UuidGenerator uuidGenerator = mock(UuidGenerator.class);
    when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
    ActionRegistry sourceActionRegistry = mock(ActionRegistry.class);
    when(sourceActionRegistry.list(any())).thenReturn(Collections.emptyList());
    OperationsSecuritySupport opsSecurity = new OperationsSecuritySupport();
    MetacardFactory metacardFactory = new MetacardFactory(props.getMimeTypeToTransformerMapper(), uuidGenerator);
    OperationsMetacardSupport opsMetacard = new OperationsMetacardSupport(props, metacardFactory);
    SourceOperations sourceOperations = new SourceOperations(props, sourceActionRegistry, mockStatusSourcePoller, mock(SourcePoller.class));
    QueryOperations queryOperations = new QueryOperations(props, sourceOperations, opsSecurity, opsMetacard);
    ResourceOperations resourceOperations = new ResourceOperations(props, queryOperations, opsSecurity);
    TransformOperations transformOperations = new TransformOperations(props);
    OperationsCatalogStoreSupport opsCatStore = new OperationsCatalogStoreSupport(props, sourceOperations);
    OperationsStorageSupport opsStorage = new OperationsStorageSupport(sourceOperations, queryOperations);
    CreateOperations createOperations = new CreateOperations(props, queryOperations, sourceOperations, opsSecurity, opsMetacard, opsCatStore, opsStorage);
    UpdateOperations updateOperations = new UpdateOperations(props, queryOperations, sourceOperations, opsSecurity, opsMetacard, opsCatStore, opsStorage);
    DeleteOperations deleteOperations = new DeleteOperations(props, queryOperations, sourceOperations, opsSecurity, opsMetacard);
    Historian historian = new Historian();
    historian.setHistoryEnabled(false);
    updateOperations.setHistorian(historian);
    deleteOperations.setHistorian(historian);
    framework = new CatalogFrameworkImpl(createOperations, updateOperations, deleteOperations, queryOperations, resourceOperations, sourceOperations, transformOperations);
    sourceOperations.bind(provider);
}
Also used : OperationsCatalogStoreSupport(ddf.catalog.impl.operations.OperationsCatalogStoreSupport) UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) ContentType(ddf.catalog.data.ContentType) ArrayList(java.util.ArrayList) DeleteOperations(ddf.catalog.impl.operations.DeleteOperations) ActionRegistry(ddf.action.ActionRegistry) Source(ddf.catalog.source.Source) PostIngestPlugin(ddf.catalog.plugin.PostIngestPlugin) SourcePoller(org.codice.ddf.catalog.sourcepoller.SourcePoller) MetacardFactory(ddf.catalog.impl.operations.MetacardFactory) OperationsStorageSupport(ddf.catalog.impl.operations.OperationsStorageSupport) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) Historian(ddf.catalog.history.Historian) DefaultAttributeValueRegistryImpl(ddf.catalog.data.defaultvalues.DefaultAttributeValueRegistryImpl) SourceOperations(ddf.catalog.impl.operations.SourceOperations) SourceStatus(org.codice.ddf.catalog.sourcepoller.SourceStatus) ResourceOperations(ddf.catalog.impl.operations.ResourceOperations) TransformOperations(ddf.catalog.impl.operations.TransformOperations) Date(java.util.Date) QueryOperations(ddf.catalog.impl.operations.QueryOperations) UpdateOperations(ddf.catalog.impl.operations.UpdateOperations) OperationsSecuritySupport(ddf.catalog.impl.operations.OperationsSecuritySupport) CreateOperations(ddf.catalog.impl.operations.CreateOperations) OperationsMetacardSupport(ddf.catalog.impl.operations.OperationsMetacardSupport) Before(org.junit.Before)

Example 8 with SourceStatus

use of org.codice.ddf.catalog.sourcepoller.SourceStatus 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)

Aggregations

Source (ddf.catalog.source.Source)8 SourceStatus (org.codice.ddf.catalog.sourcepoller.SourceStatus)8 QueryOperations (ddf.catalog.impl.operations.QueryOperations)7 ResourceOperations (ddf.catalog.impl.operations.ResourceOperations)7 SourceOperations (ddf.catalog.impl.operations.SourceOperations)7 ByteSource (com.google.common.io.ByteSource)6 MetacardFactory (ddf.catalog.impl.operations.MetacardFactory)6 OperationsCatalogStoreSupport (ddf.catalog.impl.operations.OperationsCatalogStoreSupport)6 OperationsMetacardSupport (ddf.catalog.impl.operations.OperationsMetacardSupport)6 OperationsSecuritySupport (ddf.catalog.impl.operations.OperationsSecuritySupport)6 TransformOperations (ddf.catalog.impl.operations.TransformOperations)6 FederatedSource (ddf.catalog.source.FederatedSource)6 CreateOperations (ddf.catalog.impl.operations.CreateOperations)5 DeleteOperations (ddf.catalog.impl.operations.DeleteOperations)5 OperationsStorageSupport (ddf.catalog.impl.operations.OperationsStorageSupport)5 CatalogProvider (ddf.catalog.source.CatalogProvider)5 ActionRegistry (ddf.action.ActionRegistry)4 Metacard (ddf.catalog.data.Metacard)4 UpdateOperations (ddf.catalog.impl.operations.UpdateOperations)4 ArrayList (java.util.ArrayList)4