DefaultAuthenticationProvider implements AuthenticationProvider { @Override @Transactional public Authentication authenticate(final Authentication authentication) throws AuthenticationException { String login = authentication.getName(); String password = authentication.getCredentials().toString(); LOG.info(MessageFormat.format("Starting to authenticate user with login: {0}", login)); return getAuthentication(login, password); }  @Override boolean supports(final Class<?> clazz); @Override @Transactional Authentication authenticate(final Authentication authentication);  }
@Test public void testAuthentificate() throws Exception { String login = "login"; String password = "password"; String roleCode1 = "ROLE_ADMIN"; Role role = new Role(); role.setTitle(roleCode1); UserRole userRole1 = new UserRole(); userRole1.setRole(role); List<UserRole> roles = Arrays.asList(userRole1); List<UserGroup> userGroups = Collections.emptyList(); User user = new User(); user.setLogin(login); user.setPassword(password); user.setUserRoles(roles); user.setUserGroups(userGroups); Collection<GrantedAuthority> authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority(roleCode1)); Session session = new Session(); session.setUser(user); UsernamePasswordAuthenticationToken expected = new UsernamePasswordAuthenticationToken(login, password, authorities); expected.setDetails(session); when(authentication.getName()).thenReturn(login); when(authentication.getCredentials()).thenReturn(password); when(userService.getUserByLogin(anyString())).thenReturn(user); Authentication actual = unit.authenticate(authentication); verify(userService).getUserByLogin(login); verify(authentication).getName(); verify(authentication).getCredentials(); assertEquals(expected, actual); }
@Test(expected = BadCredentialsException.class) public void testAuthentificateWithException() throws Exception { String login = "login"; String credentials = "password"; User user = new User(); user.setLogin(login); user.setPassword(credentials); when(authentication.getName()).thenReturn(login); when(authentication.getCredentials()).thenReturn(credentials); when(userService.getUserByLogin(anyString())).thenThrow(new BadCredentialsException("")); unit.authenticate(authentication); }