ClientResource extends AbstractResource { protected String generateClientId(Client client) { String clientId = sanitizeClientName(client.getName()); if (clientRepository.findByClientId(clientId) != null) { String baseClientId = clientId; int i = 2; do { clientId = baseClientId + (i++); } while (clientRepository.findByClientId(clientId) != null); } return clientId; }  @GET Response getAll(@Context HttpServletRequest request,
                         @PathParam("resourceServerId") Long resourceServerId); @GET @Path("/{clientId}") Response getById(@Context HttpServletRequest request,
                          @PathParam("resourceServerId") Long resourceServerId,
                          @PathParam("clientId") Long id); @PUT Response put(@Context HttpServletRequest request,
                      @PathParam("resourceServerId") Long resourceServerId, Client client); @DELETE @Path("/{clientId}") Response delete(@Context HttpServletRequest request,
                         @PathParam("clientId") Long id,
                         @PathParam("resourceServerId") Long resourceServerId); @POST @Path("/{clientId}") Response post(@Valid Client newOne, @PathParam("clientId") Long id,
                       @Context HttpServletRequest request,
                       @PathParam("resourceServerId") Long resourceServerId
  );  }
@Test public void uniqueClientId() { final Client existingClient = new Client(); when(clientRepository.findByClientId(anyString())).thenReturn( existingClient, existingClient, existingClient, existingClient, existingClient, null); Client newClient = new Client(); newClient.setName("myname"); String clientId = clientResource.generateClientId(newClient); LOG.debug("client id generated: " + clientId); assertEquals("myname6", clientId); }