test(DevWeb): Add tests in DAO_JPA_UserTest

This commit is contained in:
Lucàs
2024-03-28 00:43:46 +01:00
parent d839bbff75
commit b2af76ccf1
@@ -1,44 +1,136 @@
package uppa.project.dao.jpa;
import jakarta.persistence.EntityManager;
import java.util.Date;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uppa.project.dao.DAO;
import uppa.project.dao.DAOException;
import uppa.project.pojo.User;
import uppa.project.provider.EntityManagerProvider;
import static org.junit.jupiter.api.Assertions.*;
class DAO_JPA_UserTest {
@Test
void findById() {
// TODO Implement this method
fail();
static EntityManager entityManager;
static DAO<User> dao;
static User[] users;
@BeforeAll
static void setUp() throws DAOException {
EntityManagerProvider.setPersitenceUnitName("test");
entityManager = EntityManagerProvider.getInstance();
dao = new Game_JPA_DAO_Factory().getDAOUser();
assertEquals(dao.getClass(), DAO_JPA_User.class);
}
@BeforeEach
void clean() {
users = new User[] {
new User("username", "email", "password", new Date(), User.Gender.MALE),
new User("username1", "email1", "password1", new Date(), User.Gender.FEMALE),
};
entityManager.getTransaction().begin();
}
@AfterEach
void rollback() {
entityManager.getTransaction().rollback();
}
@Test
void findByField() {
// TODO Implement this method
fail();
void findById() throws DAOException {
// Create a new user
dao.create(users[0]);
User user = dao.findAll()[0];
// Find the user by id
User userInDb = dao.findById(user.getId().intValue());
assertEquals(user.getId(), userInDb.getId());
}
@Test
void findAll() {
// TODO Implement this method
fail();
void findByField() throws DAOException {
// Create users
for (User user : users) dao.create(user);
// Find the user by username
User[] usersInDb = dao.findByField("username", users[0].getUsername());
assertEquals(1, usersInDb.length);
// Check if the user is the same
assertEquals(users[0].getUsername(), usersInDb[0].getUsername());
assertEquals(users[0].getEmail(), usersInDb[0].getEmail());
assertEquals(users[0].getPassword(), usersInDb[0].getPassword());
}
@Test
void create() {
// TODO Implement this method
fail();
void findAll() throws DAOException {
// Find all users
User[] users = dao.findAll();
assertEquals(0, users.length);
// Create a new user
for (User user : users) dao.create(user);
// Find all users
User[] usersInDb = dao.findAll();
assertEquals(users.length, usersInDb.length);
}
@Test
void update() {
// TODO Implement this method
fail();
void create() throws DAOException {
// Create users
for (User user : users) {
dao.create(user);
}
// Check if the user is in the database
User[] usersInDb = dao.findAll();
assertEquals(users.length, usersInDb.length);
// Check if the user is the same
for (int i = 0; i < users.length; i++) {
assertEquals(users[i].getUsername(), usersInDb[i].getUsername());
assertEquals(users[i].getEmail(), usersInDb[i].getEmail());
assertEquals(users[i].getPassword(), usersInDb[i].getPassword());
assertEquals(users[i].getBirth(), usersInDb[i].getBirth());
assertEquals(users[i].getGender(), usersInDb[i].getGender());
}
}
@Test
void delete() {
// TODO Implement this method
fail();
void update() throws DAOException {
// Create a new user
dao.create(users[0]);
User user = dao.findAll()[0];
// Update the user
user.setUsername("new_username");
dao.update(user);
// Check if the user is in the database
User[] usersInDb = dao.findAll();
assertEquals(1, usersInDb.length);
// Check if the user is the same
assertEquals(user.getUsername(), usersInDb[0].getUsername());
}
@Test
void delete() throws DAOException {
// Create a new user
dao.create(users[0]);
User user = dao.findAll()[0];
// Delete the user
dao.delete(user);
// Check if the user is not in the database
assertNull(dao.findById(user.getId().intValue()));
}
}