Search in sources :

Example 11 with Participation

use of com.github.robozonky.api.remote.entities.Participation in project robozonky by RoboZonky.

the class SessionTest method properReal.

@Test
void properReal() {
    final Loan l = Loan.custom().setId(1).setAmount(200).setRating(Rating.D).setRemainingInvestment(200).setMyInvestment(mockMyInvestment()).build();
    final Participation p = mock(Participation.class);
    when(p.getLoanId()).thenReturn(l.getId());
    when(p.getRemainingPrincipal()).thenReturn(BigDecimal.valueOf(200));
    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 Zonky z = mockZonky(BigDecimal.valueOf(100_000));
    when(z.getLoan(eq(l.getId()))).thenReturn(l);
    final Authenticated auth = mockAuthentication(z);
    final Portfolio portfolio = spy(Portfolio.create(z, mockBalance(z)));
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, l);
    final Collection<Investment> i = Session.purchase(portfolio, auth, Stream.of(pd), new RestrictedPurchaseStrategy(s, new Restrictions()), false);
    assertThat(i).hasSize(1);
    assertThat(this.getNewEvents()).hasSize(5);
    verify(z).purchase(eq(p));
    verify(portfolio).newBlockedAmount(eq(auth), argThat((a) -> a.getLoanId() == l.getId()));
}
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) 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) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) 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)

Example 12 with Participation

use of com.github.robozonky.api.remote.entities.Participation in project robozonky by RoboZonky.

the class Purchasing method execute.

@Override
protected Collection<Investment> execute(final Portfolio portfolio, final PurchaseStrategy strategy, final Collection<Participation> marketplace) {
    final Stream<ParticipationDescriptor> participations = marketplace.parallelStream().map(p -> toDescriptor(p, auth)).filter(d -> {
        // never re-purchase what was once sold
        final Loan l = d.related();
        final boolean wasSoldBefore = portfolio.wasOnceSold(l);
        if (wasSoldBefore) {
            LOGGER.debug("Ignoring loan #{} as the user had already sold it before.", l.getId());
        }
        return !wasSoldBefore;
    });
    final RestrictedPurchaseStrategy s = new RestrictedPurchaseStrategy(strategy, auth.getRestrictions());
    return Session.purchase(portfolio, auth, participations, s, dryRun);
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) LoanCache(com.github.robozonky.app.util.LoanCache) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Optional(java.util.Optional) Portfolio(com.github.robozonky.app.portfolio.Portfolio) StrategyExecutor(com.github.robozonky.app.util.StrategyExecutor) Participation(com.github.robozonky.api.remote.entities.Participation) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor)

Example 13 with Participation

use of com.github.robozonky.api.remote.entities.Participation 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 14 with Participation

use of com.github.robozonky.api.remote.entities.Participation in project robozonky by RoboZonky.

the class ParsedStrategyTest method mockParticipationDescriptor.

private static ParticipationDescriptor mockParticipationDescriptor(final Loan loan) {
    final Participation p = mock(Participation.class);
    doReturn(loan.getTermInMonths()).when(p).getRemainingInstalmentCount();
    return new ParticipationDescriptor(p, loan);
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor)

Example 15 with Participation

use of com.github.robozonky.api.remote.entities.Participation in project robozonky by RoboZonky.

the class RecommendedParticipationTest method equals.

@Test
void equals() {
    final Participation p = mockParticipation();
    final ParticipationDescriptor d = new ParticipationDescriptor(p);
    final RecommendedParticipation r = new RecommendedParticipation(d);
    assertSoftly(softly -> {
        softly.assertThat(r).isNotEqualTo(null);
        softly.assertThat(r).isNotEqualTo(UUID.randomUUID().toString());
        softly.assertThat(r).isEqualTo(r);
    });
    final RecommendedParticipation r2 = new RecommendedParticipation(d);
    assertSoftly(softly -> {
        softly.assertThat(r).isEqualTo(r2);
        softly.assertThat(r2).isEqualTo(r);
    });
    final RecommendedParticipation r3 = new RecommendedParticipation(new ParticipationDescriptor(mockParticipation()));
    assertThat(r).isNotEqualTo(r3);
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) Test(org.junit.jupiter.api.Test)

Aggregations

Participation (com.github.robozonky.api.remote.entities.Participation)24 Test (org.junit.jupiter.api.Test)18 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)8 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)8 PurchaseRequestedEvent (com.github.robozonky.api.notifications.PurchaseRequestedEvent)7 ParticipationDescriptor (com.github.robozonky.api.strategies.ParticipationDescriptor)7 Portfolio (com.github.robozonky.app.portfolio.Portfolio)7 Event (com.github.robozonky.api.notifications.Event)6 PurchaseStrategy (com.github.robozonky.api.strategies.PurchaseStrategy)6 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)6 CollectionsApi (com.github.robozonky.api.remote.CollectionsApi)5 ControlApi (com.github.robozonky.api.remote.ControlApi)5 LoanApi (com.github.robozonky.api.remote.LoanApi)5 ParticipationApi (com.github.robozonky.api.remote.ParticipationApi)5 PortfolioApi (com.github.robozonky.api.remote.PortfolioApi)5 WalletApi (com.github.robozonky.api.remote.WalletApi)5 BlockedAmount (com.github.robozonky.api.remote.entities.BlockedAmount)5 RawDevelopment (com.github.robozonky.api.remote.entities.RawDevelopment)5 RawInvestment (com.github.robozonky.api.remote.entities.RawInvestment)5 RawLoan (com.github.robozonky.api.remote.entities.RawLoan)5