]> git.basschouten.com Git - openhab-addons.git/blob
4bf7df7c371dcdf4e21c23d4329c4e8df7fbd248
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.webservice.retry;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.Objects;
20 import java.util.function.Consumer;
21 import java.util.function.Supplier;
22
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;
34
35 /**
36  * @author Björn Lange - Initial contribution
37  */
38 @ExtendWith(MockitoExtension.class)
39 @NonNullByDefault
40 public class AuthorizationFailedRetryStrategyTest {
41     private static final String TEST_STRING = "Some Test String";
42
43     @Mock
44     @Nullable
45     private Supplier<@Nullable String> operationWithReturnValue;
46     @Mock
47     @Nullable
48     private Consumer<Exception> onException;
49     @Mock
50     @Nullable
51     private Runnable operation;
52
53     private final OAuthTokenRefresher refresher = mock(OAuthTokenRefresher.class);
54
55     private Supplier<@Nullable String> getOperationWithReturnValue() {
56         assertNotNull(operationWithReturnValue);
57         return Objects.requireNonNull(operationWithReturnValue);
58     }
59
60     private Consumer<Exception> getOnException() {
61         assertNotNull(onException);
62         return Objects.requireNonNull(onException);
63     }
64
65     private Runnable getOperation() {
66         assertNotNull(operation);
67         return Objects.requireNonNull(operation);
68     }
69
70     @Test
71     public void testPerformRetryableOperationWithReturnValueInvokesOperation() {
72         // given:
73         when(getOperationWithReturnValue().get()).thenReturn(TEST_STRING);
74
75         AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
76                 MieleCloudBindingTestConstants.SERVICE_HANDLE);
77
78         // when:
79         String result = retryStrategy.performRetryableOperation(getOperationWithReturnValue(), getOnException());
80
81         // then:
82         assertEquals(TEST_STRING, result);
83     }
84
85     @Test
86     public void testPerformRetryableOperationWithReturnValueInvokesRefreshTokenAndRetriesOperation() {
87         // given:
88         when(getOperationWithReturnValue().get()).thenThrow(AuthorizationFailedException.class).thenReturn(TEST_STRING);
89
90         AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
91                 MieleCloudBindingTestConstants.SERVICE_HANDLE);
92
93         // when:
94         String result = retryStrategy.performRetryableOperation(getOperationWithReturnValue(), getOnException());
95
96         // then:
97         assertEquals(TEST_STRING, result);
98         verify(getOnException()).accept(any());
99         verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
100         verifyNoMoreInteractions(getOnException(), refresher);
101     }
102
103     @Test
104     public void testPerformRetryableOperationWithReturnValueThrowsMieleWebserviceExceptionWhenRetryingTheOperationFails() {
105         // given:
106         when(getOperationWithReturnValue().get()).thenThrow(AuthorizationFailedException.class);
107
108         AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
109                 MieleCloudBindingTestConstants.SERVICE_HANDLE);
110
111         assertThrows(MieleWebserviceException.class, () -> {
112             try {
113                 // when:
114                 retryStrategy.performRetryableOperation(getOperationWithReturnValue(), getOnException());
115             } catch (Exception e) {
116                 // then:
117                 verify(getOnException()).accept(any());
118                 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
119                 verifyNoMoreInteractions(getOnException(), refresher);
120                 throw e;
121             }
122         });
123     }
124
125     @Test
126     public void testPerformRetryableOperationInvokesOperation() {
127         // given:
128         AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
129                 MieleCloudBindingTestConstants.SERVICE_HANDLE);
130
131         // when:
132         retryStrategy.performRetryableOperation(getOperation(), getOnException());
133
134         // then:
135         verify(getOperation()).run();
136         verifyNoMoreInteractions(getOperation());
137     }
138
139     @Test
140     public void testPerformRetryableOperationInvokesRefreshTokenAndRetriesOperation() {
141         // given:
142         doThrow(AuthorizationFailedException.class).doNothing().when(getOperation()).run();
143
144         AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
145                 MieleCloudBindingTestConstants.SERVICE_HANDLE);
146
147         // when:
148         retryStrategy.performRetryableOperation(getOperation(), getOnException());
149
150         // then:
151         verify(getOnException()).accept(any());
152         verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
153         verify(getOperation(), times(2)).run();
154         verifyNoMoreInteractions(getOnException(), refresher, getOperation());
155     }
156
157     @Test
158     public void testPerformRetryableOperationThrowsMieleWebserviceExceptionWhenRetryingTheOperationFails() {
159         // given:
160         doThrow(AuthorizationFailedException.class).when(getOperation()).run();
161
162         AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
163                 MieleCloudBindingTestConstants.SERVICE_HANDLE);
164
165         assertThrows(MieleWebserviceException.class, () -> {
166             try {
167                 // when:
168                 retryStrategy.performRetryableOperation(getOperation(), getOnException());
169             } catch (Exception e) {
170                 // then:
171                 verify(getOnException()).accept(any());
172                 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
173                 verify(getOperation(), times(2)).run();
174                 verifyNoMoreInteractions(getOnException(), refresher, getOperation());
175                 throw e;
176             }
177         });
178     }
179
180     @Test
181     public void testPerformRetryableOperationThrowsMieleWebserviceExceptionWhenTokenRefreshingFails() {
182         // given:
183         doThrow(AuthorizationFailedException.class).when(getOperation()).run();
184         doThrow(OAuthException.class).when(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
185
186         AuthorizationFailedRetryStrategy retryStrategy = new AuthorizationFailedRetryStrategy(refresher,
187                 MieleCloudBindingTestConstants.SERVICE_HANDLE);
188
189         assertThrows(MieleWebserviceException.class, () -> {
190             try {
191                 // when:
192                 retryStrategy.performRetryableOperation(getOperation(), getOnException());
193             } catch (Exception e) {
194                 // then:
195                 verify(getOnException()).accept(any());
196                 verify(refresher).refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
197                 verify(getOperation()).run();
198                 verifyNoMoreInteractions(getOnException(), refresher, getOperation());
199                 throw e;
200             }
201         });
202     }
203 }