use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class SessionTest method empty.
@Test
void empty() {
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.empty(), null, true);
assertThat(i).isEmpty();
}
use of com.github.robozonky.common.remote.Zonky 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.common.remote.Zonky in project robozonky by RoboZonky.
the class LoanCacheTest method loadLoan.
@Test
void loadLoan() {
final LoanCache c = new LoanCache();
final int loanId = 1;
final Loan loan = Loan.custom().setId(loanId).build();
final Zonky z = harmlessZonky(10_000);
when(z.getLoan(eq(loanId))).thenReturn(loan);
// return the freshly retrieved loan
assertThat(c.getLoan(loanId, z)).isEqualTo(loan);
verify(z).getLoan(eq(loanId));
// this time from the cache
assertThat(c.getLoan(loanId, z)).isEqualTo(loan);
verify(z, times(1)).getLoan(eq(loanId));
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class StrategyExecutorTest method rechecksMarketplaceIfBalanceIncreased.
@Test
void rechecksMarketplaceIfBalanceIncreased() {
final Zonky zonky = harmlessZonky(10_000);
final Portfolio p = Portfolio.create(zonky, mockBalance(zonky));
final Loan loan = Loan.custom().build();
final LoanDescriptor ld = new LoanDescriptor(loan);
final Collection<LoanDescriptor> marketplace = Collections.singleton(ld);
// prepare the executor, have it fail when executing the investment operation
final StrategyExecutor<LoanDescriptor, InvestmentStrategy> e = new AlwaysFreshNeverInvesting();
final StrategyExecutor<LoanDescriptor, InvestmentStrategy> mocked = spy(e);
// marketplace never has any updates
when(mocked.hasMarketplaceUpdates(any())).thenReturn(false);
// fresh balance, check marketplace
mocked.apply(p, marketplace);
verify(mocked).execute(eq(p), eq(ALL_ACCEPTING_STRATEGY), eq(marketplace));
// nothing changed, still only ran once
mocked.apply(p, marketplace);
verify(mocked, times(1)).execute(eq(p), eq(ALL_ACCEPTING_STRATEGY), eq(marketplace));
// increase remote balance
when(zonky.getWallet()).thenReturn(new Wallet(BigDecimal.valueOf(100_000)));
// should have checked marketplace
mocked.apply(p, marketplace);
verify(mocked, times(2)).execute(eq(p), eq(ALL_ACCEPTING_STRATEGY), eq(marketplace));
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class AbstractZonkyLeveragingTest method harmlessZonky.
protected static Zonky harmlessZonky(final int availableBalance) {
final Zonky zonky = mock(Zonky.class);
final BigDecimal balance = BigDecimal.valueOf(availableBalance);
when(zonky.getWallet()).thenReturn(new Wallet(1, 2, balance, balance));
when(zonky.getBlockedAmounts()).thenReturn(Stream.empty());
return zonky;
}
Aggregations