diff --git a/src/test/java/de/thm/arsnova/config/TestAppConfig.java b/src/test/java/de/thm/arsnova/config/TestAppConfig.java index 5c7eeed2562fb478fb7b81e9a7c132a44651e826..f2759881edb031f59b10e8070fe79f706b9400ad 100644 --- a/src/test/java/de/thm/arsnova/config/TestAppConfig.java +++ b/src/test/java/de/thm/arsnova/config/TestAppConfig.java @@ -1,6 +1,5 @@ package de.thm.arsnova.config; -import de.thm.arsnova.dao.StubDatabaseDao; import de.thm.arsnova.services.StubUserService; import de.thm.arsnova.socket.ARSnovaSocket; import de.thm.arsnova.socket.ARSnovaSocketIOServer; @@ -59,11 +58,6 @@ public class TestAppConfig { return configurer; } - @Bean - public StubDatabaseDao databaseDao() { - return new StubDatabaseDao(); - } - @Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer") public ARSnovaSocket socketTestServer() { final int testSocketPort = 1234 + testPortOffset++ % 10; diff --git a/src/test/java/de/thm/arsnova/controller/ContentControllerTest.java b/src/test/java/de/thm/arsnova/controller/ContentControllerTest.java deleted file mode 100644 index ec09fcca0b6d5685767c8329161cc8b3dff310c3..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/controller/ContentControllerTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2017 The ARSnova Team - * - * ARSnova Backend 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 Backend 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 de.thm.arsnova.services.StubUserService; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -public class ContentControllerTest extends AbstractControllerTest { - - @Autowired - private StubUserService userService; - - @Autowired - private ContentController questionController; - - private MockMvc mockMvc; - - @Autowired - private WebApplicationContext webApplicationContext; - - @Before - public void startup() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - SecurityContextHolder.clearContext(); - } - - @After - public void cleanup() { - SecurityContextHolder.clearContext(); - userService.setUserAuthenticated(false); - } - - @Test - public void testShouldNotGetLecturerQuestionsIfUnauthorized() throws Exception { - setAuthenticated(false, "nobody"); - - 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") - .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") - .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") - .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/FeedbackControllerTest.java b/src/test/java/de/thm/arsnova/controller/FeedbackControllerTest.java deleted file mode 100644 index 6824a3706a063b96f8421b413d0ee3d6a5207488..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/controller/FeedbackControllerTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2017 The ARSnova Team - * - * ARSnova Backend 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 Backend 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 de.thm.arsnova.services.StubUserService; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -public class FeedbackControllerTest extends AbstractControllerTest { - - @Autowired - private StubUserService userService; - - private MockMvc mockMvc; - - @Autowired - private WebApplicationContext webApplicationContext; - - @Before - public void setup() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - - @Test - public void testShouldNotGetFeedbackForUnknownSession() throws Exception { - userService.setUserAuthenticated(true); - mockMvc.perform(get("/session/00000000/feedback").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); - } - - @Test - public void testShouldNotGetAverageFeedbackContentForSessionWithoutFeedback() throws Exception { - userService.setUserAuthenticated(true); - mockMvc.perform(get("/session/12345678/averagefeedback").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isNoContent()); - } - - @Test - public void testShouldNotGetCorrectFeedbackCountForSessionWithoutFeedback() throws Exception { - userService.setUserAuthenticated(true); - mockMvc.perform(get("/session/12345678/feedbackcount").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().string("0")); - } - - @Test - public void testShouldReturnFeedback() throws Exception { - 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()); - } - - @Test - public void testShouldReturnXDeprecatedApiHeaderForFeedback() throws Exception { - userService.setUserAuthenticated(true); - mockMvc.perform(get("/session/87654321/feedback").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/SessionControllerTest.java b/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java deleted file mode 100644 index 3f219d532f5ed1b9f961c463d68563275095e338..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2017 The ARSnova Team - * - * ARSnova Backend 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 Backend 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 de.thm.arsnova.services.StubUserService; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -public class SessionControllerTest extends AbstractControllerTest { - - @Autowired - private StubUserService userService; - - @Autowired - private SessionController sessionController; - - private MockMvc mockMvc; - - @Autowired - private WebApplicationContext webApplicationContext; - - @Before - public void startup() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - SecurityContextHolder.clearContext(); - } - - @After - public void cleanup() { - SecurityContextHolder.clearContext(); - userService.setUserAuthenticated(false); - } - - @Test - public void testShouldNotGetUnknownSession() throws Exception { - setAuthenticated(true, "ptsr00"); - - mockMvc.perform(get("/session/00000000").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); - } - - @Test - public void testShouldNotGetUnknownSessionIfUnauthorized() throws Exception { - setAuthenticated(false, "ptsr00"); - - mockMvc.perform(get("/session/00000000").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isUnauthorized()); - } - - @Test - public void testShouldCreateSessionIfUnauthorized() throws Exception { - setAuthenticated(false, "ptsr00"); - - 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").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isUnauthorized()); - } - - @Test - public void testShouldNotReturnMyVisitedSessionsIfUnauthorized() throws Exception { - setAuthenticated(false, "ptsr00"); - - mockMvc.perform(get("/session/").param("visitedonly", "true").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isUnauthorized()); - } - - @Test - public void testShouldShowUnimplementedIfNoFlagIsSet() throws Exception { - setAuthenticated(false, "ptsr00"); - - mockMvc.perform(get("/session/").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotImplemented()); - } - - @Test - public void testShouldReturnActiveUserCount() throws Exception { - setAuthenticated(false, "ptsr00"); - - mockMvc.perform(get("/session/12345678/activeusercount").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(content().string("0")); - } - - @Test - public void testShouldReturnXDeprecatedApiHeaderForActiveUserCount() throws Exception { - setAuthenticated(false, "ptsr00"); - - mockMvc.perform(get("/session/12345678/activeusercount").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(header().string(AbstractController.X_DEPRECATED_API, "1")); - } - - @Test - public void testShouldEndInForbidden() throws Exception { - setAuthenticated(true, "ptsr00"); - - mockMvc.perform( - put("/session/12345678") - .content("{\"keyword\":\"12345678\", \"name\":\"Testsession\"}, \"shortName\":\"TS\", \"creator\":\"ptsr00\", \"active\":true") - .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); - - setAuthenticated(true, "other"); - - mockMvc.perform(delete("/session/12345678")).andExpect(status().isForbidden()); - } -} diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java deleted file mode 100644 index 9bdfb951140c86df3b6a36a8dd2c9f6e6e7c729c..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2017 The ARSnova Team - * - * ARSnova Backend 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 Backend 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.dao; - -import de.thm.arsnova.entities.*; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -@Profile("test") -@Service("databaseDao") -public class StubDatabaseDao { - - private static Map<String, Session> stubSessions = new ConcurrentHashMap<>(); - private static Map<String, Feedback> stubFeedbacks = new ConcurrentHashMap<>(); - private static Map<String, List<Content>> stubQuestions = new ConcurrentHashMap<>(); - private static Map<String, User> stubUsers = new ConcurrentHashMap<>(); - - public Comment comment; - - public StubDatabaseDao() { - fillWithDummySessions(); - fillWithDummyFeedbacks(); - fillWithDummyQuestions(); - } - - public void cleanupTestData() { - stubSessions.clear(); - stubFeedbacks.clear(); - stubQuestions.clear(); - stubUsers.clear(); - - fillWithDummySessions(); - fillWithDummyFeedbacks(); - fillWithDummyQuestions(); - } - - private void fillWithDummySessions() { - Session session = new Session(); - session.setActive(true); - session.setCreator("ptsr00"); - session.setKeyword("12345678"); - session.setName("TestSession1"); - session.setShortName("TS1"); - - stubSessions.put("12345678", session); - - session = new Session(); - session.setActive(true); - session.setCreator("ptsr00"); - session.setKeyword("87654321"); - session.setName("TestSession2"); - session.setShortName("TS2"); - - stubSessions.put("87654321", session); - - session = new Session(); - session.setActive(true); - session.setCreator("ptsr00"); - session.setKeyword("18273645"); - session.setName("TestSession2"); - session.setShortName("TS3"); - - stubSessions.put("18273645", session); - } - - private void fillWithDummyFeedbacks() { - stubFeedbacks.put("12345678", new Feedback(0, 0, 0, 0)); - stubFeedbacks.put("87654321", new Feedback(2, 3, 5, 7)); - stubFeedbacks.put("18273645", new Feedback(2, 3, 5, 11)); - } - - private void fillWithDummyQuestions() { - List<Content> contents = new ArrayList<>(); - contents.add(new Content()); - stubQuestions.put("12345678", contents); - } -} diff --git a/src/test/java/de/thm/arsnova/services/ContentServiceTest.java b/src/test/java/de/thm/arsnova/services/ContentServiceTest.java deleted file mode 100644 index 80ec08da18557f6f6c59738e49a5c870d862f335..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/services/ContentServiceTest.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2017 The ARSnova Team - * - * ARSnova Backend 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 Backend 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.services; - -import de.thm.arsnova.config.AppConfig; -import de.thm.arsnova.config.TestAppConfig; -import de.thm.arsnova.config.TestSecurityConfig; -import de.thm.arsnova.dao.StubDatabaseDao; -import de.thm.arsnova.entities.Comment; -import de.thm.arsnova.entities.Content; -import de.thm.arsnova.exceptions.NotFoundException; -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.security.access.AccessDeniedException; -import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.*; - -@RunWith(SpringJUnit4ClassRunner.class) -@WebAppConfiguration -@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class, TestSecurityConfig.class}) -@ActiveProfiles("test") -public class ContentServiceTest { - - @Autowired - private IContentService contentService; - - @Autowired - private StubUserService userService; - - @Autowired - private StubDatabaseDao databaseDao; - - private void setAuthenticated(final boolean isAuthenticated, final String username) { - if (isAuthenticated) { - final List<GrantedAuthority> ga = new ArrayList<>(); - final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga); - SecurityContextHolder.getContext().setAuthentication(token); - userService.setUserAuthenticated(isAuthenticated, username); - } else { - userService.setUserAuthenticated(isAuthenticated); - } - } - - @Before - public void startup() { - SecurityContextHolder.clearContext(); - } - - @After - public void cleanup() { - SecurityContextHolder.clearContext(); - } - - @Test(expected = AuthenticationCredentialsNotFoundException.class) - public void testShouldNotReturnQuestionsIfNotAuthenticated() { - setAuthenticated(false, "nobody"); - contentService.getSkillQuestions("12345678"); - } - - @Test(expected = NotFoundException.class) - public void testShouldFindQuestionsForNonExistantSession() { - setAuthenticated(true, "ptsr00"); - contentService.getSkillQuestions("00000000"); - } - - @Test - public void testShouldFindQuestions() { - setAuthenticated(true, "ptsr00"); - assertEquals(1, contentService.getSkillQuestionCount("12345678")); - } - - @Test - public void testShouldMarkInterposedQuestionAsReadIfSessionCreator() throws Exception { - setAuthenticated(true, "ptsr00"); - final Comment comment = new Comment(); - comment.setRead(false); - comment.setId("the internal id"); - comment.setSessionId("12345678"); - databaseDao.comment = comment; - - contentService.readInterposedQuestion(comment.getId()); - - assertTrue(comment.isRead()); - } - - @Test - public void testShouldNotMarkInterposedQuestionAsReadIfRegularUser() throws Exception { - setAuthenticated(true, "regular user"); - final Comment comment = new Comment(); - comment.setRead(false); - comment.setId("the internal id"); - comment.setSessionId("12345678"); - comment.setCreator("regular user"); - databaseDao.comment = comment; - - contentService.readInterposedQuestion(comment.getId()); - - assertFalse(comment.isRead()); - } - - @Test(expected = AccessDeniedException.class) - public void testShouldSaveQuestion() throws Exception{ - setAuthenticated(true, "regular user"); - final Content content = new Content(); - content.setSessionKeyword("12345678"); - content.setQuestionVariant("freetext"); - contentService.saveQuestion(content); - } - - @Test(expected = AccessDeniedException.class) - public void testShouldNotDeleteQuestion() throws Exception{ - setAuthenticated(true, "otheruser"); - contentService.deleteQuestion("a1a2a3a4a5a6a7a8a9a"); - } - - @Test(expected = AccessDeniedException.class) - public void testShouldNotDeleteInterposedQuestion() throws Exception{ - setAuthenticated(true, "otheruser"); - contentService.deleteInterposedQuestion("a1a2a3a4a5a6a7a8a9a"); - } -} diff --git a/src/test/java/de/thm/arsnova/services/FeedbackServiceTest.java b/src/test/java/de/thm/arsnova/services/FeedbackServiceTest.java deleted file mode 100644 index b751c9f9fbeaee564cd5d9160442c1c435a26d9c..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/services/FeedbackServiceTest.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2017 The ARSnova Team - * - * ARSnova Backend 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 Backend 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.services; - -import de.thm.arsnova.config.AppConfig; -import de.thm.arsnova.config.TestAppConfig; -import de.thm.arsnova.config.TestSecurityConfig; -import de.thm.arsnova.dao.StubDatabaseDao; -import de.thm.arsnova.entities.TestUser; -import de.thm.arsnova.exceptions.NoContentException; -import de.thm.arsnova.exceptions.NotFoundException; -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.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@RunWith(SpringJUnit4ClassRunner.class) -@WebAppConfiguration -@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class, TestSecurityConfig.class}) -@ActiveProfiles("test") -public class FeedbackServiceTest { - - @Autowired - private IFeedbackService feedbackService; - - @Autowired - private StubUserService userService; - - @Autowired - private StubDatabaseDao databaseDao; - - @Before - public void setup() { - userService.setUserAuthenticated(false); - - feedbackService.saveFeedback("87654321", 0, new TestUser("testuser01")); - feedbackService.saveFeedback("87654321", 0, new TestUser("testuser02")); - feedbackService.saveFeedback("87654321", 1, new TestUser("testuser11")); - feedbackService.saveFeedback("87654321", 1, new TestUser("testuser12")); - feedbackService.saveFeedback("87654321", 1, new TestUser("testuser13")); - feedbackService.saveFeedback("87654321", 2, new TestUser("testuser21")); - feedbackService.saveFeedback("87654321", 2, new TestUser("testuser22")); - feedbackService.saveFeedback("87654321", 2, new TestUser("testuser23")); - feedbackService.saveFeedback("87654321", 2, new TestUser("testuser24")); - feedbackService.saveFeedback("87654321", 2, new TestUser("testuser25")); - feedbackService.saveFeedback("87654321", 3, new TestUser("testuser31")); - feedbackService.saveFeedback("87654321", 3, new TestUser("testuser32")); - feedbackService.saveFeedback("87654321", 3, new TestUser("testuser33")); - feedbackService.saveFeedback("87654321", 3, new TestUser("testuser34")); - feedbackService.saveFeedback("87654321", 3, new TestUser("testuser35")); - feedbackService.saveFeedback("87654321", 3, new TestUser("testuser36")); - feedbackService.saveFeedback("87654321", 3, new TestUser("testuser37")); - - feedbackService.saveFeedback("18273645", 0, new TestUser("testuser01")); - feedbackService.saveFeedback("18273645", 0, new TestUser("testuser02")); - feedbackService.saveFeedback("18273645", 1, new TestUser("testuser11")); - feedbackService.saveFeedback("18273645", 1, new TestUser("testuser12")); - feedbackService.saveFeedback("18273645", 1, new TestUser("testuser13")); - feedbackService.saveFeedback("18273645", 2, new TestUser("testuser21")); - feedbackService.saveFeedback("18273645", 2, new TestUser("testuser22")); - feedbackService.saveFeedback("18273645", 2, new TestUser("testuser23")); - feedbackService.saveFeedback("18273645", 2, new TestUser("testuser24")); - feedbackService.saveFeedback("18273645", 2, new TestUser("testuser25")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser31")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser32")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser33")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser34")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser35")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser36")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser37")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser38")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser39")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser310")); - feedbackService.saveFeedback("18273645", 3, new TestUser("testuser311")); - - } - - @After - public void cleanup() { - databaseDao.cleanupTestData(); - userService.setUserAuthenticated(false); - } - - @Test(expected = NotFoundException.class) - public void testShouldFindFeedbackForNonExistantSession() { - userService.setUserAuthenticated(true); - feedbackService.getFeedback("00000000"); - } - - @Test - public void testShouldReturnFeedback() { - userService.setUserAuthenticated(true); - assertNotNull(feedbackService.getFeedback("87654321")); - assertEquals(2, (int) feedbackService.getFeedback("87654321").getValues().get(0)); - assertEquals(3, (int) feedbackService.getFeedback("87654321").getValues().get(1)); - assertEquals(5, (int) feedbackService.getFeedback("87654321").getValues().get(2)); - assertEquals(7, (int) feedbackService.getFeedback("87654321").getValues().get(3)); - } - - @Test(expected = NotFoundException.class) - public void testShouldFindFeedbackCountForNonExistantSession() { - userService.setUserAuthenticated(true); - feedbackService.getFeedbackCount("00000000"); - } - - @Test - public void testShouldReturnFeedbackCount() { - userService.setUserAuthenticated(true); - assertEquals(17, feedbackService.getFeedbackCount("87654321")); - } - - @Test(expected = NotFoundException.class) - public void testShouldFindAverageFeedbackForNonExistantSession() { - userService.setUserAuthenticated(true); - feedbackService.getAverageFeedback("00000000"); - } - - @Test - public void testShouldReturnZeroFeedbackCountForNoFeedbackAtAll() { - userService.setUserAuthenticated(true); - assertEquals(0, feedbackService.getFeedbackCount("12345678")); - } - - @Test(expected = NoContentException.class) - public void testShouldReturnAverageFeedbackForNoFeedbackAtAll() { - userService.setUserAuthenticated(true); - feedbackService.getAverageFeedback("12345678"); - } - - @Test - public void testShouldReturnAverageFeedbackRounded() { - userService.setUserAuthenticated(true); - assertEquals(2, feedbackService.getAverageFeedbackRounded("18273645")); - } - - @Test - public void testShouldReturnAverageFeedbackNotRounded() { - userService.setUserAuthenticated(true); - assertEquals(2.1904, feedbackService.getAverageFeedback("18273645"), 0.001); - } -} diff --git a/src/test/java/de/thm/arsnova/services/SessionServiceTest.java b/src/test/java/de/thm/arsnova/services/SessionServiceTest.java deleted file mode 100644 index c7d2e79bfbb9d5aa1404168357283aedd352a8cb..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/services/SessionServiceTest.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2017 The ARSnova Team - * - * ARSnova Backend 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 Backend 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.services; - -import de.thm.arsnova.config.AppConfig; -import de.thm.arsnova.config.TestAppConfig; -import de.thm.arsnova.config.TestSecurityConfig; -import de.thm.arsnova.dao.StubDatabaseDao; -import de.thm.arsnova.entities.Session; -import de.thm.arsnova.exceptions.NotFoundException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.aop.framework.Advised; -import org.springframework.aop.support.AopUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; - -import static org.junit.Assert.*; - -@RunWith(SpringJUnit4ClassRunner.class) -@WebAppConfiguration -@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class, TestSecurityConfig.class}) -@ActiveProfiles("test") -public class SessionServiceTest { - - @Autowired - private ISessionService sessionService; - - @Autowired - private StubUserService userService; - - @Autowired - private StubDatabaseDao databaseDao; - - private void setAuthenticated(final boolean isAuthenticated, final String username) { - if (isAuthenticated) { - final List<GrantedAuthority> ga = new ArrayList<>(); - final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga); - SecurityContextHolder.getContext().setAuthentication(token); - userService.setUserAuthenticated(isAuthenticated, username); - } else { - userService.setUserAuthenticated(isAuthenticated); - } - } - - @Before - public void startup() { - SecurityContextHolder.clearContext(); - } - - @After - public void cleanup() { - databaseDao.cleanupTestData(); - SecurityContextHolder.clearContext(); - userService.setUserAuthenticated(false); - } - - @Test - public void testShouldGenerateSessionKeyword() { - assertTrue(sessionService.generateKeyword().matches("^[0-9]{8}$")); - } - - @Test(expected = NotFoundException.class) - public void testShouldNotFindNonExistantSession() { - setAuthenticated(true, "ptsr00"); - sessionService.getSession("00000000"); - } - - @Test(expected = AuthenticationCredentialsNotFoundException.class) - public void testShouldNotReturnSessionIfUnauthorized() { - setAuthenticated(false, null); - sessionService.getSession("12345678"); - } - - @Test(expected = AuthenticationCredentialsNotFoundException.class) - public void testShouldNotSaveSessionIfUnauthorized() { - setAuthenticated(false, null); - - final Session session = new Session(); - session.setActive(true); - session.setCreator("ptsr00"); - session.setKeyword("11111111"); - session.setName("TestSessionX"); - session.setShortName("TSX"); - sessionService.saveSession(session); - - setAuthenticated(true, "ptsr00"); - - assertNull(sessionService.getSession("11111111")); - } - - @Test - public void testShouldSaveSession() { - setAuthenticated(true, "ptsr00"); - - final Session session = new Session(); - session.setActive(true); - session.setCreator("ptsr00"); - session.setKeyword("11111111"); - session.setName("TestSessionX"); - session.setShortName("TSX"); - sessionService.saveSession(session); - assertNotNull(sessionService.getSession("11111111")); - } - - @Test(expected = AccessDeniedException.class) - public void testShouldUpdateSession() { - setAuthenticated(true, "ptsr00"); - - final Session session = new Session(); - session.setActive(true); - session.setCreator("ptsr00"); - session.setKeyword("11111111"); - session.setName("TestSessionX"); - session.setShortName("TSX"); - sessionService.saveSession(session); - - setAuthenticated(true, "other"); - sessionService.updateSession(session.getKeyword(), session); - } - - @Test(expected = AuthenticationCredentialsNotFoundException.class) - public void testShouldNotDeleteSessionIfUnauthorized() { - setAuthenticated(false, "nobody"); - sessionService.deleteSession("12345678"); - } - - @Test(expected = AccessDeniedException.class) - public void testShouldNotDeleteSessionIfNotOwner() { - setAuthenticated(true, "anybody"); - sessionService.deleteSession("12345678"); - } - - @Test - public void testShouldCompareSessionByName() { - final Session sessionA = new Session(); - sessionA.setName("TestSessionA"); - sessionA.setShortName("TSA"); - - final Session sessionB = new Session(); - sessionB.setName("TestSessionB"); - sessionB.setShortName("TSB"); - - final Comparator<Session> comp = new SessionService.SessionNameComparator(); - assertTrue(comp.compare(sessionA, sessionB) < 0); - } - - @Test - public void testShouldCompareSessionByShortName() { - final Session sessionA = new Session(); - sessionA.setName("TestSessionA"); - sessionA.setShortName("TSA"); - - final Session sessionB = new Session(); - sessionB.setName("TestSessionB"); - sessionB.setShortName("TSB"); - - final Comparator<Session> comp = new SessionService.SessionShortNameComparator(); - assertTrue(comp.compare(sessionA, sessionB) < 0); - } - - @SuppressWarnings("unchecked") - public static <T> T getTargetObject(final Object proxy) { - if (AopUtils.isJdkDynamicProxy(proxy)) { - try { - return (T) getTargetObject(((Advised) proxy).getTargetSource().getTarget()); - } catch (final Exception e) { - throw new RuntimeException("Failed to unproxy target.", e); - } - } - return (T) proxy; - } -}