public void authenticate() {
UsersConnectionRepository repo = mock(UsersConnectionRepository.class);
SocialUserDetailsService userDetailsService = mock(SocialUserDetailsService.class);
SocialAuthenticationProvider provider = new SocialAuthenticationProvider(repo, userDetailsService);
DummyUserDetails userDetails = new DummyUserDetails("user1", "pass", "moderator");
Connection<Object> dummyConnection1 = dummyConnection("providerUser1");
Connection<Object> dummyConnection2 = dummyConnection("providerUser2");
Connection<Object> dummyConnection3 = dummyConnection("providerUser3");
// mapping from Connection to userId
Mockito.when(repo.findUserIdsWithConnection(dummyConnection1)).thenReturn(list("user1"));
Mockito.when(repo.findUserIdsWithConnection(dummyConnection2)).thenReturn(list("user1", "user2"));
Mockito.when(repo.findUserIdsWithConnection(dummyConnection3)).thenReturn(list("user3"));
// mapping from userId to userDetails
Mockito.when(userDetailsService.loadUserByUserId("user1")).thenReturn(userDetails);
Mockito.when(userDetailsService.loadUserByUserId("user2")).thenReturn(new DummyUserDetails("user2", "pass", "moderator"));
// success
SocialAuthenticationToken token = new SocialAuthenticationToken(dummyConnection1, null);
SocialAuthenticationToken authToken = (SocialAuthenticationToken) provider.authenticate(token);
assertNotNull(authToken);
assertTrue(token.getPrincipal() == null);
assertEquals(token.getConnection(), dummyConnection1);
assertTrue(authToken.getPrincipal() instanceof UserDetails);
assertEquals(userDetails.getUsername(), authToken.getName());
assertEquals(token.getProviderId(), authToken.getProviderId());
assertEquals(token.getProviderAccountData(), authToken.getProviderAccountData());
// fail - unknown providerUserId
try {