Search in sources :

Example 1 with OAuthFactory

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);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) ThingUID(org.openhab.core.thing.ThingUID) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) HttpClient(org.eclipse.jetty.client.HttpClient) InnogyWebSocket(org.openhab.binding.innogysmarthome.internal.InnogyWebSocket) Bridge(org.openhab.core.thing.Bridge) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with OAuthFactory

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

Example 3 with OAuthFactory

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));
}
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 4 with OAuthFactory

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

Example 5 with OAuthFactory

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;
        }
    });
}
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)

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