### Question:
LoginServlet extends RegistryBasedServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getParameter(LOGOUT) != null && req.getParameter(LOGOUT).equals("true")) { HttpSession session = req.getSession(); if (session != null) { session.invalidate(); } ServletHelper.respondAsHtmlUsingArgsAndTemplateWithHttpStatus(resp, RESOURCE_PAGE_FILE, HttpServletResponse.SC_OK, "Enter username and password"); } else { process(req, resp); } } LoginServlet(GridRegistry registry);  LoginServlet();  static final String LOGOUT; static final String FORM_ID; static final String USER_ID; static final String PASSWORD; }### Answer:
@Test public void testGetForNewLoginPage() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.doGet(request, response); validateForNewLoginPage(response); }
@Test public void testGetForNewLoginPageWithSessionPresent() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.getSession(true); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.doGet(request, response); validateForNewLoginPage(response); }
@Test public void testGetForLogout() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.getSession(true); request.addParameter(LoginServlet.LOGOUT, "true"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.doGet(request, response); validateForNewLoginPage(response); assertNull(request.getSession(false)); }
@Test public void testGetForLogin() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter(LoginServlet.FORM_ID, "login"); request.addParameter(LoginServlet.USER_ID, "admin"); request.addParameter(LoginServlet.PASSWORD, "admin"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.doGet(request, response); validateLoginSuccess(request, response, SeLionGridConstants.GRID_HOME_PAGE_URL); }
@Test public void testGetForLoginFailure() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.getSession(true); request.addParameter(LoginServlet.FORM_ID, "login"); request.addParameter(LoginServlet.USER_ID, "admin"); request.addParameter(LoginServlet.PASSWORD, "password"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.doGet(request, response); validateLoginFailure(request, response); }
@Test public void testGetForLoginWithSessionCredentialsPresent() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); HttpSession session = request.getSession(true); session.setAttribute(LoginServlet.USER_ID, "admin"); session.setAttribute(LoginServlet.PASSWORD, "admin"); request.addParameter(LoginServlet.FORM_ID, "login"); request.addParameter(LoginServlet.USER_ID, "admin"); request.addParameter(LoginServlet.PASSWORD, "password"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.doGet(request, response); validateLoginSuccess(request, response, SeLionGridConstants.GRID_HOME_PAGE_URL); }