use of com.github.robozonky.api.strategies.ParticipationDescriptor 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);
}
use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.
the class Session method purchase.
public static Collection<Investment> purchase(final Portfolio portfolio, final Authenticated auth, final Stream<ParticipationDescriptor> items, final RestrictedPurchaseStrategy strategy, final boolean dryRun) {
final Session session = new Session(portfolio, items, auth, dryRun);
final Collection<ParticipationDescriptor> c = session.getAvailable();
if (c.isEmpty()) {
return Collections.emptyList();
}
Events.fire(new PurchasingStartedEvent(c, session.portfolioOverview));
session.purchase(strategy);
final Collection<Investment> result = session.getResult();
Events.fire(new PurchasingCompletedEvent(result, session.portfolioOverview));
return Collections.unmodifiableCollection(result);
}
use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.
the class ParsedStrategyTest method exitButNoSelloff.
@Test
void exitButNoSelloff() {
final DefaultPortfolio portfolio = DefaultPortfolio.EMPTY;
final DefaultValues values = new DefaultValues(portfolio);
// exit active, no sell-off yet
values.setExitProperties(new ExitProperties(LocalDate.now().plusMonths(6)));
final ParsedStrategy strategy = new ParsedStrategy(values, Collections.emptyList(), Collections.emptyMap(), new FilterSupplier(values, Collections.emptySet(), Collections.emptySet(), Collections.emptySet()));
// no loan or participation should be bought; every investment should be sold
final Loan loanUnder = mockLoan(1000);
final Loan loanOver = Loan.custom().setId(2).setAmount(2000).setTermInMonths(84).build();
final LoanDescriptor ldOver = new LoanDescriptor(loanOver);
final LoanDescriptor ldUnder = new LoanDescriptor(loanUnder);
final ParticipationDescriptor pdOver = mockParticipationDescriptor(loanOver);
final ParticipationDescriptor pdUnder = mockParticipationDescriptor(loanUnder);
final Investment iUnder = Investment.fresh((MarketplaceLoan) loanUnder, 200);
final InvestmentDescriptor idUnder = new InvestmentDescriptor(iUnder, loanUnder);
final Investment iOver = Investment.fresh((MarketplaceLoan) loanOver, 200);
final InvestmentDescriptor idOver = new InvestmentDescriptor(iOver, loanOver);
assertSoftly(softly -> {
softly.assertThat(strategy.getApplicableLoans(Arrays.asList(ldOver, ldUnder))).containsOnly(ldUnder);
softly.assertThat(strategy.getApplicableParticipations(Arrays.asList(pdOver, pdUnder))).containsOnly(pdUnder);
softly.assertThat(strategy.getApplicableInvestments(Arrays.asList(idOver, idUnder))).isEmpty();
});
}
use of com.github.robozonky.api.strategies.ParticipationDescriptor 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);
}
use of com.github.robozonky.api.strategies.ParticipationDescriptor 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()));
}
Aggregations