use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class PortfolioUpdaterTest method backoffFailed.
@Test
void backoffFailed() {
final Zonky z = harmlessZonky(10_000);
// will always fail
doThrow(IllegalStateException.class).when(z).getInvestments();
final Authenticated a = mockAuthentication(z);
final Consumer<Throwable> t = mock(Consumer.class);
final PortfolioUpdater instance = new PortfolioUpdater(t, a, mockBalance(z), Duration.ofSeconds(2));
instance.run();
assertSoftly(softly -> {
softly.assertThat(instance.get()).isEmpty();
softly.assertThat(instance.isUpdating()).isTrue();
});
verify(t).accept(notNull());
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class PortfolioUpdaterTest method updatingDependants.
@Test
void updatingDependants() {
final Zonky z = harmlessZonky(10_000);
final Authenticated a = mockAuthentication(z);
final PortfolioDependant dependant = mock(PortfolioDependant.class);
final PortfolioUpdater instance = new PortfolioUpdater((t) -> {
}, a, mockBalance(z));
instance.registerDependant(dependant);
instance.run();
// this is the call to update Portfolio
verify(a, times(2)).call(any());
final Optional<Portfolio> result = instance.get();
// make sure that the dependants were called with the proper value of Portfolio
verify(dependant).accept(eq(result.get()), eq(a));
// it's false when update finished
assertThat(instance.isUpdating()).isFalse();
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class AbstractZonkyLeveragingTest method mockAuthentication.
protected static Authenticated mockAuthentication(final Zonky zonky) {
final Authenticated auth = mock(Authenticated.class);
when(auth.getSecretProvider()).thenReturn(SecretProvider.fallback("someone", "password".toCharArray()));
when(auth.getRestrictions()).thenReturn(new Restrictions());
doAnswer(invocation -> {
final Function<Zonky, Object> operation = invocation.getArgument(0);
return operation.apply(zonky);
}).when(auth).call(any());
doAnswer(invocation -> {
final Consumer<Zonky> operation = invocation.getArgument(0);
operation.accept(zonky);
return null;
}).when(auth).run(any());
return auth;
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class AuthenticatedTest method passwordProper.
@Test
void passwordProper() {
// 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 Authenticated a = Authenticated.passwordBased(api, sp);
// 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);
assertThat(result).isSameAs(expectedResult);
verify(oauth).login(eq(username), eq(password));
verify(oauth, never()).refresh(any());
verify(z).logout();
}
use of com.github.robozonky.common.remote.Zonky in project robozonky by RoboZonky.
the class AuthenticatedTest method passwordLogsOutEvenWhenFailing.
@Test
void passwordLogsOutEvenWhenFailing() {
// 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 Authenticated a = Authenticated.passwordBased(api, sp);
// call SUT
final Function<Zonky, Collection<RawInvestment>> f = mock(Function.class);
when(f.apply(eq(z))).thenThrow(new IllegalStateException());
assertThatThrownBy(() -> a.call(f)).isInstanceOf(IllegalStateException.class);
verify(z).logout();
}
Aggregations