Search in sources :

Example 16 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated 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 Authenticated

use of com.github.robozonky.app.authentication.Authenticated 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 Authenticated

use of com.github.robozonky.app.authentication.Authenticated 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)

Example 19 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated in project robozonky by RoboZonky.

the class PortfolioUpdater method runIt.

private Portfolio runIt() {
    final Portfolio result = authenticated.call(zonky -> Portfolio.create(zonky, balance));
    final CompletableFuture<Portfolio> combined = dependants.stream().map(d -> (Function<Portfolio, Portfolio>) folio -> {
        LOGGER.trace("Running {}.", d);
        d.accept(folio, authenticated);
        LOGGER.trace("Finished {}.", d);
        return folio;
    }).reduce(CompletableFuture.completedFuture(result), CompletableFuture::thenApply, (s1, s2) -> s1.thenCombine(s2, (p1, p2) -> p2));
    try {
        return combined.get();
    } catch (final Throwable t) {
        throw new IllegalStateException("Portfolio update failed.", t);
    }
}
Also used : Backoff(com.github.robozonky.util.Backoff) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Delinquents(com.github.robozonky.app.portfolio.Delinquents) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) PortfolioDependant(com.github.robozonky.app.portfolio.PortfolioDependant) Repayments(com.github.robozonky.app.portfolio.Repayments) Consumer(java.util.function.Consumer) RemoteBalance(com.github.robozonky.app.portfolio.RemoteBalance) Authenticated(com.github.robozonky.app.authentication.Authenticated) Duration(java.time.Duration) Optional(java.util.Optional) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Collections(java.util.Collections) Selling(com.github.robozonky.app.portfolio.Selling) Function(java.util.function.Function) CompletableFuture(java.util.concurrent.CompletableFuture) Portfolio(com.github.robozonky.app.portfolio.Portfolio)

Example 20 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated in project robozonky by RoboZonky.

the class Repayments method accept.

@Override
public void accept(final Portfolio portfolio, final Authenticated authenticated) {
    final PortfolioOverview portfolioOverview = portfolio.calculateOverview();
    final Collection<Integer> active = getActiveLastTime();
    // detect and process loans that have been fully repaid, comparing to the last time active loans were checked
    final Stream<Investment> repaid = portfolio.getActiveWithPaymentStatus(PaymentStatuses.of(PaymentStatus.PAID));
    repaid.filter(i -> active.contains(i.getLoanId())).peek(i -> {
        final Loan l = authenticated.call(zonky -> LoanCache.INSTANCE.getLoan(i, zonky));
        final Event e = new LoanRepaidEvent(i, l, portfolioOverview);
        Events.fire(e);
    }).forEach(i -> active.remove(i.getLoanId()));
    // add all active loans to date
    portfolio.getActiveWithPaymentStatus(PaymentStatus.getActive()).forEach(i -> active.add(i.getLoanId()));
    // store for future reference
    setActive(active);
}
Also used : Collection(java.util.Collection) Set(java.util.Set) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Collectors(java.util.stream.Collectors) PaymentStatus(com.github.robozonky.api.remote.enums.PaymentStatus) Event(com.github.robozonky.api.notifications.Event) Stream(java.util.stream.Stream) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) LoanCache(com.github.robozonky.app.util.LoanCache) Events(com.github.robozonky.app.Events) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) State(com.github.robozonky.internal.api.State) PaymentStatuses(com.github.robozonky.api.remote.enums.PaymentStatuses) Collections(java.util.Collections) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) Event(com.github.robozonky.api.notifications.Event) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment)

Aggregations

Authenticated (com.github.robozonky.app.authentication.Authenticated)44 Test (org.junit.jupiter.api.Test)37 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)32 Zonky (com.github.robozonky.common.remote.Zonky)31 Portfolio (com.github.robozonky.app.portfolio.Portfolio)28 Event (com.github.robozonky.api.notifications.Event)13 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)11 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)10 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)9 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)9 Optional (java.util.Optional)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 Collection (java.util.Collection)8