Search in sources :

Example 6 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor 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 7 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor 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 8 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.

the class NaturalLanguagePurchaseStrategyTest method recommendationIsMade.

@Test
void recommendationIsMade() {
    final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE);
    final ParsedStrategy ps = new ParsedStrategy(v, Collections.emptyList(), Collections.emptyMap(), new FilterSupplier(v, null, Collections.emptySet()));
    final PurchaseStrategy s = new NaturalLanguagePurchaseStrategy(ps);
    final PortfolioOverview portfolio = mock(PortfolioOverview.class);
    when(portfolio.getCzkAvailable()).thenReturn(ps.getMinimumBalance());
    when(portfolio.getCzkInvested()).thenReturn(ps.getMaximumInvestmentSizeInCzk() - 1);
    when(portfolio.getShareOnInvestment(any())).thenReturn(BigDecimal.ZERO);
    final Participation p = spy(mockParticipation());
    // not recommended due to balance
    doReturn(BigDecimal.valueOf(100000)).when(p).getRemainingPrincipal();
    doReturn(Rating.A).when(p).getRating();
    final Participation p2 = spy(mockParticipation());
    // check amounts under Zonky investment minimum
    final int amount = Defaults.MINIMUM_INVESTMENT_IN_CZK - 1;
    doReturn(BigDecimal.valueOf(amount)).when(p2).getRemainingPrincipal();
    doReturn(Rating.A).when(p2).getRating();
    final ParticipationDescriptor pd = mockDescriptor(p2);
    final List<RecommendedParticipation> result = s.recommend(Arrays.asList(mockDescriptor(p), pd), portfolio, new Restrictions()).collect(Collectors.toList());
    assertThat(result).hasSize(1);
    final RecommendedParticipation r = result.get(0);
    assertSoftly(softly -> {
        softly.assertThat(r.descriptor()).isEqualTo(pd);
        softly.assertThat(r.amount()).isEqualTo(pd.item().getRemainingPrincipal());
    });
}
Also used : RecommendedParticipation(com.github.robozonky.api.strategies.RecommendedParticipation) Participation(com.github.robozonky.api.remote.entities.Participation) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) RecommendedParticipation(com.github.robozonky.api.strategies.RecommendedParticipation) Test(org.junit.jupiter.api.Test)

Example 9 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.

the class ParsedStrategyTest method sellOffStarted.

@Test
void sellOffStarted() {
    final DefaultPortfolio portfolio = DefaultPortfolio.EMPTY;
    final DefaultValues values = new DefaultValues(portfolio);
    // activate default sell-off 3 months before the given date, which is already in the past
    values.setExitProperties(new ExitProperties(LocalDate.now().plusMonths(2)));
    final ParsedStrategy strategy = new ParsedStrategy(values, Collections.emptyList());
    // no loan or participation should be bought; every investment should be sold
    final Loan l = mockLoan(1000);
    final LoanDescriptor ld = new LoanDescriptor(l);
    final ParticipationDescriptor pd = mockParticipationDescriptor(l);
    final Investment i = Investment.fresh((MarketplaceLoan) l, 200);
    final InvestmentDescriptor id = new InvestmentDescriptor(i, l);
    assertSoftly(softly -> {
        softly.assertThat(strategy.getApplicableLoans(Collections.singleton(ld))).isEmpty();
        softly.assertThat(strategy.getApplicableParticipations(Collections.singleton(pd))).isEmpty();
        softly.assertThat(strategy.getApplicableInvestments(Collections.singleton(id))).containsOnly(id);
    });
}
Also used : Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) InvestmentDescriptor(com.github.robozonky.api.strategies.InvestmentDescriptor) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Test(org.junit.jupiter.api.Test)

Example 10 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.

the class ParsedStrategyTest method matchesAlsoSellFilter.

@Test
void matchesAlsoSellFilter() {
    final MarketplaceFilter accepting = MarketplaceFilter.of(MarketplaceFilterCondition.alwaysAccepting());
    final Collection<MarketplaceFilter> filters = Collections.singleton(accepting);
    final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE);
    final FilterSupplier s = new FilterSupplier(v, Collections.emptySet(), Collections.emptySet(), filters);
    final ParsedStrategy ps = new ParsedStrategy(v, Collections.emptyList(), Collections.emptyMap(), s);
    final Loan l = mockLoan(200_000);
    final LoanDescriptor ld = new LoanDescriptor(l);
    assertThat(ps.getApplicableLoans(Collections.singleton(ld))).isEmpty();
    final Participation p = mock(Participation.class);
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, l);
    assertThat(ps.getApplicableParticipations(Collections.singleton(pd))).isEmpty();
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) MarketplaceFilter(com.github.robozonky.strategy.natural.conditions.MarketplaceFilter) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

ParticipationDescriptor (com.github.robozonky.api.strategies.ParticipationDescriptor)10 Participation (com.github.robozonky.api.remote.entities.Participation)7 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)7 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)7 Test (org.junit.jupiter.api.Test)7 PurchaseStrategy (com.github.robozonky.api.strategies.PurchaseStrategy)5 Restrictions (com.github.robozonky.api.remote.entities.Restrictions)4 Authenticated (com.github.robozonky.app.authentication.Authenticated)4 Portfolio (com.github.robozonky.app.portfolio.Portfolio)4 Collection (java.util.Collection)4 Stream (java.util.stream.Stream)4 Event (com.github.robozonky.api.notifications.Event)3 PurchaseRequestedEvent (com.github.robozonky.api.notifications.PurchaseRequestedEvent)3 Wallet (com.github.robozonky.api.remote.entities.Wallet)3 MarketplaceLoan (com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan)3 Rating (com.github.robozonky.api.remote.enums.Rating)3 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)3 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)3 Zonky (com.github.robozonky.common.remote.Zonky)3 BigDecimal (java.math.BigDecimal)3