Search in sources :

Example 16 with Portfolio

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));
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Portfolio(com.github.robozonky.app.portfolio.Portfolio) PortfolioDependant(com.github.robozonky.app.portfolio.PortfolioDependant) Test(org.junit.jupiter.api.Test)

Example 17 with Portfolio

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());
}
Also used : Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) RoboZonkyDaemonFailedEvent(com.github.robozonky.api.notifications.RoboZonkyDaemonFailedEvent) Authenticated(com.github.robozonky.app.authentication.Authenticated) Duration(java.time.Duration) BiConsumer(java.util.function.BiConsumer) Optional(java.util.Optional) Assertions(org.assertj.core.api.Assertions) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Authenticated(com.github.robozonky.app.authentication.Authenticated) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 18 with Portfolio

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));
}
Also used : Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Wallet(com.github.robozonky.api.remote.entities.Wallet) Portfolio(com.github.robozonky.app.portfolio.Portfolio) InvestmentStrategy(com.github.robozonky.api.strategies.InvestmentStrategy) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 19 with Portfolio

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);
    }
}
Also used : Portfolio(com.github.robozonky.app.portfolio.Portfolio) RoboZonkyDaemonFailedEvent(com.github.robozonky.api.notifications.RoboZonkyDaemonFailedEvent)

Example 20 with Portfolio

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);
}
Also used : InvestmentStrategy(com.github.robozonky.api.strategies.InvestmentStrategy) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Collection(java.util.Collection) Investing(com.github.robozonky.app.investing.Investing) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Select(com.github.robozonky.common.remote.Select) Consumer(java.util.function.Consumer) Investor(com.github.robozonky.app.investing.Investor) LoanCache(com.github.robozonky.app.util.LoanCache) Authenticated(com.github.robozonky.app.authentication.Authenticated) Duration(java.time.Duration) Optional(java.util.Optional) Defaults(com.github.robozonky.internal.api.Defaults) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor)

Aggregations

Portfolio (com.github.robozonky.app.portfolio.Portfolio)38 Test (org.junit.jupiter.api.Test)34 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)33 Zonky (com.github.robozonky.common.remote.Zonky)30 Authenticated (com.github.robozonky.app.authentication.Authenticated)29 Event (com.github.robozonky.api.notifications.Event)16 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)13 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)12 Optional (java.util.Optional)10 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)9 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)8 ExecutionStartedEvent (com.github.robozonky.api.notifications.ExecutionStartedEvent)8 InvestmentDelegatedEvent (com.github.robozonky.api.notifications.InvestmentDelegatedEvent)8 InvestmentMadeEvent (com.github.robozonky.api.notifications.InvestmentMadeEvent)8 InvestmentRejectedEvent (com.github.robozonky.api.notifications.InvestmentRejectedEvent)8 InvestmentRequestedEvent (com.github.robozonky.api.notifications.InvestmentRequestedEvent)8 InvestmentSkippedEvent (com.github.robozonky.api.notifications.InvestmentSkippedEvent)8 LoanRecommendedEvent (com.github.robozonky.api.notifications.LoanRecommendedEvent)8 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)8 PurchaseRequestedEvent (com.github.robozonky.api.notifications.PurchaseRequestedEvent)7