-
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.
test[User]: Add dependency for the Bcrypt, and add positive and Negat…
…ive JUnit tests
- Loading branch information
Fredrik Marjoni
committed
Feb 26, 2026
1 parent
00cb18b
commit 8bd6986
Showing
3 changed files
with
113 additions
and
2 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,5 +1,111 @@ | ||
| package edu.group5.app.model.user; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| 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 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); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorThrowsNoException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithNegativeUserIdThrowsException() { | ||
| constructorTest(WRONG_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyRoleThrowsException() { | ||
| constructorTest(TEST_USER_ID, WRONG_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyFirstNameThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, WRONG_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyLastNameThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, WRONG_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyEmailThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, WRONG_EMAIL, TEST_PASSWORD_HASH, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void constructorWithEmptyPasswordHashThrowsException() { | ||
| constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, WRONG_PASSWORD_HASH, true); | ||
| } | ||
|
|
||
| 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 | ||
| 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); | ||
| } | ||
|
|
||
| @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)); | ||
| } | ||
|
|
||
| @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")); | ||
| } | ||
|
|
||
| @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)); | ||
| } | ||
|
|
||
| @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("")); | ||
| } | ||
| } |