Search in sources :

Example 21 with Portfolio

use of com.github.robozonky.app.portfolio.Portfolio 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 22 with Portfolio

use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.

the class Purchasing method execute.

@Override
protected Collection<Investment> execute(final Portfolio portfolio, final PurchaseStrategy strategy, final Collection<Participation> marketplace) {
    final Stream<ParticipationDescriptor> participations = marketplace.parallelStream().map(p -> toDescriptor(p, auth)).filter(d -> {
        // never re-purchase what was once sold
        final Loan l = d.related();
        final boolean wasSoldBefore = portfolio.wasOnceSold(l);
        if (wasSoldBefore) {
            LOGGER.debug("Ignoring loan #{} as the user had already sold it before.", l.getId());
        }
        return !wasSoldBefore;
    });
    final RestrictedPurchaseStrategy s = new RestrictedPurchaseStrategy(strategy, auth.getRestrictions());
    return Session.purchase(portfolio, auth, participations, s, dryRun);
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) LoanCache(com.github.robozonky.app.util.LoanCache) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Optional(java.util.Optional) Portfolio(com.github.robozonky.app.portfolio.Portfolio) StrategyExecutor(com.github.robozonky.app.util.StrategyExecutor) Participation(com.github.robozonky.api.remote.entities.Participation) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor)

Example 23 with Portfolio

use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.

the class PurchasingTest method noItems.

@Test
void noItems() {
    final Zonky zonky = mockApi();
    final Purchasing exec = new Purchasing(ALL_ACCEPTING, mockAuthentication(zonky), true);
    final Portfolio portfolio = Portfolio.create(zonky, mockBalance(zonky));
    assertThat(exec.apply(portfolio, Collections.emptyList())).isEmpty();
    final List<Event> e = this.getNewEvents();
    assertThat(e).isEmpty();
}
Also used : Portfolio(com.github.robozonky.app.portfolio.Portfolio) PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Event(com.github.robozonky.api.notifications.Event) PurchaseRecommendedEvent(com.github.robozonky.api.notifications.PurchaseRecommendedEvent) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Zonky(com.github.robozonky.common.remote.Zonky) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Test(org.junit.jupiter.api.Test)

Example 24 with Portfolio

use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.

the class SessionTest method underBalance.

@Test
void underBalance() {
    final Participation p = mock(Participation.class);
    when(p.getRemainingPrincipal()).thenReturn(BigDecimal.valueOf(200));
    final Loan l = Loan.custom().build();
    final PurchaseStrategy s = mock(PurchaseStrategy.class);
    when(s.recommend(any(), any(), any())).thenAnswer(i -> {
        final Collection<ParticipationDescriptor> participations = i.getArgument(0);
        return participations.stream().map(ParticipationDescriptor::recommend).flatMap(o -> o.map(Stream::of).orElse(Stream.empty()));
    });
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, l);
    final Zonky z = mockZonky();
    final Authenticated auth = mockAuthentication(z);
    final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
    final Collection<Investment> i = Session.purchase(portfolio, auth, Stream.of(pd), new RestrictedPurchaseStrategy(s, new Restrictions()), true);
    assertSoftly(softly -> {
        softly.assertThat(i).isEmpty();
        softly.assertThat(this.getNewEvents()).has(new Condition<List<? extends Event>>() {

            @Override
            public boolean matches(final List<? extends Event> events) {
                return events.stream().noneMatch(e -> e instanceof PurchaseRequestedEvent);
            }
        });
    });
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) Collection(java.util.Collection) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Wallet(com.github.robozonky.api.remote.entities.Wallet) Zonky(com.github.robozonky.common.remote.Zonky) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Event(com.github.robozonky.api.notifications.Event) Test(org.junit.jupiter.api.Test) BigDecimal(java.math.BigDecimal) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Condition(org.assertj.core.api.Condition) Rating(com.github.robozonky.api.remote.enums.Rating) Assertions(org.assertj.core.api.Assertions) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Participation(com.github.robozonky.api.remote.entities.Participation) Participation(com.github.robozonky.api.remote.entities.Participation) Authenticated(com.github.robozonky.app.authentication.Authenticated) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Zonky(com.github.robozonky.common.remote.Zonky) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Event(com.github.robozonky.api.notifications.Event) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) List(java.util.List) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 25 with Portfolio

use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.

the class SessionTest method properDryRun.

@Test
void properDryRun() {
    final Loan l = Loan.custom().setId(1).setAmount(200).setRating(Rating.D).setRemainingInvestment(200).setMyInvestment(mockMyInvestment()).build();
    final Participation p = mock(Participation.class);
    when(p.getLoanId()).thenReturn(l.getId());
    when(p.getRemainingPrincipal()).thenReturn(BigDecimal.valueOf(200));
    final PurchaseStrategy s = mock(PurchaseStrategy.class);
    when(s.recommend(any(), any(), any())).thenAnswer(i -> {
        final Collection<ParticipationDescriptor> participations = i.getArgument(0);
        return participations.stream().map(ParticipationDescriptor::recommend).flatMap(o -> o.map(Stream::of).orElse(Stream.empty()));
    });
    final Zonky z = mockZonky(BigDecimal.valueOf(100_000));
    when(z.getLoan(eq(l.getId()))).thenReturn(l);
    final Authenticated auth = mockAuthentication(z);
    final Portfolio portfolio = spy(Portfolio.create(z, mockBalance(z)));
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, l);
    final Collection<Investment> i = Session.purchase(portfolio, auth, Stream.of(pd), new RestrictedPurchaseStrategy(s, new Restrictions()), true);
    assertThat(i).hasSize(1);
    assertThat(this.getNewEvents()).hasSize(5);
    verify(z, never()).purchase(eq(p));
    verify(portfolio).newBlockedAmount(eq(auth), argThat((a) -> a.getLoanId() == l.getId()));
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) Collection(java.util.Collection) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Wallet(com.github.robozonky.api.remote.entities.Wallet) Zonky(com.github.robozonky.common.remote.Zonky) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Event(com.github.robozonky.api.notifications.Event) Test(org.junit.jupiter.api.Test) BigDecimal(java.math.BigDecimal) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Condition(org.assertj.core.api.Condition) Rating(com.github.robozonky.api.remote.enums.Rating) Assertions(org.assertj.core.api.Assertions) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Participation(com.github.robozonky.api.remote.entities.Participation) Participation(com.github.robozonky.api.remote.entities.Participation) Authenticated(com.github.robozonky.app.authentication.Authenticated) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Zonky(com.github.robozonky.common.remote.Zonky) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

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