Search in sources :

Example 16 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class RepaymentsTest method registerNewRepayments.

@Test
void registerNewRepayments() {
    final Loan l = Loan.custom().setId(1).setAmount(200).setRating(Rating.D).setMyInvestment(mockMyInvestment()).setRemainingInvestment(0).build();
    final Investment i = Investment.fresh(l, 200).setPaymentStatus(PaymentStatus.OK).build();
    // first, portfolio contains one active investment; no repaid
    final Zonky z = harmlessZonky(10_000);
    when(z.getLoan(eq(l.getId()))).thenReturn(l);
    final Portfolio p = new Portfolio(Collections.singletonList(i), mockBalance(z));
    final Authenticated a = mockAuthentication(z);
    final Repayments r = new Repayments();
    r.accept(p, a);
    assertThat(getNewEvents()).isEmpty();
    // now, portfolio has the same investment marked as paid; event will be triggered
    Investment.markAsPaid(i);
    r.accept(p, a);
    assertThat(getNewEvents()).first().isInstanceOf(LoanRepaidEvent.class);
    // make sure the loan was retrieved from Zonky
    verify(z).getLoan(eq(l.getId()));
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 17 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class PurchasingTest method someAccepted.

@Test
void someAccepted() {
    final int loanId = 1;
    final Loan loan = Loan.custom().setId(loanId).setAmount(100_000).setRating(Rating.D).setRemainingInvestment(1000).setMyInvestment(mockMyInvestment()).setDatePublished(OffsetDateTime.now()).build();
    final Zonky zonky = mockApi();
    when(zonky.getLoan(eq(loanId))).thenReturn(loan);
    final Participation mock = mock(Participation.class);
    when(mock.getId()).thenReturn(1);
    when(mock.getLoanId()).thenReturn(loan.getId());
    when(mock.getRemainingPrincipal()).thenReturn(BigDecimal.valueOf(250));
    when(mock.getRating()).thenReturn(loan.getRating());
    final Purchasing exec = new Purchasing(ALL_ACCEPTING, mockAuthentication(zonky), true);
    final Portfolio portfolio = Portfolio.create(zonky, mockBalance(zonky));
    assertThat(exec.apply(portfolio, Collections.singleton(mock))).isNotEmpty();
    // purchase as balance changed
    verify(zonky, never()).purchase(eq(mock));
    final List<Event> e = this.getNewEvents();
    assertThat(e).hasSize(5);
    assertSoftly(softly -> {
        softly.assertThat(e).first().isInstanceOf(PurchasingStartedEvent.class);
        softly.assertThat(e.get(1)).isInstanceOf(PurchaseRecommendedEvent.class);
        softly.assertThat(e.get(2)).isInstanceOf(PurchaseRequestedEvent.class);
        softly.assertThat(e.get(3)).isInstanceOf(InvestmentPurchasedEvent.class);
        softly.assertThat(e).last().isInstanceOf(PurchasingCompletedEvent.class);
    });
    // purchase as marketplace first initialized
    assertThat(exec.apply(portfolio, Collections.singleton(mock))).isNotEmpty();
    // no balance change, no marketplace change => don't purchase
    assertThat(exec.apply(portfolio, Collections.singleton(mock))).isEmpty();
    // nothing changed, so no more purchase
    verify(zonky, never()).purchase(eq(mock));
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) 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 18 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class SessionTest method properReal.

@Test
void properReal() {
    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()), false);
    assertThat(i).hasSize(1);
    assertThat(this.getNewEvents()).hasSize(5);
    verify(z).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)

Example 19 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class LoanCacheTest method loadLoan.

@Test
void loadLoan() {
    final LoanCache c = new LoanCache();
    final int loanId = 1;
    final Loan loan = Loan.custom().setId(loanId).build();
    final Zonky z = harmlessZonky(10_000);
    when(z.getLoan(eq(loanId))).thenReturn(loan);
    // return the freshly retrieved loan
    assertThat(c.getLoan(loanId, z)).isEqualTo(loan);
    verify(z).getLoan(eq(loanId));
    // this time from the cache
    assertThat(c.getLoan(loanId, z)).isEqualTo(loan);
    verify(z, times(1)).getLoan(eq(loanId));
}
Also used : Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 20 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan 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)

Aggregations

Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)53 Test (org.junit.jupiter.api.Test)46 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)25 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)20 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)19 MarketplaceLoan (com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan)14 Zonky (com.github.robozonky.common.remote.Zonky)14 SoftAssertions (org.assertj.core.api.SoftAssertions)14 PortfolioOverview (com.github.robozonky.api.strategies.PortfolioOverview)13 Assertions (org.assertj.core.api.Assertions)13 Mockito (org.mockito.Mockito)13 Event (com.github.robozonky.api.notifications.Event)12 Portfolio (com.github.robozonky.app.portfolio.Portfolio)12 Authenticated (com.github.robozonky.app.authentication.Authenticated)11 Collection (java.util.Collection)11 Collections (java.util.Collections)11 Stream (java.util.stream.Stream)10 Optional (java.util.Optional)9 InvestmentPurchasedEvent (com.github.robozonky.api.notifications.InvestmentPurchasedEvent)8 Participation (com.github.robozonky.api.remote.entities.Participation)8