Search in sources :

Example 1 with ExecutionStartedEvent

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

the class Session method invest.

public static Collection<Investment> invest(final Portfolio portfolio, final Investor investor, final Authenticated auth, final Collection<LoanDescriptor> loans, final RestrictedInvestmentStrategy strategy) {
    final Session session = new Session(portfolio, loans, investor, auth);
    final PortfolioOverview portfolioOverview = session.portfolioOverview;
    final int balance = portfolioOverview.getCzkAvailable();
    Events.fire(new ExecutionStartedEvent(loans, portfolioOverview));
    if (balance >= Defaults.MINIMUM_INVESTMENT_IN_CZK && !session.getAvailable().isEmpty()) {
        session.invest(strategy);
    }
    final Collection<Investment> result = session.getResult();
    // make sure we get fresh portfolio reference here
    Events.fire(new ExecutionCompletedEvent(result, session.portfolioOverview));
    return Collections.unmodifiableCollection(result);
}
Also used : ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment)

Example 2 with ExecutionStartedEvent

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

the class EmailingListenerTest method listeners.

@TestFactory
Stream<DynamicNode> listeners() throws MalformedURLException {
    // prepare data
    final Loan loan = Loan.custom().setId(66666).setAmount(100_000).setInterestRate(BigDecimal.TEN).setDatePublished(OffsetDateTime.now().minusMonths(2)).setName("Úvěr").setRegion(Region.JIHOCESKY).setPurpose(Purpose.AUTO_MOTO).setMainIncomeType(MainIncomeType.EMPLOYMENT).setRemainingInvestment(2000).setRating(Rating.AAAAA).setTermInMonths(25).setUrl(new URL("http://www.robozonky.cz")).build();
    final LoanDescriptor loanDescriptor = new LoanDescriptor(loan);
    final RecommendedLoan recommendation = loanDescriptor.recommend(1200, false).get();
    final Investment i = Investment.fresh((MarketplaceLoan) loan, 1000).build();
    // create events for listeners
    return Stream.of(forListener(SupportedListener.INVESTMENT_DELEGATED, new InvestmentDelegatedEvent(recommendation, "random")), forListener(SupportedListener.INVESTMENT_MADE, new InvestmentMadeEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.INVESTMENT_SOLD, new InvestmentSoldEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.INVESTMENT_SKIPPED, new InvestmentSkippedEvent(recommendation)), forListener(SupportedListener.INVESTMENT_REJECTED, new InvestmentRejectedEvent(recommendation, "random")), forListener(SupportedListener.LOAN_NO_LONGER_DELINQUENT, new LoanNoLongerDelinquentEvent(i, loan, LocalDate.now(), Collections.singletonList(mockCollectionNoEndDate()))), forListener(SupportedListener.LOAN_DEFAULTED, new LoanDefaultedEvent(i, loan, LocalDate.now(), Collections.singletonList(mockCollectionNoNote()))), forListener(SupportedListener.LOAN_NOW_DELINQUENT, new LoanNowDelinquentEvent(i, loan, LocalDate.now(), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_10_PLUS, new LoanDelinquent10DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(11), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_30_PLUS, new LoanDelinquent30DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(31), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_60_PLUS, new LoanDelinquent60DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(61), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_90_PLUS, new LoanDelinquent90DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(91), Collections.emptyList())), forListener(SupportedListener.LOAN_REPAID, new LoanRepaidEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.BALANCE_ON_TARGET, new ExecutionStartedEvent(Collections.emptyList(), MAX_PORTFOLIO)), forListener(SupportedListener.BALANCE_UNDER_MINIMUM, new ExecutionStartedEvent(Collections.emptyList(), mockPortfolio(0))), forListener(SupportedListener.CRASHED, new RoboZonkyCrashedEvent(ReturnCode.ERROR_UNEXPECTED, new RuntimeException())), forListener(SupportedListener.REMOTE_OPERATION_FAILED, new RemoteOperationFailedEvent(new RuntimeException())), forListener(SupportedListener.DAEMON_FAILED, new RoboZonkyDaemonFailedEvent(new RuntimeException())), forListener(SupportedListener.INITIALIZED, new RoboZonkyInitializedEvent()), forListener(SupportedListener.ENDING, new RoboZonkyEndingEvent()), forListener(SupportedListener.TESTING, new RoboZonkyTestingEvent()), forListener(SupportedListener.UPDATE_DETECTED, new RoboZonkyUpdateDetectedEvent("1.2.3")), forListener(SupportedListener.EXPERIMENTAL_UPDATE_DETECTED, new RoboZonkyExperimentalUpdateDetectedEvent("1.3.0-beta-1")), forListener(SupportedListener.INVESTMENT_PURCHASED, new InvestmentPurchasedEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.SALE_OFFERED, new SaleOfferedEvent(i, loan)));
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) InvestmentSoldEvent(com.github.robozonky.api.notifications.InvestmentSoldEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) LoanDefaultedEvent(com.github.robozonky.api.notifications.LoanDefaultedEvent) URL(java.net.URL) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) RoboZonkyEndingEvent(com.github.robozonky.api.notifications.RoboZonkyEndingEvent) RemoteOperationFailedEvent(com.github.robozonky.api.notifications.RemoteOperationFailedEvent) LoanDelinquent10DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent10DaysOrMoreEvent) LoanNoLongerDelinquentEvent(com.github.robozonky.api.notifications.LoanNoLongerDelinquentEvent) RoboZonkyDaemonFailedEvent(com.github.robozonky.api.notifications.RoboZonkyDaemonFailedEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) RoboZonkyInitializedEvent(com.github.robozonky.api.notifications.RoboZonkyInitializedEvent) RoboZonkyTestingEvent(com.github.robozonky.api.notifications.RoboZonkyTestingEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) LoanDelinquent30DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent30DaysOrMoreEvent) LoanDelinquent60DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent60DaysOrMoreEvent) RoboZonkyUpdateDetectedEvent(com.github.robozonky.api.notifications.RoboZonkyUpdateDetectedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) LoanDelinquent90DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent90DaysOrMoreEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) LoanNowDelinquentEvent(com.github.robozonky.api.notifications.LoanNowDelinquentEvent) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) RoboZonkyCrashedEvent(com.github.robozonky.api.notifications.RoboZonkyCrashedEvent) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) RoboZonkyExperimentalUpdateDetectedEvent(com.github.robozonky.api.notifications.RoboZonkyExperimentalUpdateDetectedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) TestFactory(org.junit.jupiter.api.TestFactory)

Example 3 with ExecutionStartedEvent

use of com.github.robozonky.api.notifications.ExecutionStartedEvent 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 4 with ExecutionStartedEvent

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

the class PortfolioTest method run.

@Test
void run() {
    final PortfolioOverview portfolio = mock(PortfolioOverview.class);
    when(portfolio.getCzkAvailable()).thenReturn(1000);
    when(portfolio.getCzkInvested()).thenReturn(10000);
    when(portfolio.getCzkAtRisk()).thenReturn(0);
    when(portfolio.getShareOnInvestment(any())).thenReturn(BigDecimal.ONE);
    when(portfolio.getCzkAtRisk(any())).thenReturn(0);
    when(portfolio.getAtRiskShareOnInvestment(any())).thenReturn(BigDecimal.ZERO);
    when(portfolio.getCzkInvested(any())).thenReturn(1000);
    final Portfolio mbean = new Portfolio();
    final ExecutionStartedEvent evt = new ExecutionStartedEvent(Collections.emptyList(), portfolio);
    mbean.handle(evt);
    assertSoftly(softly -> {
        softly.assertThat(mbean.getAmountAtRisk()).isEqualTo(0);
        softly.assertThat(mbean.getAvailableBalance()).isEqualTo(portfolio.getCzkAvailable());
        softly.assertThat(mbean.getInvestedAmount()).isEqualTo(portfolio.getCzkInvested());
        softly.assertThat(mbean.getLatestUpdatedDateTime()).isBeforeOrEqualTo(OffsetDateTime.now());
        // checks for proper ordering of ratings
        final String[] ratings = Stream.of(Rating.values()).map(Rating::getCode).toArray(String[]::new);
        softly.assertThat(mbean.getInvestedAmountPerRating().keySet()).containsExactly(ratings);
        softly.assertThat(mbean.getRatingShare().keySet()).containsExactly(ratings);
        // checks correct values per rating
        Stream.of(ratings).forEach(r -> {
            softly.assertThat(mbean.getInvestedAmountPerRating()).containsEntry(r, 1000);
            softly.assertThat(mbean.getShareAtRiskPerRating()).containsEntry(r, BigDecimal.ZERO);
            softly.assertThat(mbean.getAmountAtRiskPerRating()).containsEntry(r, 0);
            softly.assertThat(mbean.getRatingShare()).containsEntry(r, BigDecimal.ONE);
        });
    });
}
Also used : ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Test(org.junit.jupiter.api.Test)

Aggregations

ExecutionStartedEvent (com.github.robozonky.api.notifications.ExecutionStartedEvent)4 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)2 InvestmentDelegatedEvent (com.github.robozonky.api.notifications.InvestmentDelegatedEvent)2 InvestmentMadeEvent (com.github.robozonky.api.notifications.InvestmentMadeEvent)2 InvestmentPurchasedEvent (com.github.robozonky.api.notifications.InvestmentPurchasedEvent)2 InvestmentRejectedEvent (com.github.robozonky.api.notifications.InvestmentRejectedEvent)2 SaleOfferedEvent (com.github.robozonky.api.notifications.SaleOfferedEvent)2 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)2 PortfolioOverview (com.github.robozonky.api.strategies.PortfolioOverview)2 Event (com.github.robozonky.api.notifications.Event)1 EventListener (com.github.robozonky.api.notifications.EventListener)1 EventListenerSupplier (com.github.robozonky.api.notifications.EventListenerSupplier)1 InvestmentSkippedEvent (com.github.robozonky.api.notifications.InvestmentSkippedEvent)1 InvestmentSoldEvent (com.github.robozonky.api.notifications.InvestmentSoldEvent)1 ListenerService (com.github.robozonky.api.notifications.ListenerService)1 LoanDefaultedEvent (com.github.robozonky.api.notifications.LoanDefaultedEvent)1 LoanDelinquent10DaysOrMoreEvent (com.github.robozonky.api.notifications.LoanDelinquent10DaysOrMoreEvent)1 LoanDelinquent30DaysOrMoreEvent (com.github.robozonky.api.notifications.LoanDelinquent30DaysOrMoreEvent)1 LoanDelinquent60DaysOrMoreEvent (com.github.robozonky.api.notifications.LoanDelinquent60DaysOrMoreEvent)1 LoanDelinquent90DaysOrMoreEvent (com.github.robozonky.api.notifications.LoanDelinquent90DaysOrMoreEvent)1