Some important changes to check what kind of error occures on request

This patch uses AbstractController class to add exception handlers
into controllers. These handlers are used to detect some runtime exceptions
which can be thrown in any layer e.g. DAO or service layer.
They recognize NotFoundException and ForbiddenException. Test classes are
able to check if any of these exceptions are thrown during runtime.
parent 11cedd1b
package de.thm.arsnova.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import de.thm.arsnova.exceptions.ForbiddenException;
import de.thm.arsnova.exceptions.NotFoundException;
public class AbstractController {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
public void handleNotFoundException(Exception e, HttpServletRequest request) {
}
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(ForbiddenException.class)
public void handleForbiddenException(Exception e, HttpServletRequest request) {
}
}
......@@ -54,7 +54,7 @@ import de.thm.arsnova.entities.User;
import de.thm.arsnova.services.IUserService;
@Controller
public class LoginController {
public class LoginController extends AbstractController {
@Autowired
TwitterProvider twitterProvider;
......
......@@ -39,15 +39,13 @@ import org.springframework.web.bind.annotation.ResponseBody;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.exceptions.ForbiddenException;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.services.ISessionService;
import de.thm.arsnova.services.IUserService;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
import de.thm.arsnova.socket.message.Question;
@Controller
public class SessionController {
public class SessionController extends AbstractController {
public static final Logger logger = LoggerFactory.getLogger(SessionController.class);
......@@ -74,26 +72,14 @@ public class SessionController {
@RequestMapping(value="/session/{sessionkey}", method=RequestMethod.GET)
@ResponseBody
public Session getSession(@PathVariable String sessionkey, HttpServletResponse response) {
try {
Session session = sessionService.getSession(sessionkey);
if (session != null) return session;
} catch (NotFoundException e) {
response.setStatus(HttpStatus.NOT_FOUND.value());
} catch (ForbiddenException e) {
response.setStatus(HttpStatus.FORBIDDEN.value());
}
return null;
public Session getSession(@PathVariable String sessionkey) {
return sessionService.getSession(sessionkey);
}
@RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.GET)
@ResponseBody
public Feedback getFeedback(@PathVariable String sessionkey, HttpServletResponse response) {
Feedback feedback = sessionService.getFeedback(sessionkey);
if (feedback != null) return feedback;
response.setStatus(HttpStatus.NOT_FOUND.value());
return null;
public Feedback getFeedback(@PathVariable String sessionkey) {
return sessionService.getFeedback(sessionkey);
}
@RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.POST)
......
......@@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class WelcomeController {
public class WelcomeController extends AbstractController {
@RequestMapping(method = RequestMethod.GET, value = "/")
public ModelAndView home(HttpServletRequest request) {
String referer = request.getHeader("referer");
......
......@@ -199,8 +199,7 @@ public class CouchDBDao implements IDatabaseDao {
@Override
public Feedback getFeedback(String keyword) {
String sessionId = this.getSessionId(keyword);
if (sessionId == null)
return null;
if (sessionId == null) throw new NotFoundException();
logger.info("Time: {}", this.currentTimestamp());
......@@ -209,11 +208,10 @@ public class CouchDBDao implements IDatabaseDao {
view.setStartKey(URLEncoder.encode("[\"" + sessionId + "\"]"));
view.setEndKey(URLEncoder.encode("[\"" + sessionId + "\",{}]"));
ViewResults results = this.getDatabase().view(view);
logger.info("Feedback: {}", results.getJSONArray("rows"));
int values[] = { 0, 0, 0, 0 };
List<Integer> result = new ArrayList<Integer>();
try {
for (int i = 0; i <= 3; i++) {
......@@ -233,12 +231,7 @@ public class CouchDBDao implements IDatabaseDao {
.getInt("value");
}
} catch (Exception e) {
return new Feedback(
values[0],
values[1],
values[2],
values[3]
);
throw new NotFoundException();
}
return new Feedback(
......
......@@ -19,13 +19,14 @@ import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import de.thm.arsnova.exceptions.ForbiddenException;
import de.thm.arsnova.exceptions.NotFoundException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
@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"
})
"file:src/test/resources/test-config.xml" })
public class SessionControllerTest {
@Inject
......@@ -33,42 +34,72 @@ public class SessionControllerTest {
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);
handlerAdapter = applicationContext
.getBean(AnnotationMethodHandlerAdapter.class);
}
@Test
public void testShouldNotGetUnknownSession() {
request.setMethod("GET");
request.setRequestURI("/session/00000000");
try {
final ModelAndView mav = handlerAdapter.handle(request, response, sessionController);
final ModelAndView mav = handlerAdapter.handle(request, response,
sessionController);
assertNull(mav);
assertTrue(response.getStatus() == 404);
} catch (NotFoundException e) {
return;
} catch (Exception e) {
e.printStackTrace();
fail("An exception occured");
}
}
fail("Expected exception 'NotFoundException' did not occure");
}
@Test
public void testShouldNotGetForbiddenSession() {
request.setMethod("GET");
request.setRequestURI("/session/99999999");
try {
final ModelAndView mav = handlerAdapter.handle(request, response, sessionController);
final ModelAndView mav = handlerAdapter.handle(request, response,
sessionController);
assertNull(mav);
assertTrue(response.getStatus() == 403);
} catch (ForbiddenException e) {
return;
} catch (Exception e) {
e.printStackTrace();
fail("An exception occured");
}
fail("Expected exception 'ForbiddenException' did not occure");
}
@Test
public void testShouldNotGetFeedbackForUnknownSession() {
request.setMethod("GET");
request.setRequestURI("/session/00000000/feedback");
try {
final ModelAndView mav = handlerAdapter.handle(request, response,
sessionController);
assertNull(mav);
assertTrue(response.getStatus() == 404);
} catch (NotFoundException e) {
return;
} catch (Exception e) {
e.printStackTrace();
fail("An exception occured");
}
}
fail("Expected exception 'NotFoundException' did not occure");
}
}
......@@ -79,7 +79,13 @@ public class StubDatabaseDao implements IDatabaseDao {
@Override
public Feedback getFeedback(String keyword) {
return stubFeedbacks.get(keyword);
// Magic keyword for forbidden session
if (keyword.equals("99999999")) throw new ForbiddenException();
Feedback feedback = stubFeedbacks.get(keyword);
if (feedback == null) throw new NotFoundException();
return feedback;
}
@Override
......
......@@ -27,6 +27,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import de.thm.arsnova.exceptions.NotFoundException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/webapp/WEB-INF/arsnova-servlet.xml",
......@@ -40,7 +42,13 @@ public class SessionServiceTest {
@Test
public void testShouldFail() {
if (sessionService.getFeedback("00000000") != null) fail("Result is not null");
try {
sessionService.getFeedback("00000000");
} catch (NotFoundException e) {
return;
}
fail("Expected exception 'NotFoundException' did not occure");
}
@Test
......
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