Zonky { public PurchaseResult purchase(final Participation participation) { LOGGER.debug("Purchasing participation #{} in loan #{}.", participation.getId(), participation.getLoanId()); try { controlApi.run(api -> api.purchase(participation.getId(), new PurchaseRequest(participation))); return PurchaseResult.success(); } catch (final ClientErrorException ex) { LOGGER.debug("Caught API exception during purchasing.", ex); return PurchaseResult.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 purchase() { final ControlApi control = mock(ControlApi.class); final Api<ControlApi> ca = mockApi(control); final Zonky z = mockZonkyControl(ca); final Participation p = mock(ParticipationImpl.class); when(p.getRemainingPrincipal()).thenReturn(Money.from(10)); when(p.getId()).thenReturn(1L); assertThat(z.purchase(p)).isEqualTo(PurchaseResult.success()); }
@Test void purchaseFailure() { final ControlApi control = mock(ControlApi.class); final Api<ControlApi> ca = mockApi(control); doThrow(new ClientErrorException(Response.Status.FORBIDDEN)).when(control) .purchase(anyLong(), any()); final Zonky z = mockZonkyControl(ca); final Participation p = mock(ParticipationImpl.class); when(p.getRemainingPrincipal()).thenReturn(Money.from(10)); when(p.getId()).thenReturn(1L); assertThat(z.purchase(p) .getFailureType()).contains(PurchaseFailureType.UNKNOWN); }