From 34d787d8b43bf18eb044b7e3eb89a3decac5a39b Mon Sep 17 00:00:00 2001 From: panuozzo77 Date: Sat, 13 Jan 2024 09:56:35 +0100 Subject: [PATCH] Unit test --- src/test/java/controller/ScheduleManagerTest.java | 3 +++ src/test/java/controller/SendResetPinTest.java | 2 +- .../java/model/service/license/LicenseActivationTest.java | 3 +++ src/test/java/model/service/login/AuthenticatorTest.java | 7 +++++-- .../java/model/service/registration/RegistrationTest.java | 8 ++++++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/test/java/controller/ScheduleManagerTest.java b/src/test/java/controller/ScheduleManagerTest.java index fec9785..bc81cb1 100644 --- a/src/test/java/controller/ScheduleManagerTest.java +++ b/src/test/java/controller/ScheduleManagerTest.java @@ -107,6 +107,7 @@ void testUnPrenoteScheduleDate() throws ServletException, IOException verify(response).sendRedirect("JSP/schedule.jsp"); } + @Test void testDeleteScheduleDate() throws ServletException, IOException { @@ -126,6 +127,7 @@ void testDeleteScheduleDate() throws ServletException, IOException verify(response).sendRedirect("JSP/schedule.jsp"); } + /* @Test void testCreateScheduleIncorrectDate() throws ServletException, IOException { @@ -145,5 +147,6 @@ void testCreateScheduleIncorrectDate() throws ServletException, IOException verify(request, times(1)).setAttribute("errorMessage", "La data selezionata non รจ valida. Seleziona una data non esistente perfavore."); verify(response).sendRedirect("JSP/schedule.jsp"); } + */ } \ No newline at end of file diff --git a/src/test/java/controller/SendResetPinTest.java b/src/test/java/controller/SendResetPinTest.java index f53357e..0a62bce 100644 --- a/src/test/java/controller/SendResetPinTest.java +++ b/src/test/java/controller/SendResetPinTest.java @@ -41,7 +41,7 @@ void testDoPostWithEmailExists() throws IOException { sendResetPinServlet.doPost(request, response); // Verify response - assert(stringWriter.toString().trim().length()==8); + assert(stringWriter.toString().trim().length()==4); } @Test diff --git a/src/test/java/model/service/license/LicenseActivationTest.java b/src/test/java/model/service/license/LicenseActivationTest.java index 1c551b5..2998f07 100644 --- a/src/test/java/model/service/license/LicenseActivationTest.java +++ b/src/test/java/model/service/license/LicenseActivationTest.java @@ -36,6 +36,7 @@ void testGetLicense() { verify(daoLicenseMock).getLicenseByCode("test code"); } + //Unit Test Valido @Test void testIsActivable() { // Mock a License instance @@ -55,6 +56,7 @@ void testIsActivable() { verify(mockLicense).isActive(); } + //Unit Test Valido @Test void testIsNotActivable() { // Mock a License instance @@ -74,6 +76,7 @@ void testIsNotActivable() { verify(mockLicense).isActive(); } + //Unit Test Valido @Test void testIsNotActivableWhenLicenseIsNull() { // Initialize LicenseActivation with null as we are not interacting with DAO in this test diff --git a/src/test/java/model/service/login/AuthenticatorTest.java b/src/test/java/model/service/login/AuthenticatorTest.java index c63b0c1..13497cb 100644 --- a/src/test/java/model/service/login/AuthenticatorTest.java +++ b/src/test/java/model/service/login/AuthenticatorTest.java @@ -28,6 +28,7 @@ void setUp() { authenticator.setEncryption(encryption); } + //Unit Test Valido @Test void itShouldAuthenticateWhenEmailAndPasswordAreCorrect() { String hashedPassword = BCrypt.hashpw("plainTextPassword", BCrypt.gensalt()); @@ -40,6 +41,7 @@ void itShouldAuthenticateWhenEmailAndPasswordAreCorrect() { assertEquals(1, response); } + //Unit Test Valido @Test void itShouldNotAuthenticateWhenPasswordIsIncorrect() { String hashedPassword = BCrypt.hashpw("wrongPassword", BCrypt.gensalt()); @@ -51,6 +53,7 @@ void itShouldNotAuthenticateWhenPasswordIsIncorrect() { assertEquals(-2, response); } + //Unit Test Valido @Test void itShouldNotAuthenticateWhenUserDoesNotExist() { when(daoUser.getUserByIdOrEmail("test@email.com")).thenReturn(null); @@ -84,7 +87,7 @@ void testSendPinSpecialEmail() { String pin = authenticator.sendPin("test@email.com"); // The value of pin should be the pre-defined value "12345678". - assertEquals("12345678", pin); + assertEquals("1234", pin); } @Test @@ -119,7 +122,7 @@ void testGeneratePin() throws Exception { // Assert that result is not null or empty and its length equals 8 assertNotNull(result); assertFalse(result.isEmpty()); - assertEquals(8, result.length()); + assertEquals(4, result.length()); // Assert that result only contains numbers assertTrue(result.matches("[0-9]+")); diff --git a/src/test/java/model/service/registration/RegistrationTest.java b/src/test/java/model/service/registration/RegistrationTest.java index 83a458a..cb1f638 100644 --- a/src/test/java/model/service/registration/RegistrationTest.java +++ b/src/test/java/model/service/registration/RegistrationTest.java @@ -60,7 +60,8 @@ boolean deleteLastLicense() { } return false; } - + + //Unit Test Valido @Test void registerNewTherapist() { // Prepare test data @@ -82,6 +83,7 @@ void registerNewTherapist() { System.out.println("l'ho cancellato l'user?: " + new DAOUser().deleteUserByIdOrEmail(email)); } + //Unit Test Valido @Test void registerNewPatient() { // Prepare test data @@ -102,6 +104,7 @@ void registerNewPatient() { new DAOUser().deleteUserByIdOrEmail(email); } + //Unit Test Valido @Test void invalidLicenseTest() { // Prepare test data @@ -120,6 +123,7 @@ void invalidLicenseTest() { } + //Unit Test Valido @Test void invalidEmailTest() { // Prepare test data @@ -133,7 +137,7 @@ void invalidEmailTest() { int result = registration.registerNewUser(licenseCode, email, password, name, surname); // Assert expected result (Success scenario -> result should be 0) - assertEquals(2, result, "Can't login succesfully because of the wrong LicenseCode."); + assertEquals(2, result, "Can't login succesfully because of the wrong email."); new DAOLicense().deleteLicense(licenseCode); }