Commit 3c5d531f authored by Matija Obreza's avatar Matija Obreza

Better autocomplete for OAuthClients

parent 4c069b8c
......@@ -80,12 +80,13 @@ public interface OAuthClientDetailsService extends ClientDetailsService {
*/
OAuthClient removeClient(OAuthClient oauthClient);
/**
* Autocomplete OAuth clients by title.
*
* @param title the title
* @return list of auth clients
*/
List<OAuthClient> autocompleteClients(String title);
/**
* Autocomplete OAuth clients by title.
*
* @param title the title
* @param limit maximum number of results
* @return list of auth clients
*/
List<OAuthClient> autocompleteClients(String title, int limit);
}
......@@ -411,8 +411,8 @@ public class OAuthServiceImpl implements OAuthClientDetailsService, OAuthTokenSt
*/
@Override
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(final String clientId, final String username) {
return accessTokenRepository.findByClientIdAndUsername(clientId, username).stream().filter(at -> at != null).map(at -> deserializeAccessToken(at.getToken())).collect(Collectors
.toList());
return accessTokenRepository.findByClientIdAndUsername(clientId, username).stream().filter(at -> at != null).map(at -> deserializeAccessToken(at.getToken())).collect(
Collectors.toList());
}
/*
......@@ -638,9 +638,12 @@ public class OAuthServiceImpl implements OAuthClientDetailsService, OAuthTokenSt
return oauthClientRepository.save(client);
}
/* (non-Javadoc)
* @see org.genesys.blocks.oauth.service.OAuthClientDetailsService#addClient(org.genesys.blocks.oauth.model.OAuthClient)
/*
* (non-Javadoc)
* @see
* org.genesys.blocks.oauth.service.OAuthClientDetailsService#addClient(org.
* genesys.blocks.oauth.model.OAuthClient)
*/
@Override
@Transactional
......@@ -671,12 +674,18 @@ public class OAuthServiceImpl implements OAuthClientDetailsService, OAuthTokenSt
}
@Override
public List<OAuthClient> autocompleteClients(final String title) {
if (StringUtils.isBlank(title) || title.length() < 4)
public List<OAuthClient> autocompleteClients(final String term, int limit) {
if (StringUtils.isBlank(term) || term.length() < 1)
return Collections.emptyList();
LOG.debug("Autocomplete for={}", title);
Predicate predicate = QOAuthClient.oAuthClient.title.startsWithIgnoreCase(title);
return oauthClientRepository.findAll(predicate, new PageRequest(0, 10, new Sort("title"))).getContent();
LOG.debug("Autocomplete for={}", term);
Predicate predicate = QOAuthClient.oAuthClient.title.startsWithIgnoreCase(term)
// clientId
.or(QOAuthClient.oAuthClient.clientId.startsWithIgnoreCase(term))
// description contains
.or(QOAuthClient.oAuthClient.description.contains(term));
return oauthClientRepository.findAll(predicate, new PageRequest(0, Math.min(100, limit), new Sort("title"))).getContent();
}
}
......@@ -47,8 +47,8 @@ public class OAuthClientTest extends AbstractRestTest {
assertThat("Could not load persisted OAuthClient", storedClient, not(nullValue()));
assertThat("OAuthClient#id does not match", storedClient.getId(), equalTo(client.getId()));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(client.getTitle().substring(0, 10)), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(client.getTitle().substring(0, 5)), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(client.getTitle().substring(0, 10), 10), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(client.getTitle().substring(0, 5), 10), hasSize(1));
}
@Test
......@@ -62,7 +62,7 @@ public class OAuthClientTest extends AbstractRestTest {
assertThat("OAuthClient#clientId must not be updated", updatedClient.getClientId(), is(client.getClientId()));
assertThat("OAuthClient#clientSecret must not be updated", updatedClient.getClientSecret(), is(client.getClientSecret()));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(updatedClient.getTitle().substring(0, 10)), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(updatedClient.getTitle().substring(0, 5)), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(updatedClient.getTitle().substring(0, 10), 10), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(updatedClient.getTitle().substring(0, 5), 10), hasSize(1));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment