@Override public <T extends Event> Stream<EventListenerSupplier<T>> findListeners(final Class<T> eventType) { final NotificationEventListenerSupplier<T> l = new NotificationEventListenerSupplier<>(eventType); final long tenants = getTenantConfigurations() .peek(config -> config.registerListener(l)) .count(); if (tenants > 0) { return Stream.of(Target.values()).map(e -> () -> l.apply(e)); } else { return Stream.empty(); } } }
@Override public <T extends Event> Stream<EventListenerSupplier<T>> findListeners(final Class<T> eventType) { final NotificationEventListenerSupplier<T> l = new NotificationEventListenerSupplier<>(eventType); final long tenants = getTenantConfigurations() .peek(config -> config.registerListener(l)) .count(); if (tenants > 0) { return Stream.of(Target.values()).map(e -> () -> l.apply(e)); } else { return Stream.empty(); } } }
@Test void lifecycle() throws IOException { final NotificationEventListenerSupplier<RoboZonkyCrashedEvent> s = new NotificationEventListenerSupplier<>(RoboZonkyCrashedEvent.class); assertThat(s.apply(Target.EMAIL)).isEmpty(); // the listener is enabled here final ConfigStorage p = mockProperties(RoboZonkyCrashedEventListener.class.getResourceAsStream("notifications-enabled.cfg")); s.valueSet(p); assertThat(s.apply(Target.EMAIL)).isPresent(); // disabled here final ConfigStorage p2 = mockProperties(); s.valueChanged(p, p2); assertThat(s.apply(Target.EMAIL)).isEmpty(); // and re-enabled s.valueChanged(p2, p); assertThat(s.apply(Target.EMAIL)).isPresent(); s.valueUnset(p); assertThat(s.apply(Target.EMAIL)).isEmpty(); }
@Test void setDisabled() throws IOException { final NotificationEventListenerSupplier<RoboZonkyCrashedEvent> s = new NotificationEventListenerSupplier<>(RoboZonkyCrashedEvent.class); final ConfigStorage p = mockProperties(); s.valueSet(p); assertThat(s.apply(Target.EMAIL)).isEmpty(); } }