diff --git a/src/main/java/controller/Registration.java b/src/main/java/controller/Registration.java index 71454a9..2f0baeb 100644 --- a/src/main/java/controller/Registration.java +++ b/src/main/java/controller/Registration.java @@ -29,7 +29,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) response.setCharacterEncoding("UTF-8"); response.getWriter().write(String.valueOf(result)); if(result == 0) { - setSessionAttributes(email, request); + git s(email, request); } } diff --git a/src/main/java/model/DAO/DAOCondition.java b/src/main/java/model/DAO/DAOCondition.java index 83cb60a..8a790b3 100644 --- a/src/main/java/model/DAO/DAOCondition.java +++ b/src/main/java/model/DAO/DAOCondition.java @@ -44,16 +44,6 @@ private Condition getPeronalConditionFromResultSet(ResultSet resultSet) throws S return c; } - private PatientCondition getPatientConditionFromResultSet(ResultSet resultSet) throws SQLException { - PatientCondition pc = new PatientCondition(); - - pc.setIdCondition(resultSet.getInt("ID_condition")); - pc.setIdPatient(resultSet.getInt("ID_patient")); - pc.setSeverity(resultSet.getInt("Severity")); - - return pc; - } - public ArrayList getConditionsOfPatient(int id_patient) { PreparedStatement preparedStatement = null; diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties index d23b563..a84ca9d 100644 --- a/src/main/resources/config.properties +++ b/src/main/resources/config.properties @@ -1,3 +1,3 @@ -db.url=jdbc:mysql://81.56.127.184:16400/TalkAID2 +db.url=jdbc:mysql://81.56.127.184:16400/TalkAID db.username=dbauthority db.password=password \ No newline at end of file diff --git a/src/test/java/controller/RegistrationTest.java b/src/test/java/controller/RegistrationTest.java new file mode 100644 index 0000000..3338331 --- /dev/null +++ b/src/test/java/controller/RegistrationTest.java @@ -0,0 +1,101 @@ +package controller; + +import model.DAO.DAOLicense; +import model.DAO.DAOPersonalInfo; +import model.DAO.DAOUser; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RegistrationTest { + + private Registration registrationServlet; + private HttpServletRequest request; + private HttpServletResponse response; + private StringWriter stringWriter; + private PrintWriter writer; + + @BeforeEach + public void setUp() throws IOException { + registrationServlet = new Registration(); + request = Mockito.mock(HttpServletRequest.class); + response = Mockito.mock(HttpServletResponse.class); + stringWriter = new StringWriter(); + writer = new PrintWriter(stringWriter); + + when(response.getWriter()).thenReturn(writer); + } + + @Test + public void testCorrectlyRegistered() throws IOException { + // Set up parameters for the request + String licenseCode = new DAOLicense().generateLicense(); + when(request.getParameter("licenseCode")).thenReturn(licenseCode); + when(request.getParameter("email")).thenReturn("test@test.com"); + when(request.getParameter("password")).thenReturn("password"); + when(request.getParameter("name")).thenReturn("Testo"); + when(request.getParameter("surname")).thenReturn("Tutto"); + HttpSession mockSession = mock(HttpSession.class); + when(request.getSession()).thenReturn(mockSession); + // Call the method under test + registrationServlet.doPost(request, response); + + // Verify the response + assertEquals(stringWriter.toString(), "0"); + + //pulizia + new DAOLicense().deleteLicense(licenseCode); + new DAOPersonalInfo().deleteRegistry(new DAOUser().getUserByIdOrEmail("test@test.com").getId()); + new DAOUser().deleteUserByIdOrEmail("test@test.com"); + } + + @Test + public void testWrongRegistration() throws IOException { + // Set up parameters for the request + when(request.getParameter("licenseCode")).thenReturn("123456"); + when(request.getParameter("email")).thenReturn("test@test.com"); + when(request.getParameter("password")).thenReturn("password"); + when(request.getParameter("name")).thenReturn("Testo"); + when(request.getParameter("surname")).thenReturn("Tutto"); + HttpSession mockSession = mock(HttpSession.class); + when(request.getSession()).thenReturn(mockSession); + // Call the method under test + registrationServlet.doPost(request, response); + + // Verify the response + assertEquals(stringWriter.toString(), "1"); + + } + + @Test + public void testAlreadyTakenEmail() throws IOException { + // Set up parameters for the request + String licenseCode = new DAOLicense().generateLicense(); + when(request.getParameter("licenseCode")).thenReturn(licenseCode); + when(request.getParameter("email")).thenReturn("doc1@example.com"); + when(request.getParameter("password")).thenReturn("password"); + when(request.getParameter("name")).thenReturn("Testo"); + when(request.getParameter("surname")).thenReturn("Tutto"); + HttpSession mockSession = mock(HttpSession.class); + when(request.getSession()).thenReturn(mockSession); + // Call the method under test + registrationServlet.doPost(request, response); + + // Verify the response + assertEquals(stringWriter.toString(), "2"); + new DAOLicense().deleteLicense(licenseCode); + } + +} \ No newline at end of file diff --git a/src/test/java/model/DAO/DAOUserTest.java b/src/test/java/model/DAO/DAOUserTest.java index 6129eba..083cb5f 100644 --- a/src/test/java/model/DAO/DAOUserTest.java +++ b/src/test/java/model/DAO/DAOUserTest.java @@ -142,5 +142,58 @@ public void testDeleteUserByIdOrEmail() throws SQLException { assertTrue(daoUser.deleteUserByIdOrEmail("1")); verify(preparedStatement, times(1)).executeUpdate(); } + + @Test + public void testCheckIfEmailExists_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeQuery(); + assertFalse(daoUser.checkIfEmailExists("email@test.com")); + verify(preparedStatement, times(1)).executeQuery(); + } + @Test + public void testCreateUser_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeUpdate(); + assertEquals(-1, daoUser.createUser("email@test.com", "password", 456)); + verify(preparedStatement, times(1)).executeUpdate(); + } + @Test + public void testGetUserByIdOrEmail_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeQuery(); + assertNull(daoUser.getUserByIdOrEmail("email@test.com")); + verify(preparedStatement, times(1)).executeQuery(); + } + @Test + public void testResetPassword_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeUpdate(); + assertFalse(daoUser.resetPassword("email@test.com", "newpassword")); + verify(preparedStatement, times(1)).executeUpdate(); + } + + @Test + public void testUpdateUser_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeUpdate(); + assertEquals("Update not possible due to a server connection issue.", daoUser.updateUser(1, "test@example.com", "Test Address")); + verify(preparedStatement, times(1)).executeUpdate(); + } + + @Test + public void testUpdateAnalyticsPreference_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeUpdate(); + assertFalse(daoUser.updateAnalyticsPreference("1", true)); + verify(preparedStatement, times(1)).executeUpdate(); + } + + @Test + public void testUpdateEmailTime_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeUpdate(); + assertFalse(daoUser.updateEmailTime("1", "10:00")); + verify(preparedStatement, times(1)).executeUpdate(); + } + @Test + public void testDeleteUserByIdOrEmail_SQLException() throws SQLException { + doThrow(SQLException.class).when(preparedStatement).executeUpdate(); + assertFalse(daoUser.deleteUserByIdOrEmail("1")); + verify(preparedStatement, times(1)).executeUpdate(); + } + } diff --git a/src/test/java/model/entity/UserInfoTest.java b/src/test/java/model/entity/UserInfoTest.java new file mode 100644 index 0000000..1910c3a --- /dev/null +++ b/src/test/java/model/entity/UserInfoTest.java @@ -0,0 +1,40 @@ +package model.entity; + +import org.junit.jupiter.api.Test; +import java.sql.Timestamp; +import java.sql.Date; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class UserInfoTest { + + @Test + public void testUserInfoConstructorAndGetters() { + // Define some sample data + int id = 1; + String email = "test@example.com"; + Timestamp activationDate = new Timestamp(System.currentTimeMillis()); + String firstname = "John"; + String lastname = "Doe"; + Date dateOfBirth = new Date(System.currentTimeMillis()); + String gender = "Male"; + String address = "123 Main St"; + String ssn = "123-45-6789"; + String phone = "123-456-7890"; + + // Create a new UserInfo object + UserInfo userInfo = new UserInfo(id, email, activationDate, firstname, lastname, dateOfBirth, gender, address, ssn, phone); + + // Use the getter methods to retrieve the data and check that it matches the sample data + assertEquals(id, userInfo.getId()); + assertEquals(email, userInfo.getEmail()); + assertEquals(activationDate, userInfo.getActivationDate()); + assertEquals(firstname, userInfo.getFirstname()); + assertEquals(lastname, userInfo.getLastname()); + assertEquals(dateOfBirth, userInfo.getDateOfBirth()); + assertEquals(gender, userInfo.getGender()); + assertEquals(address, userInfo.getAddress()); + assertEquals(ssn, userInfo.getSsn()); + assertEquals(phone, userInfo.getPhone()); + } +} \ No newline at end of file diff --git a/src/test/java/model/service/registration/RegistrationTest.java b/src/test/java/model/service/registration/RegistrationTest.java index 264c3d9..83a458a 100644 --- a/src/test/java/model/service/registration/RegistrationTest.java +++ b/src/test/java/model/service/registration/RegistrationTest.java @@ -97,7 +97,6 @@ void registerNewPatient() { // Assert expected result (Success scenario -> result should be 0) assertEquals(0, result, "New user should be registered successfully."); - new DAOLicense().deleteLicense(licenseCode); new DAOPersonalInfo().deleteRegistry(new DAOUser().getUserByIdOrEmail(email).getId()); new DAOUser().deleteUserByIdOrEmail(email);