@Test
void failureDueToTooManyRequests() {

    final Loan l = new MockLoanBuilder()
        .set(LoanImpl::setAmount, Money.from(200))
        .set(LoanImpl::setRating, Rating.D)
        .set(LoanImpl::setRemainingInvestment, Money.from(200))
        .set(LoanImpl::setReservedAmount, Money.from(0))
        .set(LoanImpl::setMyInvestment, mockMyInvestment())
        .build();
    final Participation p = mock(ParticipationImpl.class);
    doReturn(l.getId()).when(p)
        .getLoanId();
    when(p.getRemainingPrincipal()).thenReturn(Money.from(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(Optional::stream);
    });
    final Zonky z = harmlessZonky();
    when(z.getLoan(eq(l.getId()))).thenReturn(l);
    final Response response = Response.status(400)
        .entity(PurchaseFailureType.TOO_MANY_REQUESTS.getReason()
            .get())
        .build();
    final ClientErrorException thrown = new BadRequestException(response);
    when(z.purchase(any())).thenReturn(PurchaseResult.failure(thrown));
    final PowerTenant auth = mockTenant(z, false);
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, () -> l);
    assertThatThrownBy(() -> PurchasingSession.purchase(auth, Collections.singleton(pd), s))
        .isInstanceOf(IllegalStateException.class);
}