use of com.github.robozonky.api.remote.entities.sanitized.Investment 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()));
}
use of com.github.robozonky.api.remote.entities.sanitized.Investment in project robozonky by RoboZonky.
the class SessionTest method investmentSuccessful.
@Test
void investmentSuccessful() {
final int oldBalance = 10_000;
final int amountToInvest = 200;
final RecommendedLoan r = AbstractZonkyLeveragingTest.mockLoanDescriptor().recommend(amountToInvest).get();
final Zonky z = AbstractZonkyLeveragingTest.harmlessZonky(oldBalance);
when(z.getLoan(eq(r.descriptor().item().getId()))).thenReturn(r.descriptor().item());
final Authenticated auth = mockAuthentication(z);
final Investor p = mock(Investor.class);
doReturn(new ZonkyResponse(amountToInvest)).when(p).invest(eq(r), anyBoolean());
doReturn(Optional.of("something")).when(p).getConfirmationProviderId();
final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
final Session t = new Session(portfolio, Collections.emptySet(), p, auth);
final boolean result = t.invest(r);
assertThat(result).isTrue();
final List<Investment> investments = t.getResult();
assertThat(investments).hasSize(1);
assertThat(investments.get(0).getOriginalPrincipal().intValue()).isEqualTo(amountToInvest);
// validate event sequence
final List<Event> newEvents = this.getNewEvents();
assertThat(newEvents).hasSize(2);
assertSoftly(softly -> {
softly.assertThat(newEvents.get(0)).isInstanceOf(InvestmentRequestedEvent.class);
softly.assertThat(newEvents.get(1)).isInstanceOf(InvestmentMadeEvent.class);
});
// validate event contents
final InvestmentMadeEvent e = (InvestmentMadeEvent) newEvents.get(1);
assertThat(e.getPortfolioOverview().getCzkAvailable()).isEqualTo(oldBalance - amountToInvest);
}
use of com.github.robozonky.api.remote.entities.sanitized.Investment in project robozonky by RoboZonky.
the class LoanCacheTest method emptyGetLoan.
@Test
void emptyGetLoan() {
final LoanCache c = new LoanCache();
final int loanId = 1;
// nothing returned at first
assertThat(c.getLoan(loanId)).isEmpty();
final MyInvestment mi = mock(MyInvestment.class);
when(mi.getTimeCreated()).thenReturn(OffsetDateTime.now());
final Loan loan = Loan.custom().setId(loanId).setMyInvestment(mi).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);
final Investment i = Investment.custom().setLoanId(loanId).build();
assertThat(c.getLoan(i, z)).isEqualTo(loan);
assertThat(i.getInvestmentDate()).isNotEmpty();
}
use of com.github.robozonky.api.remote.entities.sanitized.Investment in project robozonky by RoboZonky.
the class InvestmentDescriptorTest method recommendWrong.
@Test
void recommendWrong() {
final BigDecimal remainingPrincipal = BigDecimal.TEN;
final Investment i = mockInvestment(remainingPrincipal);
final InvestmentDescriptor id = new InvestmentDescriptor(i);
final Optional<RecommendedInvestment> r = id.recommend(remainingPrincipal.subtract(BigDecimal.ONE));
assertThat(r).isEmpty();
}
use of com.github.robozonky.api.remote.entities.sanitized.Investment in project robozonky by RoboZonky.
the class InvestmentDescriptorTest method equals.
@Test
void equals() {
final Investment i = mockInvestment(BigDecimal.TEN);
final InvestmentDescriptor id = new InvestmentDescriptor(i);
assertSoftly(softly -> {
softly.assertThat(id).isNotEqualTo(null);
softly.assertThat(id).isNotEqualTo(UUID.randomUUID().toString());
softly.assertThat(id).isEqualTo(id);
});
final InvestmentDescriptor id2 = new InvestmentDescriptor(i);
assertSoftly(softly -> {
softly.assertThat(id).isEqualTo(id2);
softly.assertThat(id2).isEqualTo(id);
});
final InvestmentDescriptor id3 = new InvestmentDescriptor(mockInvestment(BigDecimal.ONE));
assertSoftly(softly -> {
softly.assertThat(id).isNotEqualTo(id3);
});
}
Aggregations