vérification des tests soumettreDemande, validateur et creationAccountPage + avancée sur les tests pour bénévoleDemandesFinaliséesPage
This commit is contained in:
parent
141cd3fa6a
commit
c3f6d9a002
7 changed files with 94 additions and 55 deletions
|
@ -75,6 +75,14 @@ public class BenevoleDemandesFinaliseesPage extends JFrame {
|
|||
loadDemandesFinalisees();
|
||||
}
|
||||
|
||||
//Méthodes de type get()
|
||||
public JTable getTable() {return this.table;}
|
||||
public DefaultTableModel getTableModel() {return this.tableModel;}
|
||||
public JButton getVoirAvisButton() {return this.voirAvisButton;}
|
||||
public JButton getModifierAvisButton() {return this.modifierAvisButton;}
|
||||
public JButton getRetourBenevoleButton() {return this.retourBenevoleButton;}
|
||||
public int getID() {return this.utilisateurId;}
|
||||
|
||||
private void afficherAvis() {
|
||||
int selectedRow = table.getSelectedRow();
|
||||
if (selectedRow == -1) {
|
||||
|
|
|
@ -52,7 +52,4 @@ class BenevoleDemandesEncoursPageTest {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -4,46 +4,96 @@ import database.DatabaseConnection;
|
|||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class BenevoleDemandesFinaliseesPageTest {
|
||||
private BenevoleDemandesFinaliseesPage page;
|
||||
private int utilisateurId = 1; // ID fictif de l'utilisateur
|
||||
private int demandeId = 1; // ID fictif de la demande
|
||||
private BenevoleDemandesFinaliseesPage benevoleDemandesFinaliseesPage;
|
||||
private JTable table;
|
||||
private DefaultTableModel tableModel;
|
||||
private int user_besoinID = 1;
|
||||
private int benevoleID = 2; // ID fictif de l'utilisateur
|
||||
private int demandeID; // ID fictif de la demande
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws SQLException {
|
||||
// Préparer la base de données avec des données fictives pour les tests
|
||||
try (Connection connection = DatabaseConnection.getConnection()) {
|
||||
// Insérer un utilisateur fictif
|
||||
String insertUtilisateurSQL = "INSERT IGNORE INTO utilisateur (id) VALUES (?)";
|
||||
try (PreparedStatement utilisateurStatement = connection.prepareStatement(insertUtilisateurSQL)) {
|
||||
utilisateurStatement.setInt(1, utilisateurId);
|
||||
utilisateurStatement.executeUpdate();
|
||||
}
|
||||
|
||||
// Insérer une demande liée à cet utilisateur
|
||||
String insertDemandeSQL = "INSERT IGNORE INTO demandes_aide (id, description, statut, benevole_id) VALUES (?, ?, ?, ?)";
|
||||
try (PreparedStatement demandeStatement = connection.prepareStatement(insertDemandeSQL)) {
|
||||
demandeStatement.setInt(1, demandeId);
|
||||
demandeStatement.setString(2, "Test demande description");
|
||||
demandeStatement.setString(3, "finalisée");
|
||||
demandeStatement.setInt(4, utilisateurId);
|
||||
demandeStatement.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
// Initialiser la page pour les tests
|
||||
page = new BenevoleDemandesFinaliseesPage(utilisateurId);
|
||||
// Initialiser la benevoleDemandesFinaliseesPage pour les tests
|
||||
benevoleDemandesFinaliseesPage = new BenevoleDemandesFinaliseesPage(benevoleID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPageCreation() {
|
||||
// Vérifie que l'instance de la page est créée correctement
|
||||
assertNotNull(page);
|
||||
// on vérifie que l'instance de la benevoleDemandesFinaliseesPage est créée correctement
|
||||
assertNotNull(benevoleDemandesFinaliseesPage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testModifierAvisForFinalizedRequest() {
|
||||
//on crée une demande avec le statut 'finalisée' pour tester la méthode modifierAvis(), et avec un avis original
|
||||
try (Connection connection = DatabaseConnection.getConnection()) {
|
||||
String insertSQL = "INSERT INTO demandes_aide (statut, utilisateur_id, benevole_id, description, avis_benevole) VALUES (?, ?, ?, ?, ?)";
|
||||
PreparedStatement insertStatement = connection.prepareStatement(insertSQL, PreparedStatement.RETURN_GENERATED_KEYS);
|
||||
insertStatement.setString(1, "finalisée");
|
||||
insertStatement.setInt(2, user_besoinID);
|
||||
insertStatement.setInt(3, benevoleID); // Aucun bénévole affecté initialement
|
||||
insertStatement.setString(4, "Demande de test pour JUnit / Test modif avis_benevole");
|
||||
insertStatement.setString(5, "Avis_benevole à modifier");
|
||||
insertStatement.executeUpdate();
|
||||
|
||||
// Récupération de l'ID généré pour pouvoir le supprimer après
|
||||
ResultSet generatedKeys = insertStatement.getGeneratedKeys();
|
||||
if (generatedKeys.next()) {
|
||||
demandeID = generatedKeys.getInt(1);
|
||||
}
|
||||
|
||||
// on met à jour la table avec la nouvelle demande rajoutée
|
||||
// on utilise une méthode détournée pour accéder à la méthode private 'loadAnciennesDemandes()'
|
||||
Method loadDemandesFinaliseesMethod = BenevoleDemandesFinaliseesPage.class.getDeclaredMethod("loadDemandesFinalisees");
|
||||
loadDemandesFinaliseesMethod.setAccessible(true); // permet d'appeler la méthode même si elle est private
|
||||
loadDemandesFinaliseesMethod.invoke(benevoleDemandesFinaliseesPage); // on l'appele
|
||||
|
||||
//test de modifierAvis(); on sélectionne la dernière ligne de la table car elle correspond à la demande test
|
||||
int lastRow = benevoleDemandesFinaliseesPage.getTable().getRowCount()-1;
|
||||
benevoleDemandesFinaliseesPage.getTable().setRowSelectionInterval(lastRow, lastRow);
|
||||
|
||||
// idem que pour loadAnciennesDemandes() comme modifierAvis() est aussi en private
|
||||
Method modifierAvisMethod = BenevoleDemandesFinaliseesPage.class.getDeclaredMethod("modifierAvis");
|
||||
modifierAvisMethod.setAccessible(true); // permet d'appeler la méthode même si elle est private
|
||||
modifierAvisMethod.invoke(benevoleDemandesFinaliseesPage); // on l'appele
|
||||
|
||||
// on attend un moment pour que la base de données se mette à jour
|
||||
Thread.sleep(1000);
|
||||
|
||||
String sql = "SELECT avis_benevole FROM demandes_aide WHERE id = ?";
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setInt(1, demandeID);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
String currentAvis = "";
|
||||
if (resultSet.next()) {
|
||||
currentAvis = resultSet.getString("avis_benevole");
|
||||
}
|
||||
|
||||
assertNotEquals("Avis_benevole à modifier", currentAvis, "L'avis n'a pas été modifié.");
|
||||
|
||||
// Suppression de la demande de test après le test de prendreDemande()
|
||||
String deleteSQL = "DELETE FROM demandes_aide WHERE id = ?";
|
||||
PreparedStatement deleteStatement = connection.prepareStatement(deleteSQL);
|
||||
deleteStatement.setInt(1, demandeID);
|
||||
deleteStatement.executeUpdate();
|
||||
} catch (InterruptedException | InvocationTargetException | NoSuchMethodException | IllegalAccessException |
|
||||
SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,10 +24,7 @@ class CreateAccountPageTest {
|
|||
private JComboBox<String> roleComboBox;
|
||||
private JButton createAccountButton;
|
||||
private JButton retourLoginButton;
|
||||
|
||||
//TODO
|
||||
|
||||
/*
|
||||
@BeforeEach
|
||||
void setUp() throws NoSuchFieldException, IllegalAccessException {
|
||||
createAccountPage = new CreateAccountPage();
|
||||
|
@ -124,5 +121,5 @@ class CreateAccountPageTest {
|
|||
// Check that the current frame is disposed
|
||||
assertFalse(createAccountPage.isVisible(), "La page de création de compte devrait être fermée après avoir cliqué sur 'Retour à la connexion'.");
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
|
|
@ -60,11 +60,8 @@ class LoginPageTest {
|
|||
|
||||
// Verify that the email field contains the invalid email entered
|
||||
assertEquals("nonexistent@example.com", emailField.getText(), "The entered email should remain in the email field.");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testCreateAccountButtonAction() {
|
||||
createAccountButton.doClick();
|
||||
|
|
|
@ -21,12 +21,12 @@ class SoumettreDemandeTest {
|
|||
private SoumettreDemande soumettreDemande;
|
||||
private JTextField descriptionField;
|
||||
private DefaultTableModel tableModel;
|
||||
private int utilisateurId = 1; // utilisateur arbitraire qui va créer les demandes de test
|
||||
private int utilisateurID = 1; // utilisateur arbitraire qui va créer les demandes de test
|
||||
private int demandeID;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws NoSuchFieldException, IllegalAccessException {
|
||||
soumettreDemande = new SoumettreDemande(utilisateurId);
|
||||
soumettreDemande = new SoumettreDemande(utilisateurID);
|
||||
|
||||
// Access private fields using reflection
|
||||
descriptionField = (JTextField) getField("descriptionField");
|
||||
|
@ -52,7 +52,7 @@ class SoumettreDemandeTest {
|
|||
String sql = "SELECT description FROM demandes_aide WHERE description = ? AND utilisateur_id = ?";
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, "Test request description");
|
||||
statement.setInt(2, utilisateurId);
|
||||
statement.setInt(2, utilisateurID);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
assertTrue(resultSet.next(), "The request should be added to the database.");
|
||||
|
@ -77,16 +77,14 @@ class SoumettreDemandeTest {
|
|||
assertEquals(0, tableModel.getRowCount(), "No request should be submitted if the description is empty.");
|
||||
}
|
||||
|
||||
|
||||
//TODO
|
||||
/* @Test
|
||||
void testModifierAvisForFinalizedRequest() throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
@Test
|
||||
void testModifierAvisForFinalizedRequest() {
|
||||
//on crée une demande avec le statut 'finalisée' pour tester la méthode modifierAvis(), et avec un avis original
|
||||
try (Connection connection = DatabaseConnection.getConnection()) {
|
||||
String insertSQL = "INSERT INTO demandes_aide (statut, utilisateur_id, benevole_id, description, avis_besoin) VALUES (?, ?, ?, ?, ?)";
|
||||
PreparedStatement insertStatement = connection.prepareStatement(insertSQL, PreparedStatement.RETURN_GENERATED_KEYS);
|
||||
insertStatement.setString(1, "finalisée");
|
||||
insertStatement.setInt(2, utilisateurId);
|
||||
insertStatement.setInt(2, utilisateurID);
|
||||
insertStatement.setNull(3, java.sql.Types.INTEGER); // Aucun bénévole affecté initialement
|
||||
insertStatement.setString(4, "Demande de test pour JUnit / Test modif avis_besoin");
|
||||
insertStatement.setString(5, "Avis_besoin à modifier");
|
||||
|
@ -137,6 +135,6 @@ class SoumettreDemandeTest {
|
|||
SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,9 +4,6 @@ import database.DatabaseConnection;
|
|||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
|
@ -86,9 +83,7 @@ class ValidateurTest {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//TODO
|
||||
/* @Test
|
||||
@Test
|
||||
void testRejeterDemande() {
|
||||
//on crée une demande avec le statut 'soumise' pour tester la méthode rejeterDemande()
|
||||
try (Connection connection = DatabaseConnection.getConnection()) {
|
||||
|
@ -146,8 +141,5 @@ class ValidateurTest {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue