@Test
public void testRetrieveAccessToken() {
//Test approved request
OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("id", true);
OAuth2Authentication authentication = new OAuth2Authentication(storedOAuth2Request, new TestAuthentication("test2", true));
DefaultOAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken("testToken");
expectedOAuth2AccessToken.setExpiration(new Date(Long.MAX_VALUE-1));
getTokenStore().storeAccessToken(expectedOAuth2AccessToken, authentication);
//Test unapproved request
storedOAuth2Request = RequestTokenFactory.createOAuth2Request("id", false);
authentication = new OAuth2Authentication(storedOAuth2Request, new TestAuthentication("test2", true));
OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().getAccessToken(authentication);
assertEquals(expectedOAuth2AccessToken, actualOAuth2AccessToken);
assertEquals(authentication.getUserAuthentication(), getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()).getUserAuthentication());
// The authorizationRequest does not match because it is unapproved, but the token was granted to an approved request
assertFalse(storedOAuth2Request.equals(getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()).getOAuth2Request()));
actualOAuth2AccessToken = getTokenStore().getAccessToken(authentication);
assertEquals(expectedOAuth2AccessToken, actualOAuth2AccessToken);
getTokenStore().removeAccessToken(expectedOAuth2AccessToken);
assertNull(getTokenStore().readAccessToken("testToken"));
assertNull(getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()));
assertNull(getTokenStore().getAccessToken(authentication));
}