Search in sources :

Example 46 with Zonky

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());
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 47 with Zonky

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();
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Portfolio(com.github.robozonky.app.portfolio.Portfolio) PortfolioDependant(com.github.robozonky.app.portfolio.PortfolioDependant) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 48 with Zonky

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;
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Zonky(com.github.robozonky.common.remote.Zonky)

Example 49 with Zonky

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();
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) Collection(java.util.Collection) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) OAuth(com.github.robozonky.common.remote.OAuth) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 50 with Zonky

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();
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) Collection(java.util.Collection) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) OAuth(com.github.robozonky.common.remote.OAuth) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Aggregations

Zonky (com.github.robozonky.common.remote.Zonky)55 Test (org.junit.jupiter.api.Test)47 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)46 Authenticated (com.github.robozonky.app.authentication.Authenticated)30 Portfolio (com.github.robozonky.app.portfolio.Portfolio)30 Event (com.github.robozonky.api.notifications.Event)19 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)15 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)13 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)12 Wallet (com.github.robozonky.api.remote.entities.Wallet)10 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)9 BigDecimal (java.math.BigDecimal)9 Collection (java.util.Collection)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