EventBlackList implements EventFilter { @Override public Optional<? extends EventMessage> filter(EventMessage event) { EventClass type = EventClass.of(event); return forbiddenEvents.contains(type) ? Optional.empty() : Optional.of(event); } private  EventBlackList(ImmutableSet<EventClass> forbiddenEvents); @SafeVarargs static EventBlackList discardEvents(Class<? extends EventMessage>... eventClasses); @Override Optional<? extends EventMessage> filter(EventMessage event);  }
@Test @DisplayName("allow events of type not from the list") void allowArbitrary() { EventMessage event = EntProjectCreated.getDefaultInstance(); Optional<? extends EventMessage> filtered = blackList.filter(event); assertTrue(filtered.isPresent()); assertEquals(event, filtered.get()); }
@Test @DisplayName("not allow events of type from the list") void notAllowFromList() { EventMessage event = EntTaskAdded.getDefaultInstance(); Optional<? extends EventMessage> filtered = blackList.filter(event); assertFalse(filtered.isPresent()); }
@Test @DisplayName("filter out events from bulk") void filterOut() { List<Event> events = Stream.<EventMessage>of(EntProjectCreated.getDefaultInstance(), EntTaskAdded.getDefaultInstance()) .map(eventFactory::createEvent) .collect(toList()); Collection<Event> filtered = blackList.filter(events); assertEquals(1, filtered.size()); assertTrue(events.contains(events.get(0))); }