-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3a2143
commit 53a4af8
Showing
7 changed files
with
196 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters