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.json.JsonFactory;
import org.scotlandyard.impl.engine.GameEngine;
import web.servlets.AbstractServlet;
import web.servlets.Login;
import web.servlets.MockParametersMap;
/**
* TODO add description
*
* @author Hussain Al-Mutawa
* @version 1.0
* @since Sept 2011
*
*/
public class LoginTest {
public static transient final GameEngine ENGINE=GameEngine.instance();
public static transient AbstractServlet servlet;
public static transient final MockParametersMap map = new MockParametersMap();
@BeforeClass
@AfterClass // TODO add test description
public final static void beforeClass()throws Exception{
ENGINE.clearRecords();
servlet = new Login();
}
@Test // TODO add test description
public final void testGetOutput() throws Exception{
map.put("email", "hussain@email.com");
map.put("name","Hussain");
assertEquals(
"Test if the users record is empty",
0,
ENGINE.getUsers().size()
);
String output = servlet.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()
);
}
@Test // TODO add test description
public void testValidateRequest(){
int exceptionCounter=0;
exceptionCounter=this.runExceptionExpected(exceptionCounter,null,null);
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,null);
exceptionCounter=this.runExceptionExpected(exceptionCounter,null,ENGINE);
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("name",null);
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("name","Hussain");
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("email", "hussain@email.com");
map.remove("name");
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("email", "hussain@email.com");
map.put("name","");
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("name","Hussain");
map.put("email", "");
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("email", "hussain@email.com");
map.put("name",null);
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("name","Hussain");
map.put("email", null);
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("email", null);
map.put("name",null);
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
map.put("name",null);
map.put("email", "hussain@email.com");
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
exceptionCounter=this.runExceptionExpected(exceptionCounter,map,ENGINE);
assertEquals(
"check if all of the cases that can cause throwing an exception has been tested",
14,
exceptionCounter
);
}
protected int runExceptionExpected(
int exceptionCounter,
MockParametersMap pmap,
GameEngine engine){
int result=exceptionCounter;
//System.out.println(result);
try{
servlet.processRequest(pmap, engine);
}catch(Exception ex){result++;}
return result;
}
}