Skip to content

Commit

Permalink
Add subgroups sorting (keycloak#22295)
Browse files Browse the repository at this point in the history
* Review comments to add a test, update the API description and adjust the map storage.

Closes keycloak#19348

Co-authored-by: Alexander Schwartz <aschwart@redhat.com>
  • Loading branch information
Todor13 and ahus1 authored Aug 7, 2023
1 parent 5f95929 commit dffa7a3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public Stream<GroupModel> getSubGroupsStream() {
}
subGroups.add(subGroup);
}
return subGroups.stream();
return subGroups.stream().sorted(GroupModel.COMPARE_BY_NAME);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @version $Revision: 1 $
*/
@NamedQueries({
@NamedQuery(name="getGroupIdsByParent", query="select u.id from GroupEntity u where u.parentId = :parent"),
@NamedQuery(name="getGroupIdsByParent", query="select u.id from GroupEntity u where u.parentId = :parent order by u.name ASC"),
@NamedQuery(name="getGroupIdsByRealm", query="select u.id from GroupEntity u where u.realm = :realm order by u.name ASC"),
@NamedQuery(name="getGroupIdsByNameContaining", query="select u.id from GroupEntity u where u.realm = :realm and u.name like concat('%',:search,'%') order by u.name ASC"),
@NamedQuery(name="getGroupIdsByNameContainingFromIdList", query="select u.id from GroupEntity u where u.realm = :realm and lower(u.name) like lower(concat('%',:search,'%')) and u.id in :ids order by u.name ASC"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,6 @@ private Stream<GroupModel> getGroupsByParentId(RealmModel realm, String parentId
.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId())
.compare(SearchableFields.PARENT_ID, Operator.EQ, parentId);

return storeWithRealm(realm).read(withCriteria(mcb)).map(entityToAdapterFunc(realm));
return storeWithRealm(realm).read(withCriteria(mcb).orderBy(SearchableFields.NAME, ASCENDING)).map(entityToAdapterFunc(realm));
}
}
2 changes: 2 additions & 0 deletions server-spi/src/main/java/org/keycloak/models/GroupModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ interface GroupPathChangeEvent extends ProviderEvent {

/**
* Returns all sub groups for the parent group as a stream.
* The stream is sorted by the group name.
*
* @return Stream of {@link GroupModel}. Never returns {@code null}.
*/
Stream<GroupModel> getSubGroupsStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import org.keycloak.models.RealmModel;
import org.keycloak.testsuite.model.KeycloakModelTest;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -70,4 +75,28 @@ public void testGroupAttributesSetter() {
});
}

@Test
public void testSubGroupsSorted() {
List<String> subGroups = Arrays.asList("sub-group-1", "sub-group-2", "sub-group-3");

String groupId = withRealm(realmId, (session, realm) -> {
GroupModel group = session.groups().createGroup(realm, "my-group");

subGroups.stream().sorted(Collections.reverseOrder()).forEach(s -> {
GroupModel subGroup = session.groups().createGroup(realm, s);
group.addChild(subGroup);
});

return group.getId();
});
withRealm(realmId, (session, realm) -> {
GroupModel group = session.groups().getGroupById(realm, groupId);

assertThat(group.getSubGroupsStream().map(GroupModel::getName).collect(Collectors.toList()),
contains(subGroups.toArray()));

return null;
});
}

}

0 comments on commit dffa7a3

Please sign in to comment.