Search in sources :

Example 11 with OAuthFactory

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));
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) Test(org.junit.jupiter.api.Test)

Example 12 with OAuthFactory

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);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) Test(org.junit.jupiter.api.Test)

Example 13 with OAuthFactory

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);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) Nullable(org.eclipse.jdt.annotation.Nullable) Test(org.junit.jupiter.api.Test)

Example 14 with OAuthFactory

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));
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) Test(org.junit.jupiter.api.Test)

Example 15 with OAuthFactory

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);
    });
}
Also used : OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) Test(org.junit.jupiter.api.Test)

Aggregations

OAuthFactory (org.openhab.core.auth.client.oauth2.OAuthFactory)18 OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)14 Test (org.junit.jupiter.api.Test)13 OpenHabOAuthTokenRefresher (org.openhab.binding.mielecloud.internal.auth.OpenHabOAuthTokenRefresher)5 AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)5 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Nullable (org.eclipse.jdt.annotation.Nullable)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 OpenHabOsgiTest (org.openhab.binding.mielecloud.internal.util.OpenHabOsgiTest)2 IOException (java.io.IOException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 InnogyWebSocket (org.openhab.binding.innogysmarthome.internal.InnogyWebSocket)1 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)1 Configuration (org.openhab.core.config.core.Configuration)1 Bridge (org.openhab.core.thing.Bridge)1 ThingRegistry (org.openhab.core.thing.ThingRegistry)1 ThingUID (org.openhab.core.thing.ThingUID)1