DefaultSessionService implements SessionService { @Override public List<String> getRoles() { Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities(); List<String> roles = new ArrayList<String>(); for (GrantedAuthority grantedAuthority : authorities) { roles.add(grantedAuthority.getAuthority()); } return roles; }  @Override User getUser(); @Override List<String> getRoles(); @Override List<String> getGroups(); @Override Group getDefaultGroup(); @Override void verifyGroup(final String title); @Override Session getSession();  }
@Test public void testGetRoles() throws Exception { String role1 = "ROLE_ADMIN"; String role2 = "ROLE_USER"; List<String> expected = Arrays.asList(role1, role2); Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(new SimpleGrantedAuthority(role1)); authorities.add(new SimpleGrantedAuthority(role2)); when(context.getAuthentication()).thenReturn(authentification); doReturn(authorities).when(authentification).getAuthorities(); List<String> actual = unit.getRoles(); assertEquals(expected, actual); }
@Test public void testGetRolesWithEmptyAuthorities() throws Exception { List<String> expected = Collections.emptyList(); Collection<GrantedAuthority> authorities = Collections.emptyList(); when(context.getAuthentication()).thenReturn(authentification); doReturn(authorities).when(authentification).getAuthorities(); List<String> actual = unit.getRoles(); assertEquals(expected, actual); }