KEYCLOAK-11888 Fix inconsistent pagination of groups by ordering the results of 'getTopLevelGroupIds' query

This commit is contained in:
Patrick Teubner 2019-11-04 10:20:15 +01:00 committed by Stian Thorgersen
parent ba56ff778e
commit b3d87b52c2
2 changed files with 55 additions and 1 deletions

View file

@ -30,7 +30,7 @@ import java.util.Collection;
@NamedQueries({
@NamedQuery(name="getGroupIdsByParent", query="select u.id from GroupEntity u where u.parent = :parent"),
@NamedQuery(name="getGroupIdsByNameContaining", query="select u.id from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:search,'%') order by u.name ASC"),
@NamedQuery(name="getTopLevelGroupIds", query="select u.id from GroupEntity u where u.parent is null and u.realm.id = :realm"),
@NamedQuery(name="getTopLevelGroupIds", query="select u.id from GroupEntity u where u.parent is null and u.realm.id = :realm order by u.name ASC"),
@NamedQuery(name="getGroupCount", query="select count(u) from GroupEntity u where u.realm.id = :realm"),
@NamedQuery(name="getTopLevelGroupCount", query="select count(u) from GroupEntity u where u.realm.id = :realm and u.parent is null")
})

View file

@ -17,6 +17,7 @@
package org.keycloak.testsuite.admin.group;
import com.google.common.collect.Comparators;
import org.junit.Assert;
import org.junit.Test;
import org.keycloak.admin.client.resource.GroupResource;
@ -54,6 +55,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@ -768,6 +770,58 @@ public class GroupTest extends AbstractGroupTest {
assertEquals(new Long(allGroups.size() + 1), realm.groups().count(false).get("count"));
}
@Test
public void orderGroupsByName() throws Exception {
RealmResource realm = this.adminClient.realms().realm("test");
// Clean up all test groups
for (GroupRepresentation group : realm.groups().groups()) {
GroupResource resource = realm.groups().group(group.getId());
resource.remove();
assertAdminEvents.assertEvent("test", OperationType.DELETE, AdminEventPaths.groupPath(group.getId()), ResourceType.GROUP);
}
// Create two pages worth of groups in a random order
List<GroupRepresentation> testGroups = new ArrayList<>();
for (int i = 0; i < 40; i++) {
GroupRepresentation group = new GroupRepresentation();
group.setName("group" + i);
testGroups.add(group);
}
Collections.shuffle(testGroups);
for (GroupRepresentation group : testGroups) {
group = createGroup(realm, group);
}
// Groups should be ordered by name
Comparator<GroupRepresentation> compareByName = Comparator.comparing(GroupRepresentation::getName);
// Assert that all groups are returned in order
List<GroupRepresentation> allGroups = realm.groups().groups();
assertEquals(40, allGroups.size());
assertTrue(Comparators.isInStrictOrder(allGroups, compareByName));
// Assert that pagination results are returned in order
List<GroupRepresentation> firstPage = realm.groups().groups(0, 20);
assertEquals(20, firstPage.size());
assertTrue(Comparators.isInStrictOrder(firstPage, compareByName));
List<GroupRepresentation> secondPage = realm.groups().groups(20, 20);
assertEquals(20, secondPage.size());
assertTrue(Comparators.isInStrictOrder(secondPage, compareByName));
// Check that the ordering of groups across multiple pages is correct
// Since the individual pages are ordered it is sufficient to compare
// every group from the first page to the first group of the second page
GroupRepresentation firstGroupOnSecondPage = secondPage.get(0);
for (GroupRepresentation firstPageGroup : firstPage) {
int comparisonResult = compareByName.compare(firstPageGroup, firstGroupOnSecondPage);
assertTrue(comparisonResult < 0);
}
}
@Test
public void testBriefRepresentationOnGroupMembers() {
RealmResource realm = adminClient.realms().realm("test");