org.springframework.security.oauth2.client.test
Class OAuth2ContextSetup

java.lang.Object
  extended by org.junit.rules.TestWatchman
      extended by org.springframework.security.oauth2.client.test.OAuth2ContextSetup
All Implemented Interfaces:
org.junit.rules.MethodRule

public class OAuth2ContextSetup
extends org.junit.rules.TestWatchman

A rule that sets up an OAuth2 context for tests and makes the access token available inside a test method. In combination with the OAuth2ContextConfiguration annotation provides a number of different strategies for configuring an OAuth2ProtectedResourceDetails instance that will be used to create the OAuth2 context for tests. Example:

 @OAuth2ContextConfiguration(ResourceOwnerPasswordProtectedResourceDetails.class)
 public class MyIntegrationTests implements RestTemplateHolder {
 
        @Rule
        public OAuth2ContextSetup context = OAuth2ContextSetup.withEnvironment(this, TestEnvironment.instance());
 
        @Test
        public void testSomethingWithClientCredentials() {
                // This call will be authenticated with the client credentials in MyClientDetailsResource
                getRestTemplate().getForObject("http://myserver/resource", String.class);
        }
 
        // This class is used to initialize the OAuth2 context for the test methods.
        static class MyClientDetailsResource extends ResourceOwnerPasswordProtectedResourceDetails {
                public MyClientDetailsResource(Environment environment) {
             ... do stuff with environment to initialize the password credentials
         }
        }
 
 }
 

Author:
Dave Syer
See Also:
OAuth2ContextConfiguration, BeforeOAuth2Context

Method Summary
 org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runners.model.FrameworkMethod method, Object target)
           
 void finished(org.junit.runners.model.FrameworkMethod method)
           
 OAuth2AccessToken getAccessToken()
          Get the current access token.
 AccessTokenRequest getAccessTokenRequest()
           
 OAuth2ClientContext getOAuth2ClientContext()
           
 OAuth2ProtectedResourceDetails getResource()
           
 OAuth2RestTemplate getRestTemplate()
           
 void setAccessTokenProvider(AccessTokenProvider accessTokenProvider)
           
 void setParameters(Map<String,String> parameters)
           
static OAuth2ContextSetup standard(RestTemplateHolder clientHolder)
          Create a new client that knows how to create its protected resource with no externalization help.
 void starting(org.junit.runners.model.FrameworkMethod method)
           
static OAuth2ContextSetup withEnvironment(RestTemplateHolder clientHolder, org.springframework.core.env.Environment environment)
          Create a new client that can inject an Environment into its protected resource details.
static OAuth2ContextSetup withTestAccounts(RestTemplateHolder clientHolder, TestAccounts testAccounts)
          Create a new client that can inject a TestAccounts instance into its protected resource details.
 
Methods inherited from class org.junit.rules.TestWatchman
failed, succeeded
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

withEnvironment

public static OAuth2ContextSetup withEnvironment(RestTemplateHolder clientHolder,
                                                 org.springframework.core.env.Environment environment)
Create a new client that can inject an Environment into its protected resource details.

Parameters:
clientHolder - receives an OAuth2RestTemplate with the authenticated client for the duration of a test
environment - a Spring Environment that can be used to initialize the client
Returns:
a rule that wraps test methods in an OAuth2 context

withTestAccounts

public static OAuth2ContextSetup withTestAccounts(RestTemplateHolder clientHolder,
                                                  TestAccounts testAccounts)
Create a new client that can inject a TestAccounts instance into its protected resource details.

Parameters:
clientHolder - receives an OAuth2RestTemplate with the authenticated client for the duration of a test
testAccounts - a test account generator that can be used to initialize the client
Returns:
a rule that wraps test methods in an OAuth2 context

standard

public static OAuth2ContextSetup standard(RestTemplateHolder clientHolder)
Create a new client that knows how to create its protected resource with no externalization help. Typically it will use resource details which accept an instance of the current test case (downcasting it from Object). For example
 static class MyClientDetailsResource extends ClientCredentialsProtectedResourceDetails {
        public MyClientDetailsResource(Object target) {
             MyIntegrationTests test = (MyIntegrationTests) target;
             ... do stuff with test instance to initialize the client credentials
         }
 }
 

Parameters:
clientHolder - receives an OAuth2RestTemplate with the authenticated client for the duration of a test
Returns:
a rule that wraps test methods in an OAuth2 context

apply

public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
                                               org.junit.runners.model.FrameworkMethod method,
                                               Object target)
Specified by:
apply in interface org.junit.rules.MethodRule
Overrides:
apply in class org.junit.rules.TestWatchman

starting

public void starting(org.junit.runners.model.FrameworkMethod method)
Overrides:
starting in class org.junit.rules.TestWatchman

finished

public void finished(org.junit.runners.model.FrameworkMethod method)
Overrides:
finished in class org.junit.rules.TestWatchman

setAccessTokenProvider

public void setAccessTokenProvider(AccessTokenProvider accessTokenProvider)

setParameters

public void setParameters(Map<String,String> parameters)

getAccessToken

public OAuth2AccessToken getAccessToken()
Get the current access token. Should be available inside a test method as long as a resource has been setup with @OAuth2ContextConfiguration.

Returns:
the current access token initializing it if necessary

getRestTemplate

public OAuth2RestTemplate getRestTemplate()
Returns:
the client template

getResource

public OAuth2ProtectedResourceDetails getResource()
Returns:
the current client resource details

getAccessTokenRequest

public AccessTokenRequest getAccessTokenRequest()
Returns:
the current access token request

getOAuth2ClientContext

public OAuth2ClientContext getOAuth2ClientContext()
Returns:
the current OAuth2 context


Copyright © 2012. All Rights Reserved.