EventWhiteList implements EventFilter { @Override public Optional<? extends EventMessage> filter(EventMessage event) { EventClass type = EventClass.of(event); return allowedEvents.contains(type) ? Optional.of(event) : Optional.empty(); } private  EventWhiteList(ImmutableSet<EventClass> allowedEvents); @SuppressWarnings("WeakerAccess") // Public API of the framework. @SafeVarargs static EventWhiteList allowEvents(Class<? extends EventMessage>... eventClasses); @Override Optional<? extends EventMessage> filter(EventMessage event);  }
@Test @DisplayName("allow eventFactory of white list type") void acceptAllowed() { EventMessage event = EntProjectCreated.getDefaultInstance(); Optional<? extends Message> result = whiteList.filter(event); assertTrue(result.isPresent()); assertEquals(event, result.get()); }
@Test @DisplayName("filter out non-allowed events") void filterOut() { List<Event> events = Stream.<EventMessage>of(EntProjectStarted.getDefaultInstance(), EntTaskAdded.getDefaultInstance()) .map(eventFactory::createEvent) .collect(toList()); Collection<Event> filtered = whiteList.filter(events); assertEquals(1, filtered.size()); assertTrue(events.contains(events.get(0))); }
@Test @DisplayName("not allow events out from the white list") void denyEvents() { EventMessage event = EntTaskAdded.getDefaultInstance(); Optional<? extends EventMessage> result = whiteList.filter(event); assertFalse(result.isPresent()); }