Search in sources :

Example 1 with OAuthClientService

use of org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService 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 OAuthClientService

use of org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.

the class HomeConnectBridgeHandler method handleConfigurationUpdate.

@Override
public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
    if (isModifyingCurrentConfig(configurationParameters)) {
        List<String> parameters = configurationParameters.entrySet().stream().map((entry) -> {
            if (CLIENT_ID.equals(entry.getKey()) || CLIENT_SECRET.equals(entry.getKey())) {
                return entry.getKey() + ": ***";
            }
            return entry.getKey() + ": " + entry.getValue();
        }).collect(Collectors.toList());
        logger.debug("Update bridge configuration. bridge={}, parameters={}", getThing().getLabel(), parameters);
        validateConfigurationParameters(configurationParameters);
        Configuration configuration = editConfiguration();
        for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
            configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
        }
        // invalidate oAuth credentials
        try {
            logger.debug("Clear oAuth credential store. bridge={}", getThing().getLabel());
            var oAuthClientService = this.oAuthClientService;
            if (oAuthClientService != null) {
                oAuthClientService.remove();
            }
        } catch (OAuthException e) {
            logger.error("Could not clear oAuth credentials. bridge={}", getThing().getLabel(), e);
        }
        if (isInitialized()) {
            // persist new configuration and reinitialize handler
            dispose();
            updateConfiguration(configuration);
            initialize();
        } else {
            // persist new configuration and notify Thing Manager
            updateConfiguration(configuration);
            @Nullable ThingHandlerCallback callback = getCallback();
            if (callback != null) {
                callback.configurationUpdated(this.getThing());
            } else {
                logger.warn("Handler {} tried updating its configuration although the handler was already disposed.", this.getClass().getSimpleName());
            }
        }
    }
}
Also used : ApiRequest(org.openhab.binding.homeconnect.internal.client.model.ApiRequest) ScheduledFuture(java.util.concurrent.ScheduledFuture) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ZonedDateTime(java.time.ZonedDateTime) AuthorizationException(org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException) LoggerFactory(org.slf4j.LoggerFactory) ThingHandlerService(org.openhab.core.thing.binding.ThingHandlerService) ArrayList(java.util.ArrayList) HttpClient(org.eclipse.jetty.client.HttpClient) ClientBuilder(javax.ws.rs.client.ClientBuilder) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Map(java.util.Map) HomeConnectServlet(org.openhab.binding.homeconnect.internal.servlet.HomeConnectServlet) ChannelUID(org.openhab.core.thing.ChannelUID) CommunicationException(org.openhab.binding.homeconnect.internal.client.exception.CommunicationException) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) HomeConnectApiClient(org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) Collection(java.util.Collection) Event(org.openhab.binding.homeconnect.internal.client.model.Event) IOException(java.io.IOException) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) HomeConnectEventSourceClient(org.openhab.binding.homeconnect.internal.client.HomeConnectEventSourceClient) HomeConnectBindingConstants(org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants) Collectors(java.util.stream.Collectors) SseEventSourceFactory(org.osgi.service.jaxrs.client.SseEventSourceFactory) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) Entry(java.util.Map.Entry) HomeConnectDiscoveryService(org.openhab.binding.homeconnect.internal.discovery.HomeConnectDiscoveryService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) Configuration(org.openhab.core.config.core.Configuration) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with OAuthClientService

use of org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService 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 4 with OAuthClientService

use of org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService 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 5 with OAuthClientService

use of org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService 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

OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)36 OAuthFactory (org.openhab.core.auth.client.oauth2.OAuthFactory)15 Test (org.junit.jupiter.api.Test)9 IOException (java.io.IOException)7 Nullable (org.eclipse.jdt.annotation.Nullable)7 AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)7 OAuthClientService (org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService)6 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)6 List (java.util.List)3 Map (java.util.Map)3 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 ClientBuilder (javax.ws.rs.client.ClientBuilder)2