mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-07-09 13:27:45 +00:00
test(DevWeb): Add tests in DAO_JPA_UserTest
This commit is contained in:
@@ -1,44 +1,136 @@
|
|||||||
package uppa.project.dao.jpa;
|
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 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.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class DAO_JPA_UserTest {
|
class DAO_JPA_UserTest {
|
||||||
|
|
||||||
@Test
|
static EntityManager entityManager;
|
||||||
void findById() {
|
static DAO<User> dao;
|
||||||
// TODO Implement this method
|
static User[] users;
|
||||||
fail();
|
|
||||||
|
@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
|
@Test
|
||||||
void findByField() {
|
void findById() throws DAOException {
|
||||||
// TODO Implement this method
|
// Create a new user
|
||||||
fail();
|
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
|
@Test
|
||||||
void findAll() {
|
void findByField() throws DAOException {
|
||||||
// TODO Implement this method
|
// Create users
|
||||||
fail();
|
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
|
@Test
|
||||||
void create() {
|
void findAll() throws DAOException {
|
||||||
// TODO Implement this method
|
// Find all users
|
||||||
fail();
|
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
|
@Test
|
||||||
void update() {
|
void create() throws DAOException {
|
||||||
// TODO Implement this method
|
// Create users
|
||||||
fail();
|
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
|
@Test
|
||||||
void delete() {
|
void update() throws DAOException {
|
||||||
// TODO Implement this method
|
// Create a new user
|
||||||
fail();
|
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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user