2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.webservice.retry;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
19 import java.util.Objects;
20 import java.util.function.Consumer;
21 import java.util.function.Supplier;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openhab.binding.mielecloud.internal.MieleCloudBindingTestConstants;
30 import org.openhab.binding.mielecloud.internal.auth.OAuthException;
31 import org.openhab.binding.mielecloud.internal.auth.OAuthTokenRefresher;
32 import org.openhab.binding.mielecloud.internal.webservice.exception.AuthorizationFailedException;
33 import org.openhab.binding.mielecloud.internal.webservice.exception.MieleWebserviceException;
36 * @author Björn Lange - Initial contribution
38 @ExtendWith(MockitoExtension.class)
40 public class AuthorizationFailedRetryStrategyTest {
41 private static final String TEST_STRING = "Some Test String";
45 private Supplier<@Nullable String> operationWithReturnValue;
48 private Consumer<Exception> onException;
51 private Runnable operation;
53 private final OAuthTokenRefresher refresher = mock(OAuthTokenRefresher.class);
55 private Supplier<@Nullable String> getOperationWithReturnValue() {
56 assertNotNull(operationWithReturnValue);
57 return Objects.requireNonNull(operationWithReturnValue);
60 private Consumer<Exception> getOnException() {
61 assertNotNull(onException);
62 return Objects.requireNonNull(onException);
65 private Runnable getOperation() {
66 assertNotNull(operation);
67 return Objects.requireNonNull(operation);
71 public void testPerformRetryableOperationWithReturnValueInvokesOperation() {
73 when(getOperationWithReturnValue().get()).thenReturn(TEST_STRING);
75 AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
76 MieleCloudBindingTestConstants.SERVICE_HANDLE);
79 String result = retryStrategy.performRetryableOperation(getOperationWithReturnValue(), getOnException());
82 assertEquals(TEST_STRING, result);
86 public void testPerformRetryableOperationWithReturnValueInvokesRefreshTokenAndRetriesOperation() {
88 when(getOperationWithReturnValue().get()).thenThrow(AuthorizationFailedException.class).thenReturn(TEST_STRING);
90 AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
91 MieleCloudBindingTestConstants.SERVICE_HANDLE);
94 String result = retryStrategy.performRetryableOperation(getOperationWithReturnValue(), getOnException());
97 assertEquals(TEST_STRING, result);
98 verify(getOnException()).accept(any());
99 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
100 verifyNoMoreInteractions(getOnException(), refresher);
104 public void testPerformRetryableOperationWithReturnValueThrowsMieleWebserviceExceptionWhenRetryingTheOperationFails() {
106 when(getOperationWithReturnValue().get()).thenThrow(AuthorizationFailedException.class);
108 AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
109 MieleCloudBindingTestConstants.SERVICE_HANDLE);
111 assertThrows(MieleWebserviceException.class, () -> {
114 retryStrategy.performRetryableOperation(getOperationWithReturnValue(), getOnException());
115 } catch (Exception e) {
117 verify(getOnException()).accept(any());
118 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
119 verifyNoMoreInteractions(getOnException(), refresher);
126 public void testPerformRetryableOperationInvokesOperation() {
128 AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
129 MieleCloudBindingTestConstants.SERVICE_HANDLE);
132 retryStrategy.performRetryableOperation(getOperation(), getOnException());
135 verify(getOperation()).run();
136 verifyNoMoreInteractions(getOperation());
140 public void testPerformRetryableOperationInvokesRefreshTokenAndRetriesOperation() {
142 doThrow(AuthorizationFailedException.class).doNothing().when(getOperation()).run();
144 AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
145 MieleCloudBindingTestConstants.SERVICE_HANDLE);
148 retryStrategy.performRetryableOperation(getOperation(), getOnException());
151 verify(getOnException()).accept(any());
152 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
153 verify(getOperation(), times(2)).run();
154 verifyNoMoreInteractions(getOnException(), refresher, getOperation());
158 public void testPerformRetryableOperationThrowsMieleWebserviceExceptionWhenRetryingTheOperationFails() {
160 doThrow(AuthorizationFailedException.class).when(getOperation()).run();
162 AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
163 MieleCloudBindingTestConstants.SERVICE_HANDLE);
165 assertThrows(MieleWebserviceException.class, () -> {
168 retryStrategy.performRetryableOperation(getOperation(), getOnException());
169 } catch (Exception e) {
171 verify(getOnException()).accept(any());
172 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
173 verify(getOperation(), times(2)).run();
174 verifyNoMoreInteractions(getOnException(), refresher, getOperation());
181 public void testPerformRetryableOperationThrowsMieleWebserviceExceptionWhenTokenRefreshingFails() {
183 doThrow(AuthorizationFailedException.class).when(getOperation()).run();
184 doThrow(OAuthException.class).when(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
186 AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
187 MieleCloudBindingTestConstants.SERVICE_HANDLE);
189 assertThrows(MieleWebserviceException.class, () -> {
192 retryStrategy.performRetryableOperation(getOperation(), getOnException());
193 } catch (Exception e) {
195 verify(getOnException()).accept(any());
196 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
197 verify(getOperation()).run();
198 verifyNoMoreInteractions(getOnException(), refresher, getOperation());