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()));
}
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);
});
}
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());
}
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);
}
});
});
}
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()));
}
Aggregations