Skip to content

Commit

Permalink
Merge pull request #47 from pastore99/34-testing-unit-testing
Browse files Browse the repository at this point in the history
34 testing unit testing
  • Loading branch information
panuozzo77 authored Jan 7, 2024
2 parents 0fc5318 + 7d76d72 commit 1caa9fd
Show file tree
Hide file tree
Showing 26 changed files with 666 additions and 107 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>com.mockrunner</groupId>
<artifactId>mockrunner-servlet</artifactId>
Expand All @@ -125,6 +119,12 @@
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import model.service.schedule.ScheduleManager;
import model.service.email.EmailManager;
import model.service.encryption.Encryption;
import model.DAO.DAOUser;
Expand All @@ -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("[email protected]", "pwd"));
System.out.println(auth.authenticate("[email protected]", "pwd"));
r.invitePatient(9, "[email protected]", "Raffaele", "Monti");
//System.out.println(auth.authenticate("[email protected]", "pwd"));
//System.out.println(auth.authenticate("[email protected]", "pwd"));
//r.invitePatient(9, "[email protected]", "Raffaele", "Monti");

ScheduleManager manager = new ScheduleManager();
System.out.println(manager.checkData(9, "1999-10-08", "10:00-11:00"));
}
}
1 change: 0 additions & 1 deletion src/main/java/controller/AddRemovePatientCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/controller/ChangeUserInfo.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/controller/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/controller/ScheduleManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
Expand All @@ -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")) {
Expand Down Expand Up @@ -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);
}
}
}
12 changes: 0 additions & 12 deletions src/main/java/model/DAO/DAOCondition.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Condition> getConditionsOfPatient(int id_patient) {

PreparedStatement preparedStatement = null;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/model/DAO/DAOLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/model/DAO/DAOMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
1 change: 0 additions & 1 deletion src/main/java/model/DAO/DAOPersonalInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model.DAO;

import model.entity.PersonalInfo;
import model.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/model/DAO/DAOSchedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 = ?;";
Expand All @@ -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;
}

}
37 changes: 1 addition & 36 deletions src/main/java/model/DAO/DAOUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ public boolean deleteUserByIdOrEmail(Object idOrEmail) {

return rowsDeleted > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
Expand All @@ -434,42 +435,6 @@ public boolean deleteUserByIdOrEmail(Object idOrEmail) {
return false;
}

public ArrayList<User> getUsersByIdTherapist(int id) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ArrayList<User> 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<UserInfo> getUsersAndPersonalInfoByIdTherapist(int idTherapist) {
Connection connection = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import model.DAO.DAOCondition;

import java.util.ArrayList;
import java.util.concurrent.locks.Condition;

public class ConditionManager implements ConditionInterface {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/model/service/license/LicenseActivation.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/model/service/message/MessageManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/model/service/registration/Registration.java
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/model/service/schedule/ScheduleManager.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
}
}
8 changes: 0 additions & 8 deletions src/main/java/model/service/user/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<User> getUsersByIdTherapist(int ID_Therapist ){return db.getUsersByIdTherapist(ID_Therapist);}
public ArrayList<UserInfo> getUsersAndPersonalInfoByIdTherapist(int ID_Therapist) {return db.getUsersAndPersonalInfoByIdTherapist(ID_Therapist);}
}
Loading

0 comments on commit 1caa9fd

Please sign in to comment.