Package web.servlets.tests

Source Code of web.servlets.tests.LogoutTest

package web.servlets.tests;

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.scotlandyard.engine.GameException;
import org.scotlandyard.engine.constants.LoginStatus;
import org.scotlandyard.engine.json.JsonFactory;
import org.scotlandyard.engine.json.LoginStatusJsonContainer;
import org.scotlandyard.impl.engine.GameEngine;
import org.scotlandyard.impl.engine.UserImpl;

import web.servlets.AbstractServlet;
import web.servlets.GetLoginStatus;
import web.servlets.Login;
import web.servlets.Logout;
import web.servlets.MockParametersMap;
/**
* TODO add description
*
* @author Hussain Al-Mutawa
* @version 1.0
* @since Sept 2011
*
*/
public class LogoutTest{
 
 
  public static transient final GameEngine ENGINE=GameEngine.instance();
  public static transient AbstractServlet servlet;
  public static transient final MockParametersMap map = new MockParametersMap();
  public static transient final String sessionId="8q736871623wquyuqtwed871236";
 
  @BeforeClass
  @AfterClass  // TODO add test description
  public final static void beforeClass()throws Exception{
    ENGINE.clearRecords();
    servlet = new Logout();
  }
 
 
  @Test  // TODO add test description
  public synchronized final void testGetOutput() throws Exception{
    final Login login=new Login();
    map.put("sessionId", sessionId);
    map.put("email", "hussain@email.com");
    map.put("name","Hussain");
   
    assertEquals(
        "Test if the users record is empty",
        0,
        ENGINE.getUsers().size()
        );
   
    String output = login.processRequest(map, ENGINE).toString();
    assertEquals(
        "Test if login is successful",
        "done",
        JsonFactory.fromJson(output).message
        );
    assertEquals(
        "Test if the users record is not empty",
        1,
        ENGINE.getUsers().size()
        );
    output = new GetLoginStatus().processRequest(map, ENGINE).toString();
    assertEquals(
        "Test if login is successful",
        LoginStatus.LoggedIn,
        new LoginStatusJsonContainer().fromJson(output).loginStatus
        );
   
    output = servlet.processRequest(map, ENGINE).toString();
    assertEquals(
        "Test if login is successful",
        LoginStatus.LoggedOut,
        new LoginStatusJsonContainer().fromJson(output).loginStatus
        );
    assertEquals(
        "Test if the users record is not empty",
        0,
        ENGINE.getUsers().size()
        );
   
  }
 
  @Test  // TODO add test description
  public void testValidateRequest() throws GameException{
    int exceptionCounter=0;
   
    exceptionCounter=this.runExceptionExpected(exceptionCounter,null,null);
   
    exceptionCounter=this.runExceptionExpected(exceptionCounter,map,null);
    exceptionCounter=this.runExceptionExpected(exceptionCounter,null,ENGINE);
    map.put("sessionId","");
    exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
    UserImpl.getNewInstance("ss", "dd");
    map.put("sessionId","daslkfnsdklfnsdc");
    exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
    map.put("sessionId",null);
    exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
   
    map.put("sessionId",sessionId);
    exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
   
    map.remove("sessionId");
    exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
    assertEquals(
        "check if all of the cases that can cause throwing an exception has been tested",
        8,
        exceptionCounter
        );
   
  }
 
  protected int runExceptionExpected(
      int exceptionCounter,
      MockParametersMap pmap,
      GameEngine engine){
    int result=exceptionCounter;
    try{
      servlet.processRequest(pmap, engine);
    }catch(Exception ex){
      result++;
      System.out.println(result+" | "+ex.getMessage());
      }
    return result;
  }

}
TOP

Related Classes of web.servlets.tests.LogoutTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.