use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class InnogyBridgeHandlerTest method before.
@BeforeEach
public void before() throws Exception {
bridgeMock = mock(Bridge.class);
when(bridgeMock.getUID()).thenReturn(new ThingUID("innogysmarthome", "bridge"));
webSocketMock = mock(InnogyWebSocket.class);
OAuthClientService oAuthService = mock(OAuthClientService.class);
OAuthFactory oAuthFactoryMock = mock(OAuthFactory.class);
when(oAuthFactoryMock.createOAuthClientService(any(), any(), any(), any(), any(), any(), any())).thenReturn(oAuthService);
HttpClient httpClientMock = mock(HttpClient.class);
bridgeHandler = new InnogyBridgeHandlerAccessible(bridgeMock, oAuthFactoryMock, httpClientMock);
}
use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokenRefreshFailsDueToAnIllegalResponseThenTheListenerIsNotNotified.
@Test
public void whenTokenRefreshFailsDueToAnIllegalResponseThenTheListenerIsNotNotified() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
OAuthClientService oauthClientService = mock(OAuthClientService.class);
when(oauthClientService.refreshToken()).thenThrow(new OAuthResponseException());
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:
assertThrows(OAuthException.class, () -> {
try {
refresher.refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
} catch (OAuthException e) {
verifyNoInteractions(listener);
throw e;
}
});
}
use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenARefreshListenerIsRegisteredThenAListenerIsRegisteredAtTheClientService.
@Test
public void whenARefreshListenerIsRegisteredThenAListenerIsRegisteredAtTheClientService() 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);
// when:
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// then:
verify(oauthClientService).addAccessTokenRefreshListener(any());
assertNotNull(getAccessTokenRefreshListenerByServiceHandle(refresher, MieleCloudBindingTestConstants.SERVICE_HANDLE));
}
use of org.openhab.core.auth.client.oauth2.OAuthFactory in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTheRefreshListenerIsUnsetAndWasNotRegisteredBeforeThenNothingHappens.
@Test
public void whenTheRefreshListenerIsUnsetAndWasNotRegisteredBeforeThenNothingHappens() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
// given:
OAuthFactory oauthFactory = mock(OAuthFactory.class);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
// 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 whenTokenIsRefreshedAndNoAccessTokenIsProvidedThenTheListenerIsNotNotified.
@Test
public void whenTokenIsRefreshedAndNoAccessTokenIsProvidedThenTheListenerIsNotNotified() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
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:
assertThrows(OAuthException.class, () -> {
try {
refresher.refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
} catch (OAuthException e) {
verifyNoInteractions(listener);
throw e;
}
});
}
Aggregations