Search in sources :

Example 1 with ControlApi

use of com.github.robozonky.api.remote.ControlApi in project robozonky by RoboZonky.

the class ZonkyTest method wallet.

@Test
void wallet() {
    final ControlApi control = mock(ControlApi.class);
    final Api<ControlApi> ca = mockApi(control);
    final PaginatedApi<RawLoan, LoanApi> la = mockApi();
    final PaginatedApi<BlockedAmount, WalletApi> wa = mockApi();
    when(wa.execute((Function<WalletApi, Wallet>) any())).thenReturn(mock(Wallet.class));
    final PaginatedApi<RawInvestment, PortfolioApi> pa = mockApi();
    final PaginatedApi<Participation, ParticipationApi> sa = mockApi();
    final PaginatedApi<RawDevelopment, CollectionsApi> caa = mockApi();
    final Zonky z = new Zonky(ca, la, sa, pa, wa, caa);
    final Wallet w = z.getWallet();
    assertThat(w).isNotNull();
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) ParticipationApi(com.github.robozonky.api.remote.ParticipationApi) WalletApi(com.github.robozonky.api.remote.WalletApi) Wallet(com.github.robozonky.api.remote.entities.Wallet) LoanApi(com.github.robozonky.api.remote.LoanApi) PortfolioApi(com.github.robozonky.api.remote.PortfolioApi) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) RawLoan(com.github.robozonky.api.remote.entities.RawLoan) ControlApi(com.github.robozonky.api.remote.ControlApi) RawDevelopment(com.github.robozonky.api.remote.entities.RawDevelopment) CollectionsApi(com.github.robozonky.api.remote.CollectionsApi) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) Test(org.junit.jupiter.api.Test)

Example 2 with ControlApi

use of com.github.robozonky.api.remote.ControlApi in project robozonky by RoboZonky.

the class ZonkyTest method sell.

@Test
void sell() {
    final ControlApi control = mock(ControlApi.class);
    final Api<ControlApi> ca = mockApi(control);
    final PaginatedApi<RawLoan, LoanApi> la = mockApi();
    final PaginatedApi<BlockedAmount, WalletApi> wa = mockApi();
    final PaginatedApi<RawInvestment, PortfolioApi> pa = mockApi();
    final PaginatedApi<Participation, ParticipationApi> sa = mockApi();
    final PaginatedApi<RawDevelopment, CollectionsApi> caa = mockApi();
    final Zonky z = new Zonky(ca, la, sa, pa, wa, caa);
    final Investment p = Investment.custom().setRemainingPrincipal(BigDecimal.TEN).setSmpFee(BigDecimal.ONE).setId(1).build();
    z.sell(p);
    verify(control).offer(any());
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) ParticipationApi(com.github.robozonky.api.remote.ParticipationApi) WalletApi(com.github.robozonky.api.remote.WalletApi) LoanApi(com.github.robozonky.api.remote.LoanApi) PortfolioApi(com.github.robozonky.api.remote.PortfolioApi) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) RawLoan(com.github.robozonky.api.remote.entities.RawLoan) ControlApi(com.github.robozonky.api.remote.ControlApi) RawDevelopment(com.github.robozonky.api.remote.entities.RawDevelopment) CollectionsApi(com.github.robozonky.api.remote.CollectionsApi) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) Test(org.junit.jupiter.api.Test)

Example 3 with ControlApi

use of com.github.robozonky.api.remote.ControlApi in project robozonky by RoboZonky.

the class ZonkyTest method investAndlogout.

@Test
void investAndlogout() {
    final ControlApi control = mock(ControlApi.class);
    final Api<ControlApi> ca = mockApi(control);
    final PaginatedApi<RawLoan, LoanApi> la = mockApi();
    final int loanId = 1;
    final RawLoan loan = mock(RawLoan.class);
    when(loan.getId()).thenReturn(loanId);
    when(loan.getAmount()).thenReturn(200.0);
    when(loan.getRemainingInvestment()).thenReturn(200.0);
    when(la.execute((Function<LoanApi, RawLoan>) any())).thenReturn(loan);
    final PaginatedApi<BlockedAmount, WalletApi> wa = mockApi();
    final PaginatedApi<RawInvestment, PortfolioApi> pa = mockApi();
    final PaginatedApi<Participation, ParticipationApi> sa = mockApi();
    final PaginatedApi<RawDevelopment, CollectionsApi> caa = mockApi();
    final Zonky z = new Zonky(ca, la, sa, pa, wa, caa);
    final Loan l = z.getLoan(loanId);
    final Investment i = Investment.fresh((MarketplaceLoan) l, 200);
    z.invest(i);
    z.logout();
    verify(control, times(1)).invest(any());
    verify(control, times(1)).logout();
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) ParticipationApi(com.github.robozonky.api.remote.ParticipationApi) WalletApi(com.github.robozonky.api.remote.WalletApi) LoanApi(com.github.robozonky.api.remote.LoanApi) PortfolioApi(com.github.robozonky.api.remote.PortfolioApi) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) RawLoan(com.github.robozonky.api.remote.entities.RawLoan) ControlApi(com.github.robozonky.api.remote.ControlApi) RawDevelopment(com.github.robozonky.api.remote.entities.RawDevelopment) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) RawLoan(com.github.robozonky.api.remote.entities.RawLoan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) CollectionsApi(com.github.robozonky.api.remote.CollectionsApi) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) Test(org.junit.jupiter.api.Test)

Example 4 with ControlApi

use of com.github.robozonky.api.remote.ControlApi in project robozonky by RoboZonky.

the class Session method invest.

/**
 * Request {@link ControlApi} to invest in a given loan, leveraging the {@link ConfirmationProvider}.
 * @param recommendation Loan to invest into.
 * @return True if investment successful. The investment is reflected in {@link #getResult()}.
 */
public boolean invest(final RecommendedLoan recommendation) {
    final LoanDescriptor loan = recommendation.descriptor();
    final int loanId = loan.item().getId();
    if (portfolioOverview.getCzkAvailable() < recommendation.amount().intValue()) {
        // should not be allowed by the calling code
        return false;
    }
    Events.fire(new InvestmentRequestedEvent(recommendation));
    final boolean seenBefore = state.getSeenLoans().stream().anyMatch(l -> isSameLoan(l, loanId));
    final ZonkyResponse response = investor.invest(recommendation, seenBefore);
    Session.LOGGER.debug("Response for loan {}: {}.", loanId, response);
    final String providerId = investor.getConfirmationProviderId().orElse("-");
    switch(response.getType()) {
        case REJECTED:
            return investor.getConfirmationProviderId().map(c -> {
                Events.fire(new InvestmentRejectedEvent(recommendation, providerId));
                // rejected through a confirmation provider => forget
                discard(loan);
                return false;
            }).orElseGet(() -> {
                // rejected due to no confirmation provider => make available for direct investment later
                Events.fire(new InvestmentSkippedEvent(recommendation));
                Session.LOGGER.debug("Loan #{} protected by CAPTCHA, will check back later.", loanId);
                skip(loan);
                return false;
            });
        case DELEGATED:
            final Event e = new InvestmentDelegatedEvent(recommendation, providerId);
            Events.fire(e);
            if (recommendation.isConfirmationRequired()) {
                // confirmation required, delegation successful => forget
                discard(loan);
            } else {
                // confirmation not required, delegation successful => make available for direct investment later
                skip(loan);
            }
            return false;
        case INVESTED:
            final int confirmedAmount = response.getConfirmedAmount().getAsInt();
            final Investment i = Investment.fresh(recommendation.descriptor().item(), confirmedAmount);
            markSuccessfulInvestment(i);
            Events.fire(new InvestmentMadeEvent(i, loan.item(), portfolioOverview));
            return true;
        case // still protected by CAPTCHA
        SEEN_BEFORE:
            return false;
        default:
            throw new IllegalStateException("Not possible.");
    }
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) TransactionCategory(com.github.robozonky.api.remote.enums.TransactionCategory) LoggerFactory(org.slf4j.LoggerFactory) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) ControlApi(com.github.robozonky.api.remote.ControlApi) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) Events(com.github.robozonky.app.Events) Authenticated(com.github.robozonky.app.authentication.Authenticated) Defaults(com.github.robozonky.internal.api.Defaults) ConfirmationProvider(com.github.robozonky.api.confirmations.ConfirmationProvider) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Logger(org.slf4j.Logger) Collection(java.util.Collection) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) Collectors(java.util.stream.Collectors) Event(com.github.robozonky.api.notifications.Event) List(java.util.List) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Collections(java.util.Collections) LoanRecommendedEvent(com.github.robozonky.api.notifications.LoanRecommendedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) 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) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent)

Example 5 with ControlApi

use of com.github.robozonky.api.remote.ControlApi in project robozonky by RoboZonky.

the class ZonkyTest method purchase.

@Test
void purchase() {
    final ControlApi control = mock(ControlApi.class);
    final Api<ControlApi> ca = mockApi(control);
    final PaginatedApi<RawLoan, LoanApi> la = mockApi();
    final PaginatedApi<BlockedAmount, WalletApi> wa = mockApi();
    final PaginatedApi<RawInvestment, PortfolioApi> pa = mockApi();
    final PaginatedApi<Participation, ParticipationApi> sa = mockApi();
    final PaginatedApi<RawDevelopment, CollectionsApi> caa = mockApi();
    final Zonky z = new Zonky(ca, la, sa, pa, wa, caa);
    final Participation p = mock(Participation.class);
    when(p.getRemainingPrincipal()).thenReturn(BigDecimal.TEN);
    when(p.getId()).thenReturn(1);
    z.purchase(p);
    verify(control).purchase(eq(p.getId()), any());
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) ParticipationApi(com.github.robozonky.api.remote.ParticipationApi) WalletApi(com.github.robozonky.api.remote.WalletApi) LoanApi(com.github.robozonky.api.remote.LoanApi) PortfolioApi(com.github.robozonky.api.remote.PortfolioApi) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) RawLoan(com.github.robozonky.api.remote.entities.RawLoan) ControlApi(com.github.robozonky.api.remote.ControlApi) RawDevelopment(com.github.robozonky.api.remote.entities.RawDevelopment) CollectionsApi(com.github.robozonky.api.remote.CollectionsApi) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) Test(org.junit.jupiter.api.Test)

Aggregations

ControlApi (com.github.robozonky.api.remote.ControlApi)6 BlockedAmount (com.github.robozonky.api.remote.entities.BlockedAmount)6 CollectionsApi (com.github.robozonky.api.remote.CollectionsApi)5 LoanApi (com.github.robozonky.api.remote.LoanApi)5 ParticipationApi (com.github.robozonky.api.remote.ParticipationApi)5 PortfolioApi (com.github.robozonky.api.remote.PortfolioApi)5 WalletApi (com.github.robozonky.api.remote.WalletApi)5 Participation (com.github.robozonky.api.remote.entities.Participation)5 RawDevelopment (com.github.robozonky.api.remote.entities.RawDevelopment)5 RawInvestment (com.github.robozonky.api.remote.entities.RawInvestment)5 RawLoan (com.github.robozonky.api.remote.entities.RawLoan)5 Test (org.junit.jupiter.api.Test)5 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)4 ConfirmationProvider (com.github.robozonky.api.confirmations.ConfirmationProvider)1 Event (com.github.robozonky.api.notifications.Event)1 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)1 ExecutionStartedEvent (com.github.robozonky.api.notifications.ExecutionStartedEvent)1 InvestmentDelegatedEvent (com.github.robozonky.api.notifications.InvestmentDelegatedEvent)1 InvestmentMadeEvent (com.github.robozonky.api.notifications.InvestmentMadeEvent)1 InvestmentRejectedEvent (com.github.robozonky.api.notifications.InvestmentRejectedEvent)1