5
5
6
6
import com .nimbusds .oauth2 .sdk .util .URLUtils ;
7
7
import org .apache .http .HttpStatus ;
8
+ import org .junit .jupiter .api .BeforeAll ;
9
+ import org .junit .jupiter .api .BeforeEach ;
8
10
import org .junit .jupiter .api .Test ;
9
11
import org .junit .jupiter .api .TestInstance ;
10
12
import org .junit .jupiter .api .extension .ExtendWith ;
@@ -34,6 +36,7 @@ class ManagedIdentityTests {
34
36
final static String azureArcEndpoint = "http://localhost:40342/metadata/identity/oauth2/token" ;
35
37
final static String cloudShellEndpoint = "http://localhost:40342/metadata/identity/oauth2/token" ;
36
38
final static String serviceFabricEndpoint = "http://localhost:40342/metadata/identity/oauth2/token" ;
39
+ private static ManagedIdentityApplication miApp ;
37
40
38
41
private String getSuccessfulResponse (String resource ) {
39
42
long expiresOn = Instant .now ().plus (1 , ChronoUnit .HOURS ).getEpochSecond ();
@@ -131,11 +134,14 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
131
134
132
135
when (httpClientMock .send (eq (expectedRequest (source , resource )))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
133
136
134
- ManagedIdentityApplication miApp = ManagedIdentityApplication
137
+ miApp = ManagedIdentityApplication
135
138
.builder (ManagedIdentityId .systemAssigned ())
136
139
.httpClient (httpClientMock )
137
140
.build ();
138
141
142
+ // Clear caching to avoid cross test pollution.
143
+ miApp .tokenCache ().accessTokens .clear ();
144
+
139
145
IAuthenticationResult result = miApp .acquireTokenForManagedIdentity (
140
146
ManagedIdentityParameters .builder (resource )
141
147
.environmentVariables (environmentVariables )
@@ -163,11 +169,14 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
163
169
164
170
when (httpClientMock .send (eq (expectedRequest (source , resource , id )))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
165
171
166
- ManagedIdentityApplication miApp = ManagedIdentityApplication
172
+ miApp = ManagedIdentityApplication
167
173
.builder (id )
168
174
.httpClient (httpClientMock )
169
175
.build ();
170
176
177
+ // Clear caching to avoid cross test pollution.
178
+ miApp .tokenCache ().accessTokens .clear ();
179
+
171
180
IAuthenticationResult result = miApp .acquireTokenForManagedIdentity (
172
181
ManagedIdentityParameters .builder (resource )
173
182
.environmentVariables (environmentVariables )
@@ -183,11 +192,14 @@ void managedIdentityTest_UserAssigned_NotSupported(ManagedIdentitySourceType sou
183
192
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
184
193
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
185
194
186
- ManagedIdentityApplication miApp = ManagedIdentityApplication
195
+ miApp = ManagedIdentityApplication
187
196
.builder (id )
188
197
.httpClient (httpClientMock )
189
198
.build ();
190
199
200
+ // Clear caching to avoid cross test pollution.
201
+ miApp .tokenCache ().accessTokens .clear ();
202
+
191
203
try {
192
204
IAuthenticationResult result = miApp .acquireTokenForManagedIdentity (
193
205
ManagedIdentityParameters .builder (resource )
@@ -219,11 +231,14 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
219
231
when (httpClientMock .send (eq (expectedRequest (source , resource )))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
220
232
when (httpClientMock .send (eq (expectedRequest (source , anotherResource )))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
221
233
222
- ManagedIdentityApplication miApp = ManagedIdentityApplication
234
+ miApp = ManagedIdentityApplication
223
235
.builder (ManagedIdentityId .systemAssigned ())
224
236
.httpClient (httpClientMock )
225
237
.build ();
226
238
239
+ // Clear caching to avoid cross test pollution.
240
+ miApp .tokenCache ().accessTokens .clear ();
241
+
227
242
IAuthenticationResult result = miApp .acquireTokenForManagedIdentity (
228
243
ManagedIdentityParameters .builder (resource )
229
244
.environmentVariables (environmentVariables )
@@ -249,11 +264,14 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
249
264
250
265
when (httpClientMock .send (eq (expectedRequest (source , resource )))).thenReturn (expectedResponse (500 , getMsiErrorResponse ()));
251
266
252
- ManagedIdentityApplication miApp = ManagedIdentityApplication
267
+ miApp = ManagedIdentityApplication
253
268
.builder (ManagedIdentityId .systemAssigned ())
254
269
.httpClient (httpClientMock )
255
270
.build ();
256
271
272
+ // Clear caching to avoid cross test pollution.
273
+ miApp .tokenCache ().accessTokens .clear ();
274
+
257
275
try {
258
276
miApp .acquireTokenForManagedIdentity (
259
277
ManagedIdentityParameters .builder (resource )
@@ -280,11 +298,14 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
280
298
281
299
when (httpClientMock .send (eq (expectedRequest (source , resource )))).thenReturn (expectedResponse (500 , "" ));
282
300
283
- ManagedIdentityApplication miApp = ManagedIdentityApplication
301
+ miApp = ManagedIdentityApplication
284
302
.builder (ManagedIdentityId .systemAssigned ())
285
303
.httpClient (httpClientMock )
286
304
.build ();
287
305
306
+ // Clear caching to avoid cross test pollution.
307
+ miApp .tokenCache ().accessTokens .clear ();
308
+
288
309
try {
289
310
miApp .acquireTokenForManagedIdentity (
290
311
ManagedIdentityParameters .builder (resource )
@@ -311,11 +332,14 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
311
332
312
333
when (httpClientMock .send (eq (expectedRequest (source , resource )))).thenReturn (expectedResponse (200 , "" ));
313
334
314
- ManagedIdentityApplication miApp = ManagedIdentityApplication
335
+ miApp = ManagedIdentityApplication
315
336
.builder (ManagedIdentityId .systemAssigned ())
316
337
.httpClient (httpClientMock )
317
338
.build ();
318
339
340
+ // Clear caching to avoid cross test pollution.
341
+ miApp .tokenCache ().accessTokens .clear ();
342
+
319
343
try {
320
344
miApp .acquireTokenForManagedIdentity (
321
345
ManagedIdentityParameters .builder (resource )
@@ -342,11 +366,14 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
342
366
343
367
when (httpClientMock .send (eq (expectedRequest (source , resource )))).thenThrow (new SocketException ("A socket operation was attempted to an unreachable network." ));
344
368
345
- ManagedIdentityApplication miApp = ManagedIdentityApplication
369
+ miApp = ManagedIdentityApplication
346
370
.builder (ManagedIdentityId .systemAssigned ())
347
371
.httpClient (httpClientMock )
348
372
.build ();
349
373
374
+ // Clear caching to avoid cross test pollution.
375
+ miApp .tokenCache ().accessTokens .clear ();
376
+
350
377
try {
351
378
miApp .acquireTokenForManagedIdentity (
352
379
ManagedIdentityParameters .builder (resource )
@@ -375,10 +402,13 @@ void azureArcManagedIdentity_MissingAuthHeader() throws Exception {
375
402
376
403
when (httpClientMock .send (any ())).thenReturn (response );
377
404
378
- ManagedIdentityApplication miApp = ManagedIdentityApplication
405
+ miApp = ManagedIdentityApplication
379
406
.builder (ManagedIdentityId .systemAssigned ())
380
407
.httpClient (httpClientMock )
381
408
.build ();
409
+
410
+ // Clear caching to avoid cross test pollution.
411
+ miApp .tokenCache ().accessTokens .clear ();
382
412
383
413
try {
384
414
miApp .acquireTokenForManagedIdentity (
@@ -407,17 +437,20 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
407
437
408
438
when (httpClientMock .send (eq (expectedRequest (source , resource )))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
409
439
410
- ManagedIdentityApplication miApp1 = ManagedIdentityApplication
440
+ miApp = ManagedIdentityApplication
411
441
.builder (ManagedIdentityId .systemAssigned ())
412
442
.httpClient (httpClientMock )
413
443
.build ();
444
+
445
+ // Clear caching to avoid cross test pollution.
446
+ miApp .tokenCache ().accessTokens .clear ();
414
447
415
448
ManagedIdentityApplication miApp2 = ManagedIdentityApplication
416
449
.builder (ManagedIdentityId .systemAssigned ())
417
450
.httpClient (httpClientMock )
418
451
.build ();
419
452
420
- IAuthenticationResult resultMiApp1 = miApp1 .acquireTokenForManagedIdentity (
453
+ IAuthenticationResult resultMiApp1 = miApp .acquireTokenForManagedIdentity (
421
454
ManagedIdentityParameters .builder (resource )
422
455
.environmentVariables (environmentVariables )
423
456
.build ()).get ();
@@ -449,11 +482,14 @@ void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
449
482
450
483
when (httpClientMock .send (any ())).thenReturn (response );
451
484
452
- ManagedIdentityApplication miApp = ManagedIdentityApplication
485
+ miApp = ManagedIdentityApplication
453
486
.builder (ManagedIdentityId .systemAssigned ())
454
487
.httpClient (httpClientMock )
455
488
.build ();
456
489
490
+ // Clear caching to avoid cross test pollution.
491
+ miApp .tokenCache ().accessTokens .clear ();
492
+
457
493
try {
458
494
miApp .acquireTokenForManagedIdentity (
459
495
ManagedIdentityParameters .builder (resource )
0 commit comments