Search in sources :

Example 36 with FilterBuilder

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

the class OpenSearchEndpointTest method testProcessQueryForProperHandlingOfSiteNameLOCAL.

/**
     * Test method for
     * {@link org.codice.ddf.endpoints.OpenSearchEndpoint#processQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.ws.rs.core.UriInfo, java.lang.String, java.lang.String)}
     * .
     * <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.
     *
     * @throws URISyntaxException
     * @throws FederationException
     * @throws SourceUnavailableException
     * @throws UnsupportedQueryException
     * @throws UnsupportedEncodingException
     * @throws CatalogTransformerException
     */
@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(new Answer<Object>() {

        public Object answer(InvocationOnMock 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) Matchers.anyString(org.mockito.Matchers.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) Filter(org.opengis.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterBuilder(ddf.catalog.filter.FilterBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 37 with FilterBuilder

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

the class CatalogServiceImplTest method testGetMetacardAsXml.

/**
 * Tests that a geojson input has its InputTransformer invoked by the REST endpoint to create a
 * metacard that is then converted to XML and returned from the REST endpoint.
 */
@Test
@SuppressWarnings({ "unchecked" })
public void testGetMetacardAsXml() throws Exception {
    CatalogFramework framework = givenCatalogFramework();
    String metacardXml = "<metacard ns2:id=\"assigned-when-ingested\">\r\n" + "<type>type.metacard</type>\r\n" + "<string name=\"title\">\r\n" + "<value>Title goes here ...</value>\r\n" + "</string>\r\n" + "<string name=\"metadata\">\r\n" + "<value>metadata goes here ...</value>\r\n" + "</metacard>";
    // Mock XmlMetacardTransformer that CatalogFramework will call to convert generated
    // metacard into XML to be returned from REST endpoint.
    final BinaryContent content = mock(BinaryContent.class);
    InputStream inputStream = new ByteArrayInputStream(metacardXml.getBytes(GET_OUTPUT_TYPE));
    when(content.getInputStream()).thenReturn(inputStream);
    when(content.getMimeTypeValue()).thenReturn("application/json;id=geojson");
    when(framework.transform(isA(Metacard.class), anyString(), isNull())).thenReturn(content);
    CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
    // Add a MimeTypeToINputTransformer that the REST endpoint will call to create the metacard
    addMatchingService(catalogService, Collections.singletonList(getSimpleTransformer()));
    catalogService.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    catalogService.setFilterBuilder(filterBuilder);
    String json = "{\r\n" + "    \"properties\": {\r\n" + "        \"title\": \"myTitle\",\r\n" + "        \"thumbnail\": \"CA==\",\r\n" + "        \"resource-uri\": \"http://example.com\",\r\n" + "        \"created\": \"2012-09-01T00:09:19.368+0000\",\r\n" + "        \"metadata-content-type-version\": \"myVersion\",\r\n" + "        \"metadata-content-type\": \"myType\",\r\n" + "        \"metadata\": \"<xml>metadata goes here ...</xml>\",\r\n" + "        \"modified\": \"2012-09-01T00:09:19.368+0000\"\r\n" + "    },\r\n" + "    \"type\": \"Feature\",\r\n" + "    \"geometry\": {\r\n" + "        \"type\": \"Point\",\r\n" + "        \"coordinates\": [\r\n" + "            30.0,\r\n" + "            10.0\r\n" + "        ]\r\n" + "    }\r\n" + "} ";
    // Sample headers for a multipart body specifying a geojson file to have a metacard created for:
    // Content-Disposition: form-data; name="file"; filename="C:\DDF\geojson_valid.json"
    // Content-Type: application/json;id=geojson
    InputStream is = IOUtils.toInputStream(json);
    List<Attachment> attachments = new ArrayList<>();
    ContentDisposition contentDisposition = new ContentDisposition("form-data; name=file; filename=C:\\DDF\\geojson_valid.json");
    Attachment attachment = new Attachment("file_part", is, contentDisposition);
    attachments.add(attachment);
    MediaType mediaType = new MediaType(MediaType.APPLICATION_JSON, "id=geojson");
    MultipartBody multipartBody = new MultipartBody(attachments, mediaType, true);
    BinaryContent binaryContent = catalogService.createMetacard(multipartBody, AbstractCatalogService.DEFAULT_METACARD_TRANSFORMER);
    InputStream responseEntity = binaryContent.getInputStream();
    String responseXml = IOUtils.toString(responseEntity);
    assertEquals(metacardXml, responseXml);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 38 with FilterBuilder

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

the class RESTEndpointTest method executeTest.

private Response executeTest(CatalogFramework framework, String transformer, boolean local) throws Exception {
    MimeTypeMapper mimeTypeMapper = mock(MimeTypeMapper.class);
    when(mimeTypeMapper.getMimeTypeForFileExtension("txt")).thenReturn("text/plain");
    when(mimeTypeMapper.getMimeTypeForFileExtension("xml")).thenReturn("text/xml");
    CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, new AttachmentParserImpl(mimeTypeMapper), mock(AttributeRegistry.class));
    catalogServiceImpl.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    catalogServiceImpl.setFilterBuilder(filterBuilder);
    RESTEndpoint restEndpoint = new RESTEndpoint(catalogServiceImpl);
    UriInfo uriInfo;
    Response response;
    if (local) {
        uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
        response = restEndpoint.getDocument(GET_ID, transformer, uriInfo, null);
    } else {
        uriInfo = createSpecificUriInfo(FED_RETRIEVE_ADDRESS);
        response = restEndpoint.getDocument(GET_SITENAME, GET_ID, transformer, uriInfo, null);
    }
    return response;
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) MimeTypeMapper(ddf.mime.MimeTypeMapper) AttributeRegistry(ddf.catalog.data.AttributeRegistry) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) CatalogServiceImpl(org.codice.ddf.rest.service.impl.CatalogServiceImpl) AttachmentParserImpl(org.codice.ddf.attachment.impl.AttachmentParserImpl) UriInfo(javax.ws.rs.core.UriInfo)

Example 39 with FilterBuilder

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

the class FilterBuilderTest method intersects.

@Test
public void intersects() {
    FilterVisitor visitor = spy(new DefaultFilterVisitor() {
    });
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Filter filter = builder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(POINT_WKT);
    filter.accept(visitor, null);
    InOrder inOrder = inOrder(visitor);
    inOrder.verify(visitor, times(1)).visit(isA(Intersects.class), any());
}
Also used : 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) Intersects(org.opengis.filter.spatial.Intersects) Test(org.junit.Test)

Example 40 with FilterBuilder

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

the class FilterBuilderTest method isDate.

@Test
public void isDate() {
    FilterVisitor visitor = spy(new DefaultFilterVisitor() {
    });
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Filter filter = builder.attribute(FOO_ATTRIBUTE).is().date(new Date());
    filter.accept(visitor, null);
    InOrder inOrder = inOrder(visitor);
    inOrder.verify(visitor, times(1)).visit(isA(PropertyIsEqualTo.class), any());
}
Also used : InOrder(org.mockito.InOrder) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) PropertyIsEqualTo(org.opengis.filter.PropertyIsEqualTo) 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) Date(java.util.Date) 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