-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf[User]: Improve User validation and update corresponding JUnit tests
- Loading branch information
Fredrik Marjoni
committed
Feb 28, 2026
1 parent
1c15398
commit e4b38a5
Showing
1 changed file
with
92 additions
and
74 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,136 +1,154 @@ | ||
| package edu.group5.app.model.user; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
|
||
| public class UserTest { | ||
| private static final int TEST_USER_ID = 1; | ||
| private static final String TEST_ROLE = "Donor"; | ||
| private static final String TEST_FIRST_NAME = "John"; | ||
| private static final String TEST_LAST_NAME = "Doe"; | ||
| private static final String TEST_EMAIL = "john.doe@example.com"; | ||
| private static final String TEST_PASSWORD = "password123"; | ||
| private static final String TEST_PASSWORD_HASH = new BCryptPasswordEncoder().encode(TEST_PASSWORD); | ||
|
|
||
| private static final int WRONG_USER_ID = -5; | ||
| private static final String WRONG_ROLE = ""; | ||
| private static final String WRONG_FIRST_NAME = ""; | ||
| private static final String WRONG_LAST_NAME = ""; | ||
| private static final String WRONG_EMAIL = ""; | ||
| private static final String WRONG_PASSWORD_HASH = ""; | ||
| private int testUserId; | ||
| private String testRole; | ||
| private String testFirstName; | ||
| private String testLastName; | ||
| private String testEmail; | ||
| private String testPassword; | ||
| private String testPasswordHash; | ||
|
|
||
| private void constructorTest(int userId, String role, String firstName, String lastName, String email, String passwordHash, boolean negativeTest) { | ||
| boolean exceptionThrown = negativeTest; | ||
| try { | ||
| new User(userId, role, firstName, lastName, email, passwordHash); | ||
| } catch (Exception e) { | ||
| exceptionThrown = !negativeTest; | ||
| } finally { | ||
| assertFalse(exceptionThrown); | ||
| } | ||
| @BeforeEach | ||
| void setUp() { | ||
| testUserId = 1; | ||
| testRole = "Donor"; | ||
| testFirstName = "John"; | ||
| testLastName = "Doe"; | ||
| testEmail = "john.doe@example.com"; | ||
| testPassword = "password123"; | ||
| testPasswordHash = new BCryptPasswordEncoder().encode(testPassword); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorThrowsNoException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, false); | ||
| } | ||
| private void constructorTest(int userId, String role, String firstName, | ||
| String lastName, String email, String passwordHash, | ||
| String expectedMessage) { | ||
| IllegalArgumentException exception = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> new User(userId, role, firstName, lastName, email, passwordHash) | ||
| ); | ||
|
|
||
| @Test | ||
| public void constructorWithNegativeUserIdThrowsException() { | ||
| constructorTest(WRONG_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| assertEquals(expectedMessage, exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyRoleThrowsException() { | ||
| constructorTest(TEST_USER_ID, WRONG_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| void constructorCreatesValidUser() { | ||
| User user = new User(testUserId, testRole, testFirstName, | ||
| testLastName, testEmail, testPasswordHash); | ||
| assertEquals(testUserId, user.getUserId()); | ||
| assertEquals(testRole, user.getRole()); | ||
| assertEquals(testFirstName, user.getFirstName()); | ||
| assertEquals(testLastName, user.getLastName()); | ||
| assertEquals(testEmail, user.getEmail()); | ||
| assertEquals(testPasswordHash, user.getPasswordHash()); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyFirstNameThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, WRONG_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| void constructorWithNegativeUserIdThrowsException() { | ||
| constructorTest(-1, testRole, testFirstName, testLastName, | ||
| testEmail, testPasswordHash, "User ID must be positive"); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyLastNameThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, WRONG_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| void constructorWithNullRoleThrowsException() { | ||
| constructorTest(testUserId, null, testFirstName, testLastName, | ||
| testEmail, testPasswordHash, "Role cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyEmailThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, WRONG_EMAIL, TEST_PASSWORD_HASH, true); | ||
| void constructorWithEmptyRoleThrowsException() { | ||
| constructorTest(testUserId, "", testFirstName, testLastName, | ||
| testEmail, testPasswordHash, "Role cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyPasswordHashThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, WRONG_PASSWORD_HASH, true); | ||
| void constructorWithNullFirstNameThrowsException() { | ||
| constructorTest(testUserId, testRole, null, testLastName, | ||
| testEmail, testPasswordHash, "First name cannot be null or empty"); | ||
| } | ||
|
|
||
| private void getMethodComparer(User user, boolean negativeTest) { | ||
| if (user.getUserId() == TEST_USER_ID && | ||
| user.getRole().equals(TEST_ROLE) && | ||
| user.getFirstName().equals(TEST_FIRST_NAME) && | ||
| user.getLastName().equals(TEST_LAST_NAME) && | ||
| user.getEmail().equals(TEST_EMAIL) && | ||
| user.getPasswordHash() != null) { | ||
| assertFalse(negativeTest); | ||
| } | ||
| @Test | ||
| void constructorWithEmptyFirstNameThrowsException() { | ||
| constructorTest(testUserId, testRole, "", testLastName, | ||
| testEmail, testPasswordHash, "First name cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void getMethodsReturnCorrectInformation() { | ||
| User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH); | ||
| getMethodComparer(testUser, false); | ||
| void constructorWithNullLastNameThrowsException() { | ||
| constructorTest(testUserId, testRole, testFirstName, null, | ||
| testEmail, testPasswordHash, "Last name cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void verifyPasswordReturnsTrueForCorrectPassword() { | ||
| User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH); | ||
| assertTrue(testUser.verifyPassword(TEST_PASSWORD)); | ||
| void constructorWithEmptyLastNameThrowsException() { | ||
| constructorTest(testUserId, testRole, testFirstName, "", | ||
| testEmail, testPasswordHash, "Last name cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void verifyPasswordReturnsFalseForIncorrectPassword() { | ||
| User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH); | ||
| assertFalse(testUser.verifyPassword("wrongPassword")); | ||
| void constructorWithNullEmailThrowsException() { | ||
| constructorTest(testUserId, testRole, testFirstName, testLastName, | ||
| null, testPasswordHash, "Email cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void verifyPasswordReturnsFalseForNullPassword() { | ||
| User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH); | ||
| assertFalse(testUser.verifyPassword(null)); | ||
| void constructorWithEmptyEmailThrowsException() { | ||
| constructorTest(testUserId, testRole, testFirstName, testLastName, | ||
| "", testPasswordHash, "Email cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void verifyPasswordReturnsFalseForEmptyPassword() { | ||
| User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH); | ||
| assertFalse(testUser.verifyPassword("")); | ||
| void constructorWithNullPasswordHashThrowsException() { | ||
| constructorTest(testUserId, testRole, testFirstName, testLastName, | ||
| testEmail, null, "Password hash cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithNullRoleThrowsException() { | ||
| constructorTest(TEST_USER_ID, null, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| void constructorWithEmptyPasswordHashThrowsException() { | ||
| constructorTest(testUserId, testRole, testFirstName, testLastName, | ||
| testEmail, "", "Password hash cannot be null or empty"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void constructorWithNullFirstNameThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, null, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| void verifyPasswordReturnsTrueForCorrectPassword() { | ||
| User user = new User(testUserId, testRole, testFirstName, | ||
| testLastName, testEmail, testPasswordHash); | ||
|
|
||
| assertTrue(user.verifyPassword(testPassword)); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithNullLastNameThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, null, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| void verifyPasswordReturnsFalseForIncorrectPassword() { | ||
| User user = new User(testUserId, testRole, testFirstName, | ||
| testLastName, testEmail, testPasswordHash); | ||
|
|
||
| assertFalse(user.verifyPassword("wrongPassword")); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithNullEmailThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, null, TEST_PASSWORD_HASH, true); | ||
| void verifyPasswordReturnsFalseForNullPassword() { | ||
| User user = new User(testUserId, testRole, testFirstName, | ||
| testLastName, testEmail, testPasswordHash); | ||
|
|
||
| assertFalse(user.verifyPassword(null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithNullPasswordHashThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, null, true); | ||
| void verifyPasswordReturnsFalseForEmptyPassword() { | ||
| User user = new User(testUserId, testRole, testFirstName, | ||
| testLastName, testEmail, testPasswordHash); | ||
|
|
||
| assertFalse(user.verifyPassword("")); | ||
| } | ||
| } | ||
| } |