7
7
import com .azure .core .util .Configuration ;
8
8
import com .azure .identity .implementation .IdentityClient ;
9
9
import com .azure .identity .util .TestUtils ;
10
+ import org .junit .Assert ;
10
11
import org .junit .Test ;
11
- import org .junit .runner .RunWith ;
12
- import org .powermock .api .mockito .PowerMockito ;
13
- import org .powermock .core .classloader .annotations .PowerMockIgnore ;
14
- import org .powermock .core .classloader .annotations .PrepareForTest ;
15
- import org .powermock .modules .junit4 .PowerMockRunner ;
12
+ import org .mockito .MockedConstruction ;
16
13
import reactor .core .publisher .Mono ;
17
14
import reactor .test .StepVerifier ;
18
15
21
18
import java .util .UUID ;
22
19
23
20
import static org .mockito .ArgumentMatchers .*;
21
+ import static org .mockito .Mockito .mockConstruction ;
24
22
import static org .mockito .Mockito .when ;
25
23
26
- @ RunWith (PowerMockRunner .class )
27
- @ PrepareForTest (fullyQualifiedNames = "com.azure.identity.*" )
28
- @ PowerMockIgnore ({"com.sun.org.apache.xerces.*" , "javax.xml.*" , "org.xml.*" , "javax.net.ssl.*" ,
29
- "io.netty.handler.ssl.*" , "io.netty.buffer.*" , "io.netty.channel.*" })
30
24
public class AzureApplicationCredentialTest {
31
25
32
26
private static final String TENANT_ID = "contoso.com" ;
33
27
private static final String CLIENT_ID = UUID .randomUUID ().toString ();
34
28
35
29
@ Test
36
30
public void testUseEnvironmentCredential () throws Exception {
37
- Configuration configuration = Configuration .getGlobalConfiguration ();
38
-
39
- try {
40
- // setup
41
- String secret = "secret" ;
42
- String token1 = "token1" ;
43
- TokenRequestContext request1 = new TokenRequestContext ().addScopes ("https://management.azure.com" );
44
- OffsetDateTime expiresOn = OffsetDateTime .now (ZoneOffset .UTC ).plusHours (1 );
45
- configuration .put ("AZURE_CLIENT_ID" , CLIENT_ID );
46
- configuration .put ("AZURE_CLIENT_SECRET" , secret );
47
- configuration .put ("AZURE_TENANT_ID" , TENANT_ID );
48
-
49
- // mock
50
- IdentityClient identityClient = PowerMockito .mock (IdentityClient .class );
31
+ Configuration configuration = Configuration .getGlobalConfiguration ().clone ();
32
+
33
+ // setup
34
+ String secret = "secret" ;
35
+ String token1 = "token1" ;
36
+ TokenRequestContext request1 = new TokenRequestContext ().addScopes ("https://management.azure.com" );
37
+ OffsetDateTime expiresOn = OffsetDateTime .now (ZoneOffset .UTC ).plusHours (1 );
38
+ configuration .put ("AZURE_CLIENT_ID" , CLIENT_ID );
39
+ configuration .put ("AZURE_CLIENT_SECRET" , secret );
40
+ configuration .put ("AZURE_TENANT_ID" , TENANT_ID );
41
+
42
+ // mock
43
+ try (MockedConstruction <IdentityClient > identityClientMock = mockConstruction (IdentityClient .class , (identityClient , context ) -> {
51
44
when (identityClient .authenticateWithConfidentialClientCache (any ())).thenReturn (Mono .empty ());
52
45
when (identityClient .authenticateWithConfidentialClient (request1 )).thenReturn (TestUtils .getMockAccessToken (token1 , expiresOn ));
53
- PowerMockito .whenNew (IdentityClient .class ).withArguments (eq (TENANT_ID ), eq (CLIENT_ID ), eq (secret ), isNull (), isNull (), isNull (), isNull (), isNull (), isNull (), eq (false ), isNull (), any ()).thenReturn (identityClient );
54
-
46
+ })) {
55
47
// test
56
- AzureApplicationCredential credential = new AzureApplicationCredentialBuilder ().build ();
48
+ AzureApplicationCredential credential = new AzureApplicationCredentialBuilder ().configuration ( configuration ). build ();
57
49
StepVerifier .create (credential .getToken (request1 ))
58
50
.expectNextMatches (accessToken -> token1 .equals (accessToken .getToken ())
59
51
&& expiresOn .getSecond () == accessToken .getExpiresAt ().getSecond ())
60
52
.verifyComplete ();
61
- } finally {
62
- // clean up
63
- configuration .remove ("AZURE_CLIENT_ID" );
64
- configuration .remove ("AZURE_CLIENT_SECRET" );
65
- configuration .remove ("AZURE_TENANT_ID" );
53
+ Assert .assertNotNull (identityClientMock );
66
54
}
67
55
}
68
56
@@ -74,22 +62,21 @@ public void testUseManagedIdentityCredential() throws Exception {
74
62
OffsetDateTime expiresAt = OffsetDateTime .now (ZoneOffset .UTC ).plusHours (1 );
75
63
76
64
// mock
77
- IdentityClient identityClient = PowerMockito .mock (IdentityClient .class );
78
- when (identityClient .authenticateToIMDSEndpoint (request )).thenReturn (TestUtils .getMockAccessToken (token1 , expiresAt ));
79
- PowerMockito .whenNew (IdentityClient .class ).withAnyArguments ().thenReturn (identityClient );
80
-
81
- IntelliJCredential intelliJCredential = PowerMockito .mock (IntelliJCredential .class );
82
- when (intelliJCredential .getToken (request ))
83
- .thenReturn (Mono .empty ());
84
- PowerMockito .whenNew (IntelliJCredential .class ).withAnyArguments ()
85
- .thenReturn (intelliJCredential );
86
-
87
- // test
88
- AzureApplicationCredential credential = new AzureApplicationCredentialBuilder ().build ();
89
- StepVerifier .create (credential .getToken (request ))
90
- .expectNextMatches (accessToken -> token1 .equals (accessToken .getToken ())
91
- && expiresAt .getSecond () == accessToken .getExpiresAt ().getSecond ())
92
- .verifyComplete ();
65
+ try (MockedConstruction <IdentityClient > identityClientMock = mockConstruction (IdentityClient .class , (identityClient , context ) -> {
66
+ when (identityClient .authenticateToIMDSEndpoint (request )).thenReturn (TestUtils .getMockAccessToken (token1 , expiresAt ));
67
+
68
+ }); MockedConstruction <IntelliJCredential > intelliCredentialMock = mockConstruction (IntelliJCredential .class , (intelliJCredential , context ) -> {
69
+ when (intelliJCredential .getToken (request )).thenReturn (Mono .empty ());
70
+ })) {
71
+ // test
72
+ AzureApplicationCredential credential = new AzureApplicationCredentialBuilder ().build ();
73
+ StepVerifier .create (credential .getToken (request ))
74
+ .expectNextMatches (accessToken -> token1 .equals (accessToken .getToken ())
75
+ && expiresAt .getSecond () == accessToken .getExpiresAt ().getSecond ())
76
+ .verifyComplete ();
77
+ Assert .assertNotNull (identityClientMock );
78
+ Assert .assertNotNull (intelliCredentialMock );
79
+ }
93
80
}
94
81
95
82
@ Test
@@ -98,37 +85,39 @@ public void testNoCredentialWorks() throws Exception {
98
85
TokenRequestContext request = new TokenRequestContext ().addScopes ("https://management.azure.com" );
99
86
100
87
// mock
101
- IdentityClient identityClient = PowerMockito . mock (IdentityClient .class );
102
- when (identityClient .authenticateToIMDSEndpoint (request ))
103
- .thenReturn (Mono .error (new CredentialUnavailableException ("Cannot get token from managed identity" )));
104
- PowerMockito . whenNew ( IdentityClient . class ). withAnyArguments ()
105
- . thenReturn ( identityClient );
106
-
107
- // test
108
- AzureApplicationCredential credential = new AzureApplicationCredentialBuilder (). build ();
109
- StepVerifier . create ( credential . getToken ( request ))
110
- . expectErrorMatches ( t -> t instanceof CredentialUnavailableException && t . getMessage ()
111
- . startsWith ( "EnvironmentCredential authentication unavailable. " ))
112
- . verify ();
88
+ try ( MockedConstruction < IdentityClient > identityClientMock = mockConstruction (IdentityClient .class , ( identityClient , context ) -> {
89
+ when (identityClient .authenticateToIMDSEndpoint (request ))
90
+ .thenReturn (Mono .error (new CredentialUnavailableException ("Cannot get token from managed identity" )));
91
+ })) {
92
+ // test
93
+ AzureApplicationCredential credential = new AzureApplicationCredentialBuilder (). build ();
94
+ StepVerifier . create ( credential . getToken ( request ))
95
+ . expectErrorMatches ( t -> t instanceof CredentialUnavailableException && t . getMessage ()
96
+ . startsWith ( "EnvironmentCredential authentication unavailable. " ))
97
+ . verify ();
98
+ Assert . assertNotNull ( identityClientMock );
99
+ }
113
100
}
114
101
115
102
@ Test
116
103
public void testCredentialUnavailable () throws Exception {
104
+ // setup
117
105
TokenRequestContext request = new TokenRequestContext ().addScopes ("https://management.azure.com" );
118
106
119
- ManagedIdentityCredential managedIdentityCredential = PowerMockito .mock (ManagedIdentityCredential .class );
120
- when (managedIdentityCredential .getToken (request ))
121
- .thenReturn (Mono .error (
122
- new CredentialUnavailableException ("Cannot get token from Managed Identity credential" )));
123
- PowerMockito .whenNew (ManagedIdentityCredential .class ).withAnyArguments ()
124
- .thenReturn (managedIdentityCredential );
125
-
126
- // test
127
- AzureApplicationCredential credential = new AzureApplicationCredentialBuilder ()
128
- .build ();
129
- StepVerifier .create (credential .getToken (request ))
130
- .expectErrorMatches (t -> t instanceof CredentialUnavailableException && t .getMessage ()
131
- .startsWith ("EnvironmentCredential authentication unavailable. " ))
132
- .verify ();
107
+ // mock
108
+ try (MockedConstruction <ManagedIdentityCredential > managedIdentityCredentialMock = mockConstruction (ManagedIdentityCredential .class , (managedIdentityCredential , context ) -> {
109
+ when (managedIdentityCredential .getToken (request ))
110
+ .thenReturn (Mono .error (
111
+ new CredentialUnavailableException ("Cannot get token from Managed Identity credential" )));
112
+ })) {
113
+ // test
114
+ AzureApplicationCredential credential = new AzureApplicationCredentialBuilder ()
115
+ .build ();
116
+ StepVerifier .create (credential .getToken (request ))
117
+ .expectErrorMatches (t -> t instanceof CredentialUnavailableException && t .getMessage ()
118
+ .startsWith ("EnvironmentCredential authentication unavailable. " ))
119
+ .verify ();
120
+ Assert .assertNotNull (managedIdentityCredentialMock );
121
+ }
133
122
}
134
123
}
0 commit comments