Skip to content

Commit 15da510

Browse files
authored
Merge pull request #903 from AzureAD/avdunn/code-coverage
Produce code coverage reports for pipelines
2 parents 5259675 + d58d5a8 commit 15da510

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

msal4j-sdk/pom.xml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@
246246
<plugin>
247247
<groupId>org.apache.maven.plugins</groupId>
248248
<artifactId>maven-surefire-plugin</artifactId>
249-
<version>2.10</version>
249+
<version>3.5.2</version>
250250
<configuration>
251-
<argLine>-noverify</argLine>
251+
<argLine>@{argLine} -noverify</argLine>
252252
</configuration>
253253
</plugin>
254254

@@ -339,6 +339,25 @@
339339
</execution>
340340
</executions>
341341
</plugin>
342+
<plugin>
343+
<groupId>org.jacoco</groupId>
344+
<artifactId>jacoco-maven-plugin</artifactId>
345+
<version>0.8.12</version>
346+
<executions>
347+
<execution>
348+
<goals>
349+
<goal>prepare-agent</goal>
350+
</goals>
351+
</execution>
352+
<execution>
353+
<id>jacoco-site</id>
354+
<phase>test</phase>
355+
<goals>
356+
<goal>report</goal>
357+
</goals>
358+
</execution>
359+
</executions>
360+
</plugin>
342361
</plugins>
343362
</build>
344363
</project>

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class ServiceFabricManagedIdentitySource extends AbstractManagedIdentitySource {
2424
//Service Fabric requires a special check for an environment variable containing a certificate thumbprint used for validating requests.
2525
//No other flow need this and an app developer may not be aware of it, so it was decided that for the Service Fabric flow we will simply override
2626
// any HttpClient that may have been set by the app developer with our own client which performs the validation logic.
27-
private final IHttpClient httpClient = new DefaultHttpClientManagedIdentity(null, null, null, null);
28-
private final HttpHelper httpHelper = new HttpHelper(httpClient);
27+
private static IHttpClient httpClient = new DefaultHttpClientManagedIdentity(null, null, null, null);
28+
private static HttpHelper httpHelper = new HttpHelperManagedIdentity(httpClient);
2929

3030
@Override
3131
public void createManagedIdentityRequest(String resource) {
@@ -117,4 +117,10 @@ private static URI validateAndGetUri(String msiEndpoint)
117117
}
118118
}
119119

120+
//The HttpClient is not normally customizable in this flow, as it requires special behavior for certificate validation.
121+
//However, unit tests often need to mock HttpClient and need a way to inject the mocked object into this class.
122+
static void setHttpClient(IHttpClient client) {
123+
httpClient = client;
124+
httpHelper = new HttpHelperManagedIdentity(httpClient);
125+
}
120126
}

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
7171
String endpoint = null;
7272
Map<String, String> headers = new HashMap<>();
7373
Map<String, List<String>> queryParameters = new HashMap<>();
74-
Map<String, List<String>> bodyParameters = new HashMap<>();
7574

7675
switch (source) {
7776
case APP_SERVICE: {
@@ -89,10 +88,8 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
8988
headers.put("ContentType", "application/x-www-form-urlencoded");
9089
headers.put("Metadata", "true");
9190

92-
bodyParameters.put("resource", singletonList(resource));
93-
9491
queryParameters.put("resource", singletonList(resource));
95-
return new HttpRequest(HttpMethod.GET, computeUri(endpoint, queryParameters), headers, URLUtils.serializeParameters(bodyParameters));
92+
break;
9693
}
9794
case IMDS: {
9895
endpoint = IMDS_ENDPOINT;
@@ -110,11 +107,14 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
110107
headers.put("Metadata", "true");
111108
break;
112109
}
113-
case SERVICE_FABRIC:
110+
case SERVICE_FABRIC: {
114111
endpoint = serviceFabricEndpoint;
115112
queryParameters.put("api-version", singletonList("2019-07-01-preview"));
116113
queryParameters.put("resource", singletonList(resource));
114+
115+
headers.put("secret", "secret");
117116
break;
117+
}
118118
}
119119

120120
switch (id.getIdType()) {
@@ -172,6 +172,9 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
172172
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
173173
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
174174
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
175+
if (source == SERVICE_FABRIC) {
176+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
177+
}
175178

176179
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
177180

@@ -206,6 +209,9 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
206209
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
207210
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
208211
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
212+
if (source == SERVICE_FABRIC) {
213+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
214+
}
209215

210216
when(httpClientMock.send(expectedRequest(source, resource, id))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
211217

@@ -294,6 +300,9 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
294300
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
295301
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
296302
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
303+
if (source == SERVICE_FABRIC) {
304+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
305+
}
297306

298307
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
299308
when(httpClientMock.send(expectedRequest(source, anotherResource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
@@ -327,6 +336,9 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
327336
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
328337
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
329338
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
339+
if (source == SERVICE_FABRIC) {
340+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
341+
}
330342

331343
if (environmentVariables.getEnvironmentVariable("SourceType").equals(ManagedIdentitySourceType.CLOUD_SHELL.toString())) {
332344
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(500, getMsiErrorResponseCloudShell()));
@@ -364,7 +376,11 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
364376
void managedIdentityTest_Retry(ManagedIdentitySourceType source, String endpoint, String resource) throws Exception {
365377
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
366378
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
367-
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
379+
380+
DefaultHttpClientManagedIdentity httpClientMock = mock(DefaultHttpClientManagedIdentity.class);
381+
if (source == SERVICE_FABRIC) {
382+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
383+
}
368384

369385
miApp = ManagedIdentityApplication
370386
.builder(ManagedIdentityId.systemAssigned())
@@ -415,6 +431,9 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
415431
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
416432
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
417433
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
434+
if (source == SERVICE_FABRIC) {
435+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
436+
}
418437

419438
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(500, ""));
420439

@@ -449,7 +468,9 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
449468
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
450469
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
451470
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
452-
471+
if (source == SERVICE_FABRIC) {
472+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
473+
}
453474
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, ""));
454475

455476
miApp = ManagedIdentityApplication
@@ -483,6 +504,9 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
483504
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
484505
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
485506
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
507+
if (source == SERVICE_FABRIC) {
508+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
509+
}
486510

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

@@ -517,6 +541,9 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
517541
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
518542
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
519543
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
544+
if (source == SERVICE_FABRIC) {
545+
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
546+
}
520547

521548
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
522549

0 commit comments

Comments
 (0)