use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class PurchasingTest method noneAccepted.
@Test
void noneAccepted() {
final Zonky zonky = mockApi();
final Participation mock = mock(Participation.class);
when(mock.getRemainingPrincipal()).thenReturn(BigDecimal.valueOf(250));
final Purchasing exec = new Purchasing(NONE_ACCEPTING, mockAuthentication(zonky), true);
final Portfolio portfolio = Portfolio.create(zonky, mockBalance(zonky));
assertThat(exec.apply(portfolio, Collections.singleton(mock))).isEmpty();
final List<Event> e = this.getNewEvents();
assertThat(e).hasSize(2);
assertSoftly(softly -> {
softly.assertThat(e).first().isInstanceOf(PurchasingStartedEvent.class);
softly.assertThat(e).last().isInstanceOf(PurchasingCompletedEvent.class);
});
}
use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class PurchasingTest method noStrategy.
@Test
void noStrategy() {
final Participation mock = mock(Participation.class);
final Purchasing exec = new Purchasing(Optional::empty, null, true);
final Portfolio portfolio = mock(Portfolio.class);
assertThat(exec.apply(portfolio, Collections.singleton(mock))).isEmpty();
// check events
final List<Event> events = this.getNewEvents();
assertThat(events).isEmpty();
}
use of com.github.robozonky.app.portfolio.Portfolio 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));
}
use of com.github.robozonky.app.portfolio.Portfolio in project robozonky by RoboZonky.
the class SessionTest method empty.
@Test
void empty() {
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.empty(), null, true);
assertThat(i).isEmpty();
}
use of com.github.robozonky.app.portfolio.Portfolio 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()));
}
Aggregations