Search in sources :

Example 41 with Zonky

use of com.github.robozonky.common.remote.Zonky 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);
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Zonky(com.github.robozonky.common.remote.Zonky) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) Event(com.github.robozonky.api.notifications.Event) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) LoanRecommendedEvent(com.github.robozonky.api.notifications.LoanRecommendedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Test(org.junit.jupiter.api.Test)

Example 42 with Zonky

use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.

the class SessionTest method investmentFailed.

@Test
void investmentFailed() {
    final Zonky z = AbstractZonkyLeveragingTest.harmlessZonky(10_000);
    final Authenticated auth = mockAuthentication(z);
    final RecommendedLoan r = AbstractZonkyLeveragingTest.mockLoanDescriptor().recommend(200).get();
    final Exception thrown = new ServiceUnavailableException();
    final Investor p = mock(Investor.class);
    doThrow(thrown).when(p).invest(eq(r), anyBoolean());
    final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
    final Session t = new Session(portfolio, Collections.emptySet(), p, auth);
    assertThatThrownBy(() -> t.invest(r)).isSameAs(thrown);
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Portfolio(com.github.robozonky.app.portfolio.Portfolio) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Zonky(com.github.robozonky.common.remote.Zonky) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Test(org.junit.jupiter.api.Test)

Example 43 with Zonky

use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.

the class BlockedAmountsTest method newBlockedAmounts.

@Test
void newBlockedAmounts() {
    final Zonky zonky = harmlessZonky(10_000);
    when(zonky.getBlockedAmounts()).thenReturn(Stream.of(BA1, BA2));
    final Authenticated auth = mockAuthentication(zonky);
    final Portfolio p = mock(Portfolio.class);
    final BlockedAmounts blockedAmounts = new BlockedAmounts();
    // verify the first two blocked amounts were registered
    blockedAmounts.accept(p, auth);
    verify(p).newBlockedAmount(eq(auth), eq(BA1));
    verify(p).newBlockedAmount(eq(auth), eq(BA2));
    // verify only the new blocked amount was registered, and that it only happened the first time
    reset(p);
    when(zonky.getBlockedAmounts()).thenAnswer((Answer<Stream<BlockedAmount>>) invocation -> Stream.of(BA1, BA2, BA3));
    blockedAmounts.accept(p, auth);
    blockedAmounts.accept(p, auth);
    verify(p, times(1)).newBlockedAmount(eq(auth), eq(BA3));
}
Also used : Test(org.junit.jupiter.api.Test) BigDecimal(java.math.BigDecimal) Answer(org.mockito.stubbing.Answer) Mockito(org.mockito.Mockito) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) Stream(java.util.stream.Stream) Authenticated(com.github.robozonky.app.authentication.Authenticated) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Zonky(com.github.robozonky.common.remote.Zonky) Authenticated(com.github.robozonky.app.authentication.Authenticated) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Stream(java.util.stream.Stream) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 44 with Zonky

use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.

the class InvestmentDaemonTest method underBalance.

@Test
void underBalance() {
    final Zonky z = harmlessZonky(Defaults.MINIMUM_INVESTMENT_IN_CZK - 1);
    final Authenticated a = mockAuthentication(z);
    final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
    final Supplier<Optional<InvestmentStrategy>> s = Optional::empty;
    final InvestingDaemon d = new InvestingDaemon(t -> {
    }, a, new Investor.Builder(), s, () -> Optional.of(portfolio), Duration.ofSeconds(1));
    d.run();
    verify(z, never()).getAvailableLoans((Select) notNull());
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Optional(java.util.Optional) Portfolio(com.github.robozonky.app.portfolio.Portfolio) 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)

Example 45 with Zonky

use of com.github.robozonky.common.remote.Zonky 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

Zonky (com.github.robozonky.common.remote.Zonky)55 Test (org.junit.jupiter.api.Test)47 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)46 Authenticated (com.github.robozonky.app.authentication.Authenticated)30 Portfolio (com.github.robozonky.app.portfolio.Portfolio)30 Event (com.github.robozonky.api.notifications.Event)19 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)15 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)13 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)12 Wallet (com.github.robozonky.api.remote.entities.Wallet)10 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)9 BigDecimal (java.math.BigDecimal)9 Collection (java.util.Collection)9 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)8 ExecutionStartedEvent (com.github.robozonky.api.notifications.ExecutionStartedEvent)8 InvestmentDelegatedEvent (com.github.robozonky.api.notifications.InvestmentDelegatedEvent)8 InvestmentMadeEvent (com.github.robozonky.api.notifications.InvestmentMadeEvent)8 InvestmentRejectedEvent (com.github.robozonky.api.notifications.InvestmentRejectedEvent)8 InvestmentRequestedEvent (com.github.robozonky.api.notifications.InvestmentRequestedEvent)8 InvestmentSkippedEvent (com.github.robozonky.api.notifications.InvestmentSkippedEvent)8