DefaultSessionService implements SessionService { @Override public Group getDefaultGroup() { Group defaultGroup = null; User user = getUser(); for (UserGroup userGroup : user.getUserGroups()) { if (userGroup.getMajor()) { defaultGroup = userGroup.getGroup(); break; } } return defaultGroup; }  @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 testGetDefaultGroup() throws Exception { String groupTitle1 = "developers"; Group group1 = new Group(); group1.setTitle(groupTitle1); String groupTitle2 = "users"; Group group2 = new Group(); group2.setTitle(groupTitle2); UserGroup userGroup1 = new UserGroup(); userGroup1.setGroup(group1); userGroup1.setMajor(true); UserGroup userGroups2 = new UserGroup(); userGroups2.setGroup(group2); userGroups2.setMajor(false); List<Group> groups = Arrays.asList(group1, group2); List<UserGroup> userGroups = Arrays.asList(userGroup1, userGroups2); User user = new User(); user.setGroups(groups); user.setUserGroups(userGroups); Session session = new Session(); session.setUser(user); when(context.getAuthentication()).thenReturn(authentification); when(authentification.getDetails()).thenReturn(session); Group actaul = unit.getDefaultGroup(); verify(context).getAuthentication(); verify(authentification).getDetails(); assertEquals(group1, actaul); }
@Test public void testGetDefaulGroupWithEmptyUserGroups() throws Exception { List<UserGroup> userGroups = Collections.emptyList(); User user = new User(); user.setUserGroups(userGroups); Session session = new Session(); session.setUser(user); when(context.getAuthentication()).thenReturn(authentification); when(authentification.getDetails()).thenReturn(session); Group actaul = unit.getDefaultGroup(); verify(context).getAuthentication(); verify(authentification).getDetails(); assertEquals(null, actaul); }