package org.scotlandyard.tests.engine.json;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Iterator;
import org.junit.BeforeClass;
import org.junit.Test;
import org.scotlandyard.engine.GameException;
import org.scotlandyard.engine.json.UsersJsonContainer;
import org.scotlandyard.engine.json.objects.JsonUser;
import org.scotlandyard.impl.engine.GameEngine;
import org.scotlandyard.impl.engine.UserImpl;
/**
* TODO add a description for this class
*
* @author Hussain Al-Mutawa
* @version 1.0
* @since Sun Sep 23, 2011
*/
public class UsersJsonContainerTest {
//TODO add documentation
@BeforeClass
public final static void beforeClass()throws Exception{
GameEngine.instance().clearRecords();
}
@Test //TODO add description of what the test should do
public final void testUsersJsonContainerCollectionOfUser() throws GameException{
assertEquals(
"check if the number of users in the engine is zero",
0,
GameEngine.instance().getUsers().size()
);
final UsersJsonContainer ujc;
UserImpl.getNewInstance("user1", "email1");
UserImpl.getNewInstance("user2", "email2");
ujc = new UsersJsonContainer(GameEngine.instance().getUsers().values());
assertEquals(
"check if the number of users in the engine is two",
2,
GameEngine.instance().getUsers().size()
);
assertEquals(
"check if the number of users in the container is two",
2,
ujc.users.size()
);
final String json = ujc.toJson();
//System.out.println(json);
assertTrue(
"check if the number of users json string has email and name",
json.contains("\"email1\"") &&
json.contains("\"email2\"") &&
json.contains("\"user1\"") &&
json.contains("\"user2\"")
);
final UsersJsonContainer ujc2 = new UsersJsonContainer().fromJson(json);
assertEquals(
"check if the number of users in the converted container is two",
2,
ujc2.users.size()
);
final Iterator<JsonUser>it=ujc.users.iterator();
final JsonUser ju1 = it.next();
final JsonUser ju2 = it.next();
assertNotNull(
"check if the number of users container has its values initialized",
ju1
);
assertEquals(
"check if the number of users container has its values (email/name) initialized",
GameEngine.instance().getUser(ju1.email).getName(),
ju1.name
);
assertEquals(
"check if the number of users container has its values (email/name) initialized",
GameEngine.instance().getUser(ju2.email).getName(),
ju2.name
);
assertFalse(
"check if the iterator has reached the end",
it.hasNext()
);
}
@Test //TODO add description of what the test should do
public final void testGetObjectName(){
final UsersJsonContainer ujc = new UsersJsonContainer();
assertNull(
"check if the container users has not been initialized",
ujc.users
);
assertEquals(
"check if the name of the object is UsersJsonContainer",
"UsersJsonContainer",
ujc.objectName
);
}
}