use of com.github.robozonky.api.strategies.PortfolioOverview in project robozonky by RoboZonky.
the class NaturalLanguagePurchaseStrategyTest method noLoansApplicable.
@Test
void noLoansApplicable() {
final MarketplaceFilter filter = MarketplaceFilter.of(MarketplaceFilterCondition.alwaysAccepting());
final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE);
final FilterSupplier w = new FilterSupplier(v, Collections.emptySet(), Collections.singleton(filter));
final ParsedStrategy p = new ParsedStrategy(v, Collections.emptySet(), Collections.emptyMap(), w);
final PurchaseStrategy s = new NaturalLanguagePurchaseStrategy(p);
final PortfolioOverview portfolio = mock(PortfolioOverview.class);
when(portfolio.getCzkAvailable()).thenReturn(p.getMinimumBalance());
when(portfolio.getCzkInvested()).thenReturn(p.getMaximumInvestmentSizeInCzk() - 1);
final Stream<RecommendedParticipation> result = s.recommend(Collections.singletonList(mockDescriptor()), portfolio, new Restrictions());
assertThat(result).isEmpty();
}
use of com.github.robozonky.api.strategies.PortfolioOverview in project robozonky by RoboZonky.
the class NaturalLanguageSellStrategyTest method noLoansApplicable.
@Test
void noLoansApplicable() {
final MarketplaceFilter filter = MarketplaceFilter.of(MarketplaceFilterCondition.alwaysAccepting());
final ParsedStrategy p = new ParsedStrategy(DefaultPortfolio.PROGRESSIVE, Collections.singleton(filter));
final SellStrategy s = new NaturalLanguageSellStrategy(p);
final PortfolioOverview portfolio = mock(PortfolioOverview.class);
when(portfolio.getCzkAvailable()).thenReturn(p.getMinimumBalance());
when(portfolio.getCzkInvested()).thenReturn(p.getMaximumInvestmentSizeInCzk() - 1);
final Stream<RecommendedInvestment> result = s.recommend(Collections.singletonList(mockDescriptor()), portfolio);
assertThat(result).isEmpty();
}
use of com.github.robozonky.api.strategies.PortfolioOverview in project robozonky by RoboZonky.
the class Selling method sell.
private void sell(final Portfolio portfolio, final SellStrategy strategy, final Authenticated auth) {
final PortfolioOverview overview = portfolio.calculateOverview();
final Set<InvestmentDescriptor> eligible = portfolio.getActiveForSecondaryMarketplace().parallel().map(i -> getDescriptor(i, auth)).collect(Collectors.toSet());
Events.fire(new SellingStartedEvent(eligible, overview));
final Collection<Investment> investmentsSold = strategy.recommend(eligible, overview).peek(r -> Events.fire(new SaleRecommendedEvent(r))).map(r -> auth.call(zonky -> processInvestment(zonky, r))).flatMap(o -> o.map(Stream::of).orElse(Stream.empty())).collect(Collectors.toSet());
Events.fire(new SellingCompletedEvent(investmentsSold, portfolio.calculateOverview()));
}
use of com.github.robozonky.api.strategies.PortfolioOverview 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.PortfolioOverview in project robozonky by RoboZonky.
the class JmxListenerServiceTest method getParametersForInvestmentMade.
private DynamicTest getParametersForInvestmentMade() {
final Loan l = mockLoan();
final Investment i = Investment.fresh((MarketplaceLoan) l, 200);
final PortfolioOverview po = PortfolioOverview.calculate(BigDecimal.valueOf(1000), Stream::empty);
final Event evt = new InvestmentMadeEvent(i, l, po);
final Consumer<SoftAssertions> before = (softly) -> {
final OperationsMBean mbean = getOperationsMBean();
softly.assertThat(mbean.getSuccessfulInvestments()).isEmpty();
};
final Consumer<SoftAssertions> after = (softly) -> {
final OperationsMBean mbean = getOperationsMBean();
softly.assertThat(mbean.getSuccessfulInvestments()).containsOnlyKeys(l.getId());
softly.assertThat(mbean.getLatestUpdatedDateTime()).isEqualTo(evt.getCreatedOn());
};
return getParameters(evt, before, after);
}
Aggregations