Search in sources :

Example 61 with GeotoolsFilterBuilder

use of ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder 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);
    // Mock register the provider in the container
    // Mock the source poller
    SourcePoller mockPoller = mock(SourcePoller.class);
    when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
    MetacardImpl metacard = new MetacardImpl(BASIC_METACARD);
    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.singletonMap(federatedSite1Name, federatedSource1));
    props.setResourceReaders(resourceReaders);
    props.setFederationStrategy(strategy);
    props.setQueryResponsePostProcessor(mock(QueryResponsePostProcessor.class));
    props.setSourcePoller(mockPoller);
    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));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) 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) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) HashSet(java.util.HashSet) QueryRequest(ddf.catalog.operation.QueryRequest) FederationStrategy(ddf.catalog.federation.FederationStrategy) SourceMonitor(ddf.catalog.source.SourceMonitor) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FederatedSource(ddf.catalog.source.FederatedSource) Metacard(ddf.catalog.data.Metacard) CatalogProvider(ddf.catalog.source.CatalogProvider) QueryResponse(ddf.catalog.operation.QueryResponse) Test(org.junit.Test)

Example 62 with GeotoolsFilterBuilder

use of ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder in project ddf by codice.

the class CatalogFrameworkImplTest method testFederatedQueryPermissionsNotPermitted.

@Test(expected = FederationException.class)
public void testFederatedQueryPermissionsNotPermitted() throws Exception {
    MockEventProcessor eventAdmin = new MockEventProcessor();
    MockMemoryProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<>(), true, new Date());
    Map<String, CatalogStore> storeMap = new HashMap<>();
    Map<String, FederatedSource> sourceMap = new HashMap<>();
    Map<String, Set<String>> securityAttributes = new HashMap<>();
    securityAttributes.put("role", Collections.singleton("myRole"));
    MockCatalogStore store = new MockCatalogStore("catalogStoreId-1", true, securityAttributes);
    storeMap.put(store.getId(), store);
    sourceMap.put(store.getId(), store);
    CatalogFramework framework = createDummyCatalogFramework(provider, storeMap, sourceMap, eventAdmin);
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Subject subject = mock(Subject.class);
    when(subject.isPermitted(any(KeyValueCollectionPermission.class))).thenReturn(false);
    HashMap<String, Serializable> properties = new HashMap<>();
    properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.CONTENT_TYPE).is().like().text("someType"));
    QueryRequestImpl request = new QueryRequestImpl(query, false, Collections.singletonList("catalogStoreId-1"), properties);
    framework.query(request);
}
Also used : KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Serializable(java.io.Serializable) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) Subject(ddf.security.Subject) CatalogStore(ddf.catalog.source.CatalogStore) FederatedSource(ddf.catalog.source.FederatedSource) QueryImpl(ddf.catalog.operation.impl.QueryImpl) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CatalogFramework(ddf.catalog.CatalogFramework) Test(org.junit.Test)

Example 63 with GeotoolsFilterBuilder

use of ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder in project ddf by codice.

the class TestRestEndpoint method executeTest.

private Response executeTest(CatalogFramework framework, String transformer, boolean local, HttpServletRequest request) throws URISyntaxException {
    RESTEndpoint restEndpoint = new RESTEndpoint(framework);
    restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    restEndpoint.setFilterBuilder(filterBuilder);
    UriInfo uriInfo;
    Response response;
    if (local) {
        uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
        response = restEndpoint.getDocument(GET_ID, transformer, uriInfo, request);
    } else {
        uriInfo = createSpecificUriInfo(FED_RETRIEVE_ADDRESS);
        response = restEndpoint.getDocument(GET_SITENAME, GET_ID, transformer, uriInfo, request);
    }
    return response;
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) UriInfo(javax.ws.rs.core.UriInfo)

Example 64 with GeotoolsFilterBuilder

use of ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder in project ddf by codice.

the class TestRestEndpoint method headTest.

private Response headTest(boolean local) throws CatalogTransformerException, URISyntaxException, UnsupportedEncodingException, UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
    MetacardImpl metacard = null;
    List<Result> list = new ArrayList<Result>();
    Result result = mock(Result.class);
    InputStream inputStream = null;
    UriInfo uriInfo;
    Response response;
    CatalogFramework framework = givenCatalogFramework(SAMPLE_ID);
    list.add(result);
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getResults()).thenReturn(list);
    when(framework.query(isA(QueryRequest.class), isNull(FederationStrategy.class))).thenReturn(queryResponse);
    metacard = new MetacardImpl();
    metacard.setSourceId(GET_SITENAME);
    when(result.getMetacard()).thenReturn(metacard);
    Resource resource = mock(Resource.class);
    inputStream = new ByteArrayInputStream(GET_STREAM.getBytes(GET_OUTPUT_TYPE));
    when(resource.getInputStream()).thenReturn(inputStream);
    when(resource.getMimeTypeValue()).thenReturn(GET_MIME_TYPE);
    when(resource.getName()).thenReturn(GET_FILENAME);
    when(framework.transform(isA(Metacard.class), anyString(), isA(Map.class))).thenReturn(resource);
    RESTEndpoint restEndpoint = new RESTEndpoint(framework);
    restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    restEndpoint.setFilterBuilder(filterBuilder);
    uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
    if (local) {
        response = restEndpoint.getHeaders(GET_ID, uriInfo, null);
    } else {
        response = restEndpoint.getHeaders(null, GET_ID, uriInfo, null);
    }
    return response;
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) FederationStrategy(ddf.catalog.federation.FederationStrategy) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(ddf.catalog.resource.Resource) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) QueryResponse(ddf.catalog.operation.QueryResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo)

Example 65 with GeotoolsFilterBuilder

use of ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder in project ddf by codice.

the class CachingFederationStrategyTest method testCatchStopProcessingException.

@Test
public void testCatchStopProcessingException() throws Exception {
    PreFederatedQueryPlugin mockPlug = mock(PreFederatedQueryPlugin.class);
    PreFederatedQueryPlugin mockPlug2 = mock(PreFederatedQueryPlugin.class);
    when(mockPlug.process(any(Source.class), any(QueryRequest.class))).thenThrow(new StopProcessingException("test exception"));
    strategy = new CachingFederationStrategy(queryExecutor, Arrays.asList(mockPlug, mockPlug2), new ArrayList<>(), cache, cacheExecutor, mock(ValidationQueryFactory.class), new CacheQueryFactory(new GeotoolsFilterBuilder()));
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, properties);
    strategy.federate(Arrays.asList(mock(Source.class)), fedQueryRequest);
    // First plugin throws exception, so second plugin is untouched
    verify(mockPlug).process(any(Source.class), any(QueryRequest.class));
    verifyZeroInteractions(mockPlug2);
}
Also used : PreFederatedQueryPlugin(ddf.catalog.plugin.PreFederatedQueryPlugin) QueryRequest(ddf.catalog.operation.QueryRequest) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ArrayList(java.util.ArrayList) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Source(ddf.catalog.source.Source) Test(org.junit.Test)

Aggregations

GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)67 Test (org.junit.Test)50 FilterBuilder (ddf.catalog.filter.FilterBuilder)44 Filter (org.opengis.filter.Filter)38 DefaultFilterVisitor (org.geotools.filter.visitor.DefaultFilterVisitor)24 FilterVisitor (org.opengis.filter.FilterVisitor)23 InOrder (org.mockito.InOrder)21 ArrayList (java.util.ArrayList)14 Date (java.util.Date)13 GeotoolsFilterAdapterImpl (ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl)12 QueryRequest (ddf.catalog.operation.QueryRequest)10 QueryResponse (ddf.catalog.operation.QueryResponse)9 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)9 CatalogFramework (ddf.catalog.CatalogFramework)8 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 Metacard (ddf.catalog.data.Metacard)7 FilterAdapter (ddf.catalog.filter.FilterAdapter)7 Source (ddf.catalog.source.Source)7 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)7 Before (org.junit.Before)7