Zonky { public InvestmentResult invest(final Loan loan, final int amount) { LOGGER.debug("Investing into loan #{}.", loan.getId()); try { var request = new InvestmentRequest(loan.getId(), amount); controlApi.run(api -> api.invest(request)); return InvestmentResult.success(); } catch (final ClientErrorException ex) { LOGGER.debug("Caught API exception during investment.", ex); return InvestmentResult.failure(ex); } } Zonky(final ApiProvider api, final Supplier<ZonkyApiToken> tokenSupplier); InvestmentResult invest(final Loan loan, final int amount); void cancel(final Investment investment); PurchaseResult purchase(final Participation participation); void sell(final Investment investment); void accept(final Reservation reservation); Stream<Reservation> getPendingReservations(); Stream<Investment> getInvestments(final Select select); Stream<Investment> getPendingInvestments(); Stream<Investment> getSoldInvestments(); Stream<Investment> getSellableInvestments(); Stream<Investment> getDelinquentInvestments(); Loan getLoan(final int id); Investment getInvestment(final long id); LastPublishedItem getLastPublishedLoanInfo(); LastPublishedItem getLastPublishedParticipationInfo(); ParticipationDetail getParticipationDetail(final int loanId); Stream<Loan> getAvailableLoans(final Select select); Stream<Participation> getAvailableParticipations(final Select select); ReservationPreferences getReservationPreferences(); void setReservationPreferences(final ReservationPreferencesImpl preferences); Restrictions getRestrictions(); Consents getConsents(); Statistics getStatistics();  }
@Test void invest() { final ControlApi control = mock(ControlApi.class); final Api<ControlApi> ca = mockApi(control); final PaginatedApi<LoanImpl, LoanApi> la = mockApi(); final int loanId = 1; final Loan loan = mock(LoanImpl.class); when(loan.getId()).thenReturn(loanId); when(loan.getAmount()).thenReturn(Money.from(200.0)); when(loan.getRemainingInvestment()).thenReturn(Money.from(200.0)); when(la.execute(any())).thenReturn(loan); final Zonky z = mockZonky(ca, la); final Loan l = z.getLoan(loanId); assertThat(z.invest(l, 200)).isEqualTo(InvestmentResult.success()); }