Commit 501e1306 authored by Christoph Thelen's avatar Christoph Thelen

Implemented #5921: Delete session and all its data

parent f915364f
......@@ -313,6 +313,12 @@
<version>2.25.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.codehaus.groovy</groupId>
......
......@@ -64,6 +64,13 @@ public class SessionController extends AbstractController {
return sessionService.joinSession(sessionkey);
}
@RequestMapping(value = "/{sessionkey}", method = RequestMethod.DELETE)
@ResponseBody
public final void deleteSession(@PathVariable final String sessionkey) {
User user = userService.getCurrentUser();
sessionService.deleteSession(sessionkey, user);
}
@RequestMapping(value = "/{sessionkey}/online", method = RequestMethod.POST)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
......
......@@ -773,7 +773,7 @@ public class CouchDBDao implements IDatabaseDao {
}
@Override
public final void deleteQuestion(final Question question) {
public final void deleteQuestionWithAnswers(final Question question) {
try {
this.deleteAnswers(question);
this.deleteDocument(question.get_id());
......@@ -1508,4 +1508,13 @@ public class CouchDBDao implements IDatabaseDao {
}
return null;
}
@Override
public void deleteSession(Session session) {
try {
this.deleteDocument(session.get_id());
} catch (IOException e) {
LOGGER.error("Could not delete session {}", session);
}
}
}
......@@ -68,7 +68,7 @@ public interface IDatabaseDao {
List<String> getQuestionIds(Session session, User user);
void deleteQuestion(Question question);
void deleteQuestionWithAnswers(Question question);
List<String> getUnAnsweredQuestionIds(Session session, User user);
......@@ -135,4 +135,6 @@ public interface IDatabaseDao {
Session lockSession(Session session, Boolean lock);
List<String> getActiveUsers(int timeDifference);
void deleteSession(Session session);
}
......@@ -49,4 +49,6 @@ public interface ISessionService {
Session setActive(String sessionkey, Boolean lock);
Session joinSession(String keyword, UUID socketId);
void deleteSession(String sessionkey, User user);
}
......@@ -146,7 +146,7 @@ public class QuestionService implements IQuestionService {
if (user == null || session == null || !session.isCreator(user)) {
throw new UnauthorizedException();
}
databaseDao.deleteQuestion(question);
databaseDao.deleteQuestionWithAnswers(question);
}
@Override
......
......@@ -35,6 +35,7 @@ import de.thm.arsnova.connector.client.ConnectorClient;
import de.thm.arsnova.connector.model.Course;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.exceptions.ForbiddenException;
......@@ -223,4 +224,17 @@ public class SessionService implements ISessionService {
Session session = databaseDao.getSessionFromKeyword(sessionkey);
return databaseDao.lockSession(session, lock);
}
@Override
@Authenticated
public void deleteSession(String sessionkey, User user) {
Session session = databaseDao.getSession(sessionkey);
if (!session.isCreator(user)) {
throw new ForbiddenException();
}
for (Question q : databaseDao.getSkillQuestions(sessionkey)) {
databaseDao.deleteQuestionWithAnswers(q);
}
databaseDao.deleteSession(session);
}
}
......@@ -379,7 +379,7 @@ public class StubDatabaseDao implements IDatabaseDao {
}
@Override
public void deleteQuestion(Question question) {
public void deleteQuestionWithAnswers(Question question) {
// TODO Auto-generated method stub
}
......@@ -432,4 +432,9 @@ public class StubDatabaseDao implements IDatabaseDao {
// TODO Auto-generated method stub
return null;
}
@Override
public void deleteSession(Session session) {
// TODO Auto-generated method stub
}
}
......@@ -22,14 +22,23 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import static org.mockito.Mockito.*;
import org.junit.After;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.dao.StubDatabaseDao;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.exceptions.UnauthorizedException;
......@@ -55,7 +64,7 @@ public class SessionServiceTest {
databaseDao.cleanupTestData();
userService.setUserAuthenticated(false);
}
@Test
public void testShouldGenerateSessionKeyword() {
System.out.println(sessionService.generateKeyword());
......@@ -104,4 +113,37 @@ public class SessionServiceTest {
sessionService.saveSession(session);
assertNotNull(sessionService.joinSession("11111111"));
}
@Test
public void testShouldDeleteAllSessionData() {
userService.setUserAuthenticated(true);
Session session = new Session();
session.setCreator(userService.getCurrentUser().getUsername());
Question q1 = new Question();
Question q2 = new Question();
IDatabaseDao mockDatabase = mock(IDatabaseDao.class);
when(mockDatabase.getSkillQuestions(anyString())).thenReturn(Arrays.asList(q1, q2));
when(mockDatabase.getSession(anyString())).thenReturn(session);
ReflectionTestUtils.setField(getTargetObject(sessionService), "databaseDao", mockDatabase);
sessionService.deleteSession(session.getKeyword(), userService.getCurrentUser());
verify(mockDatabase).deleteQuestionWithAnswers(q1);
verify(mockDatabase).deleteQuestionWithAnswers(q2);
verify(mockDatabase).deleteSession(session);
}
@SuppressWarnings("unchecked")
public static <T> T getTargetObject(Object proxy) {
if ((AopUtils.isJdkDynamicProxy(proxy))) {
try {
return (T) getTargetObject(((Advised) proxy).getTargetSource().getTarget());
} catch (Exception e) {
throw new RuntimeException("Failed to unproxy target.", e);
}
}
return (T) proxy;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment