use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class BlockedAmountsUpdaterTest method customDependant.
@Test
void customDependant() {
final Portfolio p = mock(Portfolio.class);
final PortfolioDependant d = mock(PortfolioDependant.class);
final Authenticated auth = mock(Authenticated.class);
final BlockedAmountsUpdater bau = new BlockedAmountsUpdater(auth, () -> Optional.of(p), d);
bau.run();
verify(d).accept(eq(p), eq(auth));
}
use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class DaemonOperationTest method error.
@Test
void error() {
final Authenticated a = mock(Authenticated.class);
final BiConsumer<Portfolio, Authenticated> operation = (p, api) -> {
throw new Error();
};
final Consumer<Throwable> shutdown = mock(Consumer.class);
final DaemonOperation d = new CustomOperation(a, operation, shutdown);
d.run();
verify(shutdown).accept(any());
}
use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class StrategyExecutorTest method rechecksMarketplaceIfBalanceIncreased.
@Test
void rechecksMarketplaceIfBalanceIncreased() {
final Zonky zonky = harmlessZonky(10_000);
final Portfolio p = Portfolio.create(zonky, mockBalance(zonky));
final Loan loan = Loan.custom().build();
final LoanDescriptor ld = new LoanDescriptor(loan);
final Collection<LoanDescriptor> marketplace = Collections.singleton(ld);
// prepare the executor, have it fail when executing the investment operation
final StrategyExecutor<LoanDescriptor, InvestmentStrategy> e = new AlwaysFreshNeverInvesting();
final StrategyExecutor<LoanDescriptor, InvestmentStrategy> mocked = spy(e);
// marketplace never has any updates
when(mocked.hasMarketplaceUpdates(any())).thenReturn(false);
// fresh balance, check marketplace
mocked.apply(p, marketplace);
verify(mocked).execute(eq(p), eq(ALL_ACCEPTING_STRATEGY), eq(marketplace));
// nothing changed, still only ran once
mocked.apply(p, marketplace);
verify(mocked, times(1)).execute(eq(p), eq(ALL_ACCEPTING_STRATEGY), eq(marketplace));
// increase remote balance
when(zonky.getWallet()).thenReturn(new Wallet(BigDecimal.valueOf(100_000)));
// should have checked marketplace
mocked.apply(p, marketplace);
verify(mocked, times(2)).execute(eq(p), eq(ALL_ACCEPTING_STRATEGY), eq(marketplace));
}
use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class DaemonOperation method run.
@Override
public void run() {
try {
LOGGER.trace("Starting.");
if (isEnabled(api)) {
final Portfolio p = portfolio.get().orElseThrow(() -> new IllegalStateException("Portfolio not properly initialized."));
execute(p, api);
} else {
LOGGER.info("Access to marketplace disabled by Zonky.");
}
LOGGER.trace("Finished.");
} catch (final Exception ex) {
LOGGER.warn("Caught unexpected exception, continuing operation.", ex);
Events.fire(new RoboZonkyDaemonFailedEvent(ex));
} catch (final Error t) {
LOGGER.error("Caught unexpected error, terminating.", t);
shutdownCall.accept(t);
}
}
use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class InvestingDaemon method execute.
@Override
protected void execute(final Portfolio portfolio, final Authenticated authenticated) {
// don't query anything unless we have enough money to invest
final int balance = portfolio.getRemoteBalance().get().intValue();
final int minimum = Defaults.MINIMUM_INVESTMENT_IN_CZK;
if (balance < minimum) {
LOGGER.debug("Asleep as there is not enough available balance. ({} < {})", balance, minimum);
return;
}
// query marketplace for investment opportunities
final Collection<LoanDescriptor> loans = authenticated.call(zonky -> zonky.getAvailableLoans(SELECT)).parallel().filter(// re-investing would fail
l -> !l.getMyInvestment().isPresent()).map(l -> {
/*
* Loan is first retrieved from the authenticated API. This way, we get all available
* information, such as borrower nicknames from other loans made by the same person.
*/
final Loan complete = authenticated.call(zonky -> LoanCache.INSTANCE.getLoan(l.getId(), zonky));
/*
* We update the loan within the cache with latest information from the marketplace. This is
* done so that we don't cache stale loan information, such as how much of the loan is remaining
* to be invested.
*/
Loan.updateFromMarketplace(complete, l);
return complete;
}).map(LoanDescriptor::new).collect(Collectors.toList());
investing.apply(portfolio, loans);
}
Aggregations