Search in sources :

Example 36 with Loan

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

the class JmxListenerServiceTest method getParametersForInvestmentPurchased.

private DynamicTest getParametersForInvestmentPurchased() {
    final Loan l = mockLoan();
    final Investment i = Investment.fresh((MarketplaceLoan) l, 200);
    final Event evt = new InvestmentPurchasedEvent(i, l, PortfolioOverview.calculate(BigDecimal.valueOf(2000), Stream::empty));
    final Consumer<SoftAssertions> before = (softly) -> {
        final OperationsMBean mbean = getOperationsMBean();
        softly.assertThat(mbean.getPurchasedInvestments()).isEmpty();
    };
    final Consumer<SoftAssertions> after = (softly) -> {
        final OperationsMBean mbean = getOperationsMBean();
        softly.assertThat(mbean.getPurchasedInvestments()).containsOnlyKeys(i.getLoanId());
        softly.assertThat(mbean.getLatestUpdatedDateTime()).isEqualTo(evt.getCreatedOn());
    };
    return getParameters(evt, before, after);
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) SoftAssertions(org.assertj.core.api.SoftAssertions) TestFactory(org.junit.jupiter.api.TestFactory) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) AfterAll(org.junit.jupiter.api.AfterAll) BigDecimal(java.math.BigDecimal) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) SessionInfo(com.github.robozonky.api.notifications.SessionInfo) BeforeAll(org.junit.jupiter.api.BeforeAll) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) Lifecycle(com.github.robozonky.app.runtime.Lifecycle) Assertions(org.assertj.core.api.Assertions) ShutdownHook(com.github.robozonky.app.ShutdownHook) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Event(com.github.robozonky.api.notifications.Event) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) Stream(java.util.stream.Stream) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) EventListener(com.github.robozonky.api.notifications.EventListener) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) DynamicTest(org.junit.jupiter.api.DynamicTest) Optional(java.util.Optional) ReturnCode(com.github.robozonky.api.ReturnCode) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) RoboZonkyEndingEvent(com.github.robozonky.api.notifications.RoboZonkyEndingEvent) AbstractRoboZonkyTest(com.github.robozonky.test.AbstractRoboZonkyTest) Collections(java.util.Collections) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) SoftAssertions(org.assertj.core.api.SoftAssertions) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Event(com.github.robozonky.api.notifications.Event) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) RoboZonkyEndingEvent(com.github.robozonky.api.notifications.RoboZonkyEndingEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment)

Example 37 with Loan

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

the class DelinquentsTest method newDelinquence.

@Test
void newDelinquence() {
    final PortfolioOverview po = mock(PortfolioOverview.class);
    final Loan l = Loan.custom().setId(RANDOM.nextInt(10000)).setAmount(200).setMyInvestment(mockMyInvestment()).build();
    final Investment i = Investment.fresh(l, 200).setNextPaymentDate(OffsetDateTime.now().minusDays(1)).build();
    final Function<Investment, Loan> f = (id) -> l;
    // make sure new delinquencies are reported and stored
    Delinquents.update(Collections.singleton(i), Collections.emptyList(), po, INVESTMENT_SUPPLIER, f, COLLECTIONS_SUPPLIER);
    assertSoftly(softly -> {
        softly.assertThat(Delinquents.getDelinquents()).hasSize(1);
        softly.assertThat(this.getNewEvents()).hasSize(1);
    });
    assertThat(this.getNewEvents().get(0)).isInstanceOf(LoanNowDelinquentEvent.class);
    // make sure delinquencies are persisted even when there are none present
    Delinquents.update(Collections.emptyList(), Collections.emptyList(), po, INVESTMENT_SUPPLIER, f, COLLECTIONS_SUPPLIER);
    assertSoftly(softly -> {
        softly.assertThat(Delinquents.getDelinquents()).hasSize(1);
        softly.assertThat(this.getNewEvents()).hasSize(2);
    });
    assertThat(this.getNewEvents().get(1)).isInstanceOf(LoanNoLongerDelinquentEvent.class);
    // and when they are no longer active, they're gone for good
    Delinquents.update(Collections.emptyList(), Collections.singleton(i), po, INVESTMENT_SUPPLIER, f, COLLECTIONS_SUPPLIER);
    assertThat(Delinquents.getDelinquents()).hasSize(0);
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) Collection(java.util.Collection) BiFunction(java.util.function.BiFunction) Random(java.util.Random) LoanNowDelinquentEvent(com.github.robozonky.api.notifications.LoanNowDelinquentEvent) Development(com.github.robozonky.api.remote.entities.sanitized.Development) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Instant(java.time.Instant) Function(java.util.function.Function) PaymentStatus(com.github.robozonky.api.remote.enums.PaymentStatus) LoanNoLongerDelinquentEvent(com.github.robozonky.api.notifications.LoanNoLongerDelinquentEvent) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) OffsetDateTime(java.time.OffsetDateTime) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) LoanDefaultedEvent(com.github.robozonky.api.notifications.LoanDefaultedEvent) LocalDate(java.time.LocalDate) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Defaults(com.github.robozonky.internal.api.Defaults) Assertions(org.assertj.core.api.Assertions) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Collections(java.util.Collections) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 38 with Loan

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

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

Example 40 with Loan

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

the class InvestmentDaemonTest method standard.

@Test
void standard() {
    final int loanId = 1;
    final MarketplaceLoan ml = MarketplaceLoan.custom().setId(loanId).setRating(Rating.A).build();
    final Loan l = Loan.custom().setId(loanId).setRating(Rating.A).build();
    final Zonky z = harmlessZonky(Defaults.MINIMUM_INVESTMENT_IN_CZK);
    when(z.getAvailableLoans((Select) notNull())).thenReturn(Stream.of(ml));
    when(z.getLoan(eq(loanId))).thenReturn(l);
    final Authenticated a = mockAuthentication(z);
    final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
    final InvestmentStrategy is = mock(InvestmentStrategy.class);
    final Supplier<Optional<InvestmentStrategy>> s = () -> Optional.of(is);
    final InvestingDaemon d = new InvestingDaemon(t -> {
    }, a, new Investor.Builder(), s, () -> Optional.of(portfolio), Duration.ofSeconds(1));
    d.run();
    verify(z).getAvailableLoans((Select) notNull());
    verify(z).getLoan(ml.getId());
    verify(is).recommend(any(), any(), any());
    assertThat(d.getRefreshInterval()).isEqualByComparingTo(Duration.ofSeconds(1));
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) Optional(java.util.Optional) Portfolio(com.github.robozonky.app.portfolio.Portfolio) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) InvestmentStrategy(com.github.robozonky.api.strategies.InvestmentStrategy) Investor(com.github.robozonky.app.investing.Investor) 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