/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web.servlets.tests;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.MalformedURLException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.scotlandyard.engine.json.ExceptionJsonContainer;
import org.scotlandyard.engine.json.GamesJsonContainer;
import org.scotlandyard.engine.json.JsonFactory;
import org.xml.sax.SAXException;
import com.google.gson.Gson;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
/**
* TODO add description
*
* @author TIM
* @version 1.0
* @since Sept 2011
*
*/
public class AvailableGamesTest {
final static transient WebConversation WEB_CONVERSATION = new WebConversation();
final Gson GSON = new Gson();
ExceptionJsonContainer jec;
private final transient static String SERVER_URI = "http://localhost:8084/sy";
/**
* reset the game engine before doing any test
* in order to get the result you require
*
* @throws Exception
*/
@BeforeClass//TODO add description here
public static void beforeClass() throws Exception{
WebRequest req = new GetMethodWebRequest(SERVER_URI+"/ResetGameEngine"); // need to change to what your local host is
final WebResponse res = WEB_CONVERSATION.getResponse( req );
assertEquals(
"the result object should be of DefaultJsonContainer/JsonContainerAdapter",
"JsonContainerAdapter",
JsonFactory.fromJson(res.getText()).objectName
);
}
/**
* test if the get method in lobby returns atleast one game
*/
@Test //TODO add description here
public void testGetGames() throws MalformedURLException, IOException, SAXException
{
WebRequest req = new GetMethodWebRequest(SERVER_URI+"/AvailableGames"); // need to change to what your local host is
final WebResponse res = WEB_CONVERSATION.getResponse( req );
res.getResponseMessage();
//final org.w3c.dom.Document xml = res.getDOM();
final String response = res.getText();
Gson gson = new Gson();
GamesJsonContainer obj = gson.fromJson(response, GamesJsonContainer.class);
assertEquals(
"the value of games should be zero",
0,
obj.games.size()
);
}
@Test //TODO add description here
public void testCreateGame() throws MalformedURLException, IOException, SAXException
{
WebRequest req = new GetMethodWebRequest(SERVER_URI+"/GamePlayers"); // need to change to what your local host is
final WebResponse res = WEB_CONVERSATION.getResponse( req );
res.getResponseMessage();
final String response = res.getText().trim();
jec = GSON.fromJson(response, ExceptionJsonContainer.class);
assertEquals("is the user authenticated",
"Game id can not be null",
jec.message);
}
@Test //TODO add description here
public void testLoginFailed() throws MalformedURLException, IOException, SAXException
{
WebRequest req = new GetMethodWebRequest(SERVER_URI+"/Login"); // need to change to what your local host is
final WebResponse res = WEB_CONVERSATION.getResponse( req );
res.getResponseMessage();
final String response = res.getText().trim();
jec = GSON.fromJson(response, ExceptionJsonContainer.class);
assertEquals("is the user authenticated",
"email is not provided",
jec.message
);
}
@Test //TODO add description here
public void testLoginFailed2() throws MalformedURLException, IOException, SAXException
{
WebRequest req = new GetMethodWebRequest(SERVER_URI+"/Login"); // need to change to what your local host is
req.setParameter("name", "o");
final WebResponse res = WEB_CONVERSATION.getResponse( req );
res.getResponseMessage();
final String response = res.getText().trim();
jec = GSON.fromJson(response, ExceptionJsonContainer.class);
assertEquals("is the user authenticated",
"email is not provided",
jec.message
);
}
// @Test
// public void testCreateGame2() throws MalformedURLException, IOException, SAXException
// {
//
//
// WebResponse res;
// WebRequest req;
// String resText;
//
// req = new GetMethodWebRequest(SERVER_URI+"/index.jsp");
// res = WEB_CONVERSATION.getResponse( req );
//
// res = res.getFormWithID("form0").submit();
//
// assertEquals("user should be redirected to the login page",
// res.getURL(),SERVER_URI+"/login.jsp");
//
// res.getForms()[0].setParameter("name", "Hussain Al-Mutawa");
// res.getForms()[0].setParameter("email", "hussain@game.com");
//
// res.getForms()[0].getButtons()[0].click();
//
// resText = res.getText().trim();
//
// assertEquals("is the user authenticated", new JsonContainerAdapter().fromJson(resText).message , "done");
//
// assertEquals("user should be redirected to the lobby page",
// res.getURL(),SERVER_URI+"/lobby.jsp");
//
// //"{"games":[ ]}"
// req = new GetMethodWebRequest(SERVER_URI+"/AvailableGames");
// res = WEB_CONVERSATION.getResponse( req );
// resText = res.getText();
// assertEquals(resText,"{\"games\":[]}");
// assertEquals("test that the number of games is empty",
// new GamesJsonContainer().fromJson(resText).games.size(),0);
//
// }
}