Search in sources :

Example 21 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated in project robozonky by RoboZonky.

the class Selling method sell.

private void sell(final Portfolio portfolio, final SellStrategy strategy, final Authenticated auth) {
    final PortfolioOverview overview = portfolio.calculateOverview();
    final Set<InvestmentDescriptor> eligible = portfolio.getActiveForSecondaryMarketplace().parallel().map(i -> getDescriptor(i, auth)).collect(Collectors.toSet());
    Events.fire(new SellingStartedEvent(eligible, overview));
    final Collection<Investment> investmentsSold = strategy.recommend(eligible, overview).peek(r -> Events.fire(new SaleRecommendedEvent(r))).map(r -> auth.call(zonky -> processInvestment(zonky, r))).flatMap(o -> o.map(Stream::of).orElse(Stream.empty())).collect(Collectors.toSet());
    Events.fire(new SellingCompletedEvent(investmentsSold, portfolio.calculateOverview()));
}
Also used : SaleRecommendedEvent(com.github.robozonky.api.notifications.SaleRecommendedEvent) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Zonky(com.github.robozonky.common.remote.Zonky) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) SaleRequestedEvent(com.github.robozonky.api.notifications.SaleRequestedEvent) InvestmentDescriptor(com.github.robozonky.api.strategies.InvestmentDescriptor) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) RecommendedInvestment(com.github.robozonky.api.strategies.RecommendedInvestment) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) Stream(java.util.stream.Stream) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) LoanCache(com.github.robozonky.app.util.LoanCache) Events(com.github.robozonky.app.Events) Authenticated(com.github.robozonky.app.authentication.Authenticated) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Optional(java.util.Optional) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) SellStrategy(com.github.robozonky.api.strategies.SellStrategy) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) SaleRecommendedEvent(com.github.robozonky.api.notifications.SaleRecommendedEvent) InvestmentDescriptor(com.github.robozonky.api.strategies.InvestmentDescriptor) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) RecommendedInvestment(com.github.robozonky.api.strategies.RecommendedInvestment) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment)

Example 22 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated in project robozonky by RoboZonky.

the class CommandLine method newApplicationConfiguration.

private Optional<InvestmentMode> newApplicationConfiguration(final OperatingMode mode) {
    return SecretProviderFactory.getSecretProvider(authenticationFragment).flatMap(secrets -> {
        final Duration duration = Settings.INSTANCE.getTokenRefreshPeriod();
        final Authenticated auth = Authenticated.tokenBased(secrets, duration);
        return mode.configure(this, auth);
    });
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Duration(java.time.Duration)

Example 23 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated in project robozonky by RoboZonky.

the class OperatingMode method configure.

public Optional<InvestmentMode> configure(final CommandLine cli, final Authenticated auth) {
    final SecretProvider secretProvider = auth.getSecretProvider();
    final boolean isDryRun = cli.getTweaksFragment().isDryRunEnabled();
    // initialize SessionInfo before the robot potentially sends the first notification
    Events.initialize(new SessionInfo(secretProvider.getUsername(), cli.getName(), isDryRun));
    // and now initialize the chosen mode of operation
    return cli.getConfirmationFragment().getConfirmationCredentials().map(value -> new Credentials(value, secretProvider)).map(credentials -> this.getZonkyProxyBuilder(credentials, secretProvider)).orElse(Optional.of(new Investor.Builder())).map(builder -> {
        if (isDryRun) {
            LOGGER.info("RoboZonky is doing a dry run. It will not invest any real money.");
            builder.asDryRun();
        }
        builder.asUser(secretProvider.getUsername());
        return this.getInvestmentMode(cli, auth, builder);
    }).orElse(Optional.empty());
}
Also used : SessionInfo(com.github.robozonky.api.notifications.SessionInfo) ToStringBuilder(com.github.robozonky.internal.api.ToStringBuilder) Investor(com.github.robozonky.app.investing.Investor) Logger(org.slf4j.Logger) Events(com.github.robozonky.app.Events) ConfirmationProviderLoader(com.github.robozonky.common.extensions.ConfirmationProviderLoader) Authenticated(com.github.robozonky.app.authentication.Authenticated) Credentials(com.github.robozonky.common.secrets.Credentials) LoggerFactory(org.slf4j.LoggerFactory) Optional(java.util.Optional) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) ConfirmationProvider(com.github.robozonky.api.confirmations.ConfirmationProvider) ToStringBuilder(com.github.robozonky.internal.api.ToStringBuilder) SessionInfo(com.github.robozonky.api.notifications.SessionInfo) Credentials(com.github.robozonky.common.secrets.Credentials) SecretProvider(com.github.robozonky.common.secrets.SecretProvider)

Example 24 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated 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 25 with Authenticated

use of com.github.robozonky.app.authentication.Authenticated 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)

Aggregations

Authenticated (com.github.robozonky.app.authentication.Authenticated)44 Test (org.junit.jupiter.api.Test)37 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)32 Zonky (com.github.robozonky.common.remote.Zonky)31 Portfolio (com.github.robozonky.app.portfolio.Portfolio)28 Event (com.github.robozonky.api.notifications.Event)13 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)11 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)10 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)9 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)9 Optional (java.util.Optional)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 LoanRecommendedEvent (com.github.robozonky.api.notifications.LoanRecommendedEvent)8 Collection (java.util.Collection)8