Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return set of role labels #21751

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public String getLabel() {
* </p>
*
* @param authRole An {@link AuthRole} (may be {@code null}).
* @return The set of {@link AuthRole}s based on the provided {@link AuthRole}.
* @return The set of {@link AuthRole} labels based on the provided {@link AuthRole}.
*/
public static Set<AuthRole> buildAuthRolesSet(final AuthRole authRole) {
public static Set<String> buildAuthRolesSet(final AuthRole authRole) {
final Set<AuthRole> authRoles = new HashSet<>();

if (authRole != null) {
Expand All @@ -86,6 +86,7 @@ public static Set<AuthRole> buildAuthRolesSet(final AuthRole authRole) {
// Sort final set by descending authority order
return authRoles.stream()
.sorted(Comparator.comparingInt(AuthRole::getAuthority))
.map(role -> role.getLabel())
.collect(Collectors.toCollection(LinkedHashSet::new));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,33 @@ class AuthRoleTest {

@Test
void testBuildingAuthRoleSet() {
final Set<AuthRole> ownerResult = AuthRole.buildAuthRolesSet(AuthRole.OWNER);
final Set<String> ownerResult = AuthRole.buildAuthRolesSet(AuthRole.OWNER);
assertEquals(5, ownerResult.size());
assertEquals(Set.of(AuthRole.OWNER, AuthRole.ADMIN, AuthRole.EDITOR, AuthRole.READER, AuthRole.AUTHENTICATED_USER), ownerResult);
assertEquals(Set.of(AuthRole.OWNER.getLabel(), AuthRole.ADMIN.getLabel(), AuthRole.EDITOR.getLabel(), AuthRole.READER.getLabel(),
AuthRole.AUTHENTICATED_USER.getLabel()), ownerResult);

final Set<AuthRole> adminResult = AuthRole.buildAuthRolesSet(AuthRole.ADMIN);
final Set<String> adminResult = AuthRole.buildAuthRolesSet(AuthRole.ADMIN);
assertEquals(4, adminResult.size());
assertEquals(Set.of(AuthRole.ADMIN, AuthRole.EDITOR, AuthRole.READER, AuthRole.AUTHENTICATED_USER), adminResult);
assertEquals(Set.of(AuthRole.ADMIN.getLabel(), AuthRole.EDITOR.getLabel(), AuthRole.READER.getLabel(), AuthRole.AUTHENTICATED_USER.getLabel()),
adminResult);

final Set<AuthRole> editorResult = AuthRole.buildAuthRolesSet(AuthRole.EDITOR);
final Set<String> editorResult = AuthRole.buildAuthRolesSet(AuthRole.EDITOR);
assertEquals(3, editorResult.size());
assertEquals(Set.of(AuthRole.EDITOR, AuthRole.READER, AuthRole.AUTHENTICATED_USER), editorResult);
assertEquals(Set.of(AuthRole.EDITOR.getLabel(), AuthRole.READER.getLabel(), AuthRole.AUTHENTICATED_USER.getLabel()), editorResult);

final Set<AuthRole> readerResult = AuthRole.buildAuthRolesSet(AuthRole.READER);
final Set<String> readerResult = AuthRole.buildAuthRolesSet(AuthRole.READER);
assertEquals(2, readerResult.size());
assertEquals(Set.of(AuthRole.READER, AuthRole.AUTHENTICATED_USER), readerResult);
assertEquals(Set.of(AuthRole.READER.getLabel(), AuthRole.AUTHENTICATED_USER.getLabel()), readerResult);

final Set<AuthRole> authenticatedUserResult = AuthRole.buildAuthRolesSet(AuthRole.AUTHENTICATED_USER);
final Set<String> authenticatedUserResult = AuthRole.buildAuthRolesSet(AuthRole.AUTHENTICATED_USER);
assertEquals(1, authenticatedUserResult.size());
assertEquals(Set.of(AuthRole.AUTHENTICATED_USER), authenticatedUserResult);
assertEquals(Set.of(AuthRole.AUTHENTICATED_USER.getLabel()), authenticatedUserResult);

final Set<AuthRole> noneResult = AuthRole.buildAuthRolesSet(AuthRole.NONE);
final Set<String> noneResult = AuthRole.buildAuthRolesSet(AuthRole.NONE);
assertEquals(1, noneResult.size());
assertEquals(Set.of(AuthRole.NONE), noneResult);
assertEquals(Set.of(AuthRole.NONE.getLabel()), noneResult);

final Set<AuthRole> nullResult = AuthRole.buildAuthRolesSet(null);
final Set<String> nullResult = AuthRole.buildAuthRolesSet(null);
assertEquals(0, nullResult.size());
}

Expand Down