diff --git a/pom.xml b/pom.xml index d09601cca31f8960157a084665aa50dac2f24a0c..d3074c4e9f0161e1c7ef5a084053e8df19296640 100644 --- a/pom.xml +++ b/pom.xml @@ -215,6 +215,7 @@ <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> + <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> @@ -239,6 +240,12 @@ <artifactId>netty-socketio</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> + <dependency> + <groupId>javax.inject</groupId> + <artifactId>javax.inject</artifactId> + <version>1</version> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> @@ -275,6 +282,17 @@ <locales>en</locales> </configuration> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.12.2</version> + <configuration> + <includes> + <include>**/SessionControllerTest.java</include> + <include>**/SessionServiceTest.java</include> + </includes> + </configuration> + </plugin> </plugins> </build> diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index b175592525fda6b39989e454f13ed648ae6aaf53..4edd169b5e5bceac05261c6889c9802720a546fb 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -54,37 +54,41 @@ public class SessionService implements ISessionService { private static final ConcurrentHashMap<String, String> user2session = new ConcurrentHashMap<String, String>(); @Autowired - IDatabaseDao sessionDao; + IDatabaseDao databaseDao; + + public void setDatabaseDao(IDatabaseDao databaseDao) { + this.databaseDao = databaseDao; + } @Override @Scheduled(fixedDelay=5000) public void cleanFeedbackVotes() { - sessionDao.cleanFeedbackVotes(cleanupFeedbackDelay); + databaseDao.cleanFeedbackVotes(cleanupFeedbackDelay); } @Override public Session getSession(String keyword) { - return sessionDao.getSession(keyword); + return databaseDao.getSession(keyword); } @Override public Session saveSession(Session session) { - return sessionDao.saveSession(session); + return databaseDao.saveSession(session); } @Override public Feedback getFeedback(String keyword) { - return sessionDao.getFeedback(keyword); + return databaseDao.getFeedback(keyword); } @Override public boolean saveFeedback(String keyword, int value, User user) { - return sessionDao.saveFeedback(keyword, value, user); + return databaseDao.saveFeedback(keyword, value, user); } @Override public boolean sessionKeyAvailable(String keyword) { - return sessionDao.sessionKeyAvailable(keyword); + return databaseDao.sessionKeyAvailable(keyword); } @Override diff --git a/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java b/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..74804a308a04d7f568212f18d5a472283bc26ed8 --- /dev/null +++ b/src/test/java/de/thm/arsnova/controller/SessionControllerTest.java @@ -0,0 +1,60 @@ +package de.thm.arsnova.controller; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +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; + +import de.thm.arsnova.SessionController; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations={ + "file:src/main/webapp/WEB-INF/arsnova-servlet.xml", + "file:src/main/webapp/WEB-INF/spring/spring-main.xml", + "file:src/test/resources/test-config.xml" +}) +public class SessionControllerTest { + + @Inject + private ApplicationContext applicationContext; + private MockHttpServletRequest request; + private MockHttpServletResponse response; + private HandlerAdapter handlerAdapter; + + @Autowired + private SessionController sessionController; + + @Before + public void setUp() { + this.request = new MockHttpServletRequest(); + this.response = new MockHttpServletResponse(); + handlerAdapter = applicationContext.getBean(AnnotationMethodHandlerAdapter.class); + } + + @Test + public void testShouldNotGetMissingSession() { + request.setMethod("GET"); + request.setRequestURI("/session/12345678"); + try { + final ModelAndView mav = handlerAdapter.handle(request, response, sessionController); + assertNull(mav); + } catch (Exception e) { + e.printStackTrace(); + fail("An exception occured"); + } + + } +} diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java index a531f00f32d21da131a2ac38f84c870986a750cb..10fa394a6c1e524aca7eba8e68dd71e63245f59d 100644 --- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java +++ b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java @@ -4,15 +4,49 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; +@Component +@Scope("singleton") public class StubDatabaseDao implements IDatabaseDao { private Map<String, Session> stubSessions = new ConcurrentHashMap<String, Session>(); private Map<String, Feedback> stubFeedbacks = new ConcurrentHashMap<String, Feedback>(); + public StubDatabaseDao() { + fillWithDummySessions(); + fillWithDummyFeedbacks(); + } + + private void fillWithDummySessions() { + Session session = new Session(); + session.setActive(true); + session.setCreator("ptsr00"); + session.setKeyword("12345678"); + session.setName("TestSession1"); + session.setShortName("TS1"); + + this.stubSessions.put("12345678", session); + + session.setActive(true); + session.setCreator("ptsr00"); + session.setKeyword("87654321"); + session.setName("TestSession2"); + session.setShortName("TS2"); + + this.stubSessions.put("87654321", session); + } + + private void fillWithDummyFeedbacks() { + stubFeedbacks.put("12345678", new Feedback(0, 0, 0, 0)); + stubFeedbacks.put("87654321", new Feedback(2, 3, 5, 7)); + } + @Override public void cleanFeedbackVotes(int cleanupFeedbackDelay) { stubSessions.clear(); @@ -57,7 +91,8 @@ public class StubDatabaseDao implements IDatabaseDao { @Override public boolean sessionKeyAvailable(String keyword) { - return (stubSessions.get(keyword) != null); + System.out.println(stubSessions.get(keyword)); + return (stubSessions.get(keyword) == null); } } diff --git a/src/test/java/de/thm/arsnova/services/SessionServiceTest.java b/src/test/java/de/thm/arsnova/services/SessionServiceTest.java index 9bf9798332842bdd8e90a416b38367261c5c2e19..d2b0b41a882e5f5c333df0bc64db7314ff4b4294 100644 --- a/src/test/java/de/thm/arsnova/services/SessionServiceTest.java +++ b/src/test/java/de/thm/arsnova/services/SessionServiceTest.java @@ -18,16 +18,34 @@ */ package de.thm.arsnova.services; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations={ + "file:src/main/webapp/WEB-INF/arsnova-servlet.xml", + "file:src/main/webapp/WEB-INF/spring/spring-main.xml", + "file:src/test/resources/test-config.xml" +}) public class SessionServiceTest { + + @Autowired + ISessionService sessionService; + + @Test + public void testShouldFail() { + if (sessionService.getFeedback("00000000") != null) fail("Result is not null"); + } @Test - public void shouldGenerateSessionKeyword() { - SessionService session = new SessionService(); - System.out.println(session.generateKeyword()); - assertTrue(session.generateKeyword().matches("^[0-9]{8}$")); + public void testShouldGenerateSessionKeyword() { + System.out.println(sessionService.generateKeyword()); + assertTrue(sessionService.generateKeyword().matches("^[0-9]{8}$")); } } \ No newline at end of file diff --git a/src/test/resources/test-config.xml b/src/test/resources/test-config.xml new file mode 100644 index 0000000000000000000000000000000000000000..db94937d9b63d296ec9c3b8423b22c1903e5e80e --- /dev/null +++ b/src/test/resources/test-config.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> + + <bean id="sessionService" class="de.thm.arsnova.services.SessionService"> + <property name="databaseDao" ref="databaseDao"></property> + </bean> + + <bean id="databaseDao" class="de.thm.arsnova.dao.StubDatabaseDao"> + </bean> + +</beans>