Skip to content

Commit b81c4f8

Browse files
committed
Removed lenient from mock and merge conflicts
1 parent 2d04a99 commit b81c4f8

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AzureArcManagedIdentitySource.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ private static URI validateAndGetUri(String identityEndpoint, String imdsEndpoin
4545
} catch (URISyntaxException e) {
4646
throw new MsalManagedIdentityException(MsalError.INVALID_MANAGED_IDENTITY_ENDPOINT, String.format(
4747
MsalErrorMessage.MANAGED_IDENTITY_ENDPOINT_INVALID_URI_ERROR, "IDENTITY_ENDPOINT", identityEndpoint, AZURE_ARC),
48-
ManagedIdentitySourceType.AzureArc);
48+
ManagedIdentitySourceType.AZURE_ARC);
4949
}
5050

5151
LOG.info("[Managed Identity] Creating Azure Arc managed identity. Endpoint URI: " + endpointUri);
5252
return endpointUri;
5353
}
5454

5555
private AzureArcManagedIdentitySource(URI endpoint, MsalRequest msalRequest, ServiceBundle serviceBundle){
56-
super(msalRequest, serviceBundle, ManagedIdentitySourceType.AzureArc);
56+
super(msalRequest, serviceBundle, ManagedIdentitySourceType.AZURE_ARC);
5757
this.MSI_ENDPOINT = endpoint;
5858

5959
ManagedIdentityIdType idType =
6060
((ManagedIdentityApplication) msalRequest.application()).getManagedIdentityId().getIdType();
61-
if (idType != ManagedIdentityIdType.SystemAssigned) {
61+
if (idType != ManagedIdentityIdType.SYSTEM_ASSIGNED) {
6262
throw new MsalManagedIdentityException(MsalError.USER_ASSIGNED_MANAGED_IDENTITY_NOT_SUPPORTED,
6363
String.format(MsalErrorMessage.MANAGED_IDENTITY_USER_ASSIGNED_NOT_SUPPORTED, AZURE_ARC),
64-
ManagedIdentitySourceType.CloudShell);
64+
ManagedIdentitySourceType.AZURE_ARC);
6565
}
6666
}
6767

@@ -92,7 +92,7 @@ public ManagedIdentityResponse handleResponse(
9292
LOG.error("[Managed Identity] WWW-Authenticate header is expected but not found.");
9393
throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_REQUEST_FAILED,
9494
MsalErrorMessage.MANAGED_IDENTITY_NO_CHALLENGE_ERROR,
95-
ManagedIdentitySourceType.AzureArc);
95+
ManagedIdentitySourceType.AZURE_ARC);
9696
}
9797

9898
String challenge = response.headers().get("WWW-Authenticate").get(0);
@@ -103,7 +103,7 @@ public ManagedIdentityResponse handleResponse(
103103
LOG.error("[Managed Identity] The WWW-Authenticate header for Azure arc managed identity is not an expected format.");
104104
throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_REQUEST_FAILED,
105105
MsalErrorMessage.MANAGED_IDENTITY_INVALID_CHALLENGE,
106-
ManagedIdentitySourceType.AzureArc);
106+
ManagedIdentitySourceType.AZURE_ARC);
107107
}
108108

109109
String authHeaderValue = "Basic " + splitChallenge[1];

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public static Stream<Arguments> createData() {
2121
ManagedIdentityTests.resource),
2222
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
2323
ManagedIdentityTests.resourceDefaultSuffix),
24-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
24+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
2525
ManagedIdentityTests.resource),
26-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
26+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
2727
ManagedIdentityTests.resourceDefaultSuffix),
2828
Arguments.of(ManagedIdentitySourceType.IMDS, ManagedIdentityTests.IMDS_ENDPOINT,
2929
ManagedIdentityTests.resource),
@@ -49,11 +49,11 @@ public static Stream<Arguments> createDataUserAssignedNotSupported() {
4949
return Stream.of(
5050
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
5151
ManagedIdentityId.userAssignedClientId(CLIENT_ID)),
52-
Arguments.of(ManagedIdentitySourceType.CloudShell, ManagedIdentityTests.cloudShellEndpoint,
52+
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
5353
ManagedIdentityId.userAssignedResourceId(RESOURCE_ID)),
54-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
54+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
5555
ManagedIdentityId.userAssignedClientId(CLIENT_ID)),
56-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
56+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
5757
ManagedIdentityId.userAssignedResourceId(RESOURCE_ID)));
5858
}
5959

@@ -67,9 +67,9 @@ public static Stream<Arguments> createDataWrongScope() {
6767
"user.read"),
6868
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
6969
"https://management.core.windows.net//user_impersonation"),
70-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
70+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
7171
"user.read"),
72-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
72+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
7373
"https://management.core.windows.net//user_impersonation"),
7474
Arguments.of(ManagedIdentitySourceType.IMDS, ManagedIdentityTests.IMDS_ENDPOINT,
7575
"user.read"),
@@ -79,7 +79,7 @@ public static Stream<Arguments> createDataWrongScope() {
7979

8080
public static Stream<Arguments> createDataError() {
8181
return Stream.of(
82-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint),
82+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint),
8383
Arguments.of(ManagedIdentitySourceType.APP_SERVICE, ManagedIdentityTests.appServiceEndpoint),
8484
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint),
8585
Arguments.of(ManagedIdentitySourceType.IMDS, ManagedIdentityTests.IMDS_ENDPOINT));

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
8282
headers.put("Metadata", "true");
8383
break;
8484
}
85-
case AzureArc: {
85+
case AZURE_ARC: {
8686
endpoint = azureArcEndpoint;
8787

8888
queryParameters.put("api-version", Collections.singletonList("2019-11-01"));
@@ -129,7 +129,7 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
129129
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
130130
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
131131

132-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
132+
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
133133

134134
ManagedIdentityApplication miApp = ManagedIdentityApplication
135135
.builder(ManagedIdentityId.systemAssigned())
@@ -152,6 +152,7 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
152152

153153
assertNotNull(result.accessToken());
154154
assertEquals(accessToken, result.accessToken());
155+
verify(httpClientMock, times(1)).send(any());
155156
}
156157

157158
@ParameterizedTest
@@ -173,6 +174,7 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
173174
.build()).get();
174175

175176
assertNotNull(result.accessToken());
177+
verify(httpClientMock, times(1)).send(any());
176178
}
177179

178180
@ParameterizedTest
@@ -197,7 +199,7 @@ void managedIdentityTest_UserAssigned_NotSupported(ManagedIdentitySourceType sou
197199
assertInstanceOf(MsalManagedIdentityException.class, e.getCause());
198200

199201
MsalManagedIdentityException msalMsiException = (MsalManagedIdentityException) e.getCause();
200-
assertEquals(ManagedIdentitySourceType.CLOUD_SHELL, msalMsiException.managedIdentitySourceType);
202+
assertEquals(source, msalMsiException.managedIdentitySourceType);
201203
assertEquals(MsalError.USER_ASSIGNED_MANAGED_IDENTITY_NOT_SUPPORTED, msalMsiException.errorCode());
202204
return;
203205
}
@@ -214,8 +216,8 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
214216
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
215217
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
216218

217-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
218-
lenient().when(httpClientMock.send(eq(expectedRequest(source, anotherResource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
219+
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
220+
when(httpClientMock.send(eq(expectedRequest(source, anotherResource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
219221

220222
ManagedIdentityApplication miApp = ManagedIdentityApplication
221223
.builder(ManagedIdentityId.systemAssigned())
@@ -235,6 +237,7 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
235237
.build()).get();
236238

237239
assertNotNull(result.accessToken());
240+
verify(httpClientMock, times(2)).send(any());
238241
// TODO: Assert token source to check the token source is IDP and not Cache.
239242
}
240243

@@ -244,8 +247,7 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
244247
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
245248
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
246249

247-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, getMsiErrorResponse()));
248-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, getMsiErrorResponse()));
250+
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, getMsiErrorResponse()));
249251

250252
ManagedIdentityApplication miApp = ManagedIdentityApplication
251253
.builder(ManagedIdentityId.systemAssigned())
@@ -267,6 +269,7 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
267269
}
268270

269271
fail("MsalManagedIdentityException is expected but not thrown.");
272+
verify(httpClientMock, times(1)).send(any());
270273
}
271274

272275
@ParameterizedTest
@@ -275,8 +278,7 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
275278
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
276279
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
277280

278-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, ""));
279-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, ""));
281+
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(500, ""));
280282

281283
ManagedIdentityApplication miApp = ManagedIdentityApplication
282284
.builder(ManagedIdentityId.systemAssigned())
@@ -298,6 +300,7 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
298300
}
299301

300302
fail("MsalManagedIdentityException is expected but not thrown.");
303+
verify(httpClientMock, times(1)).send(any());
301304
}
302305

303306
@ParameterizedTest
@@ -306,8 +309,7 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
306309
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
307310
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
308311

309-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, ""));
310-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, ""));
312+
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, ""));
311313

312314
ManagedIdentityApplication miApp = ManagedIdentityApplication
313315
.builder(ManagedIdentityId.systemAssigned())
@@ -329,6 +331,7 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
329331
}
330332

331333
fail("MsalManagedIdentityException is expected but not thrown.");
334+
verify(httpClientMock, times(1)).send(any());
332335
}
333336

334337
@ParameterizedTest
@@ -337,7 +340,7 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
337340
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
338341
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
339342

340-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenThrow(new SocketException("A socket operation was attempted to an unreachable network."));
343+
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenThrow(new SocketException("A socket operation was attempted to an unreachable network."));
341344

342345
ManagedIdentityApplication miApp = ManagedIdentityApplication
343346
.builder(ManagedIdentityId.systemAssigned())
@@ -359,17 +362,18 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
359362
}
360363

361364
fail("MsalManagedIdentityException is expected but not thrown.");
365+
verify(httpClientMock, times(1)).send(any());
362366
}
363367

364368
@Test
365369
void azureArcManagedIdentity_MissingAuthHeader() throws Exception {
366-
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AzureArc, azureArcEndpoint);
370+
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AZURE_ARC, azureArcEndpoint);
367371
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
368372

369373
HttpResponse response = new HttpResponse();
370374
response.statusCode(HttpStatus.SC_UNAUTHORIZED);
371375

372-
lenient().when(httpClientMock.send(any())).thenReturn(response);
376+
when(httpClientMock.send(any())).thenReturn(response);
373377

374378
ManagedIdentityApplication miApp = ManagedIdentityApplication
375379
.builder(ManagedIdentityId.systemAssigned())
@@ -385,13 +389,14 @@ void azureArcManagedIdentity_MissingAuthHeader() throws Exception {
385389
assert(exception.getCause() instanceof MsalManagedIdentityException);
386390

387391
MsalManagedIdentityException miException = (MsalManagedIdentityException) exception.getCause();
388-
assertEquals(ManagedIdentitySourceType.AzureArc, miException.managedIdentitySourceType);
392+
assertEquals(ManagedIdentitySourceType.AZURE_ARC, miException.managedIdentitySourceType);
389393
assertEquals(MsalError.MANAGED_IDENTITY_REQUEST_FAILED, miException.errorCode());
390394
assertEquals(MsalErrorMessage.MANAGED_IDENTITY_NO_CHALLENGE_ERROR, miException.getMessage());
391395
return;
392396
}
393397

394398
fail("MsalManagedIdentityException is expected but not thrown.");
399+
verify(httpClientMock, times(1)).send(any());
395400
}
396401

397402
@ParameterizedTest
@@ -400,7 +405,7 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
400405
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
401406
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
402407

403-
lenient().when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
408+
when(httpClientMock.send(eq(expectedRequest(source, resource)))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
404409

405410
ManagedIdentityApplication miApp1 = ManagedIdentityApplication
406411
.builder(ManagedIdentityId.systemAssigned())
@@ -430,18 +435,19 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
430435
// so calling acquireTokenForManagedIdentity with the same parameters in two different ManagedIdentityApplications
431436
// should return the same token
432437
assertEquals(resultMiApp1.accessToken(), resultMiApp2.accessToken());
438+
verify(httpClientMock, times(1)).send(any());
433439
}
434440

435441
@Test
436442
void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
437-
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AzureArc, azureArcEndpoint);
443+
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AZURE_ARC, azureArcEndpoint);
438444
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
439445

440446
HttpResponse response = new HttpResponse();
441447
response.statusCode(HttpStatus.SC_UNAUTHORIZED);
442448
response.headers().put("WWW-Authenticate", Collections.singletonList("Basic realm=filepath=somepath"));
443449

444-
lenient().when(httpClientMock.send(any())).thenReturn(response);
450+
when(httpClientMock.send(any())).thenReturn(response);
445451

446452
ManagedIdentityApplication miApp = ManagedIdentityApplication
447453
.builder(ManagedIdentityId.systemAssigned())
@@ -457,12 +463,13 @@ void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
457463
assert(exception.getCause() instanceof MsalManagedIdentityException);
458464

459465
MsalManagedIdentityException miException = (MsalManagedIdentityException) exception.getCause();
460-
assertEquals(ManagedIdentitySourceType.AzureArc, miException.managedIdentitySourceType);
466+
assertEquals(ManagedIdentitySourceType.AZURE_ARC, miException.managedIdentitySourceType);
461467
assertEquals(MsalError.MANAGED_IDENTITY_REQUEST_FAILED, miException.errorCode());
462468
assertEquals(MsalErrorMessage.MANAGED_IDENTITY_INVALID_CHALLENGE, miException.getMessage());
463469
return;
464470
}
465471

466472
fail("MsalManagedIdentityException is expected but not thrown.");
473+
verify(httpClientMock, times(1)).send(any());
467474
}
468475
}

0 commit comments

Comments
 (0)