@Test
  public void authenticate() {
    RestAuthenticationToken authToken = new RestAuthenticationToken("token");
    assertFalse(authToken.isAuthenticated());

    when(tokenService.findUserByToken("token"))
        .thenReturn(
            new User("username", "password", Arrays.asList(new SimpleGrantedAuthority("admin"))));

    Authentication auth = tokenAuthenticationProvider.authenticate(authToken);
    assertNotNull(auth);
    assertTrue(auth.isAuthenticated());
    assertEquals(auth.getName(), "username");
    assertEquals(auth.getAuthorities().size(), 1);
    assertEquals(auth.getAuthorities().iterator().next().getAuthority(), "admin");
  }
 @Test(expectedExceptions = AuthenticationException.class)
 public void authenticateInvalidToken() {
   when(tokenService.findUserByToken("token"))
       .thenThrow(new UnknownTokenException("Invalid token"));
   tokenAuthenticationProvider.authenticate(new RestAuthenticationToken("token"));
 }