package com.github.timtebeek.rst.controller;

import static org.junit.Assert.assertTrue;

import com.github.timtebeek.rst.MyApp;
import com.github.timtebeek.rst.OAuthHelper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApp.class)
@WebIntegrationTest(randomPort = true)
@Slf4j
public class MyControllerIT {
	@Value("http://localhost:${local.server.port}")
	private String	host;

	@Test
	public void testHelloOAuth2WithRole() {
		OAuth2RestTemplate restTemplate = OAuthHelper.withMyscope(host + "/oauth/token");
		log.info("{}", restTemplate.getAccessToken().getValue());
		ResponseEntity<String> entity = restTemplate.getForEntity(host + "/hello", String.class);
		assertTrue(entity.getStatusCode().is2xxSuccessful());
	}

	@Test(expected = InsufficientScopeException.class)
	public void testHelloOAuth2WithoutRole() {
		OAuth2RestTemplate restTemplate = OAuthHelper.withoutMyscope(host + "/oauth/token");
		log.info("{}", restTemplate.getAccessToken().getValue());
		restTemplate.getForEntity(host + "/hello", String.class);
	}
}

@@ -133,78 +133,66 @@
 ort 
-com.github.timtebeek.rst.OAuthHelper;%0Aimport lombok.extern.slf4j.Slf4j
+lombok.Getter;%0Aimport lombok.Setter;%0Aimport org.junit.Rule
 ;%0Aim
@@ -369,24 +369,79 @@
 figuration;%0A
+import org.springframework.boot.test.TestRestTemplate;%0A
 import org.s
@@ -583,34 +583,47 @@
 .client.
+test.
 OAuth2
-RestTemplate
+ContextConfiguration
 ;%0Aimport
@@ -664,51 +664,221 @@
 h2.c
-ommon.exceptions.InsufficientScopeException
+lient.test.OAuth2ContextSetup;%0Aimport org.springframework.security.oauth2.client.test.RestTemplateHolder;%0Aimport org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails
 ;%0Aim
@@ -946,16 +946,70 @@
 sRunner;
+%0Aimport org.springframework.web.client.RestOperations;
 %0A%0A@RunWi
@@ -1141,41 +1141,109 @@
 e)%0A@
-Slf4j%0Apublic class MyControllerIT
+OAuth2ContextConfiguration(MyDetails.class)%0Apublic class MyControllerIT implements RestTemplateHolder
  %7B%0A%09
@@ -1295,23 +1295,29 @@
 %22)%0A%09
-private 
+@Getter%0A%09
 String%09
+%09%09%09%09%09
 host
@@ -1325,197 +1325,212 @@
 %0A%0A%09@
-Test%0A%09public void testHelloOAuth2WithRole() %7B%0A%09%09OAuth2RestTemplate restTemplate = OAuthHelper.withMyscope(host + %22/oauth/token%22);%0A%09%09log.info(%22%7B%7D%22, restTemplate.getAccessToken().getValue());
+Getter%0A%09@Setter%0A%09RestOperations%09%09%09%09restTemplate%09= new TestRestTemplate();%0A%0A%09@Rule%0A%09public OAuth2ContextSetup%09context%09%09%09= OAuth2ContextSetup.standard(this);%0A%0A%09@Test%0A%09public void testHelloOAuth2WithRole() %7B
 %0A%09%09R
@@ -1560,17 +1560,20 @@
 ntity = 
-r
+getR
 estTempl
@@ -1567,32 +1567,34 @@
  getRestTemplate
+()
 .getForEntity(ho
@@ -1685,173 +1685,189 @@
 ;%0A%09%7D
+%0A%7D
 %0A%0A
-%09@Test(expected = InsufficientScopeException.class)%0A%09public void testHelloOAuth2WithoutRole() %7B%0A%09%09OAuth2RestTemplate restTemplate = OAuthHelper.withoutMyscope(h
+class MyDetails extends ResourceOwnerPasswordResourceDetails %7B%0A%09public MyDetails(final Object obj) %7B%0A%09%09MyControllerIT it = (MyControllerIT) obj;%0A%09%09setAccessTokenUri(it.getH
 ost
+()
  + %22
@@ -1888,123 +1888,84 @@
 ;%0A%09%09
-log.info(%22%7B%7D%22, restTemplate.getAccessToken().getValue());%0A%09%09restTemplate.getForEntity(host + %22/hello%22, String.class
+setClientId(%22myclientwith%22);%0A%09%09setUsername(%22user%22);%0A%09%09setPassword(%22password%22
 );%0A%09

Use @OAuth2ContextConfiguration & @Rule OAuth2ContextSetup