From a78dcc63042cb5c01c91a10c52183db5d8947f9e Mon Sep 17 00:00:00 2001 From: kmitresse Date: Wed, 1 May 2024 21:28:36 +0200 Subject: [PATCH] end: devWeb - ending project --- .../project/bean/ForgottenPasswordBean.java | 1 - .../uppa/project/database/pojo/Player.java | 21 + .../java/uppa/project/database/pojo/User.java | 1 - .../project/json/websocket/SimplePlayer.java | 10 +- .../project/json/websocket/SimpleUser.java | 1 + .../project/web/servlet/ProfileServlet.java | 1 - .../uppa/project/web/websocket/GameWS.java | 11 +- .../src/main/webapp/WEB-INF/pages/game.jsp | 2 +- .../src/main/webapp/WEB-INF/pages/rules.jsp | 21 +- .../main/webapp/WEB-INF/tags/forms/login.tag | 2 - .../webapp/WEB-INF/tags/forms/new-game.tag | 2 - .../webapp/WEB-INF/tags/forms/profile.tag | 5 - .../src/main/webapp/static/js/Message.js | 1 - .../project/dao/AbstractDAOFactoryTest.java | 37 -- .../project/dao/jpa/DAO_JPA_GameTest.java | 161 -------- .../project/dao/jpa/DAO_JPA_PlayerTest.java | 151 ------- .../DAO_JPA_RecoveryPasswordTokenTest.java | 149 ------- .../project/dao/jpa/DAO_JPA_UserTest.java | 135 ------- .../dao/jpa/Game_JPA_DAO_FactoryTest.java | 78 ---- .../test/java/uppa/project/pojo/CardTest.java | 132 ------- .../test/java/uppa/project/pojo/DeckTest.java | 138 ------- .../test/java/uppa/project/pojo/GameTest.java | 333 ---------------- .../java/uppa/project/pojo/PlayerTest.java | 337 ---------------- .../pojo/RecoveryPasswordTokenTest.java | 152 ------- .../test/java/uppa/project/pojo/UserTest.java | 370 ------------------ .../provider/EntityManagerProviderTest.java | 42 -- 26 files changed, 48 insertions(+), 2246 deletions(-) delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/dao/AbstractDAOFactoryTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_GameTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_PlayerTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_RecoveryPasswordTokenTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_UserTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/Game_JPA_DAO_FactoryTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/pojo/CardTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/pojo/DeckTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/pojo/GameTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/pojo/PlayerTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/pojo/RecoveryPasswordTokenTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/pojo/UserTest.java delete mode 100644 S2/DevWeb/Projet/src/test/java/uppa/project/provider/EntityManagerProviderTest.java diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ForgottenPasswordBean.java b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ForgottenPasswordBean.java index 402501f..8c810ab 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ForgottenPasswordBean.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ForgottenPasswordBean.java @@ -134,7 +134,6 @@ public class ForgottenPasswordBean implements Serializable { // Envoi du message Transport.send(message); - System.out.println("E-mail envoyé avec succès à : " + email); } catch (MessagingException e) { throw new RuntimeException("Erreur lors de l'envoi de l'e-mail", e); } diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java index 745b96b..ed35819 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java @@ -56,6 +56,9 @@ public class Player implements Serializable { @Column(name = "right_click_count") private int rightClickCount; + @Transient + private int partialRightClickCount; + @Column(name = "rapid_click_count") private int rapidClickCount; @@ -68,6 +71,7 @@ public class Player implements Serializable { @Transient private ClickChoice currentClick = null; + /** * Constructeur par défaut */ @@ -85,6 +89,7 @@ public class Player implements Serializable { this.rapidClickCount = 0; this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor()); this.deck.shuffle(); + this.partialRightClickCount = 0; } /** @@ -103,6 +108,7 @@ public class Player implements Serializable { this.rapidClickCount = 0; this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor()); this.deck.shuffle(); + this.partialRightClickCount = 0; } /** @@ -125,6 +131,7 @@ public class Player implements Serializable { this.clickCount = clickCount; this.rightClickCount = rightClickCount; this.rapidClickCount = rapidClickCount; + this.partialRightClickCount = 0; this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor()); this.deck.shuffle(); @@ -273,6 +280,20 @@ public class Player implements Serializable { return rapidClickCount; } + /** + * @return le nombre de clics partiels corrects + */ + public int getPartialRightClickCount() { + return partialRightClickCount; + } + + /** + * Incrémente de 1 le nombre de clics partielement corrects + */ + public void incrementPartialRightClickCount() { + partialRightClickCount++; + } + /** * Modifie le nombre de clics rapides * diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/User.java b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/User.java index dcee129..9da1ab8 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/User.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/User.java @@ -351,7 +351,6 @@ public class User implements Serializable { int maxScore = 0; int totalScore = 0; for (Player p : playedGames) { - System.out.println("Score max : " + p.getScoreMax() + " Score : " + p.getScore()); maxScore += p.getScoreMax(); totalScore += p.getScore(); } diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimplePlayer.java b/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimplePlayer.java index b49c04d..8de4683 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimplePlayer.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimplePlayer.java @@ -10,6 +10,7 @@ public class SimplePlayer { private final boolean winner; private final int clickCount; private final int rightClickCount; + private final int partialRightClickCount; private final int rapidClickCount; private final Card currentCard; @@ -20,8 +21,8 @@ public class SimplePlayer { this.clickCount = player.getClickCount(); this.rightClickCount = player.getRightClickCount(); this.rapidClickCount = player.getRapidClickCount(); - - this.currentCard = player.getDeck().getCards().get(currentRound % player.getDeck().getCards().size()); + this.partialRightClickCount = player.getPartialRightClickCount(); + this.currentCard = player.getDeck().getCards().get((partialRightClickCount+rightClickCount) % player.getDeck().getCards().size()); } public SimplePlayer(Player player) { @@ -30,6 +31,7 @@ public class SimplePlayer { this.winner = player.isWinner(); this.clickCount = player.getClickCount(); this.rightClickCount = player.getRightClickCount(); + this.partialRightClickCount = player.getPartialRightClickCount(); this.rapidClickCount = player.getRapidClickCount(); this.currentCard = null; } @@ -54,6 +56,10 @@ public class SimplePlayer { return clickCount; } + public int getPartialRightClickCount() { + return partialRightClickCount; + } + public int getRightClickCount() { return rightClickCount; } diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimpleUser.java b/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimpleUser.java index b10743f..8b72193 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimpleUser.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/json/websocket/SimpleUser.java @@ -10,6 +10,7 @@ public class SimpleUser { private final int nbWin; private final double winRate; private final double scoreRate; + private final double rigthClickPercentRate; private final double rapidClickPercentRate; diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java index f7aa607..aa4a1a1 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java @@ -44,7 +44,6 @@ public class ProfileServlet extends HttpServlet { userDAO = new Game_JPA_DAO_Factory().getDAOUser(); User user = userDAO.findById(usersession.getId().intValue()); for(Player p : user.getPlayedGames()){ - System.out.println("Partie jouée le " + p.getGame().getCreatedAt().toLocaleString()); } request.getSession().setAttribute("user", user); request.getRequestDispatcher("/WEB-INF/pages/profile.jsp").forward(request, response); diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java index e2b5c21..795a378 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java @@ -119,7 +119,7 @@ public class GameWS { player.incrementClickCount(); // Compteur de clics rapides - if (gameClickCount == 1) { + if (gameClickCount == 1 && choice != ClickChoice.TIMER_END) { player.incrementRapidClickCount(); player.addToScore(1); }; @@ -141,6 +141,7 @@ public class GameWS { player.incrementRightClickCount(); player.setScore(playerScore + 2); } else { + player.incrementPartialRightClickCount(); player.setScore(playerScore + 1); } } else { @@ -153,6 +154,7 @@ public class GameWS { player.incrementRightClickCount(); player.setScore(playerScore + 2); } else { + player.incrementPartialRightClickCount(); player.setScore(playerScore + 1); } } else { @@ -180,11 +182,13 @@ public class GameWS { player.incrementRightClickCount(); player.setScore(playerScore + 2); } else { + player.setScore(playerScore - 1); } } case COLOR -> { if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { + player.incrementPartialRightClickCount(); player.setScore(playerScore + 1); } else if ((nbSameColor > nbSameCard) && (nbSameColor >= nbSameValue) && (nbSameColor >= nbNone)) { player.incrementRightClickCount(); @@ -195,6 +199,7 @@ public class GameWS { } case VALUE -> { if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { + player.incrementPartialRightClickCount(); player.setScore(playerScore + 1); } else if ((nbSameValue > nbSameCard) && (nbSameValue > nbSameColor) && (nbSameValue >= nbNone)) { player.incrementRightClickCount(); @@ -218,9 +223,6 @@ public class GameWS { // Diffuser le score du joueur broadcast(new Message("updatePlayer", gson.toJson(new SimplePlayer(player))).toJson()); - System.out.println(gameClickCount + " / " + games.get(game).size()); - - // Si tous les joueurs ont cliqué if (gameClickCount >= games.get(game).size()) { // Stopper le timer @@ -261,7 +263,6 @@ public class GameWS { for (Player p : games.get(game)) { PlayerBean playerBean = new PlayerBean(p); if (playerBean.validate()) System.out.println("Player " + p.getUser().getUsername() + " sauvegardé en base de données"); - else System.out.println(); } em.getTransaction().commit(); diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp index 74f1a13..f31d921 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp @@ -53,7 +53,7 @@

- + diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/rules.jsp b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/rules.jsp index 63e2d4f..ca4d2ab 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/rules.jsp +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/rules.jsp @@ -13,24 +13,24 @@

- Card Rush est un jeu de rapidité multijoueur en ligne.
- Chaque joueur possède un jeu de carte identique mélangé aléatoirement. Au centre du plateau un jeu de carte + Card Rush est un jeu de rapidité multijoueurs en ligne.
+ Chaque joueur possède un jeu de cartes identique mais mélangé aléatoirement. Au centre du plateau un jeu de carte similaire.
Votre objectif si vous l'acceptez, identifier les similitudes entre votre main et celle du plateau le plus rapidement possible. - Plusieurs choix sont possibles: + Plusieurs choix sont possibles :

Modes de difficultés

Deux modes de difficultés s'offrent à vous :

Vainqueur de la partie

Le vainqueur d'une partie est celui qui aura accumulé le plus de points, en cas d'égalité, le joueur le plus rapide est déclaré vainqueur.
diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/login.tag b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/login.tag index 1012822..798b403 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/login.tag +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/login.tag @@ -48,8 +48,6 @@ }) .then(() => window.location.href = "${pageContext.request.contextPath}/lobby") .catch((error) => { - console.log(error) - // Animations des champs inputs.forEach(input => { input.classList.add("is-danger"); diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/new-game.tag b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/new-game.tag index 36eb0a2..cae44a5 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/new-game.tag +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/new-game.tag @@ -98,8 +98,6 @@ tooltip.innerHTML = input.value nbRound.max = nbValues.value * nbColors.value; nbRound.value = parseInt(nbRound.value) > parseInt(nbRound.max) ? nbRound.max : nbRoundtmp; - console.log("newValue : " + nbRound.value) - }); }); diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag index ca02f52..59f69b5 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag @@ -98,7 +98,6 @@ changePassword.addEventListener("click", (e) => { e.preventDefault(); passwordFields.forEach(field => { - console.log(field) field.style.display = "block"; }); }); @@ -122,7 +121,6 @@ const url = new URL(action); const formData = new FormData(profileForm); for (const [key, value] of formData.entries()) { - console.log(key, value); url.searchParams.append(key, value); } url.searchParams.append("id", ${user.id}) @@ -130,7 +128,6 @@ fetch(url, {headers: {"Content-Type": "application/json"}, method}) .then(res => res.json()) .then(data => { - console.log(data) if (data.code !== 200) throw new Error(data.message); onSuccess() }) @@ -163,7 +160,6 @@ notification.appendChild(notificationTitle); notification.appendChild(notificationMessage); document.body.appendChild(notification); - console.log("je suis bien dans la fonction mais la notification ne s'affiche pas") setTimeout(() => { notification.remove() inputs.forEach(input => input.classList.remove("is-danger")); @@ -193,7 +189,6 @@ notification.appendChild(notificationTitle); notification.appendChild(notificationMessage); document.body.appendChild(notification); - console.log("je suis bien dans la fonction mais la notification ne s'affiche pas") setTimeout(() => notification.remove(), 5010); } diff --git a/S2/DevWeb/Projet/src/main/webapp/static/js/Message.js b/S2/DevWeb/Projet/src/main/webapp/static/js/Message.js index 00ca07b..b79c2a0 100644 --- a/S2/DevWeb/Projet/src/main/webapp/static/js/Message.js +++ b/S2/DevWeb/Projet/src/main/webapp/static/js/Message.js @@ -3,7 +3,6 @@ export default class Message { const message = JSON.parse(rawMessage); message.data = JSON.parse(message.data); - console.log(message); this.type = message.type; this.data = message.data; diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/AbstractDAOFactoryTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/dao/AbstractDAOFactoryTest.java deleted file mode 100644 index 42eb4bc..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/AbstractDAOFactoryTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package uppa.project.dao; - -import java.util.HashMap; -import java.util.Objects; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import uppa.project.database.dao.AbstractDAOFactory; -import uppa.project.database.dao.PersistenceKind; -import uppa.project.database.dao.jpa.Game_JPA_DAO_Factory; -import uppa.project.database.dao.EntityManagerProvider; - -import static org.junit.jupiter.api.Assertions.*; -class AbstractDAOFactoryTest { - - @BeforeAll - static void setUp() { - EntityManagerProvider.setPersitenceUnitName("test"); - } - - @Test - void test_getDAOFactory() { - // If the method throws an exception, make sure the database is running - assertDoesNotThrow(() -> AbstractDAOFactory.getDAOFactory(PersistenceKind.JPA)); - - // Test if the method returns the correct DAO factory - HashMap factories = new HashMap<>() {{ - put(PersistenceKind.JPA, Game_JPA_DAO_Factory.class); - }}; - - for (PersistenceKind kind : PersistenceKind.values()) { - assertEquals( - factories.get(kind), - Objects.requireNonNull(AbstractDAOFactory.getDAOFactory(kind)).getClass() - ); - } - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_GameTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_GameTest.java deleted file mode 100644 index 70cb316..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_GameTest.java +++ /dev/null @@ -1,161 +0,0 @@ -package uppa.project.dao.jpa; - -import jakarta.persistence.EntityManager; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Calendar; -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.database.dao.DAO; -import uppa.project.database.dao.DAOException; -import uppa.project.database.dao.jpa.DAO_JPA_Game; -import uppa.project.database.dao.jpa.Game_JPA_DAO_Factory; -import uppa.project.database.pojo.Game; -import uppa.project.database.dao.EntityManagerProvider; - -import static org.junit.jupiter.api.Assertions.*; - -class DAO_JPA_GameTest { - - static EntityManager entityManager; - static DAO dao; - - static Game[] fixture; - - @BeforeAll - static void setUp() throws DAOException { - EntityManagerProvider.setPersitenceUnitName("test"); - entityManager = EntityManagerProvider.getInstance(); - dao = new Game_JPA_DAO_Factory().getDAOGame(); - - assertEquals(dao.getClass(), DAO_JPA_Game.class); - } - - @BeforeEach - void clean() { - fixture = new Game[] { - new Game( - new BigDecimal(1), - new Date(100, Calendar.NOVEMBER, 1), - Game.Difficulty.EASY, - 10, - 4, - 2, - 4, - new ArrayList<>() - ), - new Game( - new BigDecimal(2), - new Date(100, Calendar.DECEMBER, 1), - Game.Difficulty.HARD, - 10, - 3, - 2, - 4, - new ArrayList<>() - ), - }; - entityManager.getTransaction().begin(); - } - - @AfterEach - void rollback() { - entityManager.getTransaction().rollback(); - } - - @Test - void findById() throws DAOException { - // Create the game - dao.create(fixture[0]); - - // Retrieve the fixture - Game game = dao.findById(fixture[0].getId().intValue()); - - // Check if the retrieved fixture is the same as the created fixture - assertEquals(game.getCreatedAt(), fixture[0].getCreatedAt()); - assertEquals(game.getDifficulty(), fixture[0].getDifficulty()); - assertEquals(game.getNbColors(), fixture[0].getNbColors()); - assertEquals(game.getNbValuesPerColor(), fixture[0].getNbValuesPerColor()); - assertEquals(game.getNbRounds(), fixture[0].getNbRounds()); - } - - @Test - void findByField() throws DAOException { - // Create games - for (Game game : fixture) { - dao.create(game); - System.out.println(game.getNbRounds()); - } - - // Retrieve the fixture by nbRounds - Game[] games = dao.findByField("nbRounds", 4); - assertEquals(1, games.length); - - // Check if the retrieved fixture is the same as the created fixture - assertEquals(games[0].getCreatedAt(), fixture[0].getCreatedAt()); - assertEquals(games[0].getDifficulty(), fixture[0].getDifficulty()); - assertEquals(games[0].getNbColors(), fixture[0].getNbColors()); - assertEquals(games[0].getNbValuesPerColor(), fixture[0].getNbValuesPerColor()); - assertEquals(games[0].getNbRounds(), fixture[0].getNbRounds()); - } - - @Test - void findAll() throws DAOException { - // Create games - for (Game game : fixture) { - dao.create(game); - } - - // Retrieve all games - Game[] games = dao.findAll(); - assertEquals(fixture.length, games.length); - } - - @Test - void create() throws DAOException { - // Create the game - dao.create(fixture[0]); - - // Retrieve the fixture - Game game = dao.findById(fixture[0].getId().intValue()); - - // Check if the retrieved fixture is the same as the created fixture - assertEquals(game.getCreatedAt(), fixture[0].getCreatedAt()); - assertEquals(game.getDifficulty(), fixture[0].getDifficulty()); - assertEquals(game.getNbColors(), fixture[0].getNbColors()); - assertEquals(game.getNbValuesPerColor(), fixture[0].getNbValuesPerColor()); - assertEquals(game.getNbRounds(), fixture[0].getNbRounds()); - } - - @Test - void update() throws DAOException { - // Create the game - dao.create(fixture[0]); - - // Update the game - fixture[0].setNbColors(4); - dao.update(fixture[0]); - - // Retrieve the fixture - Game game = dao.findById(fixture[0].getId().intValue()); - - // Check if the retrieved fixture is the same as the updated fixture - assertEquals(4, game.getNbColors()); - } - - @Test - void delete() throws DAOException { - // Create the game - dao.create(fixture[0]); - Game game = dao.findAll()[0]; - - // Delete the game - dao.delete(game); - - // Check if the game has been deleted - assertNull(dao.findById(game.getId().intValue())); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_PlayerTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_PlayerTest.java deleted file mode 100644 index 1a1f9a1..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_PlayerTest.java +++ /dev/null @@ -1,151 +0,0 @@ -package uppa.project.dao.jpa; - -import jakarta.persistence.EntityManager; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Calendar; -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.database.dao.DAO; -import uppa.project.database.dao.DAOException; -import uppa.project.database.dao.jpa.DAO_JPA_Player; -import uppa.project.database.dao.jpa.Game_JPA_DAO_Factory; -import uppa.project.database.pojo.Game; -import uppa.project.database.pojo.Player; -import uppa.project.database.pojo.User; -import uppa.project.database.dao.EntityManagerProvider; - -import static org.junit.jupiter.api.Assertions.*; - -class DAO_JPA_PlayerTest { - - static EntityManager entityManager; - static DAO dao; - - static Player[] fixture; - - @BeforeAll - static void setUp() throws DAOException { - EntityManagerProvider.setPersitenceUnitName("test"); - entityManager = EntityManagerProvider.getInstance(); - dao = new Game_JPA_DAO_Factory().getDAOPlayer(); - - assertEquals(dao.getClass(), DAO_JPA_Player.class); - } - - @BeforeEach - void clean() { - fixture = new Player[] { - new Player( - new BigDecimal(1), - new Game(new BigDecimal(1), new Date(100, 10, 1), Game.Difficulty.EASY, 10, 4, 2, 4, new ArrayList<>()), - new User("user1", "email1", "password1", new Date(100, Calendar.JANUARY, 12), User.Gender.OTHER), - 2, false, 3, 1, 2 - ), - new Player( - new BigDecimal(2), - new Game(new BigDecimal(1), new Date(100, 10, 1), Game.Difficulty.EASY, 10, 4, 2, 4, new ArrayList<>()), - new User("user2", "email2", "password2", new Date(100, Calendar.MARCH, 15), User.Gender.MALE) - , 3, true, 4, 2, 3 - ), - }; - entityManager.getTransaction().begin(); - } - - @AfterEach - void rollback() { - entityManager.getTransaction().rollback(); - } - - @Test - void findById() throws DAOException { - // Create a new player - dao.create(fixture[0]); - Player player = dao.findAll()[0]; - - // Find the player by its id - Player foundPlayer = dao.findById(player.getId().intValue()); - assertEquals(player, foundPlayer); - } - - @Test - void findByField() throws DAOException { - // Create players - for (Player player : fixture) { - dao.create(player); - } - - // Find the players by their click count - Player[] foundPlayers = dao.findByField("clickCount", 3); - assertEquals(1, foundPlayers.length); - assertEquals(3, foundPlayers[0].getClickCount()); - } - - @Test - void findAll() throws DAOException { - // Create players - for (Player player : fixture) { - dao.create(player); - } - - // Find all the players - Player[] foundPlayers = dao.findAll(); - assertEquals(fixture.length, foundPlayers.length); - } - - @Test - void create() throws DAOException { - // Create players - dao.create(fixture[0]); - - - // Find all the players - Player[] foundPlayers = dao.findAll(); - assertEquals(1, foundPlayers.length); - - // Check if the players are the same - - assertEquals(fixture[0].getClickCount(), foundPlayers[0].getClickCount()); - assertEquals(fixture[0].getScore(), foundPlayers[0].getScore()); - assertEquals(fixture[0].isWinner(), foundPlayers[0].isWinner()); - assertEquals(fixture[0].getRapidClickCount(), foundPlayers[0].getRapidClickCount()); - assertEquals(fixture[0].getRightClickCount(), foundPlayers[0].getRightClickCount()); - } - - @Test - void update() throws DAOException { - // Create a new player - dao.create(fixture[0]); - Player player = dao.findAll()[0]; - - // Update the player - player.setClickCount(5); - player.setScore(4); - player.setWinner(); - player.setRapidClickCount(3); - - dao.update(player); - - // Find the player by its id - Player foundPlayer = dao.findById(player.getId().intValue()); - assertEquals(player, foundPlayer); - - } - - @Test - void delete() throws DAOException { - // Create a new player - dao.create(fixture[0]); - Player player = dao.findAll()[0]; - - // Delete the player - dao.delete(player); - - // Find all the players - Player[] foundPlayers = dao.findAll(); - assertEquals(0, foundPlayers.length); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_RecoveryPasswordTokenTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_RecoveryPasswordTokenTest.java deleted file mode 100644 index a9b535c..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_RecoveryPasswordTokenTest.java +++ /dev/null @@ -1,149 +0,0 @@ -package uppa.project.dao.jpa; - -import jakarta.persistence.EntityManager; -import java.math.BigDecimal; -import java.util.Calendar; -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.database.dao.DAO; -import uppa.project.database.dao.DAOException; -import uppa.project.database.dao.jpa.DAO_JPA_RecoveryPasswordToken; -import uppa.project.database.dao.jpa.Game_JPA_DAO_Factory; -import uppa.project.database.pojo.RecoveryPasswordToken; -import uppa.project.database.pojo.User; -import uppa.project.database.dao.EntityManagerProvider; - -import static org.junit.jupiter.api.Assertions.*; - -class DAO_JPA_RecoveryPasswordTokenTest { - - static EntityManager entityManager; - static DAO dao; - - static RecoveryPasswordToken[] fixture; - - @BeforeAll - static void setUp() throws DAOException { - EntityManagerProvider.setPersitenceUnitName("test"); - entityManager = EntityManagerProvider.getInstance(); - dao = new Game_JPA_DAO_Factory().getDAORecoveryPasswordToken(); - - assertEquals(dao.getClass(), DAO_JPA_RecoveryPasswordToken.class); - } - - @BeforeEach - void clean() { - fixture = new RecoveryPasswordToken[] { - new RecoveryPasswordToken( - new BigDecimal(1), "token1", - new User( - "user1", "email1", "password1", - new Date(100, Calendar.JANUARY, 1), - User.Gender.OTHER - ), - new Date(124, Calendar.FEBRUARY, 2) - ), - new RecoveryPasswordToken( - new BigDecimal(2), "token2", - new User( - "user2", "email2", "password2", - new Date(100, Calendar.MARCH, 3), - User.Gender.FEMALE - ), - new Date(124, Calendar.APRIL, 4) - ), - }; - entityManager.getTransaction().begin(); - } - - @AfterEach - void rollback() { - entityManager.getTransaction().rollback(); - } - - @Test - void findById() throws DAOException { - // Create a new recovery password token - dao.create(fixture[0]); - RecoveryPasswordToken recoveryPasswordToken = dao.findAll()[0]; - - // Find the recovery password token by id - RecoveryPasswordToken recoveryPasswordTokenInDb = dao.findById(recoveryPasswordToken.getId().intValue()); - assertEquals(recoveryPasswordToken.getId(), recoveryPasswordTokenInDb.getId()); - } - - @Test - void findByField() throws DAOException { - // Create recovery password tokens - for (RecoveryPasswordToken recoveryPasswordToken : fixture) - dao.create(recoveryPasswordToken); - - // Find the recovery password token by token - RecoveryPasswordToken recoveryPasswordToken = dao.findByField("token", "token1")[0]; - assertEquals(fixture[0].getToken(), recoveryPasswordToken.getToken()); - - // Check if the recovery password token is the same - assertEquals(fixture[0].getToken(), recoveryPasswordToken.getToken()); - } - - @Test - void findAll() throws DAOException { - // Find all recovery password tokens - RecoveryPasswordToken[] recoveryPasswordTokens = dao.findAll(); - assertEquals(0, recoveryPasswordTokens.length); - - // Create recovery password tokens - for (RecoveryPasswordToken recoveryPasswordToken : fixture) - dao.create(recoveryPasswordToken); - - // Find all recovery password tokens - recoveryPasswordTokens = dao.findAll(); - assertEquals(fixture.length, recoveryPasswordTokens.length); - } - - @Test - void create() throws DAOException { - // Create a new recovery password token - dao.create(fixture[0]); - RecoveryPasswordToken recoveryPasswordToken = dao.findAll()[0]; - - // Check if the recovery password token is the same - assertEquals(fixture[0].getToken(), recoveryPasswordToken.getToken()); - assertEquals(fixture[0].getExpirationDate(), recoveryPasswordToken.getExpirationDate()); - } - - @Test - void update() throws DAOException { - // Create a new recovery password token - dao.create(fixture[0]); - RecoveryPasswordToken recoveryPasswordToken = dao.findAll()[0]; - - // Update the recovery password token - recoveryPasswordToken.setToken("newToken"); - dao.update(recoveryPasswordToken); - - // Check if the recovery password token is in the database - RecoveryPasswordToken recoveryPasswordTokenInDb = dao.findAll()[0]; - assertEquals(recoveryPasswordToken.getToken(), recoveryPasswordTokenInDb.getToken()); - - // Check if the recovery password token is the same - recoveryPasswordTokenInDb = dao.findAll()[0]; - assertEquals(recoveryPasswordToken.getToken(), recoveryPasswordTokenInDb.getToken()); - } - - @Test - void delete() throws DAOException { - // Create a new recovery password token - dao.create(fixture[0]); - RecoveryPasswordToken recoveryPasswordToken = dao.findAll()[0]; - - // Delete the recovery password token - dao.delete(recoveryPasswordToken); - - // Check if the recovery password token is not in the database - assertNull(dao.findById(recoveryPasswordToken.getId().intValue())); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_UserTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_UserTest.java deleted file mode 100644 index 84637ca..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/DAO_JPA_UserTest.java +++ /dev/null @@ -1,135 +0,0 @@ -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.database.dao.DAO; -import uppa.project.database.dao.DAOException; -import uppa.project.database.dao.jpa.DAO_JPA_User; -import uppa.project.database.dao.jpa.Game_JPA_DAO_Factory; -import uppa.project.database.pojo.User; -import uppa.project.database.dao.EntityManagerProvider; - -import static org.junit.jupiter.api.Assertions.*; - -class DAO_JPA_UserTest { - - static EntityManager entityManager; - static DAO dao; - static User[] fixture; - - @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() { - fixture = 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 findById() throws DAOException { - // Create a new user - dao.create(fixture[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 findByField() throws DAOException { - // Create users - for (User user : fixture) dao.create(user); - - // Find the user by username - User[] usersInDb = dao.findByField("username", fixture[0].getUsername()); - assertEquals(1, usersInDb.length); - - // Check if the user is the same - assertEquals(fixture[0].getUsername(), usersInDb[0].getUsername()); - assertEquals(fixture[0].getEmail(), usersInDb[0].getEmail()); - assertEquals(fixture[0].getPassword(), usersInDb[0].getPassword()); - } - - @Test - void findAll() throws DAOException { - // Find all users - User[] fixture = dao.findAll(); - assertEquals(0, fixture.length); - - // Create a new user - for (User user : fixture) dao.create(user); - - // Find all users - User[] usersInDb = dao.findAll(); - assertEquals(fixture.length, usersInDb.length); - } - - @Test - void create() throws DAOException { - // Create users - dao.create(fixture[0]); - - // Check if the user is in the database - User[] usersInDb = dao.findAll(); - assertEquals(1, usersInDb.length); - - // Check if the user is the same - assertEquals(fixture[0].getUsername(), usersInDb[0].getUsername()); - assertEquals(fixture[0].getEmail(), usersInDb[0].getEmail()); - assertEquals(fixture[0].getPassword(), usersInDb[0].getPassword()); - assertEquals(fixture[0].getBirth(), usersInDb[0].getBirth()); - assertEquals(fixture[0].getGender(), usersInDb[0].getGender()); - - } - - @Test - void update() throws DAOException { - // Create a new user - dao.create(fixture[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(fixture[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())); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/Game_JPA_DAO_FactoryTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/Game_JPA_DAO_FactoryTest.java deleted file mode 100644 index 83cb73f..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/dao/jpa/Game_JPA_DAO_FactoryTest.java +++ /dev/null @@ -1,78 +0,0 @@ -package uppa.project.dao.jpa; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import uppa.project.database.dao.DAO; -import uppa.project.database.dao.DAOException; -import uppa.project.database.dao.jpa.Game_JPA_DAO_Factory; -import uppa.project.database.pojo.User; -import uppa.project.database.dao.EntityManagerProvider; - -import static org.junit.jupiter.api.Assertions.*; - -class Game_JPA_DAO_FactoryTest { - - static Game_JPA_DAO_Factory factory; - - @BeforeAll - static void setUp() { - EntityManagerProvider.setPersitenceUnitName("test"); - factory = new Game_JPA_DAO_Factory(); - } - - @Test - void test_getDAOUser() throws DAOException { - // If the method throws an exception, make sure the database is running - assertDoesNotThrow(() -> factory.getDAOUser()); - - // Check if the returned object is a DAO - assertNotNull(factory.getDAOUser()); - - // Check if the object is unique - DAO dao1 = factory.getDAOUser(); - DAO dao2 = factory.getDAOUser(); - assertEquals(dao1, dao2); - } - - @Test - void test_getDAOGame() throws DAOException { - // If the method throws an exception, make sure the database is running - assertDoesNotThrow(() -> factory.getDAOGame()); - - // Check if the returned object is a DAO - assertNotNull(factory.getDAOGame()); - - // Check if the object is unique - DAO dao1 = factory.getDAOUser(); - DAO dao2 = factory.getDAOUser(); - assertEquals(dao1, dao2); - } - - @Test - void test_getDAOPlayer() throws DAOException { - // If the method throws an exception, make sure the database is running - assertDoesNotThrow(() -> factory.getDAOPlayer()); - - // Check if the returned object is a DAO - assertNotNull(factory.getDAOPlayer()); - - // Check if the object is unique - DAO dao1 = factory.getDAOUser(); - DAO dao2 = factory.getDAOUser(); - assertEquals(dao1, dao2); - } - - @Test - void test_getDAORecoveryPasswordToken() throws DAOException { - // If the method throws an exception, make sure the database is running - assertDoesNotThrow(() -> factory.getDAORecoveryPasswordToken()); - - // Check if the returned object is a DAO - assertNotNull(factory.getDAORecoveryPasswordToken()); - - // Check if the object is unique - DAO dao1 = factory.getDAOUser(); - DAO dao2 = factory.getDAOUser(); - assertEquals(dao1, dao2); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/CardTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/CardTest.java deleted file mode 100644 index 8ded9bc..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/CardTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package uppa.project.pojo; - -import java.util.HashMap; -import org.junit.jupiter.api.Test; -import uppa.project.database.pojo.Card; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class CardTest { - - @Test - void test_constructor() { - - // Card with correct Value - new Card(Card.Color.HEART, Card.Value.ACE); - new Card(Card.Color.HEART, Card.Value.TWO); - new Card(Card.Color.HEART, Card.Value.THREE); - new Card(Card.Color.CLUBS, Card.Value.FOUR); - new Card(Card.Color.CLUBS, Card.Value.FIVE); - new Card(Card.Color.CLUBS, Card.Value.SIX); - new Card(Card.Color.DIAMONDS, Card.Value.SEVEN); - new Card(Card.Color.DIAMONDS, Card.Value.EIGHT); - new Card(Card.Color.DIAMONDS, Card.Value.NINE); - new Card(Card.Color.SPADES, Card.Value.TEN); - new Card(Card.Color.SPADES, Card.Value.JACK); - new Card(Card.Color.SPADES, Card.Value.QUEEN); - new Card(Card.Color.HEART, Card.Value.KING); - - // Card with correct Color - new Card(Card.Color.HEART, Card.Value.ACE); - new Card(Card.Color.CLUBS, Card.Value.ACE); - new Card(Card.Color.DIAMONDS, Card.Value.ACE); - new Card(Card.Color.SPADES, Card.Value.ACE); - - // Invalid Card Color - assertThrows(IllegalArgumentException.class, () -> new Card(null, Card.Value.ACE)); - - // Invalid Card Value - assertThrows(IllegalArgumentException.class, () -> new Card(Card.Color.HEART, null)); - } - - @Test - void test_getColor() { - final HashMap TESTS = new HashMap<>() {{ - put(new Card(Card.Color.HEART, Card.Value.ACE), Card.Color.HEART); - put(new Card(Card.Color.CLUBS, Card.Value.TWO), Card.Color.CLUBS); - put(new Card(Card.Color.DIAMONDS, Card.Value.THREE), Card.Color.DIAMONDS); - put(new Card(Card.Color.SPADES, Card.Value.FOUR), Card.Color.SPADES); - }}; - - for (Card card : TESTS.keySet()) { - assertEquals(TESTS.get(card), card.getColor()); - } - } - - @Test - void test_getValue() { - final HashMap TESTS = new HashMap<>() {{ - put(new Card(Card.Color.DIAMONDS, Card.Value.ACE), Card.Value.ACE); - put(new Card(Card.Color.DIAMONDS, Card.Value.TWO), Card.Value.TWO); - put(new Card(Card.Color.DIAMONDS, Card.Value.THREE), Card.Value.THREE); - put(new Card(Card.Color.DIAMONDS, Card.Value.FOUR), Card.Value.FOUR); - put(new Card(Card.Color.DIAMONDS, Card.Value.FIVE), Card.Value.FIVE); - put(new Card(Card.Color.DIAMONDS, Card.Value.SIX), Card.Value.SIX); - put(new Card(Card.Color.DIAMONDS, Card.Value.SEVEN), Card.Value.SEVEN); - put(new Card(Card.Color.DIAMONDS, Card.Value.EIGHT), Card.Value.EIGHT); - put(new Card(Card.Color.DIAMONDS, Card.Value.NINE), Card.Value.NINE); - put(new Card(Card.Color.DIAMONDS, Card.Value.TEN), Card.Value.TEN); - put(new Card(Card.Color.DIAMONDS, Card.Value.JACK), Card.Value.JACK); - put(new Card(Card.Color.DIAMONDS, Card.Value.QUEEN), Card.Value.QUEEN); - put(new Card(Card.Color.DIAMONDS, Card.Value.KING), Card.Value.KING); - }}; - - for (Card card : TESTS.keySet()) { - assertEquals(TESTS.get(card), card.getValue()); - } - } - - @Test - void test_toString() { - - final HashMap TESTS = new HashMap<>() {{ - put(new Card(Card.Color.HEART, Card.Value.ACE), "Card{color=HEART, value=ACE}"); - put(new Card(Card.Color.CLUBS, Card.Value.TWO), "Card{color=CLUBS, value=TWO}"); - put(new Card(Card.Color.DIAMONDS, Card.Value.THREE), "Card{color=DIAMONDS, value=THREE}"); - put(new Card(Card.Color.SPADES, Card.Value.FOUR), "Card{color=SPADES, value=FOUR}"); - }}; - - for (Card card : TESTS.keySet()) { - assertEquals(TESTS.get(card), card.toString()); - } - } - - @Test - void test_CardValue() { - - // Expect the right number of values - assertEquals(13, Card.Value.values().length); - - // Expect the right name and ordinal for each value - final String[] EXPECTED_NAMES = new String[]{ - "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING" - }; - - for (int index = 0; index < EXPECTED_NAMES.length; index++) { - String expectedName = EXPECTED_NAMES[index]; - Card.Value currentValue = Card.Value.values()[index]; - - assertEquals(index, currentValue.ordinal()); // Ordinal - assertEquals(expectedName, currentValue.name()); // Name - } - } - - @Test - void test_CardColor() { - // Expect the right number of colors - assertEquals(4, Card.Color.values().length); - - // Expect the right name and ordinal for each color - final String[] EXPECTED_NAMES = new String[]{"HEART", "CLUBS", "SPADES", "DIAMONDS"}; - - for (int index = 0; index < Card.Color.values().length; index++) { - String expectedName = EXPECTED_NAMES[index]; - Card.Color currentColor = Card.Color.values()[index]; - - assertEquals(index, currentColor.ordinal()); // Ordinal - assertEquals(expectedName, currentColor.name()); // Name - } - } - -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/DeckTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/DeckTest.java deleted file mode 100644 index 7c1779e..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/DeckTest.java +++ /dev/null @@ -1,138 +0,0 @@ -package uppa.project.pojo; - -import java.util.HashMap; -import org.junit.jupiter.api.Test; -import uppa.project.database.pojo.Card; -import uppa.project.database.pojo.Deck; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DeckTest { - - @Test - void test_constructor() { - new Deck(4, 13); - new Deck(3, 10); - new Deck(2, 7); - new Deck(2, 2); - } - - @Test - void test_constructor_deckWithSameParametersHaveSameCards() { - Deck mockDeck = new Deck(4, 13); - Deck mockDeck2 = new Deck(4, 13); - assertEquals(mockDeck, mockDeck2); - } - - @Test - void test_isValidDeck() { - // Nombre de couleurs et de valeurs valides - final int[][] TRUE_EXPECTED = {{4, 13}, {3, 10}, {2, 7},}; - - for (int[] args : TRUE_EXPECTED) { - assertTrue(Deck.isDeckValid(args[0], args[1])); - } - - // Nombre de couleurs et de valeurs invalides - final int[][] FALSE_EXPECTED = {{0, 13}, {5, 10}, {8, 7}, {-1, 11}, {-6, 12},}; - - for (int[] args : FALSE_EXPECTED) { - assertFalse(Deck.isDeckValid(args[0], args[1])); - } - } - - @Test - void test_constructor_throwIllegalArgumentExceptionOnInvalidColor() { - int[] INCORRECT_COLORS = {Integer.MIN_VALUE, 0, 5, 8, -1, -6, Integer.MAX_VALUE}; - for (int incorrect_color : INCORRECT_COLORS) { - assertThrows(IllegalArgumentException.class, () -> new Deck(incorrect_color, 13)); - } - } - - @Test - void test_constructor_throwIllegalArgumentExceptionOnInvalidValues() { - int[] INCORRECT_VALUES = {Integer.MIN_VALUE, -2, 0, 14, Integer.MAX_VALUE}; - for (int incorrect_value : INCORRECT_VALUES) { - assertThrows(IllegalArgumentException.class, () -> new Deck(4, incorrect_value)); - } - } - - @Test - void test_getCards_size() { - final HashMap TESTS = new HashMap<>() {{ - put(new Deck(4, 13), 52); - put(new Deck(3, 11), 33); - put(new Deck(2, 7), 14); - put(new Deck(2, 5), 10); - }}; - for (Deck deck : TESTS.keySet()) { - assertEquals(TESTS.get(deck), deck.getCards().size()); - } - } - - @Test - void test_getCards_returnTheRightNumberOfColors() { - HashMap TESTS = new HashMap<>() {{ - put(new Deck(4, 13), 4); - put(new Deck(3, 10), 3); - put(new Deck(2, 7), 2); - put(new Deck(2, 2), 2); - }}; - - for (Deck deck : TESTS.keySet()) { - int expected = TESTS.get(deck); - int actual = (int)deck.getCards().stream().map(Card::getColor).distinct().count(); - assertEquals(expected, actual); - } - } - - @Test - void test_getCards_returnTheRightNumberOfValues() { - HashMap TESTS = new HashMap<>() {{ - put(new Deck(4, 13), 13); - put(new Deck(3, 10), 10); - put(new Deck(2, 7), 7); - put(new Deck(2, 2), 2); - }}; - - for (Deck deck : TESTS.keySet()) { - int expected = TESTS.get(deck); - int actual = (int)deck.getCards().stream().map(Card::getValue).distinct().count(); - assertEquals(expected, actual); - } - } - - @Test - void test_shuffle() { - Deck mockDeck1 = new Deck(4,6); - Deck mockDeck2 = new Deck(4,6); - Deck mockDeck3 = new Deck(4,6); - - mockDeck1.shuffle(); - mockDeck2.shuffle(); - mockDeck3.shuffle(); - assertNotEquals(mockDeck1.getCards(), mockDeck2.getCards()); - assertNotEquals(mockDeck1.getCards(), mockDeck3.getCards()); - assertNotEquals(mockDeck2.getCards(), mockDeck3.getCards()); - } - - @Test - void test_shuffle_deckSizeHasNotChange() { - Deck mockDeck = new Deck(4, 13); - int initialSize = mockDeck.getCards().size(); - mockDeck.shuffle(); - assertEquals(initialSize, mockDeck.getCards().size()); - } - - @Test - void test_shuffle_deckHaveSameCards() { - Deck mockDeck = new Deck(4, 13); - Deck mockDeck2 = new Deck(4,13); - mockDeck2.shuffle(); - assertEquals(mockDeck, mockDeck2); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/GameTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/GameTest.java deleted file mode 100644 index 48e04df..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/GameTest.java +++ /dev/null @@ -1,333 +0,0 @@ -package uppa.project.pojo; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import uppa.project.database.pojo.Deck; -import uppa.project.database.pojo.Game; -import uppa.project.database.pojo.Player; -import uppa.project.database.pojo.User; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class GameTest { - - Game[] fixture; - static Player[][] playersFixture; - - @BeforeAll - static void beforeAll(){ - playersFixture = new Player[][]{ - {new Player(new BigDecimal(1), new Game(Game.Difficulty.EASY, 10, 17, 3, 6), new User(), 10, true, 5, 5, 5)}, - {new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 10, 17, 3, 6), new User(), 15, true, 5, 5, 5), - new Player(new BigDecimal(3), new Game(Game.Difficulty.EASY, 10, 17, 3, 6), new User(), 20, true, 5, 5, 5)}, - {new Player(new BigDecimal(4), new Game(Game.Difficulty.EASY, 15, 17, 3, 6), new User(), 10, true, 5, 5, 5), - new Player(new BigDecimal(5), new Game(Game.Difficulty.EASY, 15, 17, 3, 6), new User(), 30, true, 5, 5, 5), - new Player(new BigDecimal(6), new Game(Game.Difficulty.EASY, 15, 17, 3, 6), new User(), 20, true, 5, 5, 5)} - }; - } - - @BeforeEach - void beforeEach() { - fixture = new Game[]{ - new Game(new BigDecimal(1), new Date(2024 - 1900, 5, 6), Game.Difficulty.EASY, 50, 17, 3, 6, new ArrayList<>(Arrays.asList(playersFixture[0]))), - new Game(new BigDecimal(2), new Date(2023 - 1900, 7, 9), Game.Difficulty.HARD, 10, 28, 4, 13, new ArrayList<>(Arrays.asList(playersFixture[1]))), - new Game(new BigDecimal(3), new Date(2022 - 1900, 11, 12), Game.Difficulty.EASY, 15, 16, 2, 9, new ArrayList<>(Arrays.asList(playersFixture[2]))) - }; - } - - @Test - void test_constructor() { - new Game(); - new Game(Game.Difficulty.EASY, 10, 17, 3, 6); - new Game(new BigDecimal(1), new Date(2023, 12, 25), Game.Difficulty.EASY,15, 17, 3, 6, new ArrayList()); - new Game(new BigDecimal(2), new Date(2024, 3, 26), Game.Difficulty.HARD, 20, 52, 4, 13, new ArrayList()); - } - - @Test - void test_constructor_throwIllegalArgumentExceptionOnInvalidValues() { - int[] INCORRECT_VALUES = {Integer.MIN_VALUE, -2, 0, 14, Integer.MAX_VALUE}; - for (int incorrect_value : INCORRECT_VALUES) { - assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY,10, 4 * incorrect_value, 4, incorrect_value)); - } - } - - @Test - void test_constructor_throwIllegalArgumentExceptionOnInvalidColors() { - int[] INCORRECT_VALUES = {Integer.MIN_VALUE, -9, -2, 0, 5, 8, Integer.MAX_VALUE}; - for (int incorrect_value : INCORRECT_VALUES) { - assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 4 * incorrect_value, incorrect_value, 13)); - } - } - - @Test - void test_constructor_throwIllegalArgumentExceptionOnInvalidNbRounds() { - assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 0, 4, 8)); - assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 33, 4, 8)); - assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, -5, 4, 8)); - assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 13, 2, 6)); - } - - - @Test - void test_constructor_throwIllegalArgumentExceptionOnInvalidTimers() { - int[] INCORRECT_VALUES = {Integer.MIN_VALUE, -9, -2, 0, 5, 61, Integer.MAX_VALUE}; - for (int incorrect_value : INCORRECT_VALUES) { - assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, incorrect_value, 13, 2, 6)); - } - } - - @Test - void test_getId() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new BigDecimal(1)); - put(fixture[1], new BigDecimal(2)); - put(fixture[2], new BigDecimal(3)); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getId()); - } - } - - @Test - void test_getCreatedAt() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new Date(2024 - 1900, 5, 6)); - put(fixture[1], new Date(2023 - 1900, 7, 9)); - put(fixture[2], new Date(2022 - 1900, 11, 12)); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getCreatedAt()); - } - } - - @Test - void test_getDifficulty() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], Game.Difficulty.EASY); - put(fixture[1], Game.Difficulty.HARD); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getDifficulty()); - } - } - - @Test - void test_setDifficulty() { - Game game = fixture[0]; - assertEquals(Game.Difficulty.EASY, game.getDifficulty()); - - // Change the difficulty - game.setDifficulty(Game.Difficulty.HARD); - assertEquals(Game.Difficulty.HARD, game.getDifficulty()); - } - - @Test - void test_getTimer() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 50); - put(fixture[1], 10); - put(fixture[2], 15); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getTimer()); - } - } - - @Test - void test_setTimer() { - Game game = fixture[0]; - assertEquals(50, game.getTimer()); - - // Change the timer - game.setTimer(25); - assertEquals(25, game.getTimer()); - } - - @Test - void test_getNbRounds() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 17); - put(fixture[1], 28); - put(fixture[2], 16); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getNbRounds()); - } - } - - @Test - void test_setNbRounds() { - Game game = fixture[0]; - assertEquals(17, game.getNbRounds()); - - // Change the number of rounds - game.setNbRounds(5); - assertEquals(5, game.getNbRounds()); - } - - @Test - void test_getNbColors() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 3); - put(fixture[1], 4); - put(fixture[2], 2); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getNbColors()); - } - } - - @Test - void test_setNbColors() { - Game game = fixture[0]; - assertEquals(3, game.getNbColors()); - - // Change the number of colors - game.setNbColors(2); - assertEquals(2, game.getNbColors()); - } - - @Test - void test_getNbValuesPerColor() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 6); - put(fixture[1], 13); - put(fixture[2], 9); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getNbValuesPerColor()); - } - } - - @Test - void test_setNbValuesPerColor() { - Game game = fixture[0]; - assertEquals(6, game.getNbValuesPerColor()); - - // Change the number of values per color - game.setNbValuesPerColor(8); - assertEquals(8, game.getNbValuesPerColor()); - } - - @Test - void test_getPlayers() { - final HashMap> TESTS = new HashMap<>() {{ - put(fixture[0], new ArrayList<>(Arrays.asList(playersFixture[0]))); - put(fixture[1], new ArrayList<>(Arrays.asList(playersFixture[1]))); - put(fixture[2], new ArrayList<>(Arrays.asList(playersFixture[2]))); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getPlayers()); - } - } - - @Test - void test_setPlayers() { - Game game = fixture[0]; - assertEquals(new ArrayList<>(Arrays.asList(playersFixture[0])), game.getPlayers()); - - // Add players - game.setPlayers(new ArrayList<>(Arrays.asList(playersFixture[1]))); - assertEquals(new ArrayList<>(Arrays.asList(playersFixture[1])), game.getPlayers()); - } - - @Test - void test_getNbPlayers() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 1); - put(fixture[1], 2); - put(fixture[2], 3); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getPlayers().size()); - } - } - - @Test - void test_addPlayer() { - Game game = fixture[0]; - ArrayList players = new ArrayList<>(Arrays.asList(playersFixture[0])); - assertEquals(players.size(), game.getPlayers().size()); - - Player player = new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 20, 17, 3, 6), new User(), 10, true, 5, 5, 5); - - // Add a player - game.addPlayer(player); - players.add(player); - assertEquals(players.size(), game.getPlayers().size()); - - // Check if the player is the same - assertEquals(players, game.getPlayers()); - } - - @Test - void test_getDeck() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new Deck(3, 6)); - put(fixture[1], new Deck(4, 13)); - put(fixture[2], new Deck(2, 9)); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.getDeck()); - } - } - - @Test - void test_sortPlayersByScore() { - final HashMap> TESTS = new HashMap<>() {{ - put(fixture[0], new ArrayList<>() {{ - add(playersFixture[0][0]); - }}); - put(fixture[1], new ArrayList<>() {{ - add(playersFixture[1][1]); - add(playersFixture[1][0]); - }}); - put(fixture[2], new ArrayList<>() {{ - add(playersFixture[2][1]); - add(playersFixture[2][2]); - add(playersFixture[2][0]); - }}); - }}; - - for (Game game : TESTS.keySet()) { - ArrayList expected = TESTS.get(game); - game.sortPlayersByScoreAndRapidity(); - assertEquals(expected, game.getPlayers()); - } - } - - @Test - void test_toString() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], - "Game{id=1, createdAt=Thu Jun " + "06 00:00:00 CEST 2024, difficulty=EASY, nbRounds=17, nbColors=3, nbValuesPerColor=6}"); - put(fixture[1], - "Game{id=2, createdAt=Wed Aug 09 00:00:00 CEST 2023, difficulty=HARD, nbRounds=28, nbColors=4, nbValuesPerColor=13}"); - put(fixture[2], - "Game{id=3, createdAt=Mon Dec 12 00:00:00 CET 2022, difficulty=EASY, nbRounds=16, nbColors=2, nbValuesPerColor=9}"); - }}; - - for (Game game : TESTS.keySet()) { - assertEquals(TESTS.get(game), game.toString()); - } - } -} - diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/PlayerTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/PlayerTest.java deleted file mode 100644 index 5591a02..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/PlayerTest.java +++ /dev/null @@ -1,337 +0,0 @@ -package uppa.project.pojo; - -import java.math.BigDecimal; -import java.util.Date; -import java.util.HashMap; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import uppa.project.database.pojo.Deck; -import uppa.project.database.pojo.Game; -import uppa.project.database.pojo.Player; -import uppa.project.database.pojo.User; - -import static org.junit.jupiter.api.Assertions.*; - -class PlayerTest { - - static Player[] fixture; - static Game[] games; - static User[] users; - - @BeforeAll - static void beforeAll() { - games = new Game[]{ - new Game(new BigDecimal(1), new Date(101, 2, 4), Game.Difficulty.EASY, 50,18,2,10,null), - new Game(new BigDecimal(2), new Date(101, 2, 4), Game.Difficulty.EASY, 10,18,4,13,null), - new Game(new BigDecimal(3), new Date(101, 2, 4), Game.Difficulty.EASY, 20,18,3,6,null), - new Game(new BigDecimal(4), new Date(101, 2, 4), Game.Difficulty.HARD,25,18,3,6,null), - }; - - users = new User[]{ - new User("username1", "email1", "password1", new Date(), User.Gender.MALE), - new User("username2", "email2", "password2", new Date(), User.Gender.FEMALE), - new User("username3", "email3", "password3", new Date(), User.Gender.OTHER), - new User("username4", "email4", "password4", new Date(100, 1, 1), User.Gender.OTHER) - - }; - } - @BeforeEach - void beforeEach() { - fixture = new Player[]{ - new Player( - new BigDecimal(1), - games[0], - users[0], - 10, true, 6,5,5 - ), - new Player( - new BigDecimal(2), - games[1], - users[1], - 20, false, 4,2,1 - ), - new Player( - new BigDecimal(3), - games[2], - users[2], - 15, true, 10,5,5 - ), - new Player( - new BigDecimal(4), - games[3], - users[3], - 15, true, 10,5,5 - ) - }; - - } - - @Test - void test_constructor() { - new Player(); - new Player(new Game(Game.Difficulty.EASY,25,4,2,3), new User()); - new Player(new Game(Game.Difficulty.EASY,10,4,2,3), new User()); - new Player(new Game(Game.Difficulty.EASY,20,4,2,3), new User()); - } - - @Test - void test_getGame() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], games[0]); - put(fixture[1], games[1]); - put(fixture[2], games[2]); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getGame()); - } - } - - @Test - void test_setGame() { - Player player = fixture[0]; - Game game = player.getGame(); - - // Set a new game - Game newGame = new Game(new BigDecimal(4), new Date(), Game.Difficulty.HARD,10,20,4,5,null); - player.setGame(newGame); - - assertNotEquals(game, player.getGame()); - assertEquals(newGame, player.getGame()); - } - - @Test - void test_getUser() { - Player player = fixture[0]; - - User expected = new User("username1", "email1", "password1", new Date(), User.Gender.MALE); - assertEquals(expected.getUsername(), player.getUser().getUsername()); - assertEquals(expected.getEmail(), player.getUser().getEmail()); - assertEquals(expected.getPassword(), player.getUser().getPassword()); - assertEquals(expected.getGender(), player.getUser().getGender()); - } - - - @Test - void test_setUser() { - Player player = fixture[0]; - User user = player.getUser(); - - // Set a new user - User newUser = new User("username2", "email2", "password2", new Date(), User.Gender.FEMALE); - player.setUser(newUser); - - assertNotEquals(user, player.getUser()); - assertEquals(newUser, player.getUser()); - } - - @Test - void test_getScore() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0],10); - put(fixture[1],20); - put(fixture[2],15);; - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getScore()); - } - } - - @Test - void test_setScore() { - Player player = fixture[0]; - int score = player.getScore(); - - // Set a new score - player.setScore(15); - assertEquals(15, player.getScore()); - assertNotEquals(score, player.getScore()); - } - - @Test - void test_addToScore() { - Player player = fixture[0]; - int score = player.getScore(); - - // Update the score - player.addToScore(5); - assertEquals(score + 5, player.getScore()); - assertNotEquals(score, player.getScore()); - } - - @Test - void test_isWinner() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], true); - put(fixture[1], false); - put(fixture[2], true); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.isWinner()); - } - } - - @Test - void test_setWinner() { - Player player = fixture[1]; - assertFalse(player.isWinner()); - - player.setWinner(); - assertTrue(player.isWinner()); - } - - @Test - void test_getClickCount() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 6); - put(fixture[1], 4); - put(fixture[2], 10); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getClickCount()); - } - } - - @Test - void test_setClickCount() { - Player player = fixture[0]; - int clickCount = player.getClickCount(); - - // Set a new click count - player.setClickCount(3); - assertEquals(3, player.getClickCount()); - assertNotEquals(clickCount, player.getClickCount()); - } - - @Test - void test_incrementClickCount() { - Player player = fixture[0]; - int clickCount = player.getClickCount(); - - // Increment the click count - player.incrementClickCount(); - assertEquals(clickCount + 1, player.getClickCount()); - assertNotEquals(clickCount, player.getClickCount()); - } - - @Test - void test_getRightClickCount() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 5); - put(fixture[1], 2); - put(fixture[2], 5); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getRightClickCount()); - } - } - - @Test - void test_setRightClickCount() { - Player player = fixture[0]; - int rightClickCount = player.getRightClickCount(); - - // Set a new right click count - player.setRightClickCount(3); - assertEquals(3, player.getRightClickCount()); - assertNotEquals(rightClickCount, player.getRightClickCount()); - } - - @Test - void test_incrementRightClickCount() { - Player player = fixture[0]; - int rightClickCount = player.getRightClickCount(); - - // Increment the right click count - player.incrementRightClickCount(); - assertEquals(rightClickCount + 1, player.getRightClickCount()); - assertNotEquals(rightClickCount, player.getRightClickCount()); - } - - @Test - void test_getRatioRightClick() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 83.33); - put(fixture[1], 50.); - put(fixture[2], 50.); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getRatioRightClick()); - } - - } - - @Test - void test_getRapidClickCount() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 5); - put(fixture[1], 1); - put(fixture[2], 5); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getRapidClickCount()); - } - } - - @Test - void test_setRapidClickCount() { - Player player = fixture[0]; - int rapidClickCount = player.getRapidClickCount(); - - // Set a new rapid click count - player.setRapidClickCount(3); - assertEquals(3, player.getRapidClickCount()); - assertNotEquals(rapidClickCount, player.getRapidClickCount()); - } - - @Test - void test_incrementRapidClickCount() { - Player player = fixture[0]; - int rapidClickCount = player.getRapidClickCount(); - - // Increment the rapid click count - player.incrementRapidClickCount(); - assertEquals(rapidClickCount + 1, player.getRapidClickCount()); - assertNotEquals(rapidClickCount, player.getRapidClickCount()); - } - - @Test - void test_getRatioRapidClick() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 83.33); - put(fixture[1], 25.); - put(fixture[2], 50.); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getRatioRapidClick()); - } - } - - @Test - void test_getDeck() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new Deck(2,10)); - put(fixture[1], new Deck(4,13)); - put(fixture[2], new Deck(3,6)); - }}; - - for (Player player : TESTS.keySet()) { - assertEquals(TESTS.get(player), player.getDeck()); - } - } - - @Test - void test_toString() { - String expected = "Player{id=4, game=Game{id=4, createdAt=Sun Mar 04 00:00:00 CET 2001, difficulty=HARD, nbRounds=18, nbColors=3, nbValuesPerColor=6}, user=User{id='null', username=username4, email=email4, birth='Tue Feb 01 00:00:00 CET 2000', gender='OTHER'}, score=15, winner=true, clickCount=10, rightClickCount=5, rapidClickCount=5}"; - - assertEquals(expected, fixture[3].toString()); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/RecoveryPasswordTokenTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/RecoveryPasswordTokenTest.java deleted file mode 100644 index 2520cf1..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/RecoveryPasswordTokenTest.java +++ /dev/null @@ -1,152 +0,0 @@ -package uppa.project.pojo; - -import java.math.BigDecimal; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import uppa.project.database.pojo.RecoveryPasswordToken; -import uppa.project.database.pojo.User; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class RecoveryPasswordTokenTest { - - RecoveryPasswordToken[] fixture; - static User[] usersFixture; - @BeforeAll - static void beforeAll() { - usersFixture = new User[]{ - new User(new BigDecimal(1), "username1", "email1", "password1", new Date(96, Calendar.FEBRUARY, 20), User.Gender.MALE, null), - new User(new BigDecimal(2), "username2", "email2", "password2", new Date(101, Calendar.MAY, 12), User.Gender.OTHER, null), - new User(new BigDecimal(3), "username3", "email3", "password3", new Date(100, Calendar.SEPTEMBER, 30), User.Gender.FEMALE, null) - }; - } - - @BeforeEach - void beforeEach() { - fixture = new RecoveryPasswordToken[]{ - new RecoveryPasswordToken( - new BigDecimal(1), - "token1", - usersFixture[0], - new Date(124, Calendar.MARCH,28)), - new RecoveryPasswordToken( - new BigDecimal(2), - "token2", usersFixture[1] -, new Date(124, Calendar.JULY,14)), - new RecoveryPasswordToken( - new BigDecimal(3), - "token3", - usersFixture[2], - new Date(124, Calendar.SEPTEMBER,5)) - }; - } - - - @Test - void test_constructor() { - new RecoveryPasswordToken(new BigDecimal(1), "token1", new User(), new Date(124, Calendar.FEBRUARY, 2)); - new RecoveryPasswordToken(new BigDecimal(2), "token2", new User(), new Date(124, Calendar.APRIL, 4)); - new RecoveryPasswordToken(new BigDecimal(3), "token3", new User(), new Date(124, Calendar.JUNE, 6)); - } - - @Test - void test_getId() { - HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new BigDecimal(1)); - put(fixture[1], new BigDecimal(2)); - put(fixture[2], new BigDecimal(3)); - }}; - - for (RecoveryPasswordToken token : TESTS.keySet()) - assertEquals(TESTS.get(token), token.getId()); - } - - @Test - void test_getToken() { - HashMap TESTS = new HashMap<>() { - { - put(fixture[0], "token1"); - put(fixture[1], "token2"); - put(fixture[2], "token3"); - }}; - - for (RecoveryPasswordToken token : TESTS.keySet()) - assertEquals(TESTS.get(token), token.getToken()); - } - - @Test - void test_setToken() { - final String EXPECTED = "new-token"; - final RecoveryPasswordToken TOKEN = new RecoveryPasswordToken(new BigDecimal(1), "token", new User(), new Date()); - - TOKEN.setToken(EXPECTED); - assertEquals(EXPECTED, TOKEN.getToken()); - } - - @Test - void test_getUser() { - final HashMap TESTS = new HashMap<>() { - { - put(fixture[0], usersFixture[0]); - put(fixture[1], usersFixture[1]); - put(fixture[2], usersFixture[2]); - }}; - for (RecoveryPasswordToken token : TESTS.keySet()) - assertEquals(TESTS.get(token), token.getUser()); - } - - @Test - void test_setUser() { - RecoveryPasswordToken token = fixture[0]; - assertEquals(usersFixture[0], token.getUser()); - - User newUser = usersFixture[1]; - token.setUser(newUser); - assertEquals(newUser, token.getUser()); - } - - @Test - void test_getExpiresAt() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new Date(124, Calendar.MARCH, 28)); - put(fixture[1], new Date(124, Calendar.JULY, 14)); - put(fixture[2], new Date(124, Calendar.SEPTEMBER, 5)); - }}; - for (RecoveryPasswordToken token : TESTS.keySet()) - assertEquals(TESTS.get(token), token.getExpiresAt()); - - } - - @Test - void test_setExpiresAt() { - RecoveryPasswordToken token = fixture[0]; - assertEquals(new Date(124, Calendar.MARCH, 28), token.getExpiresAt()); - Date newDate = new Date(124, Calendar.FEBRUARY, 3); - token.setExpiresAt(newDate); - assertEquals(newDate, token.getExpiresAt()); - } - - @Test - void test_ToString() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], - "RecoveryPasswordToken{id=1, token=token1, " + - "user=User{id='1', username=username1, email=email1, birth='Tue Feb 20 00:00:00 CET 1996', gender='MALE'}, " + - "expiresAt=Thu Mar 28 00:00:00 CET 2024}"); - put(fixture[1], - "RecoveryPasswordToken{id=2, token=token2, " + - "user=User{id='2', username=username2, email=email2, birth='Sat May 12 00:00:00 CEST 2001', gender='OTHER'}, " + - "expiresAt=Sun Jul 14 00:00:00 CEST 2024}"); - put(fixture[2], - "RecoveryPasswordToken{id=3, token=token3, " + - "user=User{id='3', username=username3, email=email3, birth='Sat Sep 30 00:00:00 CEST 2000', gender='FEMALE'}, " + - "expiresAt=Thu Sep 05 00:00:00 CEST 2024}"); - }}; - for (RecoveryPasswordToken token : TESTS.keySet()) - assertEquals(TESTS.get(token), token.toString()); - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/UserTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/UserTest.java deleted file mode 100644 index 6f44b6b..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/pojo/UserTest.java +++ /dev/null @@ -1,370 +0,0 @@ -package uppa.project.pojo; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import uppa.project.database.pojo.Game; -import uppa.project.database.pojo.Player; -import uppa.project.database.pojo.User; - -import static org.junit.jupiter.api.Assertions.*; - -class UserTest { - - User[] fixture; - static Player[][] playersFixture; - - @BeforeAll - static void beforeAll() { - - playersFixture = new Player[][]{ - { - new Player(new BigDecimal(1), - new Game(Game.Difficulty.EASY, 50,20, 4, 13), - new User(), 10, false, 10, 9, 5) - }, - { - new Player(new BigDecimal(2), - new Game(Game.Difficulty.EASY, 50, 20, 4, 13), - new User(), 10, true, 20, 17, 12), - new Player(new BigDecimal(3), - new Game(Game.Difficulty.EASY, 10, 20, 4, 13), - new User(), 10, false, 15, 5, 2) - }, - { - new Player(new BigDecimal(4), - new Game(Game.Difficulty.EASY, 50, 20, 4, 13), - new User(), 10, true, 5, 3, 1), - new Player(new BigDecimal(5), - new Game(Game.Difficulty.EASY, 10, 20, 4, 13), - new User(), 10, true, 16, 16, 10), - new Player(new BigDecimal(6), - new Game(Game.Difficulty.EASY, 15, 20, 4, 13), - new User(), 10, true, 17, 11, 4) - } - }; - } - @BeforeEach - void beforeEach() { - fixture = new User[]{ - new User(new BigDecimal(1), "username1", "email1", "password1", - new Date(1996 - 1900, Calendar.FEBRUARY, 20), User.Gender.MALE, new ArrayList<>(Arrays.asList(playersFixture[0]))), - new User(new BigDecimal(2), "username2", "email2", "password2", - new Date(1998 - 1900, Calendar.JUNE, 6), User.Gender.FEMALE, new ArrayList<>(Arrays.asList(playersFixture[1]))), - new User(new BigDecimal(3), "username3", "email3", "password3", - new Date(1996 - 1900, Calendar.SEPTEMBER, 18), User.Gender.OTHER, new ArrayList<>(Arrays.asList(playersFixture[2]))), - }; - } - - @Test - void test_constructor() { - new User(); - new User("username", "email", "password", new Date(), User.Gender.OTHER); - new User(new BigDecimal(1), "username1", "email1", "password1", new Date(), User.Gender.MALE, null); - new User(new BigDecimal(2), "username1", "email1", "password1", new Date(), User.Gender.MALE, new ArrayList()); - } - - @Test - void test_getId() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new BigDecimal(1)); - put(fixture[1], new BigDecimal(2)); - put(fixture[2], new BigDecimal(3)); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getId()); - } - } - - @Test - void test_getUsername() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], "username1"); - put(fixture[1], "username2"); - put(fixture[2], "username3"); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getUsername()); - } - } - - @Test - void test_setUsername() { - User user = fixture[0]; - assertEquals("username1", user.getUsername()); - - // Change the username - user.setUsername("new-username"); - assertEquals("new-username", user.getUsername()); - } - - @Test - void test_getEmail() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], "email1"); - put(fixture[1], "email2"); - put(fixture[2], "email3"); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getEmail()); - } - } - - @Test - void test_setEmail() { - User user = fixture[0]; - assertEquals("email1", user.getEmail()); - - // Change the email - user.setEmail("new-email"); - assertEquals("new-email", user.getEmail()); - } - - @Test - void test_getPassword() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], "password1"); - put(fixture[1], "password2"); - put(fixture[2], "password3"); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getPassword()); - } - } - - - @Test - void test_setPassword() { - User user = fixture[0]; - assertEquals("password1", user.getPassword()); - - // Change the password - user.setPassword("new-password"); - assertEquals(User.hashPassword("new-password"), user.getPassword()); - } - - @Test - void test_getBirth() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], new Date(1996 - 1900, Calendar.FEBRUARY, 20)); - put(fixture[1], new Date(1998 - 1900, Calendar.JUNE, 6)); - put(fixture[2], new Date(1996 - 1900, Calendar.SEPTEMBER, 18)); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getBirth()); - } - } - - - @Test - void test_setBirth() { - User user = fixture[0]; - assertEquals(new Date(1996 - 1900, Calendar.FEBRUARY, 20), user.getBirth()); - - // Change the birth - user.setBirth(new Date(1996 - 1900, Calendar.MARCH, 20)); - assertEquals(new Date(1996 - 1900, Calendar.MARCH, 20), user.getBirth()); - } - - @Test - void test_getGender() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], User.Gender.MALE); - put(fixture[1], User.Gender.FEMALE); - put(fixture[2], User.Gender.OTHER); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getGender()); - } - } - - @Test - void test_setGender() { - User user = fixture[0]; - assertEquals(User.Gender.MALE, user.getGender()); - - // Change the gender - user.setGender(User.Gender.FEMALE); - assertEquals(User.Gender.FEMALE, user.getGender()); - } - - @Test - void test_UserGender() { - - // Expect the right number of genders - assertEquals(3, User.Gender.values().length); - - // Expect the right name and ordinal for each gender - final String[] EXPECTED_NAMES = new String[]{ - "MALE", "FEMALE", "OTHER" - }; - - for (int index = 0; index < EXPECTED_NAMES.length; index++) { - String expectedName = EXPECTED_NAMES[index]; - User.Gender currentValue = User.Gender.values()[index]; - - assertEquals(index, currentValue.ordinal()); // Ordinal - assertEquals(expectedName, currentValue.name()); // Name - } - } - - @Test - void test_verifyPassword() { - String [] passwords = {"password1", "password2", "password3"}; - User[] users = { - new User("username1", "email1", "password1", new Date(), User.Gender.MALE), - new User("username2", "email2", "password2", new Date(), User.Gender.MALE), - new User("username3", "email3", "password3", new Date(), User.Gender.MALE) - }; - for (int index = 0 ; index< users.length; index++) { - assertTrue(users[index].verifyPassword(passwords[index])); - } - } - - @Test - void test_getPlayedGames() { - final HashMap> TESTS = new HashMap<>() {{ - put(fixture[0], new ArrayList<>(Arrays.asList(playersFixture[0]))); - put(fixture[1], new ArrayList<>(Arrays.asList(playersFixture[1]))); - put(fixture[2], new ArrayList<>(Arrays.asList(playersFixture[2]))); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getPlayedGames()); - } - - } - - @Test - void test_addPlayedGame() { - User user = fixture[0]; - Player player = new Player(new BigDecimal(4), - new Game(Game.Difficulty.EASY, 10,20, 4, 13), - new User(), 10, true, 5, 3, 1); - user.addPlayedGame(player); - ArrayList expected = new ArrayList<>(Arrays.asList(playersFixture[0])); - expected.add(player); - assertEquals(expected, user.getPlayedGames()); - } - - @Test - void test_getNbPlayedGame() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 1); - put(fixture[1], 2); - put(fixture[2], 3); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getPlayedGames().size()); - } - } - - @Test - void test_getNbWin() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 0); - put(fixture[1], 1); - put(fixture[2], 3); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getNbWin()); - } - } - - @Test - void test_getWinRate() { - - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 0.0); - put(fixture[1], 50.); - put(fixture[2], 100.); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getWinRate()); - } - } - - @Test - void test_getNbClicks() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 10); - put(fixture[1], 35); - put(fixture[2], 38); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getNbClicks()); - } - } - - @Test - void test_getNbRightClicks() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 9); - put(fixture[1], 22); - put(fixture[2], 30); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getNbRightClicks()); - } - } - - @Test - void test_getRightClickPercentRate() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 90.); - put(fixture[1], 62.85); - put(fixture[2], 78.94); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getRightClickPercentRate()); - } - } - - @Test - void test_getNbRapidClicks() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 5); - put(fixture[1], 14); - put(fixture[2], 15); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getNbRapidClicks()); - } - } - - @Test - void test_getRapidClickPercentRate() { - final HashMap TESTS = new HashMap<>() {{ - put(fixture[0], 50.); - put(fixture[1], 40.); - put(fixture[2], 39.47); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.getRapidClickPercentRate()); - } - } - - @Test - void test_toString() { - final HashMap TESTS = new HashMap<>() {{ - put(new User(new BigDecimal(1), "username1", "email1", "password1", - new Date(1996-1900, 1, 4), User.Gender.MALE,null), - "User{id='1', username=username1, email=email1, birth='Sun Feb 04 00:00:00 CET 1996', gender='MALE'}" - ); - put(new User(new BigDecimal(2), "username2", "email2", "password2", - new Date(1996-1900, 2, 6), User.Gender.FEMALE,null), - "User{id='2', username=username2, email=email2, birth='Wed Mar 06 00:00:00 CET 1996', gender='FEMALE'}"); - put(new User(new BigDecimal(3), "username3", "email3", "password3", - new Date(1996-1900, 3, 5), User.Gender.OTHER,null), - "User{id='3', username=username3, email=email3, birth='Fri Apr 05 00:00:00 CEST 1996', gender='OTHER'}"); - }}; - for (User user : TESTS.keySet()) { - assertEquals(TESTS.get(user), user.toString()); - } - } -} diff --git a/S2/DevWeb/Projet/src/test/java/uppa/project/provider/EntityManagerProviderTest.java b/S2/DevWeb/Projet/src/test/java/uppa/project/provider/EntityManagerProviderTest.java deleted file mode 100644 index a2cf3b7..0000000 --- a/S2/DevWeb/Projet/src/test/java/uppa/project/provider/EntityManagerProviderTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package uppa.project.provider; - -import jakarta.persistence.EntityManager; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import uppa.project.database.dao.EntityManagerProvider; - -import static org.junit.jupiter.api.Assertions.*; - -class EntityManagerProviderTest { - - @BeforeAll - static void setUp() { - EntityManagerProvider.setPersitenceUnitName("test"); - } - - @Test - void test_getInstance() { - // If the method throws an exception, make sure the database is running - assertDoesNotThrow(EntityManagerProvider::getInstance); - - // Check if the object is unique - EntityManager entity1 = EntityManagerProvider.getInstance(); - EntityManager entity2 = EntityManagerProvider.getInstance(); - - assertNotNull(entity1); - assertNotNull(entity2); - assertEquals(entity1, entity2); - } - - @Test - void test_close() { - // If the method throws an exception, make sure the database is running - assertDoesNotThrow(EntityManagerProvider::getInstance); - - // Check if the object is closed - EntityManager entity = EntityManagerProvider.getInstance(); - EntityManagerProvider.close(); - - assertFalse(entity::isOpen); - } -}