feat: dev-web - add game and recoveryPassorwdToken tests

This commit is contained in:
kmitresse
2024-03-26 22:22:51 +01:00
parent f8e28d79ab
commit ee5a0b9f58
3 changed files with 246 additions and 30 deletions
@@ -81,6 +81,15 @@ public class Game implements Serializable {
*/
public Game(Difficulty difficulty, int nbRounds, int nbColors, int nbValuesPerColor) {
this.difficulty = difficulty;
if (nbRounds < 1 || nbRounds > nbColors * nbValuesPerColor){
throw new IllegalArgumentException("Le nombre de tours doit être supérieur ou égal à 1");
}
if (nbColors < 1 || nbColors > Card.Color.values().length) {
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
}
if (nbValuesPerColor < 1 || nbValuesPerColor > Card.Value.values().length) {
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre 1 et " + Card.Value.values().length);
}
this.nbRounds = nbRounds;
this.nbColors = nbColors;
this.nbValuesPerColor = nbValuesPerColor;
@@ -102,6 +111,15 @@ public class Game implements Serializable {
this.id = id;
this.createdAt = createdAt;
this.difficulty = difficulty;
if (nbRounds < 1 || nbRounds > nbColors * nbValuesPerColor){
throw new IllegalArgumentException("Le nombre de tours doit être compris entre 1 et " + nbColors * nbValuesPerColor);
}
if (nbColors < 1 || nbColors > Card.Color.values().length) {
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
}
if (nbValuesPerColor < 1 || nbValuesPerColor > Card.Value.values().length) {
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre 1 et " + Card.Value.values().length);
}
this.nbRounds = nbRounds;
this.nbColors = nbColors;
this.nbValuesPerColor = nbValuesPerColor;
@@ -153,6 +171,15 @@ public class Game implements Serializable {
* @param nbRounds le nouveau nombre de tours de la partie
*/
public void setNbRounds(int nbRounds) {
if (nbRounds < 1 || nbRounds > nbColors * nbValuesPerColor){
throw new IllegalArgumentException("Le nombre de tours doit être compris entre 1 et " + nbColors * nbValuesPerColor);
}
if (nbColors < 1 || nbColors > Card.Color.values().length) {
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
}
if (nbValuesPerColor < 1 || nbValuesPerColor > Card.Value.values().length) {
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre 1 et " + Card.Value.values().length);
}
this.nbRounds = nbRounds;
}
@@ -167,6 +194,9 @@ public class Game implements Serializable {
* @param nbColors le nouveau nombre de couleurs présente dans le deck
*/
public void setNbColors(int nbColors) {
if (nbColors < 1 || nbColors > Card.Color.values().length) {
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
}
this.nbColors = nbColors;
}
@@ -181,6 +211,9 @@ public class Game implements Serializable {
* @param nbValuesPerColor le nouveau nombre de valeurs par couleur
*/
public void setNbValuesPerColor(int nbValuesPerColor) {
if (nbValuesPerColor < 1 || nbValuesPerColor > Card.Value.values().length) {
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre 1 et " + Card.Value.values().length);
}
this.nbValuesPerColor = nbValuesPerColor;
}
@@ -1,11 +1,82 @@
package uppa.project.pojo;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GameTest {
static Game[] FIXTURE;
@BeforeEach
void setUp() {
FIXTURE = new Game[] {
new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 3,6, new ArrayList<Player>()),
new Game(new BigDecimal(2), new Date(2024,3,26), Game.Difficulty.HARD, 52, 4,13, new ArrayList<Player>()),
new Game(new BigDecimal(3), new Date(2023,9,18), Game.Difficulty.EASY, 5, 2,10, new ArrayList<Player>()),
};
}
@Test
void error_if_invalid_nbrounds() {
//In constructor
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 0, 3,6, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, -1, 3,6, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, -5, 3,6, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 19, 3,6, new ArrayList<Player>()));
//In setter
for (int i = 0; i < FIXTURE.length; i++) {
Game game = FIXTURE[i];
assertThrows(IllegalArgumentException.class, () -> game.setNbRounds(0));
assertThrows(IllegalArgumentException.class, () -> game.setNbRounds(-1));
assertThrows(IllegalArgumentException.class, () -> game.setNbRounds(-5));
assertThrows(IllegalArgumentException.class, () -> game.setNbRounds(19));
}
}
@Test
void error_if_invalid_nbColors() {
//In constructor
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 0,6, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, -1,6, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, -5,6, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 5,6, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 17,6, new ArrayList<Player>()));
//In setter
for (int i = 0; i < FIXTURE.length; i++) {
Game game = FIXTURE[i];
assertThrows(IllegalArgumentException.class, () -> game.setNbColors(0));
assertThrows(IllegalArgumentException.class, () -> game.setNbColors(-1));
assertThrows(IllegalArgumentException.class, () -> game.setNbColors(-5));
assertThrows(IllegalArgumentException.class, () -> game.setNbColors(5));
assertThrows(IllegalArgumentException.class, () -> game.setNbColors(17));
}
}
@Test
void error_if_invalid_nbValuesPerColor() {
//In constructor
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 3,0, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 3,-1, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 3,-5, new ArrayList<Player>()));
assertThrows(IllegalArgumentException.class, () -> new Game(new BigDecimal(1), new Date(2023,12,25), Game.Difficulty.EASY, 17, 3,14, new ArrayList<Player>()));
//In setter
for (int i = 0; i < FIXTURE.length; i++) {
Game game = FIXTURE[i];
assertThrows(IllegalArgumentException.class, () -> game.setNbValuesPerColor(0));
assertThrows(IllegalArgumentException.class, () -> game.setNbValuesPerColor(-1));
assertThrows(IllegalArgumentException.class, () -> game.setNbValuesPerColor(-5));
assertThrows(IllegalArgumentException.class, () -> game.setNbValuesPerColor(14));
}
}
@Test
void test_HashCode() {
// TODO Implement this method
@@ -13,52 +84,101 @@ class GameTest {
@Test
void get_Id() {
// TODO Implement this method
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(FIXTURE[i].getId(), new BigDecimal(i + 1));
}
}
@Test
void get_Created_At() {
// TODO Implement this method
Date[] dates = new Date[]{new Date(2023,12,25),
new Date(2024,3,26),
new Date(2023,9,18)};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(FIXTURE[i].getCreatedAt(), dates[i]);
}
}
@Test
void get_Difficulty() {
// TODO Implement this method
Game.Difficulty[] difficulties = new Game.Difficulty[]{
Game.Difficulty.EASY,
Game.Difficulty.HARD,
Game.Difficulty.EASY };
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(FIXTURE[i].getDifficulty(), difficulties[i]);
}
}
@Test
void set_difficulty() {
// TODO Implement this method
Game.Difficulty[] difficulties = new Game.Difficulty[]{
Game.Difficulty.EASY,
Game.Difficulty.HARD,
Game.Difficulty.EASY };
for (int i = 0; i < FIXTURE.length; i++) {
FIXTURE[i].setDifficulty(Game.Difficulty.HARD);
assertEquals(FIXTURE[i].getDifficulty(), Game.Difficulty.HARD);
FIXTURE[i].setDifficulty(difficulties[i]);
assertEquals(FIXTURE[i].getDifficulty(), difficulties[i]);
}
}
@Test
void get_nb_Rounds() {
// TODO Implement this method
int[] nbRounds = new int[]{17, 52, 5};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(FIXTURE[i].getNbRounds(), nbRounds[i]);
}
}
@Test
void set_nb_Rounds() {
// TODO Implement this method
int[] nbRounds = new int[]{17, 52, 5};
for (int i = 0; i < FIXTURE.length; i++) {
FIXTURE[i].setNbRounds(10);
assertEquals(FIXTURE[i].getNbRounds(), 10);
FIXTURE[i].setNbRounds(nbRounds[i]);
assertEquals(FIXTURE[i].getNbRounds(), nbRounds[i]);
}
}
@Test
void get_nb_Colors() {
// TODO Implement this method
int[] nbColors = new int[]{3, 4, 2};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(FIXTURE[i].getNbColors(), nbColors[i]);
}
}
@Test
void set_nb_Colors() {
// TODO Implement this method
int[] nbColors = new int[]{3, 4, 2};
for (int i = 0; i < FIXTURE.length; i++) {
FIXTURE[i].setNbColors(5);
assertEquals(FIXTURE[i].getNbColors(), 5);
FIXTURE[i].setNbColors(nbColors[i]);
assertEquals(FIXTURE[i].getNbColors(), nbColors[i]);
}
}
@Test
void getnbValuesPerColor() {
// TODO Implement this method
int[] nbValuesPerColor = new int[]{6, 13, 10};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(FIXTURE[i].getNbValuesPerColor(), nbValuesPerColor[i]);
}
}
@Test
void setNbValuesPerColor() {
// TODO Implement this method
int[] nbValuesPerColor = new int[]{6, 13, 10};
for (int i = 0; i < FIXTURE.length; i++) {
FIXTURE[i].setNbValuesPerColor(8);
assertEquals(FIXTURE[i].getNbValuesPerColor(), 8);
FIXTURE[i].setNbValuesPerColor(nbValuesPerColor[i]);
assertEquals(FIXTURE[i].getNbValuesPerColor(), nbValuesPerColor[i]);
}
}
@Test
@@ -98,8 +218,14 @@ class GameTest {
}
@Test
void testToString() {
// TODO Implement this method
fail();
void to_string_return_the_right_format() {
String[] expected = new String[]{
"Game{id=1, createdAt=2023-12-25, players=[]}",
"Game{id=2, createdAt=2024-03-26, players=[]}",
"Game{id=3, createdAt=2023-09-18, players=[]}"
};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(FIXTURE[i].toString(), expected[i]);
}
}
}
@@ -1,62 +1,119 @@
package uppa.project.pojo;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class RecoveryPasswordTokenTest {
static RecoveryPasswordToken[] FIXTURE;
@BeforeAll
static void setUp() {
FIXTURE = new RecoveryPasswordToken[] {
new RecoveryPasswordToken(1, "token1", new User(new BigDecimal(1), "username1", "password1", "email1", new Date(1996, 1,4), User.Gender.MALE, new ArrayList<Player>()),new Date(2024,1,2)),
new RecoveryPasswordToken(2, "token2", new User(new BigDecimal(2), "username2", "password2", "email2", new Date(1996, 2,5), User.Gender.FEMALE, new ArrayList<Player>()),new Date(2024,3,4)),
new RecoveryPasswordToken(3, "token3", new User(new BigDecimal(3), "username3", "password3", "email3", new Date(1996, 3,6), User.Gender.OTHER, new ArrayList<Player>()), new Date(2024,5,6))
};
}
@Test
void getId() {
// TODO Implement this method
fail();
assertEquals(1, FIXTURE[0].getId());
assertEquals(2, FIXTURE[1].getId());
assertEquals(3, FIXTURE[2].getId());
}
@Test
void getToken() {
// TODO Implement this method
fail();
assertEquals("token1", FIXTURE[0].getToken());
assertEquals("token2", FIXTURE[1].getToken());
assertEquals("token3", FIXTURE[2].getToken());
}
@Test
void setToken() {
// TODO Implement this method
fail();
String[] expected = new String[]{"token1", "token2", "token3"};
for (int i = 0; i < FIXTURE.length; i++) {
FIXTURE[i].setToken(expected[i]);
assertEquals(expected[i], FIXTURE[i].getToken());
}
}
@Test
void getUser() {
// TODO Implement this method
fail();
User[] expected = new User[]{
new User(new BigDecimal(1), "username1", "password1", "email1", new Date(1996, 1, 4), User.Gender.MALE, new ArrayList<Player>()),
new User(new BigDecimal(2), "username2", "password2", "email2", new Date(1996, 2, 5), User.Gender.FEMALE, new ArrayList<Player>()),
new User(new BigDecimal(3), "username3", "password3", "email3", new Date(1996, 3, 6), User.Gender.OTHER, new ArrayList<Player>())};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(expected[i].getId(), FIXTURE[i].getUser().getId());
assertEquals(expected[i].getUsername(), FIXTURE[i].getUser().getUsername());
assertEquals(expected[i].getPassword(), FIXTURE[i].getUser().getPassword());
assertEquals(expected[i].getEmail(), FIXTURE[i].getUser().getEmail());
assertEquals(expected[i].getBirth(), FIXTURE[i].getUser().getBirth());
assertEquals(expected[i].getGender(), FIXTURE[i].getUser().getGender());
assertEquals(expected[i].getPlayedGames(), FIXTURE[i].getUser().getPlayedGames());
}
}
@Test
void setUser() {
// TODO Implement this method
fail();
User[] expected = new User[]{
new User(new BigDecimal(4), "username4", "password4", "email4", new Date(1996, 4, 7), User.Gender.MALE, new ArrayList<Player>()),
new User(new BigDecimal(5), "username5", "password5", "email5", new Date(1996, 5, 8), User.Gender.FEMALE, new ArrayList<Player>()),
new User(new BigDecimal(6), "username6", "password6", "email6", new Date(1996, 6, 9), User.Gender.OTHER, new ArrayList<Player>())};
for (int i = 0; i < FIXTURE.length; i++) {
FIXTURE[i].setUser(expected[i]);
assertEquals(expected[i].getId(), FIXTURE[i].getUser().getId());
assertEquals(expected[i].getUsername(), FIXTURE[i].getUser().getUsername());
assertEquals(expected[i].getPassword(), FIXTURE[i].getUser().getPassword());
assertEquals(expected[i].getEmail(), FIXTURE[i].getUser().getEmail());
assertEquals(expected[i].getBirth(), FIXTURE[i].getUser().getBirth());
assertEquals(expected[i].getGender(), FIXTURE[i].getUser().getGender());
assertEquals(expected[i].getPlayedGames(), FIXTURE[i].getUser().getPlayedGames());
}
}
@Test
void getExpiresAt() {
// TODO Implement this method
fail();
Date[] expected = new Date[]{
new Date(2024, 1, 2),
new Date(2024, 3, 4),
new Date(2024, 5, 6)};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(expected[i], FIXTURE[i].getExpiresAt());
}
}
@Test
void setExpiresAt() {
// TODO Implement this method
fail();
Date[] expected = new Date[]{
new Date(2024, 2, 3),
new Date(2024, 4, 5),
new Date(2024, 6, 7)};
for (int i = 0; i < FIXTURE.length; i++) {
FIXTURE[i].setExpiresAt(expected[i]);
assertEquals(expected[i], FIXTURE[i].getExpiresAt());
}
}
@Test
void testToString() {
void to_string_return_the_rigth_format() {
// TODO Implement this method
fail();
}
@Test
void getExpirationDate() {
// TODO Implement this method
fail();
Date[] expected = new Date[]{
new Date(2024, 1, 2),
new Date(2024, 3, 4),
new Date(2024, 5, 6)};
for (int i = 0; i < FIXTURE.length; i++) {
assertEquals(expected[i], FIXTURE[i].getExpirationDate());
}
}
}