use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTheRefreshListenerIsUnsetAndTheClientServiceIsNotAvailableThenTheListenerIsCleared.
@Test
public void whenTheRefreshListenerIsUnsetAndTheClientServiceIsNotAvailableThenTheListenerIsCleared() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
// given:
OAuthClientService oauthClientService = mock(OAuthClientService.class);
OAuthFactory oauthFactory = mock(OAuthFactory.class);
when(oauthFactory.getOAuthClientService(MieleCloudBindingTestConstants.SERVICE_HANDLE)).thenReturn(oauthClientService);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// when:
refresher.unsetRefreshListener(MieleCloudBindingTestConstants.SERVICE_HANDLE);
// then:
assertFalse(hasAccessTokenRefreshListenerForServiceHandle(refresher, MieleCloudBindingTestConstants.SERVICE_HANDLE));
}
use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokensAreRemovedThenTheRuntimeIsRequestedToDeleteServiceAndAccessToken.
@Test
public void whenTokensAreRemovedThenTheRuntimeIsRequestedToDeleteServiceAndAccessToken() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
OAuthClientService oauthClientService = mock(OAuthClientService.class);
OAuthFactory oauthFactory = mock(OAuthFactory.class);
when(oauthFactory.getOAuthClientService(MieleCloudBindingTestConstants.SERVICE_HANDLE)).thenReturn(oauthClientService);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// when:
refresher.removeTokensFromStorage(MieleCloudBindingTestConstants.SERVICE_HANDLE);
// then:
verify(oauthFactory).deleteServiceAndAccessToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
}
use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokenIsRefreshedThenTheListenerIsCalledWithTheNewAccessToken.
@Test
public void whenTokenIsRefreshedThenTheListenerIsCalledWithTheNewAccessToken() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
accessTokenResponse.setAccessToken(ACCESS_TOKEN);
OAuthClientService oauthClientService = mock(OAuthClientService.class);
OAuthFactory oauthFactory = mock(OAuthFactory.class);
when(oauthFactory.getOAuthClientService(MieleCloudBindingTestConstants.SERVICE_HANDLE)).thenReturn(oauthClientService);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
when(oauthClientService.refreshToken()).thenAnswer(new Answer<@Nullable AccessTokenResponse>() {
@Override
@Nullable
public AccessTokenResponse answer(@Nullable InvocationOnMock invocation) throws Throwable {
getAccessTokenRefreshListenerByServiceHandle(refresher, MieleCloudBindingTestConstants.SERVICE_HANDLE).onAccessTokenResponse(accessTokenResponse);
return accessTokenResponse;
}
});
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// when:
refresher.refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
// then:
verify(listener).onNewAccessToken(ACCESS_TOKEN);
}
use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTheRefreshListenerIsUnsetThenTheListenerIsClearedAndRemovedFromTheClientService.
@Test
public void whenTheRefreshListenerIsUnsetThenTheListenerIsClearedAndRemovedFromTheClientService() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
// given:
OAuthClientService oauthClientService = mock(OAuthClientService.class);
OAuthFactory oauthFactory = mock(OAuthFactory.class);
when(oauthFactory.getOAuthClientService(MieleCloudBindingTestConstants.SERVICE_HANDLE)).thenReturn(oauthClientService);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// when:
refresher.unsetRefreshListener(MieleCloudBindingTestConstants.SERVICE_HANDLE);
// then:
verify(oauthClientService).removeAccessTokenRefreshListener(any());
assertFalse(hasAccessTokenRefreshListenerForServiceHandle(refresher, MieleCloudBindingTestConstants.SERVICE_HANDLE));
}
use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTheAccountWasNotConfiguredPriorToTheThingInitializingThenNoRefreshListenerCanBeRegistered.
@Test
public void whenTheAccountWasNotConfiguredPriorToTheThingInitializingThenNoRefreshListenerCanBeRegistered() {
// given:
OAuthFactory oauthFactory = mock(OAuthFactory.class);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
// when:
assertThrows(OAuthException.class, () -> {
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
});
}
Aggregations