Search in sources :

Example 1 with InvestmentMadeEvent

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

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

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

the class Session method invest.

/**
 * Request {@link ControlApi} to invest in a given loan, leveraging the {@link ConfirmationProvider}.
 * @param recommendation Loan to invest into.
 * @return True if investment successful. The investment is reflected in {@link #getResult()}.
 */
public boolean invest(final RecommendedLoan recommendation) {
    final LoanDescriptor loan = recommendation.descriptor();
    final int loanId = loan.item().getId();
    if (portfolioOverview.getCzkAvailable() < recommendation.amount().intValue()) {
        // should not be allowed by the calling code
        return false;
    }
    Events.fire(new InvestmentRequestedEvent(recommendation));
    final boolean seenBefore = state.getSeenLoans().stream().anyMatch(l -> isSameLoan(l, loanId));
    final ZonkyResponse response = investor.invest(recommendation, seenBefore);
    Session.LOGGER.debug("Response for loan {}: {}.", loanId, response);
    final String providerId = investor.getConfirmationProviderId().orElse("-");
    switch(response.getType()) {
        case REJECTED:
            return investor.getConfirmationProviderId().map(c -> {
                Events.fire(new InvestmentRejectedEvent(recommendation, providerId));
                // rejected through a confirmation provider => forget
                discard(loan);
                return false;
            }).orElseGet(() -> {
                // rejected due to no confirmation provider => make available for direct investment later
                Events.fire(new InvestmentSkippedEvent(recommendation));
                Session.LOGGER.debug("Loan #{} protected by CAPTCHA, will check back later.", loanId);
                skip(loan);
                return false;
            });
        case DELEGATED:
            final Event e = new InvestmentDelegatedEvent(recommendation, providerId);
            Events.fire(e);
            if (recommendation.isConfirmationRequired()) {
                // confirmation required, delegation successful => forget
                discard(loan);
            } else {
                // confirmation not required, delegation successful => make available for direct investment later
                skip(loan);
            }
            return false;
        case INVESTED:
            final int confirmedAmount = response.getConfirmedAmount().getAsInt();
            final Investment i = Investment.fresh(recommendation.descriptor().item(), confirmedAmount);
            markSuccessfulInvestment(i);
            Events.fire(new InvestmentMadeEvent(i, loan.item(), portfolioOverview));
            return true;
        case // still protected by CAPTCHA
        SEEN_BEFORE:
            return false;
        default:
            throw new IllegalStateException("Not possible.");
    }
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) TransactionCategory(com.github.robozonky.api.remote.enums.TransactionCategory) LoggerFactory(org.slf4j.LoggerFactory) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) ControlApi(com.github.robozonky.api.remote.ControlApi) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) Events(com.github.robozonky.app.Events) Authenticated(com.github.robozonky.app.authentication.Authenticated) Defaults(com.github.robozonky.internal.api.Defaults) ConfirmationProvider(com.github.robozonky.api.confirmations.ConfirmationProvider) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Logger(org.slf4j.Logger) Collection(java.util.Collection) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) Collectors(java.util.stream.Collectors) Event(com.github.robozonky.api.notifications.Event) List(java.util.List) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Collections(java.util.Collections) LoanRecommendedEvent(com.github.robozonky.api.notifications.LoanRecommendedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) Event(com.github.robozonky.api.notifications.Event) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) LoanRecommendedEvent(com.github.robozonky.api.notifications.LoanRecommendedEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent)

Example 4 with InvestmentMadeEvent

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

the class JmxListenerServiceTest method getParametersForInvestmentMade.

private DynamicTest getParametersForInvestmentMade() {
    final Loan l = mockLoan();
    final Investment i = Investment.fresh((MarketplaceLoan) l, 200);
    final PortfolioOverview po = PortfolioOverview.calculate(BigDecimal.valueOf(1000), Stream::empty);
    final Event evt = new InvestmentMadeEvent(i, l, po);
    final Consumer<SoftAssertions> before = (softly) -> {
        final OperationsMBean mbean = getOperationsMBean();
        softly.assertThat(mbean.getSuccessfulInvestments()).isEmpty();
    };
    final Consumer<SoftAssertions> after = (softly) -> {
        final OperationsMBean mbean = getOperationsMBean();
        softly.assertThat(mbean.getSuccessfulInvestments()).containsOnlyKeys(l.getId());
        softly.assertThat(mbean.getLatestUpdatedDateTime()).isEqualTo(evt.getCreatedOn());
    };
    return getParameters(evt, before, after);
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) SoftAssertions(org.assertj.core.api.SoftAssertions) TestFactory(org.junit.jupiter.api.TestFactory) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) AfterAll(org.junit.jupiter.api.AfterAll) BigDecimal(java.math.BigDecimal) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) SessionInfo(com.github.robozonky.api.notifications.SessionInfo) BeforeAll(org.junit.jupiter.api.BeforeAll) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) Lifecycle(com.github.robozonky.app.runtime.Lifecycle) Assertions(org.assertj.core.api.Assertions) ShutdownHook(com.github.robozonky.app.ShutdownHook) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Event(com.github.robozonky.api.notifications.Event) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) Stream(java.util.stream.Stream) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) EventListener(com.github.robozonky.api.notifications.EventListener) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) DynamicTest(org.junit.jupiter.api.DynamicTest) Optional(java.util.Optional) ReturnCode(com.github.robozonky.api.ReturnCode) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) RoboZonkyEndingEvent(com.github.robozonky.api.notifications.RoboZonkyEndingEvent) AbstractRoboZonkyTest(com.github.robozonky.test.AbstractRoboZonkyTest) Collections(java.util.Collections) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) SoftAssertions(org.assertj.core.api.SoftAssertions) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Event(com.github.robozonky.api.notifications.Event) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) RoboZonkyEndingEvent(com.github.robozonky.api.notifications.RoboZonkyEndingEvent) Stream(java.util.stream.Stream) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent)

Example 5 with InvestmentMadeEvent

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

the class SessionTest method investmentSuccessful.

@Test
void investmentSuccessful() {
    final int oldBalance = 10_000;
    final int amountToInvest = 200;
    final RecommendedLoan r = AbstractZonkyLeveragingTest.mockLoanDescriptor().recommend(amountToInvest).get();
    final Zonky z = AbstractZonkyLeveragingTest.harmlessZonky(oldBalance);
    when(z.getLoan(eq(r.descriptor().item().getId()))).thenReturn(r.descriptor().item());
    final Authenticated auth = mockAuthentication(z);
    final Investor p = mock(Investor.class);
    doReturn(new ZonkyResponse(amountToInvest)).when(p).invest(eq(r), anyBoolean());
    doReturn(Optional.of("something")).when(p).getConfirmationProviderId();
    final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
    final Session t = new Session(portfolio, Collections.emptySet(), p, auth);
    final boolean result = t.invest(r);
    assertThat(result).isTrue();
    final List<Investment> investments = t.getResult();
    assertThat(investments).hasSize(1);
    assertThat(investments.get(0).getOriginalPrincipal().intValue()).isEqualTo(amountToInvest);
    // validate event sequence
    final List<Event> newEvents = this.getNewEvents();
    assertThat(newEvents).hasSize(2);
    assertSoftly(softly -> {
        softly.assertThat(newEvents.get(0)).isInstanceOf(InvestmentRequestedEvent.class);
        softly.assertThat(newEvents.get(1)).isInstanceOf(InvestmentMadeEvent.class);
    });
    // validate event contents
    final InvestmentMadeEvent e = (InvestmentMadeEvent) newEvents.get(1);
    assertThat(e.getPortfolioOverview().getCzkAvailable()).isEqualTo(oldBalance - amountToInvest);
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Zonky(com.github.robozonky.common.remote.Zonky) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) Event(com.github.robozonky.api.notifications.Event) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) LoanRecommendedEvent(com.github.robozonky.api.notifications.LoanRecommendedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Test(org.junit.jupiter.api.Test)

Aggregations

InvestmentDelegatedEvent (com.github.robozonky.api.notifications.InvestmentDelegatedEvent)5 InvestmentMadeEvent (com.github.robozonky.api.notifications.InvestmentMadeEvent)5 InvestmentRejectedEvent (com.github.robozonky.api.notifications.InvestmentRejectedEvent)5 Event (com.github.robozonky.api.notifications.Event)4 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)4 ExecutionStartedEvent (com.github.robozonky.api.notifications.ExecutionStartedEvent)4 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)4 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)4 InvestmentPurchasedEvent (com.github.robozonky.api.notifications.InvestmentPurchasedEvent)3 InvestmentSkippedEvent (com.github.robozonky.api.notifications.InvestmentSkippedEvent)3 SaleOfferedEvent (com.github.robozonky.api.notifications.SaleOfferedEvent)3 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)3 Collections (java.util.Collections)3 EventListener (com.github.robozonky.api.notifications.EventListener)2 EventListenerSupplier (com.github.robozonky.api.notifications.EventListenerSupplier)2 InvestmentRequestedEvent (com.github.robozonky.api.notifications.InvestmentRequestedEvent)2 LoanRecommendedEvent (com.github.robozonky.api.notifications.LoanRecommendedEvent)2 PurchasingCompletedEvent (com.github.robozonky.api.notifications.PurchasingCompletedEvent)2 PurchasingStartedEvent (com.github.robozonky.api.notifications.PurchasingStartedEvent)2 RoboZonkyEndingEvent (com.github.robozonky.api.notifications.RoboZonkyEndingEvent)2