6
6
7
7
import static java .net .HttpURLConnection .HTTP_BAD_REQUEST ;
8
8
import static java .net .HttpURLConnection .HTTP_CONFLICT ;
9
- import static java .net .HttpURLConnection .HTTP_INTERNAL_ERROR ;
10
9
import static org .hamcrest .Matchers .equalTo ;
11
10
import static org .hamcrest .Matchers .instanceOf ;
12
11
import static org .hamcrest .junit .MatcherAssert .assertThat ;
13
- import static org .junit .Assert .fail ;
14
12
15
13
import com .google .gson .GsonBuilder ;
16
14
import com .meterware .pseudoserver .HttpUserAgentTest ;
29
27
import io .kubernetes .client .models .V1Status ;
30
28
import io .kubernetes .client .models .VersionInfo ;
31
29
import java .io .IOException ;
32
- import java .lang .reflect .Field ;
33
30
import java .util .ArrayList ;
34
31
import java .util .Arrays ;
35
32
import java .util .List ;
@@ -70,7 +67,7 @@ public void setUp() throws NoSuchFieldException {
70
67
}
71
68
72
69
@ After
73
- public void tearDown () throws Exception {
70
+ public void tearDown () {
74
71
for (Memento memento : mementos ) memento .revert ();
75
72
}
76
73
@@ -117,133 +114,6 @@ public void replaceDomain_conflictResponseCode_throws() throws ApiException {
117
114
callBuilder .replaceDomain (UID , NAMESPACE , domain );
118
115
}
119
116
120
- @ Test
121
- public void replaceDomainWithRetry_sendsNewDomain () throws ApiException {
122
- Domain domain = new Domain ().withMetadata (createMetadata ());
123
- defineHttpPutResponse (
124
- DOMAIN_RESOURCE , UID , domain , (json ) -> requestBody = fromJson (json , Domain .class ));
125
-
126
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
127
-
128
- assertThat (requestBody , equalTo (domain ));
129
- }
130
-
131
- @ Test
132
- public void replaceDomainWithRetry_withMaxRetryCountOfZero_sendsNewDomain ()
133
- throws ApiException , NoSuchFieldException , IllegalAccessException {
134
- Domain domain = new Domain ().withMetadata (createMetadata ());
135
- defineHttpPutResponse (
136
- DOMAIN_RESOURCE , UID , domain , (json ) -> requestBody = fromJson (json , Domain .class ));
137
-
138
- setMaxRetryCount (callBuilder , 0 );
139
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
140
-
141
- assertThat (requestBody , equalTo (domain ));
142
- }
143
-
144
- @ Test
145
- public void replaceDomainWithRetry_sendsNewDomain_afterRetry () throws ApiException {
146
- Domain domain = new Domain ().withMetadata (createMetadata ());
147
- ConflictOncePutServlet conflictOncePutServlet =
148
- new ConflictOncePutServlet (domain , (json ) -> requestBody = fromJson (json , Domain .class ));
149
- defineResource (DOMAIN_RESOURCE + "/" + UID , conflictOncePutServlet );
150
-
151
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
152
-
153
- assertThat (requestBody , equalTo (domain ));
154
- assertThat (conflictOncePutServlet .conflictReturned , equalTo (true ));
155
- }
156
-
157
- @ Test (expected = ApiException .class )
158
- public void replaceDomainWithConflictRetry_conflictResponseCode_throws () throws ApiException {
159
- Domain domain = new Domain ().withMetadata (createMetadata ());
160
- defineHttpPutResponse (DOMAIN_RESOURCE , UID , domain , new ErrorCodePutServlet (HTTP_CONFLICT ));
161
-
162
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
163
- }
164
-
165
- @ Test (expected = ApiException .class )
166
- public void replaceDomainWithConflictRetry_errorResponseCode_throws () throws ApiException {
167
- Domain domain = new Domain ().withMetadata (createMetadata ());
168
- defineHttpPutResponse (DOMAIN_RESOURCE , UID , domain , new ErrorCodePutServlet (HTTP_BAD_REQUEST ));
169
-
170
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
171
- }
172
-
173
- @ Test
174
- public void replaceDomainWithConflictRetry_conflictResponseCode_retriedMaxTimes ()
175
- throws ApiException , NoSuchFieldException , IllegalAccessException {
176
- final int MAX_RETRY_COUNT = 5 ;
177
- setMaxRetryCount (callBuilder , MAX_RETRY_COUNT );
178
- Domain domain = new Domain ().withMetadata (createMetadata ());
179
- ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet (HTTP_CONFLICT );
180
- defineHttpPutResponse (DOMAIN_RESOURCE , UID , domain , conflictPutServlet );
181
- try {
182
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
183
- fail ("Expected ApiException not thrown" );
184
- } catch (ApiException apiException ) {
185
-
186
- }
187
- assertThat (conflictPutServlet .numGetPutResponseCalled , equalTo (MAX_RETRY_COUNT ));
188
- }
189
-
190
- @ Test
191
- public void replaceDomainWithConflictRetry_otherResponseCode_noRetries ()
192
- throws ApiException , NoSuchFieldException , IllegalAccessException {
193
- Domain domain = new Domain ().withMetadata (createMetadata ());
194
- ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet (HTTP_INTERNAL_ERROR );
195
- defineHttpPutResponse (DOMAIN_RESOURCE , UID , domain , conflictPutServlet );
196
- try {
197
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
198
- fail ("Expected ApiException not thrown" );
199
- } catch (ApiException apiException ) {
200
-
201
- }
202
- assertThat (conflictPutServlet .numGetPutResponseCalled , equalTo (1 ));
203
- }
204
-
205
- @ Test
206
- public void replaceDomainWithConflictRetry_withMaxRetryCountOfZero_noRetries ()
207
- throws ApiException , NoSuchFieldException , IllegalAccessException {
208
- Domain domain = new Domain ().withMetadata (createMetadata ());
209
- ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet (HTTP_CONFLICT );
210
- defineHttpPutResponse (DOMAIN_RESOURCE , UID , domain , conflictPutServlet );
211
- setMaxRetryCount (callBuilder , 0 );
212
- try {
213
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> domain );
214
- fail ("Expected ApiException not thrown" );
215
- } catch (ApiException apiException ) {
216
- assertThat (apiException .getCode (), equalTo (HTTP_CONFLICT ));
217
- }
218
- assertThat (conflictPutServlet .numGetPutResponseCalled , equalTo (1 ));
219
- }
220
-
221
- @ Test
222
- public void replaceDomainWithConflictRetry_nullUpdatedObject_noRetries ()
223
- throws ApiException , NoSuchFieldException , IllegalAccessException {
224
- Domain domain = new Domain ().withMetadata (createMetadata ());
225
- ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet (HTTP_CONFLICT );
226
- defineHttpPutResponse (DOMAIN_RESOURCE , UID , domain , conflictPutServlet );
227
- try {
228
- callBuilder .replaceDomainWithConflictRetry (UID , NAMESPACE , domain , () -> null );
229
- fail ("Expected ApiException not thrown" );
230
- } catch (ApiException apiException ) {
231
- assertThat (apiException .getCode (), equalTo (HTTP_CONFLICT ));
232
- }
233
- assertThat (conflictPutServlet .numGetPutResponseCalled , equalTo (1 ));
234
- }
235
-
236
- Field callBuilderMaxRetryCount ;
237
-
238
- private void setMaxRetryCount (CallBuilder callBuilder , int maxRetryCount )
239
- throws IllegalAccessException , NoSuchFieldException {
240
- if (callBuilderMaxRetryCount == null ) {
241
- callBuilderMaxRetryCount = CallBuilder .class .getDeclaredField ("maxRetryCount" );
242
- callBuilderMaxRetryCount .setAccessible (true );
243
- }
244
- callBuilderMaxRetryCount .set (callBuilder , maxRetryCount );
245
- }
246
-
247
117
@ Test
248
118
public void createPV_returnsVolumeAsJson () throws ApiException {
249
119
V1PersistentVolume volume = createPersistentVolume ();
@@ -341,6 +211,7 @@ private void defineHttpPutResponse(
341
211
defineResource (resourceName + "/" + name , new JsonPutServlet (response , bodyValidation ));
342
212
}
343
213
214
+ @ SuppressWarnings ("unused" )
344
215
private void defineHttpPutResponse (
345
216
String resourceName , String name , Object response , PseudoServlet pseudoServlet ) {
346
217
defineResource (resourceName + "/" + name , pseudoServlet );
@@ -395,7 +266,7 @@ static class ErrorCodePutServlet extends PseudoServlet {
395
266
int numGetPutResponseCalled = 0 ;
396
267
final int errorCode ;
397
268
398
- public ErrorCodePutServlet (int errorCode ) {
269
+ ErrorCodePutServlet (int errorCode ) {
399
270
this .errorCode = errorCode ;
400
271
}
401
272
@@ -406,24 +277,6 @@ public WebResource getPutResponse() {
406
277
}
407
278
}
408
279
409
- static class ConflictOncePutServlet extends JsonBodyServlet {
410
-
411
- boolean conflictReturned ;
412
-
413
- private ConflictOncePutServlet (Object returnValue , Consumer <String > bodyValidation ) {
414
- super (returnValue , bodyValidation );
415
- }
416
-
417
- @ Override
418
- public WebResource getPutResponse () throws IOException {
419
- if (!conflictReturned ) {
420
- conflictReturned = true ;
421
- return new WebResource ("" , HTTP_CONFLICT );
422
- }
423
- return getResponse ();
424
- }
425
- }
426
-
427
280
abstract static class JsonServlet extends PseudoServlet {
428
281
429
282
private WebResource response ;
@@ -448,6 +301,7 @@ private void validateParameters() throws IOException {
448
301
if (!validationErrors .isEmpty ()) throw new IOException (String .join ("\n " , validationErrors ));
449
302
}
450
303
304
+ @ SuppressWarnings ("UnusedReturnValue" )
451
305
JsonServlet expectingParameter (String name , String value ) {
452
306
parameterExpectations .add (new ParameterExpectation (name , value ));
453
307
return this ;
@@ -516,10 +370,6 @@ public WebResource getPostResponse() throws IOException {
516
370
517
371
static class JsonPutServlet extends JsonBodyServlet {
518
372
519
- private JsonPutServlet (Object returnValue ) {
520
- this (returnValue , null );
521
- }
522
-
523
373
private JsonPutServlet (Object returnValue , Consumer <String > bodyValidation ) {
524
374
super (returnValue , bodyValidation );
525
375
}
0 commit comments