import com.mongodb.*;
import course.UserDAO;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import java.net.UnknownHostException;
import static org.junit.Assert.*;
public class UserDAOTest {
static MongoClient mongoClient;
static DB blogDatabase;
static DBCollection usersCollection;
static UserDAO userDAO;
// this method called before all test (once), to initialize DB variables
@BeforeClass
public static void setUp() throws UnknownHostException {
mongoClient = new MongoClient();
blogDatabase = mongoClient.getDB("blog");
usersCollection = blogDatabase.getCollection("users");
userDAO = new UserDAO(blogDatabase);
usersCollection.drop();
}
//this method calls after every test to clean usersCollection
@After
public void dropCollection(){
usersCollection.drop();
}
@Test
public void addUserTest() {
System.out.println("Starting test on adding user");
String email = "b1102@email.ru";
userDAO.addUser("b1102" , "1111" , email );
DBObject find = new BasicDBObject("_id" , "b1102" );
DBObject user = usersCollection.find(find).next();
assertTrue(user.get("email").equals(email));
System.out.println("User successfully added");
System.out.println("");
}
@Test
public void addUserThatExistsTest() throws UnknownHostException {
System.out.println("Starting test on adding user that already exists");
String email = "b1102@email.ru";
userDAO.addUser("b1102", "1111", email);
assertFalse(userDAO.addUser("b1102", "dedede", "e3r3r"));
System.out.println("Test on adding user that already exists passed");
System.out.println("");
}
@Test
public void CheckUserPassPositive() throws UnknownHostException {
System.out.println("Starting positive test on checking user's pass");
String login = "b1102";
String email = "b1102@email.ru";
userDAO.addUser(login, "1111", email);
DBObject validatedUser = userDAO.validateLogin(login , "1111");
DBObject foundUser = usersCollection.find(new BasicDBObject("_id" , login)).next();
assertEquals(foundUser, validatedUser);
System.out.println("Positive test on checking user's pass passed");
System.out.println("");
}
@Test
public void CheckUserPassNegative() throws UnknownHostException {
System.out.println("Starting negative test on checking user's pass");
String login = "b1102";
String email = "b1102@email.ru";
userDAO.addUser(login, "1111", email);
assertNull(userDAO.validateLogin(login , "1112"));
System.out.println("Negative test on checking user's pass passed");
System.out.println("");
}
@Test
public void GetNotExistUser() throws UnknownHostException {
System.out.println("Starting test on getting not exists user");
String login = "b1102";
String email = "b1102@email.ru";
userDAO.addUser(login, "1111", email);
assertNull(userDAO.validateLogin(login+"1" , "1111"));
System.out.println("Test on getting not exists user passed");
System.out.println("");
}
}
import course.UserDAO;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import java.net.UnknownHostException;
import static org.junit.Assert.*;
public class UserDAOTest {
static MongoClient mongoClient;
static DB blogDatabase;
static DBCollection usersCollection;
static UserDAO userDAO;
// this method called before all test (once), to initialize DB variables
@BeforeClass
public static void setUp() throws UnknownHostException {
mongoClient = new MongoClient();
blogDatabase = mongoClient.getDB("blog");
usersCollection = blogDatabase.getCollection("users");
userDAO = new UserDAO(blogDatabase);
usersCollection.drop();
}
//this method calls after every test to clean usersCollection
@After
public void dropCollection(){
usersCollection.drop();
}
@Test
public void addUserTest() {
System.out.println("Starting test on adding user");
String email = "b1102@email.ru";
userDAO.addUser("b1102" , "1111" , email );
DBObject find = new BasicDBObject("_id" , "b1102" );
DBObject user = usersCollection.find(find).next();
assertTrue(user.get("email").equals(email));
System.out.println("User successfully added");
System.out.println("");
}
@Test
public void addUserThatExistsTest() throws UnknownHostException {
System.out.println("Starting test on adding user that already exists");
String email = "b1102@email.ru";
userDAO.addUser("b1102", "1111", email);
assertFalse(userDAO.addUser("b1102", "dedede", "e3r3r"));
System.out.println("Test on adding user that already exists passed");
System.out.println("");
}
@Test
public void CheckUserPassPositive() throws UnknownHostException {
System.out.println("Starting positive test on checking user's pass");
String login = "b1102";
String email = "b1102@email.ru";
userDAO.addUser(login, "1111", email);
DBObject validatedUser = userDAO.validateLogin(login , "1111");
DBObject foundUser = usersCollection.find(new BasicDBObject("_id" , login)).next();
assertEquals(foundUser, validatedUser);
System.out.println("Positive test on checking user's pass passed");
System.out.println("");
}
@Test
public void CheckUserPassNegative() throws UnknownHostException {
System.out.println("Starting negative test on checking user's pass");
String login = "b1102";
String email = "b1102@email.ru";
userDAO.addUser(login, "1111", email);
assertNull(userDAO.validateLogin(login , "1112"));
System.out.println("Negative test on checking user's pass passed");
System.out.println("");
}
@Test
public void GetNotExistUser() throws UnknownHostException {
System.out.println("Starting test on getting not exists user");
String login = "b1102";
String email = "b1102@email.ru";
userDAO.addUser(login, "1111", email);
assertNull(userDAO.validateLogin(login+"1" , "1111"));
System.out.println("Test on getting not exists user passed");
System.out.println("");
}
}
No comments:
Post a Comment