ListenerServiceLoader { static <T extends Event> List<EventListenerSupplier<T>> load(final SessionInfo sessionInfo, final Class<T> eventType, final Iterable<ListenerService> loader) { LOGGER.debug("Loading listeners for {}.", eventType); return StreamUtil.toStream(loader) .peek(s -> ListenerServiceLoader.LOGGER.debug("Processing '{}'.", s.getClass())) .flatMap(s -> s.findListeners(sessionInfo, eventType)) .filter(Objects::nonNull) .collect(Collectors.toList()); } private  ListenerServiceLoader(); static Optional<String> getNotificationConfiguration(final SessionInfo session); static void registerConfiguration(final SessionInfo session, final URL configurationLocation); static void registerConfiguration(final SessionInfo session,
            final String configurationLocation); static void unregisterConfiguration(final SessionInfo session); static List<EventListenerSupplier<T>> load(final SessionInfo sessionInfo,
            final Class<T> eventType);  }
@Test void correctLoading() { final ListenerService s1 = mock(ListenerService.class); final EventListenerSupplier<RoboZonkyStartingEvent> returned = () -> Optional.of(l); doAnswer(i -> Stream.of(returned)).when(s1) .findListeners(eq(sessionInfo), eq(RoboZonkyStartingEvent.class)); final ListenerService s2 = mock(ListenerService.class); doAnswer(i -> Stream.of((EventListenerSupplier<RoboZonkyStartingEvent>) Optional::empty)) .when(s2) .findListeners(eq(sessionInfo), eq(RoboZonkyStartingEvent.class)); final Iterable<ListenerService> s = () -> Arrays.asList(s1, s2) .iterator(); final List<EventListenerSupplier<RoboZonkyStartingEvent>> r = ListenerServiceLoader.load(sessionInfo, RoboZonkyStartingEvent.class, s); assertThat(r).hasSize(2); assertThat(r) .first() .has(new Condition<>(result -> result.get() .isPresent() && Objects.equals(result.get() .get(), l), "Exists")); assertThat(r) .last() .has(new Condition<>(result -> result.get() .isEmpty(), "Does not exist")); }
@Test void empty() { final List<EventListenerSupplier<RoboZonkyTestingEvent>> r = ListenerServiceLoader.load(sessionInfo, RoboZonkyTestingEvent.class); assertThat(r).isEmpty(); }