use of ddf.catalog.filter.FilterBuilder in project ddf by codice.
the class TestKeywordTextParser 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<String, String>();
FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
List<String> inputs = new ArrayList<String>();
// 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());
}
}
use of ddf.catalog.filter.FilterBuilder in project ddf by codice.
the class TestWorkspaceQueryService method testRun.
@SuppressWarnings("unchecked")
@Test
public void testRun() throws SchedulerException, UnsupportedQueryException, SourceUnavailableException, FederationException {
String workspaceId = "3";
QueryUpdateSubscriber queryUpdateSubscriber = mock(QueryUpdateSubscriber.class);
WorkspaceService workspaceService = mock(WorkspaceService.class);
CatalogFramework catalogFramework = mock(CatalogFramework.class);
FilterBuilder filterBuilder = mock(FilterBuilder.class);
Scheduler scheduler = mock(Scheduler.class);
when(scheduler.getContext()).thenReturn(mock(SchedulerContext.class));
Supplier<Optional<Scheduler>> schedulerSupplier = () -> Optional.of(scheduler);
SecurityService securityService = new SecurityService() {
@Override
public Subject getSystemSubject() {
return mock(Subject.class);
}
@Override
public Map<String, Serializable> addSystemSubject(Map<String, Serializable> properties) {
return properties;
}
};
FilterService filterService = mock(FilterService.class);
when(filterService.getModifiedDateFilter(any())).thenReturn(mock(Filter.class));
when(filterBuilder.anyOf(Mockito.any(Filter.class))).thenReturn(mock(Or.class));
when(filterBuilder.allOf(Mockito.<Filter>anyVararg())).thenReturn(mock(And.class));
WorkspaceQueryServiceImpl workspaceQueryServiceImpl = new WorkspaceQueryServiceImpl(queryUpdateSubscriber, workspaceService, catalogFramework, filterBuilder, schedulerSupplier, securityService, filterService);
workspaceQueryServiceImpl.setQueryTimeInterval(60);
String ecql = "area( Polygon((10 10, 20 10, 20 20, 10 10)) ) BETWEEN 10000 AND 30000";
WorkspaceMetacardImpl workspaceMetacard = mock(WorkspaceMetacardImpl.class);
when(workspaceMetacard.getId()).thenReturn(workspaceId);
QueryMetacardImpl queryMetacardWithSource = mock(QueryMetacardImpl.class);
when(queryMetacardWithSource.getSources()).thenReturn(Collections.singletonList("SomeSource"));
when(queryMetacardWithSource.getCql()).thenReturn(ecql);
Attribute id1 = mock(Attribute.class);
when(id1.getValue()).thenReturn("1");
when(queryMetacardWithSource.getAttribute(Metacard.ID)).thenReturn(id1);
QueryMetacardImpl queryMetacardWithoutSource = mock(QueryMetacardImpl.class);
when(queryMetacardWithoutSource.getSources()).thenReturn(Collections.emptyList());
when(queryMetacardWithoutSource.getCql()).thenReturn(ecql);
Attribute id2 = mock(Attribute.class);
when(id2.getValue()).thenReturn("2");
when(queryMetacardWithoutSource.getAttribute(Metacard.ID)).thenReturn(id2);
Map<String, Pair<WorkspaceMetacardImpl, List<QueryMetacardImpl>>> queryMetacards = Collections.singletonMap(id2.getValue().toString(), new ImmutablePair<>(workspaceMetacard, Arrays.asList(queryMetacardWithSource, queryMetacardWithoutSource)));
when(workspaceService.getQueryMetacards()).thenReturn(queryMetacards);
long hitCount1 = 10;
long hitCount2 = 20;
QueryResponse queryResponse = mock(QueryResponse.class);
when(queryResponse.getHits()).thenReturn(hitCount1).thenReturn(hitCount2);
when(catalogFramework.query(any())).thenReturn(queryResponse);
workspaceQueryServiceImpl.setSubject(new Subject() {
@Override
public boolean isGuest() {
return false;
}
@Override
public Object getPrincipal() {
return null;
}
@Override
public PrincipalCollection getPrincipals() {
return null;
}
@Override
public boolean isPermitted(String s) {
return false;
}
@Override
public boolean isPermitted(Permission permission) {
return false;
}
@Override
public boolean[] isPermitted(String... strings) {
return new boolean[0];
}
@Override
public boolean[] isPermitted(List<Permission> list) {
return new boolean[0];
}
@Override
public boolean isPermittedAll(String... strings) {
return false;
}
@Override
public boolean isPermittedAll(Collection<Permission> collection) {
return false;
}
@Override
public void checkPermission(String s) throws AuthorizationException {
}
@Override
public void checkPermission(Permission permission) throws AuthorizationException {
}
@Override
public void checkPermissions(String... strings) throws AuthorizationException {
}
@Override
public void checkPermissions(Collection<Permission> collection) throws AuthorizationException {
}
@Override
public boolean hasRole(String s) {
return false;
}
@Override
public boolean[] hasRoles(List<String> list) {
return new boolean[0];
}
@Override
public boolean hasAllRoles(Collection<String> collection) {
return false;
}
@Override
public void checkRole(String s) throws AuthorizationException {
}
@Override
public void checkRoles(Collection<String> collection) throws AuthorizationException {
}
@Override
public void checkRoles(String... strings) throws AuthorizationException {
}
@Override
public void login(AuthenticationToken authenticationToken) throws AuthenticationException {
}
@Override
public boolean isAuthenticated() {
return false;
}
@Override
public boolean isRemembered() {
return false;
}
@Override
public Session getSession() {
return null;
}
@Override
public Session getSession(boolean b) {
return null;
}
@Override
public void logout() {
}
@Override
public <V> V execute(Callable<V> callable) throws ExecutionException {
try {
return callable.call();
} catch (Exception e) {
throw new ExecutionException(e);
}
}
@Override
public void execute(Runnable runnable) {
}
@Override
public <V> Callable<V> associateWith(Callable<V> callable) {
return null;
}
@Override
public Runnable associateWith(Runnable runnable) {
return null;
}
@Override
public void runAs(PrincipalCollection principalCollection) throws NullPointerException, IllegalStateException {
}
@Override
public boolean isRunAs() {
return false;
}
@Override
public PrincipalCollection getPreviousPrincipals() {
return null;
}
@Override
public PrincipalCollection releaseRunAs() {
return null;
}
});
workspaceQueryServiceImpl.setCronString("0 0 0 * * ?");
workspaceQueryServiceImpl.setQueryTimeoutMinutes(5L);
workspaceQueryServiceImpl.run();
ArgumentCaptor<Map> argumentCaptor = ArgumentCaptor.forClass(Map.class);
verify(queryUpdateSubscriber).notify(argumentCaptor.capture());
Map queryUpdateSubscriberArgumentRaw = argumentCaptor.getValue();
Map<String, Pair<WorkspaceMetacardImpl, Long>> queryUpdateSubscriberArgument = (Map<String, Pair<WorkspaceMetacardImpl, Long>>) queryUpdateSubscriberArgumentRaw;
assertThat(queryUpdateSubscriberArgument.get(workspaceId).getRight(), is(hitCount1 + hitCount2));
}
use of ddf.catalog.filter.FilterBuilder in project ddf by codice.
the class TestWorkspaceServiceImpl method setup.
@Before
public void setup() {
catalogFramework = mock(CatalogFramework.class);
filterBuilder = mock(FilterBuilder.class);
workspaceTransformer = mock(WorkspaceTransformer.class);
filterService = mock(FilterService.class);
securityService = mock(SecurityService.class);
WorkspaceMetacardFilter workspaceMetacardFilter = mock(WorkspaceMetacardFilter.class);
when(workspaceMetacardFilter.filter(any())).thenReturn(true);
workspaceService = new WorkspaceServiceImpl(catalogFramework, filterBuilder, workspaceTransformer, filterService, securityService);
}
use of ddf.catalog.filter.FilterBuilder in project ddf by codice.
the class TestRestEndpoint 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.
*
* @throws Exception
*/
@Test
public void testGetMetacardAsXml() throws Exception {
String filename = "src/test/resources/ValidGeojson.json";
CatalogFramework framework = givenCatalogFramework(SAMPLE_ID);
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(Map.class))).thenAnswer(new Answer<BinaryContent>() {
@Override
public BinaryContent answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Metacard metacard = (Metacard) args[0];
return content;
}
});
RESTEndpoint restEndpoint = new RESTEndpoint(framework);
// Add a MimeTypeToINputTransformer that the REST endpoint will call to create the metacard
addMatchingService(restEndpoint, Arrays.asList(getSimpleTransformer()));
restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
restEndpoint.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);
UriInfo uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
Response response = restEndpoint.createMetacard(multipartBody, uriInfo, RESTEndpoint.DEFAULT_METACARD_TRANSFORMER);
assertEquals(OK, response.getStatus());
InputStream responseEntity = (InputStream) response.getEntity();
String responseXml = IOUtils.toString(responseEntity);
assertEquals(metacardXml, responseXml);
}
use of ddf.catalog.filter.FilterBuilder in project ddf by codice.
the class FilterBuilderTest method test.
// @Test
public void test() {
FilterBuilder builder = new GeotoolsFilterBuilder();
Filter filter = builder.allOf(builder.allOf(builder.anyOf(builder.attribute(FOO_ATTRIBUTE).is().empty())), builder.allOf(builder.attribute(FOO_ATTRIBUTE).is().empty()), builder.allOf(builder.attribute("attr").is().greaterThan().number(new Short((short) 5)), builder.attribute("frequency").is().greaterThan().number(new Integer(5)), builder.attribute("frequency").is().greaterThan().number(new Long(5)), builder.attribute("frequency").is().greaterThan().number(new Float(5)), builder.attribute("frequency").is().greaterThan().number(new Double(5)), builder.attribute("frequency").is().greaterThanOrEqualTo().number(new Short((short) 5)), builder.attribute("frequency").is().greaterThanOrEqualTo().number(new Integer(5)), builder.attribute("frequency").is().greaterThanOrEqualTo().number(new Long(5)), builder.attribute("frequency").is().greaterThanOrEqualTo().number(new Float(5)), builder.attribute("frequency").is().greaterThanOrEqualTo().number(new Double(5)), builder.attribute("frequency").is().lessThan().number(new Short((short) 5)), builder.attribute("frequency").is().lessThan().number(new Integer(5)), builder.attribute("frequency").is().lessThan().number(new Long(5)), builder.attribute("frequency").is().lessThan().number(new Float(5)), builder.attribute("frequency").is().lessThan().number(new Double(5)), builder.attribute("frequency").is().lessThanOrEqualTo().number(new Short((short) 5)), builder.attribute("frequency").is().lessThanOrEqualTo().number(new Integer(5)), builder.attribute("frequency").is().lessThanOrEqualTo().number(new Long(5)), builder.attribute("frequency").is().lessThanOrEqualTo().number(new Float(5)), builder.attribute("frequency").is().lessThanOrEqualTo().number(new Double(5)), builder.attribute("frequency").is().between().numbers((short) 5, (short) 6), builder.attribute("frequency").is().between().numbers(new Integer(5), new Integer(5)), builder.attribute("frequency").is().between().numbers(new Long(5), new Long(5)), builder.attribute("frequency").is().between().numbers(new Float(5), new Float(5)), builder.attribute("frequency").is().between().numbers(new Double(5), new Double(5)), // (IN)EQUALITY
builder.attribute(FOO_ATTRIBUTE).is().number(new Short((short) 5)), builder.attribute("frequency").is().number(new Integer(5)), builder.attribute("frequency").is().number(new Long(5)), builder.attribute("frequency").is().number(new Float(5)), builder.attribute("frequency").is().number(new Double(5)), builder.attribute("created").is().date(new Date()), builder.attribute("created").is().dateRange(new Date(), new Date()), builder.attribute("created").is().bool(true), builder.attribute("created").is().bytes(new byte[] {}), builder.attribute("created").is().wkt("POINT (10, 30)"), builder.attribute("created").is().text("POINT (10, 30)"), builder.attribute("frequency").is().equalTo().number(new Integer(5)), builder.attribute("frequency").is().equalTo().number(new Long(5)), builder.attribute("frequency").is().equalTo().number(new Float(5)), builder.attribute("frequency").is().equalTo().number(new Double(5)), builder.attribute("created").is().equalTo().date(new Date()), builder.attribute("created").is().equalTo().dateRange(new Date(), new Date()), builder.attribute("created").is().equalTo().bool(true), builder.attribute("created").is().equalTo().bytes(new byte[] {}), builder.attribute("created").is().equalTo().wkt("POINT (10, 30)"), builder.attribute("created").is().equalTo().text("POINT (10, 30)"), builder.attribute("frequency").is().notEqualTo().number(new Short((short) 5)), builder.attribute("frequency").is().notEqualTo().number(new Integer(5)), builder.attribute("frequency").is().notEqualTo().number(new Long(5)), builder.attribute("frequency").is().notEqualTo().number(new Float(5)), builder.attribute("frequency").is().notEqualTo().number(new Double(5)), builder.attribute("created").is().notEqualTo().date(new Date()), builder.attribute("created").is().notEqualTo().dateRange(new Date(), new Date()), builder.attribute("created").is().notEqualTo().bool(true), builder.attribute("created").is().notEqualTo().bytes(new byte[] {}), builder.attribute("created").is().notEqualTo().wkt("POINT (10, 30)"), builder.attribute("created").is().notEqualTo().text("POINT (10, 30)"), // NULLABLE
builder.attribute("created").is().empty(), builder.attribute(FOO_ATTRIBUTE).is().number(50), // TEMPORAL
builder.attribute("created").is().after().date(new Date()), builder.attribute("created").is().before().date(new Date()), builder.attribute("created").is().during().dates(new Date(), new Date()), builder.attribute("created").is().overlapping().dates(new Date(), new Date()), // CONTEXTUAL
builder.attribute(FOO_ATTRIBUTE).is().like().text("bar*"), // implied equality
builder.attribute("frequency").is().number((short) 5), builder.attribute("frequency").is().number(new Integer(5)), builder.attribute("frequency").is().number(new Long(5)), builder.attribute("frequency").is().number(new Float(5)), builder.attribute("frequency").is().number(new Double(5)), builder.attribute("created").is().date(new Date()), builder.attribute("created").is().dateRange(new Date(), new Date()), builder.attribute("created").is().bool(true), builder.attribute("created").is().bytes(new byte[] {}), builder.attribute("created").is().wkt("POINT (10, 30)"), builder.attribute("created").is().text("search*"), // SPATIAL
builder.attribute("location").is().intersecting().wkt("POINT (10, 30)"), builder.attribute("location").is().containing().wkt("POINT (10, 30)"), builder.attribute("location").is().beyond().wkt("POINT (10, 30)"), builder.attribute("location").is().within().wkt("POINT (10, 30)"), builder.attribute("location").is().withinBuffer().wkt("POINT (10, 30)", (long) 5), builder.attribute("frequency").greaterThan().number(new Short((short) 5)), builder.attribute("frequency").greaterThan().number(new Integer(5)), builder.attribute("frequency").greaterThan().number(new Long(5)), builder.attribute("frequency").greaterThan().number(new Float(5)), builder.attribute("frequency").greaterThan().number(new Double(5)), builder.attribute("frequency").greaterThanOrEqualTo().number(new Short((short) 5)), builder.attribute("frequency").greaterThanOrEqualTo().number(new Integer(5)), builder.attribute("frequency").greaterThanOrEqualTo().number(new Long(5)), builder.attribute("frequency").greaterThanOrEqualTo().number(new Float(5)), builder.attribute("frequency").greaterThanOrEqualTo().number(new Double(5)), builder.attribute("frequency").lessThan().number(new Short((short) 5)), builder.attribute("frequency").lessThan().number(new Integer(5)), builder.attribute("frequency").lessThan().number(new Long(5)), builder.attribute("frequency").lessThan().number(new Float(5)), builder.attribute("frequency").lessThan().number(new Double(5)), builder.attribute("frequency").lessThanOrEqualTo().number(new Short((short) 5)), builder.attribute("frequency").lessThanOrEqualTo().number(5), builder.attribute("frequency").lessThanOrEqualTo().number(5L), builder.attribute("frequency").lessThanOrEqualTo().number(5f), builder.attribute("frequency").lessThanOrEqualTo().number(5d), builder.attribute("frequency").between().numbers((short) 5, (short) 6), builder.attribute("frequency").between().numbers(5, 6), builder.attribute("frequency").between().numbers(5L, 6L), builder.attribute("frequency").between().numbers(5f, 6f), builder.attribute("frequency").between().numbers(5d, 6d), // (IN)EQUALITY
builder.attribute("frequency").number((short) 5), builder.attribute("frequency").number(5), builder.attribute("frequency").number(5L), builder.attribute("frequency").number(5f), builder.attribute("frequency").number(5d), builder.attribute("created").date(new Date()), builder.attribute("created").dateRange(new Date(), new Date()), builder.attribute("created").bool(true), builder.attribute("created").bytes(new byte[] {}), builder.attribute("created").wkt("POINT (10, 30)"), builder.attribute("created").text("POINT (10, 30)"), builder.attribute("frequency").notEqualTo().number(new Short((short) 5)), builder.attribute("frequency").notEqualTo().number(new Integer(5)), builder.attribute("frequency").notEqualTo().number(new Long(5)), builder.attribute("frequency").notEqualTo().number(new Float(5)), builder.attribute("frequency").notEqualTo().number(new Double(5)), builder.attribute("created").notEqualTo().date(new Date()), builder.attribute("created").notEqualTo().dateRange(new Date(), new Date()), builder.attribute("created").notEqualTo().bool(true), builder.attribute("created").notEqualTo().bytes(new byte[] {}), builder.attribute("created").notEqualTo().wkt("POINT (10, 30)"), builder.attribute("created").notEqualTo().text("POINT (10, 30)"), // NULLABLE
builder.attribute("created").empty(), // TEMPORAL
builder.attribute("created").after().date(new Date()), builder.attribute("created").before().date(new Date()), builder.attribute("created").during().dates(new Date(), new Date()), builder.attribute("created").overlapping().dates(new Date(), new Date()), // CONTEXTUAL
builder.attribute(FOO_ATTRIBUTE).like().text("bar*"), // SPATIAL
builder.attribute("location").intersecting().wkt("POINT (10, 30)"), builder.attribute("location").containing().wkt("POINT (10, 30)"), builder.attribute("location").beyond().wkt("POINT (10, 30)"), builder.attribute("location").within().wkt("POINT (10, 30)"), builder.attribute("location").withinBuffer().wkt("POINT (10, 30)", (long) 5), builder.xpath("//blah/blah").exists(), builder.xpath("//foo/bar").like().text("bat"), builder.xpath("//foo/bar").like().caseSensitiveText("bat"), builder.xpath("//foo/bar").like().fuzzyText("bat"), builder.xpath("//blah/blah").is().like().text("bat"), builder.xpath("//blah/blah").is().like().caseSensitiveText("bat"), builder.xpath("//blah/blah").is().like().fuzzyText("bat"), builder.attribute(FOO_ATTRIBUTE).like().fuzzyText("bar"), builder.attribute(FOO_ATTRIBUTE).like().caseSensitiveText("bar"), builder.attribute(FOO_ATTRIBUTE).like().text("bar"), builder.attribute(FOO_ATTRIBUTE).is().like().fuzzyText("bar"), builder.attribute(FOO_ATTRIBUTE).is().like().caseSensitiveText("bar"), builder.attribute(FOO_ATTRIBUTE).is().like().text("bar"), builder.attribute(FOO_ATTRIBUTE).nearestTo().wkt("POINT(10,10)"), // Relative Time
builder.attribute("created").during().last(500L), builder.attribute("created").during().next(500L)));
}
Aggregations