From f3ca6e589556cd4fea2c04628bea9464a073ba60 Mon Sep 17 00:00:00 2001
From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de>
Date: Wed, 28 May 2014 08:37:44 +0200
Subject: [PATCH] Check result media type in controller tests

---
 .../controller/FeedbackControllerTest.java    |  1 +
 .../LecturerQuestionControllerTest.java       | 43 +++++++++++++------
 .../controller/LoginControllerTest.java       | 27 +++++++++---
 .../controller/SessionControllerTest.java     | 30 +++++++------
 .../controller/StatisticsControllerTest.java  |  1 +
 5 files changed, 70 insertions(+), 32 deletions(-)

diff --git a/src/test/java/de/thm/arsnova/controller/FeedbackControllerTest.java b/src/test/java/de/thm/arsnova/controller/FeedbackControllerTest.java
index 980519c8..a9fc7a16 100644
--- a/src/test/java/de/thm/arsnova/controller/FeedbackControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/FeedbackControllerTest.java
@@ -71,6 +71,7 @@ public class FeedbackControllerTest {
 		userService.setUserAuthenticated(true);
 		mockMvc.perform(get("/session/87654321/feedback").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isOk())
+		.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
 		.andExpect(jsonPath("$.values").isArray());
 	}
 
diff --git a/src/test/java/de/thm/arsnova/controller/LecturerQuestionControllerTest.java b/src/test/java/de/thm/arsnova/controller/LecturerQuestionControllerTest.java
index 23833aed..bccdc773 100644
--- a/src/test/java/de/thm/arsnova/controller/LecturerQuestionControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/LecturerQuestionControllerTest.java
@@ -13,6 +13,7 @@ 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.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
@@ -47,10 +48,10 @@ public class LecturerQuestionControllerTest {
 	@Autowired
 	private WebApplicationContext webApplicationContext;
 
-	private void setAuthenticated(boolean isAuthenticated, String username) {
+	private void setAuthenticated(final boolean isAuthenticated, final String username) {
 		if (isAuthenticated) {
-			List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
-			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga);
+			final List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
+			final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga);
 			SecurityContextHolder.getContext().setAuthentication(token);
 			userService.setUserAuthenticated(isAuthenticated, username);
 		} else {
@@ -74,33 +75,49 @@ public class LecturerQuestionControllerTest {
 	public void testShouldNotGetLecturerQuestionsIfUnauthorized() throws Exception {
 		setAuthenticated(false, "nobody");
 
-		mockMvc.perform(get("/lecturerquestion/").param("sessionkey", "12345678").param("lecturequestionsonly", "true"))
-		.andExpect(status().isUnauthorized());
+		mockMvc.perform(
+				get("/lecturerquestion/")
+				.param("sessionkey", "12345678").param("lecturequestionsonly", "true")
+				.accept(MediaType.APPLICATION_JSON)
+				).andExpect(status().isUnauthorized()
+						);
 	}
 
 	@Test
 	public void testShouldNotGetPreparationQuestionsIfUnauthorized() throws Exception {
 		setAuthenticated(false, "nobody");
 
-		mockMvc.perform(get("/lecturerquestion/").param("sessionkey", "12345678").param("preparationquestionsonly", "true"))
-		.andExpect(status().isUnauthorized());
+		mockMvc.perform(
+				get("/lecturerquestion/")
+				.param("sessionkey", "12345678").param("preparationquestionsonly", "true")
+				.accept(MediaType.APPLICATION_JSON)
+				).andExpect(status().isUnauthorized()
+						);
 	}
 
 	@Test
 	public void testShouldReturnQuestionCount() throws Exception {
 		setAuthenticated(true, "somebody");
 
-		mockMvc.perform(get("/lecturerquestion/count").param("sessionkey", "12345678").param("lecturequestionsonly", "true"))
-		.andExpect(status().isOk())
-		.andExpect(content().string("0"));
+		mockMvc.perform(
+				get("/lecturerquestion/count")
+				.param("sessionkey", "12345678").param("lecturequestionsonly", "true")
+				.accept(MediaType.APPLICATION_JSON)
+				).andExpect(status().isOk())
+				.andExpect(content().string("0")
+						);
 	}
 
 	@Test
 	public void testShouldReturnXDeprecatedApiHeaderForQuestionCount() throws Exception {
 		setAuthenticated(true, "somebody");
 
-		mockMvc.perform(get("/lecturerquestion/count").param("sessionkey", "12345678").param("lecturequestionsonly", "true"))
-		.andExpect(status().isOk())
-		.andExpect(header().string(AbstractController.X_DEPRECATED_API, "1"));
+		mockMvc.perform(
+				get("/lecturerquestion/count")
+				.param("sessionkey", "12345678").param("lecturequestionsonly", "true")
+				.accept(MediaType.APPLICATION_JSON)
+				).andExpect(status().isOk())
+				.andExpect(header().string(AbstractController.X_DEPRECATED_API, "1")
+						);
 	}
 }
diff --git a/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java b/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
index 83b01fa6..c2e27a6b 100644
--- a/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
@@ -21,6 +21,7 @@ package de.thm.arsnova.controller;
 import static org.junit.Assert.assertEquals;
 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.jsonPath;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
@@ -28,6 +29,7 @@ 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.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
@@ -66,15 +68,22 @@ public class LoginControllerTest {
 
 	@Test
 	public void testGuestLogin() throws Exception {
-		mockMvc.perform(get("/doLogin").param("type", "guest")).andExpect(status().isMovedTemporarily()).andExpect(redirectedUrl("/#auth/checkLogin"));
+		mockMvc.perform(
+				get("/doLogin")
+				.param("type", "guest")
+				).andExpect(status().isMovedTemporarily())
+				.andExpect(redirectedUrl("/#auth/checkLogin"));
 	}
 
 	@Test
 	public void testReuseGuestLogin() throws Exception {
-		mockMvc.perform(get("/doLogin").param("type", "guest").param("user","Guest1234567890"))
-		.andExpect(status().isMovedTemporarily()).andExpect(redirectedUrl("/#auth/checkLogin"));
+		mockMvc.perform(
+				get("/doLogin")
+				.param("type", "guest").param("user","Guest1234567890")
+				).andExpect(status().isMovedTemporarily())
+				.andExpect(redirectedUrl("/#auth/checkLogin"));
 
-		Authentication auth = SecurityContextHolder.getContext()
+		final Authentication auth = SecurityContextHolder.getContext()
 				.getAuthentication();
 		assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
 		assertEquals("Guest1234567890", auth.getName());
@@ -85,13 +94,17 @@ public class LoginControllerTest {
 	public void testUser() throws Exception {
 		userService.setUserAuthenticated(true);
 
-		mockMvc.perform(get("/whoami"))
+		mockMvc.perform(get("/whoami").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isOk())
-		.andExpect(content().string("{\"username\":\"ptsr00\",\"type\":\"ldap\",\"role\":null}"));
+		.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
+		.andExpect(jsonPath("$.username").value("ptsr00"))
+		.andExpect(jsonPath("$.type").value("ldap"));
 	}
 
 	@Test
 	public void testLogoutWithoutRedirect() throws Exception {
-		mockMvc.perform(get("/logout")).andExpect(status().isMovedTemporarily()).andExpect(redirectedUrl("/"));
+		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 a6e46b1c..9918233f 100644
--- a/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java
@@ -49,10 +49,10 @@ public class SessionControllerTest {
 	@Autowired
 	private WebApplicationContext webApplicationContext;
 
-	private void setAuthenticated(boolean isAuthenticated, String username) {
+	private void setAuthenticated(final boolean isAuthenticated, final String username) {
 		if (isAuthenticated) {
-			List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
-			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga);
+			final List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
+			final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga);
 			SecurityContextHolder.getContext().setAuthentication(token);
 			userService.setUserAuthenticated(isAuthenticated, username);
 		} else {
@@ -76,7 +76,7 @@ public class SessionControllerTest {
 	public void testShouldNotGetUnknownSession() throws Exception {
 		setAuthenticated(true, "ptsr00");
 
-		mockMvc.perform(get("/session/00000000"))
+		mockMvc.perform(get("/session/00000000").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isNotFound());
 	}
 
@@ -84,7 +84,7 @@ public class SessionControllerTest {
 	public void testShouldNotGetUnknownSessionIfUnauthorized() throws Exception {
 		setAuthenticated(false, "ptsr00");
 
-		mockMvc.perform(get("/session/00000000"))
+		mockMvc.perform(get("/session/00000000").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isUnauthorized());
 	}
 
@@ -92,15 +92,20 @@ public class SessionControllerTest {
 	public void testShouldCreateSessionIfUnauthorized() throws Exception {
 		setAuthenticated(false, "ptsr00");
 
-		mockMvc.perform(post("/session/").contentType(MediaType.APPLICATION_JSON).content("{\"keyword\":12345678}"))
-		.andExpect(status().isUnauthorized());
+		mockMvc.perform(
+				post("/session/")
+				.accept(MediaType.APPLICATION_JSON)
+				.contentType(MediaType.APPLICATION_JSON)
+				.content("{\"keyword\":12345678}")
+				)
+				.andExpect(status().isUnauthorized());
 	}
 
 	@Test
 	public void testShouldNotReturnMySessionsIfUnauthorized() throws Exception {
 		setAuthenticated(false, "ptsr00");
 
-		mockMvc.perform(get("/session/").param("ownedonly", "true"))
+		mockMvc.perform(get("/session/").param("ownedonly", "true").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isUnauthorized());
 	}
 
@@ -108,7 +113,7 @@ public class SessionControllerTest {
 	public void testShouldNotReturnMyVisitedSessionsIfUnauthorized() throws Exception {
 		setAuthenticated(false, "ptsr00");
 
-		mockMvc.perform(get("/session/").param("visitedonly", "true"))
+		mockMvc.perform(get("/session/").param("visitedonly", "true").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isUnauthorized());
 	}
 
@@ -116,7 +121,7 @@ public class SessionControllerTest {
 	public void testShouldShowUnimplementedIfNoFlagIsSet() throws Exception {
 		setAuthenticated(false, "ptsr00");
 
-		mockMvc.perform(get("/session/"))
+		mockMvc.perform(get("/session/").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isNotImplemented());
 	}
 
@@ -124,8 +129,9 @@ public class SessionControllerTest {
 	public void testShouldReturnActiveUserCount() throws Exception {
 		setAuthenticated(false, "ptsr00");
 
-		mockMvc.perform(get("/session/12345678/activeusercount"))
+		mockMvc.perform(get("/session/12345678/activeusercount").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isOk())
+		.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
 		.andExpect(content().string("0"));
 	}
 
@@ -133,7 +139,7 @@ public class SessionControllerTest {
 	public void testShouldReturnXDeprecatedApiHeaderForActiveUserCount() throws Exception {
 		setAuthenticated(false, "ptsr00");
 
-		mockMvc.perform(get("/session/12345678/activeusercount"))
+		mockMvc.perform(get("/session/12345678/activeusercount").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isOk())
 		.andExpect(header().string(AbstractController.X_DEPRECATED_API, "1"));
 	}
diff --git a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java
index cc9f7dbd..a23f9132 100644
--- a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java
+++ b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java
@@ -74,6 +74,7 @@ public class StatisticsControllerTest {
 	public final void testShouldGetStatistics() throws Exception {
 		mockMvc.perform(get("/statistics").accept(MediaType.APPLICATION_JSON))
 		.andExpect(status().isOk())
+		.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
 		.andExpect(jsonPath("$.answers").value(0))
 		.andExpect(jsonPath("$.questions").value(0))
 		.andExpect(jsonPath("$.openSessions").value(3))
-- 
GitLab