7
7
import static org .junit .Assert .assertEquals ;
8
8
import static org .junit .Assert .assertThrows ;
9
9
import static org .mockito .ArgumentMatchers .any ;
10
+ import static org .mockito .Mockito .atLeastOnce ;
10
11
import static org .mockito .Mockito .doAnswer ;
11
12
import static org .mockito .Mockito .mock ;
12
- import static org .mockito .Mockito .mockConstruction ;
13
13
import static org .mockito .Mockito .never ;
14
- import static org .mockito .Mockito .spy ;
15
14
import static org .mockito .Mockito .times ;
16
15
import static org .mockito .Mockito .verify ;
17
16
22
21
import org .hamcrest .Matchers ;
23
22
import org .junit .Test ;
24
23
import org .mockito .ArgumentCaptor ;
25
- import org .mockito .MockedConstruction ;
26
- import org .mockito .Mockito ;
27
- import redis .clients .authentication .core .AuthXManager ;
28
- import redis .clients .authentication .core .AuthXManagerFactory ;
24
+
29
25
import redis .clients .authentication .core .IdentityProvider ;
30
- import redis .clients .authentication .core .IdentityProviderConfig ;
31
26
import redis .clients .authentication .core .SimpleToken ;
32
27
import redis .clients .authentication .core .Token ;
33
- import redis .clients .authentication .core .TokenAuthConfig ;
34
28
import redis .clients .authentication .core .TokenListener ;
35
29
import redis .clients .authentication .core .TokenManager ;
36
30
import redis .clients .authentication .core .TokenManagerConfig ;
39
33
40
34
public class CoreAuthenticationUnitTests {
41
35
42
- public static class CustomAuthXManager extends AuthXManager {
43
-
44
- public CustomAuthXManager (TokenManager tokenManager ) {
45
- super (tokenManager );
46
- }
47
- }
48
-
49
- @ Test
50
- public void testAuthXManagerFactory () {
51
- TokenManagerConfig tokenManagerConfig = mock (TokenManagerConfig .class );
52
- IdentityProviderConfig identityProviderConfig = mock (IdentityProviderConfig .class );
53
- IdentityProvider identityProvider = mock (IdentityProvider .class );
54
-
55
- when (identityProviderConfig .getProvider ()).thenReturn (identityProvider );
56
-
57
- try (MockedConstruction <TokenManager > mockedConstructor = mockConstruction (TokenManager .class ,
58
- (mock , context ) -> {
59
- assertEquals (identityProvider , context .arguments ().get (0 ));
60
- assertEquals (tokenManagerConfig , context .arguments ().get (1 ));
61
- })) {
62
- AuthXManagerFactory .create (new TokenAuthConfig (tokenManagerConfig , identityProviderConfig ));
63
-
64
- AuthXManagerFactory .create (CustomAuthXManager .class ,
65
- new TokenAuthConfig (tokenManagerConfig , identityProviderConfig ));
66
- }
67
- }
68
-
69
36
public static class TokenManagerConfigWrapper extends TokenManagerConfig {
70
37
int lower ;
71
38
float ratio ;
@@ -159,7 +126,8 @@ public void testCalculateRenewalDelay() {
159
126
}
160
127
161
128
@ Test
162
- public void testAuthXManager () throws InterruptedException , ExecutionException , TimeoutException {
129
+ public void testTokenManagerStart ()
130
+ throws InterruptedException , ExecutionException , TimeoutException {
163
131
164
132
IdentityProvider identityProvider = () -> new SimpleToken ("tokenVal" ,
165
133
System .currentTimeMillis () + 5 * 1000 , System .currentTimeMillis (),
@@ -168,16 +136,15 @@ public void testAuthXManager() throws InterruptedException, ExecutionException,
168
136
TokenManager tokenManager = new TokenManager (identityProvider ,
169
137
new TokenManagerConfig (0.7F , 200 , 2000 , null ));
170
138
171
- AuthXManager manager = spy (new AuthXManager (tokenManager ));
172
-
139
+ TokenListener listener = mock (TokenListener .class );
173
140
final Token [] tokenHolder = new Token [1 ];
174
141
doAnswer (invocation -> {
175
142
Object [] args = invocation .getArguments ();
176
143
tokenHolder [0 ] = (Token ) args [0 ];
177
144
return null ;
178
- }).when (manager ). authenticateConnections (any ());
145
+ }).when (listener ). onTokenRenewed (any ());
179
146
180
- manager .start (true );
147
+ tokenManager .start (listener , true );
181
148
assertEquals (tokenHolder [0 ].getValue (), "tokenVal" );
182
149
}
183
150
@@ -190,8 +157,8 @@ public void testBlockForInitialToken() {
190
157
TokenManager tokenManager = new TokenManager (identityProvider ,
191
158
new TokenManagerConfig (0.7F , 200 , 2000 , new TokenManagerConfig .RetryPolicy (5 , 100 )));
192
159
193
- AuthXManager manager = new AuthXManager ( tokenManager );
194
- ExecutionException e = assertThrows ( ExecutionException . class , ( ) -> manager .start (true ));
160
+ ExecutionException e = assertThrows ( ExecutionException . class ,
161
+ ( ) -> tokenManager .start (mock ( TokenListener . class ), true ));
195
162
196
163
assertEquals ("java.lang.RuntimeException: Test exception from identity provider!" ,
197
164
e .getCause ().getCause ().getMessage ());
@@ -210,12 +177,12 @@ public void testNoBlockForInitialToken()
210
177
TokenManager tokenManager = new TokenManager (identityProvider , new TokenManagerConfig (0.7F , 200 ,
211
178
2000 , new TokenManagerConfig .RetryPolicy (numberOfRetries - 1 , 100 )));
212
179
213
- AuthXManager manager = spy ( new AuthXManager ( tokenManager ) );
214
- manager .start (false );
180
+ TokenListener listener = mock ( TokenListener . class );
181
+ tokenManager .start (listener , false );
215
182
216
183
requesLatch .await ();
217
- verify (manager , Mockito . atLeastOnce ()).onError (Mockito . any ());
218
- verify (manager , Mockito . never ()).authenticateConnections ( Mockito . any ());
184
+ verify (listener , atLeastOnce ()).onError (any ());
185
+ verify (listener , never ()).onTokenRenewed ( any ());
219
186
}
220
187
221
188
@ Test
@@ -273,12 +240,12 @@ public void testTokenManagerWithHangingTokenRequest()
273
240
TokenManager tokenManager = new TokenManager (identityProvider , new TokenManagerConfig (0.7F , 200 ,
274
241
executionTimeout , new TokenManagerConfig .RetryPolicy (numberOfRetries , 100 )));
275
242
276
- AuthXManager manager = spy ( new AuthXManager ( tokenManager ) );
277
- manager .start (false );
243
+ TokenListener listener = mock ( TokenListener . class );
244
+ tokenManager .start (listener , false );
278
245
requesLatch .await ();
279
- verify (manager , never ()).onError (any ());
246
+ verify (listener , never ()).onError (any ());
280
247
await ().atMost (2 , TimeUnit .SECONDS ).untilAsserted (() -> {
281
- verify (manager , times (1 )).authenticateConnections (any ());
248
+ verify (listener , times (1 )).onTokenRenewed (any ());
282
249
});
283
250
}
284
251
}
0 commit comments