Skip to content

Commit

Permalink
Test controller
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Kelian committed Dec 10, 2023
1 parent bdc4641 commit ccd552d
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,24 @@ public static void deleteUserByName(String name) {
e.printStackTrace();
}
}

public static void deleteUser(int testUserId) {
try {
String deleteUserQuery = "DELETE FROM USER WHERE IdUser = '" + testUserId + "'";
bdd.state.executeUpdate(deleteUserQuery);
}
catch (SQLException e) {
e.printStackTrace();
}
}

public static void deleteMission(int testMissionId) {
try {
String deleteMissionQuery = "DELETE FROM MISSIONS WHERE IdMission = '" + testMissionId + "'";
bdd.state.executeUpdate(deleteMissionQuery);
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
150 changes: 150 additions & 0 deletions src/test/java/controller/ControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package controller;

import model.Mission;
import model.Status;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class ControllerTest {

// Utilisez ces variables pour stocker les données de test
private static final String testUserEmail = "[email protected]";
private static final String testUserPassword = "testPassword";
private static int testUserId;

// Cette méthode s'exécute une fois avant le début des tests
@BeforeAll
public static void setUp() throws SQLException {
// Ajoutez un utilisateur de test à la base de données
String firstName = "Test";
String lastName = "User";
String email = testUserEmail;
String password = testUserPassword;

String result = Controller.createNewUser(firstName, lastName, email, password, true, false, false);

assertEquals("User successfully created", result);

// Récupérez l'ID de l'utilisateur de test
String[] loginResult = Controller.logIn(email, password);
testUserId = Integer.parseInt(loginResult[2]);
}

// Cette méthode s'exécute une fois après la fin de tous les tests
@AfterAll
public static void tearDown() {
Controller.deleteUser(testUserId);
}

@Test
public void testCreateNewUser() throws SQLException {

String firstName = "Test";
String lastName = "User";
String email = "[email protected]";
String password = "testPassword";

String result = Controller.createNewUser(firstName, lastName, email, password, true, false, false);

assertEquals("User successfully created", result);

String[] loginResult = Controller.logIn(email, password);
int testId= Integer.parseInt(loginResult[2]);

Controller.deleteUser(testId);
}

@Test
public void testLogIn() throws SQLException {
// Assuming there is a test user in the database with known credentials
String email = "[email protected]";
String password = "testPassword";

String[] result = Controller.logIn(email, password);

assertEquals("Login successful", result[0]);
assertEquals("Needer", result[1]);
assertEquals(testUserId, Integer.parseInt(result[2]));
}

@Test
public void testCreateExistingUser() throws SQLException{
String firstName = "Test";
String lastName = "User";
String email = "[email protected]";
String password = "testPassword";

String result = Controller.createNewUser(firstName, lastName, email, password, true, false, false);

assertEquals("User already exists", result);
}

@Test
public void testCreateNewMission() {
// Use the test user's ID
int idUser = testUserId;

// Data for the test mission
String title = "Test Mission";
String description = "Test Description";
String date = "2023-01-01 18:00:00";

// Perform the mission creation
String result = Controller.createNewMission(title, description, date, idUser);

// Ensure the mission is created successfully
assertEquals("Mission successfully created", result);

// Retrieve the ID of the created mission (this assumes you have a method to get mission data)
ArrayList<Mission> missions = Controller.getMissionsOfNeeder(idUser);
assertFalse(missions.isEmpty());
int testMissionId = missions.get(0).getIdMission();
Controller.deleteMission(testMissionId);
}

@Test
public void testAcceptOrRefuseMission(){
int idUser = testUserId;

// Data for the test mission
String title = "Test Mission";
String description = "Test Description";
String date = "2023-01-01 18:00:00";

// Perform the mission creation
String result = Controller.createNewMission(title, description, date, idUser);

// Ensure the mission is created successfully
assertEquals("Mission successfully created", result);

// Retrieve the ID of the created mission (this assumes you have a method to get mission data)
ArrayList<Mission> missions = Controller.getMissionsOfNeeder(idUser);
assertFalse(missions.isEmpty());
int testMissionId = missions.get(0).getIdMission();

//accept
Controller.acceptMission(testMissionId);
missions.clear();
missions = Controller.getMissionsOfNeeder(idUser);
assertFalse(missions.isEmpty());
assertEquals(Status.Accepted, missions.get(0).getStatus());

//refuse
Controller.refuseMission(testMissionId);
missions.clear();
missions = Controller.getMissionsOfNeeder(idUser);
assertFalse(missions.isEmpty());
assertEquals(Status.Refused, missions.get(0).getStatus());

Controller.deleteMission(testMissionId);
}


}

0 comments on commit ccd552d

Please sign in to comment.