use of com.github.robozonky.api.strategies.LoanDescriptor in project robozonky by RoboZonky.
the class ParsedStrategyTest method conditions.
@Test
void conditions() {
final DefaultPortfolio portfolio = DefaultPortfolio.PROGRESSIVE;
// test for default values
final ParsedStrategy strategy = new ParsedStrategy(portfolio);
assertThat(strategy.getApplicableLoans(Collections.emptyList())).isEmpty();
// add loan; without filters, should be applicable
final Loan loan = mockLoan(2);
final LoanDescriptor ld = new LoanDescriptor(loan);
assertThat(strategy.getApplicableLoans(Collections.singletonList(ld))).contains(ld);
// now add a filter and see no loans applicable
final MarketplaceFilter f = mock(MarketplaceFilter.class);
when(f.test(eq(new Wrapper(loan)))).thenReturn(true);
final ParsedStrategy strategy2 = new ParsedStrategy(portfolio, Collections.singleton(f));
assertThat(strategy2.getApplicableLoans(Collections.singletonList(ld))).isEmpty();
}
use of com.github.robozonky.api.strategies.LoanDescriptor 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.");
}
}
use of com.github.robozonky.api.strategies.LoanDescriptor in project robozonky by RoboZonky.
the class SessionStateTest method skipPersistence.
@Test
void skipPersistence() {
final LoanDescriptor ld = AbstractZonkyLeveragingTest.mockLoanDescriptor();
final Collection<LoanDescriptor> lds = Arrays.asList(ld, AbstractZonkyLeveragingTest.mockLoanDescriptor());
// skip the loan and persist
final SessionState it = new SessionState(lds);
it.skip(ld);
assertThat(it.getDiscardedLoans()).isEmpty();
assertThat(it.getSeenLoans()).isNotEmpty().contains(ld);
// load again and check that persisted
final SessionState it2 = new SessionState(lds);
assertThat(it.getDiscardedLoans()).isEmpty();
assertThat(it2.getSeenLoans()).isNotEmpty().contains(ld);
}
use of com.github.robozonky.api.strategies.LoanDescriptor in project robozonky by RoboZonky.
the class SessionTest method investmentIgnoredWhenNoConfirmationProviderAndCaptcha.
@Test
void investmentIgnoredWhenNoConfirmationProviderAndCaptcha() {
final LoanDescriptor ld = AbstractZonkyLeveragingTest.mockLoanDescriptor();
final RecommendedLoan r = ld.recommend(200).get();
final Collection<LoanDescriptor> availableLoans = Collections.singletonList(ld);
// setup APIs
final Zonky z = AbstractZonkyLeveragingTest.harmlessZonky(10_000);
final Authenticated auth = mockAuthentication(z);
final Investor p = mock(Investor.class);
doReturn(new ZonkyResponse(ZonkyResponseType.REJECTED)).when(p).invest(eq(r), anyBoolean());
doReturn(Optional.empty()).when(p).getConfirmationProviderId();
final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
final Session t = new Session(portfolio, new LinkedHashSet<>(availableLoans), p, auth);
final boolean result = t.invest(r);
assertThat(result).isFalse();
// validate event
final List<Event> newEvents = this.getNewEvents();
assertThat(newEvents).hasSize(2);
assertSoftly(softly -> {
softly.assertThat(newEvents.get(0)).isInstanceOf(InvestmentRequestedEvent.class);
softly.assertThat(newEvents.get(1)).isInstanceOf(InvestmentSkippedEvent.class);
});
}
use of com.github.robozonky.api.strategies.LoanDescriptor in project robozonky by RoboZonky.
the class SessionTest method constructor.
@Test
void constructor() {
final Zonky z = AbstractZonkyLeveragingTest.harmlessZonky(10_000);
final Authenticated auth = mockAuthentication(z);
final LoanDescriptor ld = AbstractZonkyLeveragingTest.mockLoanDescriptor();
final Collection<LoanDescriptor> lds = Collections.singleton(ld);
final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
final Session it = new Session(portfolio, new LinkedHashSet<>(lds), getInvestor(auth), auth);
assertSoftly(softly -> {
softly.assertThat(it.getAvailable()).isNotSameAs(lds).containsExactly(ld);
softly.assertThat(it.getResult()).isEmpty();
});
}
Aggregations