package com.github.timtebeek.rst.controller;

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.github.timtebeek.rst.config.OAuthHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
public class MyControllerTest {
	@Autowired
	private WebApplicationContext	context;
	private MockMvc					mvc;

	@Before
	public void before() {
		mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).alwaysDo(print()).build();
	}

	@Autowired
	private OAuthHelper helper;

	@Test
	public void testHelloUserWithRole() throws Exception {
		RequestPostProcessor bearerToken = helper.bearerToken("myclientwith", "user");
		mvc.perform(get("/hello").with(bearerToken)).andExpect(status().isOk()).andExpect(content().string("Hello user"));
	}

	@Test
	public void testHelloAliceWithRole() throws Exception {
		RequestPostProcessor bearerToken = helper.bearerToken("myclientwith", "alice");
		mvc.perform(get("/hello").with(bearerToken)).andExpect(status().isOk()).andExpect(content().string("Hello alice"));
	}

	@Test
	public void testHelloWithoutRole() throws Exception {
		RequestPostProcessor bearerToken = helper.bearerToken("myclientwithout", "user");
		mvc.perform(get("/hello").with(bearerToken)).andExpect(status().isForbidden());
	}

	@Test
	public void testHelloAliceEveDenied() throws Exception {
		RequestPostProcessor bearerToken = helper.bearerToken("myclientwith", "eve");
		mvc.perform(get("/hello").with(bearerToken)).andExpect(status().isForbidden());
	}

}

@@ -61,270 +61,114 @@
 org.
-springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;%0Aimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultHandlers.prin
+mockito.Mockito.when;%0Aimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.ge
 t;%0Ai
@@ -392,24 +392,75 @@
 AuthHelper;%0A
+import com.github.timtebeek.rst.service.MyService;%0A
 import org.j
@@ -628,34 +628,48 @@
 ot.test.
-context.SpringBoot
+autoconfigure.web.servlet.WebMvc
 Test;%0Aim
@@ -707,45 +707,29 @@
 est.
-context.SpringBootTest.WebEnvironment
+mock.mockito.MockBean
 ;%0Aim
@@ -757,21 +757,16 @@
 ork.
-test.
 context.
 juni
@@ -765,25 +765,37 @@
 ext.
-junit4.SpringRunn
+annotation.ComponentScan.Filt
 er;%0A
@@ -825,32 +825,37 @@
 ork.
+con
 te
-st.web.servlet.MockMvc
+xt.annotation.FilterType
 ;%0Aim
@@ -888,47 +888,34 @@
 est.
-web.servlet.request.RequestPostProcesso
+context.junit4.SpringRunne
 r;%0Ai
@@ -961,29 +961,15 @@
 let.
-setup.
 MockMvc
-Builders
 ;%0Aim
@@ -997,235 +997,353 @@
 ork.
-web.context.WebApplicationContext;%0A%0A@RunWith(SpringRunner.class)%0A@SpringBootTest(webEnvironment = WebEnvironment.MOCK)%0Apublic class MyControllerTest %7B%0A%09@Autowired%0A%09private WebApplicationContext%09context;%0A%09private MockMvc%09%09%09%09%09mvc
+test.web.servlet.request.RequestPostProcessor;%0A%0A@RunWith(SpringRunner.class)%0A@WebMvcTest(includeFilters = @Filter(type = FilterType.REGEX, pattern = %22com%5C%5C.github%5C%5C.timtebeek%5C%5C.rst%5C%5C.config%5C%5C..*%22))%0Apublic class MyControllerTest %7B%0A%09@Autowired%0A%09private MockMvc%09%09mvc;%0A%0A%09@Autowired%0A%09private OAuthHelper%09helper;%0A%0A%09@MockBean%0A%09private MyService%09service
 ;%0A%0A%09
@@ -1367,166 +1367,69 @@
 oid 
-before
+setup
 () %7B%0A%09%09
-mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).alwaysDo(print
+when(service.greeting
 ()).
-build();%0A%09%7D%0A%0A%09@Autowired%0A%09private OAuthHelper helper;
+thenReturn(%22Hello %22);%0A%09%7D
 %0A%0A%09@
@@ -2194,238 +2194,6 @@
 %0A%09%7D%0A
-%0A%09@Test%0A%09public void testHelloAliceEveDenied() throws Exception %7B%0A%09%09RequestPostProcessor bearerToken = helper.bearerToken(%22myclientwith%22, %22eve%22);%0A%09%09mvc.perform(get(%22/hello%22).with(bearerToken)).andExpect(status().isForbidden());%0A%09%7D%0A%0A
 %7D%0A

Update MyControllerTest to use @WebMvcTest with includeFilters