use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class PortfolioUpdater method runIt.
private Portfolio runIt() {
final Portfolio result = authenticated.call(zonky -> Portfolio.create(zonky, balance));
final CompletableFuture<Portfolio> combined = dependants.stream().map(d -> (Function<Portfolio, Portfolio>) folio -> {
LOGGER.trace("Running {}.", d);
d.accept(folio, authenticated);
LOGGER.trace("Finished {}.", d);
return folio;
}).reduce(CompletableFuture.completedFuture(result), CompletableFuture::thenApply, (s1, s2) -> s1.thenCombine(s2, (p1, p2) -> p2));
try {
return combined.get();
} catch (final Throwable t) {
throw new IllegalStateException("Portfolio update failed.", t);
}
}
use of com.github.robozonky.app.portfolio.Portfolio 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.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class PurchasingTest method noItems.
@Test
void noItems() {
final Zonky zonky = mockApi();
final Purchasing exec = new Purchasing(ALL_ACCEPTING, mockAuthentication(zonky), true);
final Portfolio portfolio = Portfolio.create(zonky, mockBalance(zonky));
assertThat(exec.apply(portfolio, Collections.emptyList())).isEmpty();
final List<Event> e = this.getNewEvents();
assertThat(e).isEmpty();
}
use of com.github.robozonky.app.portfolio.Portfolio 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);
}
});
});
}
use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class SessionTest method properDryRun.
@Test
void properDryRun() {
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()), true);
assertThat(i).hasSize(1);
assertThat(this.getNewEvents()).hasSize(5);
verify(z, never()).purchase(eq(p));
verify(portfolio).newBlockedAmount(eq(auth), argThat((a) -> a.getLoanId() == l.getId()));
}
Aggregations