Search in sources :

Example 1 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.

the class ZonkySettingsValidatorTest method warning.

@Test
void warning() {
    // mock data
    final OAuth oauth = mock(OAuth.class);
    when(oauth.login(any(), any())).thenThrow(new IllegalStateException());
    final ApiProvider provider = mockApiProvider(oauth);
    final InstallData d = ZonkySettingsValidatorTest.mockInstallData();
    // execute SUT
    final ZonkySettingsValidator validator = new ZonkySettingsValidator(() -> provider);
    final DataValidator.Status result = validator.validateData(d);
    // test
    assertThat(result).isEqualTo(DataValidator.Status.WARNING);
}
Also used : InstallData(com.izforge.izpack.api.data.InstallData) DataValidator(com.izforge.izpack.api.installer.DataValidator) ApiProvider(com.github.robozonky.common.remote.ApiProvider) OAuth(com.github.robozonky.common.remote.OAuth) Test(org.junit.jupiter.api.Test)

Example 2 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.

the class ZonkySettingsValidatorTest method mockApiProvider.

private static ApiProvider mockApiProvider(final OAuth oauth, final ZonkyApiToken token, final Zonky zonky) {
    final ApiProvider api = mock(ApiProvider.class);
    when(api.oauth(any(Function.class))).then(i -> {
        final Function f = i.getArgument(0);
        return f.apply(oauth);
    });
    doAnswer(i -> {
        final Function f = i.getArgument(1);
        return f.apply(zonky);
    }).when(api).authenticated(mockToken(token), any(Function.class));
    doAnswer(i -> {
        final Consumer f = i.getArgument(1);
        f.accept(zonky);
        return null;
    }).when(api).authenticated(mockToken(token), any(Consumer.class));
    return api;
}
Also used : Function(java.util.function.Function) Consumer(java.util.function.Consumer) ApiProvider(com.github.robozonky.common.remote.ApiProvider)

Example 3 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.

the class ZonkySettingsValidator method validateDataPossiblyThrowingException.

@Override
public DataValidator.Status validateDataPossiblyThrowingException(final InstallData installData) {
    final String username = Variables.ZONKY_USERNAME.getValue(installData);
    final String password = Variables.ZONKY_PASSWORD.getValue(installData);
    final ApiProvider p = apiSupplier.get();
    return p.oauth((oauth) -> {
        try {
            LOGGER.info("Logging in.");
            final ZonkyApiToken token = oauth.login(username, password.toCharArray());
            LOGGER.info("Logging out.");
            p.authenticated(token, Zonky::logout);
            return DataValidator.Status.OK;
        } catch (final ServerErrorException t) {
            LOGGER.log(Level.SEVERE, "Failed accessing Zonky.", t);
            return DataValidator.Status.ERROR;
        } catch (final Exception t) {
            LOGGER.log(Level.WARNING, "Failed logging in.", t);
            return DataValidator.Status.WARNING;
        }
    });
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) ServerErrorException(javax.ws.rs.ServerErrorException) ServerErrorException(javax.ws.rs.ServerErrorException) Zonky(com.github.robozonky.common.remote.Zonky)

Example 4 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.

the class CheckerTest method mockApiThatReturnsOneLoan.

private static ApiProvider mockApiThatReturnsOneLoan() {
    final RawLoan l = mock(RawLoan.class);
    final ApiProvider provider = mock(ApiProvider.class);
    doReturn(Collections.singletonList(l)).when(provider).marketplace();
    return provider;
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) RawLoan(com.github.robozonky.api.remote.entities.RawLoan)

Example 5 with ApiProvider

use of com.github.robozonky.common.remote.ApiProvider in project robozonky by RoboZonky.

the class CheckerTest method confirmationsMarketplaceWithoutLoans.

@Test
void confirmationsMarketplaceWithoutLoans() {
    final ApiProvider provider = mock(ApiProvider.class);
    when(provider.marketplace()).thenReturn(Collections.emptyList());
    final boolean result = Checker.confirmations(mock(ConfirmationProvider.class), "", SECRET, () -> provider);
    assertThat(result).isFalse();
}
Also used : ConfirmationProvider(com.github.robozonky.api.confirmations.ConfirmationProvider) ApiProvider(com.github.robozonky.common.remote.ApiProvider) Test(org.junit.jupiter.api.Test)

Aggregations

ApiProvider (com.github.robozonky.common.remote.ApiProvider)19 Test (org.junit.jupiter.api.Test)13 OAuth (com.github.robozonky.common.remote.OAuth)12 ZonkyApiToken (com.github.robozonky.api.remote.entities.ZonkyApiToken)11 Zonky (com.github.robozonky.common.remote.Zonky)5 Optional (java.util.Optional)5 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)3 SecretProvider (com.github.robozonky.common.secrets.SecretProvider)3 InstallData (com.izforge.izpack.api.data.InstallData)3 DataValidator (com.izforge.izpack.api.installer.DataValidator)3 Collection (java.util.Collection)3 ConfirmationProvider (com.github.robozonky.api.confirmations.ConfirmationProvider)2 RawInvestment (com.github.robozonky.api.remote.entities.RawInvestment)2 RawLoan (com.github.robozonky.api.remote.entities.RawLoan)2 Function (java.util.function.Function)2 ServerErrorException (javax.ws.rs.ServerErrorException)2 Consumer (java.util.function.Consumer)1