use of com.github.robozonky.api.confirmations.ConfirmationProvider in project robozonky by RoboZonky.
the class ZonkoidSettingsValidatorTest method zonkoidProper.
@Test
void zonkoidProper() {
final ConfirmationProvider cp = mock(ConfirmationProvider.class);
when(cp.requestConfirmation(any(), anyInt(), anyInt())).thenReturn(true);
final InstallData d = ZonkoidSettingsValidatorTest.mockInstallData();
// execute SUT
final DataValidator validator = new ZonkoidSettingsValidator(() -> Optional.of(cp));
final DataValidator.Status result = validator.validateData(d);
// run test
assertThat(result).isEqualTo(DataValidator.Status.OK);
}
use of com.github.robozonky.api.confirmations.ConfirmationProvider in project robozonky by RoboZonky.
the class ZonkoidSettingsValidatorTest method zonkoidPresentButRejecting.
@Test
void zonkoidPresentButRejecting() {
final ConfirmationProvider cp = mock(ConfirmationProvider.class);
when(cp.requestConfirmation(any(), anyInt(), anyInt())).thenReturn(false);
final InstallData d = ZonkoidSettingsValidatorTest.mockInstallData();
// execute SUT
final DataValidator validator = new ZonkoidSettingsValidator(() -> Optional.of(cp));
final DataValidator.Status result = validator.validateData(d);
// run test
assertThat(result).isEqualTo(DataValidator.Status.WARNING);
}
use of com.github.robozonky.api.confirmations.ConfirmationProvider in project robozonky by RoboZonky.
the class CheckerTest method confirmationsRejecting.
@Test
void confirmationsRejecting() {
final ConfirmationProvider cp = mock(ConfirmationProvider.class);
when(cp.requestConfirmation(any(), anyInt(), anyInt())).thenReturn(false);
final boolean result = Checker.confirmations(cp, "", SECRET, CheckerTest::mockApiThatReturnsOneLoan);
assertThat(result).isFalse();
}
use of com.github.robozonky.api.confirmations.ConfirmationProvider in project robozonky by RoboZonky.
the class CheckerTest method confirmationsNotConfirming.
@Test
void confirmationsNotConfirming() {
final ConfirmationProvider cp = mock(ConfirmationProvider.class);
when(cp.requestConfirmation(any(), anyInt(), anyInt())).thenReturn(false);
final boolean result = Checker.confirmations(cp, "", SECRET, CheckerTest::mockApiThatReturnsOneLoan);
assertThat(result).isFalse();
}
use of com.github.robozonky.api.confirmations.ConfirmationProvider in project robozonky by RoboZonky.
the class Session method invest.
/**
* Request {@link ControlApi} to invest in a given loan, leveraging the {@link ConfirmationProvider}.
* @param recommendation Loan to invest into.
* @return True if investment successful. The investment is reflected in {@link #getResult()}.
*/
public boolean invest(final RecommendedLoan recommendation) {
final LoanDescriptor loan = recommendation.descriptor();
final int loanId = loan.item().getId();
if (portfolioOverview.getCzkAvailable() < recommendation.amount().intValue()) {
// should not be allowed by the calling code
return false;
}
Events.fire(new InvestmentRequestedEvent(recommendation));
final boolean seenBefore = state.getSeenLoans().stream().anyMatch(l -> isSameLoan(l, loanId));
final ZonkyResponse response = investor.invest(recommendation, seenBefore);
Session.LOGGER.debug("Response for loan {}: {}.", loanId, response);
final String providerId = investor.getConfirmationProviderId().orElse("-");
switch(response.getType()) {
case REJECTED:
return investor.getConfirmationProviderId().map(c -> {
Events.fire(new InvestmentRejectedEvent(recommendation, providerId));
// rejected through a confirmation provider => forget
discard(loan);
return false;
}).orElseGet(() -> {
// rejected due to no confirmation provider => make available for direct investment later
Events.fire(new InvestmentSkippedEvent(recommendation));
Session.LOGGER.debug("Loan #{} protected by CAPTCHA, will check back later.", loanId);
skip(loan);
return false;
});
case DELEGATED:
final Event e = new InvestmentDelegatedEvent(recommendation, providerId);
Events.fire(e);
if (recommendation.isConfirmationRequired()) {
// confirmation required, delegation successful => forget
discard(loan);
} else {
// confirmation not required, delegation successful => make available for direct investment later
skip(loan);
}
return false;
case INVESTED:
final int confirmedAmount = response.getConfirmedAmount().getAsInt();
final Investment i = Investment.fresh(recommendation.descriptor().item(), confirmedAmount);
markSuccessfulInvestment(i);
Events.fire(new InvestmentMadeEvent(i, loan.item(), portfolioOverview));
return true;
case // still protected by CAPTCHA
SEEN_BEFORE:
return false;
default:
throw new IllegalStateException("Not possible.");
}
}
Aggregations