Skip to content

Commit 36a007e

Browse files
committed
Clear cache in unit tests
1 parent b81c4f8 commit 36a007e

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/ManagedIdentityTests.java

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import com.nimbusds.oauth2.sdk.util.URLUtils;
77
import org.apache.http.HttpStatus;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.BeforeEach;
810
import org.junit.jupiter.api.Test;
911
import org.junit.jupiter.api.TestInstance;
1012
import org.junit.jupiter.api.extension.ExtendWith;
@@ -34,6 +36,7 @@ class ManagedIdentityTests {
3436
final static String azureArcEndpoint = "http://localhost:40342/metadata/identity/oauth2/token";
3537
final static String cloudShellEndpoint = "http://localhost:40342/metadata/identity/oauth2/token";
3638
final static String serviceFabricEndpoint = "http://localhost:40342/metadata/identity/oauth2/token";
39+
private static ManagedIdentityApplication miApp;
3740

3841
private String getSuccessfulResponse(String resource) {
3942
long expiresOn = Instant.now().plus(1, ChronoUnit.HOURS).getEpochSecond();
@@ -131,11 +134,14 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
131134

132135
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
133136

134-
ManagedIdentityApplication miApp = ManagedIdentityApplication
137+
miApp = ManagedIdentityApplication
135138
.builder(ManagedIdentityId.systemAssigned())
136139
.httpClient(httpClientMock)
137140
.build();
138141

142+
// Clear caching to avoid cross test pollution.
143+
miApp.tokenCache().accessTokens.clear();
144+
139145
IAuthenticationResult result = miApp.acquireTokenForManagedIdentity(
140146
ManagedIdentityParameters.builder(resource)
141147
.environmentVariables(environmentVariables)
@@ -163,11 +169,14 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
163169

164170
when(httpClientMock.send(eq(expectedRequest(source, resource, id)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
165171

166-
ManagedIdentityApplication miApp = ManagedIdentityApplication
172+
miApp = ManagedIdentityApplication
167173
.builder(id)
168174
.httpClient(httpClientMock)
169175
.build();
170176

177+
// Clear caching to avoid cross test pollution.
178+
miApp.tokenCache().accessTokens.clear();
179+
171180
IAuthenticationResult result = miApp.acquireTokenForManagedIdentity(
172181
ManagedIdentityParameters.builder(resource)
173182
.environmentVariables(environmentVariables)
@@ -183,11 +192,14 @@ void managedIdentityTest_UserAssigned_NotSupported(ManagedIdentitySourceType sou
183192
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
184193
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
185194

186-
ManagedIdentityApplication miApp = ManagedIdentityApplication
195+
miApp = ManagedIdentityApplication
187196
.builder(id)
188197
.httpClient(httpClientMock)
189198
.build();
190199

200+
// Clear caching to avoid cross test pollution.
201+
miApp.tokenCache().accessTokens.clear();
202+
191203
try {
192204
IAuthenticationResult result = miApp.acquireTokenForManagedIdentity(
193205
ManagedIdentityParameters.builder(resource)
@@ -219,11 +231,14 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
219231
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
220232
when(httpClientMock.send(eq(expectedRequest(source, anotherResource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
221233

222-
ManagedIdentityApplication miApp = ManagedIdentityApplication
234+
miApp = ManagedIdentityApplication
223235
.builder(ManagedIdentityId.systemAssigned())
224236
.httpClient(httpClientMock)
225237
.build();
226238

239+
// Clear caching to avoid cross test pollution.
240+
miApp.tokenCache().accessTokens.clear();
241+
227242
IAuthenticationResult result = miApp.acquireTokenForManagedIdentity(
228243
ManagedIdentityParameters.builder(resource)
229244
.environmentVariables(environmentVariables)
@@ -249,11 +264,14 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
249264

250265
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, getMsiErrorResponse()));
251266

252-
ManagedIdentityApplication miApp = ManagedIdentityApplication
267+
miApp = ManagedIdentityApplication
253268
.builder(ManagedIdentityId.systemAssigned())
254269
.httpClient(httpClientMock)
255270
.build();
256271

272+
// Clear caching to avoid cross test pollution.
273+
miApp.tokenCache().accessTokens.clear();
274+
257275
try {
258276
miApp.acquireTokenForManagedIdentity(
259277
ManagedIdentityParameters.builder(resource)
@@ -280,11 +298,14 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
280298

281299
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, ""));
282300

283-
ManagedIdentityApplication miApp = ManagedIdentityApplication
301+
miApp = ManagedIdentityApplication
284302
.builder(ManagedIdentityId.systemAssigned())
285303
.httpClient(httpClientMock)
286304
.build();
287305

306+
// Clear caching to avoid cross test pollution.
307+
miApp.tokenCache().accessTokens.clear();
308+
288309
try {
289310
miApp.acquireTokenForManagedIdentity(
290311
ManagedIdentityParameters.builder(resource)
@@ -311,11 +332,14 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
311332

312333
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, ""));
313334

314-
ManagedIdentityApplication miApp = ManagedIdentityApplication
335+
miApp = ManagedIdentityApplication
315336
.builder(ManagedIdentityId.systemAssigned())
316337
.httpClient(httpClientMock)
317338
.build();
318339

340+
// Clear caching to avoid cross test pollution.
341+
miApp.tokenCache().accessTokens.clear();
342+
319343
try {
320344
miApp.acquireTokenForManagedIdentity(
321345
ManagedIdentityParameters.builder(resource)
@@ -342,11 +366,14 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
342366

343367
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenThrow(new SocketException("A socket operation was attempted to an unreachable network."));
344368

345-
ManagedIdentityApplication miApp = ManagedIdentityApplication
369+
miApp = ManagedIdentityApplication
346370
.builder(ManagedIdentityId.systemAssigned())
347371
.httpClient(httpClientMock)
348372
.build();
349373

374+
// Clear caching to avoid cross test pollution.
375+
miApp.tokenCache().accessTokens.clear();
376+
350377
try {
351378
miApp.acquireTokenForManagedIdentity(
352379
ManagedIdentityParameters.builder(resource)
@@ -375,10 +402,13 @@ void azureArcManagedIdentity_MissingAuthHeader() throws Exception {
375402

376403
when(httpClientMock.send(any())).thenReturn(response);
377404

378-
ManagedIdentityApplication miApp = ManagedIdentityApplication
405+
miApp = ManagedIdentityApplication
379406
.builder(ManagedIdentityId.systemAssigned())
380407
.httpClient(httpClientMock)
381408
.build();
409+
410+
// Clear caching to avoid cross test pollution.
411+
miApp.tokenCache().accessTokens.clear();
382412

383413
try {
384414
miApp.acquireTokenForManagedIdentity(
@@ -407,17 +437,20 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
407437

408438
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
409439

410-
ManagedIdentityApplication miApp1 = ManagedIdentityApplication
440+
miApp = ManagedIdentityApplication
411441
.builder(ManagedIdentityId.systemAssigned())
412442
.httpClient(httpClientMock)
413443
.build();
444+
445+
// Clear caching to avoid cross test pollution.
446+
miApp.tokenCache().accessTokens.clear();
414447

415448
ManagedIdentityApplication miApp2 = ManagedIdentityApplication
416449
.builder(ManagedIdentityId.systemAssigned())
417450
.httpClient(httpClientMock)
418451
.build();
419452

420-
IAuthenticationResult resultMiApp1 = miApp1.acquireTokenForManagedIdentity(
453+
IAuthenticationResult resultMiApp1 = miApp.acquireTokenForManagedIdentity(
421454
ManagedIdentityParameters.builder(resource)
422455
.environmentVariables(environmentVariables)
423456
.build()).get();
@@ -449,11 +482,14 @@ void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
449482

450483
when(httpClientMock.send(any())).thenReturn(response);
451484

452-
ManagedIdentityApplication miApp = ManagedIdentityApplication
485+
miApp = ManagedIdentityApplication
453486
.builder(ManagedIdentityId.systemAssigned())
454487
.httpClient(httpClientMock)
455488
.build();
456489

490+
// Clear caching to avoid cross test pollution.
491+
miApp.tokenCache().accessTokens.clear();
492+
457493
try {
458494
miApp.acquireTokenForManagedIdentity(
459495
ManagedIdentityParameters.builder(resource)

0 commit comments

Comments
 (0)