diff --git a/pom.xml b/pom.xml
index 4303d24..5d46c94 100644
--- a/pom.xml
+++ b/pom.xml
@@ -96,12 +96,6 @@
test
-
- com.google.code.gson
- gson
- 2.10.1
-
-
com.mockrunner
mockrunner-servlet
@@ -125,6 +119,12 @@
javax.servlet-api
3.1.0
+
+ org.mockito
+ mockito-junit-jupiter
+ 5.3.1
+ test
+
diff --git a/src/main/java/Main.java b/src/main/java/Main.java
index 382cdfe..112de1a 100644
--- a/src/main/java/Main.java
+++ b/src/main/java/Main.java
@@ -1,3 +1,4 @@
+import model.service.schedule.ScheduleManager;
import model.service.email.EmailManager;
import model.service.encryption.Encryption;
import model.DAO.DAOUser;
@@ -8,11 +9,14 @@
public class Main {
public static void main(String[] args) {
- Authenticator auth = new Authenticator();
- Registration r = new Registration();
+ //Authenticator auth = new Authenticator();
+ //Registration r = new Registration();
- System.out.println(auth.authenticate("patient1@example.com", "pwd"));
- System.out.println(auth.authenticate("patient1@example.com", "pwd"));
- r.invitePatient(9, "thenicrodeath@gmail.com", "Raffaele", "Monti");
+ //System.out.println(auth.authenticate("patient1@example.com", "pwd"));
+ //System.out.println(auth.authenticate("patient1@example.com", "pwd"));
+ //r.invitePatient(9, "thenicrodeath@gmail.com", "Raffaele", "Monti");
+
+ ScheduleManager manager = new ScheduleManager();
+ System.out.println(manager.checkData(9, "1999-10-08", "10:00-11:00"));
}
}
diff --git a/src/main/java/controller/AddRemovePatientCondition.java b/src/main/java/controller/AddRemovePatientCondition.java
index 0bdf98b..8e61ed4 100644
--- a/src/main/java/controller/AddRemovePatientCondition.java
+++ b/src/main/java/controller/AddRemovePatientCondition.java
@@ -2,7 +2,6 @@
import model.service.condition.ConditionManager;
-import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
diff --git a/src/main/java/controller/ChangeUserInfo.java b/src/main/java/controller/ChangeUserInfo.java
index 948b0ea..e23399f 100644
--- a/src/main/java/controller/ChangeUserInfo.java
+++ b/src/main/java/controller/ChangeUserInfo.java
@@ -1,6 +1,7 @@
package controller;
import model.service.user.UserRegistry;
+
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
diff --git a/src/main/java/controller/Login.java b/src/main/java/controller/Login.java
index c0cc6ad..8a199ad 100644
--- a/src/main/java/controller/Login.java
+++ b/src/main/java/controller/Login.java
@@ -2,7 +2,6 @@
import model.entity.PersonalInfo;
import model.entity.User;
-import model.entity.UserInfo;
import model.service.login.Authenticator;
import model.service.user.UserData;
import model.service.user.UserRegistry;
@@ -13,7 +12,6 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
-import java.util.ArrayList;
@WebServlet("/login")
public class Login extends HttpServlet {
diff --git a/src/main/java/controller/ScheduleManager.java b/src/main/java/controller/ScheduleManager.java
index fbe6a93..668e225 100644
--- a/src/main/java/controller/ScheduleManager.java
+++ b/src/main/java/controller/ScheduleManager.java
@@ -1,6 +1,5 @@
package controller;
-import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -9,10 +8,6 @@
@WebServlet("/ScheduleServlet")
public class ScheduleManager extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
- doPost(request, response);
- }
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
int userId = (Integer) request.getSession().getAttribute("id");
response.getWriter().append("Served at: ").append(request.getContextPath());
@@ -30,7 +25,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
} else {
String errorMessage = "La data selezionata non è valida. Seleziona una data non esistente perfavore.";
request.setAttribute("errorMessage", errorMessage);
- request.getRequestDispatcher("JSP/schedule.jsp").forward(request, response);
+ //request.getRequestDispatcher("JSP/schedule.jsp").forward(request, response);
+ response.sendRedirect("JSP/schedule.jsp");
}
}
else if (action.equalsIgnoreCase("deleteSchedule")) {
@@ -59,8 +55,6 @@ else if (action.equalsIgnoreCase("prenoteSchedule")) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
- } catch (ServletException e) {
- throw new RuntimeException(e);
}
}
}
\ No newline at end of file
diff --git a/src/main/java/model/DAO/DAOCondition.java b/src/main/java/model/DAO/DAOCondition.java
index 83cb60a..e1c569f 100644
--- a/src/main/java/model/DAO/DAOCondition.java
+++ b/src/main/java/model/DAO/DAOCondition.java
@@ -1,8 +1,6 @@
package model.DAO;
import model.entity.Condition;
-import model.entity.PatientCondition;
-import model.entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -44,16 +42,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/java/model/DAO/DAOLicense.java b/src/main/java/model/DAO/DAOLicense.java
index 53fd1a3..434e232 100644
--- a/src/main/java/model/DAO/DAOLicense.java
+++ b/src/main/java/model/DAO/DAOLicense.java
@@ -84,7 +84,7 @@ public License getLicenseByCode(String code) {
* @param userId the ID of the user to link to the license.
*/
public void activate(License license, int userId) {
- String updateQuery = "UPDATE TalkAID2.license SET active = TRUE, ID_User = ? WHERE Sequence = ?";
+ String updateQuery = "UPDATE license SET active = TRUE, ID_User = ? WHERE Sequence = ?";
PreparedStatement preparedStatement = null;
try {
@@ -114,7 +114,7 @@ public void activate(License license, int userId) {
public String generateLicense(){
final int length = 8;
License l = new License(length);
- String insertQuery = "INSERT INTO TalkAID2.license (Sequence, ID_User, ExpirationDate) VALUES (?, ?, ?);";
+ String insertQuery = "INSERT INTO license (Sequence, ID_User, ExpirationDate) VALUES (?, ?, ?);";
PreparedStatement preparedStatement = null;
try {
@@ -148,7 +148,7 @@ public String generateLicense(){
public String generateInvitation(int userId){
final int length = 4;
License l = new License(length);
- String insertQuery = "INSERT INTO TalkAID2.license (Sequence, ID_User, ExpirationDate) VALUES (?,?,?);";
+ String insertQuery = "INSERT INTO license (Sequence, ID_User, ExpirationDate) VALUES (?,?,?);";
PreparedStatement preparedStatement = null;
try {
@@ -181,7 +181,7 @@ public boolean deleteLicense(String code) {
connection = connection.isClosed() ? DAOConnection.getConnection() : connection;
// Prepare the SQL query
- String query = "DELETE FROM TalkAID2.license WHERE Sequence = ?";
+ String query = "DELETE FROM license WHERE Sequence = ?";
preparedStatement = connection.prepareStatement(query);
// Set parameter for the prepared statement
diff --git a/src/main/java/model/DAO/DAOMessage.java b/src/main/java/model/DAO/DAOMessage.java
index 175c8ae..6804580 100644
--- a/src/main/java/model/DAO/DAOMessage.java
+++ b/src/main/java/model/DAO/DAOMessage.java
@@ -3,7 +3,8 @@
import model.entity.Message;
import java.sql.*;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
public class DAOMessage {
diff --git a/src/main/java/model/DAO/DAOPersonalInfo.java b/src/main/java/model/DAO/DAOPersonalInfo.java
index 23c2e2e..1ff703b 100644
--- a/src/main/java/model/DAO/DAOPersonalInfo.java
+++ b/src/main/java/model/DAO/DAOPersonalInfo.java
@@ -1,7 +1,6 @@
package model.DAO;
import model.entity.PersonalInfo;
-import model.entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
diff --git a/src/main/java/model/DAO/DAOSchedule.java b/src/main/java/model/DAO/DAOSchedule.java
index 5a9dcfe..b0a3d48 100644
--- a/src/main/java/model/DAO/DAOSchedule.java
+++ b/src/main/java/model/DAO/DAOSchedule.java
@@ -2,12 +2,10 @@
import model.entity.Schedule;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.*;
-import java.sql.Date;
+import java.sql.*;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
public class DAOSchedule {
@@ -286,12 +284,20 @@ public int retrieveAllPrenotedSchedulesCount(int idTherapist) {
return count;
}
- public int checkData(int idTherapist, Date date, String timeSlot) {
+ //Restituisce TRUE se la data è disponibile. Restituisce FALSE se la data NON è disponibile O non è valida.
+ public boolean checkData(int idTherapist, Date date, String timeSlot) {
PreparedStatement pstmt = null;
ResultSet rs = null;
int count = 0;
try {
+ // Comparing the provided date with the current date
+ LocalDate localDate = LocalDate.now();
+ Date currentDate = java.sql.Date.valueOf(localDate);
+ if(currentDate.after(date)){
+ return false;
+ }
+
connection = connection.isClosed() ? DAOConnection.getConnection() : connection;
String sql = "SELECT COUNT(*) FROM schedule WHERE ID_therapist = ? AND Date = ? AND TimeSlot = ?;";
@@ -316,7 +322,9 @@ public int checkData(int idTherapist, Date date, String timeSlot) {
e.printStackTrace();
}
}
- return count;
+ // Here we change the condition. It will return true if count > 0, meaning the data is available.
+ // It returns false otherwise.
+ return count > 0;
}
}
diff --git a/src/main/java/model/DAO/DAOUser.java b/src/main/java/model/DAO/DAOUser.java
index f3d9251..f13b8af 100644
--- a/src/main/java/model/DAO/DAOUser.java
+++ b/src/main/java/model/DAO/DAOUser.java
@@ -420,6 +420,7 @@ public boolean deleteUserByIdOrEmail(Object idOrEmail) {
return rowsDeleted > 0;
} catch (SQLException e) {
+ e.printStackTrace();
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
@@ -434,42 +435,6 @@ public boolean deleteUserByIdOrEmail(Object idOrEmail) {
return false;
}
- public ArrayList getUsersByIdTherapist(int id) {
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- ArrayList list_user=new ArrayList<>();
- try {
-
- connection = DAOConnection.getConnection();
- String query = null;
-
- query = "SELECT * FROM user WHERE ID_Therapist = ?";
-
- preparedStatement = connection.prepareStatement(query);
- preparedStatement.setObject(1, id);
-
- resultSet = preparedStatement.executeQuery();
-
- while (resultSet.next()) {
- list_user.add(getUserFromResultSet(resultSet));
- }
- return list_user;
- } catch (SQLException e) {
- // Handle the exception (e.g., log or throw)
- e.printStackTrace();
- } finally {
- try {
- if (resultSet != null) resultSet.close();
- if (preparedStatement != null) preparedStatement.close();
- DAOConnection.releaseConnection(connection);
- } catch (SQLException e) {
- // Handle the exception (e.g., log or throw)
- e.printStackTrace();
- }
- }
- return null; // or you may throw an exception here
- }
public ArrayList getUsersAndPersonalInfoByIdTherapist(int idTherapist) {
Connection connection = null;
diff --git a/src/main/java/model/service/condition/ConditionManager.java b/src/main/java/model/service/condition/ConditionManager.java
index 55ddd56..aeaf5ca 100644
--- a/src/main/java/model/service/condition/ConditionManager.java
+++ b/src/main/java/model/service/condition/ConditionManager.java
@@ -3,7 +3,6 @@
import model.DAO.DAOCondition;
import java.util.ArrayList;
-import java.util.concurrent.locks.Condition;
public class ConditionManager implements ConditionInterface {
diff --git a/src/main/java/model/service/license/LicenseActivation.java b/src/main/java/model/service/license/LicenseActivation.java
index 9974095..4aa17b4 100644
--- a/src/main/java/model/service/license/LicenseActivation.java
+++ b/src/main/java/model/service/license/LicenseActivation.java
@@ -1,7 +1,7 @@
package model.service.license;
-import model.entity.License;
import model.DAO.DAOLicense;
+import model.entity.License;
public class LicenseActivation implements LicenseActivationInterface {
DAOLicense daoLicense;
diff --git a/src/main/java/model/service/message/MessageManager.java b/src/main/java/model/service/message/MessageManager.java
index 2ed1c6b..49bf8c7 100644
--- a/src/main/java/model/service/message/MessageManager.java
+++ b/src/main/java/model/service/message/MessageManager.java
@@ -1,17 +1,12 @@
package model.service.message;
import model.DAO.DAOMessage;
-import model.DAO.DAOSchedule;
-import model.DAO.DAOUser;
import model.entity.Message;
import model.entity.User;
import model.service.user.UserData;
-import java.sql.Connection;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
public class MessageManager implements MessageManagerInterface {
private int userId;
diff --git a/src/main/java/model/service/registration/Registration.java b/src/main/java/model/service/registration/Registration.java
index 4071bed..9b8dd41 100644
--- a/src/main/java/model/service/registration/Registration.java
+++ b/src/main/java/model/service/registration/Registration.java
@@ -1,14 +1,15 @@
package model.service.registration;
-import model.service.email.EmailManager;
+
+import model.entity.License;
import model.service.email.EmailManager;
import model.service.encryption.Encryption;
import model.service.license.LicenseActivation;
-import model.entity.License;
import model.service.user.UserData;
import model.service.user.UserRegistry;
public class Registration implements RegistrationInterface {
+
@Override
public int registerNewUser(String licenseCode, String email, String password, String name, String surname) {
License license = validateLicense(licenseCode);
diff --git a/src/main/java/model/service/schedule/ScheduleManager.java b/src/main/java/model/service/schedule/ScheduleManager.java
index c1a18f4..3b1ace9 100644
--- a/src/main/java/model/service/schedule/ScheduleManager.java
+++ b/src/main/java/model/service/schedule/ScheduleManager.java
@@ -1,12 +1,9 @@
package model.service.schedule;
import model.DAO.DAOSchedule;
-import model.DAO.DAOUser;
import model.entity.Schedule;
-import java.sql.Connection;
import java.sql.Date;
-
import java.util.List;
public class ScheduleManager implements ScheduleManagerInterface {
@@ -53,6 +50,6 @@ public int retrieveAllPrenotedSchedulesCount(int idTherapist){
}
public boolean checkData(int idTherapist, String date, String timeSlot) {
- return db.checkData(idTherapist, convStoD(date), timeSlot) == 0;
+ return db.checkData(idTherapist, convStoD(date), timeSlot);
}
}
diff --git a/src/main/java/model/service/user/UserData.java b/src/main/java/model/service/user/UserData.java
index 8ac170a..5cd222a 100644
--- a/src/main/java/model/service/user/UserData.java
+++ b/src/main/java/model/service/user/UserData.java
@@ -5,9 +5,6 @@
import model.entity.UserInfo;
import java.util.ArrayList;
-import model.entity.User;
-
-import java.sql.Connection;
public class UserData implements UserDataInterface {
DAOUser db;
@@ -42,10 +39,5 @@ public boolean updateEmailTime(String id, String value) {
return db.updateEmailTime(id, value);
}
- public String updateUser(int idUser, String Email, String address)
- {
- return db.updateUser(idUser, Email, address);
- }
- public ArrayList getUsersByIdTherapist(int ID_Therapist ){return db.getUsersByIdTherapist(ID_Therapist);}
public ArrayList getUsersAndPersonalInfoByIdTherapist(int ID_Therapist) {return db.getUsersAndPersonalInfoByIdTherapist(ID_Therapist);}
}
diff --git a/src/main/java/model/service/user/UserRegistry.java b/src/main/java/model/service/user/UserRegistry.java
index c66d8ab..173d526 100644
--- a/src/main/java/model/service/user/UserRegistry.java
+++ b/src/main/java/model/service/user/UserRegistry.java
@@ -1,7 +1,6 @@
package model.service.user;
import model.DAO.DAOPersonalInfo;
-import model.DAO.DAOUser;
import model.entity.PersonalInfo;
import java.sql.Connection;
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/controller/ScheduleManagerTest.java b/src/test/java/controller/ScheduleManagerTest.java
new file mode 100644
index 0000000..fec9785
--- /dev/null
+++ b/src/test/java/controller/ScheduleManagerTest.java
@@ -0,0 +1,149 @@
+package controller;
+
+import model.service.login.Authenticator;
+import model.service.user.UserData;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import javax.servlet.ServletException;
+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.Mockito.*;
+
+public class ScheduleManagerTest
+{
+
+ @InjectMocks
+ private ScheduleManager servlet;
+
+ @Mock
+ private HttpServletRequest request;
+
+ @Mock
+ private HttpServletResponse response;
+
+ @Mock
+ private HttpSession session = mock(HttpSession.class);
+
+ @Mock
+ private UserData userData;
+
+ @Mock
+ private Authenticator authenticator;
+
+ @BeforeEach
+ public void setUp() throws Exception {
+ MockitoAnnotations.openMocks(this);
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ void testCreateScheduleCorrectDate() throws ServletException, IOException
+ {
+
+ when(request.getSession()).thenReturn(session);
+ when(session.getAttribute("id")).thenReturn(9);
+ when(request.getParameter("action")).thenReturn("createNewSchedule");
+ when(request.getParameter("date")).thenReturn("2024-02-08");
+ when(request.getParameter("timeslot")).thenReturn("10:00-11:00");
+
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ when(response.getWriter()).thenReturn(writer);
+
+ servlet.doPost(request,response);
+
+ verify(response).sendRedirect("JSP/schedule.jsp");
+ }
+
+ @Test
+ void testPrenoteScheduleDate() throws ServletException, IOException
+ {
+
+ when(request.getSession()).thenReturn(session);
+ when(session.getAttribute("id")).thenReturn(9);
+ when(request.getParameter("action")).thenReturn("prenoteSchedule");
+ when(request.getParameter("idTherapist")).thenReturn("9");
+ when(request.getParameter("date")).thenReturn("2024-02-08");
+ when(request.getParameter("timeslot")).thenReturn("10:00-11:00");
+
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ when(response.getWriter()).thenReturn(writer);
+
+ servlet.doPost(request,response);
+
+ verify(response).sendRedirect("JSP/schedule.jsp");
+ }
+
+ @Test
+ void testUnPrenoteScheduleDate() throws ServletException, IOException
+ {
+
+ when(request.getSession()).thenReturn(session);
+ when(session.getAttribute("id")).thenReturn(9);
+ when(request.getParameter("password")).thenReturn("pwd");
+ when(request.getParameter("action")).thenReturn("unprenoteSchedule");
+ when(request.getParameter("idTherapist")).thenReturn("9");
+ when(request.getParameter("date")).thenReturn("2024-02-08");
+ when(request.getParameter("timeslot")).thenReturn("10:00-11:00");
+
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ when(response.getWriter()).thenReturn(writer);
+
+ servlet.doPost(request,response);
+
+ verify(response).sendRedirect("JSP/schedule.jsp");
+ }
+
+ void testDeleteScheduleDate() throws ServletException, IOException
+ {
+
+ when(request.getSession()).thenReturn(session);
+ when(session.getAttribute("id")).thenReturn(9);
+ when(request.getParameter("password")).thenReturn("pwd");
+ when(request.getParameter("action")).thenReturn("deleteSchedule");
+ when(request.getParameter("date")).thenReturn("2024-02-08");
+ when(request.getParameter("timeslot")).thenReturn("10:00-11:00");
+
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ when(response.getWriter()).thenReturn(writer);
+
+ servlet.doPost(request,response);
+
+ verify(response).sendRedirect("JSP/schedule.jsp");
+ }
+
+ @Test
+ void testCreateScheduleIncorrectDate() throws ServletException, IOException
+ {
+
+ when(request.getSession()).thenReturn(session);
+ when(session.getAttribute("id")).thenReturn(9);
+ when(request.getParameter("action")).thenReturn("createNewSchedule");
+ when(request.getParameter("date")).thenReturn("1999-10-08");
+ when(request.getParameter("timeslot")).thenReturn("10:00-11:00");
+
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ when(response.getWriter()).thenReturn(writer);
+
+ servlet.doPost(request,response);
+
+ 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/model/DAO/DAOConditionTest.java b/src/test/java/model/DAO/DAOConditionTest.java
new file mode 100644
index 0000000..595a37c
--- /dev/null
+++ b/src/test/java/model/DAO/DAOConditionTest.java
@@ -0,0 +1,120 @@
+package model.DAO;
+
+import org.junit.jupiter.api.*;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import static org.mockito.BDDMockito.*;
+import static org.junit.jupiter.api.Assertions.*;
+import java.sql.*;
+import java.util.ArrayList;
+import model.entity.Condition;
+
+
+@ExtendWith(MockitoExtension.class)
+public class DAOConditionTest {
+
+ @Mock
+ private Connection connection;
+
+ @Mock
+ private PreparedStatement preparedStatement;
+
+ @Mock
+ private ResultSet resultSet;
+
+ private DAOCondition daoCondition;
+
+ @BeforeEach
+ void setUp() throws SQLException {
+ daoCondition = new DAOCondition(connection);
+
+ when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
+ }
+
+ @Test
+ public void testGetConditionsOfPatient() throws SQLException {
+ int idPatient = 1;
+
+ when(connection.isClosed()).thenReturn(false);
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, false); // simulate returning one row
+ when(resultSet.getInt("ID_condition")).thenReturn(1);
+ when(resultSet.getString("DisorderDescription")).thenReturn("test description");
+ when(resultSet.getString("DisorderName")).thenReturn("test disorder");
+ when(resultSet.getInt("Severity")).thenReturn(1);
+
+ ArrayList result = daoCondition.getConditionsOfPatient(idPatient);
+
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ assertEquals("test disorder", result.get(0).getDisorderName());
+ }
+
+ @Test
+ public void testAddConditionPatient() throws SQLException {
+ int conditionID = 1;
+ int patientID = 1;
+ int severity = 1;
+
+ when(connection.isClosed()).thenReturn(false);
+ when(preparedStatement.executeUpdate()).thenReturn(1);
+
+
+ boolean result = daoCondition.AddConditionPatient(conditionID, patientID, severity);
+
+ assertTrue(result);
+ verify(connection, times(1)).prepareStatement("INSERT INTO PatientCondition (ID_condition, ID_patient, Severity)\n" +
+ "VALUES (?, ?, ?)");
+ verify(preparedStatement, times(1)).setInt(1, conditionID);
+ verify(preparedStatement, times(1)).setInt(2, patientID);
+ verify(preparedStatement, times(1)).setInt(3, severity);
+ verify(preparedStatement, times(1)).executeUpdate();
+ verify(connection, times(1)).commit();
+ }
+
+ @Test
+ public void testRemoveConditionPatient() throws SQLException {
+ int conditionID = 1;
+ int patientID = 1;
+
+ when(connection.isClosed()).thenReturn(false);
+ when(preparedStatement.executeUpdate()).thenReturn(1);
+
+ boolean result = daoCondition.RemoveConditionPatient(conditionID, patientID);
+
+ assertTrue(result);
+ verify(connection, times(1)).prepareStatement("DELETE FROM PatientCondition\n" +
+ "WHERE ID_condition = ? AND ID_patient = ?;");
+ verify(preparedStatement, times(1)).setInt(1, conditionID);
+ verify(preparedStatement, times(1)).setInt(2, patientID);
+ verify(preparedStatement, times(1)).executeUpdate();
+ verify(connection, times(1)).commit();
+ }
+
+ @Test
+ public void testGetConditionsNOTOfPatient() throws SQLException {
+ int idPatient = 1;
+
+ when(connection.isClosed()).thenReturn(false);
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, false); // simulates returning one row
+ when(resultSet.getInt("ID_condition")).thenReturn(1);
+ when(resultSet.getString("DisorderDescription")).thenReturn("test description");
+ when(resultSet.getString("DisorderName")).thenReturn("test disorder");
+
+ ArrayList result = daoCondition.getConditionsNOTOfPatient(idPatient);
+
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ assertEquals("test disorder", result.get(0).getDisorderName());
+ verify(connection, times(1)).prepareStatement("SELECT c.*\n" +
+ "FROM `condition` c\n" +
+ "LEFT JOIN PatientCondition pc ON c.ID_condition = pc.ID_condition AND pc.ID_patient = ?\n" +
+ "WHERE pc.ID_patient IS NULL\n" +
+ "ORDER BY c.DisorderName;");
+ verify(preparedStatement, times(1)).setObject(1, idPatient);
+ verify(preparedStatement, times(1)).executeQuery();
+ }
+
+}
\ 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 33b35ca..083cb5f 100644
--- a/src/test/java/model/DAO/DAOUserTest.java
+++ b/src/test/java/model/DAO/DAOUserTest.java
@@ -1,5 +1,7 @@
package model.DAO;
+import model.entity.User;
+import model.entity.UserInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
@@ -7,6 +9,7 @@
import org.mockito.MockitoAnnotations;
import java.sql.*;
+import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@@ -139,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
new file mode 100644
index 0000000..83a458a
--- /dev/null
+++ b/src/test/java/model/service/registration/RegistrationTest.java
@@ -0,0 +1,153 @@
+package model.service.registration;
+
+import model.DAO.DAOConnection;
+import model.DAO.DAOLicense;
+import model.DAO.DAOPersonalInfo;
+import model.DAO.DAOUser;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.*;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class RegistrationTest {
+
+ private Registration registration;
+
+ @BeforeEach
+ void setUp() {
+ registration = new Registration();
+ }
+
+ boolean deleteLastLicense() {
+ Statement statement = null;
+ ResultSet resultSet;
+ Connection connection = null;
+ try {
+ // Get database connection
+ connection = DAOConnection.getConnection();
+ connection.setAutoCommit(false); // start transaction
+
+ statement = connection.createStatement();
+ // Get the expiration date of the latest license
+ resultSet = statement.executeQuery("SELECT MAX(ExpirationDate) AS latest_exp_date FROM license");
+ if(resultSet.next()) {
+ Date latestDate = resultSet.getDate("latest_exp_date");
+ // Prepare DELETE statement with latest expiration date
+ int rowsAffected = statement.executeUpdate("DELETE FROM license WHERE ExpirationDate = '"+latestDate+"'");
+ connection.commit(); // end transaction
+ return rowsAffected > 0;
+ }
+ connection.rollback();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ try {
+ if (connection != null) {
+ connection.rollback();
+ }
+ } catch (SQLException exception) {
+ exception.printStackTrace();
+ }
+ } finally {
+ try {
+ if (statement != null) statement.close();
+ DAOConnection.releaseConnection(connection);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+ return false;
+ }
+
+ @Test
+ void registerNewTherapist() {
+ // Prepare test data
+ String licenseCode = new DAOLicense().generateLicense();
+ System.out.println(licenseCode);
+ String email = "test@test.com";
+ String password = "testPassword";
+ String name = "testName";
+ String surname = "testSurname";
+
+ // Run registerNewUser method
+ int result = registration.registerNewUser(licenseCode, email, password, name, surname);
+
+ // 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());
+ System.out.println("l'ho cancellato l'user?: " + new DAOUser().deleteUserByIdOrEmail(email));
+ }
+
+ @Test
+ void registerNewPatient() {
+ // Prepare test data
+ String licenseCode = new DAOLicense().generateInvitation(999);
+ System.out.println(licenseCode);
+ String email = "test2@test.com";
+ String password = "testPassword";
+ String name = "testName";
+ String surname = "testSurname";
+
+ // Run registerNewUser method
+ int result = registration.registerNewUser(licenseCode, email, password, name, surname);
+
+ // 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);
+ }
+
+ @Test
+ void invalidLicenseTest() {
+ // Prepare test data
+ String licenseCode = "notWorking";
+ String email = "test@test.com";
+ String password = "testPassword";
+ String name = "testName";
+ String surname = "testSurname";
+
+ // Run registerNewUser method
+ int result = registration.registerNewUser(licenseCode, email, password, name, surname);
+
+ // Assert expected result (Success scenario -> result should be 0)
+ assertEquals(1, result, "Can't login succesfully because of the wrong LicenseCode.");
+
+
+ }
+
+ @Test
+ void invalidEmailTest() {
+ // Prepare test data
+ String licenseCode = new DAOLicense().generateLicense();
+ String email = "doc1@example.com";
+ String password = "testPassword";
+ String name = "testName";
+ String surname = "testSurname";
+
+ // Run registerNewUser method
+ 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.");
+
+ new DAOLicense().deleteLicense(licenseCode);
+ }
+
+ @Test
+ void invitePatient() {
+ boolean result = registration.invitePatient(9, "test@test.com", "patientName", "patientSurname");
+ assertEquals(true, result, "Patient should be invited successfully.");
+ deleteLastLicense();
+ }
+
+ @Test
+ void dontInvitePatient() {
+ boolean result = registration.invitePatient(9, "doc1@example.com", "patientName", "patientSurname");
+ assertEquals(false, result, "Patient should NOT be invited successfully.");
+ }
+}
\ No newline at end of file