HttpUserRolesProvider implements UserRolesProvider { @Override public List<String> getUserRoles(final String uid, final String realm, final String accessToken) { Assert.hasText(uid, "uid should never be null or empty"); Assert.hasText(uid, "accessToken should never be null or empty"); final List<String> rolesList = new ArrayList<>(); if (EMPLOYEES.equals(realm)) { final HttpEntity<String> entity = getHttpEntity(accessToken); final ResponseEntity<List<Role>> response = restTemplate.exchange(roleInfoUri, GET, entity, ROLES, uid); final List<Role> roleList = response.getBody(); if (roleList == null) { logger.warn("No roles can be extracted for uid ({}) !", uid); return Collections.emptyList(); } for (final Role role : roleList) { if (rolePrefix != null) { rolesList.add(rolePrefix + ROLE_SEPARATOR + role.getName()); } else { rolesList.add(role.getName()); } } } else { rolesList.add(DEFAULT_ROLE); } return rolesList; } HttpUserRolesProvider(final String roleInfoUri);  HttpUserRolesProvider(final String roleInfoUri, final String rolePrefix);  HttpUserRolesProvider(final String roleInfoUri, final String rolePrefix, final RestTemplate restTemplate); @Override List<String> getUserRoles(final String uid, final String realm, final String accessToken); static final ParameterizedTypeReference<List<Role>> ROLES; static final String EMPLOYEES; static final String DEFAULT_ROLE; }
@Test public void testGetUserRoles() throws Exception { final HttpUserRolesProvider unit = new HttpUserRolesProvider(ROLE_INFO_URL, ROLE_PREFIX, restTemplate); final List<String> roles = unit.getUserRoles(USER_UID, EMPLOYEES, "access_token"); assertThat(roles, hasSize(usersRoles.size())); for (final Role role : usersRoles) { assertThat(roles, hasItem(ROLE_PREFIX + "_" + role.getName())); } }
@Test public void testGetUserRolesWithoutPrefix() throws Exception { final HttpUserRolesProvider unit = new HttpUserRolesProvider(ROLE_INFO_URL, null, restTemplate); final List<String> roles = unit.getUserRoles(USER_UID, EMPLOYEES, "access_token"); assertThat(roles, hasSize(usersRoles.size())); for (final Role role : usersRoles) { assertThat(roles, hasItem(role.getName())); } }