@Test
public void test() throws Exception {
@SuppressWarnings("unchecked")
final OAuth2ConnectionFactory<Object> factory = mock(OAuth2ConnectionFactory.class);
final OAuth2Operations operations = mock(OAuth2Operations.class);
final String serverName = "example.com";
final AccessGrant accessGrant = new AccessGrant("my_token");
final String code = "code";
final Connection<Object> connection = DummyConnection.dummy("provider", "user");
final OAuth2AuthenticationService<Object> authSvc = new OAuth2AuthenticationService<Object>(factory);
authSvc.getReturnToUrlParameters().add("param");
authSvc.afterPropertiesSet();
final MockServletContext context = new MockServletContext();
final MockHttpSession session = new MockHttpSession(context);
// mock definitions
when(factory.getProviderId()).thenReturn(connection.getKey().getProviderId());
when(factory.getOAuthOperations()).thenReturn(operations);
when(factory.createConnection(accessGrant)).thenReturn(connection);
when(
operations.buildAuthenticateUrl(oAuth2Parameters("http://example.com/auth/foo?param=param_value"))).thenReturn(
"http://facebook.com/auth");
when(operations.exchangeForAccess(code, "http://example.com/auth/foo", null)).thenReturn(accessGrant);
// first phase
MockHttpServletRequest request = new MockHttpServletRequest(context, "GET", "/auth/foo");
request.setServerName(serverName);
request.setSession(session);