Implement UserManager with CRUD operations and authentication
parent
b643d2d687
commit
5dd01944e6
|
@ -5,10 +5,12 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import domain.exception.UserAlreadyExistsException;
|
import domain.exception.UserAlreadyExistsException;
|
||||||
|
import domain.exception.UserNotFoundException;
|
||||||
|
|
||||||
public class UserManager {
|
public class UserManager {
|
||||||
|
|
||||||
private ArrayList<User> users;
|
private ArrayList<User> users;
|
||||||
|
private User currentUser;
|
||||||
|
|
||||||
public UserManager() {
|
public UserManager() {
|
||||||
this.users = new ArrayList<>();
|
this.users = new ArrayList<>();
|
||||||
|
@ -31,9 +33,7 @@ public class UserManager {
|
||||||
throw new IllegalArgumentException("Passwords do not match!");
|
throw new IllegalArgumentException("Passwords do not match!");
|
||||||
|
|
||||||
String email = username + "@easymail.de";
|
String email = username + "@easymail.de";
|
||||||
|
if (findUserByUsername(email) != null)
|
||||||
for (User tempUser : users)
|
|
||||||
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(email))
|
|
||||||
throw new UserAlreadyExistsException("This email address is already taken!");
|
throw new UserAlreadyExistsException("This email address is already taken!");
|
||||||
|
|
||||||
int month = getMonthNumber(monthName);
|
int month = getMonthNumber(monthName);
|
||||||
|
@ -64,6 +64,62 @@ public class UserManager {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean removeUser(String username) throws UserNotFoundException {
|
||||||
|
if (username == null || username.trim().isEmpty())
|
||||||
|
throw new IllegalArgumentException("Username cannot be null or empty!");
|
||||||
|
|
||||||
|
User userToBeRemoved = findUserByUsername(username);
|
||||||
|
if (userToBeRemoved == null)
|
||||||
|
throw new UserNotFoundException("This email address is not found!");
|
||||||
|
|
||||||
|
users.remove(userToBeRemoved);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumberOfUsers() {
|
||||||
|
return users.size();
|
||||||
|
}
|
||||||
|
public boolean updateUser(String username, String firstName, String lastName, char[] password, char[] confirm) throws Exception {
|
||||||
|
User userToBeUpdated = findUserByUsername(username);
|
||||||
|
if (userToBeUpdated == null)
|
||||||
|
throw new UserNotFoundException("This email address is not found!");
|
||||||
|
|
||||||
|
if (firstName == null || lastName == null || password == null || confirm == null)
|
||||||
|
throw new IllegalArgumentException("Fields cannot be null!");
|
||||||
|
|
||||||
|
if (firstName.trim().isEmpty() || lastName.trim().isEmpty())
|
||||||
|
throw new IllegalArgumentException("First name and last name are required!");
|
||||||
|
|
||||||
|
if (!Arrays.equals(password, confirm))
|
||||||
|
throw new IllegalArgumentException("Passwords do not match!");
|
||||||
|
|
||||||
|
userToBeUpdated.setFirstname(firstName);
|
||||||
|
userToBeUpdated.setLastname(lastName);
|
||||||
|
|
||||||
|
char[] passwordCopy = Arrays.copyOf(password, password.length);
|
||||||
|
userToBeUpdated.getUsermail().setPassword(passwordCopy);
|
||||||
|
Arrays.fill(password, ' ');
|
||||||
|
Arrays.fill(confirm, ' ');
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getUserByUsername(String username) {
|
||||||
|
this.currentUser = findUserByUsername(username);
|
||||||
|
if (this.currentUser == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return currentUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
private User findUserByUsername(String username) {
|
||||||
|
for (User tempUser : users)
|
||||||
|
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(username))
|
||||||
|
return tempUser;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private int getMonthNumber(String txtMonth) {
|
private int getMonthNumber(String txtMonth) {
|
||||||
switch (txtMonth.toLowerCase()) {
|
switch (txtMonth.toLowerCase()) {
|
||||||
|
@ -82,5 +138,7 @@ public class UserManager {
|
||||||
default: return 0;
|
default: return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package domain.exception;
|
||||||
|
|
||||||
|
public class UserNotFoundException extends Exception {
|
||||||
|
public UserNotFoundException(String error) {
|
||||||
|
super(error);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package domain.unitTest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import domain.UserManager;
|
||||||
|
import domain.exception.UserNotFoundException;
|
||||||
|
|
||||||
|
class RemoveUserTest {
|
||||||
|
|
||||||
|
private UserManager userManager;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() throws Exception {
|
||||||
|
userManager = new UserManager();
|
||||||
|
userManager.addUser("Jane", "Doe", "janedoe", 1992, 20, "April", "securePass1".toCharArray(), "securePass1".toCharArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemoveUserSuccessfully() throws Exception {
|
||||||
|
boolean removed = userManager.removeUser("janedoe@easymail.de");
|
||||||
|
assertTrue(removed);
|
||||||
|
|
||||||
|
assertNull(userManager.getUserByUsername("janedoe@easymail.de"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemoveUserNotFound() {
|
||||||
|
assertThrows(UserNotFoundException.class, () -> {
|
||||||
|
userManager.removeUser("nonexistent@easymail.de");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemoveUserNullUsername() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.removeUser(null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemoveUserEmptyUsername() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.removeUser(" ");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
package domain.unitTest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import domain.User;
|
||||||
|
import domain.UserManager;
|
||||||
|
import domain.exception.UserNotFoundException;
|
||||||
|
|
||||||
|
class UpdateUserTest {
|
||||||
|
|
||||||
|
private UserManager userManager;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() throws Exception {
|
||||||
|
userManager = new UserManager();
|
||||||
|
userManager.addUser("John", "Doe", "johndoe", 1990, 15, "Mai", "password123".toCharArray(), "password123".toCharArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateUserSuccessfully() throws Exception {
|
||||||
|
boolean updated = userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
"Johnny",
|
||||||
|
"Doeman",
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
"newpass123".toCharArray()
|
||||||
|
);
|
||||||
|
assertTrue(updated);
|
||||||
|
|
||||||
|
User updatedUser = userManager.getUserByUsername("johndoe@easymail.de");
|
||||||
|
assertEquals("Johnny", updatedUser.getFirstname());
|
||||||
|
assertEquals("Doeman", updatedUser.getLastname());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateUserNotFound() {
|
||||||
|
assertThrows(UserNotFoundException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"unknown@easymail.de",
|
||||||
|
"Johnny",
|
||||||
|
"Doeman",
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
"newpass123".toCharArray()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateUserNullFields() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
null,
|
||||||
|
"Doeman",
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
"newpass123".toCharArray()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
"Johnny",
|
||||||
|
null,
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
"newpass123".toCharArray()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
"Johnny",
|
||||||
|
"Doeman",
|
||||||
|
null,
|
||||||
|
"newpass123".toCharArray()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
"Johnny",
|
||||||
|
"Doeman",
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateUserEmptyFirstnameOrLastname() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
" ",
|
||||||
|
"Doeman",
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
"newpass123".toCharArray()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
"Johnny",
|
||||||
|
" ",
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
"newpass123".toCharArray()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateUserPasswordMismatch() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
userManager.updateUser(
|
||||||
|
"johndoe@easymail.de",
|
||||||
|
"Johnny",
|
||||||
|
"Doeman",
|
||||||
|
"newpass123".toCharArray(),
|
||||||
|
"differentpass".toCharArray()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue