DefaultSessionService implements SessionService { @Override public void verifyGroup(final String title) { List<String> groups = getGroups(); Boolean result = false; for (String groupTitle : groups) { if (groupTitle.equals(title)) { result = true; break; } } if (!result) { throw new AccessDeniedException("Access to appropriate group is denied to current user"); } }  @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 testVerifyGroup() throws Exception { String title = "developers"; Group group1 = new Group(); group1.setTitle(title); String groupTitle2 = "users"; Group group2 = new Group(); group2.setTitle(groupTitle2); List<Group> groups = Arrays.asList(group1, group2); User user = new User(); user.setGroups(groups); Session session = new Session(); session.setUser(user); when(context.getAuthentication()).thenReturn(authentification); when(authentification.getDetails()).thenReturn(session); unit.verifyGroup(title); verify(context).getAuthentication(); verify(authentification).getDetails(); }
@Test(expected = AccessDeniedException.class) public void testVerifyGroupWithException() throws Exception { String title = "developers"; String groupTitle2 = "users"; Group group2 = new Group(); group2.setTitle(groupTitle2); List<Group> groups = Arrays.asList(group2); User user = new User(); user.setGroups(groups); Session session = new Session(); session.setUser(user); when(context.getAuthentication()).thenReturn(authentification); when(authentification.getDetails()).thenReturn(session); unit.verifyGroup(title); verify(context).getAuthentication(); verify(authentification).getDetails(); }