// get token response
System.out.println("Getting response");
String tokenResponse = getAuthorizationCodeRequestHandler().getTokenResponseBlocking();
System.out.println("Got response");
AccessTokenResponse accessTokenResponse = getMapper().readValue(tokenResponse, AccessTokenResponse.class);
assertTrue(StringUtils.isNotBlank(accessTokenResponse.getAccessToken()));
assertTrue(StringUtils.isNotBlank(accessTokenResponse.getRefreshToken()));
assertTrue(StringUtils.isNotBlank(accessTokenResponse.getScope()));
assertTrue(StringUtils.isNotBlank(accessTokenResponse.getTokenType()));
assertTrue(accessTokenResponse.getExpiresIn() > 0);
String tokenUrl = String.format("%s/oauth2/token", baseUrl());
final HttpPost tokenRequest = new HttpPost(tokenUrl);
/*
* Now make a request for a new AccessToken based on the refreshToken
*/
String postBody = String.format("grant_type=%s&refresh_token=%s&state=%s",
OAuth2Validator.GRANT_TYPE_REFRESH_TOKEN, accessTokenResponse.getRefreshToken(), "dummy");
tokenRequest.setEntity(new ByteArrayEntity(postBody.getBytes()));
tokenRequest.addHeader("Authorization", AuthorizationCodeTestIT.authorizationBasic(clientId, secret));
tokenRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse tokenHttpResponse = new DefaultHttpClient().execute(tokenRequest);
final InputStream responseContent = tokenHttpResponse.getEntity().getContent();
String responseAsString = IOUtils.toString(responseContent);
AccessTokenResponse refreshTokenResponse = getMapper().readValue(responseAsString, AccessTokenResponse.class);
assertTrue(StringUtils.isNotBlank(refreshTokenResponse.getAccessToken()));
assertTrue(StringUtils.isNotBlank(refreshTokenResponse.getRefreshToken()));
assertTrue(StringUtils.isNotBlank(refreshTokenResponse.getScope()));
assertTrue(StringUtils.isNotBlank(refreshTokenResponse.getTokenType()));
assertTrue(accessTokenResponse.getExpiresIn() > 0);
assertNotSame(refreshTokenResponse.getAccessToken(), accessTokenResponse.getAccessToken());
assertNotSame(refreshTokenResponse.getRefreshToken(), accessTokenResponse.getRefreshToken());
assertEquals(refreshTokenResponse.getScope(), accessTokenResponse.getScope());
assertEquals(refreshTokenResponse.getExpiresIn(), accessTokenResponse.getExpiresIn());
}