GroupPredicate implements Predicate<Authorizable> { @Override public boolean apply(@Nullable Authorizable authorizable) { String id = getID(authorizable); if (id != null) { if (memberIds.contains(id)) { return true; } else { while (membersIterator.hasNext()) { String memberId = getID(membersIterator.next()); if (memberId != null) { memberIds.add(memberId); if (memberId.equals(id)) { return true; } } } } } return false; } GroupPredicate(@NotNull UserManager userManager, @NotNull String groupId, boolean declaredMembersOnly); @Override boolean apply(@Nullable Authorizable authorizable);  }
@Test public void testUnknownGroupId() throws Exception { String id = "unknownGroupId"; assertNull(userManager.getAuthorizable(id)); GroupPredicate gp = new GroupPredicate(userManager, id, false); assertFalse(gp.apply(testUser)); assertFalse(gp.apply(testGroup)); assertFalse(gp.apply(null)); }
@Test public void testUserId() throws Exception { GroupPredicate gp = new GroupPredicate(userManager, testUser.getID(), false); assertFalse(gp.apply(testUser)); assertFalse(gp.apply(testGroup)); assertFalse(gp.apply(null)); }
@Test public void testDeclaredMembersOnly() throws Exception { GroupPredicate gp = new GroupPredicate(userManager, testGroup.getID(), true); assertTrue(gp.apply(testMember)); assertFalse(gp.apply(testUser)); assertFalse(gp.apply(testGroup)); assertFalse(gp.apply(null)); }
@Test public void testInheritedMembers() throws Exception { GroupPredicate gp = new GroupPredicate(userManager, testGroup.getID(), false); assertTrue(gp.apply(testMember)); assertTrue(gp.apply(testUser)); assertFalse(gp.apply(testGroup)); assertFalse(gp.apply(null)); }
@Test public void testApplyTwice() throws Exception { GroupPredicate gp = new GroupPredicate(userManager, testGroup.getID(), true); gp.apply(testMember); assertTrue(gp.apply(testMember)); }
@Test public void testApplyTwiceNotMember() throws Exception { GroupPredicate gp = new GroupPredicate(userManager, testGroup.getID(), true); gp.apply(testUser); assertFalse(gp.apply(testUser)); }
@Test public void testGetIdFails() throws Exception { GroupPredicate gp = new GroupPredicate(userManager, testGroup.getID(), true); Authorizable a = mock(Authorizable.class); when(a.getID()).thenThrow(new RepositoryException()); assertFalse(gp.apply(a)); }
@Test public void testGetMemberIdFails() throws Exception { Authorizable member = when(mock(Authorizable.class).getID()).thenThrow(new RepositoryException()).getMock(); Group g = when(mock(Group.class).getDeclaredMembers()).thenReturn(Iterators.singletonIterator(member)).getMock(); when(g.isGroup()).thenReturn(true); UserManager uMgr = when(mock(UserManager.class).getAuthorizable("g")).thenReturn(g).getMock(); Authorizable a = when(mock(Authorizable.class).getID()).thenReturn("a").getMock(); GroupPredicate gp = new GroupPredicate(uMgr, "g", true); assertFalse(gp.apply(a)); }