use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class AuthenticatedTest method run.
@Test
void run() {
final Zonky z = mock(Zonky.class);
final AbstractAuthenticated a = new AbstractAuthenticated() {
@Override
public <T> T call(final Function<Zonky, T> operation) {
return operation.apply(z);
}
@Override
public SecretProvider getSecretProvider() {
return null;
}
};
final Consumer<Zonky> runnable = mock(Consumer.class);
a.run(runnable);
verify(runnable).accept(eq(z));
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class AuthenticatedTest method tokenProper.
@Test
void tokenProper() {
// prepare SUT
final SecretProvider sp = SecretProvider.fallback(UUID.randomUUID().toString(), new char[0]);
final String username = sp.getUsername();
final char[] password = sp.getPassword();
final ZonkyApiToken token = new ZonkyApiToken(UUID.randomUUID().toString(), UUID.randomUUID().toString(), 299);
final OAuth oauth = mock(OAuth.class);
when(oauth.login(eq(username), eq(password))).thenReturn(token);
final Zonky z = mock(Zonky.class);
final ApiProvider api = mockApiProvider(oauth, z);
final TokenBasedAccess a = (TokenBasedAccess) Authenticated.tokenBased(api, sp, Duration.ofSeconds(60));
// call SUT
final Function<Zonky, Collection<RawInvestment>> f = mock(Function.class);
final Collection<RawInvestment> expectedResult = Collections.emptyList();
when(f.apply(eq(z))).thenReturn(expectedResult);
final Collection<RawInvestment> result = a.call(f);
assertSoftly(softly -> {
softly.assertThat(result).isSameAs(expectedResult);
softly.assertThat(a.getSecretProvider()).isSameAs(sp);
});
verify(oauth).login(eq(username), eq(password));
verify(oauth, never()).refresh(any());
verify(z, never()).logout();
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class AuthenticatedTest method defaultMethod.
@Test
void defaultMethod() {
final Zonky z = mock(Zonky.class);
final Authenticated a = mockAuthentication(z);
final Consumer<Zonky> c = zonky -> z.logout();
a.run(c);
verify(z).logout();
}
use of com.github.robozonky.common.remote.Zonky 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.common.remote.Zonky in project robozonky by RoboZonky.
the class SellingTest method mockApi.
private static Zonky mockApi() {
final Zonky zonky = mock(Zonky.class);
when(zonky.getWallet()).thenReturn(new Wallet(BigDecimal.TEN, BigDecimal.ZERO));
when(zonky.getLoan(anyInt())).thenReturn(Loan.custom().build());
return zonky;
}
Aggregations