From 353c566c9aaf82fee80ce7ee0bf7dd4af482b00d Mon Sep 17 00:00:00 2001
From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de>
Date: Mon, 19 May 2014 17:06:11 +0200
Subject: [PATCH] Some more reworked unit tests

---
 .../controller/LoginControllerTest.java       | 115 ++++-------------
 .../controller/SessionControllerTest.java     | 122 +++++-------------
 .../controller/WelcomeControllerTest.java     |  71 ----------
 3 files changed, 58 insertions(+), 250 deletions(-)
 delete mode 100644 src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java

diff --git a/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java b/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
index 65b8ffa4a..2b2287dd4 100644
--- a/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2012 THM webMedia
- * 
+ *
  * This file is part of ARSnova.
  *
  * ARSnova is free software: you can redistribute it and/or modify
@@ -19,139 +19,78 @@
 package de.thm.arsnova.controller;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-import javax.inject.Inject;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
-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.context.ApplicationContext;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.springframework.mock.web.MockHttpServletResponse;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.web.servlet.HandlerAdapter;
-import org.springframework.web.servlet.ModelAndView;
-import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
-import org.springframework.web.servlet.view.RedirectView;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
 
-import de.thm.arsnova.dao.StubDatabaseDao;
 import de.thm.arsnova.services.StubUserService;
 
 @RunWith(SpringJUnit4ClassRunner.class)
+@WebAppConfiguration
 @ContextConfiguration(locations = {
 		"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
 		"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
-		"file:src/test/resources/test-config.xml" })
+		"file:src/test/resources/test-config.xml",
+		"file:src/test/resources/test-socketioconfig.xml"
+})
 public class LoginControllerTest {
 
-	@Inject
-	private ApplicationContext applicationContext;
-	private MockHttpServletRequest request;
-	private MockHttpServletResponse response;
-	private HandlerAdapter handlerAdapter;
-
-	@Autowired
-	private LoginController loginController;
-
 	@Autowired
 	private StubUserService userService;
-	
-	@Autowired
-	private StubDatabaseDao databaseDao;
 
-	@After
-	public final void cleanup() {
-		databaseDao.cleanupTestData();
-	}
+	private MockMvc mockMvc;
+
+	@Autowired
+	private WebApplicationContext webApplicationContext;
 
 	@Before
-	public void setUp() throws Exception {
-		this.request = new MockHttpServletRequest();
-		this.response = new MockHttpServletResponse();
-		handlerAdapter = applicationContext
-				.getBean(AnnotationMethodHandlerAdapter.class);
+	public void setup() {
+		mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
 	}
 
 	@Test
 	public void testGuestLogin() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/doLogin");
-		request.addParameter("type", "guest");
-
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				loginController);
-
-		assertNotNull(mav);
-		assertNotNull(mav.getView());
-		RedirectView view = (RedirectView) mav.getView();
-		assertEquals("/#auth/checkLogin", view.getUrl());
-		Authentication auth = SecurityContextHolder.getContext()
-				.getAuthentication();
-		assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
+		mockMvc.perform(get("/doLogin").param("type", "guest")).andExpect(status().isMovedTemporarily()).andExpect(redirectedUrl("/#auth/checkLogin"));
 	}
 
 	@Test
 	public void testReuseGuestLogin() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/doLogin");
-		request.addParameter("type", "guest");
-		request.addParameter("user", "Guest1234567890");
+		mockMvc.perform(get("/doLogin").param("type", "guest").param("user","Guest1234567890"))
+		.andExpect(status().isMovedTemporarily()).andExpect(redirectedUrl("/#auth/checkLogin"));
 
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				loginController);
-
-		assertNotNull(mav);
-		assertNotNull(mav.getView());
-		RedirectView view = (RedirectView) mav.getView();
-		assertEquals("/#auth/checkLogin", view.getUrl());
 		Authentication auth = SecurityContextHolder.getContext()
 				.getAuthentication();
 		assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
 		assertEquals("Guest1234567890", auth.getName());
+
 	}
 
 	@Test
 	public void testUser() throws Exception {
 		userService.setUserAuthenticated(true);
 
-		request.setMethod("GET");
-		request.setRequestURI("/whoami");
-
-		handlerAdapter.handle(request, response, loginController);
-		assertNotNull(response);
-		assertEquals("{\"username\":\"ptsr00\",\"type\":\"ldap\",\"role\":null}", response.getContentAsString());
+		mockMvc.perform(get("/whoami"))
+		.andExpect(status().isOk())
+		.andExpect(content().string("{\"username\":\"ptsr00\",\"type\":\"ldap\",\"role\":null}"));
 	}
 
 	@Test
 	public void testLogoutWithoutRedirect() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/logout");
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				loginController);
-		assertNotNull(mav);
-		assertNotNull(mav.getView());
-		RedirectView view = (RedirectView) mav.getView();
-		assertEquals("/", view.getUrl());
-	}
-
-	@Test
-	public void testLogoutWithRedirect() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/logout");
-		request.addHeader("referer", "/dojo-index.html");
-
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				loginController);
-		assertNotNull(mav);
-		assertNotNull(mav.getView());
-		RedirectView view = (RedirectView) mav.getView();
-		assertEquals("/dojo-index.html", view.getUrl());
+		mockMvc.perform(get("/logout")).andExpect(status().isMovedTemporarily()).andExpect(redirectedUrl("/"));
 	}
 }
diff --git a/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java b/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java
index cdb927ae3..3b8ff8872 100644
--- a/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java
@@ -1,139 +1,79 @@
 package de.thm.arsnova.controller;
 
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
-import javax.inject.Inject;
-
-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.context.ApplicationContext;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.http.MediaType;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.web.servlet.HandlerAdapter;
-import org.springframework.web.servlet.ModelAndView;
-import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
-
-import de.thm.arsnova.dao.StubDatabaseDao;
-import de.thm.arsnova.entities.Session;
-import de.thm.arsnova.exceptions.ForbiddenException;
-import de.thm.arsnova.exceptions.NotFoundException;
-import de.thm.arsnova.exceptions.UnauthorizedException;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
 import de.thm.arsnova.services.StubUserService;
-import de.thm.arsnova.services.UserSessionService;
 
 @RunWith(SpringJUnit4ClassRunner.class)
+@WebAppConfiguration
 @ContextConfiguration(locations = {
 		"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
 		"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
-		"file:src/test/resources/test-config.xml"
+		"file:src/test/resources/test-config.xml",
+		"file:src/test/resources/test-socketioconfig.xml"
 })
 public class SessionControllerTest {
 
-	@Inject
-	private ApplicationContext applicationContext;
-	private MockHttpServletRequest request;
-	private MockHttpServletResponse response;
-	private HandlerAdapter handlerAdapter;
+	@Autowired
+	private StubUserService userService;
 
 	@Autowired
 	private SessionController sessionController;
 
-	@Autowired
-	private StubUserService userService;
-	
-	@Autowired
-	private StubDatabaseDao databaseDao;
+	private MockMvc mockMvc;
 
-	@After
-	public final void cleanup() {
-		databaseDao.cleanupTestData();
-	}
+	@Autowired
+	private WebApplicationContext webApplicationContext;
 
 	@Before
-	public void setUp() {
-		this.request = new MockHttpServletRequest();
-		this.response = new MockHttpServletResponse();
-		handlerAdapter = applicationContext
-				.getBean(AnnotationMethodHandlerAdapter.class);
+	public void setup() {
+		mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
 	}
 
-	@Test(expected = NotFoundException.class)
+	@Test
 	public void testShouldNotGetUnknownSession() throws Exception {
 		userService.setUserAuthenticated(true);
 
-		request.setMethod("GET");
-		request.setRequestURI("/session/00000000");
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				sessionController);
-
-		assertNull(mav);
-		assertTrue(response.getStatus() == 404);
+		mockMvc.perform(get("/session/00000000"))
+		.andExpect(status().isNotFound());
 	}
 
-	@Test(expected = ForbiddenException.class)
-	public void testShouldNotGetForbiddenSession() throws Exception {
-		Session session = new Session();
-		session.setKeyword("08154711");
-		session.setCreator("some other user");
-		session.setActive(false);
-		databaseDao.saveSession(session);
-		userService.setUserAuthenticated(true);
-		userService.setRole(UserSessionService.Role.STUDENT);
-
-		request.setMethod("GET");
-		request.setRequestURI("/session/08154711");
-		final ModelAndView mav = handlerAdapter.handle(request, response, sessionController);
-
-		assertNull(mav);
-		assertTrue(response.getStatus() == 403);
-	}
-
-	@Test(expected = UnauthorizedException.class)
+	@Test
 	public void testShouldNotGetSessionIfUnauthorized() throws Exception {
 		userService.setUserAuthenticated(false);
 
-		request.setMethod("GET");
-		request.setRequestURI("/session/00000000");
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				sessionController);
-
-		assertNull(mav);
-		assertTrue(response.getStatus() == 401);
+		mockMvc.perform(get("/session/00000000"))
+		.andExpect(status().isUnauthorized());
 	}
 
-	@Test(expected = UnauthorizedException.class)
+	@Test
 	public void testShouldNotGetSessionIfAnonymous() throws Exception {
 		userService.setUserAuthenticated(false);
 		userService.useAnonymousUser();
 
-		request.setMethod("GET");
-		request.setRequestURI("/session/00000000");
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				sessionController);
-
-		assertNull(mav);
-		assertTrue(response.getStatus() == 401);
+		mockMvc.perform(get("/session/00000000"))
+		.andExpect(status().isUnauthorized());
 	}
 
-	@Test(expected = UnauthorizedException.class)
+	@Test
 	public void testShouldCreateSessionIfUnauthorized() throws Exception {
 		userService.setUserAuthenticated(false);
 
-		request.setMethod("POST");
-		request.setRequestURI("/session/");
-		request.setContentType("application/json");
-		request.setContent("{}".getBytes());
-
-		final ModelAndView mav = handlerAdapter.handle(request, response,
-				sessionController);
-
-		assertNull(mav);
-		assertTrue(response.getStatus() == 401);
+		mockMvc.perform(post("/session/").contentType(MediaType.APPLICATION_JSON).content("{}"))
+		.andExpect(status().isUnauthorized());
 	}
 }
diff --git a/src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java b/src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java
deleted file mode 100644
index 35d3287b3..000000000
--- a/src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (C) 2012 THM webMedia
- * 
- * This file is part of ARSnova.
- *
- * ARSnova is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * ARSnova is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package de.thm.arsnova.controller;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-import javax.inject.Inject;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.ApplicationContext;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.springframework.mock.web.MockHttpServletResponse;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.web.servlet.HandlerAdapter;
-import org.springframework.web.servlet.ModelAndView;
-import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
-
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = {
-		"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
-		"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
-		"file:src/test/resources/test-config.xml" })
-public class WelcomeControllerTest {
-
-	@Inject
-	private ApplicationContext applicationContext;
-	private MockHttpServletRequest request;
-	private MockHttpServletResponse response;
-	private HandlerAdapter handlerAdapter;
-
-	@Autowired
-	private WelcomeController welcomeController;
-
-	@Before
-	public final void setUp() {
-		this.request = new MockHttpServletRequest();
-		this.response = new MockHttpServletResponse();
-		handlerAdapter = applicationContext.getBean(AnnotationMethodHandlerAdapter.class);
-	}
-
-	@Test
-	public void testIndexPage() throws Exception {
-		request.setMethod("GET");
-		request.setRequestURI("/");
-
-		final ModelAndView mav = handlerAdapter.handle(request, response, welcomeController);
-		assertNotNull(mav);
-		assertEquals("redirect:/index.html", mav.getViewName());
-	}
-}
-- 
GitLab