15
15
16
16
package software .amazon .awssdk .auth .credentials ;
17
17
18
- import static org .junit .Assert .assertEquals ;
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
20
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
19
21
20
- import org . junit . Rule ;
22
+ import java . util . Arrays ;
21
23
import org .junit .Test ;
22
- import org .junit .rules . ExpectedException ;
24
+ import org .junit .jupiter . api . function . Executable ;
23
25
import software .amazon .awssdk .core .exception .SdkClientException ;
26
+ import software .amazon .awssdk .identity .spi .AwsCredentialsIdentity ;
27
+ import software .amazon .awssdk .identity .spi .IdentityProvider ;
24
28
import software .amazon .awssdk .profiles .ProfileFile ;
25
29
import software .amazon .awssdk .utils .StringInputStream ;
26
30
27
31
public class AwsCredentialsProviderChainTest {
28
32
29
- @ Rule
30
- public ExpectedException thrown = ExpectedException .none ();
31
-
32
33
/**
33
34
* Tests that, by default, the chain remembers which provider was able to
34
35
* provide credentials, and only calls that provider for any additional
35
36
* calls to getCredentials.
36
37
*/
37
38
@ Test
38
- public void testReusingLastProvider () throws Exception {
39
+ public void resolveCredentials_reuseEnabled_reusesLastProvider () throws Exception {
39
40
MockCredentialsProvider provider1 = new MockCredentialsProvider ("Failed!" );
40
41
MockCredentialsProvider provider2 = new MockCredentialsProvider ();
41
42
AwsCredentialsProviderChain chain = AwsCredentialsProviderChain .builder ()
@@ -64,7 +65,7 @@ public void testReusingLastProvider() throws Exception {
64
65
* provider that can return credentials.
65
66
*/
66
67
@ Test
67
- public void testDisableReusingLastProvider () throws Exception {
68
+ public void resolveCredentials_reuseDisabled_alwaysGoesThroughChain () throws Exception {
68
69
MockCredentialsProvider provider1 = new MockCredentialsProvider ("Failed!" );
69
70
MockCredentialsProvider provider2 = new MockCredentialsProvider ();
70
71
AwsCredentialsProviderChain chain = AwsCredentialsProviderChain .builder ()
@@ -85,7 +86,7 @@ public void testDisableReusingLastProvider() throws Exception {
85
86
}
86
87
87
88
@ Test
88
- public void testMissingProfileUsesNextProvider () {
89
+ public void resolveCredentials_missingProfile_usesNextProvider () {
89
90
ProfileCredentialsProvider provider =
90
91
new ProfileCredentialsProvider .BuilderImpl ()
91
92
.defaultProfileFileLoader (() -> ProfileFile .builder ()
@@ -103,24 +104,73 @@ public void testMissingProfileUsesNextProvider() {
103
104
}
104
105
105
106
/**
106
- * Tests that getCredentials throws an thrown if all providers in the
107
+ * Tests that resolveCredentials throws an thrown if all providers in the
107
108
* chain fail to provide credentials.
108
109
*/
109
110
@ Test
110
- public void testGetCredentialsException () {
111
+ public void resolveCredentials_allProvidersFail_throwsExceptionWithMessageFromAllProviders () {
111
112
MockCredentialsProvider provider1 = new MockCredentialsProvider ("Failed!" );
112
113
MockCredentialsProvider provider2 = new MockCredentialsProvider ("Bad!" );
113
114
AwsCredentialsProviderChain chain = AwsCredentialsProviderChain .builder ()
114
115
.credentialsProviders (provider1 , provider2 )
115
116
.build ();
116
117
117
- thrown .expect (SdkClientException .class );
118
- thrown .expectMessage (provider1 .exceptionMessage );
119
- thrown .expectMessage (provider2 .exceptionMessage );
118
+ SdkClientException e = assertThrows (SdkClientException .class , () -> chain .resolveCredentials ());
119
+ assertThat (e .getMessage ()).contains (provider1 .exceptionMessage );
120
+ assertThat (e .getMessage ()).contains (provider2 .exceptionMessage );
121
+ }
120
122
121
- chain .resolveCredentials ();
123
+ @ Test
124
+ public void resolveCredentials_emptyChain_throwsException () {
125
+ assertThrowsIllegalArgument (() -> AwsCredentialsProviderChain .of ());
126
+
127
+ assertThrowsIllegalArgument (() -> AwsCredentialsProviderChain
128
+ .builder ()
129
+ .credentialsProviders ()
130
+ .build ());
131
+
132
+ assertThrowsIllegalArgument (() -> AwsCredentialsProviderChain
133
+ .builder ()
134
+ .credentialsProviders (Arrays .asList ())
135
+ .build ());
122
136
}
123
137
138
+ private void assertThrowsIllegalArgument (Executable executable ) {
139
+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class , executable );
140
+ assertThat (e .getMessage ()).contains ("No credential providers were specified." );
141
+ }
142
+
143
+ /**
144
+ * Tests that the chain is setup correctly with the overloaded methods that accept the AwsCredentialsProvider type.
145
+ */
146
+ @ Test
147
+ public void createMethods_withOldCredentialsType_work () {
148
+ AwsCredentialsProvider provider = StaticCredentialsProvider .create (AwsBasicCredentials .create (
149
+ "accessKey" , "secretKey" ));
150
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .of (provider ));
151
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .builder ().credentialsProviders (provider ).build ());
152
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .builder ().credentialsProviders (Arrays .asList (provider )).build ());
153
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .builder ().addCredentialsProvider (provider ).build ());
154
+ }
155
+
156
+ /**
157
+ * Tests that the chain is setup correctly with the overloaded methods that accept the IdentityProvider type.
158
+ */
159
+ @ Test
160
+ public void createMethods_withNewCredentialsType_work () {
161
+ IdentityProvider <AwsCredentialsIdentity > provider = StaticCredentialsProvider .create (AwsBasicCredentials .create (
162
+ "accessKey" , "secretKey" ));
163
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .of (provider ));
164
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .builder ().credentialsProviders (provider ).build ());
165
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .builder ().credentialsIdentityProviders (Arrays .asList (provider )).build ());
166
+ assertChainResolvesCorrectly (AwsCredentialsProviderChain .builder ().addCredentialsProvider (provider ).build ());
167
+ }
168
+
169
+ private static void assertChainResolvesCorrectly (AwsCredentialsProviderChain chain ) {
170
+ AwsCredentials credentials = chain .resolveCredentials ();
171
+ assertThat (credentials .accessKeyId ()).isEqualTo ("accessKey" );
172
+ assertThat (credentials .secretAccessKey ()).isEqualTo ("secretKey" );
173
+ }
124
174
125
175
private static final class MockCredentialsProvider implements AwsCredentialsProvider {
126
176
private final StaticCredentialsProvider staticCredentialsProvider ;
0 commit comments