From 2e41f1185e26b0cdf601460e822b31c605dd30f4 Mon Sep 17 00:00:00 2001 From: el-ouali Date: Fri, 15 Nov 2024 23:15:44 +0100 Subject: [PATCH] Actualiser src/test/java/controller/SoumettreDemandeTest.java --- .../java/controller/SoumettreDemandeTest.java | 118 ++++++++++++++++-- 1 file changed, 108 insertions(+), 10 deletions(-) diff --git a/src/test/java/controller/SoumettreDemandeTest.java b/src/test/java/controller/SoumettreDemandeTest.java index 6fd2d52..0fa63db 100644 --- a/src/test/java/controller/SoumettreDemandeTest.java +++ b/src/test/java/controller/SoumettreDemandeTest.java @@ -1,28 +1,126 @@ package controller; +import database.DatabaseConnection; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.lang.reflect.Field; +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.*; -class SoumettreDemandeTest { +class LoginPageTest { - private SoumettreDemande soumettreDemande; + private LoginPage loginPage; + private JTextField emailField; + private JButton loginButton; + private JButton createAccountButton; @BeforeEach - void setUp() { - soumettreDemande = new SoumettreDemande(2); // Utilisateur ID fictif + void setUp() throws NoSuchFieldException, IllegalAccessException { + loginPage = new LoginPage(); + + // Access private fields using reflection + emailField = (JTextField) getField("emailField"); + loginButton = (JButton) getField("loginButton"); + createAccountButton = (JButton) getField("createAccountButton"); + } + + // Helper method to access private fields + private Object getField(String fieldName) throws NoSuchFieldException, IllegalAccessException { + Field field = LoginPage.class.getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(loginPage); } @Test - void testSoumettreDemandeNotNull() { - // Vérifier si la page de soumission de demande est bien créée - assertNotNull(soumettreDemande); + void testLoginPageComponents() { + assertNotNull(loginPage); + assertNotNull(emailField); + assertNotNull(loginButton); + assertNotNull(createAccountButton); + + // Check default values and UI setup + assertEquals("", emailField.getText()); } @Test - void testDescriptionFieldIsEmptyInitially() { - // Tester si le champ de description est vide au départ - // assertEquals("", soumettreDemande.getDescriptionField().getText()); + void testLoginWithValidUser() throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { + // Insert a test user in the database + try (Connection connection = DatabaseConnection.getConnection()) { + String insertSQL = "INSERT INTO utilisateur (email, role) VALUES (?, ?)"; + PreparedStatement insertStatement = connection.prepareStatement(insertSQL, PreparedStatement.RETURN_GENERATED_KEYS); + insertStatement.setString(1, "test@example.com"); + insertStatement.setString(2, "benevole"); + insertStatement.executeUpdate(); + + // Get generated user ID for cleanup later + ResultSet generatedKeys = insertStatement.getGeneratedKeys(); + int testUserId = -1; + if (generatedKeys.next()) { + testUserId = generatedKeys.getInt(1); + } + + // Set email field to match the test user + emailField.setText("test@example.com"); + + // Access and invoke the loginUser() method using reflection + Method loginUserMethod = LoginPage.class.getDeclaredMethod("loginUser"); + loginUserMethod.setAccessible(true); + loginUserMethod.invoke(loginPage); + + // Verify redirection to MenuBenevole + assertFalse(loginPage.isVisible(), "LoginPage should close upon successful login."); + + // Clean up the inserted test data + String deleteSQL = "DELETE FROM utilisateur WHERE id = ?"; + PreparedStatement deleteStatement = connection.prepareStatement(deleteSQL); + deleteStatement.setInt(1, testUserId); + deleteStatement.executeUpdate(); + } + } + + @Test + void testLoginWithInvalidEmail() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + // Set an email that does not exist in the database + emailField.setText("nonexistent@example.com"); + + // Access and invoke the loginUser() method using reflection + Method loginUserMethod = LoginPage.class.getDeclaredMethod("loginUser"); + loginUserMethod.setAccessible(true); + loginUserMethod.invoke(loginPage); + + // Check for a dialog box error message (mocked by checking that page remains open) + assertTrue(loginPage.isVisible(), "LoginPage should remain open if login fails."); + } + + @Test + void testLoginWithEmptyEmailField() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + // Leave email field empty + emailField.setText(""); + + // Access and invoke the loginUser() method using reflection + Method loginUserMethod = LoginPage.class.getDeclaredMethod("loginUser"); + loginUserMethod.setAccessible(true); + loginUserMethod.invoke(loginPage); + + // Check for a dialog box error message (mocked by checking that page remains open) + assertTrue(loginPage.isVisible(), "LoginPage should remain open if email field is empty."); + } + + @Test + void testCreateAccountButtonAction() { + // Simulate clicking the "Créer un compte" button + createAccountButton.doClick(); + + // Check that the current frame is disposed + assertFalse(loginPage.isVisible(), "LoginPage should close after clicking 'Créer un compte'."); } }