Skip to content

Commit

Permalink
servlet Registration 57% Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
panuozzo77 committed Jan 6, 2024
1 parent a3a2143 commit 53a4af8
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/controller/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/model/DAO/DAOCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Condition> getConditionsOfPatient(int id_patient) {

PreparedStatement preparedStatement = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.properties
Original file line number Diff line number Diff line change
@@ -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
101 changes: 101 additions & 0 deletions src/test/java/controller/RegistrationTest.java
Original file line number Diff line number Diff line change
@@ -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("[email protected]");
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("[email protected]").getId());
new DAOUser().deleteUserByIdOrEmail("[email protected]");
}

@Test
public void testWrongRegistration() throws IOException {
// Set up parameters for the request
when(request.getParameter("licenseCode")).thenReturn("123456");
when(request.getParameter("email")).thenReturn("[email protected]");
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("[email protected]");
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);
}

}
53 changes: 53 additions & 0 deletions src/test/java/model/DAO/DAOUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]"));
verify(preparedStatement, times(1)).executeQuery();
}
@Test
public void testCreateUser_SQLException() throws SQLException {
doThrow(SQLException.class).when(preparedStatement).executeUpdate();
assertEquals(-1, daoUser.createUser("[email protected]", "password", 456));
verify(preparedStatement, times(1)).executeUpdate();
}
@Test
public void testGetUserByIdOrEmail_SQLException() throws SQLException {
doThrow(SQLException.class).when(preparedStatement).executeQuery();
assertNull(daoUser.getUserByIdOrEmail("[email protected]"));
verify(preparedStatement, times(1)).executeQuery();
}
@Test
public void testResetPassword_SQLException() throws SQLException {
doThrow(SQLException.class).when(preparedStatement).executeUpdate();
assertFalse(daoUser.resetPassword("[email protected]", "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, "[email protected]", "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();
}

}

40 changes: 40 additions & 0 deletions src/test/java/model/entity/UserInfoTest.java
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]";
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 53a4af8

Please sign in to comment.