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