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()));
}
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);
}
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;
}
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);
}
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);
}
Aggregations