Search in sources :

Example 1 with EventListener

use of com.github.robozonky.api.notifications.EventListener in project robozonky by RoboZonky.

the class JmxListenerService method newListener.

private static <T extends Event> EventListener<T> newListener(final Class<T> eventType) {
    if (Objects.equals(eventType, ExecutionStartedEvent.class)) {
        return (event, sessionInfo) -> callOnPortfolio(bean -> bean.handle((ExecutionStartedEvent) event));
    } else if (Objects.equals(eventType, ExecutionCompletedEvent.class)) {
        return (event, sessionInfo) -> {
            final ExecutionCompletedEvent evt = (ExecutionCompletedEvent) event;
            callOnRuntime(bean -> bean.handle(evt, sessionInfo));
            callOnPortfolio(bean -> bean.handle(evt));
        };
    } else if (Objects.equals(eventType, SellingStartedEvent.class)) {
        return (event, sessionInfo) -> callOnPortfolio(bean -> bean.handle((SellingStartedEvent) event));
    } else if (Objects.equals(eventType, SellingCompletedEvent.class)) {
        return (event, sessionInfo) -> callOnPortfolio(bean -> bean.handle((SellingCompletedEvent) event));
    } else if (Objects.equals(eventType, PurchasingStartedEvent.class)) {
        return (event, sessionInfo) -> callOnPortfolio(bean -> bean.handle((PurchasingStartedEvent) event));
    } else if (Objects.equals(eventType, PurchasingCompletedEvent.class)) {
        return (event, sessionInfo) -> callOnPortfolio(bean -> bean.handle((PurchasingCompletedEvent) event));
    } else if (Objects.equals(eventType, InvestmentDelegatedEvent.class)) {
        return (event, sessionInfo) -> callOnOperations(bean -> bean.handle((InvestmentDelegatedEvent) event));
    } else if (Objects.equals(eventType, InvestmentRejectedEvent.class)) {
        return (event, sessionInfo) -> callOnOperations(bean -> bean.handle((InvestmentRejectedEvent) event));
    } else if (Objects.equals(eventType, InvestmentMadeEvent.class)) {
        return (event, sessionInfo) -> callOnOperations(bean -> bean.handle((InvestmentMadeEvent) event));
    } else if (Objects.equals(eventType, SaleOfferedEvent.class)) {
        return (event, sessionInfo) -> callOnOperations(bean -> bean.handle((SaleOfferedEvent) event));
    } else if (Objects.equals(eventType, InvestmentPurchasedEvent.class)) {
        return (event, sessionInfo) -> callOnOperations(bean -> bean.handle((InvestmentPurchasedEvent) event));
    } else {
        return null;
    }
}
Also used : EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) Logger(org.slf4j.Logger) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent) LoggerFactory(org.slf4j.LoggerFactory) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) ListenerService(com.github.robozonky.api.notifications.ListenerService) Event(com.github.robozonky.api.notifications.Event) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) Objects(java.util.Objects) Consumer(java.util.function.Consumer) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) Map(java.util.Map) EventListener(com.github.robozonky.api.notifications.EventListener) PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) Optional(java.util.Optional) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) Collections(java.util.Collections) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent)

Example 2 with EventListener

use of com.github.robozonky.api.notifications.EventListener in project robozonky by RoboZonky.

the class Checker method notifications.

public static boolean notifications(final String username, final List<EventListenerSupplier<RoboZonkyTestingEvent>> refreshables) {
    final Collection<EventListener<RoboZonkyTestingEvent>> listeners = refreshables.stream().flatMap(r -> r.get().map(Stream::of).orElse(Stream.empty())).collect(Collectors.toSet());
    if (listeners.size() > 0) {
        final SessionInfo sessionInfo = new SessionInfo(username);
        final RoboZonkyTestingEvent evt = new RoboZonkyTestingEvent();
        listeners.forEach(l -> l.handle(evt, sessionInfo));
        return true;
    } else {
        return false;
    }
}
Also used : EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) Logger(org.slf4j.Logger) Collection(java.util.Collection) ApiProvider(com.github.robozonky.common.remote.ApiProvider) LoggerFactory(org.slf4j.LoggerFactory) RawLoan(com.github.robozonky.api.remote.entities.RawLoan) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) SessionInfo(com.github.robozonky.api.notifications.SessionInfo) List(java.util.List) Stream(java.util.stream.Stream) RequestId(com.github.robozonky.api.confirmations.RequestId) EventListener(com.github.robozonky.api.notifications.EventListener) Optional(java.util.Optional) RoboZonkyTestingEvent(com.github.robozonky.api.notifications.RoboZonkyTestingEvent) Comparator(java.util.Comparator) ConfirmationProvider(com.github.robozonky.api.confirmations.ConfirmationProvider) SessionInfo(com.github.robozonky.api.notifications.SessionInfo) EventListener(com.github.robozonky.api.notifications.EventListener) RoboZonkyTestingEvent(com.github.robozonky.api.notifications.RoboZonkyTestingEvent)

Example 3 with EventListener

use of com.github.robozonky.api.notifications.EventListener in project robozonky by RoboZonky.

the class ListenerServiceLoaderTest method correctLoading.

@Test
void correctLoading() {
    final RoboZonkyStartingEvent e = new RoboZonkyStartingEvent();
    final EventListener<RoboZonkyStartingEvent> l = mock(EventListener.class);
    final ListenerService s1 = mock(ListenerService.class);
    final EventListenerSupplier<RoboZonkyStartingEvent> returned = () -> Optional.of(l);
    doReturn(returned).when(s1).findListener(eq(e.getClass()));
    final ListenerService s2 = mock(ListenerService.class);
    doReturn((EventListenerSupplier<RoboZonkyStartingEvent>) Optional::empty).when(s2).findListener(eq(e.getClass()));
    final Iterable<ListenerService> s = () -> Arrays.asList(s1, s2).iterator();
    final List<EventListenerSupplier<RoboZonkyStartingEvent>> r = ListenerServiceLoader.load(RoboZonkyStartingEvent.class, s);
    assertThat(r).hasSize(2);
    assertThat(r).first().has(new Condition<>(result -> result.get().isPresent() && result.get().get() == l, "Exists"));
    assertThat(r).last().has(new Condition<>(result -> !result.get().isPresent(), "Does not exist"));
}
Also used : ListenerService(com.github.robozonky.api.notifications.ListenerService) Test(org.junit.jupiter.api.Test) EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) Mockito(org.mockito.Mockito) Arrays(java.util.Arrays) List(java.util.List) RoboZonkyStartingEvent(com.github.robozonky.api.notifications.RoboZonkyStartingEvent) EventListener(com.github.robozonky.api.notifications.EventListener) Condition(org.assertj.core.api.Condition) Optional(java.util.Optional) Assertions(org.assertj.core.api.Assertions) RoboZonkyTestingEvent(com.github.robozonky.api.notifications.RoboZonkyTestingEvent) ListenerService(com.github.robozonky.api.notifications.ListenerService) RoboZonkyStartingEvent(com.github.robozonky.api.notifications.RoboZonkyStartingEvent) EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) Test(org.junit.jupiter.api.Test)

Aggregations

EventListener (com.github.robozonky.api.notifications.EventListener)3 EventListenerSupplier (com.github.robozonky.api.notifications.EventListenerSupplier)3 Optional (java.util.Optional)3 ListenerService (com.github.robozonky.api.notifications.ListenerService)2 RoboZonkyTestingEvent (com.github.robozonky.api.notifications.RoboZonkyTestingEvent)2 List (java.util.List)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ConfirmationProvider (com.github.robozonky.api.confirmations.ConfirmationProvider)1 RequestId (com.github.robozonky.api.confirmations.RequestId)1 Event (com.github.robozonky.api.notifications.Event)1 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)1 ExecutionStartedEvent (com.github.robozonky.api.notifications.ExecutionStartedEvent)1 InvestmentDelegatedEvent (com.github.robozonky.api.notifications.InvestmentDelegatedEvent)1 InvestmentMadeEvent (com.github.robozonky.api.notifications.InvestmentMadeEvent)1 InvestmentPurchasedEvent (com.github.robozonky.api.notifications.InvestmentPurchasedEvent)1 InvestmentRejectedEvent (com.github.robozonky.api.notifications.InvestmentRejectedEvent)1 PurchasingCompletedEvent (com.github.robozonky.api.notifications.PurchasingCompletedEvent)1 PurchasingStartedEvent (com.github.robozonky.api.notifications.PurchasingStartedEvent)1 RoboZonkyStartingEvent (com.github.robozonky.api.notifications.RoboZonkyStartingEvent)1