ExternalGroupPrincipalProvider implements PrincipalProvider, ExternalIdentityConstants { @NotNull @Override public Iterator<? extends Principal> findPrincipals(@Nullable String nameHint, int searchType) { if (PrincipalManager.SEARCH_TYPE_NOT_GROUP != searchType) { Result result = findPrincipals(Strings.nullToEmpty(nameHint), false); if (result != null) { return Iterators.filter(new GroupPrincipalIterator(nameHint, result), Predicates.notNull()); } } return Collections.emptyIterator(); } ExternalGroupPrincipalProvider(@NotNull Root root, @NotNull UserConfiguration uc,
                                   @NotNull NamePathMapper namePathMapper,
                                   @NotNull Map<String, String[]> autoMembershipMapping); @Override Principal getPrincipal(@NotNull String principalName); @NotNull @Override Set<Principal> getMembershipPrincipals(@NotNull Principal principal); @NotNull @Override Set<? extends Principal> getPrincipals(@NotNull String userID); @NotNull @Override Iterator<? extends Principal> findPrincipals(@Nullable String nameHint, int searchType); @NotNull @Override Iterator<? extends Principal> findPrincipals(int searchType); @NotNull @Override Iterator<? extends Principal> findPrincipals(@Nullable String nameHint, boolean fullText, int searchType,
            long offset, long limit);  }
@Test public void testFindPrincipalsByHintTypeNotGroup() { Iterator<? extends Principal> iter = principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_NOT_GROUP); assertSame(Collections.emptyIterator(), iter); Iterator<? extends Principal> iter2 = principalProvider.findPrincipals("a", false, PrincipalManager.SEARCH_TYPE_NOT_GROUP, 0, -1); assertSame(Collections.emptyIterator(), iter2); }
@Test public void testFindPrincipalsByHintTypeGroup() { Set<? extends Principal> expected = ImmutableSet.of(new PrincipalImpl("a")); Set<? extends Principal> res = ImmutableSet .copyOf(principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_GROUP)); assertEquals(expected, res); Set<? extends Principal> res2 = ImmutableSet .copyOf(principalProvider.findPrincipals("a", false, PrincipalManager.SEARCH_TYPE_GROUP, 0, -1)); assertEquals(expected, res2); }
@Test public void testFindPrincipalsByHintTypeAll() { Set<? extends Principal> expected = ImmutableSet.of(new PrincipalImpl("a")); Set<? extends Principal> res = ImmutableSet .copyOf(principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_ALL)); assertEquals(expected, res); Set<? extends Principal> res2 = ImmutableSet .copyOf(principalProvider.findPrincipals("a", false, PrincipalManager.SEARCH_TYPE_ALL, 0, -1)); assertEquals(expected, res2); }
@Test public void testFindPrincipalsContainingUnderscore() throws Exception { ExternalUser externalUser = idp.getUser(TestIdentityProvider.ID_WILDCARD_USER); sync(externalUser); Set<? extends Principal> expected = ImmutableSet.of(new PrincipalImpl("_gr_u_")); Set<? extends Principal> res = ImmutableSet .copyOf(principalProvider.findPrincipals("_", PrincipalManager.SEARCH_TYPE_ALL)); assertEquals(expected, res); Set<? extends Principal> res2 = ImmutableSet .copyOf(principalProvider.findPrincipals("_", false, PrincipalManager.SEARCH_TYPE_ALL, 0, -1)); assertEquals(expected, res2); }
@Test public void testFindPrincipalsContainingPercentSign() throws Exception { ExternalUser externalUser = idp.getUser(TestIdentityProvider.ID_WILDCARD_USER); sync(externalUser); Set<? extends Principal> expected = ImmutableSet.of( new PrincipalImpl("g%r%")); Set<? extends Principal> res = ImmutableSet.copyOf(principalProvider.findPrincipals("%", PrincipalManager.SEARCH_TYPE_ALL)); assertEquals(expected, res); Set<? extends Principal> res2 = ImmutableSet .copyOf(principalProvider.findPrincipals("%", false, PrincipalManager.SEARCH_TYPE_ALL, 0, -1)); assertEquals(expected, res2); }
@Test public void testFindPrincipalsByTypeNotGroup() { Iterator<? extends Principal> iter = principalProvider.findPrincipals(PrincipalManager.SEARCH_TYPE_NOT_GROUP); assertSame(Collections.emptyIterator(), iter); }
@Test public void testFindPrincipalsByTypeGroup() throws Exception { Set<? extends Principal> res = ImmutableSet.copyOf(principalProvider.findPrincipals(PrincipalManager.SEARCH_TYPE_GROUP)); assertEquals(getExpectedAllSearchResult(USER_ID), res); Set<? extends Principal> res2 = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, -1)); assertEquals(getExpectedAllSearchResult(USER_ID), res2); }
@Test public void testFindPrincipalsByTypeAll() throws Exception { Set<? extends Principal> res = ImmutableSet.copyOf(principalProvider.findPrincipals(PrincipalManager.SEARCH_TYPE_ALL)); assertEquals(getExpectedAllSearchResult(USER_ID), res); }
@Test public void testFindPrincipalsFiltersDuplicates() throws Exception { ExternalGroup gr = idp.getGroup("a"); ExternalUser otherUser = new TestUser("anotherUser", ImmutableSet.of(gr.getExternalId())); sync(otherUser); Set<Principal> expected = new HashSet<>(); expected.add(new PrincipalImpl(gr.getPrincipalName())); long depth = syncConfig.user().getMembershipNestingDepth(); if (depth > 1) { collectExpectedPrincipals(expected, gr.getDeclaredGroups(), --depth); } Iterator<? extends Principal> res = principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_ALL); assertTrue(res.hasNext()); assertEquals(expected, ImmutableSet.copyOf(res)); Iterator<? extends Principal> res2 = principalProvider.findPrincipals("a", false, PrincipalManager.SEARCH_TYPE_ALL, 0, -1); assertTrue(res2.hasNext()); assertEquals(expected, ImmutableSet.copyOf(res2)); }
@Test public void testFindPrincipalsSorted() throws Exception { List<Principal> in = Arrays.asList(new PrincipalImpl("p3"), new PrincipalImpl("p1"), new PrincipalImpl("p2")); ExternalGroupPrincipalProvider p = new ExternalGroupPrincipalProvider(root, getSecurityProvider().getConfiguration(UserConfiguration.class), NamePathMapper.DEFAULT, ImmutableMap.of(idp.getName(), getAutoMembership())) { @NotNull @Override public Iterator<? extends Principal> findPrincipals(@Nullable String nameHint, int searchType) { return in.iterator(); } }; List<Principal> out = ImmutableList.copyOf(p.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_ALL, 0, -1)); Collections.sort(in, Comparator.comparing(Principal::getName)); assertEquals(in, out); }
@Test public void testFindPrincipalsWithOffset() throws Exception { Set<Principal> all = getExpectedAllSearchResult(USER_ID); long offset = 2; long expectedSize = (all.size() <= offset) ? 0 : all.size()-offset; Set<? extends Principal> result = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, offset, -1)); assertEquals(expectedSize, result.size()); }
@Test public void testFindPrincipalsWithOffsetEqualsResultSize() throws Exception { Set<Principal> all = getExpectedAllSearchResult(USER_ID); Set<? extends Principal> result = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, all.size(), -1)); assertTrue(result.isEmpty()); }
@Test public void testFindPrincipalsWithOffsetExceedsResultSize() throws Exception { Set<Principal> all = getExpectedAllSearchResult(USER_ID); Set<? extends Principal> result = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, all.size()+1, -1)); assertTrue(result.isEmpty()); }
@Test public void testFindPrincipalsWithLimit() throws Exception { Set<Principal> all = getExpectedAllSearchResult(USER_ID); Set<? extends Principal> result = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, 1)); assertEquals(1, result.size()); }
@Test public void testFindPrincipalsWithLimitExceedsResultSize() throws Exception { Set<Principal> all = getExpectedAllSearchResult(USER_ID); Set<? extends Principal> result = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, all.size()+1)); assertEquals(all, result); }
@Test public void testFindPrincipalsWithZeroLimit() throws Exception { Set<? extends Principal> result = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, 0)); assertTrue(result.isEmpty()); }
@Test public void testFindPrincipalsWithOffsetAndLimit() throws Exception { Set<Principal> all = getExpectedAllSearchResult(USER_ID); long offset = all.size()-1; long limit = all.size(); Set<? extends Principal> result = ImmutableSet.copyOf(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, offset, limit)); assertEquals(1, result.size()); }