Skip to content

Commit c7fc022

Browse files
committed
update tests
1 parent 2ae8018 commit c7fc022

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

sdk/identity/azure-identity/src/main/java/com/azure/identity/OSBrokerCredential.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
class OSBrokerCredential implements TokenCredential {
1717
private static final ClientLogger LOGGER = new ClientLogger(OSBrokerCredential.class);
18-
private static final String BROKER_BUILDER_CLASS
19-
= "com.azure.identity.broker.InteractiveBrowserBrokerCredentialBuilder";
18+
private static final String BROKER_BUILDER_CLASS = "com.azure.identity.broker.InteractiveBrowserBrokerCredentialBuilder";
2019
private final String tenantId;
2120
private final AtomicReference<TokenCredential> cached = new AtomicReference<>();
2221

@@ -28,7 +27,18 @@ class OSBrokerCredential implements TokenCredential {
2827
public Mono<AccessToken> getToken(TokenRequestContext request) {
2928
TokenCredential credential = cached.get();
3029
if (credential == null) {
31-
TokenCredential newCredential = createBrokerCredential();
30+
TokenCredential newCredential;
31+
try {
32+
// Create the broker credential dynamically
33+
newCredential = createBrokerCredential();
34+
} catch (CredentialUnavailableException e) {
35+
// If the broker is unavailable, throw the exception
36+
return Mono.error(e);
37+
} catch (Exception e) {
38+
// Log and throw any other exceptions that occur during credential creation
39+
return Mono.error(LOGGER.logExceptionAsError(
40+
new CredentialUnavailableException("Failed to create OS Broker credential.", e)));
41+
}
3242
if (cached.compareAndSet(null, newCredential)) {
3343
credential = newCredential;
3444
} else {
@@ -39,12 +49,11 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
3949
}
4050

4151
private TokenCredential createBrokerCredential() {
42-
final String troubleshoot
43-
= " To mitigate this issue, refer to http://aka.ms/azsdk/java/identity/dacbrokerauth/troubleshoot";
52+
final String troubleshoot = " To mitigate this issue, refer to http://aka.ms/azsdk/java/identity/dacbrokerauth/troubleshoot";
4453
if (!IdentityUtil.isBrokerAvailable()) {
4554
throw LOGGER.logExceptionAsError(
4655
new CredentialUnavailableException("azure-identity-broker dependency is not available. "
47-
+ "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot));
56+
+ "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot));
4857
}
4958
try {
5059
Class<?> builderClass = Class.forName(BROKER_BUILDER_CLASS);
@@ -58,14 +67,12 @@ private TokenCredential createBrokerCredential() {
5867
}
5968
return browserCredentialBuilder.build();
6069
} catch (ClassNotFoundException e) {
61-
throw LOGGER
62-
.logExceptionAsError(new CredentialUnavailableException(
63-
"InteractiveBrowserBrokerCredentialBuilder class not found. "
64-
+ "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot,
65-
e));
70+
throw LOGGER.logExceptionAsError(
71+
new CredentialUnavailableException("InteractiveBrowserBrokerCredentialBuilder class not found. "
72+
+ "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot, e));
6673
} catch (Exception e) {
67-
throw LOGGER.logExceptionAsError(new CredentialUnavailableException(
68-
"Failed to create InteractiveBrowserBrokerCredential dynamically." + troubleshoot, e));
74+
throw LOGGER.logExceptionAsError(
75+
new CredentialUnavailableException("Failed to create InteractiveBrowserBrokerCredential dynamically." + troubleshoot, e));
6976
}
7077
}
7178
}

sdk/identity/azure-identity/src/test/java/com/azure/identity/DefaultAzureCredentialTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,12 @@ public void testNoCredentialWorks() {
321321
= mockConstruction(IntelliJCredential.class, (intelliJCredential, context) -> {
322322
when(intelliJCredential.getToken(request)).thenReturn(
323323
Mono.error(new CredentialUnavailableException("Cannot get token from IntelliJ Credential")));
324+
});
325+
MockedConstruction<OSBrokerCredential> osBrokerCredentialMock
326+
= mockConstruction(OSBrokerCredential.class, (osBrokerCredential, context) -> {
327+
when(osBrokerCredential.getToken(request)).thenReturn(
328+
Mono.error(new CredentialUnavailableException("Cannot get token from OS Broker credential")));
324329
})) {
325-
326330
// test
327331
DefaultAzureCredential credential
328332
= new DefaultAzureCredentialBuilder().configuration(configuration).build();
@@ -335,6 +339,7 @@ public void testNoCredentialWorks() {
335339
Assertions.assertNotNull(azureDeveloperCliCredentialMock);
336340
Assertions.assertNotNull(azurePowerShellCredentialMock);
337341
Assertions.assertNotNull(intelliJCredentialMock);
342+
Assertions.assertNotNull(osBrokerCredentialMock);
338343
}
339344
}
340345

@@ -368,7 +373,13 @@ public void testCredentialUnavailable() {
368373
= mockConstruction(AzureDeveloperCliCredential.class, (AzureDeveloperCliCredential, context) -> {
369374
when(AzureDeveloperCliCredential.getToken(request)).thenReturn(Mono.error(
370375
new CredentialUnavailableException("Cannot get token from Azure Developer CLI credential")));
376+
});
377+
MockedConstruction<OSBrokerCredential> osBrokerCredentialMock
378+
= mockConstruction(OSBrokerCredential.class, (osBrokerCredential, context) -> {
379+
when(osBrokerCredential.getToken(request)).thenReturn(
380+
Mono.error(new CredentialUnavailableException("Cannot get token from OS Broker credential")));
371381
})) {
382+
372383
// test
373384
DefaultAzureCredential credential
374385
= new DefaultAzureCredentialBuilder().configuration(configuration).build();
@@ -381,8 +392,8 @@ public void testCredentialUnavailable() {
381392
Assertions.assertNotNull(powerShellCredentialMock);
382393
Assertions.assertNotNull(azureCliCredentialMock);
383394
Assertions.assertNotNull(azureDeveloperCliCredentialMock);
395+
Assertions.assertNotNull(osBrokerCredentialMock);
384396
}
385-
386397
}
387398

388399
@Test

0 commit comments

Comments
 (0)