Search in sources :

Example 31 with Event

use of org.osgi.service.event.Event in project ddf by codice.

the class PredicateTest method testMultipleCriteria.

@Test
public void testMultipleCriteria() throws Exception {
    String methodName = "testMultipleCriteria";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    // test with temporal, spatial, and entry
    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.MODIFIED);
    String wkt = "POLYGON((0 10, 0 0, 10 0, 10 10, 0 10))";
    query.addSpatialFilter(wkt, 0.0, "Meter", "CONTAINS");
    // create entry criteria
    String catalogId = "ABC123";
    query.addEntryFilter(catalogId);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setLocation("POINT(5 5)");
    metacard.setId(catalogId);
    metacard.setCreatedDate(new Date());
    metacard.setExpirationDate(new Date());
    metacard.setEffectiveDate(new Date());
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    Date modifiedDate = cal.toGregorianCalendar().getTime();
    metacard.setModifiedDate(modifiedDate);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    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
    Event testEvent = new Event("topic", properties);
    // input passes temporal, id, and geo
    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);
    assertTrue(pred.matches(testEvent));
    // input passes temporal, id, but fails geo
    // geo out of range
    metacard.setLocation("POINT(5 50)");
    properties.clear();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    // Below Pulled from PubSubProviderImpl
    contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // input passes geo, and id, but fails temporal
    metacard.setLocation("POINT(5 5)");
    XMLGregorianCalendar cal1 = df.newXMLGregorianCalendarDate(2011, 10, 28, 0);
    Date modifiedDate1 = cal1.toGregorianCalendar().getTime();
    // date out of range
    metacard.setModifiedDate(modifiedDate1);
    properties.clear();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    // Below Pulled from PubSubProviderImpl
    contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // input passes temporal, geo, but fails id
    XMLGregorianCalendar cal2 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    Date modifiedDate2 = cal2.toGregorianCalendar().getTime();
    metacard.setModifiedDate(modifiedDate2);
    // bad id
    metacard.setId("invalid_id");
    properties.clear();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    // Below Pulled from PubSubProviderImpl
    contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) HashMap(java.util.HashMap) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Filter(org.opengis.filter.Filter) Event(org.osgi.service.event.Event) FilterTransformer(org.geotools.filter.FilterTransformer) Test(org.junit.Test)

Example 32 with Event

use of org.osgi.service.event.Event in project ddf by codice.

the class PredicateTest method testContextualQuerySpecialNotMatch.

@Test
public void testContextualQuerySpecialNotMatch() throws IOException {
    for (String term : Arrays.asList(LEADING_TERM, TRAILING_TERM, EMBEDDED_TERM)) {
        for (Character specialChar : ContextualTokenizer.SPECIAL_CHARACTERS_SET) {
            String phrase = String.format(term, specialChar);
            for (Character differentSpecialChar : ContextualTokenizer.SPECIAL_CHARACTERS_SET) {
                if (specialChar != differentSpecialChar) {
                    String metadata = String.format(METADATA_FORMAT, StringEscapeUtils.escapeXml(String.format(term, differentSpecialChar)));
                    Predicate predicate = getPredicate("\"" + phrase + "\"");
                    Event testEvent = getEvent(metadata);
                    assertThat(phrase + " matched", predicate.matches(testEvent), is(equalTo(false)));
                }
            }
        }
    }
}
Also used : Event(org.osgi.service.event.Event) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) Test(org.junit.Test)

Example 33 with Event

use of org.osgi.service.event.Event in project ddf by codice.

the class PredicateTest method testGeospatialPredicateNullOperation.

@Test
public void testGeospatialPredicateNullOperation() throws IOException {
    String methodName = "testGeospatialPredicateNullOperation";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    String geometryWkt = "POLYGON ((40 34, 40 33, 44.5 33, 44.5 34, 40 34))";
    String operation = "overlaps";
    double distance = 0.0;
    GeospatialPredicate predicate = new GeospatialPredicate(geometryWkt, operation, distance);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setLocation("POINT (41 34)");
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "type1,version1");
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, null);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    Event testEvent = new Event("topic", properties);
    assertTrue(predicate.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : HashMap(java.util.HashMap) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) Event(org.osgi.service.event.Event) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 34 with Event

use of org.osgi.service.event.Event in project ddf by codice.

the class PredicateTest method testContentTypeFilterTypeOnly.

@Test
public void testContentTypeFilterTypeOnly() throws Exception {
    String methodName = "testContentTypeFilterTypeOnly";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    String type1 = "type_1";
    List<MockTypeVersionsExtension> extensions = new ArrayList<>();
    MockTypeVersionsExtension ext1 = new MockTypeVersionsExtension();
    ext1.setExtensionTypeName(type1);
    extensions.add(ext1);
    MockQuery query = new MockQuery();
    query.addTypeFilter(extensions);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    ContentTypePredicate pred = (ContentTypePredicate) query.getFilter().accept(visitor, null);
    assertEquals(type1, pred.getType());
    assertNull(pred.getVersion());
    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
    // handle null case
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, null);
    Event testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // handle content type
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + ",");
    testEvent = new Event("topic", properties);
    assertTrue(pred.matches(testEvent));
    // handle content version that matches content type
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "," + type1);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    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 + "," + "random_version");
    testEvent = new Event("topic", properties);
    assertTrue(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "unmatchingtype" + "," + "random_version");
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    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
    "," + "unmatchingversion");
    // input
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Event(org.osgi.service.event.Event) Test(org.junit.Test)

Example 35 with Event

use of org.osgi.service.event.Event in project ddf by codice.

the class PredicateTest method testTemporal.

@Test
public void testTemporal() throws Exception {
    String methodName = "testTemporal";
    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());
    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_ENTRY_KEY, metacard);
    Event testEvent = new Event("topic", properties);
    boolean b = pred.matches(testEvent);
    assertTrue(b);
    // input that fails temporal
    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_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Filter(org.opengis.filter.Filter) Event(org.osgi.service.event.Event) FilterTransformer(org.geotools.filter.FilterTransformer) Test(org.junit.Test)

Aggregations

Event (org.osgi.service.event.Event)142 Test (org.junit.Test)79 HashMap (java.util.HashMap)48 Hashtable (java.util.Hashtable)44 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)23 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)20 EventAdmin (org.osgi.service.event.EventAdmin)19 GeospatialPredicate (ddf.catalog.pubsub.predicate.GeospatialPredicate)18 Predicate (ddf.catalog.pubsub.predicate.Predicate)16 ArrayList (java.util.ArrayList)16 ActivityEvent (org.codice.ddf.activities.ActivityEvent)13 EventHandler (org.osgi.service.event.EventHandler)13 SubscriptionFilterVisitor (ddf.catalog.pubsub.internal.SubscriptionFilterVisitor)11 Date (java.util.Date)11 Job (org.apache.sling.event.jobs.Job)9 Map (java.util.Map)8 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 JobManager (org.apache.sling.event.jobs.JobManager)7 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)7