|
1 | | -/* |
2 | | - * OpenFGA |
3 | | - * A high performance and flexible authorization/permission engine built for developers and inspired by Google Zanzibar. |
4 | | - * |
5 | | - * The version of the OpenAPI document: 1.x |
6 | | - * Contact: community@openfga.dev |
7 | | - * |
8 | | - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). |
9 | | - * https://openapi-generator.tech |
10 | | - * Do not edit the class manually. |
11 | | - */ |
12 | | - |
13 | | -package dev.openfga.sdk.util; |
14 | | - |
15 | | -import static org.assertj.core.api.Assertions.assertThat; |
16 | | -import static org.mockito.Mockito.mock; |
17 | | -import static org.mockito.Mockito.when; |
18 | | - |
19 | | -import dev.openfga.sdk.errors.HttpStatusCode; |
20 | | -import java.net.URI; |
21 | | -import java.net.http.HttpRequest; |
22 | | -import java.time.Duration; |
23 | | -import java.util.Optional; |
24 | | -import org.junit.jupiter.api.Test; |
25 | | - |
26 | | -class RetryStrategyTest { |
27 | | - |
28 | | - @Test |
29 | | - void shouldRetryGetRequestOn429() { |
30 | | - HttpRequest request = HttpRequest.newBuilder() |
31 | | - .uri(URI.create("https://example.com/test")) |
32 | | - .GET() |
33 | | - .build(); |
34 | | - |
35 | | - boolean result = RetryStrategy.shouldRetry(request, 429, false); |
36 | | - assertThat(result).isTrue(); |
37 | | - } |
38 | | - |
39 | | - @Test |
40 | | - void shouldRetryPostRequestOn429() { |
41 | | - HttpRequest request = HttpRequest.newBuilder() |
42 | | - .uri(URI.create("https://example.com/test")) |
43 | | - .POST(HttpRequest.BodyPublishers.ofString("{}")) |
44 | | - .build(); |
45 | | - |
46 | | - boolean result = RetryStrategy.shouldRetry(request, 429, false); |
47 | | - assertThat(result).isTrue(); |
48 | | - } |
49 | | - |
50 | | - @Test |
51 | | - void shouldRetryGetRequestOn500() { |
52 | | - HttpRequest request = HttpRequest.newBuilder() |
53 | | - .uri(URI.create("https://example.com/test")) |
54 | | - .GET() |
55 | | - .build(); |
56 | | - |
57 | | - boolean result = RetryStrategy.shouldRetry(request, 500, false); |
58 | | - assertThat(result).isTrue(); |
59 | | - } |
60 | | - |
61 | | - @Test |
62 | | - void shouldRetryPostRequestOn500() { |
63 | | - HttpRequest request = HttpRequest.newBuilder() |
64 | | - .uri(URI.create("https://example.com/test")) |
65 | | - .POST(HttpRequest.BodyPublishers.ofString("{}")) |
66 | | - .build(); |
67 | | - |
68 | | - // Simplified logic: POST requests should retry on 5xx errors |
69 | | - boolean result = RetryStrategy.shouldRetry(request, 500, false); |
70 | | - assertThat(result).isTrue(); |
71 | | - } |
72 | | - |
73 | | - @Test |
74 | | - void shouldRetryPostRequestOn500WithRetryAfter() { |
75 | | - HttpRequest request = HttpRequest.newBuilder() |
76 | | - .uri(URI.create("https://example.com/test")) |
77 | | - .POST(HttpRequest.BodyPublishers.ofString("{}")) |
78 | | - .build(); |
79 | | - |
80 | | - boolean result = RetryStrategy.shouldRetry(request, 500, true); |
81 | | - assertThat(result).isTrue(); |
82 | | - } |
83 | | - |
84 | | - @Test |
85 | | - void shouldRetryPutRequestOn502() { |
86 | | - HttpRequest request = HttpRequest.newBuilder() |
87 | | - .uri(URI.create("https://example.com/test")) |
88 | | - .PUT(HttpRequest.BodyPublishers.ofString("{}")) |
89 | | - .build(); |
90 | | - |
91 | | - // Simplified logic: PUT requests should retry on 5xx errors |
92 | | - boolean result = RetryStrategy.shouldRetry(request, 502, false); |
93 | | - assertThat(result).isTrue(); |
94 | | - } |
95 | | - |
96 | | - @Test |
97 | | - void shouldRetryPatchRequestOn503() { |
98 | | - HttpRequest request = HttpRequest.newBuilder() |
99 | | - .uri(URI.create("https://example.com/test")) |
100 | | - .method("PATCH", HttpRequest.BodyPublishers.ofString("{}")) |
101 | | - .build(); |
102 | | - |
103 | | - // Simplified logic: PATCH requests should retry on 5xx errors |
104 | | - boolean result = RetryStrategy.shouldRetry(request, 503, false); |
105 | | - assertThat(result).isTrue(); |
106 | | - } |
107 | | - |
108 | | - @Test |
109 | | - void shouldRetryDeleteRequestOn504() { |
110 | | - HttpRequest request = HttpRequest.newBuilder() |
111 | | - .uri(URI.create("https://example.com/test")) |
112 | | - .DELETE() |
113 | | - .build(); |
114 | | - |
115 | | - // Simplified logic: DELETE requests should retry on 5xx errors |
116 | | - boolean result = RetryStrategy.shouldRetry(request, 504, false); |
117 | | - assertThat(result).isTrue(); |
118 | | - } |
119 | | - |
120 | | - @Test |
121 | | - void shouldRetry_with501Status_shouldReturnFalseRegardlessOfMethod() { |
122 | | - // Given |
123 | | - HttpRequest getRequest = HttpRequest.newBuilder() |
124 | | - .uri(URI.create("https://example.com/test")) |
125 | | - .GET() |
126 | | - .build(); |
127 | | - HttpRequest postRequest = HttpRequest.newBuilder() |
128 | | - .uri(URI.create("https://example.com/test")) |
129 | | - .POST(HttpRequest.BodyPublishers.ofString("{}")) |
130 | | - .build(); |
131 | | - int statusCode = HttpStatusCode.NOT_IMPLEMENTED; |
132 | | - boolean hasRetryAfterHeader = true; |
133 | | - |
134 | | - // When & Then |
135 | | - assertThat(RetryStrategy.shouldRetry(getRequest, statusCode, hasRetryAfterHeader)) |
136 | | - .isFalse(); |
137 | | - assertThat(RetryStrategy.shouldRetry(postRequest, statusCode, hasRetryAfterHeader)) |
138 | | - .isFalse(); |
139 | | - } |
140 | | - |
141 | | - @Test |
142 | | - void shouldNotRetryOn400() { |
143 | | - HttpRequest request = HttpRequest.newBuilder() |
144 | | - .uri(URI.create("https://example.com/test")) |
145 | | - .GET() |
146 | | - .build(); |
147 | | - |
148 | | - boolean result = RetryStrategy.shouldRetry(request, 400, false); |
149 | | - assertThat(result).isFalse(); |
150 | | - } |
151 | | - |
152 | | - @Test |
153 | | - void shouldRetry_withNonStateAffectingMethods_shouldAlwaysRetryOn5xx() { |
154 | | - // Given |
155 | | - String[] nonStateAffectingMethods = {"GET", "HEAD", "OPTIONS"}; |
156 | | - int serverErrorStatus = 502; |
157 | | - |
158 | | - for (String method : nonStateAffectingMethods) { |
159 | | - HttpRequest request = createMockRequest(method); |
160 | | - |
161 | | - // Without Retry-After header |
162 | | - assertThat(RetryStrategy.shouldRetry(request, serverErrorStatus, false)) |
163 | | - .describedAs("Method %s without Retry-After should still retry on 5xx", method) |
164 | | - .isTrue(); |
165 | | - |
166 | | - // With Retry-After header |
167 | | - assertThat(RetryStrategy.shouldRetry(request, serverErrorStatus, true)) |
168 | | - .describedAs("Method %s with Retry-After should retry on 5xx", method) |
169 | | - .isTrue(); |
170 | | - } |
171 | | - } |
172 | | - |
173 | | - @Test |
174 | | - void shouldRetry_with4xxStatusExcept429_shouldReturnFalse() { |
175 | | - // Given |
176 | | - HttpRequest request = createMockRequest("GET"); |
177 | | - int[] clientErrorStatuses = {400, 401, 403, 404, 422}; |
178 | | - |
179 | | - for (int statusCode : clientErrorStatuses) { |
180 | | - // When & Then |
181 | | - assertThat(RetryStrategy.shouldRetry(request, statusCode, false)) |
182 | | - .describedAs("Status %d should not be retryable", statusCode) |
183 | | - .isFalse(); |
184 | | - assertThat(RetryStrategy.shouldRetry(request, statusCode, true)) |
185 | | - .describedAs("Status %d should not be retryable even with Retry-After", statusCode) |
186 | | - .isFalse(); |
187 | | - } |
188 | | - } |
189 | | - |
190 | | - @Test |
191 | | - void shouldRetry_with2xxStatus_shouldReturnFalse() { |
192 | | - // Given |
193 | | - HttpRequest request = createMockRequest("GET"); |
194 | | - int[] successStatuses = {200, 201, 204}; |
195 | | - |
196 | | - for (int statusCode : successStatuses) { |
197 | | - // When & Then |
198 | | - assertThat(RetryStrategy.shouldRetry(request, statusCode, false)) |
199 | | - .describedAs("Status %d should not be retryable", statusCode) |
200 | | - .isFalse(); |
201 | | - } |
202 | | - } |
203 | | - |
204 | | - @Test |
205 | | - void calculateRetryDelay_withRetryAfterHeader_shouldUseHeaderValue() { |
206 | | - // Given |
207 | | - Optional<Duration> retryAfterDelay = Optional.of(Duration.ofSeconds(30)); |
208 | | - int retryCount = 2; |
209 | | - |
210 | | - // When |
211 | | - Duration result = RetryStrategy.calculateRetryDelay(retryAfterDelay, retryCount); |
212 | | - |
213 | | - // Then |
214 | | - assertThat(result).isEqualTo(Duration.ofSeconds(30)); |
215 | | - } |
216 | | - |
217 | | - @Test |
218 | | - void calculateRetryDelay_withoutRetryAfterHeader_shouldUseExponentialBackoff() { |
219 | | - // Given |
220 | | - Optional<Duration> retryAfterDelay = Optional.empty(); |
221 | | - int retryCount = 1; |
222 | | - |
223 | | - // When |
224 | | - Duration result = RetryStrategy.calculateRetryDelay(retryAfterDelay, retryCount); |
225 | | - |
226 | | - // Then |
227 | | - // For retry count 1: 2^1 * 100ms = 200ms base, with jitter between 200ms and 400ms |
228 | | - assertThat(result.toMillis()).isBetween(200L, 400L); |
229 | | - } |
230 | | - |
231 | | - @Test |
232 | | - void calculateRetryDelay_withEmptyRetryAfterHeader_shouldUseExponentialBackoff() { |
233 | | - // Given |
234 | | - Optional<Duration> retryAfterDelay = Optional.empty(); |
235 | | - int retryCount = 0; |
236 | | - |
237 | | - // When |
238 | | - Duration result = RetryStrategy.calculateRetryDelay(retryAfterDelay, retryCount); |
239 | | - |
240 | | - // Then |
241 | | - // For retry count 0: 2^0 * 100ms = 100ms base, with jitter between 100ms and 200ms |
242 | | - assertThat(result.toMillis()).isBetween(100L, 200L); |
243 | | - } |
244 | | - |
245 | | - @Test |
246 | | - void shouldRetry_withCaseInsensitiveMethods_shouldWorkCorrectly() { |
247 | | - // Given - method case doesn't matter in simplified logic, but test for consistency |
248 | | - HttpRequest lowerCaseRequest = createMockRequest("post"); |
249 | | - HttpRequest upperCaseRequest = createMockRequest("POST"); |
250 | | - HttpRequest mixedCaseRequest = createMockRequest("Post"); |
251 | | - int statusCode = 500; |
252 | | - |
253 | | - // When & Then - all should retry on 5xx regardless of method case or Retry-After header |
254 | | - assertThat(RetryStrategy.shouldRetry(lowerCaseRequest, statusCode, false)) |
255 | | - .isTrue(); |
256 | | - assertThat(RetryStrategy.shouldRetry(upperCaseRequest, statusCode, false)) |
257 | | - .isTrue(); |
258 | | - assertThat(RetryStrategy.shouldRetry(mixedCaseRequest, statusCode, true)) |
259 | | - .isTrue(); |
260 | | - } |
261 | | - |
262 | | - private HttpRequest createMockRequest(String method) { |
263 | | - HttpRequest request = mock(HttpRequest.class); |
264 | | - when(request.method()).thenReturn(method); |
265 | | - when(request.uri()).thenReturn(URI.create("https://api.example.com/test")); |
266 | | - return request; |
267 | | - } |
268 | | -} |
0 commit comments