Search in sources :

Example 6 with FilterBuilder

use of ddf.catalog.filter.FilterBuilder in project ddf by codice.

the class CatalogFrameworkImplTest method testFederatedQueryPermissionsNoSubject.

@Test(expected = FederationException.class)
public void testFederatedQueryPermissionsNoSubject() throws Exception {
    MockEventProcessor eventAdmin = new MockEventProcessor();
    MockMemoryProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<>(), true, new Date());
    List<CatalogStore> storeList = new ArrayList<>();
    List<FederatedSource> sourceList = new ArrayList<>();
    Map<String, Set<String>> securityAttributes = new HashMap<>();
    securityAttributes.put("role", Collections.singleton("myRole"));
    MockCatalogStore store = new MockCatalogStore("catalogStoreId-1", true, securityAttributes);
    storeList.add(store);
    sourceList.add(store);
    CatalogFramework framework = createDummyCatalogFramework(provider, storeList, sourceList, eventAdmin);
    FilterBuilder builder = new GeotoolsFilterBuilder();
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.CONTENT_TYPE).is().like().text("someType"));
    QueryRequestImpl request = new QueryRequestImpl(query, Collections.singletonList("catalogStoreId-1"));
    framework.query(request);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Date(java.util.Date) 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 7 with FilterBuilder

use of ddf.catalog.filter.FilterBuilder in project ddf by codice.

the class TagsFilterQueryPluginTest method setup.

@Before
public void setup() {
    CatalogProvider catProvider1 = mock(CatalogProvider.class);
    CatalogProvider catProvider2 = mock(CatalogProvider.class);
    CatalogProvider catProvider3 = mock(CatalogProvider.class);
    when(catProvider1.getId()).thenReturn("cat1");
    when(catProvider2.getId()).thenReturn("cat2");
    when(catProvider3.getId()).thenReturn("cat3");
    ImmutableList<CatalogProvider> catalogProviders = ImmutableList.of(catProvider1, catProvider2, catProvider3);
    filterAdapter = mock(FilterAdapter.class);
    filterBuilder = mock(FilterBuilder.class);
    plugin = new TagsFilterQueryPlugin(catalogProviders, filterAdapter, filterBuilder);
    source = mock(CatalogProvider.class);
    when(source.getId()).thenReturn("cat2");
    cache = mock(SourceCache.class);
    when(cache.getId()).thenReturn("cache");
    queryRequest = mock(QueryRequest.class);
    query = mock(Query.class);
    when(queryRequest.getQuery()).thenReturn(query);
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) Query(ddf.catalog.operation.Query) CatalogProvider(ddf.catalog.source.CatalogProvider) FilterBuilder(ddf.catalog.filter.FilterBuilder) FilterAdapter(ddf.catalog.filter.FilterAdapter) SourceCache(ddf.catalog.source.SourceCache) Before(org.junit.Before)

Example 8 with FilterBuilder

use of ddf.catalog.filter.FilterBuilder in project ddf by codice.

the class OpenSearchEndpointTest method testProcessQueryForProperHandlingOfSiteNameLOCAL.

/**
 * Test method for {@link OpenSearchEndpoint#processQuery(String, String, String, String, String,
 * String, String, String, String, String, String, String, String, String, String, String, String,
 * String, UriInfo, String, String, HttpServletRequest)} .
 *
 * <p>This test will verify that the string "local" in the sources passed to
 * OpenSearchEndpoint.processQuery is replaced with the local site name (in this case the mocked
 * name "TestSiteName"). The QueryRequest object is checked when the framework.query is called to
 * retrieve the OpenSearchQuery, which contains the Set of sites. An assertion within the Answer
 * object for the call framework.query checks that the sites Set is contains the TEST_SITE_NAME.
 */
@SuppressWarnings("unchecked")
@Test
public void testProcessQueryForProperHandlingOfSiteNameLOCAL() throws URISyntaxException, UnsupportedQueryException, SourceUnavailableException, FederationException, UnsupportedEncodingException, CatalogTransformerException {
    // ***Test setup***
    final String testSiteName = "TestSiteName";
    CatalogFramework mockFramework = mock(CatalogFramework.class);
    FilterBuilder mockFilterBuilder = mock(FilterBuilder.class);
    AttributeBuilder mockAB = mock(AttributeBuilder.class);
    ExpressionBuilder mockEB = mock(ExpressionBuilder.class);
    ContextualExpressionBuilder mockCEB = mock(ContextualExpressionBuilder.class);
    Filter mockFilter = mock(Filter.class);
    when(mockFilterBuilder.attribute(anyString())).thenReturn(mockAB);
    when(mockAB.is()).thenReturn(mockEB);
    when(mockEB.like()).thenReturn(mockCEB);
    when(mockCEB.text(anyString())).thenReturn(mockFilter);
    String searchTerms = "searchForThis";
    // "local" MUST be included
    String sources = "test, local";
    String count = "200";
    // dummy UriInfo, not really used functionally
    UriInfo mockUriInfo = mock(UriInfo.class);
    URI uri = new URI("test");
    when(mockUriInfo.getRequestUri()).thenReturn(uri);
    MultivaluedMap<String, String> mockMVMap = mock(MultivaluedMap.class);
    when(mockMVMap.get("subscription")).thenReturn(null);
    when(mockMVMap.get("interval")).thenReturn(null);
    when(mockUriInfo.getQueryParameters()).thenReturn(mockMVMap);
    @SuppressWarnings("unused") BinaryContent mockByteContent = mock(BinaryContent.class);
    // Check on the sites passed in to framework.query
    when(mockFramework.query(any(QueryRequest.class))).thenAnswer(invocation -> {
        QueryRequest queryRequest = (QueryRequest) invocation.getArguments()[0];
        // ***Test verification***
        // This assert is the whole point of this unit test
        Assert.assertTrue(((OpenSearchQuery) queryRequest.getQuery()).getSiteIds().contains(testSiteName));
        return new QueryResponseImpl(queryRequest);
    });
    // setup the BinaryContent for the call to Response.ok(...)
    // This is just needed to get the method to complete, the
    BinaryContent mockBinaryContent = mock(BinaryContent.class);
    InputStream is = new ByteArrayInputStream("Test String From InputStream".getBytes("UTF-8"));
    when(mockBinaryContent.getInputStream()).thenReturn(is);
    when(mockBinaryContent.getMimeTypeValue()).thenReturn("text/plain");
    when(mockFramework.transform(any(QueryResponse.class), anyString(), anyMap())).thenReturn(mockBinaryContent);
    OpenSearchEndpoint osEndPoint = new OpenSearchEndpoint(mockFramework, mockFilterBuilder);
    System.setProperty(SystemInfo.SITE_NAME, testSiteName);
    // ***Test Execution***
    osEndPoint.processQuery(searchTerms, null, sources, null, null, count, null, null, null, null, null, null, null, null, null, null, null, null, mockUriInfo, null, null, null);
}
Also used : AttributeBuilder(ddf.catalog.filter.AttributeBuilder) QueryRequest(ddf.catalog.operation.QueryRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ContextualExpressionBuilder(ddf.catalog.filter.ContextualExpressionBuilder) ContextualExpressionBuilder(ddf.catalog.filter.ContextualExpressionBuilder) ExpressionBuilder(ddf.catalog.filter.ExpressionBuilder) BinaryContent(ddf.catalog.data.BinaryContent) URI(java.net.URI) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) OpenSearchQuery(org.codice.ddf.opensearch.endpoint.query.OpenSearchQuery) Filter(org.opengis.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterBuilder(ddf.catalog.filter.FilterBuilder) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 9 with FilterBuilder

use of ddf.catalog.filter.FilterBuilder in project ddf by codice.

the class KeywordTextParserTest method trace.

// We have been using this for debugging purposes, its not meant to be a test.
@Ignore
@Test
public void trace() {
    Map<String, String> inputToOutput = new LinkedHashMap<>();
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    List<String> inputs = new ArrayList<>();
    // inputs.add("A \"(test test2)\" OR test2");
    inputs.add("A B  C D");
    for (String input : inputs) {
        KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
        ParsingResult<ASTNode> result = new TracingParseRunner(parser.inputPhrase()).run(input);
        // ParsingResult<ASTNode> result = new
        // ReportingParseRunner(parser.inputPhrase()).run(input);
        KeywordFilterGenerator kfg = new KeywordFilterGenerator(filterBuilder);
        Filter filter = kfg.getFilterFromASTNode(result.resultValue);
        inputToOutput.put(input, filter.toString());
    // visualize(result);
    }
    for (Map.Entry<String, String> iteration : inputToOutput.entrySet()) {
        System.out.println(iteration.getKey() + " : " + iteration.getValue());
    }
}
Also used : KeywordFilterGenerator(org.codice.ddf.opensearch.endpoint.KeywordFilterGenerator) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TracingParseRunner(org.parboiled.parserunners.TracingParseRunner) Filter(org.opengis.filter.Filter) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) ASTNode(org.codice.ddf.opensearch.endpoint.ASTNode) KeywordTextParser(org.codice.ddf.opensearch.endpoint.KeywordTextParser) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with FilterBuilder

use of ddf.catalog.filter.FilterBuilder in project ddf by codice.

the class FilterBuilderTest method like.

@Test
public void like() {
    FilterVisitor visitor = spy(new DefaultFilterVisitor() {
    });
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Filter filter = builder.attribute(FOO_ATTRIBUTE).is().like().text("bar");
    filter.accept(visitor, null);
    InOrder inOrder = inOrder(visitor);
    inOrder.verify(visitor, times(1)).visit(isA(PropertyIsLike.class), any());
// TODO check case sensitivity
}
Also used : PropertyIsLike(org.opengis.filter.PropertyIsLike) InOrder(org.mockito.InOrder) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) FilterVisitor(org.opengis.filter.FilterVisitor) Filter(org.opengis.filter.Filter) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) Test(org.junit.Test)

Aggregations

FilterBuilder (ddf.catalog.filter.FilterBuilder)54 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)49 Test (org.junit.Test)44 Filter (org.opengis.filter.Filter)41 DefaultFilterVisitor (org.geotools.filter.visitor.DefaultFilterVisitor)24 FilterVisitor (org.opengis.filter.FilterVisitor)23 InOrder (org.mockito.InOrder)21 CatalogFramework (ddf.catalog.CatalogFramework)11 ArrayList (java.util.ArrayList)10 Date (java.util.Date)10 QueryResponse (ddf.catalog.operation.QueryResponse)9 FilterAdapter (ddf.catalog.filter.FilterAdapter)7 UriInfo (javax.ws.rs.core.UriInfo)7 Metacard (ddf.catalog.data.Metacard)6 GeotoolsFilterAdapterImpl (ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl)6 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)6 TikaMimeTypeResolver (ddf.mime.tika.TikaMimeTypeResolver)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 PropertyIsLike (org.opengis.filter.PropertyIsLike)6