Make code cleaner in ProviderManagerTests

This commit is contained in:
Thomas Vitale 2020-01-30 19:26:47 +01:00 committed by Josh Cummings
parent 5ce60022d3
commit ace89e12f2

View File

@ -35,11 +35,10 @@ import static org.mockito.Mockito.*;
* *
* @author Ben Alex * @author Ben Alex
*/ */
@SuppressWarnings("unchecked")
public class ProviderManagerTests { public class ProviderManagerTests {
@Test(expected = ProviderNotFoundException.class) @Test(expected = ProviderNotFoundException.class)
public void authenticationFailsWithUnsupportedToken() throws Exception { public void authenticationFailsWithUnsupportedToken() {
Authentication token = new AbstractAuthenticationToken(null) { Authentication token = new AbstractAuthenticationToken(null) {
public Object getCredentials() { public Object getCredentials() {
return ""; return "";
@ -55,7 +54,7 @@ public class ProviderManagerTests {
} }
@Test @Test
public void credentialsAreClearedByDefault() throws Exception { public void credentialsAreClearedByDefault() {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"Test", "Password"); "Test", "Password");
ProviderManager mgr = makeProviderManager(); ProviderManager mgr = makeProviderManager();
@ -71,8 +70,7 @@ public class ProviderManagerTests {
@Test @Test
public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() { public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() {
final Authentication a = mock(Authentication.class); final Authentication a = mock(Authentication.class);
ProviderManager mgr = new ProviderManager( ProviderManager mgr = new ProviderManager(createProviderWhichReturns(a));
Arrays.asList(createProviderWhichReturns(a)));
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class); AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
mgr.setAuthenticationEventPublisher(publisher); mgr.setAuthenticationEventPublisher(publisher);
@ -122,7 +120,7 @@ public class ProviderManagerTests {
} }
}; };
ProviderManager authMgr = new ProviderManager(Arrays.asList(provider)); ProviderManager authMgr = new ProviderManager(provider);
TestingAuthenticationToken request = createAuthenticationToken(); TestingAuthenticationToken request = createAuthenticationToken();
request.setDetails(requestDetails); request.setDetails(requestDetails);
@ -132,8 +130,7 @@ public class ProviderManagerTests {
} }
@Test @Test
public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() {
throws Exception {
Object details = new Object(); Object details = new Object();
ProviderManager authMgr = makeProviderManager(); ProviderManager authMgr = makeProviderManager();
@ -149,8 +146,8 @@ public class ProviderManagerTests {
public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() { public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() {
final Authentication authReq = mock(Authentication.class); final Authentication authReq = mock(Authentication.class);
ProviderManager mgr = new ProviderManager( ProviderManager mgr = new ProviderManager(
Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", createProviderWhichThrows(new BadCredentialsException("",
new Throwable())), createProviderWhichReturns(authReq))); new Throwable())), createProviderWhichReturns(authReq));
assertThat(mgr.authenticate(mock(Authentication.class))).isSameAs(authReq); assertThat(mgr.authenticate(mock(Authentication.class))).isSameAs(authReq);
} }
@ -185,7 +182,7 @@ public class ProviderManagerTests {
} }
catch (AccountStatusException expected) { catch (AccountStatusException expected) {
} }
verifyZeroInteractions(otherProvider); verifyNoInteractions(otherProvider);
} }
@Test @Test
@ -194,7 +191,7 @@ public class ProviderManagerTests {
Authentication authReq = mock(Authentication.class); Authentication authReq = mock(Authentication.class);
when(parent.authenticate(authReq)).thenReturn(authReq); when(parent.authenticate(authReq)).thenReturn(authReq);
ProviderManager mgr = new ProviderManager( ProviderManager mgr = new ProviderManager(
Arrays.asList(mock(AuthenticationProvider.class)), parent); Collections.singletonList(mock(AuthenticationProvider.class)), parent);
assertThat(mgr.authenticate(authReq)).isSameAs(authReq); assertThat(mgr.authenticate(authReq)).isSameAs(authReq);
} }
@ -205,14 +202,14 @@ public class ProviderManagerTests {
}); });
AuthenticationManager parent = mock(AuthenticationManager.class); AuthenticationManager parent = mock(AuthenticationManager.class);
ProviderManager mgr = new ProviderManager( ProviderManager mgr = new ProviderManager(
Arrays.asList(iThrowAccountStatusException), parent); Collections.singletonList(iThrowAccountStatusException), parent);
try { try {
mgr.authenticate(mock(Authentication.class)); mgr.authenticate(mock(Authentication.class));
fail("Expected exception"); fail("Expected exception");
} }
catch (AccountStatusException expected) { catch (AccountStatusException expected) {
} }
verifyZeroInteractions(parent); verifyNoInteractions(parent);
} }
@Test @Test
@ -225,7 +222,7 @@ public class ProviderManagerTests {
// Set a provider that throws an exception - this is the exception we expect to be // Set a provider that throws an exception - this is the exception we expect to be
// propagated // propagated
ProviderManager mgr = new ProviderManager( ProviderManager mgr = new ProviderManager(
Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))), Collections.singletonList(createProviderWhichThrows(new BadCredentialsException(""))),
parent); parent);
mgr.setAuthenticationEventPublisher(publisher); mgr.setAuthenticationEventPublisher(publisher);
@ -242,7 +239,7 @@ public class ProviderManagerTests {
public void authenticationExceptionFromParentOverridesPreviousOnes() { public void authenticationExceptionFromParentOverridesPreviousOnes() {
AuthenticationManager parent = mock(AuthenticationManager.class); AuthenticationManager parent = mock(AuthenticationManager.class);
ProviderManager mgr = new ProviderManager( ProviderManager mgr = new ProviderManager(
Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))), Collections.singletonList(createProviderWhichThrows(new BadCredentialsException(""))),
parent); parent);
final Authentication authReq = mock(Authentication.class); final Authentication authReq = mock(Authentication.class);
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class); AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
@ -262,12 +259,11 @@ public class ProviderManagerTests {
} }
@Test @Test
@SuppressWarnings("deprecation")
public void statusExceptionIsPublished() { public void statusExceptionIsPublished() {
AuthenticationManager parent = mock(AuthenticationManager.class); AuthenticationManager parent = mock(AuthenticationManager.class);
final LockedException expected = new LockedException(""); final LockedException expected = new LockedException("");
ProviderManager mgr = new ProviderManager( ProviderManager mgr = new ProviderManager(
Arrays.asList(createProviderWhichThrows(expected)), parent); Collections.singletonList(createProviderWhichThrows(expected)), parent);
final Authentication authReq = mock(Authentication.class); final Authentication authReq = mock(Authentication.class);
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class); AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
mgr.setAuthenticationEventPublisher(publisher); mgr.setAuthenticationEventPublisher(publisher);
@ -303,10 +299,9 @@ public class ProviderManagerTests {
@Test @Test
public void authenticateWhenFailsInParentAndPublishesThenChildDoesNotPublish() { public void authenticateWhenFailsInParentAndPublishesThenChildDoesNotPublish() {
BadCredentialsException badCredentialsExParent = new BadCredentialsException("Bad Credentials in parent"); BadCredentialsException badCredentialsExParent = new BadCredentialsException("Bad Credentials in parent");
ProviderManager parentMgr = new ProviderManager( ProviderManager parentMgr = new ProviderManager(createProviderWhichThrows(badCredentialsExParent));
Collections.singletonList(createProviderWhichThrows(badCredentialsExParent)));
ProviderManager childMgr = new ProviderManager(Collections.singletonList(createProviderWhichThrows( ProviderManager childMgr = new ProviderManager(Collections.singletonList(createProviderWhichThrows(
new BadCredentialsException("Bad Credentials in child"))), parentMgr); new BadCredentialsException("Bad Credentials in child"))), parentMgr);
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class); AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
parentMgr.setAuthenticationEventPublisher(publisher); parentMgr.setAuthenticationEventPublisher(publisher);
@ -348,17 +343,14 @@ public class ProviderManagerTests {
} }
private ProviderManager makeProviderManager() { private ProviderManager makeProviderManager() {
MockProvider provider1 = new MockProvider(); MockProvider provider = new MockProvider();
List<AuthenticationProvider> providers = new ArrayList<>(); return new ProviderManager(provider);
providers.add(provider1);
return new ProviderManager(providers);
} }
// ~ Inner Classes // ~ Inner Classes
// ================================================================================================== // ==================================================================================================
private class MockProvider implements AuthenticationProvider { private static class MockProvider implements AuthenticationProvider {
public Authentication authenticate(Authentication authentication) public Authentication authenticate(Authentication authentication)
throws AuthenticationException { throws AuthenticationException {
if (supports(authentication.getClass())) { if (supports(authentication.getClass())) {
@ -372,7 +364,7 @@ public class ProviderManagerTests {
public boolean supports(Class<?> authentication) { public boolean supports(Class<?> authentication) {
return TestingAuthenticationToken.class.isAssignableFrom(authentication) return TestingAuthenticationToken.class.isAssignableFrom(authentication)
|| UsernamePasswordAuthenticationToken.class || UsernamePasswordAuthenticationToken.class
.isAssignableFrom(authentication); .isAssignableFrom(authentication);
} }
} }
} }