Search in sources :

Example 1 with PurchaseRequestedEvent

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

the class Session method purchase.

public boolean purchase(final RecommendedParticipation recommendation) {
    Events.fire(new PurchaseRequestedEvent(recommendation));
    final Participation participation = recommendation.descriptor().item();
    final Loan loan = recommendation.descriptor().related();
    final boolean purchased = isDryRun || actualPurchase(participation);
    if (purchased) {
        final Investment i = Investment.fresh(participation, loan, recommendation.amount());
        markSuccessfulPurchase(i);
        Events.fire(new InvestmentPurchasedEvent(i, loan, portfolioOverview));
    }
    return purchased;
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) RecommendedParticipation(com.github.robozonky.api.strategies.RecommendedParticipation) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment)

Example 2 with PurchaseRequestedEvent

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

the class SessionTest method underBalance.

@Test
void underBalance() {
    final Participation p = mock(Participation.class);
    when(p.getRemainingPrincipal()).thenReturn(BigDecimal.valueOf(200));
    final Loan l = Loan.custom().build();
    final PurchaseStrategy s = mock(PurchaseStrategy.class);
    when(s.recommend(any(), any(), any())).thenAnswer(i -> {
        final Collection<ParticipationDescriptor> participations = i.getArgument(0);
        return participations.stream().map(ParticipationDescriptor::recommend).flatMap(o -> o.map(Stream::of).orElse(Stream.empty()));
    });
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, l);
    final Zonky z = mockZonky();
    final Authenticated auth = mockAuthentication(z);
    final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
    final Collection<Investment> i = Session.purchase(portfolio, auth, Stream.of(pd), new RestrictedPurchaseStrategy(s, new Restrictions()), true);
    assertSoftly(softly -> {
        softly.assertThat(i).isEmpty();
        softly.assertThat(this.getNewEvents()).has(new Condition<List<? extends Event>>() {

            @Override
            public boolean matches(final List<? extends Event> events) {
                return events.stream().noneMatch(e -> e instanceof PurchaseRequestedEvent);
            }
        });
    });
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) Collection(java.util.Collection) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Wallet(com.github.robozonky.api.remote.entities.Wallet) Zonky(com.github.robozonky.common.remote.Zonky) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Event(com.github.robozonky.api.notifications.Event) Test(org.junit.jupiter.api.Test) BigDecimal(java.math.BigDecimal) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Condition(org.assertj.core.api.Condition) Rating(com.github.robozonky.api.remote.enums.Rating) Assertions(org.assertj.core.api.Assertions) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Participation(com.github.robozonky.api.remote.entities.Participation) Participation(com.github.robozonky.api.remote.entities.Participation) Authenticated(com.github.robozonky.app.authentication.Authenticated) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Zonky(com.github.robozonky.common.remote.Zonky) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Event(com.github.robozonky.api.notifications.Event) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) List(java.util.List) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Aggregations

PurchaseRequestedEvent (com.github.robozonky.api.notifications.PurchaseRequestedEvent)2 Participation (com.github.robozonky.api.remote.entities.Participation)2 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)2 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)2 Event (com.github.robozonky.api.notifications.Event)1 InvestmentPurchasedEvent (com.github.robozonky.api.notifications.InvestmentPurchasedEvent)1 Restrictions (com.github.robozonky.api.remote.entities.Restrictions)1 Wallet (com.github.robozonky.api.remote.entities.Wallet)1 Rating (com.github.robozonky.api.remote.enums.Rating)1 ParticipationDescriptor (com.github.robozonky.api.strategies.ParticipationDescriptor)1 PurchaseStrategy (com.github.robozonky.api.strategies.PurchaseStrategy)1 RecommendedParticipation (com.github.robozonky.api.strategies.RecommendedParticipation)1 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)1 Authenticated (com.github.robozonky.app.authentication.Authenticated)1 Portfolio (com.github.robozonky.app.portfolio.Portfolio)1 Zonky (com.github.robozonky.common.remote.Zonky)1 BigDecimal (java.math.BigDecimal)1 Collection (java.util.Collection)1 List (java.util.List)1 Stream (java.util.stream.Stream)1