use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class OpenSearchQueryTest method testOgcFilterEvaluateTemporalBetween.
@Test
public // @Ignore
void testOgcFilterEvaluateTemporalBetween() throws Exception {
FilterFactory filterFactory = new FilterFactoryImpl();
// get a calendar instance, which defaults to "now"
Calendar calendar = Calendar.getInstance();
// get a date to represent "today"
Date now = calendar.getTime();
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
String dateInRange = dateFormatter.format(now);
// set calendar time in past to create start date for temporal filter's criteria
calendar.add(Calendar.DAY_OF_YEAR, -1);
Date start = calendar.getTime();
String startDate = dateFormatter.format(start);
LOGGER.debug("startDate = {}", startDate);
// set calendar time in future to create end date for temporal filter's criteria
calendar.add(Calendar.DAY_OF_YEAR, +3);
Date end = calendar.getTime();
String endDate = dateFormatter.format(end);
LOGGER.debug("endDate = {}", endDate);
// Test date between start and end dates
Filter filter = filterFactory.between(filterFactory.literal(dateInRange), filterFactory.literal(startDate), filterFactory.literal(endDate));
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
LOGGER.debug(transform.transform(filter));
boolean result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertTrue(result);
// Test date that is after end date
calendar.add(Calendar.DAY_OF_YEAR, +3);
Date outOfRange = calendar.getTime();
String outOfRangeDate = dateFormatter.format(outOfRange);
filter = filterFactory.between(filterFactory.literal(outOfRangeDate), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertFalse(result);
// Test date that is before start date
calendar.add(Calendar.DAY_OF_YEAR, -20);
Date outOfRange2 = calendar.getTime();
String outOfRangeDate2 = dateFormatter.format(outOfRange2);
filter = filterFactory.between(filterFactory.literal(outOfRangeDate2), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertFalse(result);
// Test date that is equal to start date
filter = filterFactory.between(filterFactory.literal(startDate), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertTrue(result);
// Test date that is equal to end date
filter = filterFactory.between(filterFactory.literal(endDate), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertTrue(result);
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class PredicateTest method testTemporalNullMetadata.
@Test
public void testTemporalNullMetadata() throws Exception {
String methodName = "testTemporalNullMetadata";
LOGGER.debug("*************** START: {} *****************", methodName);
MockQuery query = new MockQuery();
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
query.addTemporalFilter(start, end, Metacard.EFFECTIVE);
SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
LOGGER.debug("Resulting Predicate: {}", pred);
Filter filter = query.getFilter();
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
String filterXml = transform.transform(filter);
LOGGER.debug(filterXml);
// input that passes temporal
LOGGER.debug("\npass temporal.\n");
MetacardImpl metacard = new MetacardImpl();
metacard.setCreatedDate(new Date());
metacard.setExpirationDate(new Date());
metacard.setModifiedDate(new Date());
XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
Date effectiveDate = cal.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate);
HashMap<String, Object> properties = new HashMap<>();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
// no contextual map containing indexed contextual data
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
Event testEvent = new Event("topic", properties);
boolean b = pred.matches(testEvent);
assertTrue(b);
LOGGER.debug("*************** END: {} *****************", methodName);
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class PredicateTest method testSpatial.
@Test
public void testSpatial() throws Exception {
String methodName = "testSpatial";
LOGGER.debug("*************** START: {} *****************", methodName);
MockQuery query = new MockQuery();
// XMLGregorianCalendar start = df.newXMLGregorianCalendarDate( 2011, 10, 25, 0 );
// XMLGregorianCalendar end = df.newXMLGregorianCalendarDate( 2011, 10, 27, 0 );
// query.addTemporalFilter( start, end, Metacard.EFFECTIVE );
String geometryWkt = "POINT(44.5 34.5)";
Double inputRadius = 66672.0;
String linearUnit = Distance.LinearUnit.METER.name();
String spatialType = "POINT_RADIUS";
query.addSpatialFilter(geometryWkt, inputRadius, linearUnit, spatialType);
SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
LOGGER.debug("Resulting Predicate: {}", pred);
Filter filter = query.getFilter();
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
String filterXml = transform.transform(filter);
LOGGER.debug(filterXml);
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class PredicateTest method testMultipleCriteriaWithContentTypes.
@Test
public void testMultipleCriteriaWithContentTypes() throws Exception {
String methodName = "testMultipleCriteriaWithContentTypes";
LOGGER.debug("*************** START: {} *****************", methodName);
MockQuery query = new MockQuery();
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
query.addTemporalFilter(start, end, Metacard.EFFECTIVE);
// create content type criteria
String version1 = "version1";
String type1 = "type1";
List<MockTypeVersionsExtension> extensions = new ArrayList<>();
MockTypeVersionsExtension ext1 = new MockTypeVersionsExtension();
List<String> ext1Versions = ext1.getVersions();
ext1Versions.add(version1);
ext1.setExtensionTypeName(type1);
extensions.add(ext1);
query.addTypeFilter(extensions);
SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
LOGGER.debug("Resulting Predicate: {}", pred);
Filter filter = query.getFilter();
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
String filterXml = transform.transform(filter);
LOGGER.debug(filterXml);
// input that passes both temporal and content type
LOGGER.debug("\npass temporal and pass content type.\n");
MetacardImpl metacard = new MetacardImpl();
metacard.setCreatedDate(new Date());
metacard.setExpirationDate(new Date());
metacard.setModifiedDate(new Date());
metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
Date effectiveDate = cal.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate);
HashMap<String, Object> properties = new HashMap<>();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
Map<String, Object> contextualMap = constructContextualMap(metacard);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
// Above Pulled from PubSubProviderImpl
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
Event testEvent = new Event("topic", properties);
boolean b = pred.matches(testEvent);
assertTrue(b);
// input that fails both temporal and content type
LOGGER.debug("\nfail temporal. fail content type.\n");
// time out of
XMLGregorianCalendar cal1 = df.newXMLGregorianCalendarDate(2012, 10, 30, 0);
// range
Date effectiveDate1 = cal1.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate1);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "invalid_type" + "," + version1);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
// input that passes temporal and fails content type
LOGGER.debug("\npass temporal. fail content type\n");
// time in
XMLGregorianCalendar cal2 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
// range
Date effectiveDate2 = cal2.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate2);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "invalid_type" + "," + version1);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
// input that fails temporal and passes content type
LOGGER.debug("\nfail temporal. pass content type\n");
// time out of
XMLGregorianCalendar cal3 = df.newXMLGregorianCalendarDate(2012, 10, 26, 0);
// range
Date effectiveDate3 = cal3.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate3);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
// multiple content types
LOGGER.debug("\nTesting multiple content types.\n");
String type2 = "type2";
String version2 = "version2";
MockTypeVersionsExtension ext2 = new MockTypeVersionsExtension();
List<String> ext2Versions = ext2.getVersions();
ext2Versions.add(version2);
ext2.setExtensionTypeName(type2);
extensions.add(ext2);
// No version
String type3 = "type3";
MockTypeVersionsExtension ext3 = new MockTypeVersionsExtension();
ext3.setExtensionTypeName(type3);
extensions.add(ext3);
MockQuery query2 = new MockQuery();
query2.addTemporalFilter(start, end, Metacard.EFFECTIVE);
query2.addTypeFilter(extensions);
SubscriptionFilterVisitor visitor1 = new SubscriptionFilterVisitor();
Predicate pred1 = (Predicate) query2.getFilter().accept(visitor1, null);
LOGGER.debug("resulting predicate: " + pred1);
// Create metacard for input
// time and contentType match
// time in
XMLGregorianCalendar cal4 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
// range
Date effectiveDate4 = cal4.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate4);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertTrue(pred1.matches(testEvent));
// time and contentType match against content type 3 with any version
// time in
XMLGregorianCalendar cal5 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
// range
Date effectiveDate5 = cal5.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate5);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type3 + "," + "random_version");
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertTrue(pred1.matches(testEvent));
// time matches and contentType matches type2
// time in
XMLGregorianCalendar cal6 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
// range
Date effectiveDate6 = cal6.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate6);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version2);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertTrue(pred1.matches(testEvent));
// time matches and content type doesn't match
// time in
XMLGregorianCalendar cal7 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
// range
Date effectiveDate7 = cal7.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate7);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version1);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertFalse(pred1.matches(testEvent));
LOGGER.debug("*************** END: {} *****************", methodName);
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class TestPubSubOgcFilter method printFilter.
private void printFilter(Filter filter) throws TransformerException {
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
LOGGER.debug(transform.transform(filter));
}
Aggregations