package de.digitalcollections.blueprints.crud.frontend.webapp.controller;

import de.digitalcollections.blueprints.crud.config.SpringConfigSecurity;
import de.digitalcollections.blueprints.crud.config.SpringConfigWeb;
import de.digitalcollections.blueprints.crud.frontend.webapp.config.SpringConfigBackendForTest;
import de.digitalcollections.blueprints.crud.frontend.webapp.config.SpringConfigBusinessForTest;
import javax.annotation.Resource;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * unit testing PageController
 *
 * see also: http://zjhzxhz.com/2014/05/unit-testing-of-spring-mvc-controllers/,
 * http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-security/
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {SpringConfigWeb.class, SpringConfigSecurity.class, SpringConfigBusinessForTest.class,
  SpringConfigBackendForTest.class})
public class UserControllerTest {

  private MockMvc mockMvc;

  @Autowired
  private WebApplicationContext wac;

  @Resource
  private FilterChainProxy springSecurityFilterChain;

  @Test
  public void resourceRequest() throws Exception {
    this.mockMvc.perform(MockMvcRequestBuilders.get("/css/main.css"))
            .andDo(MockMvcResultHandlers.print()) // print the request/response in the console
            //                .andExpect(MockMvcResultMatchers.content().contentType("text/css"))
            .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("background")));
  }

  @Before
  public void setUp() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
            .addFilter(springSecurityFilterChain)
            .build();
  }

  @After
  public void tearDown() {
  }

  @Test
  public void testApiCall() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.get("/api/users"))
            .andExpect(status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
//                .andExpect(redirectedUrl("/index.htm"));
  }
}

@@ -485,21 +485,37 @@
 g.junit.
+jupiter.api.
 After
+Each
 ;%0Aimport
@@ -525,22 +525,38 @@
 g.junit.
+jupiter.api.
 Before
+Each
 ;%0Aimport
@@ -566,16 +566,28 @@
 g.junit.
+jupiter.api.
 Test;%0Aim
@@ -605,18 +605,36 @@
 nit.
-runner.Run
+jupiter.api.extension.Extend
 With
@@ -910,33 +910,32 @@
 unit
-4.SpringJUnit4ClassRunner
+.jupiter.SpringExtension
 ;%0Aim
@@ -1278,96 +1278,8 @@
 rs;%0A
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;%0A
 impo
@@ -1404,16 +1404,105 @@
 ntext;%0A%0A
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;%0A%0A
 /**%0A * u
@@ -1738,19 +1738,22 @@
 y/%0A */%0A@
-Run
+Extend
 With(Spr
@@ -1759,25 +1759,17 @@
 ring
-JUnit4ClassRunner
+Extension
 .cla
@@ -2569,16 +2569,20 @@
  @Before
+Each
 %0A  publi
@@ -2743,16 +2743,16 @@
  %7D%0A%0A
-
   @After
 %0A  p
@@ -2747,16 +2747,20 @@
   @After
+Each
 %0A  publi

migrate unit tests to JUnit5